All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH XEN v8 00/29] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
@ 2016-01-15 13:22 ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 01/29] tools/libxc: Remove osdep indirection for xc_evtchn Ian Campbell
                     ` (28 more replies)
  2016-01-15 13:23 ` [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (7 subsequent siblings)
  8 siblings, 29 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

We intend to stabilise some parts of the libxenctrl interface by
splitting out some functionality into separate stable libraries.

This is the xen part of the first phase of that change.

This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
super-intro mail covering all of the related patch series and which
contains more details.

Ian Campbell (29):
  tools/libxc: Remove osdep indirection for xc_evtchn
  tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  tools: Arrange to check public headers for ANSI compatiblity
  tools/libxc: Remove osdep indirection for xc_gnt{shr,tab}
  tools: Refactor /dev/xen/gnt{dev,shr} wrappers into libxengnttab.
  tools/libxc: Remove osdep indirection for privcmd
  tools: Refactor hypercall calling wrappers into libxencall.
  tools/libxc: drop xc_map_foreign_bulk_compat wrappers
  tools: Remove xc_map_foreign_batch
  tools: Implement xc_map_foreign_range(s) in terms of common helper
  tools: Refactor foreign memory mapping into libxenforeignmemory
  tools/libs/foreignmemory: provide xenforeignmemory_unmap.
  tools/libs/foreignmemory: use size_t for size arguments.
  tools/libs/foreignmemory: Mention restrictions on fork in docs.
  tools/libs/foreignmemory: Support err == NULL to map.
  tools/libs/foreignmemory: pull array length argument to map forward
  tools/libs/evtchn: Review and update doc comments.
  tools/libs/evtchn: Use uint32_t for domid arguments
  tools/libs: Clean up hard tabs.
  tools/libs/gnttab: Extensive updates to API documentation.
  tools/libs/call: Update some log messages to not refer to xc.
  tools/libs/call: Describe return values and error semantics for
    xencall*
  tools/libs/call: Avoid xc_memalign in netbsd and solaris backends
  tools/libs/call: linux: touch newly allocated pages after madvise
    lockdown
  tools/libs/{call,evtchn}: Document requirements around forking.
  tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
  tools: Update CFLAGS for qemu-xen to allow it to use new libraries
  tools/libs/*: Introduce APIs to restrict handles to a specific domain.
  HACK: Update Config.mk to pull all the right bits from my xenbits
    trees

 .gitignore                                         |   9 +
 Config.mk                                          |  22 +-
 config/FreeBSD.mk                                  |   2 -
 config/NetBSD.mk                                   |   3 -
 config/NetBSDRump.mk                               |   1 -
 config/StdGNU.mk                                   |   1 -
 config/SunOS.mk                                    |   1 -
 stubdom/Makefile                                   |  62 +-
 tools/Makefile                                     |  15 +-
 tools/Rules.mk                                     |  44 +-
 tools/console/Makefile                             |   3 +-
 tools/console/daemon/io.c                          |  64 +-
 tools/console/daemon/utils.c                       |   1 -
 tools/console/daemon/utils.h                       |   1 +
 tools/libs/Makefile                                |   4 +
 tools/libs/call/Makefile                           |  71 ++
 tools/libs/call/buffer.c                           | 192 +++++
 tools/libs/call/core.c                             | 154 ++++
 tools/libs/call/freebsd.c                          | 126 +++
 tools/libs/call/include/xencall.h                  | 161 ++++
 tools/libs/call/libxencall.map                     |  21 +
 tools/libs/call/linux.c                            | 134 +++
 tools/libs/call/minios.c                           |  81 ++
 tools/libs/call/netbsd.c                           | 121 +++
 tools/libs/call/private.h                          |  68 ++
 tools/libs/call/solaris.c                          |  97 +++
 tools/libs/evtchn/Makefile                         |  71 ++
 tools/libs/evtchn/core.c                           |  79 ++
 tools/libs/evtchn/freebsd.c                        | 138 ++++
 tools/libs/evtchn/include/xenevtchn.h              | 199 +++++
 tools/libs/evtchn/libxenevtchn.map                 |  21 +
 tools/libs/evtchn/linux.c                          | 140 ++++
 tools/libs/evtchn/minios.c                         | 266 ++++++
 tools/libs/evtchn/netbsd.c                         | 147 ++++
 tools/libs/evtchn/private.h                        |  25 +
 tools/libs/evtchn/solaris.c                        | 135 +++
 tools/libs/foreignmemory/Makefile                  |  71 ++
 tools/libs/foreignmemory/compat.c                  |  72 ++
 tools/libs/foreignmemory/core.c                    | 123 +++
 tools/libs/foreignmemory/freebsd.c                 | 115 +++
 .../libs/foreignmemory/include/xenforeignmemory.h  | 150 ++++
 tools/libs/foreignmemory/libxenforeignmemory.map   |  11 +
 tools/libs/foreignmemory/linux.c                   | 283 +++++++
 tools/libs/foreignmemory/minios.c                  |  69 ++
 tools/libs/foreignmemory/netbsd.c                  | 111 +++
 tools/libs/foreignmemory/private.h                 |  54 ++
 tools/libs/foreignmemory/solaris.c                 | 109 +++
 tools/libs/gnttab/Makefile                         |  73 ++
 tools/libs/gnttab/gntshr_core.c                    | 103 +++
 tools/libs/gnttab/gntshr_unimp.c                   |  62 ++
 tools/libs/gnttab/gnttab_core.c                    | 131 +++
 tools/libs/gnttab/gnttab_unimp.c                   |  89 ++
 tools/libs/gnttab/include/xengnttab.h              | 415 ++++++++++
 tools/libs/gnttab/libxengnttab.map                 |  27 +
 tools/libs/gnttab/linux.c                          | 335 ++++++++
 tools/libs/gnttab/minios.c                         | 117 +++
 tools/libs/gnttab/private.h                        |  47 ++
 tools/libs/toollog/Makefile                        |   5 +-
 tools/libs/toollog/include/xentoollog.h            |  10 +
 tools/libs/toollog/xtl_core.c                      |  10 +
 tools/libs/toollog/xtl_logger_stdio.c              |  12 +-
 tools/libvchan/Makefile                            |   8 +-
 tools/libvchan/init.c                              |  46 +-
 tools/libvchan/io.c                                |  20 +-
 tools/libvchan/libxenvchan.h                       |   9 +-
 tools/libxc/Makefile                               |  41 +-
 tools/libxc/include/xenctrl.h                      | 312 +------
 tools/libxc/include/xenctrl_compat.h               | 132 +++
 tools/libxc/include/xenctrlosdep.h                 | 172 ----
 tools/libxc/include/xenguest.h                     |  14 +-
 tools/libxc/xc_altp2m.c                            |  64 +-
 tools/libxc/xc_domain.c                            | 105 +--
 tools/libxc/xc_evtchn.c                            |  54 +-
 tools/libxc/xc_evtchn_compat.c                     |  75 ++
 tools/libxc/xc_flask.c                             |   8 +-
 tools/libxc/xc_foreign_memory.c                    | 119 +--
 tools/libxc/xc_freebsd_osdep.c                     | 407 ----------
 tools/libxc/xc_gnttab.c                            |  93 +--
 tools/libxc/xc_gnttab_compat.c                     | 111 +++
 tools/libxc/xc_hcall_buf.c                         | 138 +---
 tools/libxc/xc_kexec.c                             |  36 +-
 tools/libxc/xc_linux_osdep.c                       | 904 ---------------------
 tools/libxc/xc_minios.c                            | 482 -----------
 tools/libxc/xc_misc.c                              |  79 +-
 tools/libxc/xc_netbsd.c                            | 345 --------
 tools/libxc/xc_private.c                           | 277 +------
 tools/libxc/xc_private.h                           | 105 +--
 tools/libxc/xc_solaris.c                           | 305 -------
 tools/libxc/xc_sr_restore.c                        |   8 +-
 tools/libxc/xc_sr_save.c                           |   6 +-
 tools/libxc/xc_suspend.c                           |  18 +-
 tools/libxc/xc_tmem.c                              |   7 +-
 tools/libxc/xc_vm_event.c                          |  20 +-
 tools/libxc/xenctrl_osdep_ENOSYS.c                 | 210 -----
 tools/libxc/xg_private.h                           |   3 +-
 tools/libxl/Makefile                               |   5 +-
 tools/libxl/libxl.c                                |   2 +-
 tools/libxl/libxl_dom_suspend.c                    |   4 +-
 tools/libxl/libxl_event.c                          |  14 +-
 tools/libxl/libxl_internal.h                       |   6 +-
 tools/misc/Makefile                                |  11 +-
 tools/misc/xen-hptool.c                            |  13 +-
 tools/misc/xen-lowmemd.c                           |  15 +-
 tools/misc/xen-mfndump.c                           |   1 +
 tools/ocaml/libs/eventchn/Makefile                 |   4 +-
 tools/ocaml/libs/eventchn/xeneventchn_stubs.c      |  20 +-
 tools/ocaml/libs/xc/xenctrl.ml                     |   2 -
 tools/ocaml/libs/xc/xenctrl.mli                    |   1 -
 tools/ocaml/libs/xc/xenctrl_stubs.c                |   8 +-
 tools/ocaml/xenstored/domains.ml                   |   6 +-
 tools/ocaml/xenstored/xenstored.ml                 |   5 +-
 tools/python/setup.py                              |   7 +-
 tools/python/xen/lowlevel/xc/xc.c                  |   2 +
 tools/tests/mce-test/tools/xen-mceinj.c            |   1 +
 tools/tests/mem-sharing/memshrtool.c               |   1 +
 tools/tests/xen-access/Makefile                    |   3 +-
 tools/tests/xen-access/xen-access.c                |  21 +-
 tools/xcutils/Makefile                             |   4 +-
 tools/xenmon/Makefile                              |   2 +
 tools/xenmon/xenbaked.c                            |  26 +-
 tools/xenpaging/Makefile                           |   4 +-
 tools/xenpaging/pagein.c                           |   1 -
 tools/xenpaging/xenpaging.c                        |  33 +-
 tools/xenpaging/xenpaging.h                        |   5 +-
 tools/xenstore/Makefile                            |   7 +-
 tools/xenstore/xenstored_core.c                    |   7 +-
 tools/xenstore/xenstored_core.h                    |   5 +-
 tools/xenstore/xenstored_domain.c                  |  49 +-
 tools/xenstore/xenstored_minios.c                  |   5 +-
 tools/xentrace/Makefile                            |   5 +-
 tools/xentrace/xenctx.c                            |   3 +-
 tools/xentrace/xentrace.c                          |  17 +-
 132 files changed, 6054 insertions(+), 4288 deletions(-)
 create mode 100644 tools/libs/call/Makefile
 create mode 100644 tools/libs/call/buffer.c
 create mode 100644 tools/libs/call/core.c
 create mode 100644 tools/libs/call/freebsd.c
 create mode 100644 tools/libs/call/include/xencall.h
 create mode 100644 tools/libs/call/libxencall.map
 create mode 100644 tools/libs/call/linux.c
 create mode 100644 tools/libs/call/minios.c
 create mode 100644 tools/libs/call/netbsd.c
 create mode 100644 tools/libs/call/private.h
 create mode 100644 tools/libs/call/solaris.c
 create mode 100644 tools/libs/evtchn/Makefile
 create mode 100644 tools/libs/evtchn/core.c
 create mode 100644 tools/libs/evtchn/freebsd.c
 create mode 100644 tools/libs/evtchn/include/xenevtchn.h
 create mode 100644 tools/libs/evtchn/libxenevtchn.map
 create mode 100644 tools/libs/evtchn/linux.c
 create mode 100644 tools/libs/evtchn/minios.c
 create mode 100644 tools/libs/evtchn/netbsd.c
 create mode 100644 tools/libs/evtchn/private.h
 create mode 100644 tools/libs/evtchn/solaris.c
 create mode 100644 tools/libs/foreignmemory/Makefile
 create mode 100644 tools/libs/foreignmemory/compat.c
 create mode 100644 tools/libs/foreignmemory/core.c
 create mode 100644 tools/libs/foreignmemory/freebsd.c
 create mode 100644 tools/libs/foreignmemory/include/xenforeignmemory.h
 create mode 100644 tools/libs/foreignmemory/libxenforeignmemory.map
 create mode 100644 tools/libs/foreignmemory/linux.c
 create mode 100644 tools/libs/foreignmemory/minios.c
 create mode 100644 tools/libs/foreignmemory/netbsd.c
 create mode 100644 tools/libs/foreignmemory/private.h
 create mode 100644 tools/libs/foreignmemory/solaris.c
 create mode 100644 tools/libs/gnttab/Makefile
 create mode 100644 tools/libs/gnttab/gntshr_core.c
 create mode 100644 tools/libs/gnttab/gntshr_unimp.c
 create mode 100644 tools/libs/gnttab/gnttab_core.c
 create mode 100644 tools/libs/gnttab/gnttab_unimp.c
 create mode 100644 tools/libs/gnttab/include/xengnttab.h
 create mode 100644 tools/libs/gnttab/libxengnttab.map
 create mode 100644 tools/libs/gnttab/linux.c
 create mode 100644 tools/libs/gnttab/minios.c
 create mode 100644 tools/libs/gnttab/private.h
 create mode 100644 tools/libxc/include/xenctrl_compat.h
 delete mode 100644 tools/libxc/include/xenctrlosdep.h
 create mode 100644 tools/libxc/xc_evtchn_compat.c
 delete mode 100644 tools/libxc/xc_freebsd_osdep.c
 create mode 100644 tools/libxc/xc_gnttab_compat.c
 delete mode 100644 tools/libxc/xc_linux_osdep.c
 delete mode 100644 tools/libxc/xenctrl_osdep_ENOSYS.c

-- 
2.1.4

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 01/29] tools/libxc: Remove osdep indirection for xc_evtchn
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn Ian Campbell
                     ` (27 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

The alternative backend (a xen-api/xapi shim) is no longer around and
so this stuff is now just baggage which is getting in the way of
refactoring libxenctrl.

Note that the intention is to move this into a separate library
shortly.

Nested virt probably suffices for this use case now.

One incorrect instance of using xc_interface where xc_evtchn (in ocaml
stubs) is removed, this used to work because they were typedefs to the
same struct, but is no longer permitted.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxc/include/xenctrl.h                 |  2 +-
 tools/libxc/include/xenctrlosdep.h            | 16 -----
 tools/libxc/xc_evtchn.c                       | 45 --------------
 tools/libxc/xc_freebsd_osdep.c                | 79 ++++++++----------------
 tools/libxc/xc_linux_osdep.c                  | 76 +++++++++--------------
 tools/libxc/xc_minios.c                       | 63 ++++++++-----------
 tools/libxc/xc_netbsd.c                       | 72 ++++++++--------------
 tools/libxc/xc_private.c                      | 37 +++++++++---
 tools/libxc/xc_private.h                      | 17 ++++++
 tools/libxc/xc_solaris.c                      | 71 ++++++++--------------
 tools/libxc/xenctrl_osdep_ENOSYS.c            | 87 ---------------------------
 tools/ocaml/libs/eventchn/xeneventchn_stubs.c |  4 +-
 12 files changed, 180 insertions(+), 389 deletions(-)

diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 01a6dda..881dcd5 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -117,7 +117,7 @@
  */
 
 typedef struct xc_interface_core xc_interface;
-typedef struct xc_interface_core xc_evtchn;
+typedef struct xenevtchn_handle xc_evtchn;
 typedef struct xc_interface_core xc_gnttab;
 typedef struct xc_interface_core xc_gntshr;
 
diff --git a/tools/libxc/include/xenctrlosdep.h b/tools/libxc/include/xenctrlosdep.h
index 5121d9b..89564e1 100644
--- a/tools/libxc/include/xenctrlosdep.h
+++ b/tools/libxc/include/xenctrlosdep.h
@@ -51,7 +51,6 @@
 
 enum xc_osdep_type {
     XC_OSDEP_PRIVCMD,
-    XC_OSDEP_EVTCHN,
     XC_OSDEP_GNTTAB,
     XC_OSDEP_GNTSHR,
 };
@@ -90,21 +89,6 @@ struct xc_osdep_ops
                                         int nentries);
         } privcmd;
         struct {
-            int (*fd)(xc_evtchn *xce, xc_osdep_handle h);
-
-            int (*notify)(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port);
-
-            evtchn_port_or_error_t (*bind_unbound_port)(xc_evtchn *xce, xc_osdep_handle h, int domid);
-            evtchn_port_or_error_t (*bind_interdomain)(xc_evtchn *xce, xc_osdep_handle h, int domid,
-                                                       evtchn_port_t remote_port);
-            evtchn_port_or_error_t (*bind_virq)(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq);
-
-            int (*unbind)(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port);
-
-            evtchn_port_or_error_t (*pending)(xc_evtchn *xce, xc_osdep_handle h);
-            int (*unmask)(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port);
-        } evtchn;
-        struct {
 #define XC_GRANT_MAP_SINGLE_DOMAIN 0x1
             void *(*grant_map)(xc_gnttab *xcg, xc_osdep_handle h,
                                uint32_t count, int flags, int prot,
diff --git a/tools/libxc/xc_evtchn.c b/tools/libxc/xc_evtchn.c
index 15f0580..ae2fe1a 100644
--- a/tools/libxc/xc_evtchn.c
+++ b/tools/libxc/xc_evtchn.c
@@ -77,51 +77,6 @@ int xc_evtchn_status(xc_interface *xch, xc_evtchn_status_t *status)
                         sizeof(*status), 1);
 }
 
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->ops->u.evtchn.fd(xce, xce->ops_handle);
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    return xce->ops->u.evtchn.notify(xce, xce->ops_handle, port);
-}
-
-evtchn_port_or_error_t
-xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    return xce->ops->u.evtchn.bind_unbound_port(xce, xce->ops_handle, domid);
-}
-
-evtchn_port_or_error_t
-xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                           evtchn_port_t remote_port)
-{
-    return xce->ops->u.evtchn.bind_interdomain(xce, xce->ops_handle, domid, remote_port);
-}
-
-evtchn_port_or_error_t
-xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    return xce->ops->u.evtchn.bind_virq(xce, xce->ops_handle, virq);
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    return xce->ops->u.evtchn.unbind(xce, xce->ops_handle, port);
-}
-
-evtchn_port_or_error_t
-xc_evtchn_pending(xc_evtchn *xce)
-{
-    return xce->ops->u.evtchn.pending(xce, xce->ops_handle);
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    return xce->ops->u.evtchn.unmask(xce, xce->ops_handle, port);
-}
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c
index 4d31a1e..4323e16 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libxc/xc_freebsd_osdep.c
@@ -252,34 +252,32 @@ static struct xc_osdep_ops freebsd_privcmd_ops = {
 };
 
 /*-------------------------- Evtchn device interface -------------------------*/
-static xc_osdep_handle
-freebsd_evtchn_open(xc_evtchn *xce)
+int osdep_evtchn_open(xc_evtchn *xce)
 {
     int fd = open(EVTCHN_DEV, O_RDWR);
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
-
-    return (xc_osdep_handle)fd;
+        return -1;
+    xce->fd = fd;
+    return 0;
 }
 
-static int
-freebsd_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
+int osdep_evtchn_close(xc_evtchn *xce)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
 }
 
-static int
-freebsd_evtchn_fd(xc_evtchn *xce, xc_osdep_handle h)
+int xc_evtchn_fd(xc_evtchn *xce)
 {
-    return (int)h;
+    return xce->fd;
 }
 
 /*------------------------------ Evtchn interface ----------------------------*/
-static int
-freebsd_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_notify notify;
 
     notify.port = port;
@@ -287,10 +285,9 @@ freebsd_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-static evtchn_port_or_error_t
-freebsd_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
+evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
 {
-    int ret, fd = (int)h;
+    int ret, fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
 
     bind.remote_domain = domid;
@@ -299,11 +296,10 @@ freebsd_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
     return ( ret == 0 ) ? bind.port : ret;
 }
 
-static evtchn_port_or_error_t
-freebsd_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
-                                evtchn_port_t remote_port)
+evtchn_port_or_error_t
+xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid, evtchn_port_t remote_port)
 {
-    int ret, fd = (int)h;
+    int ret, fd = xce->fd;
     struct ioctl_evtchn_bind_interdomain bind;
 
     bind.remote_domain = domid;
@@ -313,10 +309,9 @@ freebsd_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
     return ( ret == 0 ) ? bind.port : ret;
 }
 
-static evtchn_port_or_error_t
-freebsd_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
+evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
 {
-    int ret, fd = (int)h;
+    int ret, fd = xce->fd;
     struct ioctl_evtchn_bind_virq bind;
 
     bind.virq = virq;
@@ -325,10 +320,9 @@ freebsd_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
     return ( ret == 0 ) ? bind.port : ret;
 }
 
-static int
-freebsd_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_unbind unbind;
 
     unbind.port = port;
@@ -336,10 +330,9 @@ freebsd_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
     return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
 }
 
-static evtchn_port_or_error_t
-freebsd_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
+evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     evtchn_port_t port;
 
     if ( read(fd, &port, sizeof(port)) != sizeof(port) )
@@ -348,33 +341,15 @@ freebsd_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
     return port;
 }
 
-static int
-freebsd_evtchn_unmask(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
 
     if ( write(fd, &port, sizeof(port)) != sizeof(port) )
         return -1;
     return 0;
 }
 
-/*----------------------------- Evtchn handlers ------------------------------*/
-static struct xc_osdep_ops freebsd_evtchn_ops = {
-    .open = &freebsd_evtchn_open,
-    .close = &freebsd_evtchn_close,
-
-    .u.evtchn = {
-        .fd = &freebsd_evtchn_fd,
-        .notify = &freebsd_evtchn_notify,
-        .bind_unbound_port = &freebsd_evtchn_bind_unbound_port,
-        .bind_interdomain = &freebsd_evtchn_bind_interdomain,
-        .bind_virq = &freebsd_evtchn_bind_virq,
-        .unbind = &freebsd_evtchn_unbind,
-        .pending = &freebsd_evtchn_pending,
-        .unmask = &freebsd_evtchn_unmask,
-    },
-};
-
 /*---------------------------- FreeBSD interface -----------------------------*/
 static struct xc_osdep_ops *
 freebsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
@@ -383,8 +358,6 @@ freebsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
     {
     case XC_OSDEP_PRIVCMD:
         return &freebsd_privcmd_ops;
-    case XC_OSDEP_EVTCHN:
-        return &freebsd_evtchn_ops;
     default:
         return NULL;
     }
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index c3a3a14..90544fb 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -38,10 +38,9 @@
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
 
+#include "xc_private.h"
+
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
-#define ERROR(_m, _a...)  xc_osdep_log(xch,XTL_ERROR,XC_INTERNAL_ERROR,_m , ## _a )
-#define PERROR(_m, _a...) xc_osdep_log(xch,XTL_ERROR,XC_INTERNAL_ERROR,_m \
-                  " (%d = %s)", ## _a , errno, xc_strerror(xch, errno))
 
 static xc_osdep_handle linux_privcmd_open(xc_interface *xch)
 {
@@ -462,29 +461,31 @@ static struct xc_osdep_ops linux_privcmd_ops = {
 
 #define DEVXEN "/dev/xen/"
 
-static xc_osdep_handle linux_evtchn_open(xc_evtchn *xce)
+int osdep_evtchn_open(xc_evtchn *xce)
 {
     int fd = open(DEVXEN "evtchn", O_RDWR);
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
-
-    return (xc_osdep_handle)fd;
+        return -1;
+    xce->fd = fd;
+    return 0;
 }
 
-static int linux_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
+int osdep_evtchn_close(xc_evtchn *xce)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
 }
 
-static int linux_evtchn_fd(xc_evtchn *xce, xc_osdep_handle h)
+int xc_evtchn_fd(xc_evtchn *xce)
 {
-    return (int)h;
+    return xce->fd;
 }
 
-static int linux_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_notify notify;
 
     notify.port = port;
@@ -492,10 +493,9 @@ static int linux_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-static evtchn_port_or_error_t
-linux_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
+evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
 
     bind.remote_domain = domid;
@@ -503,11 +503,10 @@ linux_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
     return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
 }
 
-static evtchn_port_or_error_t
-linux_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
-                              evtchn_port_t remote_port)
+evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                                                  evtchn_port_t remote_port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_interdomain bind;
 
     bind.remote_domain = domid;
@@ -516,10 +515,9 @@ linux_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
     return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
 }
 
-static evtchn_port_or_error_t
-linux_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
+evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_virq bind;
 
     bind.virq = virq;
@@ -527,9 +525,9 @@ linux_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
     return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
 }
 
-static int linux_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_unbind unbind;
 
     unbind.port = port;
@@ -537,9 +535,9 @@ static int linux_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t
     return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
 }
 
-static evtchn_port_or_error_t linux_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
+evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     evtchn_port_t port;
 
     if ( read(fd, &port, sizeof(port)) != sizeof(port) )
@@ -548,31 +546,15 @@ static evtchn_port_or_error_t linux_evtchn_pending(xc_evtchn *xce, xc_osdep_hand
     return port;
 }
 
-static int linux_evtchn_unmask(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
 
     if ( write(fd, &port, sizeof(port)) != sizeof(port) )
         return -1;
     return 0;
 }
 
-static struct xc_osdep_ops linux_evtchn_ops = {
-    .open = &linux_evtchn_open,
-    .close = &linux_evtchn_close,
-
-    .u.evtchn = {
-        .fd = &linux_evtchn_fd,
-        .notify = &linux_evtchn_notify,
-        .bind_unbound_port = &linux_evtchn_bind_unbound_port,
-        .bind_interdomain = &linux_evtchn_bind_interdomain,
-        .bind_virq = &linux_evtchn_bind_virq,
-        .unbind = &linux_evtchn_unbind,
-        .pending = &linux_evtchn_pending,
-        .unmask = &linux_evtchn_unmask,
-    },
-};
-
 static xc_osdep_handle linux_gnttab_open(xc_gnttab *xcg)
 {
     int fd = open(DEVXEN "gntdev", O_RDWR);
@@ -876,8 +858,6 @@ static struct xc_osdep_ops *linux_osdep_init(xc_interface *xch, enum xc_osdep_ty
     {
     case XC_OSDEP_PRIVCMD:
         return &linux_privcmd_ops;
-    case XC_OSDEP_EVTCHN:
-        return &linux_evtchn_ops;
     case XC_OSDEP_GNTTAB:
         return &linux_gnttab_ops;
     case XC_OSDEP_GNTSHR:
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index 4f6498f..4ace1b5 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -219,20 +219,23 @@ static void port_dealloc(struct evtchn_port_info *port_info) {
     free(port_info);
 }
 
-static xc_osdep_handle minios_evtchn_open(xc_evtchn *xce)
+int osdep_evtchn_open(xc_evtchn *xce)
 {
     int fd = alloc_fd(FTYPE_EVTCHN);
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
     LIST_INIT(&files[fd].evtchn.ports);
+    xce->fd = fd;
     printf("evtchn_open() -> %d\n", fd);
-    return (xc_osdep_handle)fd;
+    return 0;
 }
 
-static int minios_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
+int osdep_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
 }
 
 void minios_evtchn_close_fd(int fd)
@@ -244,12 +247,12 @@ void minios_evtchn_close_fd(int fd)
     files[fd].type = FTYPE_NONE;
 }
 
-static int minios_evtchn_fd(xc_evtchn *xce, xc_osdep_handle h)
+int xc_evtchn_fd(xc_evtchn *xce)
 {
-    return (int)h;
+    return xce->fd;
 }
 
-static int minios_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
 {
     int ret;
 
@@ -281,9 +284,9 @@ static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
     wake_up(&event_queue);
 }
 
-static evtchn_port_or_error_t minios_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
+evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct evtchn_port_info *port_info;
     int ret;
     evtchn_port_t port;
@@ -308,10 +311,10 @@ static evtchn_port_or_error_t minios_evtchn_bind_unbound_port(xc_evtchn *xce, xc
     return port;
 }
 
-static evtchn_port_or_error_t minios_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
-    evtchn_port_t remote_port)
+evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                                                  evtchn_port_t remote_port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct evtchn_port_info *port_info;
     evtchn_port_t local_port;
     int ret;
@@ -336,9 +339,9 @@ static evtchn_port_or_error_t minios_evtchn_bind_interdomain(xc_evtchn *xce, xc_
     return local_port;
 }
 
-static int minios_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct evtchn_port_info *port_info;
 
     LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
@@ -352,9 +355,9 @@ static int minios_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t
     return -1;
 }
 
-static evtchn_port_or_error_t minios_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
+evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct evtchn_port_info *port_info;
     evtchn_port_t port;
 
@@ -377,9 +380,9 @@ static evtchn_port_or_error_t minios_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_h
     return port;
 }
 
-static evtchn_port_or_error_t minios_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
+evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct evtchn_port_info *port_info;
     unsigned long flags;
     evtchn_port_t ret = -1;
@@ -402,28 +405,12 @@ static evtchn_port_or_error_t minios_evtchn_pending(xc_evtchn *xce, xc_osdep_han
     return ret;
 }
 
-static int minios_evtchn_unmask(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
 {
     unmask_evtchn(port);
     return 0;
 }
 
-static struct xc_osdep_ops minios_evtchn_ops = {
-    .open = &minios_evtchn_open,
-    .close = &minios_evtchn_close,
-
-    .u.evtchn = {
-        .fd = &minios_evtchn_fd,
-        .notify = &minios_evtchn_notify,
-        .bind_unbound_port = &minios_evtchn_bind_unbound_port,
-        .bind_interdomain = &minios_evtchn_bind_interdomain,
-        .bind_virq = &minios_evtchn_bind_virq,
-        .unbind = &minios_evtchn_unbind,
-        .pending = &minios_evtchn_pending,
-        .unmask = &minios_evtchn_unmask,
-   },
-};
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush)
 {
@@ -523,8 +510,6 @@ static struct xc_osdep_ops *minios_osdep_init(xc_interface *xch, enum xc_osdep_t
     {
     case XC_OSDEP_PRIVCMD:
         return &minios_privcmd_ops;
-    case XC_OSDEP_EVTCHN:
-        return &minios_evtchn_ops;
     case XC_OSDEP_GNTTAB:
         return &minios_gnttab_ops;
     default:
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index 54f1d7b..b1b828f 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -225,29 +225,31 @@ static struct xc_osdep_ops netbsd_privcmd_ops = {
 
 #define EVTCHN_DEV_NAME  "/dev/xenevt"
 
-static xc_osdep_handle netbsd_evtchn_open(xc_evtchn *xce)
+int osdep_evtchn_open(xc_evtchn *xce)
 {
     int fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
-
-    return (xc_osdep_handle)fd;
+        return -1;
+    xce->fd = fd;
+    return 0;
 }
 
-static int netbsd_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
+int osdep_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
 }
 
-static int netbsd_evtchn_fd(xc_evtchn *xce, xc_osdep_handle h)
+int xc_evtchn_fd(xc_evtchn *xce)
 {
-    return (int)h;
+    return xce->fd;
 }
 
-static int netbsd_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_notify notify;
 
     notify.port = port;
@@ -255,10 +257,9 @@ static int netbsd_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-static evtchn_port_or_error_t
-netbsd_evtchn_bind_unbound_port(xc_evtchn * xce, xc_osdep_handle h, int domid)
+evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn * xce, int domid)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
     int ret;
 
@@ -271,11 +272,10 @@ netbsd_evtchn_bind_unbound_port(xc_evtchn * xce, xc_osdep_handle h, int domid)
 	return -1;
 }
 
-static evtchn_port_or_error_t
-netbsd_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
-                               evtchn_port_t remote_port)
+evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                                                  evtchn_port_t remote_port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_interdomain bind;
     int ret;
 
@@ -289,9 +289,9 @@ netbsd_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
 	return -1;
 }
 
-static int netbsd_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_unbind unbind;
 
     unbind.port = port;
@@ -299,10 +299,9 @@ static int netbsd_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t
     return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
 }
 
-static evtchn_port_or_error_t
-netbsd_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
+evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_virq bind;
     int err;
 
@@ -315,10 +314,9 @@ netbsd_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
 	return bind.port;
 }
 
-static evtchn_port_or_error_t
-netbsd_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
+evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     evtchn_port_t port;
 
     if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
@@ -327,28 +325,12 @@ netbsd_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
     return port;
 }
 
-static int netbsd_evtchn_unmask(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     return write_exact(fd, (char *)&port, sizeof(port));
 }
 
-static struct xc_osdep_ops netbsd_evtchn_ops = {
-    .open = &netbsd_evtchn_open,
-    .close = &netbsd_evtchn_close,
-
-    .u.evtchn = {
-         .fd = &netbsd_evtchn_fd,
-         .notify = &netbsd_evtchn_notify,
-         .bind_unbound_port = &netbsd_evtchn_bind_unbound_port,
-         .bind_interdomain = &netbsd_evtchn_bind_interdomain,
-         .bind_virq = &netbsd_evtchn_bind_virq,
-         .unbind = &netbsd_evtchn_unbind,
-         .pending = &netbsd_evtchn_pending,
-         .unmask = &netbsd_evtchn_unmask,
-    },
-};
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
@@ -395,8 +377,6 @@ static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_t
     {
     case XC_OSDEP_PRIVCMD:
         return &netbsd_privcmd_ops;
-    case XC_OSDEP_EVTCHN:
-        return &netbsd_evtchn_ops;
     default:
         return NULL;
     }
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index 6c0c0d6..cb79f22 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -119,7 +119,6 @@ static const char *xc_osdep_type_name(enum xc_osdep_type type)
     switch ( type )
     {
     case XC_OSDEP_PRIVCMD: return "privcmd";
-    case XC_OSDEP_EVTCHN:  return "evtchn";
     case XC_OSDEP_GNTTAB:  return "gnttab";
     case XC_OSDEP_GNTSHR:  return "gntshr";
     }
@@ -252,20 +251,44 @@ int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
     return xch->ops->u.privcmd.hypercall(xch, xch->ops_handle, hypercall);
 }
 
-xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
-                             unsigned open_flags)
+xc_evtchn *xc_evtchn_open(xentoollog_logger *logger, unsigned open_flags)
 {
-    xc_evtchn *xce;
+    xc_evtchn *xce = malloc(sizeof(*xce));
+    int rc;
+
+    if (!xce) return NULL;
+
+    xce->fd = -1;
+    xce->logger = logger;
+    xce->logger_tofree  = NULL;
 
-    xce = xc_interface_open_common(logger, NULL, open_flags,
-                                   XC_OSDEP_EVTCHN);
+    if (!xce->logger) {
+        xce->logger = xce->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xce->logger) goto err;
+    }
+
+    rc = osdep_evtchn_open(xce);
+    if ( rc  < 0 ) goto err;
 
     return xce;
+
+err:
+    osdep_evtchn_close(xce);
+    xtl_logger_destroy(xce->logger_tofree);
+    free(xce);
+    return NULL;
 }
 
 int xc_evtchn_close(xc_evtchn *xce)
 {
-    return xc_interface_close_common(xce);
+    int rc;
+
+    rc = osdep_evtchn_close(xce);
+    xtl_logger_destroy(xce->logger_tofree);
+    free(xce);
+    return rc;
 }
 
 xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index f603c15..f4d155a 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -123,6 +123,13 @@ struct xc_interface_core {
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
 };
 
+struct xenevtchn_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    int fd;
+};
+int osdep_evtchn_open(xc_evtchn *xce);
+int osdep_evtchn_close(xc_evtchn *xce);
+
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
@@ -446,3 +453,13 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param,
                          uint32_t *port);
 
 #endif /* __XC_PRIVATE_H__ */
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 7e5d847..088228a 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -194,33 +194,36 @@ static struct xc_osdep_ops solaris_privcmd_ops = {
     },
 };
 
-static xc_osdep_handle solaris_evtchn_open(xc_evtchn *xce)
+int osdep_evtchn_open(xc_evtchn *xce)
 {
     int fd;
 
     if ( (fd = open("/dev/xen/evtchn", O_RDWR)) == -1 )
     {
         PERROR("Could not open event channel interface");
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
     }
 
-    return (xc_osdep_handle)fd;
+    xce->fd = fd;
+    return 0;
 }
 
-static int solaris_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
+int osdep_evtchn_close(xc_evtchn *xce)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
 }
 
-static int solaris_evtchn_fd(xc_evtchn *xce, xc_osdep_handle h)
+int xc_evtchn_fd(xc_evtchn *xce)
 {
-    return (int)h;
+    return xce->fd;
 }
 
-static int solaris_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_notify notify;
 
     notify.port = port;
@@ -228,10 +231,9 @@ static int solaris_evtchn_notify(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-static evtchn_port_or_error_t
-solaris_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
+evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
 
     bind.remote_domain = domid;
@@ -239,11 +241,10 @@ solaris_evtchn_bind_unbound_port(xc_evtchn *xce, xc_osdep_handle h, int domid)
     return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
 }
 
-evtchn_port_or_error_t
-solaris_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
-                           evtchn_port_t remote_port)
+evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                                                  evtchn_port_t remote_port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_interdomain bind;
 
     bind.remote_domain = domid;
@@ -252,10 +253,9 @@ solaris_evtchn_bind_interdomain(xc_evtchn *xce, xc_osdep_handle h, int domid,
     return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
 }
 
-static evtchn_port_or_error_t
-solaris_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
+evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_bind_virq bind;
 
     bind.virq = virq;
@@ -263,9 +263,9 @@ solaris_evtchn_bind_virq(xc_evtchn *xce, xc_osdep_handle h, unsigned int virq)
     return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
 }
 
-static int solaris_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_t port)
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     struct ioctl_evtchn_unbind unbind;
 
     unbind.port = port;
@@ -273,10 +273,9 @@ static int solaris_evtchn_unbind(xc_evtchn *xce, xc_osdep_handle h, evtchn_port_
     return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
 }
 
-static evtchn_port_or_error_t
-solaris_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
+evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     evtchn_port_t port;
 
     if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
@@ -285,28 +284,12 @@ solaris_evtchn_pending(xc_evtchn *xce, xc_osdep_handle h)
     return port;
 }
 
-static int solaris_evtchn_unmask(xc_evtchn *xce, xc_osdep_handle h,evtchn_port_t port)
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
 {
-    int fd = (int)h;
+    int fd = xce->fd;
     return write_exact(fd, (char *)&port, sizeof(port));
 }
 
-static struct xc_osdep_ops solaris_evtchn_ops = {
-    .open = &solaris_evtchn_open,
-    .close = &solaris_evtchn_close,
-
-    .u.evtchn = {
-        .fd = &solaris_evtchn_fd,
-        .notify = &solaris_evtchn_notify,
-        .bind_unbound_port = &solaris_evtchn_bind_unbound_port,
-        .bind_interdomain = &solaris_evtchn_bind_interdomain,
-        .bind_virq = &solaris_evtchn_bind_virq,
-        .unbind = &solaris_evtchn_unbind,
-        .pending = &solaris_evtchn_pending,
-        .unmask = &solaris_evtchn_unmask,
-    },
-};
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
@@ -324,8 +307,6 @@ static struct xc_osdep_ops *solaris_osdep_init(xc_interface *xch, enum xc_osdep_
     {
     case XC_OSDEP_PRIVCMD:
         return &solaris_privcmd_ops;
-    case XC_OSDEP_EVTCHN:
-        return &solaris_evtchn_ops;
     default:
         return NULL;
     }
diff --git a/tools/libxc/xenctrl_osdep_ENOSYS.c b/tools/libxc/xenctrl_osdep_ENOSYS.c
index dd59dcd..5182532 100644
--- a/tools/libxc/xenctrl_osdep_ENOSYS.c
+++ b/tools/libxc/xenctrl_osdep_ENOSYS.c
@@ -81,90 +81,6 @@ static struct xc_osdep_ops ENOSYS_privcmd_ops =
     }
 };
 
-static xc_osdep_handle ENOSYS_evtchn_open(xc_interface *xce)
-{
-    IPRINTF(xce, "ENOSYS_evtchn: opening handle %p\n", (void *)1);
-    return (xc_osdep_handle)2; /*dummy*/
-}
-
-static int ENOSYS_evtchn_close(xc_interface *xce, xc_osdep_handle h)
-{
-    IPRINTF(xce, "ENOSYS_evtchn: closing handle %lx\n", h);
-    return 0;
-}
-
-static int ENOSYS_evtchn_fd(xc_interface *xce, xc_osdep_handle h)
-{
-    IPRINTF(xce, "ENOSYS_fd %lx fd\n", h);
-    return (int)h;
-}
-
-static int ENOSYS_evtchn_notify(xc_interface *xce, xc_osdep_handle h, evtchn_port_t port)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx notify: %d\n", h, port);
-    return -ENOSYS;
-}
-
-static int ENOSYS_evtchn_bind_unbound_port(xc_interface *xce, xc_osdep_handle h, int domid)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx bind_unbound_port: dom%d\n", h, domid);
-    return -ENOSYS;
-}
-
-
-static int ENOSYS_evtchn_bind_interdomain(xc_interface *xce, xc_osdep_handle h, int domid, evtchn_port_t remote_port)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx bind_interdomain: dmo%d %d\n", h, domid, remote_port);
-    return -ENOSYS;
-}
-
-
-static int ENOSYS_evtchn_bind_virq(xc_interface *xce, xc_osdep_handle h, unsigned int virq)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx bind_virq: %d\n", h, virq);
-    return -ENOSYS;
-}
-
-
-static int ENOSYS_evtchn_unbind(xc_interface *xce, xc_osdep_handle h, evtchn_port_t port)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx unbind: %d\n", h, port);
-    return -ENOSYS;
-}
-
-
-static evtchn_port_or_error_t ENOSYS_evtchn_pending(xc_interface *xce, xc_osdep_handle h)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx pending\n", h);
-    return -ENOSYS;
-}
-
-static int ENOSYS_evtchn_unmask(xc_interface *xce, xc_osdep_handle h, evtchn_port_t port)
-{
-    IPRINTF(xce, "ENOSYS_evtchn %lx unmask: %d\n", h, port);
-    return -ENOSYS;
-}
-
-static struct xc_osdep_ops ENOSYS_evtchn_ops = {
-    .open = &ENOSYS_evtchn_open,
-    .close = &ENOSYS_evtchn_close,
-
-    .u.evtchn = {
-        .fd = &ENOSYS_evtchn_fd,
-
-        .notify = &ENOSYS_evtchn_notify,
-
-        .bind_unbound_port = &ENOSYS_evtchn_bind_unbound_port,
-        .bind_interdomain = &ENOSYS_evtchn_bind_interdomain,
-        .bind_virq = &ENOSYS_evtchn_bind_virq,
-
-        .unbind = &ENOSYS_evtchn_unbind,
-
-        .pending = &ENOSYS_evtchn_pending,
-        .unmask = &ENOSYS_evtchn_unmask,
-    },
-};
-
 static struct xc_osdep_ops * ENOSYS_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     struct xc_osdep_ops *ops;
@@ -180,9 +96,6 @@ static struct xc_osdep_ops * ENOSYS_osdep_init(xc_interface *xch, enum xc_osdep_
     case XC_OSDEP_PRIVCMD:
         ops = &ENOSYS_privcmd_ops;
         break;
-    case XC_OSDEP_EVTCHN:
-        ops = &ENOSYS_evtchn_ops;
-        break;
     default:
         ops = NULL;
         break;
diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
index abefd6b..5939e7c 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
+++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
@@ -34,14 +34,14 @@
 #include <caml/callback.h>
 #include <caml/fail.h>
 
-#define _H(__h) ((xc_interface *)(__h))
+#define _H(__h) ((xc_evtchn *)(__h))
 
 CAMLprim value stub_eventchn_init(void)
 {
 	CAMLparam0();
 	CAMLlocal1(result);
 
-	xc_interface *xce = xc_evtchn_open(NULL, XC_OPENFLAG_NON_REENTRANT);
+	xc_evtchn *xce = xc_evtchn_open(NULL, XC_OPENFLAG_NON_REENTRANT);
 	if (xce == NULL)
 		caml_failwith("open failed");
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 01/29] tools/libxc: Remove osdep indirection for xc_evtchn Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-22 11:48     ` Ian Campbell
  2016-01-22 17:12     ` Boris Ostrovsky
  2016-01-15 13:22   ` [PATCH XEN v8 03/29] tools: Arrange to check public headers for ANSI compatiblity Ian Campbell
                     ` (26 subsequent siblings)
  28 siblings, 2 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

libxenevtchn will provide a stable API and ABI for accessing the
evtchn device.

The functions are moved into the xenevtchn namespace to make a clean
break from libxc and avoid ambiguity regarding which interfaces are
stable.

All in-tree users are updated to use the new names.

Upon request (via #define XC_WANT_COMPAT_EVTCHN_API) libxenctrl will
provide a compat API for the old names. This is used by qemu-xen for
the time being. qemu-xen-traditional is updated in lockstep.

This leaves a few event channel related functions which go via privcmd
(EVTCHNOP) rather than ioctls on the /dev/xen/evtchn device in
libxenctrl. Specifically:

 - xc_evtchn_alloc_unbound
 - xc_evtchn_reset
 - xc_evtchn_status

Note that xc_evtchn_alloc_unbound's functionality is also provided by
xenevtchn_bind_unbound_port() (née xc_evtchn_bind_unbound_port) and is
probably redundant.

These functions do not appear to be needed by qemu-dm, qemu-pv
(provision of device model to HVM guests and PV backends respectively)
or by libvchan suggesting they are not needed by non-toolstack uses of
event channels. QEMU does use these in hw/xenpv/xen_domainbuild.c but
that is a "toolstack use".

The new library uses a version script to ensure that only expected
symbols are exported and to version them such that ABI guarantees can
be kept in the future.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---

Must be applied with:

 - "qemu-xen-traditional: Use libxenevtchn" and a corresponding
   QEMU_TAG update folded here.
 - "mini-os: Include libxenevtchn with libxc"" and a corresponding
   bump to MINIOS_UPSTREAM_REVISION folded in here.

v2: Update doc at same time
    Removed some stray compat thing
v3: Moved to tools/libs/evtchn
v4: typedef the xenevtchn_handle in xenguest.h instead of #include, to
    avoid leaking the libxenevtchn namespace into callers who may not
    want it. (And also to avoid adding lots of -I to places like
    stubdom builds)
v5: Handle NULL passed to _close()
v6: Properly port xen-access to new API (instead of a single function call)
v7: Added mk-headers-$(XEN_TARGET_ARCH) build dependency instead of
    open coding the recursion.
v8: Remove *.so on clean, add distclean target.
---
 .gitignore                                    |   1 +
 stubdom/Makefile                              |  17 +-
 tools/Makefile                                |   5 +-
 tools/Rules.mk                                |  14 +-
 tools/console/Makefile                        |   2 +
 tools/console/daemon/io.c                     |  43 ++--
 tools/libs/Makefile                           |   1 +
 tools/libs/evtchn/Makefile                    |  69 +++++++
 tools/libs/evtchn/core.c                      |  72 +++++++
 tools/libs/evtchn/freebsd.c                   | 138 +++++++++++++
 tools/libs/evtchn/include/xenevtchn.h         | 150 ++++++++++++++
 tools/libs/evtchn/libxenevtchn.map            |  19 ++
 tools/libs/evtchn/linux.c                     | 136 +++++++++++++
 tools/libs/evtchn/minios.c                    | 269 ++++++++++++++++++++++++++
 tools/libs/evtchn/netbsd.c                    | 147 ++++++++++++++
 tools/libs/evtchn/private.h                   |  25 +++
 tools/libs/evtchn/solaris.c                   | 135 +++++++++++++
 tools/libs/toollog/include/xentoollog.h       |  10 +
 tools/libs/toollog/xtl_core.c                 |  10 +
 tools/libs/toollog/xtl_logger_stdio.c         |  10 +
 tools/libvchan/Makefile                       |   6 +-
 tools/libvchan/init.c                         |  20 +-
 tools/libvchan/io.c                           |  12 +-
 tools/libvchan/libxenvchan.h                  |   3 +-
 tools/libxc/Makefile                          |   8 +-
 tools/libxc/include/xenctrl.h                 |  99 +---------
 tools/libxc/include/xenctrl_compat.h          |  48 +++++
 tools/libxc/include/xenguest.h                |  14 +-
 tools/libxc/xc_evtchn_compat.c                |  75 +++++++
 tools/libxc/xc_freebsd_osdep.c                | 101 ----------
 tools/libxc/xc_linux_osdep.c                  |  95 ---------
 tools/libxc/xc_minios.c                       | 221 ---------------------
 tools/libxc/xc_netbsd.c                       | 109 -----------
 tools/libxc/xc_private.c                      |  40 ----
 tools/libxc/xc_private.h                      |   7 -
 tools/libxc/xc_solaris.c                      |  97 ----------
 tools/libxc/xc_suspend.c                      |  18 +-
 tools/libxl/Makefile                          |   5 +-
 tools/libxl/libxl.c                           |   2 +-
 tools/libxl/libxl_dom_suspend.c               |   4 +-
 tools/libxl/libxl_event.c                     |  14 +-
 tools/libxl/libxl_internal.h                  |   5 +-
 tools/misc/Makefile                           |   7 +-
 tools/misc/xen-hptool.c                       |  13 +-
 tools/misc/xen-lowmemd.c                      |  15 +-
 tools/ocaml/libs/eventchn/Makefile            |   4 +-
 tools/ocaml/libs/eventchn/xeneventchn_stubs.c |  20 +-
 tools/python/setup.py                         |   7 +-
 tools/tests/xen-access/Makefile               |   3 +-
 tools/tests/xen-access/xen-access.c           |  21 +-
 tools/xcutils/Makefile                        |   4 +-
 tools/xenmon/Makefile                         |   2 +
 tools/xenmon/xenbaked.c                       |  13 +-
 tools/xenpaging/Makefile                      |   4 +-
 tools/xenpaging/xenpaging.c                   |  18 +-
 tools/xenpaging/xenpaging.h                   |   3 +-
 tools/xenstore/Makefile                       |   5 +-
 tools/xenstore/xenstored_core.c               |   6 +-
 tools/xenstore/xenstored_domain.c             |  25 +--
 tools/xentrace/Makefile                       |   5 +-
 tools/xentrace/xentrace.c                     |  13 +-
 61 files changed, 1539 insertions(+), 925 deletions(-)
 create mode 100644 tools/libs/evtchn/Makefile
 create mode 100644 tools/libs/evtchn/core.c
 create mode 100644 tools/libs/evtchn/freebsd.c
 create mode 100644 tools/libs/evtchn/include/xenevtchn.h
 create mode 100644 tools/libs/evtchn/libxenevtchn.map
 create mode 100644 tools/libs/evtchn/linux.c
 create mode 100644 tools/libs/evtchn/minios.c
 create mode 100644 tools/libs/evtchn/netbsd.c
 create mode 100644 tools/libs/evtchn/private.h
 create mode 100644 tools/libs/evtchn/solaris.c
 create mode 100644 tools/libxc/include/xenctrl_compat.h
 create mode 100644 tools/libxc/xc_evtchn_compat.c

diff --git a/.gitignore b/.gitignore
index 02b2100..f79a7b3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -62,6 +62,7 @@ stubdom/ioemu
 stubdom/ioemu/
 stubdom/libs-*
 stubdom/libxc-*
+stubdom/libxenevtchn-*
 stubdom/libxentoollog-*
 stubdom/lwip-*
 stubdom/lwip/
diff --git a/stubdom/Makefile b/stubdom/Makefile
index 9c77205..702d66b 100644
--- a/stubdom/Makefile
+++ b/stubdom/Makefile
@@ -319,6 +319,12 @@ mk-headers-$(XEN_TARGET_ARCH): $(IOEMU_LINKFARM_TARGET)
 	  ln -sf $(XEN_ROOT)/tools/libs/toollog/include/*.h . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/toollog/*.c . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/toollog/Makefile . )
+	mkdir -p libs-$(XEN_TARGET_ARCH)/evtchn/include
+	[ -h libs-$(XEN_TARGET_ARCH)/evtchn/Makefile ] || ( cd libs-$(XEN_TARGET_ARCH)/evtchn && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/*.h . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/include/*.h include/ && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/*.c . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/Makefile . )
 	mkdir -p libxc-$(XEN_TARGET_ARCH)
 	[ -h libxc-$(XEN_TARGET_ARCH)/Makefile ] || ( cd libxc-$(XEN_TARGET_ARCH) && \
 	  ln -sf $(XEN_ROOT)/tools/libxc/*.h . && \
@@ -351,12 +357,21 @@ libs-$(XEN_TARGET_ARCH)/toollog/libxentoollog.a: mk-headers-$(XEN_TARGET_ARCH) $
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/toollog
 
 #######
+# libxenevtchn
+#######
+
+.PHONY: libxenevtchn
+libxenevtchn: libs-$(XEN_TARGET_ARCH)/evtchn/libxenevtchn.a
+libs-$(XEN_TARGET_ARCH)/evtchn/libxenevtchn.a: mk-headers-$(XEN_TARGET_ARCH) $(NEWLIB_STAMPFILE)
+	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/evtchn
+
+#######
 # libxc
 #######
 
 .PHONY: libxc
 libxc: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a libxc-$(XEN_TARGET_ARCH)/libxenguest.a
-libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog cross-zlib
+libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn cross-zlib
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= CONFIG_LIBXC_MINIOS=y -C libxc-$(XEN_TARGET_ARCH)
 
  libxc-$(XEN_TARGET_ARCH)/libxenguest.a: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a
diff --git a/tools/Makefile b/tools/Makefile
index 9f74ac7..99e016a 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -248,8 +248,10 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		--libdir=$(LIBEXEC_LIB) \
 		--includedir=$(LIBEXEC_INC) \
 		--source-path=$$source \
-		--extra-cflags="-I$(XEN_ROOT)/tools/include \
+		--extra-cflags="-DXC_WANT_COMPAT_EVTCHN_API=1 \
+		-I$(XEN_ROOT)/tools/include \
 		-I$(XEN_ROOT)/tools/libs/toollog/include \
+		-I$(XEN_ROOT)/tools/libs/evtchn/include \
 		-I$(XEN_ROOT)/tools/libxc/include \
 		-I$(XEN_ROOT)/tools/xenstore/include \
 		-I$(XEN_ROOT)/tools/xenstore/compat/include \
@@ -257,6 +259,7 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		--extra-ldflags="-L$(XEN_ROOT)/tools/libxc \
 		-L$(XEN_ROOT)/tools/xenstore \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog \
+		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/evtchn \
 		$(QEMU_UPSTREAM_RPATH)" \
 		--bindir=$(LIBEXEC_BIN) \
 		--datadir=$(SHAREDIR)/qemu-xen \
diff --git a/tools/Rules.mk b/tools/Rules.mk
index 0652e4b..75d02c4 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -11,6 +11,7 @@ INSTALL = $(XEN_ROOT)/tools/cross-install
 
 XEN_INCLUDE        = $(XEN_ROOT)/tools/include
 XEN_LIBXENTOOLLOG  = $(XEN_ROOT)/tools/libs/toollog
+XEN_LIBXENEVTCHN   = $(XEN_ROOT)/tools/libs/evtchn
 XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
 XEN_XENLIGHT       = $(XEN_ROOT)/tools/libxl
 XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
@@ -82,13 +83,18 @@ SHDEPS_libxentoollog =
 LDLIBS_libxentoollog = $(XEN_LIBXENTOOLLOG)/libxentoollog$(libextension)
 SHLIB_libxentoollog  = -Wl,-rpath-link=$(XEN_LIBXENTOOLLOG)
 
+CFLAGS_libxenevtchn = -I$(XEN_LIBXENEVTCHN)/include $(CFLAGS_xeninclude)
+SHDEPS_libxenevtchn =
+LDLIBS_libxenevtchn = $(XEN_LIBXENEVTCHN)/libxenevtchn$(libextension)
+SHLIB_libxenevtchn  = -Wl,-rpath-link=$(XEN_LIBXENEVTCHN)
+
 CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_xeninclude)
-SHDEPS_libxenctrl = $(SHLIB_libxentoollog)
+SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn)
 LDLIBS_libxenctrl = $(SHDEPS_libxenctrl) $(XEN_LIBXC)/libxenctrl$(libextension)
 SHLIB_libxenctrl  = $(SHDEPS_libxenctrl) -Wl,-rpath-link=$(XEN_LIBXC)
 
-CFLAGS_libxenguest = -I$(XEN_LIBXC)/include $(CFLAGS_xeninclude)
-SHDEPS_libxenguest =
+CFLAGS_libxenguest = -I$(XEN_LIBXC)/include $(CFLAGS_libxenevtchn) $(CFLAGS_xeninclude)
+SHDEPS_libxenguest = $(SHLIB_libxenevtchn)
 LDLIBS_libxenguest = $(SHDEPS_libxenguest) $(XEN_LIBXC)/libxenguest$(libextension)
 SHLIB_libxenguest  = $(SHDEPS_libxenguest) -Wl,-rpath-link=$(XEN_LIBXC)
 
@@ -103,7 +109,7 @@ LDLIBS_libxenstat  = $(SHDEPS_libxenstat) $(XEN_LIBXENSTAT)/libxenstat$(libexten
 SHLIB_libxenstat   = $(SHDEPS_libxenstat) -Wl,-rpath-link=$(XEN_LIBXENSTAT)
 
 CFLAGS_libxenvchan = -I$(XEN_LIBVCHAN)
-SHDEPS_libxenvchan = $(SHLIB_libxenctrl) $(SHLIB_libxenstore)
+SHDEPS_libxenvchan = $(SHLIB_libxenctrl) $(SHLIB_libxenstore) $(SHLIB_libxenevtchn)
 LDLIBS_libxenvchan = $(SHDEPS_libxenvchan) $(XEN_LIBVCHAN)/libxenvchan$(libextension)
 SHLIB_libxenvchan  = $(SHDEPS_libxenvchan) -Wl,-rpath-link=$(XEN_LIBVCHAN)
 
diff --git a/tools/console/Makefile b/tools/console/Makefile
index 77e8f29..4b3a492 100644
--- a/tools/console/Makefile
+++ b/tools/console/Makefile
@@ -3,8 +3,10 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS  += -Werror
 
+CFLAGS  += $(CFLAGS_libxenevtchn)
 CFLAGS  += $(CFLAGS_libxenctrl)
 CFLAGS  += $(CFLAGS_libxenstore)
+LDLIBS += $(LDLIBS_libxenevtchn)
 LDLIBS += $(LDLIBS_libxenctrl)
 LDLIBS += $(LDLIBS_libxenstore)
 LDLIBS += $(SOCKET_LIBS)
diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index cafc7b7..2f2e9c5 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -21,6 +21,7 @@
 
 #include "utils.h"
 #include "io.h"
+#include <xenevtchn.h>
 #include <xenstore.h>
 #include <xen/io/console.h>
 #include <xen/grant_table.h>
@@ -101,7 +102,7 @@ struct domain {
 	int ring_ref;
 	evtchn_port_or_error_t local_port;
 	evtchn_port_or_error_t remote_port;
-	xc_evtchn *xce_handle;
+	xenevtchn_handle *xce_handle;
 	int xce_pollfd_idx;
 	struct xencons_interface *interface;
 	int event_count;
@@ -185,7 +186,7 @@ static void buffer_append(struct domain *dom)
 
 	xen_mb();
 	intf->out_cons = cons;
-	xc_evtchn_notify(dom->xce_handle, dom->local_port);
+	xenevtchn_notify(dom->xce_handle, dom->local_port);
 
 	/* Get the data to the logfile as early as possible because if
 	 * no one is listening on the console pty then it will fill up
@@ -584,22 +585,22 @@ static int domain_create_ring(struct domain *dom)
 	dom->local_port = -1;
 	dom->remote_port = -1;
 	if (dom->xce_handle != NULL)
-		xc_evtchn_close(dom->xce_handle);
+		xenevtchn_close(dom->xce_handle);
 
 	/* Opening evtchn independently for each console is a bit
 	 * wasteful, but that's how the code is structured... */
-	dom->xce_handle = xc_evtchn_open(NULL, 0);
+	dom->xce_handle = xenevtchn_open(NULL, 0);
 	if (dom->xce_handle == NULL) {
 		err = errno;
 		goto out;
 	}
  
-	rc = xc_evtchn_bind_interdomain(dom->xce_handle,
+	rc = xenevtchn_bind_interdomain(dom->xce_handle,
 		dom->domid, remote_port);
 
 	if (rc == -1) {
 		err = errno;
-		xc_evtchn_close(dom->xce_handle);
+		xenevtchn_close(dom->xce_handle);
 		dom->xce_handle = NULL;
 		goto out;
 	}
@@ -609,7 +610,7 @@ static int domain_create_ring(struct domain *dom)
 	if (dom->master_fd == -1) {
 		if (!domain_create_tty(dom)) {
 			err = errno;
-			xc_evtchn_close(dom->xce_handle);
+			xenevtchn_close(dom->xce_handle);
 			dom->xce_handle = NULL;
 			dom->local_port = -1;
 			dom->remote_port = -1;
@@ -749,7 +750,7 @@ static void shutdown_domain(struct domain *d)
 	watch_domain(d, false);
 	domain_unmap_interface(d);
 	if (d->xce_handle != NULL)
-		xc_evtchn_close(d->xce_handle);
+		xenevtchn_close(d->xce_handle);
 	d->xce_handle = NULL;
 }
 
@@ -839,7 +840,7 @@ static void handle_tty_read(struct domain *dom)
 		}
 		xen_wmb();
 		intf->in_prod = prod;
-		xc_evtchn_notify(dom->xce_handle, dom->local_port);
+		xenevtchn_notify(dom->xce_handle, dom->local_port);
 	} else {
 		domain_close_tty(dom);
 		shutdown_domain(dom);
@@ -871,7 +872,7 @@ static void handle_ring_read(struct domain *dom)
 	if (dom->is_dead)
 		return;
 
-	if ((port = xc_evtchn_pending(dom->xce_handle)) == -1)
+	if ((port = xenevtchn_pending(dom->xce_handle)) == -1)
 		return;
 
 	dom->event_count++;
@@ -879,7 +880,7 @@ static void handle_ring_read(struct domain *dom)
 	buffer_append(dom);
 
 	if (dom->event_count < RATE_LIMIT_ALLOWANCE)
-		(void)xc_evtchn_unmask(dom->xce_handle, port);
+		(void)xenevtchn_unmask(dom->xce_handle, port);
 }
 
 static void handle_xs(void)
@@ -906,7 +907,7 @@ static void handle_xs(void)
 	free(vec);
 }
 
-static void handle_hv_logs(xc_evtchn *xce_handle, bool force)
+static void handle_hv_logs(xenevtchn_handle *xce_handle, bool force)
 {
 	static char buffer[1024*16];
 	char *bufptr = buffer;
@@ -914,7 +915,7 @@ static void handle_hv_logs(xc_evtchn *xce_handle, bool force)
 	static uint32_t index = 0;
 	evtchn_port_or_error_t port = -1;
 
-	if (!force && ((port = xc_evtchn_pending(xce_handle)) == -1))
+	if (!force && ((port = xenevtchn_pending(xce_handle)) == -1))
 		return;
 
 	do
@@ -938,7 +939,7 @@ static void handle_hv_logs(xc_evtchn *xce_handle, bool force)
 	} while (size == sizeof(buffer));
 
 	if (port != -1)
-		(void)xc_evtchn_unmask(xce_handle, port);
+		(void)xenevtchn_unmask(xce_handle, port);
 }
 
 static void handle_log_reload(void)
@@ -1006,10 +1007,10 @@ void handle_io(void)
 	evtchn_port_or_error_t log_hv_evtchn = -1;
 	int xce_pollfd_idx = -1;
 	int xs_pollfd_idx = -1;
-	xc_evtchn *xce_handle = NULL;
+	xenevtchn_handle *xce_handle = NULL;
 
 	if (log_hv) {
-		xce_handle = xc_evtchn_open(NULL, 0);
+		xce_handle = xenevtchn_open(NULL, 0);
 		if (xce_handle == NULL) {
 			dolog(LOG_ERR, "Failed to open xce handle: %d (%s)",
 			      errno, strerror(errno));
@@ -1018,7 +1019,7 @@ void handle_io(void)
 		log_hv_fd = create_hv_log();
 		if (log_hv_fd == -1)
 			goto out;
-		log_hv_evtchn = xc_evtchn_bind_virq(xce_handle, VIRQ_CON_RING);
+		log_hv_evtchn = xenevtchn_bind_virq(xce_handle, VIRQ_CON_RING);
 		if (log_hv_evtchn == -1) {
 			dolog(LOG_ERR, "Failed to bind to VIRQ_CON_RING: "
 			      "%d (%s)", errno, strerror(errno));
@@ -1047,7 +1048,7 @@ void handle_io(void)
 		xs_pollfd_idx = set_fds(xs_fileno(xs), POLLIN|POLLPRI);
 
 		if (log_hv)
-			xce_pollfd_idx = set_fds(xc_evtchn_fd(xce_handle),
+			xce_pollfd_idx = set_fds(xenevtchn_fd(xce_handle),
 						 POLLIN|POLLPRI);
 
 		if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
@@ -1066,7 +1067,7 @@ void handle_io(void)
 			if ((now+5) > d->next_period) {
 				d->next_period = now + RATE_LIMIT_PERIOD;
 				if (d->event_count >= RATE_LIMIT_ALLOWANCE) {
-					(void)xc_evtchn_unmask(d->xce_handle, d->local_port);
+					(void)xenevtchn_unmask(d->xce_handle, d->local_port);
 				}
 				d->event_count = 0;
 			}
@@ -1082,7 +1083,7 @@ void handle_io(void)
 				if (discard_overflowed_data ||
 				    !d->buffer.max_capacity ||
 				    d->buffer.size < d->buffer.max_capacity) {
-					int evtchn_fd = xc_evtchn_fd(d->xce_handle);
+					int evtchn_fd = xenevtchn_fd(d->xce_handle);
 					d->xce_pollfd_idx = set_fds(evtchn_fd,
 								    POLLIN|POLLPRI);
 				}
@@ -1202,7 +1203,7 @@ void handle_io(void)
 		log_hv_fd = -1;
 	}
 	if (xce_handle != NULL) {
-		xc_evtchn_close(xce_handle);
+		xenevtchn_close(xce_handle);
 		xce_handle = NULL;
 	}
 	if (xcg_handle != NULL) {
diff --git a/tools/libs/Makefile b/tools/libs/Makefile
index 73be500..0e3f523 100644
--- a/tools/libs/Makefile
+++ b/tools/libs/Makefile
@@ -3,5 +3,6 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 SUBDIRS-y :=
 SUBDIRS-y += toollog
+SUBDIRS-y += evtchn
 
 all clean install distclean: %: subdirs-%
diff --git a/tools/libs/evtchn/Makefile b/tools/libs/evtchn/Makefile
new file mode 100644
index 0000000..46a807f
--- /dev/null
+++ b/tools/libs/evtchn/Makefile
@@ -0,0 +1,69 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+SHLIB_LDFLAGS += -Wl,--version-script=libxenevtchn.map
+
+CFLAGS   += -Werror -Wmissing-prototypes
+CFLAGS   += -I./include $(CFLAGS_xeninclude)
+CFLAGS   += $(CFLAGS_libxentoollog)
+
+SRCS-y                 += core.c
+SRCS-$(CONFIG_Linux)   += linux.c
+SRCS-$(CONFIG_FreeBSD) += freebsd.c
+SRCS-$(CONFIG_SunOS)   += solaris.c
+SRCS-$(CONFIG_NetBSD)  += netbsd.c
+SRCS-$(CONFIG_MiniOS)  += minios.c
+
+LIB_OBJS := $(patsubst %.c,%.o,$(SRCS-y))
+PIC_OBJS := $(patsubst %.c,%.opic,$(SRCS-y))
+
+LIB := libxenevtchn.a
+ifneq ($(nosharedlibs),y)
+LIB += libxenevtchn.so
+endif
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build:
+	$(MAKE) libs
+
+.PHONY: libs
+libs: $(LIB)
+
+
+libxenevtchn.a: $(LIB_OBJS)
+	$(AR) rc $@ $^
+
+libxenevtchn.so: libxenevtchn.so.$(MAJOR)
+	$(SYMLINK_SHLIB) $< $@
+libxenevtchn.so.$(MAJOR): libxenevtchn.so.$(MAJOR).$(MINOR)
+	$(SYMLINK_SHLIB) $< $@
+
+libxenevtchn.so.$(MAJOR).$(MINOR): $(PIC_OBJS) libxenevtchn.map
+	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenevtchn.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(PIC_OBJS) $(LDLIBS_libxentoollog) $(APPEND_LDFLAGS)
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(libdir)
+	$(INSTALL_DIR) $(DESTDIR)$(includedir)
+	$(INSTALL_SHLIB) libxenevtchn.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
+	$(INSTALL_DATA) libxenevtchn.a $(DESTDIR)$(libdir)
+	$(SYMLINK_SHLIB) libxenevtchn.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenevtchn.so.$(MAJOR)
+	$(SYMLINK_SHLIB) libxenevtchn.so.$(MAJOR) $(DESTDIR)$(libdir)/libxenevtchn.so
+	$(INSTALL_DATA) include/xenevtchn.h $(DESTDIR)$(includedir)
+
+.PHONY: TAGS
+TAGS:
+	etags -t *.c *.h
+
+.PHONY: clean
+clean:
+	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
+	rm -f libxenevtchn.so.$(MAJOR).$(MINOR) libxenevtchn.so.$(MAJOR)
+
+.PHONY: distclean
+distclean: clean
diff --git a/tools/libs/evtchn/core.c b/tools/libs/evtchn/core.c
new file mode 100644
index 0000000..c31e08c
--- /dev/null
+++ b/tools/libs/evtchn/core.c
@@ -0,0 +1,72 @@
+/*
+ * 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 <unistd.h>
+#include <stdlib.h>
+
+#include "private.h"
+
+xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags)
+{
+    xenevtchn_handle *xce = malloc(sizeof(*xce));
+    int rc;
+
+    if (!xce) return NULL;
+
+    xce->fd = -1;
+    xce->logger = logger;
+    xce->logger_tofree  = NULL;
+
+    if (!xce->logger) {
+        xce->logger = xce->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xce->logger) goto err;
+    }
+
+    rc = osdep_evtchn_open(xce);
+    if ( rc  < 0 ) goto err;
+
+    return xce;
+
+err:
+    osdep_evtchn_close(xce);
+    xtl_logger_destroy(xce->logger_tofree);
+    free(xce);
+    return NULL;
+}
+
+int xenevtchn_close(xenevtchn_handle *xce)
+{
+    int rc;
+
+    if ( !xce )
+        return 0;
+
+    rc = osdep_evtchn_close(xce);
+    xtl_logger_destroy(xce->logger_tofree);
+    free(xce);
+    return rc;
+}
+
+/*
+ * 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/evtchn/freebsd.c b/tools/libs/evtchn/freebsd.c
new file mode 100644
index 0000000..636f052
--- /dev/null
+++ b/tools/libs/evtchn/freebsd.c
@@ -0,0 +1,138 @@
+ /******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split off from xc_freebsd_osdep.c
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+#define EVTCHN_DEV      "/dev/xen/evtchn"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = open(EVTCHN_DEV, O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+{
+    int ret, fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+
+    bind.remote_domain = domid;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+    return ( ret == 0 ) ? bind.port : ret;
+}
+
+evtchn_port_or_error_t
+xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid, evtchn_port_t remote_port)
+{
+    int ret, fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+    return ( ret == 0 ) ? bind.port : ret;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int ret, fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+
+    bind.virq = virq;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+    return ( ret == 0 ) ? bind.port : ret;
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+
+    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+    return 0;
+}
+
+/*
+ * 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/evtchn/include/xenevtchn.h b/tools/libs/evtchn/include/xenevtchn.h
new file mode 100644
index 0000000..3380fa3
--- /dev/null
+++ b/tools/libs/evtchn/include/xenevtchn.h
@@ -0,0 +1,150 @@
+/*
+ * 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/>.
+ *
+ * Split off from:
+ * xenctrl.h
+ *
+ * A library for low-level access to the Xen control interfaces.
+ *
+ * Copyright (c) 2003-2004, K A Fraser.
+ */
+
+#ifndef XENEVTCHN_H
+#define XENEVTCHN_H
+
+#include <stdint.h>
+
+#include <xen/event_channel.h>
+
+/* A port identifier is guaranteed to fit in 31 bits. */
+typedef int evtchn_port_or_error_t;
+
+typedef struct xenevtchn_handle xenevtchn_handle;
+
+/* Callers who don't care don't need to #include <xentoollog.h> */
+typedef struct xentoollog_logger xentoollog_logger;
+
+/*
+ * EVENT CHANNEL FUNCTIONS
+ *
+ * None of these do any logging.
+ */
+
+/*
+ * Return a handle to the event channel driver, or NULL on failure, in
+ * which case errno will be set appropriately.
+ *
+ * Note:
+ * After fork a child process must not use any opened xc evtchn
+ * handle inherited from their parent. They must open a new handle if
+ * they want to interact with xc.
+ *
+ * Before Xen pre-4.1 this function would sometimes report errors with perror.
+ */
+/* Currently no flags are defined */
+xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags);
+
+/*
+ * Close a handle previously allocated with xenevtchn_open().
+ */
+int xenevtchn_close(xenevtchn_handle *xce);
+
+/*
+ * Return an fd that can be select()ed on.
+ *
+ * Note that due to bugs, setting this fd to non blocking may not
+ * work: you would hope that it would result in xenevtchn_pending
+ * failing with EWOULDBLOCK if there are no events signaled, but in
+ * fact it may block.  (Bug is present in at least Linux 3.12, and
+ * perhaps on other platforms or later version.)
+ *
+ * To be safe, you must use poll() or select() before each call to
+ * xenevtchn_pending.  If you have multiple threads (or processes)
+ * sharing a single xce handle this will not work, and there is no
+ * straightforward workaround.  Please design your program some other
+ * way.
+ */
+int xenevtchn_fd(xenevtchn_handle *xce);
+
+/*
+ * Notify the given event channel. Returns -1 on failure, in which case
+ * errno will be set appropriately.
+ */
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port);
+
+/*
+ * Returns a new event port awaiting interdomain connection from the given
+ * domain ID, or -1 on failure, in which case errno will be set appropriately.
+ */
+evtchn_port_or_error_t
+xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid);
+
+/*
+ * Returns a new event port bound to the remote port for the given domain ID,
+ * or -1 on failure, in which case errno will be set appropriately.
+ */
+evtchn_port_or_error_t
+xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                           evtchn_port_t remote_port);
+
+/*
+ * Bind an event channel to the given VIRQ. Returns the event channel bound to
+ * the VIRQ, or -1 on failure, in which case errno will be set appropriately.
+ */
+evtchn_port_or_error_t
+xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq);
+
+/*
+ * Unbind the given event channel. Returns -1 on failure, in which case errno
+ * will be set appropriately.
+ */
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port);
+
+/*
+ * Return the next event channel to become pending, or -1 on failure, in which
+ * case errno will be set appropriately.
+ *
+ * At the hypervisor level the event channel will have been masked,
+ * and then cleared, by the underlying machinery (evtchn kernel
+ * driver, or equivalent).  So if the event channel is signaled again
+ * after it is returned here, it will be queued up, and delivered
+ * again after you unmask it.  (See the documentation in the Xen
+ * public header event_channel.h.)
+ *
+ * On receiving the notification from xenevtchn_pending, you should
+ * normally: check (by other means) what work needs doing; do the
+ * necessary work (if any); unmask the event channel with
+ * xenevtchn_unmask (if you want to receive any further
+ * notifications).
+ */
+evtchn_port_or_error_t
+xenevtchn_pending(xenevtchn_handle *xce);
+
+/*
+ * Unmask the given event channel. Returns -1 on failure, in which case errno
+ * will be set appropriately.
+ */
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port);
+
+#endif
+
+/*
+ * 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/evtchn/libxenevtchn.map b/tools/libs/evtchn/libxenevtchn.map
new file mode 100644
index 0000000..625a1e2
--- /dev/null
+++ b/tools/libs/evtchn/libxenevtchn.map
@@ -0,0 +1,19 @@
+VERS_1.0 {
+	global:
+		xenevtchn_open;
+		xenevtchn_close;
+
+		xenevtchn_fd;
+
+		xenevtchn_bind_unbound_port;
+		xenevtchn_bind_interdomain;
+		xenevtchn_bind_virq;
+
+		xenevtchn_notify;
+
+		xenevtchn_unbind;
+		xenevtchn_unmask;
+
+		xenevtchn_pending;
+	local: *; /* Do not expose anything by default */
+};
diff --git a/tools/libs/evtchn/linux.c b/tools/libs/evtchn/linux.c
new file mode 100644
index 0000000..27fd6e9
--- /dev/null
+++ b/tools/libs/evtchn/linux.c
@@ -0,0 +1,136 @@
+/*
+ * 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/>.
+ *
+ * Split out from xc_linus_osdep.c:
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = open("/dev/xen/evtchn", O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
+                                                   int domid)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+
+    bind.remote_domain = domid;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
+                                                  int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
+                                           unsigned int virq)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+
+    bind.virq = virq;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+
+    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+    return 0;
+}
+
+/*
+ * 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/evtchn/minios.c b/tools/libs/evtchn/minios.c
new file mode 100644
index 0000000..fb913a2
--- /dev/null
+++ b/tools/libs/evtchn/minios.c
@@ -0,0 +1,269 @@
+/******************************************************************************
+ *
+ * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
+ * All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split off from xc_minios.c
+ */
+
+#include "xen-external/bsd-sys-queue.h"
+#include <mini-os/types.h>
+#include <mini-os/os.h>
+#include <mini-os/lib.h>
+#include <mini-os/events.h>
+#include <mini-os/wait.h>
+
+#include <sys/socket.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <malloc.h>
+
+#include "private.h"
+
+extern void minios_evtchn_close_fd(int fd);
+
+extern struct wait_queue_head event_queue;
+
+//void minios_evtchn_close_fd(int fd);
+
+/* XXX Note: This is not threadsafe */
+static struct evtchn_port_info* port_alloc(int fd) {
+    struct evtchn_port_info *port_info;
+    port_info = malloc(sizeof(struct evtchn_port_info));
+    if (port_info == NULL)
+        return NULL;
+    port_info->pending = 0;
+    port_info->port = -1;
+    port_info->bound = 0;
+
+    LIST_INSERT_HEAD(&files[fd].evtchn.ports, port_info, list);
+    return port_info;
+}
+
+static void port_dealloc(struct evtchn_port_info *port_info) {
+    if (port_info->bound)
+        unbind_evtchn(port_info->port);
+    LIST_REMOVE(port_info, list);
+    free(port_info);
+}
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = alloc_fd(FTYPE_EVTCHN);
+    if ( fd == -1 )
+        return -1;
+    LIST_INIT(&files[fd].evtchn.ports);
+    xce->fd = fd;
+    printf("evtchn_open() -> %d\n", fd);
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+void minios_evtchn_close_fd(int fd)
+{
+    struct evtchn_port_info *port_info, *tmp;
+    LIST_FOREACH_SAFE(port_info, &files[fd].evtchn.ports, list, tmp)
+        port_dealloc(port_info);
+
+    files[fd].type = FTYPE_NONE;
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int ret;
+
+    ret = notify_remote_via_evtchn(port);
+
+    if (ret < 0) {
+	errno = -ret;
+	ret = -1;
+    }
+    return ret;
+}
+
+static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
+{
+    int fd = (int)(intptr_t)data;
+    struct evtchn_port_info *port_info;
+    assert(files[fd].type == FTYPE_EVTCHN);
+    mask_evtchn(port);
+    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
+        if (port_info->port == port)
+            goto found;
+    }
+    printk("Unknown port for handle %d\n", fd);
+    return;
+
+ found:
+    port_info->pending = 1;
+    files[fd].read = 1;
+    wake_up(&event_queue);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    int ret;
+    evtchn_port_t port;
+
+    assert(get_current() == main_thread);
+    port_info = port_alloc(fd);
+    if (port_info == NULL)
+	return -1;
+
+    printf("xenevtchn_bind_unbound_port(%d)", domid);
+    ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)fd, &port);
+    printf(" = %d\n", ret);
+
+    if (ret < 0) {
+	port_dealloc(port_info);
+	errno = -ret;
+	return -1;
+    }
+    port_info->bound = 1;
+    port_info->port = port;
+    unmask_evtchn(port);
+    return port;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    evtchn_port_t local_port;
+    int ret;
+
+    assert(get_current() == main_thread);
+    port_info = port_alloc(fd);
+    if (port_info == NULL)
+	return -1;
+
+    printf("xenevtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
+    ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)fd, &local_port);
+    printf(" = %d\n", ret);
+
+    if (ret < 0) {
+	port_dealloc(port_info);
+	errno = -ret;
+	return -1;
+    }
+    port_info->bound = 1;
+    port_info->port = local_port;
+    unmask_evtchn(local_port);
+    return local_port;
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+
+    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
+        if (port_info->port == port) {
+            port_dealloc(port_info);
+            return 0;
+        }
+    }
+    printf("Warning: couldn't find port %"PRId32" for xc handle %x\n", port, fd);
+    errno = EINVAL;
+    return -1;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    evtchn_port_t port;
+
+    assert(get_current() == main_thread);
+    port_info = port_alloc(fd);
+    if (port_info == NULL)
+	return -1;
+
+    printf("xenevtchn_bind_virq(%d)", virq);
+    port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)fd);
+
+    if (port < 0) {
+	port_dealloc(port_info);
+	errno = -port;
+	return -1;
+    }
+    port_info->bound = 1;
+    port_info->port = port;
+    unmask_evtchn(port);
+    return port;
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    unsigned long flags;
+    evtchn_port_t ret = -1;
+
+    local_irq_save(flags);
+    files[fd].read = 0;
+
+    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
+        if (port_info->port != -1 && port_info->pending) {
+            if (ret == -1) {
+                ret = port_info->port;
+                port_info->pending = 0;
+            } else {
+                files[fd].read = 1;
+                break;
+            }
+        }
+    }
+    local_irq_restore(flags);
+    return ret;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    unmask_evtchn(port);
+    return 0;
+}
+
+/*
+ * 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/evtchn/netbsd.c b/tools/libs/evtchn/netbsd.c
new file mode 100644
index 0000000..c4123fe
--- /dev/null
+++ b/tools/libs/evtchn/netbsd.c
@@ -0,0 +1,147 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split out from xc_netbsd.c
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+#define EVTCHN_DEV_NAME  "/dev/xenevt"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, int domid)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+    int ret;
+
+    bind.remote_domain = domid;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+    if (ret == 0)
+	return bind.port;
+    else
+	return -1;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+    int ret;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+    if (ret == 0)
+	return bind.port;
+    else
+	return -1;
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+    int err;
+
+    bind.virq = virq;
+
+    err = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+    if (err)
+	return -1;
+    else
+	return bind.port;
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    return write_exact(fd, (char *)&port, sizeof(port));
+}
+
+/*
+ * 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/evtchn/private.h b/tools/libs/evtchn/private.h
new file mode 100644
index 0000000..fcd0e96
--- /dev/null
+++ b/tools/libs/evtchn/private.h
@@ -0,0 +1,25 @@
+#ifndef XENEVTCHN_PRIVATE_H
+#define XENEVTCHN_PRIVATE_H
+
+#include <xentoollog.h>
+#include <xenevtchn.h>
+
+struct xenevtchn_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    int fd;
+};
+
+int osdep_evtchn_open(xenevtchn_handle *xce);
+int osdep_evtchn_close(xenevtchn_handle *xce);
+
+#endif
+
+/*
+ * 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/evtchn/solaris.c b/tools/libs/evtchn/solaris.c
new file mode 100644
index 0000000..114cefb
--- /dev/null
+++ b/tools/libs/evtchn/solaris.c
@@ -0,0 +1,135 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split out from xc_solaris.c
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd;
+
+    if ( (fd = open("/dev/xen/evtchn", O_RDWR)) == -1 )
+    {
+        PERROR("Could not open event channel interface");
+        return -1;
+    }
+
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+
+    bind.remote_domain = domid;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+
+    bind.virq = virq;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    return write_exact(fd, (char *)&port, sizeof(port));
+}
+
+/*
+ * 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/toollog/include/xentoollog.h b/tools/libs/toollog/include/xentoollog.h
index 853e9c7..76f17fe 100644
--- a/tools/libs/toollog/include/xentoollog.h
+++ b/tools/libs/toollog/include/xentoollog.h
@@ -134,3 +134,13 @@ const char *xtl_level_to_string(xentoollog_level); /* never fails */
 
 
 #endif /* XENTOOLLOG_H */
+
+/*
+ * 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/toollog/xtl_core.c b/tools/libs/toollog/xtl_core.c
index c4724a0..099d2f3 100644
--- a/tools/libs/toollog/xtl_core.c
+++ b/tools/libs/toollog/xtl_core.c
@@ -81,3 +81,13 @@ void xtl_logger_destroy(struct xentoollog_logger *logger) {
     if (!logger) return;
     logger->destroy(logger);
 }
+
+/*
+ * 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/toollog/xtl_logger_stdio.c b/tools/libs/toollog/xtl_logger_stdio.c
index 0cd9206..f9c5bd8 100644
--- a/tools/libs/toollog/xtl_logger_stdio.c
+++ b/tools/libs/toollog/xtl_logger_stdio.c
@@ -190,3 +190,13 @@ xentoollog_logger_stdiostream *xtl_createlogger_stdiostream
 
     return XTL_NEW_LOGGER(stdiostream, newlogger);
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libvchan/Makefile b/tools/libvchan/Makefile
index 3c50fe6..84128a3 100644
--- a/tools/libvchan/Makefile
+++ b/tools/libvchan/Makefile
@@ -10,9 +10,9 @@ NODE_OBJS = node.o
 NODE2_OBJS = node-select.o
 
 LIBVCHAN_PIC_OBJS = $(patsubst %.o,%.opic,$(LIBVCHAN_OBJS))
-LIBVCHAN_LIBS = $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl)
-$(LIBVCHAN_OBJS) $(LIBVCHAN_PIC_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(CFLAGS_libxenctrl)
-$(NODE_OBJS) $(NODE2_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
+LIBVCHAN_LIBS = $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenevtchn)
+$(LIBVCHAN_OBJS) $(LIBVCHAN_PIC_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
+$(NODE_OBJS) $(NODE2_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
 
 MAJOR = 1.0
 MINOR = 0
diff --git a/tools/libvchan/init.c b/tools/libvchan/init.c
index 77be4e7..66cb103 100644
--- a/tools/libvchan/init.c
+++ b/tools/libvchan/init.c
@@ -216,25 +216,25 @@ static int init_evt_srv(struct libxenvchan *ctrl, int domain, xentoollog_logger
 {
 	evtchn_port_or_error_t port;
 
-	ctrl->event = xc_evtchn_open(logger, 0);
+	ctrl->event = xenevtchn_open(logger, 0);
 	if (!ctrl->event)
 		return -1;
 
-	port = xc_evtchn_bind_unbound_port(ctrl->event, domain);
+	port = xenevtchn_bind_unbound_port(ctrl->event, domain);
 	if (port < 0)
 		goto fail;
 	ctrl->event_port = port;
 
-	if (xc_evtchn_unmask(ctrl->event, ctrl->event_port))
+	if (xenevtchn_unmask(ctrl->event, ctrl->event_port))
 		goto fail;
 
 	return 0;
 
 fail:
 	if (port >= 0)
-		xc_evtchn_unbind(ctrl->event, port);
+		xenevtchn_unbind(ctrl->event, port);
 
-	xc_evtchn_close(ctrl->event);
+	xenevtchn_close(ctrl->event);
 	ctrl->event = NULL;
 
 	return -1;
@@ -346,26 +346,26 @@ static int init_evt_cli(struct libxenvchan *ctrl, int domain, xentoollog_logger
 {
 	evtchn_port_or_error_t port;
 
-	ctrl->event = xc_evtchn_open(logger, 0);
+	ctrl->event = xenevtchn_open(logger, 0);
 	if (!ctrl->event)
 		return -1;
 
-	port = xc_evtchn_bind_interdomain(ctrl->event,
+	port = xenevtchn_bind_interdomain(ctrl->event,
 		domain, ctrl->event_port);
 	if (port < 0)
 		goto fail;
 	ctrl->event_port = port;
 
-	if (xc_evtchn_unmask(ctrl->event, ctrl->event_port))
+	if (xenevtchn_unmask(ctrl->event, ctrl->event_port))
 		goto fail;
 
 	return 0;
 
 fail:
 	if (port >= 0)
-		xc_evtchn_unbind(ctrl->event, port);
+		xenevtchn_unbind(ctrl->event, port);
 
-	xc_evtchn_close(ctrl->event);
+	xenevtchn_close(ctrl->event);
 	ctrl->event = NULL;
 
 	return -1;
diff --git a/tools/libvchan/io.c b/tools/libvchan/io.c
index 381cc05..53393a5 100644
--- a/tools/libvchan/io.c
+++ b/tools/libvchan/io.c
@@ -105,7 +105,7 @@ static inline int send_notify(struct libxenvchan *ctrl, uint8_t bit)
 	notify = ctrl->is_server ? &ctrl->ring->srv_notify : &ctrl->ring->cli_notify;
 	prev = __sync_fetch_and_and(notify, ~bit);
 	if (prev & bit)
-		return xc_evtchn_notify(ctrl->event, ctrl->event_port);
+		return xenevtchn_notify(ctrl->event, ctrl->event_port);
 	else
 		return 0;
 }
@@ -196,10 +196,10 @@ int libxenvchan_buffer_space(struct libxenvchan *ctrl)
 
 int libxenvchan_wait(struct libxenvchan *ctrl)
 {
-	int ret = xc_evtchn_pending(ctrl->event);
+	int ret = xenevtchn_pending(ctrl->event);
 	if (ret < 0)
 		return -1;
-	xc_evtchn_unmask(ctrl->event, ret);
+	xenevtchn_unmask(ctrl->event, ret);
 	return 0;
 }
 
@@ -352,7 +352,7 @@ int libxenvchan_is_open(struct libxenvchan* ctrl)
 
 int libxenvchan_fd_for_select(struct libxenvchan *ctrl)
 {
-	return xc_evtchn_fd(ctrl->event);
+	return xenevtchn_fd(ctrl->event);
 }
 
 void libxenvchan_close(struct libxenvchan *ctrl)
@@ -374,8 +374,8 @@ void libxenvchan_close(struct libxenvchan *ctrl)
 	}
 	if (ctrl->event) {
 		if (ctrl->ring)
-			xc_evtchn_notify(ctrl->event, ctrl->event_port);
-		xc_evtchn_close(ctrl->event);
+			xenevtchn_notify(ctrl->event, ctrl->event_port);
+		xenevtchn_close(ctrl->event);
 	}
 	if (ctrl->is_server) {
 		if (ctrl->gntshr)
diff --git a/tools/libvchan/libxenvchan.h b/tools/libvchan/libxenvchan.h
index 0944a0e..1544378 100644
--- a/tools/libvchan/libxenvchan.h
+++ b/tools/libvchan/libxenvchan.h
@@ -44,6 +44,7 @@
 
 #include <xen/io/libxenvchan.h>
 #include <xen/sys/evtchn.h>
+#include <xenevtchn.h>
 #include <xenctrl.h>
 
 struct libxenvchan_ring {
@@ -71,7 +72,7 @@ struct libxenvchan {
 	/* Pointer to shared ring page */
 	struct vchan_interface *ring;
 	/* event channel interface */
-	xc_evtchn *event;
+	xenevtchn_handle *event;
 	uint32_t event_port;
 	/* informative flags: are we acting as server? */
 	int is_server:1;
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index 940708f..b8fc6a5 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -48,6 +48,7 @@ CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c
 CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c
 CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c
 CTRL_SRCS-$(CONFIG_MiniOS) += xc_minios.c
+CTRL_SRCS-y       += xc_evtchn_compat.c
 
 GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
@@ -111,6 +112,7 @@ CFLAGS-$(CONFIG_Linux) += -D_GNU_SOURCE
 
 CFLAGS	+= $(PTHREAD_CFLAGS)
 CFLAGS	+= $(CFLAGS_libxentoollog)
+CFLAGS	+= $(CFLAGS_libxenevtchn)
 
 CTRL_LIB_OBJS := $(patsubst %.c,%.o,$(CTRL_SRCS-y))
 CTRL_PIC_OBJS := $(patsubst %.c,%.opic,$(CTRL_SRCS-y))
@@ -164,7 +166,7 @@ install: build
 	$(INSTALL_DATA) libxenctrl.a $(DESTDIR)$(libdir)
 	$(SYMLINK_SHLIB) libxenctrl.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenctrl.so.$(MAJOR)
 	$(SYMLINK_SHLIB) libxenctrl.so.$(MAJOR) $(DESTDIR)$(libdir)/libxenctrl.so
-	$(INSTALL_DATA) include/xenctrl.h include/xenctrlosdep.h $(DESTDIR)$(includedir)
+	$(INSTALL_DATA) include/xenctrl.h include/xenctrl_compat.h include/xenctrlosdep.h $(DESTDIR)$(includedir)
 	$(INSTALL_SHLIB) libxenguest.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
 	$(INSTALL_DATA) libxenguest.a $(DESTDIR)$(libdir)
 	$(SYMLINK_SHLIB) libxenguest.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenguest.so.$(MAJOR)
@@ -207,7 +209,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
 	$(SYMLINK_SHLIB) $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 # libxenguest
 
@@ -230,7 +232,7 @@ xc_dom_bzimageloader.opic: CFLAGS += $(call zlib-options,D)
 
 libxenguest.so.$(MAJOR).$(MINOR): COMPRESSION_LIBS = $(call zlib-options,l)
 libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
-	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(GUEST_PIC_OBJS) $(COMPRESSION_LIBS) -lz $(LDLIBS_libxenctrl) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(GUEST_PIC_OBJS) $(COMPRESSION_LIBS) -lz $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 xenctrl_osdep_ENOSYS.so: $(OSDEP_PIC_OBJS) libxenctrl.so
 	$(CC) $(LDFLAGS) $(SHLIB_LDFLAGS) -o $@ $(OSDEP_PIC_OBJS) $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 881dcd5..8f31c4d 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -117,7 +117,6 @@
  */
 
 typedef struct xc_interface_core xc_interface;
-typedef struct xenevtchn_handle xc_evtchn;
 typedef struct xc_interface_core xc_gnttab;
 typedef struct xc_interface_core xc_gntshr;
 
@@ -1085,7 +1084,6 @@ int xc_cpupool_movedomain(xc_interface *xch,
  */
 xc_cpumap_t xc_cpupool_freeinfo(xc_interface *xch);
 
-
 /*
  * EVENT CHANNEL FUNCTIONS
  *
@@ -1120,101 +1118,7 @@ int xc_evtchn_reset(xc_interface *xch,
 typedef struct evtchn_status xc_evtchn_status_t;
 int xc_evtchn_status(xc_interface *xch, xc_evtchn_status_t *status);
 
-/*
- * Return a handle to the event channel driver, or NULL on failure, in
- * which case errno will be set appropriately.
- *
- * Note:
- * After fork a child process must not use any opened xc evtchn
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
- *
- * Before Xen pre-4.1 this function would sometimes report errors with perror.
- */
-xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
-                             unsigned open_flags);
 
-/*
- * Close a handle previously allocated with xc_evtchn_open().
- */
-int xc_evtchn_close(xc_evtchn *xce);
-
-/*
- * Return an fd that can be select()ed on.
- *
- * Note that due to bugs, setting this fd to non blocking may not
- * work: you would hope that it would result in xc_evtchn_pending
- * failing with EWOULDBLOCK if there are no events signaled, but in
- * fact it may block.  (Bug is present in at least Linux 3.12, and
- * perhaps on other platforms or later version.)
- *
- * To be safe, you must use poll() or select() before each call to
- * xc_evtchn_pending.  If you have multiple threads (or processes)
- * sharing a single xce handle this will not work, and there is no
- * straightforward workaround.  Please design your program some other
- * way.
- */
-int xc_evtchn_fd(xc_evtchn *xce);
-
-/*
- * Notify the given event channel. Returns -1 on failure, in which case
- * errno will be set appropriately.
- */
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port);
-
-/*
- * Returns a new event port awaiting interdomain connection from the given
- * domain ID, or -1 on failure, in which case errno will be set appropriately.
- */
-evtchn_port_or_error_t
-xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid);
-
-/*
- * Returns a new event port bound to the remote port for the given domain ID,
- * or -1 on failure, in which case errno will be set appropriately.
- */
-evtchn_port_or_error_t
-xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                           evtchn_port_t remote_port);
-
-/*
- * Bind an event channel to the given VIRQ. Returns the event channel bound to
- * the VIRQ, or -1 on failure, in which case errno will be set appropriately.
- */
-evtchn_port_or_error_t
-xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq);
-
-/*
- * Unbind the given event channel. Returns -1 on failure, in which case errno
- * will be set appropriately.
- */
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port);
-
-/*
- * Return the next event channel to become pending, or -1 on failure, in which
- * case errno will be set appropriately.
- *
- * At the hypervisor level the event channel will have been masked,
- * and then cleared, by the underlying machinery (evtchn kernel
- * driver, or equivalent).  So if the event channel is signaled again
- * after it is returned here, it will be queued up, and delivered
- * again after you unmask it.  (See the documentation in the Xen
- * public header event_channel.h.)
- *
- * On receiving the notification from xc_evtchn_pending, you should
- * normally: check (by other means) what work needs doing; do the
- * necessary work (if any); unmask the event channel with
- * xc_evtchn_unmask (if you want to receive any further
- * notifications).
- */
-evtchn_port_or_error_t
-xc_evtchn_pending(xc_evtchn *xce);
-
-/*
- * Unmask the given event channel. Returns -1 on failure, in which case errno
- * will be set appropriately.
- */
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port);
 
 int xc_physdev_pci_access_modify(xc_interface *xch,
                                  uint32_t domid,
@@ -2851,6 +2755,9 @@ int xc_psr_cat_get_l3_info(xc_interface *xch, uint32_t socket,
                            bool *cdp_enabled);
 #endif
 
+/* Compat shims */
+#include "xenctrl_compat.h"
+
 #endif /* XENCTRL_H */
 
 /*
diff --git a/tools/libxc/include/xenctrl_compat.h b/tools/libxc/include/xenctrl_compat.h
new file mode 100644
index 0000000..48daeb2
--- /dev/null
+++ b/tools/libxc/include/xenctrl_compat.h
@@ -0,0 +1,48 @@
+/*
+ * Compat shims for use of 3rd party consumers of libxenctrl
+ * functionality which has been split into separate libraries.
+ *
+ * New code should use the separate libraries.
+ *
+ * Each interface must be opted-into separately by defining:
+ *
+ * XC_WANT_COMPAT_EVTCHN_API
+ *  - Functions relating to /dev/xen/evtchn
+ */
+#ifndef XENCTRL_COMPAT_H
+#define XENCTRL_COMPAT_H
+
+#ifdef XC_WANT_COMPAT_EVTCHN_API
+
+typedef struct xenevtchn_handle xc_evtchn;
+
+xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
+                             unsigned open_flags);
+int xc_evtchn_close(xc_evtchn *xce);
+int xc_evtchn_fd(xc_evtchn *xce);
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port);
+evtchn_port_or_error_t
+xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid);
+evtchn_port_or_error_t
+xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                           evtchn_port_t remote_port);
+evtchn_port_or_error_t
+xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq);
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port);
+evtchn_port_or_error_t
+xc_evtchn_pending(xc_evtchn *xce);
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port);
+
+#endif /* XC_WANT_COMPAT_EVTCHN_API */
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/include/xenguest.h b/tools/libxc/include/xenguest.h
index 8f918b1..050a537 100644
--- a/tools/libxc/include/xenguest.h
+++ b/tools/libxc/include/xenguest.h
@@ -34,6 +34,12 @@
 #define X86_64_B_SIZE   64 
 #define X86_32_B_SIZE   32
 
+/*
+ * User not using xc_suspend_* / xc_await_suspent may not want to
+ * include the full libxenevtchn API here.
+ */
+typedef struct xenevtchn_handle xenevtchn_handle;
+
 /* callbacks provided by xc_domain_save */
 struct save_callbacks {
     /* Called after expiration of checkpoint interval,
@@ -161,18 +167,18 @@ struct xc_hvm_firmware_module {
  * Sets *lockfd to -1.
  * Has deallocated everything even on error.
  */
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn, int *lockfd);
+int xc_suspend_evtchn_release(xc_interface *xch, xenevtchn_handle *xce, int domid, int suspend_evtchn, int *lockfd);
 
 /**
  * This function eats the initial notification.
  * xce must not be used for anything else
  * See xc_suspend_evtchn_init_sane re lockfd.
  */
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xenevtchn_handle *xce,
                                      int domid, int port, int *lockfd);
 
 /* xce must not be used for anything else */
-int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
+int xc_await_suspend(xc_interface *xch, xenevtchn_handle *xce, int suspend_evtchn);
 
 /**
  * The port will be signaled immediately after this call
@@ -181,7 +187,7 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
  * and fed to xc_suspend_evtchn_release.  (On error *lockfd is
  * undefined and xc_suspend_evtchn_release is not allowed.)
  */
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xenevtchn_handle *xce,
                                 int domid, int port, int *lockfd);
 
 int xc_mark_page_online(xc_interface *xch, unsigned long start,
diff --git a/tools/libxc/xc_evtchn_compat.c b/tools/libxc/xc_evtchn_compat.c
new file mode 100644
index 0000000..5d3e4ba
--- /dev/null
+++ b/tools/libxc/xc_evtchn_compat.c
@@ -0,0 +1,75 @@
+/*
+ * Compat shims for use of 3rd party consumers of libxenctrl xc_evtchn
+ * functionality which has been split into separate libraries.
+ */
+
+#include <xenevtchn.h>
+
+#define XC_WANT_COMPAT_EVTCHN_API
+#include "xenctrl.h"
+
+xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
+                          unsigned open_flags)
+{
+    return xenevtchn_open(logger, open_flags);
+}
+
+int xc_evtchn_close(xc_evtchn *xce)
+{
+    return xenevtchn_close(xce);
+}
+
+int xc_evtchn_fd(xc_evtchn *xce)
+{
+    return xenevtchn_fd(xce);
+}
+
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
+{
+    return xenevtchn_notify(xce, port);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
+{
+    return xenevtchn_bind_unbound_port(xce, domid);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                           evtchn_port_t remote_port)
+{
+    return xenevtchn_bind_interdomain(xce, domid, remote_port);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
+{
+    return xenevtchn_bind_virq(xce, virq);
+}
+
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
+{
+    return xenevtchn_unbind(xce, port);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_pending(xc_evtchn *xce)
+{
+    return xenevtchn_pending(xce);
+}
+
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
+{
+    return xenevtchn_unmask(xce, port);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c
index 4323e16..339997c 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libxc/xc_freebsd_osdep.c
@@ -31,13 +31,11 @@
 #include <sys/ioctl.h>
 
 #include <xen/memory.h>
-#include <xen/sys/evtchn.h>
 
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
 
 #define PRIVCMD_DEV     "/dev/xen/privcmd"
-#define EVTCHN_DEV      "/dev/xen/evtchn"
 
 #define PERROR(_m, _a...) xc_osdep_log(xch,XTL_ERROR,XC_INTERNAL_ERROR,_m \
                   " (%d = %s)", ## _a , errno, xc_strerror(xch, errno))
@@ -251,105 +249,6 @@ static struct xc_osdep_ops freebsd_privcmd_ops = {
     },
 };
 
-/*-------------------------- Evtchn device interface -------------------------*/
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = open(EVTCHN_DEV, O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-/*------------------------------ Evtchn interface ----------------------------*/
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int ret, fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-
-    bind.remote_domain = domid;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-    return ( ret == 0 ) ? bind.port : ret;
-}
-
-evtchn_port_or_error_t
-xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid, evtchn_port_t remote_port)
-{
-    int ret, fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-    return ( ret == 0 ) ? bind.port : ret;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int ret, fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-
-    bind.virq = virq;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-    return ( ret == 0 ) ? bind.port : ret;
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-
-    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-    return 0;
-}
-
 /*---------------------------- FreeBSD interface -----------------------------*/
 static struct xc_osdep_ops *
 freebsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 90544fb..65299d0 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -31,7 +31,6 @@
 #include <sys/ioctl.h>
 
 #include <xen/memory.h>
-#include <xen/sys/evtchn.h>
 #include <xen/sys/gntdev.h>
 #include <xen/sys/gntalloc.h>
 
@@ -461,100 +460,6 @@ static struct xc_osdep_ops linux_privcmd_ops = {
 
 #define DEVXEN "/dev/xen/"
 
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = open(DEVXEN "evtchn", O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-
-    bind.remote_domain = domid;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-
-    bind.virq = virq;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-
-    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-    return 0;
-}
-
 static xc_osdep_handle linux_gnttab_open(xc_gnttab *xcg)
 {
     int fd = open(DEVXEN "gntdev", O_RDWR);
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index 4ace1b5..db7b344 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -19,14 +19,11 @@
  */
 
 #undef NDEBUG
-#include "xen-external/bsd-sys-queue.h"
 #include <mini-os/types.h>
 #include <mini-os/os.h>
 #include <mini-os/mm.h>
 #include <mini-os/lib.h>
 #include <mini-os/gntmap.h>
-#include <mini-os/events.h>
-#include <mini-os/wait.h>
 #include <sys/mman.h>
 
 #include <xen/memory.h>
@@ -41,13 +38,9 @@
 #include "xc_private.h"
 
 void minios_interface_close_fd(int fd);
-void minios_evtchn_close_fd(int fd);
 void minios_gnttab_close_fd(int fd);
 
 extern void minios_interface_close_fd(int fd);
-extern void minios_evtchn_close_fd(int fd);
-
-extern struct wait_queue_head event_queue;
 
 static xc_osdep_handle minios_privcmd_open(xc_interface *xch)
 {
@@ -197,220 +190,6 @@ static struct xc_osdep_ops minios_privcmd_ops = {
     },
 };
 
-
-/* XXX Note: This is not threadsafe */
-static struct evtchn_port_info* port_alloc(int fd) {
-    struct evtchn_port_info *port_info;
-    port_info = malloc(sizeof(struct evtchn_port_info));
-    if (port_info == NULL)
-        return NULL;
-    port_info->pending = 0;
-    port_info->port = -1;
-    port_info->bound = 0;
-
-    LIST_INSERT_HEAD(&files[fd].evtchn.ports, port_info, list);
-    return port_info;
-}
-
-static void port_dealloc(struct evtchn_port_info *port_info) {
-    if (port_info->bound)
-        unbind_evtchn(port_info->port);
-    LIST_REMOVE(port_info, list);
-    free(port_info);
-}
-
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = alloc_fd(FTYPE_EVTCHN);
-    if ( fd == -1 )
-        return -1;
-    LIST_INIT(&files[fd].evtchn.ports);
-    xce->fd = fd;
-    printf("evtchn_open() -> %d\n", fd);
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-void minios_evtchn_close_fd(int fd)
-{
-    struct evtchn_port_info *port_info, *tmp;
-    LIST_FOREACH_SAFE(port_info, &files[fd].evtchn.ports, list, tmp)
-        port_dealloc(port_info);
-
-    files[fd].type = FTYPE_NONE;
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int ret;
-
-    ret = notify_remote_via_evtchn(port);
-
-    if (ret < 0) {
-	errno = -ret;
-	ret = -1;
-    }
-    return ret;
-}
-
-static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
-{
-    int fd = (int)(intptr_t)data;
-    struct evtchn_port_info *port_info;
-    assert(files[fd].type == FTYPE_EVTCHN);
-    mask_evtchn(port);
-    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
-        if (port_info->port == port)
-            goto found;
-    }
-    printk("Unknown port for handle %d\n", fd);
-    return;
-
- found:
-    port_info->pending = 1;
-    files[fd].read = 1;
-    wake_up(&event_queue);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    int ret;
-    evtchn_port_t port;
-
-    assert(get_current() == main_thread);
-    port_info = port_alloc(fd);
-    if (port_info == NULL)
-	return -1;
-
-    printf("xc_evtchn_bind_unbound_port(%d)", domid);
-    ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)fd, &port);
-    printf(" = %d\n", ret);
-
-    if (ret < 0) {
-	port_dealloc(port_info);
-	errno = -ret;
-	return -1;
-    }
-    port_info->bound = 1;
-    port_info->port = port;
-    unmask_evtchn(port);
-    return port;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    evtchn_port_t local_port;
-    int ret;
-
-    assert(get_current() == main_thread);
-    port_info = port_alloc(fd);
-    if (port_info == NULL)
-	return -1;
-
-    printf("xc_evtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
-    ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)fd, &local_port);
-    printf(" = %d\n", ret);
-
-    if (ret < 0) {
-	port_dealloc(port_info);
-	errno = -ret;
-	return -1;
-    }
-    port_info->bound = 1;
-    port_info->port = local_port;
-    unmask_evtchn(local_port);
-    return local_port;
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-
-    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
-        if (port_info->port == port) {
-            port_dealloc(port_info);
-            return 0;
-        }
-    }
-    printf("Warning: couldn't find port %"PRId32" for xc handle %x\n", port, fd);
-    errno = EINVAL;
-    return -1;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    evtchn_port_t port;
-
-    assert(get_current() == main_thread);
-    port_info = port_alloc(fd);
-    if (port_info == NULL)
-	return -1;
-
-    printf("xc_evtchn_bind_virq(%d)", virq);
-    port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)fd);
-
-    if (port < 0) {
-	port_dealloc(port_info);
-	errno = -port;
-	return -1;
-    }
-    port_info->bound = 1;
-    port_info->port = port;
-    unmask_evtchn(port);
-    return port;
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    unsigned long flags;
-    evtchn_port_t ret = -1;
-
-    local_irq_save(flags);
-    files[fd].read = 0;
-
-    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
-        if (port_info->port != -1 && port_info->pending) {
-            if (ret == -1) {
-                ret = port_info->port;
-                port_info->pending = 0;
-            } else {
-                files[fd].read = 1;
-                break;
-            }
-        }
-    }
-    local_irq_restore(flags);
-    return ret;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    unmask_evtchn(port);
-    return 0;
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush)
 {
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index b1b828f..fe4f0a1 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -19,7 +19,6 @@
 
 #include "xc_private.h"
 
-#include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <malloc.h>
@@ -223,114 +222,6 @@ static struct xc_osdep_ops netbsd_privcmd_ops = {
     },
 };
 
-#define EVTCHN_DEV_NAME  "/dev/xenevt"
-
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn * xce, int domid)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-    int ret;
-
-    bind.remote_domain = domid;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-    if (ret == 0)
-	return bind.port;
-    else
-	return -1;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-    int ret;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-    if (ret == 0)
-	return bind.port;
-    else
-	return -1;
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-    int err;
-
-    bind.virq = virq;
-
-    err = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-    if (err)
-	return -1;
-    else
-	return bind.port;
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    return write_exact(fd, (char *)&port, sizeof(port));
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index cb79f22..56d54b6 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -251,46 +251,6 @@ int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
     return xch->ops->u.privcmd.hypercall(xch, xch->ops_handle, hypercall);
 }
 
-xc_evtchn *xc_evtchn_open(xentoollog_logger *logger, unsigned open_flags)
-{
-    xc_evtchn *xce = malloc(sizeof(*xce));
-    int rc;
-
-    if (!xce) return NULL;
-
-    xce->fd = -1;
-    xce->logger = logger;
-    xce->logger_tofree  = NULL;
-
-    if (!xce->logger) {
-        xce->logger = xce->logger_tofree =
-            (xentoollog_logger*)
-            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
-        if (!xce->logger) goto err;
-    }
-
-    rc = osdep_evtchn_open(xce);
-    if ( rc  < 0 ) goto err;
-
-    return xce;
-
-err:
-    osdep_evtchn_close(xce);
-    xtl_logger_destroy(xce->logger_tofree);
-    free(xce);
-    return NULL;
-}
-
-int xc_evtchn_close(xc_evtchn *xce)
-{
-    int rc;
-
-    rc = osdep_evtchn_close(xce);
-    xtl_logger_destroy(xce->logger_tofree);
-    free(xce);
-    return rc;
-}
-
 xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
                              unsigned open_flags)
 {
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index f4d155a..a32accb 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -123,13 +123,6 @@ struct xc_interface_core {
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
 };
 
-struct xenevtchn_handle {
-    xentoollog_logger *logger, *logger_tofree;
-    int fd;
-};
-int osdep_evtchn_open(xc_evtchn *xce);
-int osdep_evtchn_close(xc_evtchn *xce);
-
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 088228a..ed7987c 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -20,7 +20,6 @@
 #include "xc_private.h"
 
 #include <xen/memory.h>
-#include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <malloc.h>
@@ -194,102 +193,6 @@ static struct xc_osdep_ops solaris_privcmd_ops = {
     },
 };
 
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd;
-
-    if ( (fd = open("/dev/xen/evtchn", O_RDWR)) == -1 )
-    {
-        PERROR("Could not open event channel interface");
-        return -1;
-    }
-
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-
-    bind.remote_domain = domid;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-
-    bind.virq = virq;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    return write_exact(fd, (char *)&port, sizeof(port));
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
diff --git a/tools/libxc/xc_suspend.c b/tools/libxc/xc_suspend.c
index bba36e7..8361c7b 100644
--- a/tools/libxc/xc_suspend.c
+++ b/tools/libxc/xc_suspend.c
@@ -16,6 +16,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+#include <xenevtchn.h>
+
 #include "xc_private.h"
 #include "xenguest.h"
 
@@ -124,12 +126,12 @@ static int unlock_suspend_event(xc_interface *xch, int domid, int *lockfd)
     return -1;
 }
 
-int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
+int xc_await_suspend(xc_interface *xch, xenevtchn_handle *xce, int suspend_evtchn)
 {
     int rc;
 
     do {
-        rc = xc_evtchn_pending(xce);
+        rc = xenevtchn_pending(xce);
         if (rc < 0) {
             ERROR("error polling suspend notification channel: %d", rc);
             return -1;
@@ -137,7 +139,7 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
     } while (rc != suspend_evtchn);
 
     /* harmless for one-off suspend */
-    if (xc_evtchn_unmask(xce, suspend_evtchn) < 0)
+    if (xenevtchn_unmask(xce, suspend_evtchn) < 0)
         ERROR("failed to unmask suspend notification channel: %d", rc);
 
     return 0;
@@ -145,16 +147,16 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
 
 /* Internal callers are allowed to call this with suspend_evtchn<0
  * but *lockfd>0. */
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_release(xc_interface *xch, xenevtchn_handle *xce,
                               int domid, int suspend_evtchn, int *lockfd)
 {
     if (suspend_evtchn >= 0)
-        xc_evtchn_unbind(xce, suspend_evtchn);
+        xenevtchn_unbind(xce, suspend_evtchn);
 
     return unlock_suspend_event(xch, domid, lockfd);
 }
 
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xenevtchn_handle *xce,
                                 int domid, int port, int *lockfd)
 {
     int rc, suspend_evtchn = -1;
@@ -164,7 +166,7 @@ int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
         goto cleanup;
     }
 
-    suspend_evtchn = xc_evtchn_bind_interdomain(xce, domid, port);
+    suspend_evtchn = xenevtchn_bind_interdomain(xce, domid, port);
     if (suspend_evtchn < 0) {
         ERROR("failed to bind suspend event channel: %d", suspend_evtchn);
         goto cleanup;
@@ -184,7 +186,7 @@ cleanup:
     return -1;
 }
 
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xenevtchn_handle *xce,
                                      int domid, int port, int *lockfd)
 {
     int suspend_evtchn;
diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index 2abae0c..10b1741 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -20,12 +20,13 @@ LIBUUID_LIBS += -luuid
 endif
 
 LIBXL_LIBS =
-LIBXL_LIBS = $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)
+LIBXL_LIBS = $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)
 ifeq ($(CONFIG_REMUS_NETBUF),y)
 LIBXL_LIBS += $(LIBNL3_LIBS)
 endif
 
 CFLAGS_LIBXL += $(CFLAGS_libxentoollog)
+CFLAGS_LIBXL += $(CFLAGS_libxenevtchn)
 CFLAGS_LIBXL += $(CFLAGS_libxenctrl)
 CFLAGS_LIBXL += $(CFLAGS_libxenguest)
 CFLAGS_LIBXL += $(CFLAGS_libxenstore)
@@ -159,7 +160,7 @@ $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
 $(XEN_INIT_DOM0_OBJS): CFLAGS += $(CFLAGS_libxenstore)
 
 SAVE_HELPER_OBJS = libxl_save_helper.o _libxl_save_msgs_helper.o
-$(SAVE_HELPER_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
+$(SAVE_HELPER_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
 
 PKG_CONFIG = xenlight.pc xlutil.pc
 
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 9207621..249fed8 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -186,7 +186,7 @@ int libxl_ctx_free(libxl_ctx *ctx)
     if (ctx->xch) xc_interface_close(ctx->xch);
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
-    if (ctx->xce) xc_evtchn_close(ctx->xce);
+    if (ctx->xce) xenevtchn_close(ctx->xce);
 
     libxl__poller_put(ctx, ctx->poller_app);
     ctx->poller_app = NULL;
diff --git a/tools/libxl/libxl_dom_suspend.c b/tools/libxl/libxl_dom_suspend.c
index 3313ad1..16f603f 100644
--- a/tools/libxl/libxl_dom_suspend.c
+++ b/tools/libxl/libxl_dom_suspend.c
@@ -109,9 +109,9 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     if ((hvm_s_state == 0) && (dss->guest_evtchn.port >= 0)) {
         LOG(DEBUG, "issuing %s suspend request via event channel",
             dss->hvm ? "PVHVM" : "PV");
-        ret = xc_evtchn_notify(CTX->xce, dss->guest_evtchn.port);
+        ret = xenevtchn_notify(CTX->xce, dss->guest_evtchn.port);
         if (ret < 0) {
-            LOG(ERROR, "xc_evtchn_notify failed ret=%d", ret);
+            LOG(ERROR, "xenevtchn_notify failed ret=%d", ret);
             rc = ERROR_FAIL;
             goto err;
         }
diff --git a/tools/libxl/libxl_event.c b/tools/libxl/libxl_event.c
index 139e49a..8548365 100644
--- a/tools/libxl/libxl_event.c
+++ b/tools/libxl/libxl_event.c
@@ -739,7 +739,7 @@ static void evtchn_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
         /* OK, that's that workaround done.  We can actually check for
          * work for us to do: */
 
-        port = xc_evtchn_pending(CTX->xce);
+        port = xenevtchn_pending(CTX->xce);
         if (port < 0) {
             if (errno == EAGAIN)
                 break;
@@ -765,20 +765,20 @@ static void evtchn_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
 }
 
 int libxl__ctx_evtchn_init(libxl__gc *gc) {
-    xc_evtchn *xce;
+    xenevtchn_handle *xce;
     int rc, fd;
 
     if (CTX->xce)
         return 0;
 
-    xce = xc_evtchn_open(CTX->lg, 0);
+    xce = xenevtchn_open(CTX->lg, 0);
     if (!xce) {
         LOGE(ERROR,"cannot open libxc evtchn handle");
         rc = ERROR_FAIL;
         goto out;
     }
 
-    fd = xc_evtchn_fd(xce);
+    fd = xenevtchn_fd(xce);
     assert(fd >= 0);
 
     rc = libxl_fd_set_nonblock(CTX, fd, 1);
@@ -788,7 +788,7 @@ int libxl__ctx_evtchn_init(libxl__gc *gc) {
     return 0;
 
  out:
-    xc_evtchn_close(xce);
+    xenevtchn_close(xce);
     return rc;
 }
 
@@ -810,14 +810,14 @@ int libxl__ev_evtchn_wait(libxl__gc *gc, libxl__ev_evtchn *evev)
 
     if (!libxl__ev_fd_isregistered(&CTX->evtchn_efd)) {
         rc = libxl__ev_fd_register(gc, &CTX->evtchn_efd, evtchn_fd_callback,
-                                   xc_evtchn_fd(CTX->xce), POLLIN);
+                                   xenevtchn_fd(CTX->xce), POLLIN);
         if (rc) goto out;
     }
 
     if (evev->waiting)
         return 0;
 
-    r = xc_evtchn_unmask(CTX->xce, evev->port);
+    r = xenevtchn_unmask(CTX->xce, evev->port);
     if (r) {
         LOGE(ERROR,"cannot unmask event channel %d",evev->port);
         rc = ERROR_FAIL;
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index a556a38..77bf251 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -48,6 +48,7 @@
 #include <sys/file.h>
 #include <sys/ioctl.h>
 
+#include <xenevtchn.h>
 #include <xenstore.h>
 #include <xenctrl.h>
 #include <xenguest.h>
@@ -448,7 +449,7 @@ struct libxl__ctx {
     uint32_t watch_counter; /* helps disambiguate slot reuse */
     libxl__ev_fd watch_efd;
 
-    xc_evtchn *xce; /* waiting must be done only with libxl__ev_evtchn* */
+    xenevtchn_handle *xce; /* waiting must be done only with libxl__ev_evtchn* */
     LIBXL_LIST_HEAD(, libxl__ev_evtchn) evtchns_waiting;
     libxl__ev_fd evtchn_efd;
 
@@ -925,7 +926,7 @@ static inline int libxl__ev_xswatch_isregistered(const libxl__ev_xswatch *xw)
  * When the event is signaled then the callback will be made, once.
  * Then you must call libxl__ev_evtchn_wait again, if desired.
  *
- * You must NOT call xc_evtchn_unmask.  wait will do that for you.
+ * You must NOT call xenevtchn_unmask.  wait will do that for you.
  *
  * Calling libxl__ev_evtchn_cancel will arrange for libxl to disregard
  * future occurrences of event.  Both libxl__ev_evtchn_wait and
diff --git a/tools/misc/Makefile b/tools/misc/Makefile
index c4490f3..cf6a475 100644
--- a/tools/misc/Makefile
+++ b/tools/misc/Makefile
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 CFLAGS += -Werror
 # Include configure output (config.h)
 CFLAGS += -include $(XEN_ROOT)/tools/config.h
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += $(CFLAGS_xeninclude)
 CFLAGS += $(CFLAGS_libxenstore)
@@ -88,18 +89,18 @@ xenlockprof: xenlockprof.o
 # xen-hptool incorrectly uses libxc internals
 xen-hptool.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc
 xen-hptool: xen-hptool.o
-	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
 
 # xen-mfndump incorrectly uses libxc internals
 xen-mfndump.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc
 xen-mfndump: xen-mfndump.o
-	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(APPEND_LDFLAGS)
 
 xenwatchdogd: xenwatchdogd.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
 
 xen-lowmemd: xen-lowmemd.o
-	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
 
 gtraceview: gtraceview.o
 	$(CC) $(LDFLAGS) -o $@ $< $(CURSES_LIBS) $(TINFO_LIBS) $(APPEND_LDFLAGS)
diff --git a/tools/misc/xen-hptool.c b/tools/misc/xen-hptool.c
index c7561a9..ebcc9e8 100644
--- a/tools/misc/xen-hptool.c
+++ b/tools/misc/xen-hptool.c
@@ -1,3 +1,4 @@
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xc_private.h>
 #include <xc_core.h>
@@ -98,7 +99,7 @@ static int hp_mem_query_func(int argc, char *argv[])
     return ret;
 }
 
-static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
+static int suspend_guest(xc_interface *xch, xenevtchn_handle *xce, int domid,
                          int *evtchn, int *lockfd)
 {
     int port, rc, suspend_evtchn = -1;
@@ -123,7 +124,7 @@ static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
     }
     *evtchn = suspend_evtchn;
 
-    rc = xc_evtchn_notify(xce, suspend_evtchn);
+    rc = xenevtchn_notify(xce, suspend_evtchn);
     if (rc < 0)
     {
         fprintf(stderr, "Failed to notify suspend channel: errno %d\n", rc);
@@ -198,8 +199,8 @@ static int hp_mem_offline_func(int argc, char *argv[])
                 else if (status & PG_OFFLINE_OWNED)
                 {
                     int result, suspend_evtchn = -1, suspend_lockfd = -1;
-                    xc_evtchn *xce;
-                    xce = xc_evtchn_open(NULL, 0);
+                    xenevtchn_handle *xce;
+                    xce = xenevtchn_open(NULL, 0);
 
                     if (xce == NULL)
                     {
@@ -214,7 +215,7 @@ static int hp_mem_offline_func(int argc, char *argv[])
                     {
                         fprintf(stderr, "Failed to suspend guest %d for"
                                 " mfn %lx\n", domid, mfn);
-                        xc_evtchn_close(xce);
+                        xenevtchn_close(xce);
                         return -1;
                     }
 
@@ -238,7 +239,7 @@ static int hp_mem_offline_func(int argc, char *argv[])
                     xc_domain_resume(xch, domid, 1);
                     xc_suspend_evtchn_release(xch, xce, domid,
                                               suspend_evtchn, &suspend_lockfd);
-                    xc_evtchn_close(xce);
+                    xenevtchn_close(xce);
                 }
                 break;
             }
diff --git a/tools/misc/xen-lowmemd.c b/tools/misc/xen-lowmemd.c
index 82ffd75..3200404 100644
--- a/tools/misc/xen-lowmemd.c
+++ b/tools/misc/xen-lowmemd.c
@@ -4,22 +4,23 @@
  */
 
 #include <stdio.h>
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xenstore.h>
 #include <stdlib.h>
 #include <string.h>
 
 static evtchn_port_t virq_port      = -1;
-static xc_evtchn *xce_handle        = NULL;
+static xenevtchn_handle *xce_handle = NULL;
 static xc_interface *xch            = NULL;
 static struct xs_handle *xs_handle  = NULL;
 
 void cleanup(void)
 {
     if (virq_port > -1)
-        xc_evtchn_unbind(xce_handle, virq_port);
+        xenevtchn_unbind(xce_handle, virq_port);
     if (xce_handle)
-        xc_evtchn_close(xce_handle);
+        xenevtchn_close(xce_handle);
     if (xch)
         xc_interface_close(xch);
     if (xs_handle)
@@ -94,7 +95,7 @@ int main(int argc, char *argv[])
         return 1;
     }
 
-	xce_handle = xc_evtchn_open(NULL, 0);
+	xce_handle = xenevtchn_open(NULL, 0);
 	if (xce_handle == NULL)
     {
         perror("Failed to open evtchn device");
@@ -108,7 +109,7 @@ int main(int argc, char *argv[])
         return 3;
     }
 
-	if ((rc = xc_evtchn_bind_virq(xce_handle, VIRQ_ENOMEM)) == -1)
+	if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_ENOMEM)) == -1)
     {
         perror("Failed to bind to domain exception virq port");
         return 4;
@@ -120,7 +121,7 @@ int main(int argc, char *argv[])
     {
         evtchn_port_t port;
 
-        if ((port = xc_evtchn_pending(xce_handle)) == -1)
+        if ((port = xenevtchn_pending(xce_handle)) == -1)
         {
             perror("Failed to listen for pending event channel");
             return 5;
@@ -134,7 +135,7 @@ int main(int argc, char *argv[])
             return 6;
         }
 
-        if (xc_evtchn_unmask(xce_handle, port) == -1)
+        if (xenevtchn_unmask(xce_handle, port) == -1)
         {
             perror("Failed to unmask port");
             return 7;
diff --git a/tools/ocaml/libs/eventchn/Makefile b/tools/ocaml/libs/eventchn/Makefile
index 2d8d618..154efd4 100644
--- a/tools/ocaml/libs/eventchn/Makefile
+++ b/tools/ocaml/libs/eventchn/Makefile
@@ -2,13 +2,13 @@ TOPLEVEL=$(CURDIR)/../..
 XEN_ROOT=$(TOPLEVEL)/../..
 include $(TOPLEVEL)/common.make
 
-CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude)
+CFLAGS += $(CFLAGS_libxenevtchn) $(CFLAGS_xeninclude)
 
 OBJS = xeneventchn
 INTF = $(foreach obj, $(OBJS),$(obj).cmi)
 LIBS = xeneventchn.cma xeneventchn.cmxa
 
-LIBS_xeneventchn = $(LDLIBS_libxenctrl)
+LIBS_xeneventchn = $(LDLIBS_libxenevtchn)
 
 all: $(INTF) $(LIBS) $(PROGRAMS)
 
diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
index 5939e7c..c2d4737 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
+++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
@@ -24,7 +24,7 @@
 #include <xen/sysctl.h>
 #include <xen/xen.h>
 #include <xen/sys/evtchn.h>
-#include <xenctrl.h>
+#include <xenevtchn.h>
 
 #define CAML_NAME_SPACE
 #include <caml/mlvalues.h>
@@ -34,14 +34,14 @@
 #include <caml/callback.h>
 #include <caml/fail.h>
 
-#define _H(__h) ((xc_evtchn *)(__h))
+#define _H(__h) ((xenevtchn_handle *)(__h))
 
 CAMLprim value stub_eventchn_init(void)
 {
 	CAMLparam0();
 	CAMLlocal1(result);
 
-	xc_evtchn *xce = xc_evtchn_open(NULL, XC_OPENFLAG_NON_REENTRANT);
+	xenevtchn_handle *xce = xenevtchn_open(NULL, 0);
 	if (xce == NULL)
 		caml_failwith("open failed");
 
@@ -55,7 +55,7 @@ CAMLprim value stub_eventchn_fd(value xce)
 	CAMLlocal1(result);
 	int fd;
 
-	fd = xc_evtchn_fd(_H(xce));
+	fd = xenevtchn_fd(_H(xce));
 	if (fd == -1)
 		caml_failwith("evtchn fd failed");
 
@@ -69,7 +69,7 @@ CAMLprim value stub_eventchn_notify(value xce, value port)
 	CAMLparam2(xce, port);
 	int rc;
 
-	rc = xc_evtchn_notify(_H(xce), Int_val(port));
+	rc = xenevtchn_notify(_H(xce), Int_val(port));
 	if (rc == -1)
 		caml_failwith("evtchn notify failed");
 
@@ -83,7 +83,7 @@ CAMLprim value stub_eventchn_bind_interdomain(value xce, value domid,
 	CAMLlocal1(port);
 	evtchn_port_or_error_t rc;
 
-	rc = xc_evtchn_bind_interdomain(_H(xce), Int_val(domid), Int_val(remote_port));
+	rc = xenevtchn_bind_interdomain(_H(xce), Int_val(domid), Int_val(remote_port));
 	if (rc == -1)
 		caml_failwith("evtchn bind_interdomain failed");
 	port = Val_int(rc);
@@ -97,7 +97,7 @@ CAMLprim value stub_eventchn_bind_dom_exc_virq(value xce)
 	CAMLlocal1(port);
 	evtchn_port_or_error_t rc;
 
-	rc = xc_evtchn_bind_virq(_H(xce), VIRQ_DOM_EXC);
+	rc = xenevtchn_bind_virq(_H(xce), VIRQ_DOM_EXC);
 	if (rc == -1)
 		caml_failwith("evtchn bind_dom_exc_virq failed");
 	port = Val_int(rc);
@@ -110,7 +110,7 @@ CAMLprim value stub_eventchn_unbind(value xce, value port)
 	CAMLparam2(xce, port);
 	int rc;
 
-	rc = xc_evtchn_unbind(_H(xce), Int_val(port));
+	rc = xenevtchn_unbind(_H(xce), Int_val(port));
 	if (rc == -1)
 		caml_failwith("evtchn unbind failed");
 
@@ -123,7 +123,7 @@ CAMLprim value stub_eventchn_pending(value xce)
 	CAMLlocal1(result);
 	evtchn_port_or_error_t port;
 
-	port = xc_evtchn_pending(_H(xce));
+	port = xenevtchn_pending(_H(xce));
 	if (port == -1)
 		caml_failwith("evtchn pending failed");
 	result = Val_int(port);
@@ -137,7 +137,7 @@ CAMLprim value stub_eventchn_unmask(value xce, value _port)
 	evtchn_port_t port;
 
 	port = Int_val(_port);
-	if (xc_evtchn_unmask(_H(xce), port))
+	if (xenevtchn_unmask(_H(xce), port))
 		caml_failwith("evtchn unmask failed");
 	CAMLreturn(Val_unit);
 }
diff --git a/tools/python/setup.py b/tools/python/setup.py
index 604b314..8faf1c0 100644
--- a/tools/python/setup.py
+++ b/tools/python/setup.py
@@ -8,13 +8,18 @@ extra_compile_args  = [ "-fno-strict-aliasing", "-Werror" ]
 
 PATH_XEN      = XEN_ROOT + "/tools/include"
 PATH_LIBXENTOOLLOG = XEN_ROOT + "/tools/libs/toollog"
+PATH_LIBXENEVTCHN = XEN_ROOT + "/tools/libs/evtchn"
 PATH_LIBXC    = XEN_ROOT + "/tools/libxc"
 PATH_LIBXL    = XEN_ROOT + "/tools/libxl"
 PATH_XENSTORE = XEN_ROOT + "/tools/xenstore"
 
 xc = Extension("xc",
                extra_compile_args = extra_compile_args,
-               include_dirs       = [ PATH_XEN, PATH_LIBXENTOOLLOG + "/include", PATH_LIBXC + "/include", "xen/lowlevel/xc" ],
+               include_dirs       = [ PATH_XEN,
+                                      PATH_LIBXENTOOLLOG + "/include",
+                                      PATH_LIBXENEVTCHN + "/include",
+                                      PATH_LIBXC + "/include",
+                                      "xen/lowlevel/xc" ],
                library_dirs       = [ PATH_LIBXC ],
                libraries          = [ "xenctrl", "xenguest" ],
                depends            = [ PATH_LIBXC + "/libxenctrl.so", PATH_LIBXC + "/libxenguest.so" ],
diff --git a/tools/tests/xen-access/Makefile b/tools/tests/xen-access/Makefile
index dd70f9f..25ff469 100644
--- a/tools/tests/xen-access/Makefile
+++ b/tools/tests/xen-access/Makefile
@@ -5,6 +5,7 @@ CFLAGS += -Werror
 
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += $(CFLAGS_libxenguest)
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_xeninclude)
 
 TARGETS-y := xen-access
@@ -24,6 +25,6 @@ clean:
 distclean: clean
 
 xen-access: xen-access.o Makefile
-	$(CC) -o $@ $< $(LDFLAGS) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest)
+	$(CC) -o $@ $< $(LDFLAGS) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenevtchn)
 
 -include $(DEPS)
diff --git a/tools/tests/xen-access/xen-access.c b/tools/tests/xen-access/xen-access.c
index a52ca6e..7993947 100644
--- a/tools/tests/xen-access/xen-access.c
+++ b/tools/tests/xen-access/xen-access.c
@@ -39,6 +39,7 @@
 #include <sys/poll.h>
 
 #include <xenctrl.h>
+#include <xenevtchn.h>
 #include <xen/vm_event.h>
 
 #if defined(__arm__) || defined(__aarch64__)
@@ -54,7 +55,7 @@
 
 typedef struct vm_event {
     domid_t domain_id;
-    xc_evtchn *xce_handle;
+    xenevtchn_handle *xce_handle;
     int port;
     vm_event_back_ring_t back_ring;
     uint32_t evtchn_port;
@@ -77,9 +78,9 @@ static void close_handler(int sig)
     interrupted = sig;
 }
 
-int xc_wait_for_event_or_timeout(xc_interface *xch, xc_evtchn *xce, unsigned long ms)
+int xc_wait_for_event_or_timeout(xc_interface *xch, xenevtchn_handle *xce, unsigned long ms)
 {
-    struct pollfd fd = { .fd = xc_evtchn_fd(xce), .events = POLLIN | POLLERR };
+    struct pollfd fd = { .fd = xenevtchn_fd(xce), .events = POLLIN | POLLERR };
     int port;
     int rc;
 
@@ -95,14 +96,14 @@ int xc_wait_for_event_or_timeout(xc_interface *xch, xc_evtchn *xce, unsigned lon
 
     if ( rc == 1 )
     {
-        port = xc_evtchn_pending(xce);
+        port = xenevtchn_pending(xce);
         if ( port == -1 )
         {
             ERROR("Failed to read port from event channel");
             goto err;
         }
 
-        rc = xc_evtchn_unmask(xce, port);
+        rc = xenevtchn_unmask(xce, port);
         if ( rc != 0 )
         {
             ERROR("Failed to unmask event channel port");
@@ -143,7 +144,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     /* Unbind VIRQ */
     if ( evtchn_bind )
     {
-        rc = xc_evtchn_unbind(xenaccess->vm_event.xce_handle,
+        rc = xenevtchn_unbind(xenaccess->vm_event.xce_handle,
                               xenaccess->vm_event.port);
         if ( rc != 0 )
         {
@@ -155,7 +156,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     /* Close event channel */
     if ( evtchn_open )
     {
-        rc = xc_evtchn_close(xenaccess->vm_event.xce_handle);
+        rc = xenevtchn_close(xenaccess->vm_event.xce_handle);
         if ( rc != 0 )
         {
             ERROR("Error closing event channel");
@@ -223,7 +224,7 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     mem_access_enable = 1;
 
     /* Open event channel */
-    xenaccess->vm_event.xce_handle = xc_evtchn_open(NULL, 0);
+    xenaccess->vm_event.xce_handle = xenevtchn_open(NULL, 0);
     if ( xenaccess->vm_event.xce_handle == NULL )
     {
         ERROR("Failed to open event channel");
@@ -232,7 +233,7 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     evtchn_open = 1;
 
     /* Bind event notification */
-    rc = xc_evtchn_bind_interdomain(xenaccess->vm_event.xce_handle,
+    rc = xenevtchn_bind_interdomain(xenaccess->vm_event.xce_handle,
                                     xenaccess->vm_event.domain_id,
                                     xenaccess->vm_event.evtchn_port);
     if ( rc < 0 )
@@ -678,7 +679,7 @@ int main(int argc, char *argv[])
         }
 
         /* Tell Xen page is ready */
-        rc = xc_evtchn_notify(xenaccess->vm_event.xce_handle,
+        rc = xenevtchn_notify(xenaccess->vm_event.xce_handle,
                               xenaccess->vm_event.port);
 
         if ( rc != 0 )
diff --git a/tools/xcutils/Makefile b/tools/xcutils/Makefile
index fff519d..2d1f112 100644
--- a/tools/xcutils/Makefile
+++ b/tools/xcutils/Makefile
@@ -16,8 +16,8 @@ PROGRAMS = readnotes lsevtchn
 CFLAGS += -Werror
 
 # incorrectly uses libxc internals
-CFLAGS_readnotes.o  := $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) -I$(XEN_ROOT)/tools/libxc
-CFLAGS_lsevtchn.o   := $(CFLAGS_libxenctrl)
+CFLAGS_readnotes.o  := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) -I$(XEN_ROOT)/tools/libxc
+CFLAGS_lsevtchn.o   := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl)
 
 .PHONY: all
 all: build
diff --git a/tools/xenmon/Makefile b/tools/xenmon/Makefile
index 20ea100..98056f2 100644
--- a/tools/xenmon/Makefile
+++ b/tools/xenmon/Makefile
@@ -14,8 +14,10 @@ XEN_ROOT=$(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS  += -Werror
+CFLAGS  += $(CFLAGS_libxenevtchn)
 CFLAGS  += $(CFLAGS_libxenctrl)
 LDLIBS  += $(LDLIBS_libxenctrl)
+LDLIBS  += $(LDLIBS_libxenevtchn)
 
 SCRIPTS = xenmon.py
 
diff --git a/tools/xenmon/xenbaked.c b/tools/xenmon/xenbaked.c
index 1ddb29b..e4602ef 100644
--- a/tools/xenmon/xenbaked.c
+++ b/tools/xenmon/xenbaked.c
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <signal.h>
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xen/xen.h>
 #include <string.h>
@@ -267,7 +268,7 @@ static void log_event(int event_id)
 }
 
 int virq_port;
-xc_evtchn *xce_handle = NULL;
+xenevtchn_handle *xce_handle = NULL;
 
 /* Returns the event channel handle. */
 /* Stolen from xenstore code */
@@ -279,12 +280,12 @@ static int eventchn_init(void)
     if (0)
         return -1;
   
-    xce_handle = xc_evtchn_open(NULL, 0);
+    xce_handle = xenevtchn_open(NULL, 0);
 
     if (xce_handle == NULL)
         perror("Failed to open evtchn device");
   
-    if ((rc = xc_evtchn_bind_virq(xce_handle, VIRQ_TBUF)) == -1)
+    if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_TBUF)) == -1)
         perror("Failed to bind to domain exception virq port");
     virq_port = rc;
   
@@ -304,7 +305,7 @@ static void wait_for_event(void)
         return;
     }
 
-    evtchn_fd = xc_evtchn_fd(xce_handle);
+    evtchn_fd = xenevtchn_fd(xce_handle);
 
     FD_ZERO(&inset);
     FD_SET(evtchn_fd, &inset);
@@ -314,13 +315,13 @@ static void wait_for_event(void)
     ret = select(evtchn_fd+1, &inset, NULL, NULL, &tv);
   
     if ( (ret == 1) && FD_ISSET(evtchn_fd, &inset)) {
-        if ((port = xc_evtchn_pending(xce_handle)) == -1)
+        if ((port = xenevtchn_pending(xce_handle)) == -1)
             perror("Failed to read from event fd");
     
         //    if (port == virq_port)
         //      printf("got the event I was looking for\r\n");
 
-        if (xc_evtchn_unmask(xce_handle, port) == -1)
+        if (xenevtchn_unmask(xce_handle, port) == -1)
             perror("Failed to write to event fd");
     }
 }
diff --git a/tools/xenpaging/Makefile b/tools/xenpaging/Makefile
index e63d894..d491867 100644
--- a/tools/xenpaging/Makefile
+++ b/tools/xenpaging/Makefile
@@ -2,8 +2,8 @@ XEN_ROOT=$(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 # xenpaging.c and file_ops.c incorrectly use libxc internals
-CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) -I$(XEN_ROOT)/tools/libxc
-LDLIBS += $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
+CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) -I$(XEN_ROOT)/tools/libxc
+LDLIBS += $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
 LDFLAGS += $(PTHREAD_LDFLAGS)
 
 POLICY    = default
diff --git a/tools/xenpaging/xenpaging.c b/tools/xenpaging/xenpaging.c
index b5ffee6..df99c6a 100644
--- a/tools/xenpaging/xenpaging.c
+++ b/tools/xenpaging/xenpaging.c
@@ -73,7 +73,7 @@ static void xenpaging_mem_paging_flush_ioemu_cache(struct xenpaging *paging)
 static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
 {
     xc_interface *xch = paging->xc_handle;
-    xc_evtchn *xce = paging->vm_event.xce_handle;
+    xenevtchn_handle *xce = paging->vm_event.xce_handle;
     char **vec, *val;
     unsigned int num;
     struct pollfd fd[2];
@@ -82,7 +82,7 @@ static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
     int timeout;
 
     /* Wait for event channel and xenstore */
-    fd[0].fd = xc_evtchn_fd(xce);
+    fd[0].fd = xenevtchn_fd(xce);
     fd[0].events = POLLIN | POLLERR;
     fd[1].fd = xs_fileno(paging->xs_handle);
     fd[1].events = POLLIN | POLLERR;
@@ -146,7 +146,7 @@ static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
     if ( rc && fd[0].revents & POLLIN )
     {
         DPRINTF("Got event from evtchn\n");
-        port = xc_evtchn_pending(xce);
+        port = xenevtchn_pending(xce);
         if ( port == -1 )
         {
             PERROR("Failed to read port from event channel");
@@ -154,7 +154,7 @@ static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
             goto err;
         }
 
-        rc = xc_evtchn_unmask(xce, port);
+        rc = xenevtchn_unmask(xce, port);
         if ( rc < 0 )
         {
             PERROR("Failed to unmask event channel port");
@@ -393,7 +393,7 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[])
     }
 
     /* Open event channel */
-    paging->vm_event.xce_handle = xc_evtchn_open(NULL, 0);
+    paging->vm_event.xce_handle = xenevtchn_open(NULL, 0);
     if ( paging->vm_event.xce_handle == NULL )
     {
         PERROR("Failed to open event channel");
@@ -401,7 +401,7 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[])
     }
 
     /* Bind event notification */
-    rc = xc_evtchn_bind_interdomain(paging->vm_event.xce_handle,
+    rc = xenevtchn_bind_interdomain(paging->vm_event.xce_handle,
                                     paging->vm_event.domain_id,
                                     paging->vm_event.evtchn_port);
     if ( rc < 0 )
@@ -531,7 +531,7 @@ static void xenpaging_teardown(struct xenpaging *paging)
     }
 
     /* Unbind VIRQ */
-    rc = xc_evtchn_unbind(paging->vm_event.xce_handle, paging->vm_event.port);
+    rc = xenevtchn_unbind(paging->vm_event.xce_handle, paging->vm_event.port);
     if ( rc != 0 )
     {
         PERROR("Error unbinding event port");
@@ -539,7 +539,7 @@ static void xenpaging_teardown(struct xenpaging *paging)
     paging->vm_event.port = -1;
 
     /* Close event channel */
-    rc = xc_evtchn_close(paging->vm_event.xce_handle);
+    rc = xenevtchn_close(paging->vm_event.xce_handle);
     if ( rc != 0 )
     {
         PERROR("Error closing event channel");
@@ -692,7 +692,7 @@ static int xenpaging_resume_page(struct xenpaging *paging, vm_event_response_t *
     }
 
     /* Tell Xen page is ready */
-    return xc_evtchn_notify(paging->vm_event.xce_handle, paging->vm_event.port);
+    return xenevtchn_notify(paging->vm_event.xce_handle, paging->vm_event.port);
 }
 
 static int xenpaging_populate_page(struct xenpaging *paging, unsigned long gfn, int i)
diff --git a/tools/xenpaging/xenpaging.h b/tools/xenpaging/xenpaging.h
index c6ab77c..d0f8d20 100644
--- a/tools/xenpaging/xenpaging.h
+++ b/tools/xenpaging/xenpaging.h
@@ -24,6 +24,7 @@
 #define __XEN_PAGING2_H__
 
 
+#include <xenevtchn.h>
 #include <xc_private.h>
 #include <xen/event_channel.h>
 #include <xen/vm_event.h>
@@ -32,7 +33,7 @@
 
 struct vm_event {
     domid_t domain_id;
-    xc_evtchn *xce_handle;
+    xenevtchn_handle *xce_handle;
     int port;
     vm_event_back_ring_t back_ring;
     uint32_t evtchn_port;
diff --git a/tools/xenstore/Makefile b/tools/xenstore/Makefile
index 1b4a494..c161046 100644
--- a/tools/xenstore/Makefile
+++ b/tools/xenstore/Makefile
@@ -9,6 +9,7 @@ CFLAGS += -I.
 # Include configure output (config.h)
 CFLAGS += -include $(XEN_ROOT)/tools/config.h
 CFLAGS += -I./include
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += -DXEN_LIB_STORED="\"$(XEN_LIB_STORED)\""
 
@@ -75,10 +76,10 @@ endif
 init-xenstore-domain.o: CFLAGS += $(CFLAGS_libxenguest)
 
 init-xenstore-domain: init-xenstore-domain.o $(LIBXENSTORE)
-	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) -o $@ $(APPEND_LDFLAGS)
+	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) -o $@ $(APPEND_LDFLAGS)
 
 xenstored: $(XENSTORED_OBJS)
-	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
+	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
 
 xenstored.a: $(XENSTORED_OBJS)
 	$(AR) cr $@ $^
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 3c0307e..f62c192 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -40,6 +40,8 @@
 #include <assert.h>
 #include <setjmp.h>
 
+#include <xenevtchn.h>
+
 #include "utils.h"
 #include "list.h"
 #include "talloc.h"
@@ -63,7 +65,7 @@
 #include <systemd/sd-daemon.h>
 #endif
 
-extern xc_evtchn *xce_handle; /* in xenstored_domain.c */
+extern xenevtchn_handle *xce_handle; /* in xenstored_domain.c */
 static int xce_pollfd_idx = -1;
 static struct pollfd *fds;
 static unsigned int current_array_size;
@@ -372,7 +374,7 @@ static void initialize_fds(int sock, int *p_sock_pollfd_idx,
 			set_fd(reopen_log_pipe[0], POLLIN|POLLPRI);
 
 	if (xce_handle != NULL)
-		xce_pollfd_idx = set_fd(xc_evtchn_fd(xce_handle),
+		xce_pollfd_idx = set_fd(xenevtchn_fd(xce_handle),
 					POLLIN|POLLPRI);
 
 	list_for_each_entry(conn, &connections, list) {
diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_domain.c
index dcd6581..6ceec29 100644
--- a/tools/xenstore/xenstored_domain.c
+++ b/tools/xenstore/xenstored_domain.c
@@ -29,6 +29,7 @@
 #include "xenstored_transaction.h"
 #include "xenstored_watch.h"
 
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xen/grant_table.h>
 
@@ -36,7 +37,7 @@ static xc_interface **xc_handle;
 xc_gnttab **xcg_handle;
 static evtchn_port_t virq_port;
 
-xc_evtchn *xce_handle = NULL;
+xenevtchn_handle *xce_handle = NULL;
 
 struct domain
 {
@@ -128,7 +129,7 @@ static int writechn(struct connection *conn,
 	xen_mb();
 	intf->rsp_prod += len;
 
-	xc_evtchn_notify(xce_handle, conn->domain->port);
+	xenevtchn_notify(xce_handle, conn->domain->port);
 
 	return len;
 }
@@ -158,7 +159,7 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
 	xen_mb();
 	intf->req_cons += len;
 
-	xc_evtchn_notify(xce_handle, conn->domain->port);
+	xenevtchn_notify(xce_handle, conn->domain->port);
 
 	return len;
 }
@@ -190,7 +191,7 @@ static int destroy_domain(void *_domain)
 	list_del(&domain->list);
 
 	if (domain->port) {
-		if (xc_evtchn_unbind(xce_handle, domain->port) == -1)
+		if (xenevtchn_unbind(xce_handle, domain->port) == -1)
 			eprintf("> Unbinding port %i failed!\n", domain->port);
 	}
 
@@ -239,13 +240,13 @@ void handle_event(void)
 {
 	evtchn_port_t port;
 
-	if ((port = xc_evtchn_pending(xce_handle)) == -1)
+	if ((port = xenevtchn_pending(xce_handle)) == -1)
 		barf_perror("Failed to read from event fd");
 
 	if (port == virq_port)
 		domain_cleanup();
 
-	if (xc_evtchn_unmask(xce_handle, port) == -1)
+	if (xenevtchn_unmask(xce_handle, port) == -1)
 		barf_perror("Failed to write to event fd");
 }
 
@@ -287,7 +288,7 @@ static struct domain *new_domain(void *context, unsigned int domid,
 	talloc_set_destructor(domain, destroy_domain);
 
 	/* Tell kernel we're interested in this event. */
-	rc = xc_evtchn_bind_interdomain(xce_handle, domid, port);
+	rc = xenevtchn_bind_interdomain(xce_handle, domid, port);
 	if (rc == -1)
 	    return NULL;
 	domain->port = rc;
@@ -392,8 +393,8 @@ void do_introduce(struct connection *conn, struct buffered_data *in)
 	} else if ((domain->mfn == mfn) && (domain->conn != conn)) {
 		/* Use XS_INTRODUCE for recreating the xenbus event-channel. */
 		if (domain->port)
-			xc_evtchn_unbind(xce_handle, domain->port);
-		rc = xc_evtchn_bind_interdomain(xce_handle, domid, port);
+			xenevtchn_unbind(xce_handle, domain->port);
+		rc = xenevtchn_bind_interdomain(xce_handle, domid, port);
 		domain->port = (rc == -1) ? 0 : rc;
 		domain->remote_port = port;
 	} else {
@@ -614,7 +615,7 @@ static int dom0_init(void)
 
 	talloc_steal(dom0->conn, dom0); 
 
-	xc_evtchn_notify(xce_handle, dom0->port); 
+	xenevtchn_notify(xce_handle, dom0->port); 
 
 	return 0; 
 }
@@ -643,7 +644,7 @@ void domain_init(void)
 	else
 		talloc_set_destructor(xcg_handle, close_xcg_handle);
 
-	xce_handle = xc_evtchn_open(NULL, 0);
+	xce_handle = xenevtchn_open(NULL, 0);
 
 	if (xce_handle == NULL)
 		barf_perror("Failed to open evtchn device");
@@ -651,7 +652,7 @@ void domain_init(void)
 	if (dom0_init() != 0) 
 		barf_perror("Failed to initialize dom0 state"); 
 
-	if ((rc = xc_evtchn_bind_virq(xce_handle, VIRQ_DOM_EXC)) == -1)
+	if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_DOM_EXC)) == -1)
 		barf_perror("Failed to bind to domain exception virq port");
 	virq_port = rc;
 }
diff --git a/tools/xentrace/Makefile b/tools/xentrace/Makefile
index 6c13cd1..0157be2 100644
--- a/tools/xentrace/Makefile
+++ b/tools/xentrace/Makefile
@@ -3,8 +3,11 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS += -Werror
 
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
-LDLIBS += $(LDLIBS_libxenctrl) $(ARGP_LDFLAGS)
+LDLIBS += $(LDLIBS_libxenevtchn)
+LDLIBS += $(LDLIBS_libxenctrl)
+LDLIBS += $(ARGP_LDFLAGS)
 
 BIN-$(CONFIG_X86) = xenalyze
 BIN      = $(BIN-y)
diff --git a/tools/xentrace/xentrace.c b/tools/xentrace/xentrace.c
index 4ee1458..c970d42 100644
--- a/tools/xentrace/xentrace.c
+++ b/tools/xentrace/xentrace.c
@@ -30,6 +30,7 @@
 #include <xen/xen.h>
 #include <xen/trace.h>
 
+#include <xenevtchn.h>
 #include <xenctrl.h>
 
 #define PERROR(_m, _a...)                                       \
@@ -74,7 +75,7 @@ settings_t opts;
 int interrupted = 0; /* gets set if we get a SIGHUP */
 
 static xc_interface *xc_handle;
-static xc_evtchn *xce_handle = NULL;
+static xenevtchn_handle *xce_handle = NULL;
 static int virq_port = -1;
 static int outfd = 1;
 
@@ -602,13 +603,13 @@ static void event_init(void)
 {
     int rc;
 
-    xce_handle = xc_evtchn_open(NULL, 0);
+    xce_handle = xenevtchn_open(NULL, 0);
     if (xce_handle == NULL) {
         perror("event channel open");
         exit(EXIT_FAILURE);
     }
 
-    rc = xc_evtchn_bind_virq(xce_handle, VIRQ_TBUF);
+    rc = xenevtchn_bind_virq(xce_handle, VIRQ_TBUF);
     if (rc == -1) {
         PERROR("failed to bind to VIRQ port");
         exit(EXIT_FAILURE);
@@ -623,7 +624,7 @@ static void event_init(void)
 static void wait_for_event_or_timeout(unsigned long milliseconds)
 {
     int rc;
-    struct pollfd fd = { .fd = xc_evtchn_fd(xce_handle),
+    struct pollfd fd = { .fd = xenevtchn_fd(xce_handle),
                          .events = POLLIN | POLLERR };
     int port;
 
@@ -636,7 +637,7 @@ static void wait_for_event_or_timeout(unsigned long milliseconds)
     }
 
     if (rc == 1) {
-        port = xc_evtchn_pending(xce_handle);
+        port = xenevtchn_pending(xce_handle);
         if (port == -1) {
             PERROR("failed to read port from evtchn");
             exit(EXIT_FAILURE);
@@ -647,7 +648,7 @@ static void wait_for_event_or_timeout(unsigned long milliseconds)
                     port, virq_port);
             exit(EXIT_FAILURE);
         }
-        rc = xc_evtchn_unmask(xce_handle, port);
+        rc = xenevtchn_unmask(xce_handle, port);
         if (rc == -1) {
             PERROR("failed to write port to evtchn");
             exit(EXIT_FAILURE);
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 03/29] tools: Arrange to check public headers for ANSI compatiblity
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 01/29] tools/libxc: Remove osdep indirection for xc_evtchn Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 04/29] tools/libxc: Remove osdep indirection for xc_gnt{shr, tab} Ian Campbell
                     ` (25 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Using the same rune as we use for the Xen public headers, except we do
not need stdint.h here.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
v7: Drop -pendantic: Some library headers include parts of
    xen/include/public, which in turn are not -pedantic clean
    Retained ack, hope that's ok.
---
 .gitignore                  | 2 ++
 tools/Rules.mk              | 8 ++++++++
 tools/libs/evtchn/Makefile  | 4 +++-
 tools/libs/toollog/Makefile | 5 ++++-
 4 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/.gitignore b/.gitignore
index f79a7b3..aa92d78 100644
--- a/.gitignore
+++ b/.gitignore
@@ -87,6 +87,8 @@ tools/config.cache
 config/Tools.mk
 config/Stubdom.mk
 config/Docs.mk
+tools/libs/toollog/headers.chk
+tools/libs/evtchn/headers.chk
 tools/blktap2/daemon/blktapctrl
 tools/blktap2/drivers/img2qcow
 tools/blktap2/drivers/lock-util
diff --git a/tools/Rules.mk b/tools/Rules.mk
index 75d02c4..0c83e22 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -176,6 +176,14 @@ INSTALL_PYTHON_PROG = \
 %.opic: %.S
 	$(CC) $(CPPFLAGS) -DPIC $(CFLAGS) $(CFLAGS.opic) -fPIC -c -o $@ $< $(APPEND_CFLAGS)
 
+headers.chk:
+	for i in $(filter %.h,$^); do \
+	    $(CC) -x c -ansi -Wall -Werror $(CFLAGS_xeninclude) \
+	          -S -o /dev/null $$i || exit 1; \
+	    echo $$i; \
+	done >$@.new
+	mv $@.new $@
+
 subdirs-all subdirs-clean subdirs-install subdirs-distclean: .phony
 	@set -e; for subdir in $(SUBDIRS) $(SUBDIRS-y); do \
 		$(MAKE) subdir-$(patsubst subdirs-%,%,$@)-$$subdir; \
diff --git a/tools/libs/evtchn/Makefile b/tools/libs/evtchn/Makefile
index 46a807f..9917864 100644
--- a/tools/libs/evtchn/Makefile
+++ b/tools/libs/evtchn/Makefile
@@ -32,8 +32,9 @@ build:
 	$(MAKE) libs
 
 .PHONY: libs
-libs: $(LIB)
+libs: headers.chk $(LIB)
 
+headers.chk: $(wildcard include/*.h)
 
 libxenevtchn.a: $(LIB_OBJS)
 	$(AR) rc $@ $^
@@ -64,6 +65,7 @@ TAGS:
 clean:
 	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
 	rm -f libxenevtchn.so.$(MAJOR).$(MINOR) libxenevtchn.so.$(MAJOR)
+	rm -f headers.chk
 
 .PHONY: distclean
 distclean: clean
diff --git a/tools/libs/toollog/Makefile b/tools/libs/toollog/Makefile
index 9bfd179..fb701be 100644
--- a/tools/libs/toollog/Makefile
+++ b/tools/libs/toollog/Makefile
@@ -27,7 +27,9 @@ build:
 	$(MAKE) libs
 
 .PHONY: libs
-libs: $(LIB)
+libs: headers.chk $(LIB)
+
+headers.chk: $(wildcard include/*.h)
 
 libxentoollog.a: $(LIB_OBJS)
 	$(AR) rc $@ $^
@@ -58,6 +60,7 @@ TAGS:
 clean:
 	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
 	rm -f libxentoollog.so.$(MAJOR).$(MINOR) libxentoollog.so.$(MAJOR)
+	rm -f headers.chk
 
 .PHONY: distclean
 distclean: clean
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 04/29] tools/libxc: Remove osdep indirection for xc_gnt{shr, tab}
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (2 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 03/29] tools: Arrange to check public headers for ANSI compatiblity Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 05/29] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab Ian Campbell
                     ` (24 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

The alternative backend (a xen-api/xapi shim) is no longer around and
so this stuff is now just baggage which is getting in the way of
refactoring libxenctrl.

Nested virt probably suffices for this use case now.

It is now necessary to provide explicit versions of things for
platforms which do not implement this functionality, since the osdep
dispatcher cannot fulfil this need any more. These are provided by
appropriate xc_nognt???.c files which are compiled and linked on the
appropriate platforms. In them open and close return failure and
everything else aborts, since if open fails they should never be
called.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxc/Makefile               |  10 ++--
 tools/libxc/include/xenctrl.h      |   4 +-
 tools/libxc/include/xenctrlosdep.h |  23 -------
 tools/libxc/xc_gnttab.c            |  57 ++++--------------
 tools/libxc/xc_linux_osdep.c       | 119 +++++++++++++++----------------------
 tools/libxc/xc_minios.c            |  51 ++++++----------
 tools/libxc/xc_nogntshr.c          |  46 ++++++++++++++
 tools/libxc/xc_nognttab.c          |  50 ++++++++++++++++
 tools/libxc/xc_private.c           |  83 +++++++++++++++++++++-----
 tools/libxc/xc_private.h           |  24 ++++++++
 10 files changed, 274 insertions(+), 193 deletions(-)
 create mode 100644 tools/libxc/xc_nogntshr.c
 create mode 100644 tools/libxc/xc_nognttab.c

diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index b8fc6a5..184cbb7 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -43,11 +43,11 @@ CTRL_SRCS-y       += xc_resource.c
 CTRL_SRCS-$(CONFIG_X86) += xc_psr.c
 CTRL_SRCS-$(CONFIG_X86) += xc_pagetab.c
 CTRL_SRCS-$(CONFIG_Linux) += xc_linux.c xc_linux_osdep.c
-CTRL_SRCS-$(CONFIG_FreeBSD) += xc_freebsd.c xc_freebsd_osdep.c
-CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c
-CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c
-CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c
-CTRL_SRCS-$(CONFIG_MiniOS) += xc_minios.c
+CTRL_SRCS-$(CONFIG_FreeBSD) += xc_freebsd.c xc_freebsd_osdep.c xc_nognttab.c xc_nogntshr.c
+CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c xc_nognttab.c xc_nogntshr.c
+CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c xc_nognttab.c xc_nogntshr.c
+CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c xc_nognttab.c xc_nogntshr.c
+CTRL_SRCS-$(CONFIG_MiniOS) += xc_minios.c xc_nogntshr.c
 CTRL_SRCS-y       += xc_evtchn_compat.c
 
 GUEST_SRCS-y :=
diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 8f31c4d..0fc2a11 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -117,8 +117,8 @@
  */
 
 typedef struct xc_interface_core xc_interface;
-typedef struct xc_interface_core xc_gnttab;
-typedef struct xc_interface_core xc_gntshr;
+typedef struct xengntdev_handle xc_gnttab;
+typedef struct xengntdev_handle xc_gntshr;
 
 enum xc_error_code {
   XC_ERROR_NONE = 0,
diff --git a/tools/libxc/include/xenctrlosdep.h b/tools/libxc/include/xenctrlosdep.h
index 89564e1..423660d 100644
--- a/tools/libxc/include/xenctrlosdep.h
+++ b/tools/libxc/include/xenctrlosdep.h
@@ -51,8 +51,6 @@
 
 enum xc_osdep_type {
     XC_OSDEP_PRIVCMD,
-    XC_OSDEP_GNTTAB,
-    XC_OSDEP_GNTSHR,
 };
 
 /* Opaque handle internal to the backend */
@@ -88,27 +86,6 @@ struct xc_osdep_ops
                                         size_t chunksize, privcmd_mmap_entry_t entries[],
                                         int nentries);
         } privcmd;
-        struct {
-#define XC_GRANT_MAP_SINGLE_DOMAIN 0x1
-            void *(*grant_map)(xc_gnttab *xcg, xc_osdep_handle h,
-                               uint32_t count, int flags, int prot,
-                               uint32_t *domids, uint32_t *refs,
-                               uint32_t notify_offset,
-                               evtchn_port_t notify_port);
-            int (*munmap)(xc_gnttab *xcg, xc_osdep_handle h,
-                          void *start_address,
-                          uint32_t count);
-            int (*set_max_grants)(xc_gnttab *xcg, xc_osdep_handle h, uint32_t count);
-        } gnttab;
-        struct {
-            void *(*share_pages)(xc_gntshr *xcg, xc_osdep_handle h,
-                                 uint32_t domid, int count,
-                                 uint32_t *refs, int writable,
-                                 uint32_t notify_offset,
-                                 evtchn_port_t notify_port);
-            int (*munmap)(xc_gntshr *xcg, xc_osdep_handle h,
-                          void *start_address, uint32_t count);
-        } gntshr;
     } u;
 };
 typedef struct xc_osdep_ops xc_osdep_ops;
diff --git a/tools/libxc/xc_gnttab.c b/tools/libxc/xc_gnttab.c
index 60335d8..a51f405 100644
--- a/tools/libxc/xc_gnttab.c
+++ b/tools/libxc/xc_gnttab.c
@@ -143,68 +143,48 @@ grant_entry_v2_t *xc_gnttab_map_table_v2(xc_interface *xch, int domid,
     return _gnttab_map_table(xch, domid, gnt_num);
 }
 
-void *xc_gnttab_map_grant_ref(xc_gnttab *xcg,
+void *xc_gnttab_map_grant_ref(xc_gnttab *xgt,
                               uint32_t domid,
                               uint32_t ref,
                               int prot)
 {
-	return xcg->ops->u.gnttab.grant_map(xcg, xcg->ops_handle, 1, 0, prot,
-	                                    &domid, &ref, -1, -1);
+    return osdep_gnttab_grant_map(xgt, 1, 0, prot, &domid, &ref, -1, -1);
 }
 
-void *xc_gnttab_map_grant_refs(xc_gnttab *xcg,
+void *xc_gnttab_map_grant_refs(xc_gnttab *xgt,
                                uint32_t count,
                                uint32_t *domids,
                                uint32_t *refs,
                                int prot)
 {
-	return xcg->ops->u.gnttab.grant_map(xcg, xcg->ops_handle, count, 0,
-	                                    prot, domids, refs, -1, -1);
+    return osdep_gnttab_grant_map(xgt, count, 0, prot, domids, refs, -1, -1);
 }
 
-void *xc_gnttab_map_domain_grant_refs(xc_gnttab *xcg,
+void *xc_gnttab_map_domain_grant_refs(xc_gnttab *xgt,
                                       uint32_t count,
                                       uint32_t domid,
                                       uint32_t *refs,
                                       int prot)
 {
-	return xcg->ops->u.gnttab.grant_map(xcg, xcg->ops_handle, count,
-	                                    XC_GRANT_MAP_SINGLE_DOMAIN,
-	                                    prot, &domid, refs, -1, -1);
+    return osdep_gnttab_grant_map(xgt, count, XC_GRANT_MAP_SINGLE_DOMAIN,
+                                  prot, &domid, refs, -1, -1);
 }
 
-void *xc_gnttab_map_grant_ref_notify(xc_gnttab *xcg,
+void *xc_gnttab_map_grant_ref_notify(xc_gnttab *xgt,
                                      uint32_t domid,
                                      uint32_t ref,
                                      int prot,
                                      uint32_t notify_offset,
                                      evtchn_port_t notify_port)
 {
-	return xcg->ops->u.gnttab.grant_map(xcg, xcg->ops_handle, 1, 0, prot,
-	                              &domid, &ref, notify_offset, notify_port);
-}
-
-
-int xc_gnttab_munmap(xc_gnttab *xcg,
-                     void *start_address,
-                     uint32_t count)
-{
-	return xcg->ops->u.gnttab.munmap(xcg, xcg->ops_handle,
-					 start_address, count);
-}
-
-int xc_gnttab_set_max_grants(xc_gnttab *xcg, uint32_t count)
-{
-	if (!xcg->ops->u.gnttab.set_max_grants)
-		return 0;
-	return xcg->ops->u.gnttab.set_max_grants(xcg, xcg->ops_handle, count);
+    return osdep_gnttab_grant_map(xgt, 1, 0, prot,  &domid, &ref,
+                                  notify_offset, notify_port);
 }
 
 void *xc_gntshr_share_pages(xc_gntshr *xcg, uint32_t domid,
                             int count, uint32_t *refs, int writable)
 {
-	return xcg->ops->u.gntshr.share_pages(xcg, xcg->ops_handle, domid,
-	                                      count, refs, writable, -1, -1);
+    return osdep_gntshr_share_pages(xcg, domid, count, refs, writable, -1, -1);
 }
 
 void *xc_gntshr_share_page_notify(xc_gntshr *xcg, uint32_t domid,
@@ -212,22 +192,11 @@ void *xc_gntshr_share_page_notify(xc_gntshr *xcg, uint32_t domid,
                                   uint32_t notify_offset,
                                   evtchn_port_t notify_port)
 {
-	return xcg->ops->u.gntshr.share_pages(xcg, xcg->ops_handle,
-			domid, 1, ref, writable, notify_offset, notify_port);
+    return osdep_gntshr_share_pages(xcg, domid, 1, ref, writable,
+                                    notify_offset, notify_port);
 }
 
 /*
- * Unmaps the @count pages starting at @start_address, which were mapped by a
- * call to xc_gntshr_share_*. Never logs.
- */
-int xc_gntshr_munmap(xc_gntshr *xcg, void *start_address, uint32_t count)
-{
-	return xcg->ops->u.gntshr.munmap(xcg, xcg->ops_handle,
-					 start_address, count);
-}
-
-
-/*
  * Local variables:
  * mode: C
  * c-file-style: "BSD"
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 65299d0..6b329ce 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -41,6 +41,9 @@
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
+#define GTERROR(_l, _f...) xtl_log(_l, XTL_ERROR, errno, "gnttab", _f)
+#define GSERROR(_l, _f...) xtl_log(_l, XTL_ERROR, errno, "gntshr", _f)
+
 static xc_osdep_handle linux_privcmd_open(xc_interface *xch)
 {
     int flags, saved_errno;
@@ -460,26 +463,26 @@ static struct xc_osdep_ops linux_privcmd_ops = {
 
 #define DEVXEN "/dev/xen/"
 
-static xc_osdep_handle linux_gnttab_open(xc_gnttab *xcg)
+int osdep_gnttab_open(xc_gnttab *xgt)
 {
     int fd = open(DEVXEN "gntdev", O_RDWR);
-
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
-
-    return (xc_osdep_handle)fd;
+        return -1;
+    xgt->fd = fd;
+    return 0;
 }
 
-static int linux_gnttab_close(xc_gnttab *xcg, xc_osdep_handle h)
+int osdep_gnttab_close(xc_gnttab *xgt)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xgt->fd == -1 )
+        return 0;
+
+    return close(xgt->fd);
 }
 
-static int linux_gnttab_set_max_grants(xc_gnttab *xch, xc_osdep_handle h,
-                                       uint32_t count)
+int xc_gnttab_set_max_grants(xc_gnttab *xgt, uint32_t count)
 {
-    int fd = (int)h, rc;
+    int fd = xgt->fd, rc;
     struct ioctl_gntdev_set_max_grants max_grants = { .count = count };
 
     rc = ioctl(fd, IOCTL_GNTDEV_SET_MAX_GRANTS, &max_grants);
@@ -491,19 +494,19 @@ static int linux_gnttab_set_max_grants(xc_gnttab *xch, xc_osdep_handle h,
         if (errno == ENOTTY)
             rc = 0;
         else
-            PERROR("linux_gnttab_set_max_grants: ioctl SET_MAX_GRANTS failed");
+            GTERROR(xgt->logger, "ioctl SET_MAX_GRANTS failed");
     }
 
     return rc;
 }
 
-static void *linux_gnttab_grant_map(xc_gnttab *xch, xc_osdep_handle h,
-                                    uint32_t count, int flags, int prot,
-                                    uint32_t *domids, uint32_t *refs,
-                                    uint32_t notify_offset,
-                                    evtchn_port_t notify_port)
+void *osdep_gnttab_grant_map(xc_gnttab *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port)
 {
-    int fd = (int)h;
+    int fd = xgt->fd;
     struct ioctl_gntdev_map_grant_ref *map;
     unsigned int map_size = ROUNDUP((sizeof(*map) + (count - 1) *
                                     sizeof(struct ioctl_gntdev_map_grant_ref)),
@@ -524,7 +527,7 @@ static void *linux_gnttab_grant_map(xc_gnttab *xch, xc_osdep_handle h,
                    MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
         if ( map == MAP_FAILED )
         {
-            PERROR("linux_gnttab_grant_map: mmap of map failed");
+            GTERROR(xgt->logger, "mmap of map failed");
             return NULL;
         }
     }
@@ -538,7 +541,7 @@ static void *linux_gnttab_grant_map(xc_gnttab *xch, xc_osdep_handle h,
     map->count = count;
 
     if ( ioctl(fd, IOCTL_GNTDEV_MAP_GRANT_REF, map) ) {
-        PERROR("linux_gnttab_grant_map: ioctl MAP_GRANT_REF failed");
+        GTERROR(xgt->logger, "ioctl MAP_GRANT_REF failed");
         goto out;
     }
 
@@ -577,7 +580,7 @@ static void *linux_gnttab_grant_map(xc_gnttab *xch, xc_osdep_handle h,
         if (notify.action)
             rv = ioctl(fd, IOCTL_GNTDEV_SET_UNMAP_NOTIFY, &notify);
         if (rv) {
-            PERROR("linux_gnttab_grant_map: ioctl SET_UNMAP_NOTIFY failed");
+            GTERROR(xgt->logger, "ioctl SET_UNMAP_NOTIFY failed");
             munmap(addr, count * XC_PAGE_SIZE);
             addr = MAP_FAILED;
         }
@@ -589,7 +592,7 @@ static void *linux_gnttab_grant_map(xc_gnttab *xch, xc_osdep_handle h,
         struct ioctl_gntdev_unmap_grant_ref unmap_grant;
 
         /* Unmap the driver slots used to store the grant information. */
-        PERROR("xc_gnttab_map_grant_refs: mmap failed");
+        GTERROR(xgt->logger, "mmap failed");
         unmap_grant.index = map->index;
         unmap_grant.count = count;
         ioctl(fd, IOCTL_GNTDEV_UNMAP_GRANT_REF, &unmap_grant);
@@ -604,12 +607,9 @@ static void *linux_gnttab_grant_map(xc_gnttab *xch, xc_osdep_handle h,
     return addr;
 }
 
-
-
-static int linux_gnttab_munmap(xc_gnttab *xcg, xc_osdep_handle h,
-                               void *start_address, uint32_t count)
+int xc_gnttab_munmap(xc_gnttab *xgt, void *start_address, uint32_t count)
 {
-    int fd = (int)h;
+    int fd = xgt->fd;
     struct ioctl_gntdev_get_offset_for_vaddr get_offset;
     struct ioctl_gntdev_unmap_grant_ref unmap_grant;
     int rc;
@@ -647,43 +647,33 @@ static int linux_gnttab_munmap(xc_gnttab *xcg, xc_osdep_handle h,
     return 0;
 }
 
-static struct xc_osdep_ops linux_gnttab_ops = {
-    .open = &linux_gnttab_open,
-    .close = &linux_gnttab_close,
-
-    .u.gnttab = {
-        .set_max_grants = linux_gnttab_set_max_grants,
-        .grant_map = &linux_gnttab_grant_map,
-        .munmap = &linux_gnttab_munmap,
-    },
-};
-
-static xc_osdep_handle linux_gntshr_open(xc_gntshr *xcg)
+int osdep_gntshr_open(xc_gntshr *xgs)
 {
     int fd = open(DEVXEN "gntalloc", O_RDWR);
-
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
-
-    return (xc_osdep_handle)fd;
+        return -1;
+    xgs->fd = fd;
+    return 0;
 }
 
-static int linux_gntshr_close(xc_gntshr *xcg, xc_osdep_handle h)
+int osdep_gntshr_close(xc_gntshr *xgs)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xgs->fd == -1 )
+        return 0;
+
+    return close(xgs->fd);
 }
 
-static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
-                                      uint32_t domid, int count,
-                                      uint32_t *refs, int writable,
-                                      uint32_t notify_offset,
-                                      evtchn_port_t notify_port)
+void *osdep_gntshr_share_pages(xc_gntshr *xgs,
+                               uint32_t domid, int count,
+                               uint32_t *refs, int writable,
+                               uint32_t notify_offset,
+                               evtchn_port_t notify_port)
 {
     struct ioctl_gntalloc_alloc_gref *gref_info = NULL;
     struct ioctl_gntalloc_unmap_notify notify;
     struct ioctl_gntalloc_dealloc_gref gref_drop;
-    int fd = (int)h;
+    int fd = xgs->fd;
     int err;
     void *area = NULL;
     gref_info = malloc(sizeof(*gref_info) + count * sizeof(uint32_t));
@@ -695,7 +685,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 
     err = ioctl(fd, IOCTL_GNTALLOC_ALLOC_GREF, gref_info);
     if (err) {
-        PERROR("linux_gntshr_share_pages: ioctl failed");
+        GSERROR(xgs->logger, "ioctl failed");
         goto out;
     }
 
@@ -704,7 +694,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
 
     if (area == MAP_FAILED) {
         area = NULL;
-        PERROR("linux_gntshr_share_pages: mmap failed");
+        GSERROR(xgs->logger, "mmap failed");
         goto out_remove_fdmap;
     }
 
@@ -721,7 +711,7 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
     if (notify.action)
         err = ioctl(fd, IOCTL_GNTALLOC_SET_UNMAP_NOTIFY, &notify);
     if (err) {
-        PERROR("linux_gntshr_share_page_notify: ioctl SET_UNMAP_NOTIFY failed");
+        GSERROR(xgs->logger, "ioctl SET_UNMAP_NOTIFY failed");
 		munmap(area, count * XC_PAGE_SIZE);
 		area = NULL;
 	}
@@ -740,33 +730,18 @@ static void *linux_gntshr_share_pages(xc_gntshr *xch, xc_osdep_handle h,
     return area;
 }
 
-static int linux_gntshr_munmap(xc_gntshr *xcg, xc_osdep_handle h,
-                               void *start_address, uint32_t count)
+int xc_gntshr_munmap(xc_gntshr *xgs,
+                     void *start_address, uint32_t count)
 {
     return munmap(start_address, count * XC_PAGE_SIZE);
 }
 
-static struct xc_osdep_ops linux_gntshr_ops = {
-    .open = &linux_gntshr_open,
-    .close = &linux_gntshr_close,
-
-    .u.gntshr = {
-        .share_pages = &linux_gntshr_share_pages,
-        .munmap = &linux_gntshr_munmap,
-    },
-};
-
-
 static struct xc_osdep_ops *linux_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
     {
     case XC_OSDEP_PRIVCMD:
         return &linux_privcmd_ops;
-    case XC_OSDEP_GNTTAB:
-        return &linux_gnttab_ops;
-    case XC_OSDEP_GNTSHR:
-        return &linux_gntshr_ops;
     default:
         return NULL;
     }
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index db7b344..fd7def6 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -202,19 +202,22 @@ void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
     return memalign(alignment, size);
 }
 
-static xc_osdep_handle minios_gnttab_open(xc_gnttab *xcg)
+int osdep_gnttab_open(xc_gnttab *xgt)
 {
     int fd = alloc_fd(FTYPE_GNTMAP);
     if ( fd == -1 )
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
     gntmap_init(&files[fd].gntmap);
-    return (xc_osdep_handle)fd;
+    xgt->fd = fd;
+    return 0;
 }
 
-static int minios_gnttab_close(xc_gnttab *xcg, xc_osdep_handle h)
+int osdep_gnttab_close(xc_gnttab *xgt)
 {
-    int fd = (int)h;
-    return close(fd);
+    if ( xgt->fd == -1 )
+        return 0;
+
+    return close(xgt->fd);
 }
 
 void minios_gnttab_close_fd(int fd)
@@ -223,13 +226,13 @@ void minios_gnttab_close_fd(int fd)
     files[fd].type = FTYPE_NONE;
 }
 
-static void *minios_gnttab_grant_map(xc_gnttab *xcg, xc_osdep_handle h,
-                                     uint32_t count, int flags, int prot,
-                                     uint32_t *domids, uint32_t *refs,
-                                     uint32_t notify_offset,
-                                     evtchn_port_t notify_port)
+void *osdep_gnttab_grant_map(xc_gnttab *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port)
 {
-    int fd = (int)h;
+    int fd = xgt->fd;
     int stride = 1;
     if (flags & XC_GRANT_MAP_SINGLE_DOMAIN)
         stride = 0;
@@ -242,11 +245,9 @@ static void *minios_gnttab_grant_map(xc_gnttab *xcg, xc_osdep_handle h,
                                  refs, prot & PROT_WRITE);
 }
 
-static int minios_gnttab_munmap(xc_gnttab *xcg, xc_osdep_handle h,
-                                void *start_address,
-                                uint32_t count)
+int xc_gnttab_munmap(xc_gnttab *xgt, void *start_address, uint32_t count)
 {
-    int fd = (int)h;
+    int fd = xgt->fd;
     int ret;
     ret = gntmap_munmap(&files[fd].gntmap,
                         (unsigned long) start_address,
@@ -258,10 +259,9 @@ static int minios_gnttab_munmap(xc_gnttab *xcg, xc_osdep_handle h,
     return ret;
 }
 
-static int minios_gnttab_set_max_grants(xc_gnttab *xcg, xc_osdep_handle h,
-                             uint32_t count)
+int xc_gnttab_set_max_grants(xc_gnttab *xgt, uint32_t count)
 {
-    int fd = (int)h;
+    int fd = xgt->fd;
     int ret;
     ret = gntmap_set_max_grants(&files[fd].gntmap,
                                 count);
@@ -272,25 +272,12 @@ static int minios_gnttab_set_max_grants(xc_gnttab *xcg, xc_osdep_handle h,
     return ret;
 }
 
-static struct xc_osdep_ops minios_gnttab_ops = {
-    .open = &minios_gnttab_open,
-    .close = &minios_gnttab_close,
-
-    .u.gnttab = {
-        .grant_map = &minios_gnttab_grant_map,
-        .munmap = &minios_gnttab_munmap,
-        .set_max_grants = &minios_gnttab_set_max_grants,
-    },
-};
-
 static struct xc_osdep_ops *minios_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
     {
     case XC_OSDEP_PRIVCMD:
         return &minios_privcmd_ops;
-    case XC_OSDEP_GNTTAB:
-        return &minios_gnttab_ops;
     default:
         return NULL;
     }
diff --git a/tools/libxc/xc_nogntshr.c b/tools/libxc/xc_nogntshr.c
new file mode 100644
index 0000000..9aa6064
--- /dev/null
+++ b/tools/libxc/xc_nogntshr.c
@@ -0,0 +1,46 @@
+/******************************************************************************
+ *
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ *
+ * 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 <stdlib.h>
+
+#include "xc_private.h"
+
+int osdep_gntshr_open(xc_gnttab *xgt)
+{
+    return -1;
+}
+
+int osdep_gntshr_close(xc_gnttab *xgt)
+{
+    return 0;
+}
+
+void *osdep_gntshr_share_pages(xc_gntshr *xgs,
+                               uint32_t domid, int count,
+                               uint32_t *refs, int writable,
+                               uint32_t notify_offset,
+                               evtchn_port_t notify_port)
+{
+    abort()
+}
+
+int xc_gntshr_munmap(xc_gntshr *xgs,
+                     void *start_address, uint32_t count)
+{
+    abort();
+}
diff --git a/tools/libxc/xc_nognttab.c b/tools/libxc/xc_nognttab.c
new file mode 100644
index 0000000..e8a0fcb
--- /dev/null
+++ b/tools/libxc/xc_nognttab.c
@@ -0,0 +1,50 @@
+/******************************************************************************
+ *
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ *
+ * 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 <stdlib.h>
+
+#include "xc_private.h"
+
+int osdep_gnttab_open(xc_gnttab *xgt)
+{
+    return -1;
+}
+
+int osdep_gnttab_close(xc_gnttab *xgt)
+{
+    return 0;
+}
+
+int xc_gnttab_set_max_grants(xc_gnttab *xgt, uint32_t count)
+{
+    abort();
+}
+
+void *osdep_gnttab_grant_map(xc_gnttab *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port)
+{
+    abort();
+}
+
+int xc_gnttab_munmap(xc_gnttab *xgt, void *start_address, uint32_t count)
+{
+    abort();
+}
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index 56d54b6..7f52a5d 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -119,8 +119,6 @@ static const char *xc_osdep_type_name(enum xc_osdep_type type)
     switch ( type )
     {
     case XC_OSDEP_PRIVCMD: return "privcmd";
-    case XC_OSDEP_GNTTAB:  return "gnttab";
-    case XC_OSDEP_GNTSHR:  return "gntshr";
     }
     return "unknown";
 }
@@ -251,30 +249,85 @@ int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
     return xch->ops->u.privcmd.hypercall(xch, xch->ops_handle, hypercall);
 }
 
-xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
-                             unsigned open_flags)
+xc_gnttab *xc_gnttab_open(xentoollog_logger *logger, unsigned open_flags)
 {
-    return xc_interface_open_common(logger, NULL, open_flags,
-                                    XC_OSDEP_GNTTAB);
+    xc_gnttab *xgt = malloc(sizeof(*xgt));
+    int rc;
+
+    if (!xgt) return NULL;
+
+    xgt->fd = -1;
+    xgt->logger = logger;
+    xgt->logger_tofree  = NULL;
+
+    if (!xgt->logger) {
+        xgt->logger = xgt->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xgt->logger) goto err;
+    }
+
+    rc = osdep_gnttab_open(xgt);
+    if ( rc  < 0 ) goto err;
+
+    return xgt;
+
+err:
+    osdep_gnttab_close(xgt);
+    xtl_logger_destroy(xgt->logger_tofree);
+    free(xgt);
+    return NULL;
 }
 
-int xc_gnttab_close(xc_gnttab *xcg)
+int xc_gnttab_close(xc_gnttab *xgt)
 {
-    return xc_interface_close_common(xcg);
+    int rc;
+
+    rc = osdep_gnttab_close(xgt);
+    xtl_logger_destroy(xgt->logger_tofree);
+    free(xgt);
+    return rc;
 }
 
-xc_gntshr *xc_gntshr_open(xentoollog_logger *logger,
-                             unsigned open_flags)
+xc_gntshr *xc_gntshr_open(xentoollog_logger *logger, unsigned open_flags)
 {
-    return xc_interface_open_common(logger, NULL, open_flags,
-                                    XC_OSDEP_GNTSHR);
+    xc_gntshr *xgs = malloc(sizeof(*xgs));
+    int rc;
+
+    if (!xgs) return NULL;
+
+    xgs->fd = -1;
+    xgs->logger = logger;
+    xgs->logger_tofree  = NULL;
+
+    if (!xgs->logger) {
+        xgs->logger = xgs->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xgs->logger) goto err;
+    }
+
+    rc = osdep_gntshr_open(xgs);
+    if ( rc  < 0 ) goto err;
+
+    return xgs;
+
+err:
+    osdep_gntshr_close(xgs);
+    xtl_logger_destroy(xgs->logger_tofree);
+    free(xgs);
+    return NULL;
 }
 
-int xc_gntshr_close(xc_gntshr *xcg)
+int xc_gntshr_close(xc_gntshr *xgs)
 {
-    return xc_interface_close_common(xcg);
-}
+    int rc;
 
+    rc = osdep_gntshr_close(xgs);
+    xtl_logger_destroy(xgs->logger_tofree);
+    free(xgs);
+    return rc;
+}
 
 static pthread_key_t errbuf_pkey;
 static pthread_once_t errbuf_pkey_once = PTHREAD_ONCE_INIT;
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index a32accb..35c99e0 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -123,6 +123,30 @@ struct xc_interface_core {
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
 };
 
+struct xengntdev_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    int fd;
+};
+
+int osdep_gnttab_open(xc_gnttab *xgt);
+int osdep_gnttab_close(xc_gnttab *xgt);
+
+#define XC_GRANT_MAP_SINGLE_DOMAIN 0x1
+void *osdep_gnttab_grant_map(xc_gnttab *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port);
+
+int osdep_gntshr_open(xc_gntshr *xgs);
+int osdep_gntshr_close(xc_gntshr *xgs);
+
+void *osdep_gntshr_share_pages(xc_gntshr *xgs,
+                               uint32_t domid, int count,
+                               uint32_t *refs, int writable,
+                               uint32_t notify_offset,
+                               evtchn_port_t notify_port);
+
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 05/29] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (3 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 04/29] tools/libxc: Remove osdep indirection for xc_gnt{shr, tab} Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 06/29] tools/libxc: Remove osdep indirection for privcmd Ian Campbell
                     ` (23 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

libxengnttab will provide a stable API and ABI for accessing the
grant table devices.

The functions are moved into the xengnt{tab,shr} namespace to make a
clean break from libxc and avoid ambiguity regarding which interfaces
are stable.

All in-tree users are updated to use the new names.

Upon request (via #define XC_WANT_COMPAT_GNTTAB_API) libxenctrl will
provide a compat API for the old names. This is used by qemu-xen for
the time being. qemu-xen-traditional is updated in lockstep.

This leaves a few grant table related functions which go via privcmd
(GNTTABOP) rather than ioctls on the /dev/xen/gnt* devices in
libxenctrl. Specifically:

  - xc_gnttab_get_version
  - xc_gnttab_map_table_v1
  - xc_gnttab_map_table_v2
  - xc_gnttab_op

These functions do not appear to be needed by qemu-dm, qemu-pv
(provision of device model to HVM guests and PV backends respectively)
or by libvchan suggesting they are not needed by non-toolstack uses of
event channels.

The new library uses a version script to ensure that only expected
symbols are exported and to version them such that ABI guarantees can
be kept in the future.

After this change libxenvchan no longer needs to link against
libxenctrl. It still needs xenctrl.h in one file for xen_mb and
friends.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---

Must be applied with:

 - "qemu-xen-traditional: Use libxengnttab" and a corresponding
   QEMU_TAG update folded here.
 - "mini-os: Include libxengnttab with libxc"" and a corresponding
   bump to MINIOS_UPSTREAM_REVISION folded in here.

v3:
 - Remove SHLIB_libxenctrl from SHDEPS_libxenvchan, and replace with
   SHLIB_libxentoollog.
 - Move to tools/libs/gnttab
 - Adjust for rebase over 31cf2ca75181 "tools/libxc: linux: Don't use
   getpagesize() when unmapping the grants"

v5: Allow _close(NULL).

v6: The extensive API document updates from the previous round of
    review have been implemented in "tools/libs/gnttab: Extensive
    updates to API documentation." later in the series, so as not to
    add further functional changes than already required to this
    refactoring patch.

v7: Added mk-headers-$(XEN_TARGET_ARCH) build dependency instead of
    open coding the recursion.
    s/gnttab_munmap/gnttab_unmap/ and s/gntshr_munmap/gntshr_unshare/
    in the API and equivalents in the internals/osdeps etc.

v8: s/EVTCHNOP/GNTTABOP/ cut-&-paste error in commit log.
    Remove *.so on clean, add distclean target.
---
 .gitignore                                         |   2 +
 stubdom/Makefile                                   |  17 +-
 tools/Makefile                                     |   3 +
 tools/Rules.mk                                     |  14 +-
 tools/console/Makefile                             |   5 +-
 tools/console/daemon/io.c                          |  21 +-
 tools/libs/Makefile                                |   1 +
 tools/libs/evtchn/minios.c                         |   5 +-
 tools/libs/gnttab/Makefile                         |  73 +++++
 tools/libs/gnttab/gntshr_core.c                    |  95 ++++++
 .../xc_nognttab.c => libs/gnttab/gntshr_unimp.c}   |  34 ++-
 tools/libs/gnttab/gnttab_core.c                    | 124 ++++++++
 tools/libs/gnttab/gnttab_unimp.c                   |  89 ++++++
 tools/libs/gnttab/include/xengnttab.h              | 216 ++++++++++++++
 tools/libs/gnttab/libxengnttab.map                 |  23 ++
 tools/libs/gnttab/linux.c                          | 329 +++++++++++++++++++++
 tools/libs/gnttab/minios.c                         | 117 ++++++++
 tools/libs/gnttab/private.h                        |  47 +++
 tools/libvchan/Makefile                            |   8 +-
 tools/libvchan/init.c                              |  26 +-
 tools/libvchan/io.c                                |   8 +-
 tools/libvchan/libxenvchan.h                       |   6 +-
 tools/libxc/Makefile                               |  15 +-
 tools/libxc/include/xenctrl.h                      | 168 -----------
 tools/libxc/include/xenctrl_compat.h               |  48 +++
 tools/libxc/xc_gnttab.c                            |  53 ----
 tools/libxc/xc_gnttab_compat.c                     | 111 +++++++
 tools/libxc/xc_linux_osdep.c                       | 280 ------------------
 tools/libxc/xc_minios.c                            |  73 -----
 tools/libxc/xc_nogntshr.c                          |  46 ---
 tools/libxc/xc_private.c                           |  80 -----
 tools/libxc/xc_private.h                           |  24 --
 tools/xenstore/Makefile                            |   4 +-
 tools/xenstore/xenstored_core.h                    |   4 +-
 tools/xenstore/xenstored_domain.c                  |  24 +-
 tools/xenstore/xenstored_minios.c                  |   5 +-
 36 files changed, 1397 insertions(+), 801 deletions(-)
 create mode 100644 tools/libs/gnttab/Makefile
 create mode 100644 tools/libs/gnttab/gntshr_core.c
 rename tools/{libxc/xc_nognttab.c => libs/gnttab/gntshr_unimp.c} (52%)
 create mode 100644 tools/libs/gnttab/gnttab_core.c
 create mode 100644 tools/libs/gnttab/gnttab_unimp.c
 create mode 100644 tools/libs/gnttab/include/xengnttab.h
 create mode 100644 tools/libs/gnttab/libxengnttab.map
 create mode 100644 tools/libs/gnttab/linux.c
 create mode 100644 tools/libs/gnttab/minios.c
 create mode 100644 tools/libs/gnttab/private.h
 create mode 100644 tools/libxc/xc_gnttab_compat.c
 delete mode 100644 tools/libxc/xc_nogntshr.c

diff --git a/.gitignore b/.gitignore
index aa92d78..a117161 100644
--- a/.gitignore
+++ b/.gitignore
@@ -63,6 +63,7 @@ stubdom/ioemu/
 stubdom/libs-*
 stubdom/libxc-*
 stubdom/libxenevtchn-*
+stubdom/libxengnttab-*
 stubdom/libxentoollog-*
 stubdom/lwip-*
 stubdom/lwip/
@@ -89,6 +90,7 @@ config/Stubdom.mk
 config/Docs.mk
 tools/libs/toollog/headers.chk
 tools/libs/evtchn/headers.chk
+tools/libs/gnttab/headers.chk
 tools/blktap2/daemon/blktapctrl
 tools/blktap2/drivers/img2qcow
 tools/blktap2/drivers/lock-util
diff --git a/stubdom/Makefile b/stubdom/Makefile
index 702d66b..2dbf4a8 100644
--- a/stubdom/Makefile
+++ b/stubdom/Makefile
@@ -325,6 +325,12 @@ mk-headers-$(XEN_TARGET_ARCH): $(IOEMU_LINKFARM_TARGET)
 	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/include/*.h include/ && \
 	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/*.c . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/Makefile . )
+	mkdir -p libs-$(XEN_TARGET_ARCH)/gnttab/include
+	[ -h libs-$(XEN_TARGET_ARCH)/gnttab/Makefile ] || ( cd libs-$(XEN_TARGET_ARCH)/gnttab && \
+	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/*.h . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/include/*.h include/ && \
+	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/*.c . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/Makefile . )
 	mkdir -p libxc-$(XEN_TARGET_ARCH)
 	[ -h libxc-$(XEN_TARGET_ARCH)/Makefile ] || ( cd libxc-$(XEN_TARGET_ARCH) && \
 	  ln -sf $(XEN_ROOT)/tools/libxc/*.h . && \
@@ -366,12 +372,21 @@ libs-$(XEN_TARGET_ARCH)/evtchn/libxenevtchn.a: mk-headers-$(XEN_TARGET_ARCH) $(N
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/evtchn
 
 #######
+# libxengnttab
+#######
+
+.PHONY: libxengnttab
+libxengnttab: libs-$(XEN_TARGET_ARCH)/gnttab/libxengnttab.a
+libs-$(XEN_TARGET_ARCH)/gnttab/libxengnttab.a: mk-headers-$(XEN_TARGET_ARCH) $(NEWLIB_STAMPFILE)
+	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/gnttab
+
+#######
 # libxc
 #######
 
 .PHONY: libxc
 libxc: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a libxc-$(XEN_TARGET_ARCH)/libxenguest.a
-libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn cross-zlib
+libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn libxengnttab cross-zlib
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= CONFIG_LIBXC_MINIOS=y -C libxc-$(XEN_TARGET_ARCH)
 
  libxc-$(XEN_TARGET_ARCH)/libxenguest.a: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a
diff --git a/tools/Makefile b/tools/Makefile
index 99e016a..f373e71 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -249,9 +249,11 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		--includedir=$(LIBEXEC_INC) \
 		--source-path=$$source \
 		--extra-cflags="-DXC_WANT_COMPAT_EVTCHN_API=1 \
+		-DXC_WANT_COMPAT_GNTTAB_API=1 \
 		-I$(XEN_ROOT)/tools/include \
 		-I$(XEN_ROOT)/tools/libs/toollog/include \
 		-I$(XEN_ROOT)/tools/libs/evtchn/include \
+		-I$(XEN_ROOT)/tools/libs/gnttab/include \
 		-I$(XEN_ROOT)/tools/libxc/include \
 		-I$(XEN_ROOT)/tools/xenstore/include \
 		-I$(XEN_ROOT)/tools/xenstore/compat/include \
@@ -260,6 +262,7 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		-L$(XEN_ROOT)/tools/xenstore \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/evtchn \
+		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/gnttab \
 		$(QEMU_UPSTREAM_RPATH)" \
 		--bindir=$(LIBEXEC_BIN) \
 		--datadir=$(SHAREDIR)/qemu-xen \
diff --git a/tools/Rules.mk b/tools/Rules.mk
index 0c83e22..379990f 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -12,6 +12,7 @@ INSTALL = $(XEN_ROOT)/tools/cross-install
 XEN_INCLUDE        = $(XEN_ROOT)/tools/include
 XEN_LIBXENTOOLLOG  = $(XEN_ROOT)/tools/libs/toollog
 XEN_LIBXENEVTCHN   = $(XEN_ROOT)/tools/libs/evtchn
+XEN_LIBXENGNTTAB   = $(XEN_ROOT)/tools/libs/gnttab
 XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
 XEN_XENLIGHT       = $(XEN_ROOT)/tools/libxl
 XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
@@ -88,8 +89,17 @@ SHDEPS_libxenevtchn =
 LDLIBS_libxenevtchn = $(XEN_LIBXENEVTCHN)/libxenevtchn$(libextension)
 SHLIB_libxenevtchn  = -Wl,-rpath-link=$(XEN_LIBXENEVTCHN)
 
+CFLAGS_libxengnttab = -I$(XEN_LIBXENGNTTAB)/include $(CFLAGS_xeninclude)
+LDLIBS_libxengnttab = $(XEN_LIBXENGNTTAB)/libxengnttab$(libextension)
+SHLIB_libxengnttab  = -Wl,-rpath-link=$(XEN_LIBXENGNTTAB)
+
+# xengntshr_* interfaces are actually part of libxengnttab.so
+CFLAGS_libxengntshr = -I$(XEN_LIBXENGNTTAB)/include $(CFLAGS_xeninclude)
+LDLIBS_libxengntshr = $(XEN_LIBXENGNTTAB)/libxengnttab$(libextension)
+SHLIB_libxengntshr  = -Wl,-rpath-link=$(XEN_LIBXENGNTTAB)
+
 CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_xeninclude)
-SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn)
+SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn) $(SHLIB_libxengnttab) $(SHLIB_libxengntshr)
 LDLIBS_libxenctrl = $(SHDEPS_libxenctrl) $(XEN_LIBXC)/libxenctrl$(libextension)
 SHLIB_libxenctrl  = $(SHDEPS_libxenctrl) -Wl,-rpath-link=$(XEN_LIBXC)
 
@@ -109,7 +119,7 @@ LDLIBS_libxenstat  = $(SHDEPS_libxenstat) $(XEN_LIBXENSTAT)/libxenstat$(libexten
 SHLIB_libxenstat   = $(SHDEPS_libxenstat) -Wl,-rpath-link=$(XEN_LIBXENSTAT)
 
 CFLAGS_libxenvchan = -I$(XEN_LIBVCHAN)
-SHDEPS_libxenvchan = $(SHLIB_libxenctrl) $(SHLIB_libxenstore) $(SHLIB_libxenevtchn)
+SHDEPS_libxenvchan = $(SHLIB_libxentoollog) $(SHLIB_libxenstore) $(SHLIB_libxenevtchn) $(SHLIB_libxengnttab) $(SHLIB_libxengntshr)
 LDLIBS_libxenvchan = $(SHDEPS_libxenvchan) $(XEN_LIBVCHAN)/libxenvchan$(libextension)
 SHLIB_libxenvchan  = $(SHDEPS_libxenvchan) -Wl,-rpath-link=$(XEN_LIBVCHAN)
 
diff --git a/tools/console/Makefile b/tools/console/Makefile
index 4b3a492..6eeac8f 100644
--- a/tools/console/Makefile
+++ b/tools/console/Makefile
@@ -3,10 +3,8 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS  += -Werror
 
-CFLAGS  += $(CFLAGS_libxenevtchn)
 CFLAGS  += $(CFLAGS_libxenctrl)
 CFLAGS  += $(CFLAGS_libxenstore)
-LDLIBS += $(LDLIBS_libxenevtchn)
 LDLIBS += $(LDLIBS_libxenctrl)
 LDLIBS += $(LDLIBS_libxenstore)
 LDLIBS += $(SOCKET_LIBS)
@@ -28,8 +26,9 @@ clean:
 .PHONY: distclean
 distclean: clean
 
+daemon/io.o: CFLAGS += $(CFLAGS_libxenevtchn) $(CFLAGS_libxengnttab)
 xenconsoled: $(patsubst %.c,%.o,$(wildcard daemon/*.c))
-	$(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsoled) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_xenconsoled) $(APPEND_LDFLAGS)
 
 xenconsole: client/_paths.h $(patsubst %.c,%.o,$(wildcard client/*.c))
 	$(CC) $(LDFLAGS) $^ -o $@ $(LDLIBS) $(LDLIBS_xenconsole) $(APPEND_LDFLAGS)
diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index 2f2e9c5..e2e7a6b 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -22,6 +22,7 @@
 #include "utils.h"
 #include "io.h"
 #include <xenevtchn.h>
+#include <xengnttab.h>
 #include <xenstore.h>
 #include <xen/io/console.h>
 #include <xen/grant_table.h>
@@ -72,7 +73,7 @@ static int log_time_hv_needts = 1;
 static int log_time_guest_needts = 1;
 static int log_hv_fd = -1;
 
-static xc_gnttab *xcg_handle = NULL;
+static xengnttab_handle *xgt_handle = NULL;
 
 static struct pollfd  *fds;
 static unsigned int current_array_size;
@@ -520,8 +521,8 @@ static void domain_unmap_interface(struct domain *dom)
 {
 	if (dom->interface == NULL)
 		return;
-	if (xcg_handle && dom->ring_ref == -1)
-		xc_gnttab_munmap(xcg_handle, dom->interface, 1);
+	if (xgt_handle && dom->ring_ref == -1)
+		xengnttab_unmap(xgt_handle, dom->interface, 1);
 	else
 		munmap(dom->interface, XC_PAGE_SIZE);
 	dom->interface = NULL;
@@ -552,9 +553,9 @@ static int domain_create_ring(struct domain *dom)
 	if (ring_ref != dom->ring_ref && dom->ring_ref != -1)
 		domain_unmap_interface(dom);
 
-	if (!dom->interface && xcg_handle) {
+	if (!dom->interface && xgt_handle) {
 		/* Prefer using grant table */
-		dom->interface = xc_gnttab_map_grant_ref(xcg_handle,
+		dom->interface = xengnttab_map_grant_ref(xgt_handle,
 			dom->domid, GNTTAB_RESERVED_CONSOLE,
 			PROT_READ|PROT_WRITE);
 		dom->ring_ref = -1;
@@ -1029,8 +1030,8 @@ void handle_io(void)
 		handle_hv_logs(xce_handle, true);
 	}
 
-	xcg_handle = xc_gnttab_open(NULL, 0);
-	if (xcg_handle == NULL) {
+	xgt_handle = xengnttab_open(NULL, 0);
+	if (xgt_handle == NULL) {
 		dolog(LOG_DEBUG, "Failed to open xcg handle: %d (%s)",
 		      errno, strerror(errno));
 	}
@@ -1206,9 +1207,9 @@ void handle_io(void)
 		xenevtchn_close(xce_handle);
 		xce_handle = NULL;
 	}
-	if (xcg_handle != NULL) {
-		xc_gnttab_close(xcg_handle);
-		xcg_handle = NULL;
+	if (xgt_handle != NULL) {
+		xengnttab_close(xgt_handle);
+		xgt_handle = NULL;
 	}
 	log_hv_evtchn = -1;
 }
diff --git a/tools/libs/Makefile b/tools/libs/Makefile
index 0e3f523..00156ae 100644
--- a/tools/libs/Makefile
+++ b/tools/libs/Makefile
@@ -4,5 +4,6 @@ include $(XEN_ROOT)/tools/Rules.mk
 SUBDIRS-y :=
 SUBDIRS-y += toollog
 SUBDIRS-y += evtchn
+SUBDIRS-y += gnttab
 
 all clean install distclean: %: subdirs-%
diff --git a/tools/libs/evtchn/minios.c b/tools/libs/evtchn/minios.c
index fb913a2..b839cd0 100644
--- a/tools/libs/evtchn/minios.c
+++ b/tools/libs/evtchn/minios.c
@@ -27,13 +27,12 @@
 #include <mini-os/events.h>
 #include <mini-os/wait.h>
 
-#include <sys/socket.h>
-
 #include <assert.h>
 #include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdint.h>
+#include <unistd.h>
 #include <inttypes.h>
 #include <malloc.h>
 
@@ -43,8 +42,6 @@ extern void minios_evtchn_close_fd(int fd);
 
 extern struct wait_queue_head event_queue;
 
-//void minios_evtchn_close_fd(int fd);
-
 /* XXX Note: This is not threadsafe */
 static struct evtchn_port_info* port_alloc(int fd) {
     struct evtchn_port_info *port_info;
diff --git a/tools/libs/gnttab/Makefile b/tools/libs/gnttab/Makefile
new file mode 100644
index 0000000..af64542
--- /dev/null
+++ b/tools/libs/gnttab/Makefile
@@ -0,0 +1,73 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+SHLIB_LDFLAGS += -Wl,--version-script=libxengnttab.map
+
+CFLAGS   += -Werror -Wmissing-prototypes
+CFLAGS   += -I./include $(CFLAGS_xeninclude)
+CFLAGS   += $(CFLAGS_libxentoollog)
+
+SRCS-GNTTAB            += gnttab_core.c
+SRCS-GNTSHR            += gntshr_core.c
+
+SRCS-$(CONFIG_Linux)   += $(SRCS-GNTTAB) $(SRCS-GNTSHR) linux.c
+SRCS-$(CONFIG_MiniOS)  += $(SRCS-GNTTAB) gntshr_unimp.c minios.c
+SRCS-$(CONFIG_FreeBSD) += gnttab_unimp.c gntshr_unimp.c
+SRCS-$(CONFIG_SunOS)   += gnttab_unimp.c gntshr_unimp.c
+SRCS-$(CONFIG_NetBSD)  += gnttab_unimp.c gntshr_unimp.c
+
+LIB_OBJS := $(patsubst %.c,%.o,$(SRCS-y))
+PIC_OBJS := $(patsubst %.c,%.opic,$(SRCS-y))
+
+LIB := libxengnttab.a
+ifneq ($(nosharedlibs),y)
+LIB += libxengnttab.so
+endif
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build:
+	$(MAKE) libs
+
+.PHONY: libs
+libs: headers.chk $(LIB)
+
+headers.chk: $(wildcard include/*.h)
+
+libxengnttab.a: $(LIB_OBJS)
+	$(AR) rc $@ $^
+
+libxengnttab.so: libxengnttab.so.$(MAJOR)
+	$(SYMLINK_SHLIB) $< $@
+libxengnttab.so.$(MAJOR): libxengnttab.so.$(MAJOR).$(MINOR)
+	$(SYMLINK_SHLIB) $< $@
+
+libxengnttab.so.$(MAJOR).$(MINOR): $(PIC_OBJS) libxengnttab.map
+	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxengnttab.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(PIC_OBJS) $(LDLIBS_libxentoollog) $(APPEND_LDFLAGS)
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(libdir)
+	$(INSTALL_DIR) $(DESTDIR)$(includedir)
+	$(INSTALL_SHLIB) libxengnttab.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
+	$(INSTALL_DATA) libxengnttab.a $(DESTDIR)$(libdir)
+	$(SYMLINK_SHLIB) libxengnttab.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxengnttab.so.$(MAJOR)
+	$(SYMLINK_SHLIB) libxengnttab.so.$(MAJOR) $(DESTDIR)$(libdir)/libxengnttab.so
+	$(INSTALL_DATA) include/xengnttab.h $(DESTDIR)$(includedir)
+
+.PHONY: TAGS
+TAGS:
+	etags -t *.c *.h
+
+.PHONY: clean
+clean:
+	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
+	rm -f libxengnttab.so.$(MAJOR).$(MINOR) libxengnttab.so.$(MAJOR)
+	rm -f headers.chk
+
+.PHONY: distclean
+distclean: clean
diff --git a/tools/libs/gnttab/gntshr_core.c b/tools/libs/gnttab/gntshr_core.c
new file mode 100644
index 0000000..7f6bf9d
--- /dev/null
+++ b/tools/libs/gnttab/gntshr_core.c
@@ -0,0 +1,95 @@
+/******************************************************************************
+ *
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ *
+ * 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/>.
+ *
+ * Split out from xc_gnttab.c
+ */
+
+#include <stdlib.h>
+
+#include "private.h"
+
+xengntshr_handle *xengntshr_open(xentoollog_logger *logger, unsigned open_flags)
+{
+    xengntshr_handle *xgs = malloc(sizeof(*xgs));
+    int rc;
+
+    if (!xgs) return NULL;
+
+    xgs->fd = -1;
+    xgs->logger = logger;
+    xgs->logger_tofree  = NULL;
+
+    if (!xgs->logger) {
+        xgs->logger = xgs->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xgs->logger) goto err;
+    }
+
+    rc = osdep_gntshr_open(xgs);
+    if ( rc  < 0 ) goto err;
+
+    return xgs;
+
+err:
+    osdep_gntshr_close(xgs);
+    xtl_logger_destroy(xgs->logger_tofree);
+    free(xgs);
+    return NULL;
+}
+
+int xengntshr_close(xengntshr_handle *xgs)
+{
+    int rc;
+
+    if ( !xgs )
+        return 0;
+
+    rc = osdep_gntshr_close(xgs);
+    xtl_logger_destroy(xgs->logger_tofree);
+    free(xgs);
+    return rc;
+}
+void *xengntshr_share_pages(xengntshr_handle *xcg, uint32_t domid,
+                            int count, uint32_t *refs, int writable)
+{
+    return osdep_gntshr_share_pages(xcg, domid, count, refs, writable, -1, -1);
+}
+
+void *xengntshr_share_page_notify(xengntshr_handle *xcg, uint32_t domid,
+                                  uint32_t *ref, int writable,
+                                  uint32_t notify_offset,
+                                  evtchn_port_t notify_port)
+{
+    return osdep_gntshr_share_pages(xcg, domid, 1, ref, writable,
+                                    notify_offset, notify_port);
+}
+
+int xengntshr_unshare(xengntshr_handle *xgs, void *start_address, uint32_t count)
+{
+    return osdep_gntshr_unshare(xgs, start_address, count);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/xc_nognttab.c b/tools/libs/gnttab/gntshr_unimp.c
similarity index 52%
rename from tools/libxc/xc_nognttab.c
rename to tools/libs/gnttab/gntshr_unimp.c
index e8a0fcb..e210484 100644
--- a/tools/libxc/xc_nognttab.c
+++ b/tools/libs/gnttab/gntshr_unimp.c
@@ -14,37 +14,49 @@
  *
  * 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/>.
+ *
+ * Split out from xc_gnttab.c
  */
 
 #include <stdlib.h>
 
-#include "xc_private.h"
+#include "private.h"
 
-int osdep_gnttab_open(xc_gnttab *xgt)
+xengntshr_handle *xengntshr_open(xentoollog_logger *logger, unsigned open_flags)
 {
-    return -1;
+    return NULL;
 }
 
-int osdep_gnttab_close(xc_gnttab *xgt)
+int xengntshr_close(xengntshr_handle *xgs)
 {
     return 0;
 }
 
-int xc_gnttab_set_max_grants(xc_gnttab *xgt, uint32_t count)
+void *xengntshr_share_pages(xengntshr_handle *xcg, uint32_t domid,
+                            int count, uint32_t *refs, int writable)
 {
     abort();
 }
 
-void *osdep_gnttab_grant_map(xc_gnttab *xgt,
-                             uint32_t count, int flags, int prot,
-                             uint32_t *domids, uint32_t *refs,
-                             uint32_t notify_offset,
-                             evtchn_port_t notify_port)
+void *xengntshr_share_page_notify(xengntshr_handle *xcg, uint32_t domid,
+                                  uint32_t *ref, int writable,
+                                  uint32_t notify_offset,
+                                  evtchn_port_t notify_port)
 {
     abort();
 }
 
-int xc_gnttab_munmap(xc_gnttab *xgt, void *start_address, uint32_t count)
+int xengntshr_unshare(xengntshr_handle *xgs, void *start_address, uint32_t count)
 {
     abort();
 }
+
+/*
+ * 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/gnttab/gnttab_core.c b/tools/libs/gnttab/gnttab_core.c
new file mode 100644
index 0000000..5d0474d
--- /dev/null
+++ b/tools/libs/gnttab/gnttab_core.c
@@ -0,0 +1,124 @@
+/******************************************************************************
+ *
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ *
+ * 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/>.
+ *
+ * Split out from xc_gnttab.c
+ */
+
+#include <stdlib.h>
+
+#include "private.h"
+
+xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags)
+{
+    xengnttab_handle *xgt = malloc(sizeof(*xgt));
+    int rc;
+
+    if (!xgt) return NULL;
+
+    xgt->fd = -1;
+    xgt->logger = logger;
+    xgt->logger_tofree  = NULL;
+
+    if (!xgt->logger) {
+        xgt->logger = xgt->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xgt->logger) goto err;
+    }
+
+    rc = osdep_gnttab_open(xgt);
+    if ( rc  < 0 ) goto err;
+
+    return xgt;
+
+err:
+    osdep_gnttab_close(xgt);
+    xtl_logger_destroy(xgt->logger_tofree);
+    free(xgt);
+    return NULL;
+}
+
+int xengnttab_close(xengnttab_handle *xgt)
+{
+    int rc;
+
+    if ( !xgt )
+        return 0;
+
+    rc = osdep_gnttab_close(xgt);
+    xtl_logger_destroy(xgt->logger_tofree);
+    free(xgt);
+    return rc;
+}
+
+int xengnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
+{
+    return osdep_gnttab_set_max_grants(xgt, count);
+}
+
+void *xengnttab_map_grant_ref(xengnttab_handle *xgt,
+                              uint32_t domid,
+                              uint32_t ref,
+                              int prot)
+{
+    return osdep_gnttab_grant_map(xgt, 1, 0, prot, &domid, &ref, -1, -1);
+}
+
+void *xengnttab_map_grant_refs(xengnttab_handle *xgt,
+                               uint32_t count,
+                               uint32_t *domids,
+                               uint32_t *refs,
+                               int prot)
+{
+    return osdep_gnttab_grant_map(xgt, count, 0, prot, domids, refs, -1, -1);
+}
+
+void *xengnttab_map_domain_grant_refs(xengnttab_handle *xgt,
+                                      uint32_t count,
+                                      uint32_t domid,
+                                      uint32_t *refs,
+                                      int prot)
+{
+    return osdep_gnttab_grant_map(xgt, count, XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN,
+                                  prot, &domid, refs, -1, -1);
+}
+
+void *xengnttab_map_grant_ref_notify(xengnttab_handle *xgt,
+                                     uint32_t domid,
+                                     uint32_t ref,
+                                     int prot,
+                                     uint32_t notify_offset,
+                                     evtchn_port_t notify_port)
+{
+    return osdep_gnttab_grant_map(xgt, 1, 0, prot,  &domid, &ref,
+                                  notify_offset, notify_port);
+}
+
+int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count)
+{
+    return osdep_gnttab_unmap(xgt, start_address, count);
+}
+
+/*
+ * 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/gnttab/gnttab_unimp.c b/tools/libs/gnttab/gnttab_unimp.c
new file mode 100644
index 0000000..b3a4a20
--- /dev/null
+++ b/tools/libs/gnttab/gnttab_unimp.c
@@ -0,0 +1,89 @@
+/******************************************************************************
+ *
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ *
+ * 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/>.
+ *
+ * Split out from xc_gnttab.c
+ */
+
+#include <stdlib.h>
+
+#include "private.h"
+
+xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags)
+{
+    return NULL;
+}
+
+int xengnttab_close(xengnttab_handle *xgt)
+{
+    return 0;
+}
+
+int xengnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
+{
+    abort();
+}
+
+void *xengnttab_map_grant_ref(xengnttab_handle *xgt,
+                              uint32_t domid,
+                              uint32_t ref,
+                              int prot)
+{
+    abort();
+}
+
+void *xengnttab_map_grant_refs(xengnttab_handle *xgt,
+                               uint32_t count,
+                               uint32_t *domids,
+                               uint32_t *refs,
+                               int prot)
+{
+    abort();
+}
+
+void *xengnttab_map_domain_grant_refs(xengnttab_handle *xgt,
+                                      uint32_t count,
+                                      uint32_t domid,
+                                      uint32_t *refs,
+                                      int prot)
+{
+    abort();
+}
+
+void *xengnttab_map_grant_ref_notify(xengnttab_handle *xgt,
+                                     uint32_t domid,
+                                     uint32_t ref,
+                                     int prot,
+                                     uint32_t notify_offset,
+                                     evtchn_port_t notify_port)
+{
+    abort();
+}
+
+int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count)
+{
+    abort();
+}
+
+/*
+ * 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/gnttab/include/xengnttab.h b/tools/libs/gnttab/include/xengnttab.h
new file mode 100644
index 0000000..700a5f1
--- /dev/null
+++ b/tools/libs/gnttab/include/xengnttab.h
@@ -0,0 +1,216 @@
+/*
+ * 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/>.
+ *
+ * Split off from:
+ * xenctrl.h
+ *
+ * A library for low-level access to the Xen control interfaces.
+ *
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ */
+#ifndef XENGNTTAB_H
+#define XENGNTTAB_H
+
+#include <stdint.h>
+
+#include <xen/grant_table.h>
+#include <xen/event_channel.h>
+
+/* Callers who don't care don't need to #include <xentoollog.h> */
+typedef struct xentoollog_logger xentoollog_logger;
+
+/*
+ * Grant Table Interface (making use of grants from other domains)
+ */
+
+typedef struct xengntdev_handle xengnttab_handle;
+
+/*
+ * Note:
+ * After fork a child process must not use any opened xc gnttab
+ * handle inherited from their parent. They must open a new handle if
+ * they want to interact with xc.
+ *
+ * Return an fd onto the grant table driver.  Logs errors.
+ */
+xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags);
+
+/*
+ * Close a handle previously allocated with xengnttab_open().
+ * Never logs errors.
+ */
+int xengnttab_close(xengnttab_handle *xgt);
+
+/*
+ * Memory maps a grant reference from one domain to a local address range.
+ * Mappings should be unmapped with xengnttab_unmap.  Logs errors.
+ *
+ * @parm xgt a handle on an open grant table interface
+ * @parm domid the domain to map memory from
+ * @parm ref the grant reference ID to map
+ * @parm prot same flag as in mmap()
+ */
+void *xengnttab_map_grant_ref(xengnttab_handle *xgt,
+                              uint32_t domid,
+                              uint32_t ref,
+                              int prot);
+
+/**
+ * Memory maps one or more grant references from one or more domains to a
+ * contiguous local address range. Mappings should be unmapped with
+ * xengnttab_unmap.  Logs errors.
+ *
+ * @parm xgt a handle on an open grant table interface
+ * @parm count the number of grant references to be mapped
+ * @parm domids an array of @count domain IDs by which the corresponding @refs
+ *              were granted
+ * @parm refs an array of @count grant references to be mapped
+ * @parm prot same flag as in mmap()
+ */
+void *xengnttab_map_grant_refs(xengnttab_handle *xgt,
+                               uint32_t count,
+                               uint32_t *domids,
+                               uint32_t *refs,
+                               int prot);
+
+/**
+ * Memory maps one or more grant references from one domain to a
+ * contiguous local address range. Mappings should be unmapped with
+ * xengnttab_unmap.  Logs errors.
+ *
+ * @parm xgt a handle on an open grant table interface
+ * @parm count the number of grant references to be mapped
+ * @parm domid the domain to map memory from
+ * @parm refs an array of @count grant references to be mapped
+ * @parm prot same flag as in mmap()
+ */
+void *xengnttab_map_domain_grant_refs(xengnttab_handle *xgt,
+                                      uint32_t count,
+                                      uint32_t domid,
+                                      uint32_t *refs,
+                                      int prot);
+
+/**
+ * Memory maps a grant reference from one domain to a local address range.
+ * Mappings should be unmapped with xengnttab_unmap. If notify_offset or
+ * notify_port are not -1, this version will attempt to set up an unmap
+ * notification at the given offset and event channel. When the page is
+ * unmapped, the byte at the given offset will be zeroed and a wakeup will be
+ * sent to the given event channel.  Logs errors.
+ *
+ * @parm xgt a handle on an open grant table interface
+ * @parm domid the domain to map memory from
+ * @parm ref the grant reference ID to map
+ * @parm prot same flag as in mmap()
+ * @parm notify_offset The byte offset in the page to use for unmap
+ *                     notification; -1 for none.
+ * @parm notify_port The event channel port to use for unmap notify, or -1
+ */
+void *xengnttab_map_grant_ref_notify(xengnttab_handle *xgt,
+                                     uint32_t domid,
+                                     uint32_t ref,
+                                     int prot,
+                                     uint32_t notify_offset,
+                                     evtchn_port_t notify_port);
+
+/*
+ * Unmaps the @count pages starting at @start_address, which were mapped by a
+ * call to xengnttab_map_grant_ref or xengnttab_map_grant_refs. Never logs.
+ */
+int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count);
+
+/*
+ * Sets the maximum number of grants that may be mapped by the given instance
+ * to @count.  Never logs.
+ *
+ * N.B. This function must be called after opening the handle, and before any
+ *      other functions are invoked on it.
+ *
+ * N.B. When variable-length grants are mapped, fragmentation may be observed,
+ *      and it may not be possible to satisfy requests up to the maximum number
+ *      of grants.
+ */
+int xengnttab_set_max_grants(xengnttab_handle *xgt,
+			     uint32_t count);
+
+/*
+ * Grant Sharing Interface (allocating and granting pages)
+ */
+
+typedef struct xengntdev_handle xengntshr_handle;
+
+/*
+ * Return an fd onto the grant sharing driver.  Logs errors.
+ *
+ * Note:
+ * After fork a child process must not use any opened xc gntshr
+ * handle inherited from their parent. They must open a new handle if
+ * they want to interact with xc.
+ *
+ */
+xengntshr_handle *xengntshr_open(xentoollog_logger *logger,
+			  unsigned open_flags);
+
+/*
+ * Close a handle previously allocated with xengntshr_open().
+ * Never logs errors.
+ */
+int xengntshr_close(xengntshr_handle *xgs);
+
+/*
+ * Creates and shares pages with another domain.
+ *
+ * @parm xgs a handle to an open grant sharing instance
+ * @parm domid the domain to share memory with
+ * @parm count the number of pages to share
+ * @parm refs the grant references of the pages (output)
+ * @parm writable true if the other domain can write to the pages
+ * @return local mapping of the pages
+ */
+void *xengntshr_share_pages(xengntshr_handle *xgs, uint32_t domid,
+                            int count, uint32_t *refs, int writable);
+
+/*
+ * Creates and shares a page with another domain, with unmap notification.
+ *
+ * @parm xgs a handle to an open grant sharing instance
+ * @parm domid the domain to share memory with
+ * @parm refs the grant reference of the pages (output)
+ * @parm writable true if the other domain can write to the page
+ * @parm notify_offset The byte offset in the page to use for unmap
+ *                     notification; -1 for none.
+ * @parm notify_port The event channel port to use for unmap notify, or -1
+ * @return local mapping of the page
+ */
+void *xengntshr_share_page_notify(xengntshr_handle *xgs, uint32_t domid,
+                                  uint32_t *ref, int writable,
+                                  uint32_t notify_offset,
+                                  evtchn_port_t notify_port);
+/*
+ * Unmaps the @count pages starting at @start_address, which were mapped by a
+ * call to xengntshr_share_*. Never logs.
+ */
+int xengntshr_unshare(xengntshr_handle *xgs, void *start_address, uint32_t count);
+
+#endif
+
+/*
+ * 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/gnttab/libxengnttab.map b/tools/libs/gnttab/libxengnttab.map
new file mode 100644
index 0000000..66e8c12
--- /dev/null
+++ b/tools/libs/gnttab/libxengnttab.map
@@ -0,0 +1,23 @@
+VERS_1.0 {
+	global:
+		xengnttab_open;
+		xengnttab_close;
+
+		xengnttab_set_max_grants;
+
+		xengnttab_map_domain_grant_refs;
+		xengnttab_map_grant_ref;
+		xengnttab_map_grant_ref_notify;
+		xengnttab_map_grant_refs;
+
+		xengnttab_unmap;
+		
+		xengntshr_open;
+		xengntshr_close;
+		
+		xengntshr_share_page_notify;
+		xengntshr_share_pages;
+		
+		xengntshr_unshare;
+	local: *; /* Do not expose anything by default */
+};
diff --git a/tools/libs/gnttab/linux.c b/tools/libs/gnttab/linux.c
new file mode 100644
index 0000000..768119a
--- /dev/null
+++ b/tools/libs/gnttab/linux.c
@@ -0,0 +1,329 @@
+/*
+ * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
+ *
+ * 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/>.
+ *
+ * Split out from xc_linux_osdep.c
+ */
+
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include <sys/ioctl.h>
+#include <sys/mman.h>
+
+#include <xen/sys/gntdev.h>
+#include <xen/sys/gntalloc.h>
+
+#include "private.h"
+
+#define DEVXEN "/dev/xen/"
+
+#define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
+
+#define GTERROR(_l, _f...) xtl_log(_l, XTL_ERROR, errno, "gnttab", _f)
+#define GSERROR(_l, _f...) xtl_log(_l, XTL_ERROR, errno, "gntshr", _f)
+
+#define PAGE_SHIFT           12
+#define PAGE_SIZE            (1UL << PAGE_SHIFT)
+#define PAGE_MASK            (~(PAGE_SIZE-1))
+
+int osdep_gnttab_open(xengnttab_handle *xgt)
+{
+    int fd = open(DEVXEN "gntdev", O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xgt->fd = fd;
+    return 0;
+}
+
+int osdep_gnttab_close(xengnttab_handle *xgt)
+{
+    if ( xgt->fd == -1 )
+        return 0;
+
+    return close(xgt->fd);
+}
+
+int osdep_gnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
+{
+    int fd = xgt->fd, rc;
+    struct ioctl_gntdev_set_max_grants max_grants = { .count = count };
+
+    rc = ioctl(fd, IOCTL_GNTDEV_SET_MAX_GRANTS, &max_grants);
+    if (rc) {
+        /*
+         * Newer (e.g. pv-ops) kernels don't implement this IOCTL,
+         * so ignore the resulting specific failure.
+         */
+        if (errno == ENOTTY)
+            rc = 0;
+        else
+            GTERROR(xgt->logger, "ioctl SET_MAX_GRANTS failed");
+    }
+
+    return rc;
+}
+
+void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port)
+{
+    int fd = xgt->fd;
+    struct ioctl_gntdev_map_grant_ref *map;
+    unsigned int map_size = ROUNDUP((sizeof(*map) + (count - 1) *
+                                    sizeof(struct ioctl_gntdev_map_grant_ref)),
+                                    PAGE_SHIFT);
+    void *addr = NULL;
+    int domids_stride = 1;
+    int i;
+
+    if (flags & XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN)
+        domids_stride = 0;
+
+    if ( map_size <= PAGE_SIZE )
+        map = alloca(sizeof(*map) +
+                     (count - 1) * sizeof(struct ioctl_gntdev_map_grant_ref));
+    else
+    {
+        map = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
+                   MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
+        if ( map == MAP_FAILED )
+        {
+            GTERROR(xgt->logger, "mmap of map failed");
+            return NULL;
+        }
+    }
+
+    for ( i = 0; i < count; i++ )
+    {
+        map->refs[i].domid = domids[i * domids_stride];
+        map->refs[i].ref = refs[i];
+    }
+
+    map->count = count;
+
+    if ( ioctl(fd, IOCTL_GNTDEV_MAP_GRANT_REF, map) ) {
+        GTERROR(xgt->logger, "ioctl MAP_GRANT_REF failed");
+        goto out;
+    }
+
+ retry:
+    addr = mmap(NULL, PAGE_SIZE * count, prot, MAP_SHARED, fd,
+                map->index);
+
+    if (addr == MAP_FAILED && errno == EAGAIN)
+    {
+        /*
+         * The grant hypercall can return EAGAIN if the granted page is
+         * swapped out. Since the paging daemon may be in the same domain, the
+         * hypercall cannot block without causing a deadlock.
+         *
+         * Because there are no notificaitons when the page is swapped in, wait
+         * a bit before retrying, and hope that the page will arrive eventually.
+         */
+        usleep(1000);
+        goto retry;
+    }
+
+    if (addr != MAP_FAILED)
+    {
+        int rv = 0;
+        struct ioctl_gntdev_unmap_notify notify;
+        notify.index = map->index;
+        notify.action = 0;
+        if (notify_offset < PAGE_SIZE * count) {
+            notify.index += notify_offset;
+            notify.action |= UNMAP_NOTIFY_CLEAR_BYTE;
+        }
+        if (notify_port != -1) {
+            notify.event_channel_port = notify_port;
+            notify.action |= UNMAP_NOTIFY_SEND_EVENT;
+        }
+        if (notify.action)
+            rv = ioctl(fd, IOCTL_GNTDEV_SET_UNMAP_NOTIFY, &notify);
+        if (rv) {
+            GTERROR(xgt->logger, "ioctl SET_UNMAP_NOTIFY failed");
+            munmap(addr, count * PAGE_SIZE);
+            addr = MAP_FAILED;
+        }
+    }
+
+    if (addr == MAP_FAILED)
+    {
+        int saved_errno = errno;
+        struct ioctl_gntdev_unmap_grant_ref unmap_grant;
+
+        /* Unmap the driver slots used to store the grant information. */
+        GTERROR(xgt->logger, "mmap failed");
+        unmap_grant.index = map->index;
+        unmap_grant.count = count;
+        ioctl(fd, IOCTL_GNTDEV_UNMAP_GRANT_REF, &unmap_grant);
+        errno = saved_errno;
+        addr = NULL;
+    }
+
+ out:
+    if ( map_size > PAGE_SIZE )
+        munmap(map, map_size);
+
+    return addr;
+}
+
+int osdep_gnttab_unmap(xengnttab_handle *xgt,
+                       void *start_address,
+                       uint32_t count)
+{
+    int fd = xgt->fd;
+    struct ioctl_gntdev_get_offset_for_vaddr get_offset;
+    struct ioctl_gntdev_unmap_grant_ref unmap_grant;
+    int rc;
+
+    if ( start_address == NULL )
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    /* First, it is necessary to get the offset which was initially used to
+     * mmap() the pages.
+     */
+    get_offset.vaddr = (unsigned long)start_address;
+    if ( (rc = ioctl(fd, IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR,
+                     &get_offset)) )
+        return rc;
+
+    if ( get_offset.count != count )
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    /* Next, unmap the memory. */
+    if ( (rc = munmap(start_address, count * PAGE_SIZE)) )
+        return rc;
+
+    /* Finally, unmap the driver slots used to store the grant information. */
+    unmap_grant.index = get_offset.offset;
+    unmap_grant.count = count;
+    if ( (rc = ioctl(fd, IOCTL_GNTDEV_UNMAP_GRANT_REF, &unmap_grant)) )
+        return rc;
+
+    return 0;
+}
+
+int osdep_gntshr_open(xengntshr_handle *xgs)
+{
+    int fd = open(DEVXEN "gntalloc", O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xgs->fd = fd;
+    return 0;
+}
+
+int osdep_gntshr_close(xengntshr_handle *xgs)
+{
+    if ( xgs->fd == -1 )
+        return 0;
+
+    return close(xgs->fd);
+}
+
+void *osdep_gntshr_share_pages(xengntshr_handle *xgs,
+                               uint32_t domid, int count,
+                               uint32_t *refs, int writable,
+                               uint32_t notify_offset,
+                               evtchn_port_t notify_port)
+{
+    struct ioctl_gntalloc_alloc_gref *gref_info = NULL;
+    struct ioctl_gntalloc_unmap_notify notify;
+    struct ioctl_gntalloc_dealloc_gref gref_drop;
+    int fd = xgs->fd;
+    int err;
+    void *area = NULL;
+    gref_info = malloc(sizeof(*gref_info) + count * sizeof(uint32_t));
+    if (!gref_info)
+        return NULL;
+    gref_info->domid = domid;
+    gref_info->flags = writable ? GNTALLOC_FLAG_WRITABLE : 0;
+    gref_info->count = count;
+
+    err = ioctl(fd, IOCTL_GNTALLOC_ALLOC_GREF, gref_info);
+    if (err) {
+        GSERROR(xgs->logger, "ioctl failed");
+        goto out;
+    }
+
+    area = mmap(NULL, count * PAGE_SIZE, PROT_READ | PROT_WRITE,
+        MAP_SHARED, fd, gref_info->index);
+
+    if (area == MAP_FAILED) {
+        area = NULL;
+        GSERROR(xgs->logger, "mmap failed");
+        goto out_remove_fdmap;
+    }
+
+    notify.index = gref_info->index;
+    notify.action = 0;
+    if (notify_offset < PAGE_SIZE * count) {
+        notify.index += notify_offset;
+        notify.action |= UNMAP_NOTIFY_CLEAR_BYTE;
+    }
+    if (notify_port != -1) {
+        notify.event_channel_port = notify_port;
+        notify.action |= UNMAP_NOTIFY_SEND_EVENT;
+    }
+    if (notify.action)
+        err = ioctl(fd, IOCTL_GNTALLOC_SET_UNMAP_NOTIFY, &notify);
+    if (err) {
+        GSERROR(xgs->logger, "ioctl SET_UNMAP_NOTIFY failed");
+		munmap(area, count * PAGE_SIZE);
+		area = NULL;
+	}
+
+    memcpy(refs, gref_info->gref_ids, count * sizeof(uint32_t));
+
+ out_remove_fdmap:
+    /* Removing the mapping from the file descriptor does not cause the pages to
+     * be deallocated until the mapping is removed.
+     */
+    gref_drop.index = gref_info->index;
+    gref_drop.count = count;
+    ioctl(fd, IOCTL_GNTALLOC_DEALLOC_GREF, &gref_drop);
+ out:
+    free(gref_info);
+    return area;
+}
+
+int osdep_gntshr_unshare(xengntshr_handle *xgs,
+                         void *start_address, uint32_t count)
+{
+    return munmap(start_address, count * PAGE_SIZE);
+}
+
+/*
+ * 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/gnttab/minios.c b/tools/libs/gnttab/minios.c
new file mode 100644
index 0000000..7e04174
--- /dev/null
+++ b/tools/libs/gnttab/minios.c
@@ -0,0 +1,117 @@
+/*
+ *
+ * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
+ * All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Splitfrom xc_minios.c
+ */
+
+#include <mini-os/types.h>
+#include <mini-os/os.h>
+#include <mini-os/lib.h>
+
+#include <mini-os/gntmap.h>
+#include <sys/mman.h>
+
+#include <errno.h>
+#include <unistd.h>
+
+#include "private.h"
+
+void minios_gnttab_close_fd(int fd);
+
+int osdep_gnttab_open(xengnttab_handle *xgt)
+{
+    int fd = alloc_fd(FTYPE_GNTMAP);
+    if ( fd == -1 )
+        return -1;
+    gntmap_init(&files[fd].gntmap);
+    xgt->fd = fd;
+    return 0;
+}
+
+int osdep_gnttab_close(xengnttab_handle *xgt)
+{
+    if ( xgt->fd == -1 )
+        return 0;
+
+    return close(xgt->fd);
+}
+
+void minios_gnttab_close_fd(int fd)
+{
+    gntmap_fini(&files[fd].gntmap);
+    files[fd].type = FTYPE_NONE;
+}
+
+void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port)
+{
+    int fd = xgt->fd;
+    int stride = 1;
+    if (flags & XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN)
+        stride = 0;
+    if (notify_offset != -1 || notify_port != -1) {
+        errno = ENOSYS;
+        return NULL;
+    }
+    return gntmap_map_grant_refs(&files[fd].gntmap,
+                                 count, domids, stride,
+                                 refs, prot & PROT_WRITE);
+}
+
+int osdep_gnttab_unmap(xengnttab_handle *xgt,
+                       void *start_address,
+                       uint32_t count)
+{
+    int fd = xgt->fd;
+    int ret;
+    ret = gntmap_munmap(&files[fd].gntmap,
+                        (unsigned long) start_address,
+                        count);
+    if (ret < 0) {
+        errno = -ret;
+        return -1;
+    }
+    return ret;
+}
+
+int osdep_gnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
+{
+    int fd = xgt->fd;
+    int ret;
+    ret = gntmap_set_max_grants(&files[fd].gntmap,
+                                count);
+    if (ret < 0) {
+        errno = -ret;
+        return -1;
+    }
+    return ret;
+}
+
+/*
+ * 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/gnttab/private.h b/tools/libs/gnttab/private.h
new file mode 100644
index 0000000..d286c86
--- /dev/null
+++ b/tools/libs/gnttab/private.h
@@ -0,0 +1,47 @@
+#ifndef XENGNTTAB_PRIVATE_H
+#define XENGNTTAB_PRIVATE_H
+
+#include <xentoollog.h>
+#include <xengnttab.h>
+
+struct xengntdev_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    int fd;
+};
+
+int osdep_gnttab_open(xengnttab_handle *xgt);
+int osdep_gnttab_close(xengnttab_handle *xgt);
+
+int osdep_gnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count);
+
+#define XENGNTTAB_GRANT_MAP_SINGLE_DOMAIN 0x1
+void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
+                             uint32_t count, int flags, int prot,
+                             uint32_t *domids, uint32_t *refs,
+                             uint32_t notify_offset,
+                             evtchn_port_t notify_port);
+int osdep_gnttab_unmap(xengnttab_handle *xgt,
+                       void *start_address,
+                       uint32_t count);
+int osdep_gntshr_open(xengntshr_handle *xgs);
+int osdep_gntshr_close(xengntshr_handle *xgs);
+
+void *osdep_gntshr_share_pages(xengntshr_handle *xgs,
+                               uint32_t domid, int count,
+                               uint32_t *refs, int writable,
+                               uint32_t notify_offset,
+                               evtchn_port_t notify_port);
+int osdep_gntshr_unshare(xengntshr_handle *xgs,
+                         void *start_address, uint32_t count);
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libvchan/Makefile b/tools/libvchan/Makefile
index 84128a3..0573d2f 100644
--- a/tools/libvchan/Makefile
+++ b/tools/libvchan/Makefile
@@ -10,15 +10,17 @@ NODE_OBJS = node.o
 NODE2_OBJS = node-select.o
 
 LIBVCHAN_PIC_OBJS = $(patsubst %.o,%.opic,$(LIBVCHAN_OBJS))
-LIBVCHAN_LIBS = $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenevtchn)
-$(LIBVCHAN_OBJS) $(LIBVCHAN_PIC_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
-$(NODE_OBJS) $(NODE2_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
+LIBVCHAN_LIBS = $(LDLIBS_libxenstore) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(LDLIBS_libxenevtchn)
+$(LIBVCHAN_OBJS) $(LIBVCHAN_PIC_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(CFLAGS_libxengnttab) $(CFLAGS_libxengntshr) $(CFLAGS_libxenevtchn)
+$(NODE_OBJS) $(NODE2_OBJS): CFLAGS += $(CFLAGS_libxengnttab) $(CFLAGS_libxengntshr) $(CFLAGS_libxenevtchn)
 
 MAJOR = 1.0
 MINOR = 0
 
 CFLAGS += -I../include -I.
 
+io.o io.opic: CFLAGS += $(CFLAGS_libxenctrl) # for xen_mb et al
+
 .PHONY: all
 all: libxenvchan.so vchan-node1 vchan-node2 libxenvchan.a
 
diff --git a/tools/libvchan/init.c b/tools/libvchan/init.c
index 66cb103..91531b9 100644
--- a/tools/libvchan/init.c
+++ b/tools/libvchan/init.c
@@ -78,7 +78,7 @@ static int init_gnt_srv(struct libxenvchan *ctrl, int domain)
 	uint32_t ring_ref = -1;
 	void *ring;
 
-	ring = xc_gntshr_share_page_notify(ctrl->gntshr, domain,
+	ring = xengntshr_share_page_notify(ctrl->gntshr, domain,
 			&ring_ref, 1, offsetof(struct vchan_interface, srv_live),
 			ctrl->event_port);
 
@@ -104,7 +104,7 @@ static int init_gnt_srv(struct libxenvchan *ctrl, int domain)
 		ctrl->read.buffer = ((void*)ctrl->ring) + LARGE_RING_OFFSET;
 		break;
 	default:
-		ctrl->read.buffer = xc_gntshr_share_pages(ctrl->gntshr, domain,
+		ctrl->read.buffer = xengntshr_share_pages(ctrl->gntshr, domain,
 			pages_left, ctrl->ring->grants, 1);
 		if (!ctrl->read.buffer)
 			goto out_ring;
@@ -118,7 +118,7 @@ static int init_gnt_srv(struct libxenvchan *ctrl, int domain)
 		ctrl->write.buffer = ((void*)ctrl->ring) + LARGE_RING_OFFSET;
 		break;
 	default:
-		ctrl->write.buffer = xc_gntshr_share_pages(ctrl->gntshr, domain,
+		ctrl->write.buffer = xengntshr_share_pages(ctrl->gntshr, domain,
 			pages_right, ctrl->ring->grants + pages_left, 1);
 		if (!ctrl->write.buffer)
 			goto out_unmap_left;
@@ -128,9 +128,9 @@ out:
 	return ring_ref;
 out_unmap_left:
 	if (pages_left)
-		xc_gntshr_munmap(ctrl->gntshr, ctrl->read.buffer, pages_left);
+		xengntshr_unshare(ctrl->gntshr, ctrl->read.buffer, pages_left);
 out_ring:
-	xc_gntshr_munmap(ctrl->gntshr, ring, 1);
+	xengntshr_unshare(ctrl->gntshr, ring, 1);
 	ring_ref = -1;
 	ctrl->ring = NULL;
 	ctrl->write.order = ctrl->read.order = 0;
@@ -142,7 +142,7 @@ static int init_gnt_cli(struct libxenvchan *ctrl, int domain, uint32_t ring_ref)
 	int rv = -1;
 	uint32_t *grants;
 
-	ctrl->ring = xc_gnttab_map_grant_ref_notify(ctrl->gnttab,
+	ctrl->ring = xengnttab_map_grant_ref_notify(ctrl->gnttab,
 		domain, ring_ref, PROT_READ|PROT_WRITE,
 		offsetof(struct vchan_interface, cli_live), ctrl->event_port);
 
@@ -172,7 +172,7 @@ static int init_gnt_cli(struct libxenvchan *ctrl, int domain, uint32_t ring_ref)
 	default:
 		{
 			int pages_left = 1 << (ctrl->write.order - PAGE_SHIFT);
-			ctrl->write.buffer = xc_gnttab_map_domain_grant_refs(ctrl->gnttab,
+			ctrl->write.buffer = xengnttab_map_domain_grant_refs(ctrl->gnttab,
 				pages_left, domain, grants, PROT_READ|PROT_WRITE);
 			if (!ctrl->write.buffer)
 				goto out_unmap_ring;
@@ -190,7 +190,7 @@ static int init_gnt_cli(struct libxenvchan *ctrl, int domain, uint32_t ring_ref)
 	default:
 		{
 			int pages_right = 1 << (ctrl->read.order - PAGE_SHIFT);
-			ctrl->read.buffer = xc_gnttab_map_domain_grant_refs(ctrl->gnttab,
+			ctrl->read.buffer = xengnttab_map_domain_grant_refs(ctrl->gnttab,
 				pages_right, domain, grants, PROT_READ);
 			if (!ctrl->read.buffer)
 				goto out_unmap_left;
@@ -202,10 +202,10 @@ static int init_gnt_cli(struct libxenvchan *ctrl, int domain, uint32_t ring_ref)
 	return rv;
  out_unmap_left:
 	if (ctrl->write.order >= PAGE_SHIFT)
-		xc_gnttab_munmap(ctrl->gnttab, ctrl->write.buffer,
-		                 1 << (ctrl->write.order - PAGE_SHIFT));
+		xengnttab_unmap(ctrl->gnttab, ctrl->write.buffer,
+		                1 << (ctrl->write.order - PAGE_SHIFT));
  out_unmap_ring:
-	xc_gnttab_munmap(ctrl->gnttab, ctrl->ring, 1);
+	xengnttab_unmap(ctrl->gnttab, ctrl->ring, 1);
 	ctrl->ring = 0;
 	ctrl->write.order = ctrl->read.order = 0;
 	rv = -1;
@@ -325,7 +325,7 @@ struct libxenvchan *libxenvchan_server_init(xentoollog_logger *logger, int domai
 		ctrl->write.order = LARGE_RING_SHIFT;
 	}
 
-	ctrl->gntshr = xc_gntshr_open(logger, 0);
+	ctrl->gntshr = xengntshr_open(logger, 0);
 	if (!ctrl->gntshr)
 		goto out;
 
@@ -413,7 +413,7 @@ struct libxenvchan *libxenvchan_client_init(xentoollog_logger *logger, int domai
 	if (!ctrl->event_port)
 		goto fail;
 
-	ctrl->gnttab = xc_gnttab_open(logger, 0);
+	ctrl->gnttab = xengnttab_open(logger, 0);
 	if (!ctrl->gnttab)
 		goto fail;
 
diff --git a/tools/libvchan/io.c b/tools/libvchan/io.c
index 53393a5..da303fb 100644
--- a/tools/libvchan/io.c
+++ b/tools/libvchan/io.c
@@ -366,10 +366,10 @@ void libxenvchan_close(struct libxenvchan *ctrl)
 	if (ctrl->ring) {
 		if (ctrl->is_server) {
 			ctrl->ring->srv_live = 0;
-			xc_gntshr_munmap(ctrl->gntshr, ctrl->ring, 1);
+			xengntshr_unshare(ctrl->gntshr, ctrl->ring, 1);
 		} else {
 			ctrl->ring->cli_live = 0;
-			xc_gnttab_munmap(ctrl->gnttab, ctrl->ring, 1);
+			xengnttab_unmap(ctrl->gnttab, ctrl->ring, 1);
 		}
 	}
 	if (ctrl->event) {
@@ -379,10 +379,10 @@ void libxenvchan_close(struct libxenvchan *ctrl)
 	}
 	if (ctrl->is_server) {
 		if (ctrl->gntshr)
-			xc_gntshr_close(ctrl->gntshr);
+			xengntshr_close(ctrl->gntshr);
 	} else {
 		if (ctrl->gnttab)
-			xc_gnttab_close(ctrl->gnttab);
+			xengnttab_close(ctrl->gnttab);
 	}
 	free(ctrl);
 }
diff --git a/tools/libvchan/libxenvchan.h b/tools/libvchan/libxenvchan.h
index 1544378..341c375 100644
--- a/tools/libvchan/libxenvchan.h
+++ b/tools/libvchan/libxenvchan.h
@@ -45,7 +45,7 @@
 #include <xen/io/libxenvchan.h>
 #include <xen/sys/evtchn.h>
 #include <xenevtchn.h>
-#include <xenctrl.h>
+#include <xengnttab.h>
 
 struct libxenvchan_ring {
 	/* Pointer into the shared page. Offsets into buffer. */
@@ -66,8 +66,8 @@ struct libxenvchan_ring {
 struct libxenvchan {
 	/* Mapping handle for shared ring page */
 	union {
-		xc_gntshr *gntshr; /* for server */
-		xc_gnttab *gnttab; /* for client */
+		xengntshr_handle *gntshr; /* for server */
+		xengnttab_handle *gnttab; /* for client */
 	};
 	/* Pointer to shared ring page */
 	struct vchan_interface *ring;
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index 184cbb7..33d18db 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -43,12 +43,13 @@ CTRL_SRCS-y       += xc_resource.c
 CTRL_SRCS-$(CONFIG_X86) += xc_psr.c
 CTRL_SRCS-$(CONFIG_X86) += xc_pagetab.c
 CTRL_SRCS-$(CONFIG_Linux) += xc_linux.c xc_linux_osdep.c
-CTRL_SRCS-$(CONFIG_FreeBSD) += xc_freebsd.c xc_freebsd_osdep.c xc_nognttab.c xc_nogntshr.c
-CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c xc_nognttab.c xc_nogntshr.c
-CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c xc_nognttab.c xc_nogntshr.c
-CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c xc_nognttab.c xc_nogntshr.c
-CTRL_SRCS-$(CONFIG_MiniOS) += xc_minios.c xc_nogntshr.c
+CTRL_SRCS-$(CONFIG_FreeBSD) += xc_freebsd.c xc_freebsd_osdep.c
+CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c
+CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c
+CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c
+CTRL_SRCS-$(CONFIG_MiniOS) += xc_minios.c
 CTRL_SRCS-y       += xc_evtchn_compat.c
+CTRL_SRCS-y       += xc_gnttab_compat.c
 
 GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
@@ -126,6 +127,8 @@ OSDEP_PIC_OBJS := $(patsubst %.c,%.opic,$(OSDEP_SRCS-y))
 $(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) $(OSDEP_LIB_OBJS) \
 $(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS) $(OSDEP_PIC_OBJS) : CFLAGS += -include $(XEN_ROOT)/tools/config.h
 
+$(CTRL_LIB_OBJS) $(CTRL_PIC_OBJS): CFLAGS += $(CFLAGS_libxengnttab) $(CFLAGS_libxengntshr)
+
 LIB := libxenctrl.a
 ifneq ($(nosharedlibs),y)
 LIB += libxenctrl.so libxenctrl.so.$(MAJOR) libxenctrl.so.$(MAJOR).$(MINOR)
@@ -209,7 +212,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
 	$(SYMLINK_SHLIB) $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 # libxenguest
 
diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 0fc2a11..16e2628 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -5,9 +5,6 @@
  *
  * Copyright (c) 2003-2004, K A Fraser.
  *
- * xc_gnttab functions:
- * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
- *
  * 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;
@@ -117,8 +114,6 @@
  */
 
 typedef struct xc_interface_core xc_interface;
-typedef struct xengntdev_handle xc_gnttab;
-typedef struct xengntdev_handle xc_gntshr;
 
 enum xc_error_code {
   XC_ERROR_NONE = 0,
@@ -1548,116 +1543,6 @@ int xc_domain_subscribe_for_suspend(
  * These functions sometimes log messages as above, but not always.
  */
 
-/*
- * Note:
- * After fork a child process must not use any opened xc gnttab
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
- *
- * Return an fd onto the grant table driver.  Logs errors.
- */
-xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
-			  unsigned open_flags);
-
-/*
- * Close a handle previously allocated with xc_gnttab_open().
- * Never logs errors.
- */
-int xc_gnttab_close(xc_gnttab *xcg);
-
-/*
- * Memory maps a grant reference from one domain to a local address range.
- * Mappings should be unmapped with xc_gnttab_munmap.  Logs errors.
- *
- * @parm xcg a handle on an open grant table interface
- * @parm domid the domain to map memory from
- * @parm ref the grant reference ID to map
- * @parm prot same flag as in mmap()
- */
-void *xc_gnttab_map_grant_ref(xc_gnttab *xcg,
-                              uint32_t domid,
-                              uint32_t ref,
-                              int prot);
-
-/**
- * Memory maps one or more grant references from one or more domains to a
- * contiguous local address range. Mappings should be unmapped with
- * xc_gnttab_munmap.  Logs errors.
- *
- * @parm xcg a handle on an open grant table interface
- * @parm count the number of grant references to be mapped
- * @parm domids an array of @count domain IDs by which the corresponding @refs
- *              were granted
- * @parm refs an array of @count grant references to be mapped
- * @parm prot same flag as in mmap()
- */
-void *xc_gnttab_map_grant_refs(xc_gnttab *xcg,
-                               uint32_t count,
-                               uint32_t *domids,
-                               uint32_t *refs,
-                               int prot);
-
-/**
- * Memory maps one or more grant references from one domain to a
- * contiguous local address range. Mappings should be unmapped with
- * xc_gnttab_munmap.  Logs errors.
- *
- * @parm xcg a handle on an open grant table interface
- * @parm count the number of grant references to be mapped
- * @parm domid the domain to map memory from
- * @parm refs an array of @count grant references to be mapped
- * @parm prot same flag as in mmap()
- */
-void *xc_gnttab_map_domain_grant_refs(xc_gnttab *xcg,
-                                      uint32_t count,
-                                      uint32_t domid,
-                                      uint32_t *refs,
-                                      int prot);
-
-/**
- * Memory maps a grant reference from one domain to a local address range.
- * Mappings should be unmapped with xc_gnttab_munmap. If notify_offset or
- * notify_port are not -1, this version will attempt to set up an unmap
- * notification at the given offset and event channel. When the page is
- * unmapped, the byte at the given offset will be zeroed and a wakeup will be
- * sent to the given event channel.  Logs errors.
- *
- * @parm xcg a handle on an open grant table interface
- * @parm domid the domain to map memory from
- * @parm ref the grant reference ID to map
- * @parm prot same flag as in mmap()
- * @parm notify_offset The byte offset in the page to use for unmap
- *                     notification; -1 for none.
- * @parm notify_port The event channel port to use for unmap notify, or -1
- */
-void *xc_gnttab_map_grant_ref_notify(xc_gnttab *xcg,
-                                     uint32_t domid,
-                                     uint32_t ref,
-                                     int prot,
-                                     uint32_t notify_offset,
-                                     evtchn_port_t notify_port);
-
-/*
- * Unmaps the @count pages starting at @start_address, which were mapped by a
- * call to xc_gnttab_map_grant_ref or xc_gnttab_map_grant_refs. Never logs.
- */
-int xc_gnttab_munmap(xc_gnttab *xcg,
-                     void *start_address,
-                     uint32_t count);
-
-/*
- * Sets the maximum number of grants that may be mapped by the given instance
- * to @count.  Never logs.
- *
- * N.B. This function must be called after opening the handle, and before any
- *      other functions are invoked on it.
- *
- * N.B. When variable-length grants are mapped, fragmentation may be observed,
- *      and it may not be possible to satisfy requests up to the maximum number
- *      of grants.
- */
-int xc_gnttab_set_max_grants(xc_gnttab *xcg,
-			     uint32_t count);
 
 int xc_gnttab_op(xc_interface *xch, int cmd,
                  void * op, int op_size, int count);
@@ -1668,59 +1553,6 @@ grant_entry_v1_t *xc_gnttab_map_table_v1(xc_interface *xch, int domid, int *gnt_
 grant_entry_v2_t *xc_gnttab_map_table_v2(xc_interface *xch, int domid, int *gnt_num);
 /* Sometimes these don't set errno [fixme], and sometimes they don't log. */
 
-/*
- * Return an fd onto the grant sharing driver.  Logs errors.
- *
- * Note:
- * After fork a child process must not use any opened xc gntshr
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
- *
- */
-xc_gntshr *xc_gntshr_open(xentoollog_logger *logger,
-			  unsigned open_flags);
-
-/*
- * Close a handle previously allocated with xc_gntshr_open().
- * Never logs errors.
- */
-int xc_gntshr_close(xc_gntshr *xcg);
-
-/*
- * Creates and shares pages with another domain.
- * 
- * @parm xcg a handle to an open grant sharing instance
- * @parm domid the domain to share memory with
- * @parm count the number of pages to share
- * @parm refs the grant references of the pages (output)
- * @parm writable true if the other domain can write to the pages
- * @return local mapping of the pages
- */
-void *xc_gntshr_share_pages(xc_gntshr *xcg, uint32_t domid,
-                            int count, uint32_t *refs, int writable);
-
-/*
- * Creates and shares a page with another domain, with unmap notification.
- * 
- * @parm xcg a handle to an open grant sharing instance
- * @parm domid the domain to share memory with
- * @parm refs the grant reference of the pages (output)
- * @parm writable true if the other domain can write to the page
- * @parm notify_offset The byte offset in the page to use for unmap
- *                     notification; -1 for none.
- * @parm notify_port The event channel port to use for unmap notify, or -1
- * @return local mapping of the page
- */
-void *xc_gntshr_share_page_notify(xc_gntshr *xcg, uint32_t domid,
-                                  uint32_t *ref, int writable,
-                                  uint32_t notify_offset,
-                                  evtchn_port_t notify_port);
-/*
- * Unmaps the @count pages starting at @start_address, which were mapped by a
- * call to xc_gntshr_share_*. Never logs.
- */
-int xc_gntshr_munmap(xc_gntshr *xcg, void *start_address, uint32_t count);
-
 int xc_physdev_map_pirq(xc_interface *xch,
                         int domid,
                         int index,
diff --git a/tools/libxc/include/xenctrl_compat.h b/tools/libxc/include/xenctrl_compat.h
index 48daeb2..d99fa11 100644
--- a/tools/libxc/include/xenctrl_compat.h
+++ b/tools/libxc/include/xenctrl_compat.h
@@ -35,6 +35,54 @@ int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port);
 
 #endif /* XC_WANT_COMPAT_EVTCHN_API */
 
+#ifdef XC_WANT_COMPAT_GNTTAB_API
+
+typedef struct xengntdev_handle xc_gnttab;
+
+xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
+                          unsigned open_flags);
+int xc_gnttab_close(xc_gnttab *xcg);
+void *xc_gnttab_map_grant_ref(xc_gnttab *xcg,
+                              uint32_t domid,
+                              uint32_t ref,
+                              int prot);
+void *xc_gnttab_map_grant_refs(xc_gnttab *xcg,
+                               uint32_t count,
+                               uint32_t *domids,
+                               uint32_t *refs,
+                               int prot);
+void *xc_gnttab_map_domain_grant_refs(xc_gnttab *xcg,
+                                      uint32_t count,
+                                      uint32_t domid,
+                                      uint32_t *refs,
+                                      int prot);
+void *xc_gnttab_map_grant_ref_notify(xc_gnttab *xcg,
+                                     uint32_t domid,
+                                     uint32_t ref,
+                                     int prot,
+                                     uint32_t notify_offset,
+                                     evtchn_port_t notify_port);
+int xc_gnttab_munmap(xc_gnttab *xcg,
+                     void *start_address,
+                     uint32_t count);
+int xc_gnttab_set_max_grants(xc_gnttab *xcg,
+                             uint32_t count);
+
+typedef struct xengntdev_handle xc_gntshr;
+
+xc_gntshr *xc_gntshr_open(xentoollog_logger *logger,
+                          unsigned open_flags);
+int xc_gntshr_close(xc_gntshr *xcg);
+void *xc_gntshr_share_pages(xc_gntshr *xcg, uint32_t domid,
+                            int count, uint32_t *refs, int writable);
+void *xc_gntshr_share_page_notify(xc_gntshr *xcg, uint32_t domid,
+                                  uint32_t *ref, int writable,
+                                  uint32_t notify_offset,
+                                  evtchn_port_t notify_port);
+int xc_gntshr_munmap(xc_gntshr *xcg, void *start_address, uint32_t count);
+
+#endif /* XC_WANT_COMPAT_GNTTAB_API */
+
 #endif
 
 /*
diff --git a/tools/libxc/xc_gnttab.c b/tools/libxc/xc_gnttab.c
index a51f405..dd32aa2 100644
--- a/tools/libxc/xc_gnttab.c
+++ b/tools/libxc/xc_gnttab.c
@@ -143,59 +143,6 @@ grant_entry_v2_t *xc_gnttab_map_table_v2(xc_interface *xch, int domid,
     return _gnttab_map_table(xch, domid, gnt_num);
 }
 
-void *xc_gnttab_map_grant_ref(xc_gnttab *xgt,
-                              uint32_t domid,
-                              uint32_t ref,
-                              int prot)
-{
-    return osdep_gnttab_grant_map(xgt, 1, 0, prot, &domid, &ref, -1, -1);
-}
-
-void *xc_gnttab_map_grant_refs(xc_gnttab *xgt,
-                               uint32_t count,
-                               uint32_t *domids,
-                               uint32_t *refs,
-                               int prot)
-{
-    return osdep_gnttab_grant_map(xgt, count, 0, prot, domids, refs, -1, -1);
-}
-
-void *xc_gnttab_map_domain_grant_refs(xc_gnttab *xgt,
-                                      uint32_t count,
-                                      uint32_t domid,
-                                      uint32_t *refs,
-                                      int prot)
-{
-    return osdep_gnttab_grant_map(xgt, count, XC_GRANT_MAP_SINGLE_DOMAIN,
-                                  prot, &domid, refs, -1, -1);
-}
-
-void *xc_gnttab_map_grant_ref_notify(xc_gnttab *xgt,
-                                     uint32_t domid,
-                                     uint32_t ref,
-                                     int prot,
-                                     uint32_t notify_offset,
-                                     evtchn_port_t notify_port)
-{
-    return osdep_gnttab_grant_map(xgt, 1, 0, prot,  &domid, &ref,
-                                  notify_offset, notify_port);
-}
-
-void *xc_gntshr_share_pages(xc_gntshr *xcg, uint32_t domid,
-                            int count, uint32_t *refs, int writable)
-{
-    return osdep_gntshr_share_pages(xcg, domid, count, refs, writable, -1, -1);
-}
-
-void *xc_gntshr_share_page_notify(xc_gntshr *xcg, uint32_t domid,
-                                  uint32_t *ref, int writable,
-                                  uint32_t notify_offset,
-                                  evtchn_port_t notify_port)
-{
-    return osdep_gntshr_share_pages(xcg, domid, 1, ref, writable,
-                                    notify_offset, notify_port);
-}
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_gnttab_compat.c b/tools/libxc/xc_gnttab_compat.c
new file mode 100644
index 0000000..6f036d8
--- /dev/null
+++ b/tools/libxc/xc_gnttab_compat.c
@@ -0,0 +1,111 @@
+/*
+ * Compat shims for use of 3rd party consumers of libxenctrl xc_gnt{tab,shr}
+ * functionality which has been split into separate libraries.
+ */
+
+#include <xengnttab.h>
+
+#define XC_WANT_COMPAT_GNTTAB_API
+#include "xenctrl.h"
+
+xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
+                          unsigned open_flags)
+{
+    return xengnttab_open(logger, open_flags);
+}
+
+int xc_gnttab_close(xc_gnttab *xcg)
+{
+    return xengnttab_close(xcg);
+}
+
+void *xc_gnttab_map_grant_ref(xc_gnttab *xcg,
+                              uint32_t domid,
+                              uint32_t ref,
+                              int prot)
+{
+    return xengnttab_map_grant_ref(xcg, domid, ref, prot);
+}
+
+void *xc_gnttab_map_grant_refs(xc_gnttab *xcg,
+                               uint32_t count,
+                               uint32_t *domids,
+                               uint32_t *refs,
+                               int prot)
+{
+    return xengnttab_map_grant_refs(xcg, count, domids, refs, prot);
+}
+
+void *xc_gnttab_map_domain_grant_refs(xc_gnttab *xcg,
+                                      uint32_t count,
+                                      uint32_t domid,
+                                      uint32_t *refs,
+                                      int prot)
+{
+    return xengnttab_map_domain_grant_refs(xcg, count, domid, refs, prot);
+}
+
+void *xc_gnttab_map_grant_ref_notify(xc_gnttab *xcg,
+                                     uint32_t domid,
+                                     uint32_t ref,
+                                     int prot,
+                                     uint32_t notify_offset,
+                                     evtchn_port_t notify_port)
+{
+    return xengnttab_map_grant_ref_notify(xcg, domid, ref, prot,
+                                          notify_offset, notify_port);
+}
+
+int xc_gnttab_munmap(xc_gnttab *xcg,
+                     void *start_address,
+                     uint32_t count)
+{
+    return xengnttab_unmap(xcg, start_address, count);
+}
+
+int xc_gnttab_set_max_grants(xc_gnttab *xcg,
+                             uint32_t count)
+{
+    return xengnttab_set_max_grants(xcg, count);
+}
+
+xc_gntshr *xc_gntshr_open(xentoollog_logger *logger,
+                          unsigned open_flags)
+{
+    return xengntshr_open(logger, open_flags);
+}
+
+int xc_gntshr_close(xc_gntshr *xcg)
+{
+    return xengntshr_close(xcg);
+}
+
+void *xc_gntshr_share_pages(xc_gntshr *xcg, uint32_t domid,
+                            int count, uint32_t *refs, int writable)
+{
+    return xengntshr_share_pages(xcg, domid, count, refs, writable);
+}
+
+void *xc_gntshr_share_page_notify(xc_gntshr *xcg, uint32_t domid,
+                                  uint32_t *ref, int writable,
+                                  uint32_t notify_offset,
+                                  evtchn_port_t notify_port)
+{
+    return xengntshr_share_page_notify(xcg, domid, ref, writable,
+                                       notify_offset, notify_port);
+}
+
+int xc_gntshr_munmap(xc_gntshr *xcg, void *start_address, uint32_t count)
+{
+    return xengntshr_unshare(xcg, start_address, count);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 6b329ce..9c318e0 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -31,8 +31,6 @@
 #include <sys/ioctl.h>
 
 #include <xen/memory.h>
-#include <xen/sys/gntdev.h>
-#include <xen/sys/gntalloc.h>
 
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
@@ -41,9 +39,6 @@
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
-#define GTERROR(_l, _f...) xtl_log(_l, XTL_ERROR, errno, "gnttab", _f)
-#define GSERROR(_l, _f...) xtl_log(_l, XTL_ERROR, errno, "gntshr", _f)
-
 static xc_osdep_handle linux_privcmd_open(xc_interface *xch)
 {
     int flags, saved_errno;
@@ -461,281 +456,6 @@ static struct xc_osdep_ops linux_privcmd_ops = {
     },
 };
 
-#define DEVXEN "/dev/xen/"
-
-int osdep_gnttab_open(xc_gnttab *xgt)
-{
-    int fd = open(DEVXEN "gntdev", O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xgt->fd = fd;
-    return 0;
-}
-
-int osdep_gnttab_close(xc_gnttab *xgt)
-{
-    if ( xgt->fd == -1 )
-        return 0;
-
-    return close(xgt->fd);
-}
-
-int xc_gnttab_set_max_grants(xc_gnttab *xgt, uint32_t count)
-{
-    int fd = xgt->fd, rc;
-    struct ioctl_gntdev_set_max_grants max_grants = { .count = count };
-
-    rc = ioctl(fd, IOCTL_GNTDEV_SET_MAX_GRANTS, &max_grants);
-    if (rc) {
-        /*
-         * Newer (e.g. pv-ops) kernels don't implement this IOCTL,
-         * so ignore the resulting specific failure.
-         */
-        if (errno == ENOTTY)
-            rc = 0;
-        else
-            GTERROR(xgt->logger, "ioctl SET_MAX_GRANTS failed");
-    }
-
-    return rc;
-}
-
-void *osdep_gnttab_grant_map(xc_gnttab *xgt,
-                             uint32_t count, int flags, int prot,
-                             uint32_t *domids, uint32_t *refs,
-                             uint32_t notify_offset,
-                             evtchn_port_t notify_port)
-{
-    int fd = xgt->fd;
-    struct ioctl_gntdev_map_grant_ref *map;
-    unsigned int map_size = ROUNDUP((sizeof(*map) + (count - 1) *
-                                    sizeof(struct ioctl_gntdev_map_grant_ref)),
-                                    XC_PAGE_SHIFT);
-    void *addr = NULL;
-    int domids_stride = 1;
-    int i;
-
-    if (flags & XC_GRANT_MAP_SINGLE_DOMAIN)
-        domids_stride = 0;
-
-    if ( map_size <= XC_PAGE_SIZE )
-        map = alloca(sizeof(*map) +
-                     (count - 1) * sizeof(struct ioctl_gntdev_map_grant_ref));
-    else
-    {
-        map = mmap(NULL, map_size, PROT_READ | PROT_WRITE,
-                   MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
-        if ( map == MAP_FAILED )
-        {
-            GTERROR(xgt->logger, "mmap of map failed");
-            return NULL;
-        }
-    }
-
-    for ( i = 0; i < count; i++ )
-    {
-        map->refs[i].domid = domids[i * domids_stride];
-        map->refs[i].ref = refs[i];
-    }
-
-    map->count = count;
-
-    if ( ioctl(fd, IOCTL_GNTDEV_MAP_GRANT_REF, map) ) {
-        GTERROR(xgt->logger, "ioctl MAP_GRANT_REF failed");
-        goto out;
-    }
-
- retry:
-    addr = mmap(NULL, XC_PAGE_SIZE * count, prot, MAP_SHARED, fd,
-                map->index);
-
-    if (addr == MAP_FAILED && errno == EAGAIN)
-    {
-        /*
-         * The grant hypercall can return EAGAIN if the granted page is
-         * swapped out. Since the paging daemon may be in the same domain, the
-         * hypercall cannot block without causing a deadlock.
-         *
-         * Because there are no notificaitons when the page is swapped in, wait
-         * a bit before retrying, and hope that the page will arrive eventually.
-         */
-        usleep(1000);
-        goto retry;
-    }
-
-    if (addr != MAP_FAILED)
-    {
-        int rv = 0;
-        struct ioctl_gntdev_unmap_notify notify;
-        notify.index = map->index;
-        notify.action = 0;
-        if (notify_offset < XC_PAGE_SIZE * count) {
-            notify.index += notify_offset;
-            notify.action |= UNMAP_NOTIFY_CLEAR_BYTE;
-        }
-        if (notify_port != -1) {
-            notify.event_channel_port = notify_port;
-            notify.action |= UNMAP_NOTIFY_SEND_EVENT;
-        }
-        if (notify.action)
-            rv = ioctl(fd, IOCTL_GNTDEV_SET_UNMAP_NOTIFY, &notify);
-        if (rv) {
-            GTERROR(xgt->logger, "ioctl SET_UNMAP_NOTIFY failed");
-            munmap(addr, count * XC_PAGE_SIZE);
-            addr = MAP_FAILED;
-        }
-    }
-
-    if (addr == MAP_FAILED)
-    {
-        int saved_errno = errno;
-        struct ioctl_gntdev_unmap_grant_ref unmap_grant;
-
-        /* Unmap the driver slots used to store the grant information. */
-        GTERROR(xgt->logger, "mmap failed");
-        unmap_grant.index = map->index;
-        unmap_grant.count = count;
-        ioctl(fd, IOCTL_GNTDEV_UNMAP_GRANT_REF, &unmap_grant);
-        errno = saved_errno;
-        addr = NULL;
-    }
-
- out:
-    if ( map_size > XC_PAGE_SIZE )
-        munmap(map, map_size);
-
-    return addr;
-}
-
-int xc_gnttab_munmap(xc_gnttab *xgt, void *start_address, uint32_t count)
-{
-    int fd = xgt->fd;
-    struct ioctl_gntdev_get_offset_for_vaddr get_offset;
-    struct ioctl_gntdev_unmap_grant_ref unmap_grant;
-    int rc;
-
-    if ( start_address == NULL )
-    {
-        errno = EINVAL;
-        return -1;
-    }
-
-    /* First, it is necessary to get the offset which was initially used to
-     * mmap() the pages.
-     */
-    get_offset.vaddr = (unsigned long)start_address;
-    if ( (rc = ioctl(fd, IOCTL_GNTDEV_GET_OFFSET_FOR_VADDR,
-                     &get_offset)) )
-        return rc;
-
-    if ( get_offset.count != count )
-    {
-        errno = EINVAL;
-        return -1;
-    }
-
-    /* Next, unmap the memory. */
-    if ( (rc = munmap(start_address, count * XC_PAGE_SIZE)) )
-        return rc;
-
-    /* Finally, unmap the driver slots used to store the grant information. */
-    unmap_grant.index = get_offset.offset;
-    unmap_grant.count = count;
-    if ( (rc = ioctl(fd, IOCTL_GNTDEV_UNMAP_GRANT_REF, &unmap_grant)) )
-        return rc;
-
-    return 0;
-}
-
-int osdep_gntshr_open(xc_gntshr *xgs)
-{
-    int fd = open(DEVXEN "gntalloc", O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xgs->fd = fd;
-    return 0;
-}
-
-int osdep_gntshr_close(xc_gntshr *xgs)
-{
-    if ( xgs->fd == -1 )
-        return 0;
-
-    return close(xgs->fd);
-}
-
-void *osdep_gntshr_share_pages(xc_gntshr *xgs,
-                               uint32_t domid, int count,
-                               uint32_t *refs, int writable,
-                               uint32_t notify_offset,
-                               evtchn_port_t notify_port)
-{
-    struct ioctl_gntalloc_alloc_gref *gref_info = NULL;
-    struct ioctl_gntalloc_unmap_notify notify;
-    struct ioctl_gntalloc_dealloc_gref gref_drop;
-    int fd = xgs->fd;
-    int err;
-    void *area = NULL;
-    gref_info = malloc(sizeof(*gref_info) + count * sizeof(uint32_t));
-    if (!gref_info)
-        return NULL;
-    gref_info->domid = domid;
-    gref_info->flags = writable ? GNTALLOC_FLAG_WRITABLE : 0;
-    gref_info->count = count;
-
-    err = ioctl(fd, IOCTL_GNTALLOC_ALLOC_GREF, gref_info);
-    if (err) {
-        GSERROR(xgs->logger, "ioctl failed");
-        goto out;
-    }
-
-    area = mmap(NULL, count * XC_PAGE_SIZE, PROT_READ | PROT_WRITE,
-        MAP_SHARED, fd, gref_info->index);
-
-    if (area == MAP_FAILED) {
-        area = NULL;
-        GSERROR(xgs->logger, "mmap failed");
-        goto out_remove_fdmap;
-    }
-
-    notify.index = gref_info->index;
-    notify.action = 0;
-    if (notify_offset < XC_PAGE_SIZE * count) {
-        notify.index += notify_offset;
-        notify.action |= UNMAP_NOTIFY_CLEAR_BYTE;
-    }
-    if (notify_port != -1) {
-        notify.event_channel_port = notify_port;
-        notify.action |= UNMAP_NOTIFY_SEND_EVENT;
-    }
-    if (notify.action)
-        err = ioctl(fd, IOCTL_GNTALLOC_SET_UNMAP_NOTIFY, &notify);
-    if (err) {
-        GSERROR(xgs->logger, "ioctl SET_UNMAP_NOTIFY failed");
-		munmap(area, count * XC_PAGE_SIZE);
-		area = NULL;
-	}
-
-    memcpy(refs, gref_info->gref_ids, count * sizeof(uint32_t));
-
- out_remove_fdmap:
-    /* Removing the mapping from the file descriptor does not cause the pages to
-     * be deallocated until the mapping is removed.
-     */
-    gref_drop.index = gref_info->index;
-    gref_drop.count = count;
-    ioctl(fd, IOCTL_GNTALLOC_DEALLOC_GREF, &gref_drop);
- out:
-    free(gref_info);
-    return area;
-}
-
-int xc_gntshr_munmap(xc_gntshr *xgs,
-                     void *start_address, uint32_t count)
-{
-    return munmap(start_address, count * XC_PAGE_SIZE);
-}
-
 static struct xc_osdep_ops *linux_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index fd7def6..22d985c 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -23,8 +23,6 @@
 #include <mini-os/os.h>
 #include <mini-os/mm.h>
 #include <mini-os/lib.h>
-#include <mini-os/gntmap.h>
-#include <sys/mman.h>
 
 #include <xen/memory.h>
 #include <unistd.h>
@@ -38,7 +36,6 @@
 #include "xc_private.h"
 
 void minios_interface_close_fd(int fd);
-void minios_gnttab_close_fd(int fd);
 
 extern void minios_interface_close_fd(int fd);
 
@@ -202,76 +199,6 @@ void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
     return memalign(alignment, size);
 }
 
-int osdep_gnttab_open(xc_gnttab *xgt)
-{
-    int fd = alloc_fd(FTYPE_GNTMAP);
-    if ( fd == -1 )
-        return -1;
-    gntmap_init(&files[fd].gntmap);
-    xgt->fd = fd;
-    return 0;
-}
-
-int osdep_gnttab_close(xc_gnttab *xgt)
-{
-    if ( xgt->fd == -1 )
-        return 0;
-
-    return close(xgt->fd);
-}
-
-void minios_gnttab_close_fd(int fd)
-{
-    gntmap_fini(&files[fd].gntmap);
-    files[fd].type = FTYPE_NONE;
-}
-
-void *osdep_gnttab_grant_map(xc_gnttab *xgt,
-                             uint32_t count, int flags, int prot,
-                             uint32_t *domids, uint32_t *refs,
-                             uint32_t notify_offset,
-                             evtchn_port_t notify_port)
-{
-    int fd = xgt->fd;
-    int stride = 1;
-    if (flags & XC_GRANT_MAP_SINGLE_DOMAIN)
-        stride = 0;
-    if (notify_offset != -1 || notify_port != -1) {
-        errno = ENOSYS;
-        return NULL;
-    }
-    return gntmap_map_grant_refs(&files[fd].gntmap,
-                                 count, domids, stride,
-                                 refs, prot & PROT_WRITE);
-}
-
-int xc_gnttab_munmap(xc_gnttab *xgt, void *start_address, uint32_t count)
-{
-    int fd = xgt->fd;
-    int ret;
-    ret = gntmap_munmap(&files[fd].gntmap,
-                        (unsigned long) start_address,
-                        count);
-    if (ret < 0) {
-        errno = -ret;
-        return -1;
-    }
-    return ret;
-}
-
-int xc_gnttab_set_max_grants(xc_gnttab *xgt, uint32_t count)
-{
-    int fd = xgt->fd;
-    int ret;
-    ret = gntmap_set_max_grants(&files[fd].gntmap,
-                                count);
-    if (ret < 0) {
-        errno = -ret;
-        return -1;
-    }
-    return ret;
-}
-
 static struct xc_osdep_ops *minios_osdep_init(xc_interface *xch, enum xc_osdep_type type)
 {
     switch ( type )
diff --git a/tools/libxc/xc_nogntshr.c b/tools/libxc/xc_nogntshr.c
deleted file mode 100644
index 9aa6064..0000000
--- a/tools/libxc/xc_nogntshr.c
+++ /dev/null
@@ -1,46 +0,0 @@
-/******************************************************************************
- *
- * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
- *
- * 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 <stdlib.h>
-
-#include "xc_private.h"
-
-int osdep_gntshr_open(xc_gnttab *xgt)
-{
-    return -1;
-}
-
-int osdep_gntshr_close(xc_gnttab *xgt)
-{
-    return 0;
-}
-
-void *osdep_gntshr_share_pages(xc_gntshr *xgs,
-                               uint32_t domid, int count,
-                               uint32_t *refs, int writable,
-                               uint32_t notify_offset,
-                               evtchn_port_t notify_port)
-{
-    abort()
-}
-
-int xc_gntshr_munmap(xc_gntshr *xgs,
-                     void *start_address, uint32_t count)
-{
-    abort();
-}
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index 7f52a5d..85e6f02 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -249,86 +249,6 @@ int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
     return xch->ops->u.privcmd.hypercall(xch, xch->ops_handle, hypercall);
 }
 
-xc_gnttab *xc_gnttab_open(xentoollog_logger *logger, unsigned open_flags)
-{
-    xc_gnttab *xgt = malloc(sizeof(*xgt));
-    int rc;
-
-    if (!xgt) return NULL;
-
-    xgt->fd = -1;
-    xgt->logger = logger;
-    xgt->logger_tofree  = NULL;
-
-    if (!xgt->logger) {
-        xgt->logger = xgt->logger_tofree =
-            (xentoollog_logger*)
-            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
-        if (!xgt->logger) goto err;
-    }
-
-    rc = osdep_gnttab_open(xgt);
-    if ( rc  < 0 ) goto err;
-
-    return xgt;
-
-err:
-    osdep_gnttab_close(xgt);
-    xtl_logger_destroy(xgt->logger_tofree);
-    free(xgt);
-    return NULL;
-}
-
-int xc_gnttab_close(xc_gnttab *xgt)
-{
-    int rc;
-
-    rc = osdep_gnttab_close(xgt);
-    xtl_logger_destroy(xgt->logger_tofree);
-    free(xgt);
-    return rc;
-}
-
-xc_gntshr *xc_gntshr_open(xentoollog_logger *logger, unsigned open_flags)
-{
-    xc_gntshr *xgs = malloc(sizeof(*xgs));
-    int rc;
-
-    if (!xgs) return NULL;
-
-    xgs->fd = -1;
-    xgs->logger = logger;
-    xgs->logger_tofree  = NULL;
-
-    if (!xgs->logger) {
-        xgs->logger = xgs->logger_tofree =
-            (xentoollog_logger*)
-            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
-        if (!xgs->logger) goto err;
-    }
-
-    rc = osdep_gntshr_open(xgs);
-    if ( rc  < 0 ) goto err;
-
-    return xgs;
-
-err:
-    osdep_gntshr_close(xgs);
-    xtl_logger_destroy(xgs->logger_tofree);
-    free(xgs);
-    return NULL;
-}
-
-int xc_gntshr_close(xc_gntshr *xgs)
-{
-    int rc;
-
-    rc = osdep_gntshr_close(xgs);
-    xtl_logger_destroy(xgs->logger_tofree);
-    free(xgs);
-    return rc;
-}
-
 static pthread_key_t errbuf_pkey;
 static pthread_once_t errbuf_pkey_once = PTHREAD_ONCE_INIT;
 
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index 35c99e0..a32accb 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -123,30 +123,6 @@ struct xc_interface_core {
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
 };
 
-struct xengntdev_handle {
-    xentoollog_logger *logger, *logger_tofree;
-    int fd;
-};
-
-int osdep_gnttab_open(xc_gnttab *xgt);
-int osdep_gnttab_close(xc_gnttab *xgt);
-
-#define XC_GRANT_MAP_SINGLE_DOMAIN 0x1
-void *osdep_gnttab_grant_map(xc_gnttab *xgt,
-                             uint32_t count, int flags, int prot,
-                             uint32_t *domids, uint32_t *refs,
-                             uint32_t notify_offset,
-                             evtchn_port_t notify_port);
-
-int osdep_gntshr_open(xc_gntshr *xgs);
-int osdep_gntshr_close(xc_gntshr *xgs);
-
-void *osdep_gntshr_share_pages(xc_gntshr *xgs,
-                               uint32_t domid, int count,
-                               uint32_t *refs, int writable,
-                               uint32_t notify_offset,
-                               evtchn_port_t notify_port);
-
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
diff --git a/tools/xenstore/Makefile b/tools/xenstore/Makefile
index c161046..178771f 100644
--- a/tools/xenstore/Makefile
+++ b/tools/xenstore/Makefile
@@ -78,8 +78,10 @@ init-xenstore-domain.o: CFLAGS += $(CFLAGS_libxenguest)
 init-xenstore-domain: init-xenstore-domain.o $(LIBXENSTORE)
 	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) -o $@ $(APPEND_LDFLAGS)
 
+$(XENSTORED_OBJS): CFLAGS += $(CFLAGS_libxengnttab)
+
 xenstored: $(XENSTORED_OBJS)
-	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
+	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
 
 xenstored.a: $(XENSTORED_OBJS)
 	$(AR) cr $@ $^
diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h
index 8c853c9..624737d 100644
--- a/tools/xenstore/xenstored_core.h
+++ b/tools/xenstore/xenstored_core.h
@@ -20,12 +20,14 @@
 #define _XENSTORED_CORE_H
 
 #include <xenctrl.h>
+#include <xengnttab.h>
 
 #include <sys/types.h>
 #include <dirent.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <errno.h>
+
 #include "xenstore_lib.h"
 #include "list.h"
 #include "tdb.h"
@@ -196,7 +198,7 @@ void finish_daemonize(void);
 /* Open a pipe for signal handling */
 void init_pipe(int reopen_log_pipe[2]);
 
-xc_gnttab **xcg_handle;
+xengnttab_handle **xgt_handle;
 
 #endif /* _XENSTORED_CORE_H */
 
diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_domain.c
index 6ceec29..414a9fd 100644
--- a/tools/xenstore/xenstored_domain.c
+++ b/tools/xenstore/xenstored_domain.c
@@ -34,7 +34,7 @@
 #include <xen/grant_table.h>
 
 static xc_interface **xc_handle;
-xc_gnttab **xcg_handle;
+xengnttab_handle **xgt_handle;
 static evtchn_port_t virq_port;
 
 xenevtchn_handle *xce_handle = NULL;
@@ -166,9 +166,9 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
 
 static void *map_interface(domid_t domid, unsigned long mfn)
 {
-	if (*xcg_handle != NULL) {
+	if (*xgt_handle != NULL) {
 		/* this is the preferred method */
-		return xc_gnttab_map_grant_ref(*xcg_handle, domid,
+		return xengnttab_map_grant_ref(*xgt_handle, domid,
 			GNTTAB_RESERVED_XENSTORE, PROT_READ|PROT_WRITE);
 	} else {
 		return xc_map_foreign_range(*xc_handle, domid,
@@ -178,8 +178,8 @@ static void *map_interface(domid_t domid, unsigned long mfn)
 
 static void unmap_interface(void *interface)
 {
-	if (*xcg_handle != NULL)
-		xc_gnttab_munmap(*xcg_handle, interface, 1);
+	if (*xgt_handle != NULL)
+		xengnttab_unmap(*xgt_handle, interface, 1);
 	else
 		munmap(interface, XC_PAGE_SIZE);
 }
@@ -577,9 +577,9 @@ static int close_xc_handle(void *_handle)
 	return 0;
 }
 
-static int close_xcg_handle(void *_handle)
+static int close_xgt_handle(void *_handle)
 {
-	xc_gnttab_close(*(xc_gnttab **)_handle);
+	xengnttab_close(*(xengnttab_handle **)_handle);
 	return 0;
 }
 
@@ -634,15 +634,15 @@ void domain_init(void)
 
 	talloc_set_destructor(xc_handle, close_xc_handle);
 
-	xcg_handle = talloc(talloc_autofree_context(), xc_gnttab*);
-	if (!xcg_handle)
+	xgt_handle = talloc(talloc_autofree_context(), xengnttab_handle*);
+	if (!xgt_handle)
 		barf_perror("Failed to allocate domain gnttab handle");
 
-	*xcg_handle = xc_gnttab_open(NULL, 0);
-	if (*xcg_handle == NULL)
+	*xgt_handle = xengnttab_open(NULL, 0);
+	if (*xgt_handle == NULL)
 		xprintf("WARNING: Failed to open connection to gnttab\n");
 	else
-		talloc_set_destructor(xcg_handle, close_xcg_handle);
+		talloc_set_destructor(xgt_handle, close_xgt_handle);
 
 	xce_handle = xenevtchn_open(NULL, 0);
 
diff --git a/tools/xenstore/xenstored_minios.c b/tools/xenstore/xenstored_minios.c
index b686e1c..c94493e 100644
--- a/tools/xenstore/xenstored_minios.c
+++ b/tools/xenstore/xenstored_minios.c
@@ -17,7 +17,6 @@
 */
 #include <sys/types.h>
 #include <sys/mman.h>
-#include <xenctrl.h>
 #include "xenstored_core.h"
 #include <xen/grant_table.h>
 
@@ -50,12 +49,12 @@ evtchn_port_t xenbus_evtchn(void)
 
 void *xenbus_map(void)
 {
-	return xc_gnttab_map_grant_ref(*xcg_handle, xenbus_master_domid(),
+	return xengnttab_map_grant_ref(*xgt_handle, xenbus_master_domid(),
 			GNTTAB_RESERVED_XENSTORE, PROT_READ|PROT_WRITE);
 }
 
 void unmap_xenbus(void *interface)
 {
-	xc_gnttab_munmap(*xcg_handle, interface, 1);
+	xengnttab_unmap(*xgt_handle, interface, 1);
 }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 06/29] tools/libxc: Remove osdep indirection for privcmd
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (4 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 05/29] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 07/29] tools: Refactor hypercall calling wrappers into libxencall Ian Campbell
                     ` (22 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

The alternative backend (a xen-api/xapi shim) is no longer around and
so this stuff is now just baggage which is getting in the way of
refactoring libxenctrl.

Nested virt probably suffices for this use case now.

This was the last component of the osdep infrastructure, so all the
dynamic loading etc stuff all falls away too.

As part of this I was forced to investigate the twisty
xc_map_foreign_* maze, which I have added to the
toolstack-library-apis doc in the hopes of doing something sensible.

NetBSD and Solaris now call xc_map_foreign_bulk_compat directly from
their xc_map_foreign_bulk, which could have been achieved by using
some ifdefs around a renamed function. This will fall out in the wash
when these functions move to their own library.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: David Scott <dave.scott@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
v7: Stop CCing Dave's dead citrix address.
---
 config/FreeBSD.mk                   |   2 -
 config/NetBSD.mk                    |   3 -
 config/NetBSDRump.mk                |   1 -
 config/StdGNU.mk                    |   1 -
 config/SunOS.mk                     |   1 -
 tools/libxc/Makefile                |  27 ++----
 tools/libxc/include/xenctrl.h       |   9 --
 tools/libxc/include/xenctrlosdep.h  | 133 ---------------------------
 tools/libxc/xc_foreign_memory.c     |  31 +------
 tools/libxc/xc_freebsd_osdep.c      | 100 ++++++---------------
 tools/libxc/xc_hcall_buf.c          |   6 +-
 tools/libxc/xc_linux_osdep.c        |  88 ++++++------------
 tools/libxc/xc_minios.c             |  82 +++++------------
 tools/libxc/xc_netbsd.c             |  90 +++++++------------
 tools/libxc/xc_private.c            | 174 ++----------------------------------
 tools/libxc/xc_private.h            |  19 ++--
 tools/libxc/xc_solaris.c            |  90 +++++++------------
 tools/libxc/xenctrl_osdep_ENOSYS.c  | 123 -------------------------
 tools/ocaml/libs/xc/xenctrl.ml      |   2 -
 tools/ocaml/libs/xc/xenctrl.mli     |   1 -
 tools/ocaml/libs/xc/xenctrl_stubs.c |   7 --
 tools/ocaml/xenstored/domains.ml    |   6 +-
 tools/ocaml/xenstored/xenstored.ml  |   5 +-
 23 files changed, 178 insertions(+), 823 deletions(-)
 delete mode 100644 tools/libxc/include/xenctrlosdep.h
 delete mode 100644 tools/libxc/xenctrl_osdep_ENOSYS.c

diff --git a/config/FreeBSD.mk b/config/FreeBSD.mk
index 5a13d607..bb3a5d0 100644
--- a/config/FreeBSD.mk
+++ b/config/FreeBSD.mk
@@ -1,6 +1,4 @@
 include $(XEN_ROOT)/config/StdGNU.mk
 
-DLOPEN_LIBS =
-
 # No wget on FreeBSD base system
 WGET = ftp
diff --git a/config/NetBSD.mk b/config/NetBSD.mk
index 21318d6..cf766e5 100644
--- a/config/NetBSD.mk
+++ b/config/NetBSD.mk
@@ -1,6 +1,3 @@
 include $(XEN_ROOT)/config/StdGNU.mk
 
-# Override settings for this OS
-DLOPEN_LIBS =
-
 WGET = ftp
diff --git a/config/NetBSDRump.mk b/config/NetBSDRump.mk
index 2a87218..74755a1 100644
--- a/config/NetBSDRump.mk
+++ b/config/NetBSDRump.mk
@@ -1,6 +1,5 @@
 include $(XEN_ROOT)/config/StdGNU.mk
 
-DLOPEN_LIBS =
 PTHREAD_LIBS =
 
 WGET = ftp
diff --git a/config/StdGNU.mk b/config/StdGNU.mk
index 129d5c8..39d36b2 100644
--- a/config/StdGNU.mk
+++ b/config/StdGNU.mk
@@ -31,7 +31,6 @@ DEBUG_DIR ?= /usr/lib/debug
 
 SOCKET_LIBS =
 UTIL_LIBS = -lutil
-DLOPEN_LIBS = -ldl
 
 SONAME_LDFLAG = -soname
 SHLIB_LDFLAGS = -shared
diff --git a/config/SunOS.mk b/config/SunOS.mk
index db5e898..86a384d 100644
--- a/config/SunOS.mk
+++ b/config/SunOS.mk
@@ -27,7 +27,6 @@ SunOS_LIBDIR_x86_64 = /usr/sfw/lib/amd64
 SOCKET_LIBS = -lsocket
 PTHREAD_LIBS = -lpthread
 UTIL_LIBS =
-DLOPEN_LIBS = -ldl
 
 SONAME_LDFLAG = -h
 SHLIB_LDFLAGS = -R $(SunOS_LIBDIR) -shared
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index 33d18db..3305fdd 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -101,8 +101,6 @@ GUEST_SRCS-y                 += xc_dom_decompress_unsafe_lzo1x.c
 GUEST_SRCS-y                 += xc_dom_decompress_unsafe_xz.c
 endif
 
-OSDEP_SRCS-y                 += xenctrl_osdep_ENOSYS.c
-
 -include $(XEN_TARGET_ARCH)/Makefile
 
 CFLAGS   += -Werror -Wmissing-prototypes
@@ -121,11 +119,8 @@ CTRL_PIC_OBJS := $(patsubst %.c,%.opic,$(CTRL_SRCS-y))
 GUEST_LIB_OBJS := $(patsubst %.c,%.o,$(GUEST_SRCS-y))
 GUEST_PIC_OBJS := $(patsubst %.c,%.opic,$(GUEST_SRCS-y))
 
-OSDEP_LIB_OBJS := $(patsubst %.c,%.o,$(OSDEP_SRCS-y))
-OSDEP_PIC_OBJS := $(patsubst %.c,%.opic,$(OSDEP_SRCS-y))
-
-$(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) $(OSDEP_LIB_OBJS) \
-$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS) $(OSDEP_PIC_OBJS) : CFLAGS += -include $(XEN_ROOT)/tools/config.h
+$(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) \
+$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h
 
 $(CTRL_LIB_OBJS) $(CTRL_PIC_OBJS): CFLAGS += $(CFLAGS_libxengnttab) $(CFLAGS_libxengntshr)
 
@@ -139,17 +134,13 @@ ifneq ($(nosharedlibs),y)
 LIB += libxenguest.so libxenguest.so.$(MAJOR) libxenguest.so.$(MAJOR).$(MINOR)
 endif
 
-ifneq ($(nosharedlibs),y)
-LIB += xenctrl_osdep_ENOSYS.so
-endif
-
 genpath-target = $(call buildmakevars2header,_paths.h)
 $(eval $(genpath-target))
 
 xc_private.h: _paths.h
 
-$(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) $(OSDEP_LIB_OBJS) \
-$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS) $(OSDEP_PIC_OBJS): xc_private.h
+$(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) \
+$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): xc_private.h
 
 .PHONY: all
 all: build
@@ -169,7 +160,7 @@ install: build
 	$(INSTALL_DATA) libxenctrl.a $(DESTDIR)$(libdir)
 	$(SYMLINK_SHLIB) libxenctrl.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenctrl.so.$(MAJOR)
 	$(SYMLINK_SHLIB) libxenctrl.so.$(MAJOR) $(DESTDIR)$(libdir)/libxenctrl.so
-	$(INSTALL_DATA) include/xenctrl.h include/xenctrl_compat.h include/xenctrlosdep.h $(DESTDIR)$(includedir)
+	$(INSTALL_DATA) include/xenctrl.h include/xenctrl_compat.h $(DESTDIR)$(includedir)
 	$(INSTALL_SHLIB) libxenguest.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
 	$(INSTALL_DATA) libxenguest.a $(DESTDIR)$(libdir)
 	$(SYMLINK_SHLIB) libxenguest.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenguest.so.$(MAJOR)
@@ -185,8 +176,7 @@ clean:
 	rm -rf *.rpm $(LIB) *~ $(DEPS) \
             _paths.h \
             $(CTRL_LIB_OBJS) $(CTRL_PIC_OBJS) \
-            $(GUEST_LIB_OBJS) $(GUEST_PIC_OBJS) \
-            $(OSDEP_LIB_OBJS) $(OSDEP_PIC_OBJS)
+            $(GUEST_LIB_OBJS) $(GUEST_PIC_OBJS)
 
 .PHONY: distclean
 distclean: clean
@@ -212,7 +202,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
 	$(SYMLINK_SHLIB) $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 # libxenguest
 
@@ -237,8 +227,5 @@ libxenguest.so.$(MAJOR).$(MINOR): COMPRESSION_LIBS = $(call zlib-options,l)
 libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
 	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(GUEST_PIC_OBJS) $(COMPRESSION_LIBS) -lz $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
-xenctrl_osdep_ENOSYS.so: $(OSDEP_PIC_OBJS) libxenctrl.so
-	$(CC) $(LDFLAGS) $(SHLIB_LDFLAGS) -o $@ $(OSDEP_PIC_OBJS) $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
-
 -include $(DEPS)
 
diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 16e2628..78400d3 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -181,15 +181,6 @@ enum xc_open_flags {
  */
 int xc_interface_close(xc_interface *xch);
 
-/**
- * Query the active OS interface (i.e. that which would be returned by
- * xc_interface_open) to find out if it is fake (i.e. backends onto
- * something other than an actual Xen hypervisor).
- *
- * @return 0 is "real", >0 if fake, -1 on error.
- */
-int xc_interface_is_fake(void);
-
 /*
  * HYPERCALL SAFE MEMORY BUFFER
  *
diff --git a/tools/libxc/include/xenctrlosdep.h b/tools/libxc/include/xenctrlosdep.h
deleted file mode 100644
index 423660d..0000000
--- a/tools/libxc/include/xenctrlosdep.h
+++ /dev/null
@@ -1,133 +0,0 @@
-/******************************************************************************
- *
- * Interface to OS specific low-level operations
- *
- * Copyright (c) 2010, 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/>.
- */
-
-/*
- * This interface defines the interactions between the Xen control
- * libraries and the OS facilities used to communicate with the
- * hypervisor.
- *
- * It is possible to override the default (native) implementation by
- * setting the XENCTRL_OSDEP environment variable to point to a
- * plugin library. Userspace can use this facility to intercept
- * hypervisor operations. This can be used e.g. to implement a
- * userspace simulator for Xen hypercalls.
- *
- * The plugin must contain a data structure:
- *  xc_osdep_info_t xc_osdep_info;
- *
- * xc_osdep_init:
- *   Must return a suitable struct xc_osdep_ops pointer or NULL on failure.
- */
-
-#ifndef XC_OSDEP_H
-#define XC_OSDEP_H
-
-/* Tell the Xen public headers we are a user-space tools build. */
-#ifndef __XEN_TOOLS__
-#define __XEN_TOOLS__ 1
-#endif
-
-#include <sys/mman.h>
-#include <sys/types.h>
-
-#include <xen/sys/privcmd.h>
-
-enum xc_osdep_type {
-    XC_OSDEP_PRIVCMD,
-};
-
-/* Opaque handle internal to the backend */
-typedef unsigned long xc_osdep_handle;
-
-#define XC_OSDEP_OPEN_ERROR ((xc_osdep_handle)-1)
-
-struct xc_osdep_ops
-{
-    /* Opens an interface.
-     *
-     * Must return an opaque handle on success or
-     * XC_OSDEP_OPEN_ERROR on failure
-     */
-    xc_osdep_handle (*open)(xc_interface *xch);
-
-    int (*close)(xc_interface *xch, xc_osdep_handle h);
-
-    union {
-        struct {
-            void *(*alloc_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, int npages);
-            void (*free_hypercall_buffer)(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages);
-
-            int (*hypercall)(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall);
-
-            void *(*map_foreign_batch)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
-                                       xen_pfn_t *arr, int num);
-            void *(*map_foreign_bulk)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
-                                      const xen_pfn_t *arr, int *err, unsigned int num);
-            void *(*map_foreign_range)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int size, int prot,
-                                       unsigned long mfn);
-            void *(*map_foreign_ranges)(xc_interface *xch, xc_osdep_handle h, uint32_t dom, size_t size, int prot,
-                                        size_t chunksize, privcmd_mmap_entry_t entries[],
-                                        int nentries);
-        } privcmd;
-    } u;
-};
-typedef struct xc_osdep_ops xc_osdep_ops;
-
-typedef xc_osdep_ops *(*xc_osdep_init_fn)(xc_interface *xch, enum xc_osdep_type);
-
-struct xc_osdep_info
-{
-    /* Describes this backend. */
-    const char *name;
-
-    /* Returns ops function. */
-    xc_osdep_init_fn init;
-
-    /* True if this interface backs onto a fake Xen. */
-    int fake;
-
-    /* For internal use by loader. */
-    void *dl_handle;
-};
-typedef struct xc_osdep_info xc_osdep_info_t;
-
-/* All backends, including the builtin backend, must supply this structure. */
-extern xc_osdep_info_t xc_osdep_info;
-
-/* Stub for not yet converted OSes */
-void *xc_map_foreign_bulk_compat(xc_interface *xch, xc_osdep_handle h,
-                                 uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, unsigned int num);
-
-/* Report errors through xc_interface */
-void xc_osdep_log(xc_interface *xch, xentoollog_level level, int code,
-                  const char *fmt, ...) __attribute__((format(printf, 4, 5)));
-
-#endif
-
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- */
diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index f42d140..9c705b6 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -50,37 +50,8 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
     return res;
 }
 
-void *xc_map_foreign_range(xc_interface *xch, uint32_t dom,
-                           int size, int prot, unsigned long mfn)
-{
-    return xch->ops->u.privcmd.map_foreign_range(xch, xch->ops_handle,
-                                                 dom, size, prot, mfn);
-}
-
-void *xc_map_foreign_ranges(xc_interface *xch, uint32_t dom,
-                            size_t size, int prot, size_t chunksize,
-                            privcmd_mmap_entry_t entries[], int nentries)
-{
-    return xch->ops->u.privcmd.map_foreign_ranges(xch, xch->ops_handle,
-                                                  dom, size, prot, chunksize, entries, nentries);
-}
-
-void *xc_map_foreign_batch(xc_interface *xch, uint32_t dom, int prot,
-                           xen_pfn_t *arr, int num)
-{
-    return xch->ops->u.privcmd.map_foreign_batch(xch, xch->ops_handle,
-                                                 dom, prot, arr, num);
-}
-
-void *xc_map_foreign_bulk(xc_interface *xch, uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err, unsigned int num)
-{
-    return xch->ops->u.privcmd.map_foreign_bulk(xch, xch->ops_handle,
-                                                dom, prot, arr, err, num);
-}
-
 /* stub for all not yet converted OSes */
-void *xc_map_foreign_bulk_compat(xc_interface *xch, xc_osdep_handle h,
+void *xc_map_foreign_bulk_compat(xc_interface *xch,
                                  uint32_t dom, int prot,
                                  const xen_pfn_t *arr, int *err, unsigned int num)
 {
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c
index 339997c..f6a2ccd 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libxc/xc_freebsd_osdep.c
@@ -32,16 +32,12 @@
 
 #include <xen/memory.h>
 
-#include "xenctrl.h"
-#include "xenctrlosdep.h"
+#include "xc_private.h"
 
 #define PRIVCMD_DEV     "/dev/xen/privcmd"
 
-#define PERROR(_m, _a...) xc_osdep_log(xch,XTL_ERROR,XC_INTERNAL_ERROR,_m \
-                  " (%d = %s)", ## _a , errno, xc_strerror(xch, errno))
-
 /*------------------------- Privcmd device interface -------------------------*/
-static xc_osdep_handle freebsd_privcmd_open(xc_interface *xch)
+int osdep_privcmd_open(xc_interface *xch)
 {
     int flags, saved_errno;
     int fd = open(PRIVCMD_DEV, O_RDWR);
@@ -50,7 +46,7 @@ static xc_osdep_handle freebsd_privcmd_open(xc_interface *xch)
     {
         PERROR("Could not obtain handle on privileged command interface "
                PRIVCMD_DEV);
-        return XC_OSDEP_OPEN_ERROR;
+        return -1
     }
 
     /*
@@ -73,27 +69,27 @@ static xc_osdep_handle freebsd_privcmd_open(xc_interface *xch)
         goto error;
     }
 
-    return (xc_osdep_handle)fd;
+    xch->privcmdfd = fd;
+    return 0;
 
  error:
     saved_errno = errno;
     close(fd);
     errno = saved_errno;
 
-    return XC_OSDEP_OPEN_ERROR;
+    return -1;
 }
 
-static int freebsd_privcmd_close(xc_interface *xch, xc_osdep_handle h)
+int osdep_privcmd_close(xc_interface *xch)
 {
-    int fd = (int)h;
-
+    int fd = xch->privcmdfd;
+    if ( fd == -1 )
+        return 0;
     return close(fd);
 }
 
 /*------------------------ Privcmd hypercall interface -----------------------*/
-static void *freebsd_privcmd_alloc_hypercall_buffer(xc_interface *xch,
-                                                    xc_osdep_handle h,
-                                                    int npages)
+void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
@@ -117,9 +113,7 @@ static void *freebsd_privcmd_alloc_hypercall_buffer(xc_interface *xch,
     return p;
 }
 
-static void freebsd_privcmd_free_hypercall_buffer(xc_interface *xch,
-                                                  xc_osdep_handle h, void *ptr,
-                                                  int npages)
+void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
 {
 
     int saved_errno = errno;
@@ -131,10 +125,9 @@ static void freebsd_privcmd_free_hypercall_buffer(xc_interface *xch,
     errno = saved_errno;
 }
 
-static int freebsd_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h,
-                                     privcmd_hypercall_t *hypercall)
+int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     int ret;
 
     ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
@@ -143,13 +136,12 @@ static int freebsd_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h,
 }
 
 /*----------------------- Privcmd foreign map interface ----------------------*/
-static void *freebsd_privcmd_map_foreign_bulk(xc_interface *xch,
-                                               xc_osdep_handle h,
-                                               uint32_t dom, int prot,
-                                               const xen_pfn_t *arr, int *err,
-                                               unsigned int num)
+void *xc_map_foreign_bulk(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err,
+                          unsigned int num)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     privcmd_mmapbatch_t ioctlx;
     void *addr;
     int rc;
@@ -180,10 +172,9 @@ static void *freebsd_privcmd_map_foreign_bulk(xc_interface *xch,
     return addr;
 }
 
-static void *freebsd_privcmd_map_foreign_range(xc_interface *xch,
-                                               xc_osdep_handle h,
-                                               uint32_t dom, int size, int prot,
-                                               unsigned long mfn)
+void *xc_map_foreign_range(xc_interface *xch,
+                           uint32_t dom, int size, int prot,
+                           unsigned long mfn)
 {
     xen_pfn_t *arr;
     int num;
@@ -203,12 +194,11 @@ static void *freebsd_privcmd_map_foreign_range(xc_interface *xch,
     return ret;
 }
 
-static void *freebsd_privcmd_map_foreign_ranges(xc_interface *xch,
-                                                xc_osdep_handle h,
-                                                uint32_t dom, size_t size,
-                                                int prot, size_t chunksize,
-                                                privcmd_mmap_entry_t entries[],
-                                                int nentries)
+void *xc_map_foreign_ranges(xc_interface *xch,
+                            uint32_t dom, size_t size,
+                            int prot, size_t chunksize,
+                            privcmd_mmap_entry_t entries[],
+                            int nentries)
 {
     xen_pfn_t *arr;
     int num_per_entry;
@@ -232,42 +222,6 @@ static void *freebsd_privcmd_map_foreign_ranges(xc_interface *xch,
     return ret;
 }
 
-/*----------------------------- Privcmd handlers -----------------------------*/
-static struct xc_osdep_ops freebsd_privcmd_ops = {
-    .open = &freebsd_privcmd_open,
-    .close = &freebsd_privcmd_close,
-
-    .u.privcmd = {
-        .alloc_hypercall_buffer = &freebsd_privcmd_alloc_hypercall_buffer,
-        .free_hypercall_buffer = &freebsd_privcmd_free_hypercall_buffer,
-
-        .hypercall = &freebsd_privcmd_hypercall,
-
-        .map_foreign_bulk = &freebsd_privcmd_map_foreign_bulk,
-        .map_foreign_range = &freebsd_privcmd_map_foreign_range,
-        .map_foreign_ranges = &freebsd_privcmd_map_foreign_ranges,
-    },
-};
-
-/*---------------------------- FreeBSD interface -----------------------------*/
-static struct xc_osdep_ops *
-freebsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
-{
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD:
-        return &freebsd_privcmd_ops;
-    default:
-        return NULL;
-    }
-}
-
-xc_osdep_info_t xc_osdep_info = {
-    .name = "FreeBSD Native OS interface",
-    .init = &freebsd_osdep_init,
-    .fake = 0,
-};
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_hcall_buf.c b/tools/libxc/xc_hcall_buf.c
index 6e3c958..a0e66cf 100644
--- a/tools/libxc/xc_hcall_buf.c
+++ b/tools/libxc/xc_hcall_buf.c
@@ -122,7 +122,7 @@ void xc__hypercall_buffer_cache_release(xc_interface *xch)
     while ( xch->hypercall_buffer_cache_nr > 0 )
     {
         p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
-        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, p, 1);
+        osdep_free_hypercall_buffer(xch, p, 1);
     }
 
     hypercall_buffer_cache_unlock(xch);
@@ -133,7 +133,7 @@ void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t
     void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
 
     if ( !p )
-        p = xch->ops->u.privcmd.alloc_hypercall_buffer(xch, xch->ops_handle, nr_pages);
+        p = osdep_alloc_hypercall_buffer(xch, nr_pages);
 
     if (!p)
         return NULL;
@@ -151,7 +151,7 @@ void xc__hypercall_buffer_free_pages(xc_interface *xch, xc_hypercall_buffer_t *b
         return;
 
     if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
-        xch->ops->u.privcmd.free_hypercall_buffer(xch, xch->ops_handle, b->hbuf, nr_pages);
+        osdep_free_hypercall_buffer(xch, b->hbuf, nr_pages);
 }
 
 struct allocation_header {
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 9c318e0..9f4c707 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -33,13 +33,12 @@
 #include <xen/memory.h>
 
 #include "xenctrl.h"
-#include "xenctrlosdep.h"
 
 #include "xc_private.h"
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
-static xc_osdep_handle linux_privcmd_open(xc_interface *xch)
+int osdep_privcmd_open(xc_interface *xch)
 {
     int flags, saved_errno;
     int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
@@ -53,7 +52,7 @@ static xc_osdep_handle linux_privcmd_open(xc_interface *xch)
     if ( fd == -1 )
     {
         PERROR("Could not obtain handle on privileged command interface");
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
     }
 
     /* Although we return the file handle as the 'xc handle' the API
@@ -74,22 +73,25 @@ static xc_osdep_handle linux_privcmd_open(xc_interface *xch)
         goto error;
     }
 
-    return (xc_osdep_handle)fd;
+    xch->privcmdfd = fd;
+    return 0;
 
  error:
     saved_errno = errno;
     close(fd);
     errno = saved_errno;
-    return XC_OSDEP_OPEN_ERROR;
+    return -1;
 }
 
-static int linux_privcmd_close(xc_interface *xch, xc_osdep_handle h)
+int osdep_privcmd_close(xc_interface *xch)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
+    if (fd == -1)
+        return 0;
     return close(fd);
 }
 
-static void *linux_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, int npages)
+void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
@@ -121,7 +123,7 @@ out:
     return NULL;
 }
 
-static void linux_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages)
+void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
 {
     int saved_errno = errno;
     /* Recover the VMA flags. Maybe it's not necessary */
@@ -132,9 +134,9 @@ static void linux_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_hand
     errno = saved_errno;
 }
 
-static int linux_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall)
+int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     return ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
 }
 
@@ -160,11 +162,11 @@ static int xc_map_foreign_batch_single(int fd, uint32_t dom,
     return rc;
 }
 
-static void *linux_privcmd_map_foreign_batch(xc_interface *xch, xc_osdep_handle h,
-                                             uint32_t dom, int prot,
-                                             xen_pfn_t *arr, int num)
+void *xc_map_foreign_batch(xc_interface *xch,
+                           uint32_t dom, int prot,
+                           xen_pfn_t *arr, int num)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     privcmd_mmapbatch_t ioctlx;
     void *addr;
     int rc;
@@ -266,11 +268,11 @@ out:
     return rc;
 }
 
-static void *linux_privcmd_map_foreign_bulk(xc_interface *xch, xc_osdep_handle h,
-                                            uint32_t dom, int prot,
-                                            const xen_pfn_t *arr, int *err, unsigned int num)
+void *xc_map_foreign_bulk(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err, unsigned int num)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     privcmd_mmapbatch_v2_t ioctlx;
     void *addr;
     unsigned int i;
@@ -390,9 +392,9 @@ static void *linux_privcmd_map_foreign_bulk(xc_interface *xch, xc_osdep_handle h
     return addr;
 }
 
-static void *linux_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle h,
-                                             uint32_t dom, int size, int prot,
-                                             unsigned long mfn)
+void *xc_map_foreign_range(xc_interface *xch,
+                           uint32_t dom, int size, int prot,
+                           unsigned long mfn)
 {
     xen_pfn_t *arr;
     int num;
@@ -412,10 +414,10 @@ static void *linux_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle
     return ret;
 }
 
-static void *linux_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handle h,
-                                              uint32_t dom, size_t size, int prot,
-                                              size_t chunksize, privcmd_mmap_entry_t entries[],
-                                              int nentries)
+void *xc_map_foreign_ranges(xc_interface *xch,
+                            uint32_t dom, size_t size, int prot,
+                            size_t chunksize, privcmd_mmap_entry_t entries[],
+                            int nentries)
 {
     xen_pfn_t *arr;
     int num_per_entry;
@@ -439,40 +441,6 @@ static void *linux_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handle
     return ret;
 }
 
-static struct xc_osdep_ops linux_privcmd_ops = {
-    .open = &linux_privcmd_open,
-    .close = &linux_privcmd_close,
-
-    .u.privcmd = {
-        .alloc_hypercall_buffer = &linux_privcmd_alloc_hypercall_buffer,
-        .free_hypercall_buffer = &linux_privcmd_free_hypercall_buffer,
-
-        .hypercall = &linux_privcmd_hypercall,
-
-        .map_foreign_batch = &linux_privcmd_map_foreign_batch,
-        .map_foreign_bulk = &linux_privcmd_map_foreign_bulk,
-        .map_foreign_range = &linux_privcmd_map_foreign_range,
-        .map_foreign_ranges = &linux_privcmd_map_foreign_ranges,
-    },
-};
-
-static struct xc_osdep_ops *linux_osdep_init(xc_interface *xch, enum xc_osdep_type type)
-{
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD:
-        return &linux_privcmd_ops;
-    default:
-        return NULL;
-    }
-}
-
-xc_osdep_info_t xc_osdep_info = {
-    .name = "Linux Native OS interface",
-    .init = &linux_osdep_init,
-    .fake = 0,
-};
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index 22d985c..047e13b 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -39,19 +39,20 @@ void minios_interface_close_fd(int fd);
 
 extern void minios_interface_close_fd(int fd);
 
-static xc_osdep_handle minios_privcmd_open(xc_interface *xch)
+int osdep_privcmd_open(xc_interface *xch)
 {
     int fd = alloc_fd(FTYPE_XC);
 
     if ( fd == -1)
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
 
-    return (xc_osdep_handle)fd;
+    xch->privcmdfd = fd;
+    return 0;
 }
 
-static int minios_privcmd_close(xc_interface *xch, xc_osdep_handle h)
+int osdep_privcmd_close(xc_interface *xch)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     return close(fd);
 }
 
@@ -60,17 +61,17 @@ void minios_interface_close_fd(int fd)
     files[fd].type = FTYPE_NONE;
 }
 
-static void *minios_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, int npages)
+void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
 {
     return xc_memalign(xch, PAGE_SIZE, npages * PAGE_SIZE);
 }
 
-static void minios_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages)
+void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
 {
     free(ptr);
 }
 
-static int minios_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall)
+int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
 {
     multicall_entry_t call;
     int i, ret;
@@ -92,21 +93,21 @@ static int minios_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcm
     return call.result;
 }
 
-static void *minios_privcmd_map_foreign_bulk(xc_interface *xch, xc_osdep_handle h,
-                                             uint32_t dom, int prot,
-                                             const xen_pfn_t *arr, int *err, unsigned int num)
+void *xc_map_foreign_bulk(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err, unsigned int num)
 {
     unsigned long pt_prot = 0;
     if (prot & PROT_READ)
 	pt_prot = L1_PROT_RO;
     if (prot & PROT_WRITE)
 	pt_prot = L1_PROT;
-    return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);    
+    return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
 }
 
-static void *minios_privcmd_map_foreign_batch(xc_interface *xch,  xc_osdep_handle h,
-                                              uint32_t dom, int prot,
-                                              xen_pfn_t *arr, int num)
+void *xc_map_foreign_batch(xc_interface *xch,
+                           uint32_t dom, int prot,
+                           xen_pfn_t *arr, int num)
 {
     unsigned long pt_prot = 0;
     int err[num];
@@ -126,10 +127,10 @@ static void *minios_privcmd_map_foreign_batch(xc_interface *xch,  xc_osdep_handl
     return (void *) addr;
 }
 
-static void *minios_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle h,
-                                              uint32_t dom,
-                                              int size, int prot,
-                                              unsigned long mfn)
+void *xc_map_foreign_range(xc_interface *xch,
+                           uint32_t dom,
+                           int size, int prot,
+                           unsigned long mfn)
 {
     unsigned long pt_prot = 0;
 
@@ -142,10 +143,10 @@ static void *minios_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle
     return map_frames_ex(&mfn, size / getpagesize(), 0, 1, 1, dom, NULL, pt_prot);
 }
 
-static void *minios_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handle h,
-                                               uint32_t dom,
-                                               size_t size, int prot, size_t chunksize,
-                                               privcmd_mmap_entry_t entries[], int nentries)
+void *xc_map_foreign_ranges(xc_interface *xch,
+                            uint32_t dom,
+                            size_t size, int prot, size_t chunksize,
+                            privcmd_mmap_entry_t entries[], int nentries)
 {
     unsigned long *mfns;
     int i, j, n;
@@ -169,24 +170,6 @@ static void *minios_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handl
     return ret;
 }
 
-
-static struct xc_osdep_ops minios_privcmd_ops = {
-    .open = &minios_privcmd_open,
-    .close = &minios_privcmd_close,
-
-    .u.privcmd = {
-        .alloc_hypercall_buffer = &minios_privcmd_alloc_hypercall_buffer,
-        .free_hypercall_buffer = &minios_privcmd_free_hypercall_buffer,
-
-        .hypercall = &minios_privcmd_hypercall,
-
-        .map_foreign_batch = &minios_privcmd_map_foreign_batch,
-        .map_foreign_bulk = &minios_privcmd_map_foreign_bulk,
-        .map_foreign_range = &minios_privcmd_map_foreign_range,
-        .map_foreign_ranges = &minios_privcmd_map_foreign_ranges,
-    },
-};
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush)
 {
@@ -199,23 +182,6 @@ void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
     return memalign(alignment, size);
 }
 
-static struct xc_osdep_ops *minios_osdep_init(xc_interface *xch, enum xc_osdep_type type)
-{
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD:
-        return &minios_privcmd_ops;
-    default:
-        return NULL;
-    }
-}
-
-xc_osdep_info_t xc_osdep_info = {
-    .name = "Minios Native OS interface",
-    .init = &minios_osdep_init,
-    .fake = 0,
-};
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index fe4f0a1..9abb3b6 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -24,7 +24,7 @@
 #include <malloc.h>
 #include <sys/mman.h>
 
-static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
+int osdep_privcmd_open(xc_interface *xch)
 {
     int flags, saved_errno;
     int fd = open("/kern/xen/privcmd", O_RDWR);
@@ -32,7 +32,7 @@ static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
     if ( fd == -1 )
     {
         PERROR("Could not obtain handle on privileged command interface");
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
     }
 
     /* Although we return the file handle as the 'xc handle' the API
@@ -51,22 +51,23 @@ static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
         goto error;
     }
 
-    return (xc_osdep_handle)fd;
+    xch->privcmdfd = fd;
+    return 0;
 
  error:
     saved_errno = errno;
     close(fd);
     errno = saved_errno;
-    return XC_OSDEP_OPEN_ERROR;
+    return -1;
 }
 
-static int netbsd_privcmd_close(xc_interface *xch, xc_osdep_handle h)
+int osdep_privcmd_close(xc_interface *xch)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     return close(fd);
 }
 
-static void *netbsd_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, int npages)
+void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
 {
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
@@ -83,15 +84,15 @@ static void *netbsd_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_h
     return p;
 }
 
-static void netbsd_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages)
+void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
 {
     (void) munlock(ptr, npages * XC_PAGE_SIZE);
     free(ptr);
 }
 
-static int netbsd_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall)
+int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     int error = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
 
     /*
@@ -106,11 +107,18 @@ static int netbsd_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcm
         return hypercall->retval;
 }
 
-static void *netbsd_privcmd_map_foreign_batch(xc_interface *xch, xc_osdep_handle h,
-                                              uint32_t dom, int prot,
-                                              xen_pfn_t *arr, int num)
+void *xc_map_foreign_bulk(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err, unsigned int num)
 {
-    int fd = (int)h;
+    return xc_map_foreign_bulk_compat(xch, dom, prot, arr, err, num);
+}
+
+void *xc_map_foreign_batch(xc_interface *xch,
+                           uint32_t dom, int prot,
+                           xen_pfn_t *arr, int num)
+{
+    int fd = xch->privcmdfd;
     privcmd_mmapbatch_t ioctlx;
     void *addr;
     addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_ANON | MAP_SHARED, -1, 0);
@@ -135,12 +143,12 @@ static void *netbsd_privcmd_map_foreign_batch(xc_interface *xch, xc_osdep_handle
 
 }
 
-static void *netbsd_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle h,
-                                              uint32_t dom,
-                                              int size, int prot,
-                                              unsigned long mfn)
+void *xc_map_foreign_range(xc_interface *xch,
+                           uint32_t dom,
+                           int size, int prot,
+                           unsigned long mfn)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     privcmd_mmap_t ioctlx;
     privcmd_mmap_entry_t entry;
     void *addr;
@@ -167,12 +175,12 @@ static void *netbsd_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle
     return addr;
 }
 
-static void *netbsd_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handle h,
-                                               uint32_t dom,
-                                               size_t size, int prot, size_t chunksize,
-                                               privcmd_mmap_entry_t entries[], int nentries)
+void *xc_map_foreign_ranges(xc_interface *xch,
+                            uint32_t dom,
+                            size_t size, int prot, size_t chunksize,
+                            privcmd_mmap_entry_t entries[], int nentries)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
 	privcmd_mmap_t ioctlx;
 	int i, rc;
 	void *addr;
@@ -205,23 +213,6 @@ mmap_failed:
 	return NULL;
 }
 
-static struct xc_osdep_ops netbsd_privcmd_ops = {
-    .open = &netbsd_privcmd_open,
-    .close = &netbsd_privcmd_close,
-
-    .u.privcmd = {
-        .alloc_hypercall_buffer = &netbsd_privcmd_alloc_hypercall_buffer,
-        .free_hypercall_buffer = &netbsd_privcmd_free_hypercall_buffer,
-
-        .hypercall = &netbsd_privcmd_hypercall,
-
-        .map_foreign_batch = &netbsd_privcmd_map_foreign_batch,
-        .map_foreign_bulk = &xc_map_foreign_bulk_compat,
-        .map_foreign_range = &netbsd_privcmd_map_foreign_range,
-        .map_foreign_ranges = &netbsd_privcmd_map_foreign_ranges,
-    },
-};
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
@@ -262,23 +253,6 @@ void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
     return valloc(size);
 }
 
-static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
-{
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD:
-        return &netbsd_privcmd_ops;
-    default:
-        return NULL;
-    }
-}
-
-xc_osdep_info_t xc_osdep_info = {
-    .name = "Netbsd Native OS interface",
-    .init = &netbsd_osdep_init,
-    .fake = 0,
-};
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index 85e6f02..de5873e 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -26,111 +26,12 @@
 #include <pthread.h>
 #include <assert.h>
 
-#ifndef __MINIOS__
-#include <dlfcn.h>
-#endif
-
-#define XENCTRL_OSDEP "XENCTRL_OSDEP"
-
-#if !defined (__MINIOS__) && !defined(__RUMPUSER_XEN__) && !defined(__RUMPRUN__)
-#define DO_DYNAMIC_OSDEP
-#endif
-
-/*
- * Returns a (shallow) copy of the xc_osdep_info_t for the
- * active OS interface.
- *
- * On success a handle to the relevant library is opened.  The user
- * must subsequently call xc_osdep_put_info() when it is
- * finished with the library.
- *
- * Logs IFF xch != NULL.
- *
- * Returns:
- *  0 - on success
- * -1 - on error
- */
-static int xc_osdep_get_info(xc_interface *xch, xc_osdep_info_t *info)
-{
-    int rc = -1;
-#ifdef DO_DYNAMIC_OSDEP
-    const char *lib = getenv(XENCTRL_OSDEP);
-    xc_osdep_info_t *pinfo;
-    void *dl_handle = NULL;
-
-    if ( lib != NULL )
-    {
-        if ( getuid() != geteuid() )
-        {
-            if ( xch ) ERROR("cannot use %s=%s with setuid application", XENCTRL_OSDEP, lib);
-            abort();
-        }
-        if ( getgid() != getegid() )
-        {
-            if ( xch ) ERROR("cannot use %s=%s with setgid application", XENCTRL_OSDEP, lib);
-            abort();
-        }
-
-        dl_handle = dlopen(lib, RTLD_LAZY|RTLD_LOCAL);
-        if ( !dl_handle )
-        {
-            if ( xch ) ERROR("unable to open osdep library %s: %s", lib, dlerror());
-            goto out;
-        }
-
-        pinfo = dlsym(dl_handle, "xc_osdep_info");
-        if ( !pinfo )
-        {
-            if ( xch ) ERROR("unable to find xc_osinteface_info in %s: %s", lib, dlerror());
-            goto out;
-        }
-
-        *info = *pinfo;
-        info->dl_handle = dl_handle;
-    }
-    else
-#endif /*DO_DYNAMIC_OSDEP*/
-    {
-        *info = xc_osdep_info;
-        info->dl_handle = NULL;
-    }
-
-    rc = 0;
-
-#ifdef DO_DYNAMIC_OSDEP
-out:
-    if ( dl_handle && rc == -1 )
-        dlclose(dl_handle);
-#endif /*DO_DYNAMIC_OSDEP*/
-
-    return rc;
-}
-
-static void xc_osdep_put(xc_osdep_info_t *info)
-{
-#ifdef DO_DYNAMIC_OSDEP
-    if ( info->dl_handle )
-        dlclose(info->dl_handle);
-#endif /*DO_DYNAMIC_OSDEP*/
-}
-
-static const char *xc_osdep_type_name(enum xc_osdep_type type)
-{
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD: return "privcmd";
-    }
-    return "unknown";
-}
-
-static struct xc_interface_core *xc_interface_open_common(xentoollog_logger *logger,
-                                                          xentoollog_logger *dombuild_logger,
-                                                          unsigned open_flags,
-                                                          enum xc_osdep_type type)
+struct xc_interface_core *xc_interface_open(xentoollog_logger *logger,
+                                            xentoollog_logger *dombuild_logger,
+                                            unsigned open_flags)
 {
     struct xc_interface_core xch_buf, *xch = &xch_buf;
 
-    xch->type = type;
     xch->flags = open_flags;
     xch->dombuild_logger_file = 0;
     xc_clear_last_error(xch);
@@ -148,9 +49,6 @@ static struct xc_interface_core *xc_interface_open_common(xentoollog_logger *log
     xch->hypercall_buffer_cache_misses = 0;
     xch->hypercall_buffer_cache_toobig = 0;
 
-    xch->ops_handle = XC_OSDEP_OPEN_ERROR;
-    xch->ops = NULL;
-
     if (!xch->error_handler) {
         xch->error_handler = xch->error_handler_tofree =
             (xentoollog_logger*)
@@ -168,40 +66,26 @@ static struct xc_interface_core *xc_interface_open_common(xentoollog_logger *log
     *xch = xch_buf;
 
     if (!(open_flags & XC_OPENFLAG_DUMMY)) {
-        if ( xc_osdep_get_info(xch, &xch->osdep) < 0 )
+        if ( osdep_privcmd_open(xch) < 0 )
             goto err;
-
-        xch->ops = xch->osdep.init(xch, type);
-        if ( xch->ops == NULL )
-        {
-            DPRINTF("OSDEP: interface %d (%s) not supported on this platform",
-                  type, xc_osdep_type_name(type));
-            goto err_put_iface;
-        }
-
-        xch->ops_handle = xch->ops->open(xch);
-        if (xch->ops_handle == XC_OSDEP_OPEN_ERROR)
-            goto err_put_iface;
     }
 
     return xch;
 
-err_put_iface:
-    xc_osdep_put(&xch->osdep);
  err:
     xtl_logger_destroy(xch->error_handler_tofree);
     if (xch != &xch_buf) free(xch);
     return NULL;
 }
 
-static int xc_interface_close_common(xc_interface *xch)
+int xc_interface_close(xc_interface *xch)
 {
     int rc = 0;
 
     if (!xch)
-	return 0;
+        return 0;
 
-    rc = xch->ops->close(xch, xch->ops_handle);
+    rc = osdep_privcmd_close(xch);
     if (rc) PERROR("Could not close hypervisor interface");
 
     xc__hypercall_buffer_cache_release(xch);
@@ -213,42 +97,6 @@ static int xc_interface_close_common(xc_interface *xch)
     return rc;
 }
 
-int xc_interface_is_fake(void)
-{
-    xc_osdep_info_t info;
-
-    if ( xc_osdep_get_info(NULL, &info) < 0 )
-        return -1;
-
-    /* Have a copy of info so can release the interface now. */
-    xc_osdep_put(&info);
-
-    return info.fake;
-}
-
-xc_interface *xc_interface_open(xentoollog_logger *logger,
-                                xentoollog_logger *dombuild_logger,
-                                unsigned open_flags)
-{
-    xc_interface *xch;
-
-    xch = xc_interface_open_common(logger, dombuild_logger, open_flags,
-                                   XC_OSDEP_PRIVCMD);
-
-    return xch;
-}
-
-int xc_interface_close(xc_interface *xch)
-{
-    return xc_interface_close_common(xch);
-}
-
-
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
-{
-    return xch->ops->u.privcmd.hypercall(xch, xch->ops_handle, hypercall);
-}
-
 static pthread_key_t errbuf_pkey;
 static pthread_once_t errbuf_pkey_once = PTHREAD_ONCE_INIT;
 
@@ -335,14 +183,6 @@ void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     va_end(args);
 }
 
-void xc_osdep_log(xc_interface *xch, xentoollog_level level, int code, const char *fmt, ...)
-{
-    va_list args;
-    va_start(args, fmt);
-    xc_reportv(xch, xch->error_handler, level, code, fmt, args);
-    va_end(args);
-}
-
 const char *xc_set_progress_prefix(xc_interface *xch, const char *doing)
 {
     const char *old = xch->currently_progress_reporting;
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index a32accb..30157cf 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -30,7 +30,6 @@
 
 #include "_paths.h"
 #include "xenctrl.h"
-#include "xenctrlosdep.h"
 
 #include <xen/sys/privcmd.h>
 
@@ -88,7 +87,6 @@ struct iovec {
 #define MAX_PAGECACHE_USAGE (4*1024)
 
 struct xc_interface_core {
-    enum xc_osdep_type type;
     int flags;
     xentoollog_logger *error_handler,   *error_handler_tofree;
     xentoollog_logger *dombuild_logger, *dombuild_logger_tofree;
@@ -117,12 +115,21 @@ struct xc_interface_core {
     int hypercall_buffer_cache_misses;
     int hypercall_buffer_cache_toobig;
 
-    /* Low lovel OS interface */
-    xc_osdep_info_t  osdep;
-    xc_osdep_ops    *ops; /* backend operations */
-    xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
+    /* Privcmd interface */
+    int privcmdfd;
 };
 
+int osdep_privcmd_open(xc_interface *xch);
+int osdep_privcmd_close(xc_interface *xch);
+
+void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages);
+void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages);
+
+/* Stub for not yet converted OSes */
+void *xc_map_foreign_bulk_compat(xc_interface *xch,
+                                 uint32_t dom, int prot,
+                                 const xen_pfn_t *arr, int *err, unsigned int num);
+
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index ed7987c..6f84b82 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -24,7 +24,7 @@
 #include <fcntl.h>
 #include <malloc.h>
 
-static xc_osdep_handle solaris_privcmd_open(xc_interface *xch)
+int osdep_privcmd_open(xc_interface *xch)
 {
     int flags, saved_errno;
     int fd = open("/dev/xen/privcmd", O_RDWR);
@@ -32,7 +32,7 @@ static xc_osdep_handle solaris_privcmd_open(xc_interface *xch)
     if ( fd == -1 )
     {
         PERROR("Could not obtain handle on privileged command interface");
-        return XC_OSDEP_OPEN_ERROR;
+        return -1;
     }
 
     /* Although we return the file handle as the 'xc handle' the API
@@ -51,42 +51,43 @@ static xc_osdep_handle solaris_privcmd_open(xc_interface *xch)
         goto error;
     }
 
-    return (xc_osdep_handle)fd;
+    xch->privcmdfd = fd;
+    return 0;
 
  error:
     saved_errno = errno;
     close(fd);
     errno = saved_errno;
-    return XC_OSDEP_OPEN_ERROR;
+    return -1;
 }
 
-static int solaris_privcmd_close(xc_interface *xch, xc_osdep_handle h)
+int osdep_privcmd_close(xc_interface *xch)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     return close(fd);
 }
 
-static void *solaris_privcmd_alloc_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, int npages)
+void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
 {
     return xc_memalign(xch, XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
 }
 
-static void solaris_privcmd_free_hypercall_buffer(xc_interface *xch, xc_osdep_handle h, void *ptr, int npages)
+static void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
 {
     free(ptr);
 }
 
-static int solaris_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall)
+int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     return ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
 }
 
-static void *solaris_privcmd_map_foreign_batch(xc_interface *xch, xc_osdep_handle h,
-                                               uint32_t dom, int prot,
-                                               xen_pfn_t *arr, int num)
+void *xc_map_foreign_batch(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          xen_pfn_t *arr, int num)
 {
-    int fd = (int)h;
+    int fd = xch->privcmdfd;
     privcmd_mmapbatch_t ioctlx;
     void *addr;
     addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_SHARED, fd, 0);
@@ -109,12 +110,19 @@ static void *solaris_privcmd_map_foreign_batch(xc_interface *xch, xc_osdep_handl
 
 }
 
-static void *xc_map_foreign_range(xc_interface *xch, xc_osdep_handle h,
-                                  uint32_t dom,
-                                  int size, int prot,
-                                  unsigned long mfn)
+void *xc_map_foreign_bulk(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err, unsigned int num)
 {
-    int fd = (int)fd;
+    return xc_map_foreign_bulk_compat(xch, dom, prot, arr, err, num);
+}
+
+void *xc_map_foreign_range(xc_interface *xch,
+                           uint32_t dom,
+                           int size, int prot,
+                           unsigned long mfn)
+{
+    int fd = xch->privcmdfd;
     privcmd_mmap_t ioctlx;
     privcmd_mmap_entry_t entry;
     void *addr;
@@ -138,12 +146,12 @@ static void *xc_map_foreign_range(xc_interface *xch, xc_osdep_handle h,
     return addr;
 }
 
-static void *solaric_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handle h,
-                                                uint32_t dom,
-                                                size_t size, int prot, size_t chunksize,
-                                                privcmd_mmap_entry_t entries[], int nentries)
+void *xc_map_foreign_ranges(xc_interface *xch,
+                            uint32_t dom,
+                            size_t size, int prot, size_t chunksize,
+                            privcmd_mmap_entry_t entries[], int nentries)
 {
-    int fd = (int)fd;
+    int fd = xch->privcmdfd;
     privcmd_mmap_t ioctlx;
     int i, rc;
     void *addr;
@@ -176,23 +184,6 @@ mmap_failed:
     return NULL;
 }
 
-static struct xc_osdep_ops solaris_privcmd_ops = {
-    .open = &solaris_privcmd_open,
-    .close = &solaris_privcmd_close,
-
-    .u.privcmd = {
-        .alloc_hypercall_buffer = &solaris_privcmd_alloc_hypercall_buffer,
-        .free_hypercall_buffer = &solaris_privcmd_free_hypercall_buffer,
-
-        .hypercall = &solaris_privcmd_hypercall;
-
-        .map_foreign_batch = &solaris_privcmd_map_foreign_batch,
-        .map_foreign_bulk = &xc_map_foreign_bulk_compat,
-        .map_foreign_range = &solaris_privcmd_map_foreign_range,
-        .map_foreign_ranges = &solaris_privcmd_map_foreign_ranges,
-    },
-};
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
@@ -204,23 +195,6 @@ void *xc_memalign(xc_interface *xch, size_t alignment, size_t size)
     return memalign(alignment, size);
 }
 
-static struct xc_osdep_ops *solaris_osdep_init(xc_interface *xch, enum xc_osdep_type type)
-{
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD:
-        return &solaris_privcmd_ops;
-    default:
-        return NULL;
-    }
-}
-
-xc_osdep_info_t xc_osdep_info = {
-    .name = "Solaris Native OS interface",
-    .init = &solaris_osdep_init,
-    .fake = 0,
-};
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xenctrl_osdep_ENOSYS.c b/tools/libxc/xenctrl_osdep_ENOSYS.c
deleted file mode 100644
index 5182532..0000000
--- a/tools/libxc/xenctrl_osdep_ENOSYS.c
+++ /dev/null
@@ -1,123 +0,0 @@
-/* Dummy backend which just logs and returns ENOSYS. */
-
-#include <errno.h>
-#include <inttypes.h>
-#include <stdlib.h>
-
-#include "xenctrl.h"
-#include "xenctrlosdep.h"
-
-#define IPRINTF(_x, _f, _a...) xc_osdep_log(_x,XTL_INFO,0, _f , ## _a)
-
-#define ERROR(_x, _m, _a...)  xc_osdep_log(_x,XTL_ERROR,XC_INTERNAL_ERROR,_m , ## _a )
-#define PERROR(_x, _m, _a...) xc_osdep_log(_x,XTL_ERROR,XC_INTERNAL_ERROR,_m \
-                  " (%d = %s)", ## _a , errno, xc_strerror(xch, errno))
-
-static xc_osdep_handle ENOSYS_privcmd_open(xc_interface *xch)
-{
-    IPRINTF(xch, "ENOSYS_privcmd: opening handle %d\n", 1);
-    return (xc_osdep_handle)1; /*dummy*/
-}
-
-static int ENOSYS_privcmd_close(xc_interface *xch, xc_osdep_handle h)
-{
-    IPRINTF(xch, "ENOSYS_privcmd: closing handle %lx\n", h);
-    return 0;
-}
-
-static int ENOSYS_privcmd_hypercall(xc_interface *xch, xc_osdep_handle h, privcmd_hypercall_t *hypercall)
-{
-#if defined(__FreeBSD__) || defined(__NetBSD__)
-    IPRINTF(xch, "ENOSYS_privcmd %lx: hypercall: %02lu(%#lx,%#lx,%#lx,%#lx,%#lx)\n",
-#else
-    IPRINTF(xch, "ENOSYS_privcmd %lx: hypercall: %02lld(%#llx,%#llx,%#llx,%#llx,%#llx)\n",
-#endif
-            h, hypercall->op,
-            hypercall->arg[0], hypercall->arg[1], hypercall->arg[2],
-            hypercall->arg[3], hypercall->arg[4]);
-    return -ENOSYS;
-}
-
-static void *ENOSYS_privcmd_map_foreign_batch(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
-                                      xen_pfn_t *arr, int num)
-{
-    IPRINTF(xch, "ENOSYS_privcmd %lx: map_foreign_batch: dom%d prot %#x arr %p num %d\n", h, dom, prot, arr, num);
-    return MAP_FAILED;
-}
-
-static void *ENOSYS_privcmd_map_foreign_bulk(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int prot,
-                                     const xen_pfn_t *arr, int *err, unsigned int num)
-{
-    IPRINTF(xch, "ENOSYS_privcmd %lx map_foreign_buld: dom%d prot %#x arr %p err %p num %d\n", h, dom, prot, arr, err, num);
-    return MAP_FAILED;
-}
-
-static void *ENOSYS_privcmd_map_foreign_range(xc_interface *xch, xc_osdep_handle h, uint32_t dom, int size, int prot,
-                                      unsigned long mfn)
-{
-    IPRINTF(xch, "ENOSYS_privcmd %lx map_foreign_range: dom%d size %#x prot %#x mfn %ld\n", h, dom, size, prot, mfn);
-    return MAP_FAILED;
-}
-
-static void *ENOSYS_privcmd_map_foreign_ranges(xc_interface *xch, xc_osdep_handle h, uint32_t dom, size_t size, int prot,
-                                       size_t chunksize, privcmd_mmap_entry_t entries[],
-                                       int nentries)
-{
-    IPRINTF(xch, "ENOSYS_privcmd %lx map_foreign_ranges: dom%d size %zd prot %#x chunksize %zd entries %p num %d\n", h, dom, size, prot, chunksize, entries, nentries);
-    return MAP_FAILED;
-}
-
-static struct xc_osdep_ops ENOSYS_privcmd_ops =
-{
-    .open      = &ENOSYS_privcmd_open,
-    .close     = &ENOSYS_privcmd_close,
-    .u.privcmd   = {
-        .hypercall = &ENOSYS_privcmd_hypercall,
-
-        .map_foreign_batch = &ENOSYS_privcmd_map_foreign_batch,
-        .map_foreign_bulk = &ENOSYS_privcmd_map_foreign_bulk,
-        .map_foreign_range = &ENOSYS_privcmd_map_foreign_range,
-        .map_foreign_ranges = &ENOSYS_privcmd_map_foreign_ranges,
-    }
-};
-
-static struct xc_osdep_ops * ENOSYS_osdep_init(xc_interface *xch, enum xc_osdep_type type)
-{
-    struct xc_osdep_ops *ops;
-
-    if (getenv("ENOSYS") == NULL)
-    {
-        PERROR(xch, "ENOSYS: not configured\n");
-        return NULL;
-    }
-
-    switch ( type )
-    {
-    case XC_OSDEP_PRIVCMD:
-        ops = &ENOSYS_privcmd_ops;
-        break;
-    default:
-        ops = NULL;
-        break;
-    }
-
-    IPRINTF(xch, "ENOSYS_osdep_init: initialising handle ops at %p\n", ops);
-
-    return ops;
-}
-
-xc_osdep_info_t xc_osdep_info = {
-    .name = "Pessimistic ENOSYS OS interface",
-    .init = &ENOSYS_osdep_init,
-    .fake = 1,
-};
-
-/*
- * Local variables:
- * mode: C
- * c-file-style: "BSD"
- * c-basic-offset: 4
- * tab-width: 4
- * indent-tabs-mode: nil
- * End:
- */
diff --git a/tools/ocaml/libs/xc/xenctrl.ml b/tools/ocaml/libs/xc/xenctrl.ml
index 41d228d..58a53a1 100644
--- a/tools/ocaml/libs/xc/xenctrl.ml
+++ b/tools/ocaml/libs/xc/xenctrl.ml
@@ -108,8 +108,6 @@ external sizeof_xen_pfn: unit -> int = "stub_sizeof_xen_pfn"
 external interface_open: unit -> handle = "stub_xc_interface_open"
 external interface_close: handle -> unit = "stub_xc_interface_close"
 
-external is_fake: unit -> bool = "stub_xc_interface_is_fake"
-
 let with_intf f =
 	let xc = interface_open () in
 	let r = try f xc with exn -> interface_close xc; raise exn in
diff --git a/tools/ocaml/libs/xc/xenctrl.mli b/tools/ocaml/libs/xc/xenctrl.mli
index b4a175b..16443df 100644
--- a/tools/ocaml/libs/xc/xenctrl.mli
+++ b/tools/ocaml/libs/xc/xenctrl.mli
@@ -72,7 +72,6 @@ external sizeof_vcpu_guest_context : unit -> int
   = "stub_sizeof_vcpu_guest_context"
 external sizeof_xen_pfn : unit -> int = "stub_sizeof_xen_pfn"
 external interface_open : unit -> handle = "stub_xc_interface_open"
-external is_fake : unit -> bool = "stub_xc_interface_is_fake"
 external interface_close : handle -> unit = "stub_xc_interface_close"
 val with_intf : (handle -> 'a) -> 'a
 val domain_create : handle -> int32 -> domain_create_flag list -> string -> domid
diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c
index 393156c..e2fa7e4 100644
--- a/tools/ocaml/libs/xc/xenctrl_stubs.c
+++ b/tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -126,13 +126,6 @@ CAMLprim value stub_xc_interface_open(void)
 }
 
 
-CAMLprim value stub_xc_interface_is_fake(void)
-{
-	CAMLparam0();
-	int is_fake = xc_interface_is_fake();
-	CAMLreturn(Val_int(is_fake));
-}
-
 CAMLprim value stub_xc_interface_close(value xch)
 {
 	CAMLparam1(xch);
diff --git a/tools/ocaml/xenstored/domains.ml b/tools/ocaml/xenstored/domains.ml
index 92e438f..395f3a9 100644
--- a/tools/ocaml/xenstored/domains.ml
+++ b/tools/ocaml/xenstored/domains.ml
@@ -65,11 +65,9 @@ let create xc doms domid mfn port =
 	Domain.bind_interdomain dom;
 	dom
 
-let create0 fake doms =
+let create0 doms =
 	let port, interface =
-		if fake then (
-			0, Xenctrl.with_intf (fun xc -> Xenctrl.map_foreign_range xc 0 (Xenmmap.getpagesize()) 0n)
-		) else (
+		(
 			let port = Utils.read_file_single_integer Define.xenstored_proc_port
 			and fd = Unix.openfile Define.xenstored_proc_kva
 					       [ Unix.O_RDWR ] 0o600 in
diff --git a/tools/ocaml/xenstored/xenstored.ml b/tools/ocaml/xenstored/xenstored.ml
index 42b8183..25c126a 100644
--- a/tools/ocaml/xenstored/xenstored.ml
+++ b/tools/ocaml/xenstored/xenstored.ml
@@ -176,7 +176,7 @@ let from_channel store cons doms chan =
 			if domid > 0 then
 				Domains.create xc doms domid mfn port
 			else
-				Domains.create0 false doms
+				Domains.create0 doms
 			in
 		Connections.add_domain cons ndom;
 		in
@@ -278,8 +278,7 @@ let _ =
 			Store.mkdir store (Perms.Connection.create 0) localpath;
 
 		if cf.domain_init then (
-			let usingxiu = Xenctrl.is_fake () in
-			Connections.add_domain cons (Domains.create0 usingxiu domains);
+			Connections.add_domain cons (Domains.create0 domains);
 			Event.bind_dom_exc_virq eventchn
 		);
 	);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 07/29] tools: Refactor hypercall calling wrappers into libxencall.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (5 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 06/29] tools/libxc: Remove osdep indirection for privcmd Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 08/29] tools/libxc: drop xc_map_foreign_bulk_compat wrappers Ian Campbell
                     ` (21 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

libxencall will provide a stable API and ABI for calling hypercalls
(although those hypercalls themselves may not have a stable API). As
well as the hypercall buffer infrastructure needed in order to safely
provide pointer arguments to hypercalls.

libxenctrl encapsulates a instance of this interface, so users of that
library are not currently subjected to any actual changes. However all
hypercalls made internally by libxc now use the correct interface. It
is expected that most users of this library will be other libraries
providing a higher level interface, rather than applications directly.

Only the basic functionality to allocate hypercall safe memory is
moved, the type safe stuff and bounce buffers remain in libxc.

Note that the functionality to map foreign pages using privcmd is not
yet moved, meaning that an xc_interface will now contain two open
privcmd file descriptors. Foreign memory mapping is logically separate
functionality and will be moved into its own library.

The new library uses a version script to ensure that only expected
symbols are exported and to version them such that ABI guarantees can
be kept in the future.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---

Must be applied with:
  - "qemu-xen-traditional: Add libxencall to rpath-link" and a
    corresponding QEMU_TAG update folded here.
  - "mini-os: Include libxencall with libxc" and a corresponding bump
    to MINIOS_UPSTREAM_REVISION folded in here.

v3: Moved to tools/libs/call
    Ported new wrappers (altp2m)

v5: Allow _close(NULL).

v6: Use size_t for nr_pages throughout.
    Rebase over "libxc: prefer using privcmd character device"

v7: Added mk-headers-$(XEN_TARGET_ARCH) build dependency instead of
    open coding the recursion.
    Remove incorrect/spurious APP_LDLIBS (which anyway was removed by
    a later patch)

v8: Remove *.so on clean, add distclean target.
---
 .gitignore                        |   2 +
 stubdom/Makefile                  |  17 +++-
 tools/Makefile                    |   1 +
 tools/Rules.mk                    |   7 +-
 tools/libs/Makefile               |   1 +
 tools/libs/call/Makefile          |  71 ++++++++++++++
 tools/libs/call/buffer.c          | 192 ++++++++++++++++++++++++++++++++++++++
 tools/libs/call/core.c            | 147 +++++++++++++++++++++++++++++
 tools/libs/call/freebsd.c         | 140 +++++++++++++++++++++++++++
 tools/libs/call/include/xencall.h |  84 +++++++++++++++++
 tools/libs/call/libxencall.map    |  19 ++++
 tools/libs/call/linux.c           | 138 +++++++++++++++++++++++++++
 tools/libs/call/minios.c          |  81 ++++++++++++++++
 tools/libs/call/netbsd.c          | 121 ++++++++++++++++++++++++
 tools/libs/call/private.h         |  68 ++++++++++++++
 tools/libs/call/solaris.c         |  97 +++++++++++++++++++
 tools/libxc/Makefile              |   7 +-
 tools/libxc/xc_altp2m.c           |  64 ++++---------
 tools/libxc/xc_domain.c           | 105 +++++++--------------
 tools/libxc/xc_evtchn.c           |   9 +-
 tools/libxc/xc_flask.c            |   8 +-
 tools/libxc/xc_freebsd_osdep.c    |  47 ----------
 tools/libxc/xc_gnttab.c           |   9 +-
 tools/libxc/xc_hcall_buf.c        | 138 ++-------------------------
 tools/libxc/xc_kexec.c            |  36 +++----
 tools/libxc/xc_linux_osdep.c      |  49 ----------
 tools/libxc/xc_minios.c           |  32 -------
 tools/libxc/xc_misc.c             |  79 ++++++----------
 tools/libxc/xc_netbsd.c           |  40 --------
 tools/libxc/xc_private.c          |  64 +++++--------
 tools/libxc/xc_private.h          |  76 +++++----------
 tools/libxc/xc_solaris.c          |  16 ----
 tools/libxc/xc_tmem.c             |   7 +-
 tools/misc/Makefile               |   4 +-
 tools/xcutils/Makefile            |   2 +-
 tools/xenpaging/Makefile          |   2 +-
 36 files changed, 1348 insertions(+), 632 deletions(-)
 create mode 100644 tools/libs/call/Makefile
 create mode 100644 tools/libs/call/buffer.c
 create mode 100644 tools/libs/call/core.c
 create mode 100644 tools/libs/call/freebsd.c
 create mode 100644 tools/libs/call/include/xencall.h
 create mode 100644 tools/libs/call/libxencall.map
 create mode 100644 tools/libs/call/linux.c
 create mode 100644 tools/libs/call/minios.c
 create mode 100644 tools/libs/call/netbsd.c
 create mode 100644 tools/libs/call/private.h
 create mode 100644 tools/libs/call/solaris.c

diff --git a/.gitignore b/.gitignore
index a117161..55ecfb7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -62,6 +62,7 @@ stubdom/ioemu
 stubdom/ioemu/
 stubdom/libs-*
 stubdom/libxc-*
+stubdom/libxencall-*
 stubdom/libxenevtchn-*
 stubdom/libxengnttab-*
 stubdom/libxentoollog-*
@@ -91,6 +92,7 @@ config/Docs.mk
 tools/libs/toollog/headers.chk
 tools/libs/evtchn/headers.chk
 tools/libs/gnttab/headers.chk
+tools/libs/call/headers.chk
 tools/blktap2/daemon/blktapctrl
 tools/blktap2/drivers/img2qcow
 tools/blktap2/drivers/lock-util
diff --git a/stubdom/Makefile b/stubdom/Makefile
index 2dbf4a8..5bf4ed6 100644
--- a/stubdom/Makefile
+++ b/stubdom/Makefile
@@ -331,6 +331,12 @@ mk-headers-$(XEN_TARGET_ARCH): $(IOEMU_LINKFARM_TARGET)
 	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/include/*.h include/ && \
 	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/*.c . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/gnttab/Makefile . )
+	mkdir -p libs-$(XEN_TARGET_ARCH)/call/include
+	[ -h libs-$(XEN_TARGET_ARCH)/call/Makefile ] || ( cd libs-$(XEN_TARGET_ARCH)/call && \
+	  ln -sf $(XEN_ROOT)/tools/libs/call/*.h . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/call/include/*.h include/ && \
+	  ln -sf $(XEN_ROOT)/tools/libs/call/*.c . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/call/Makefile . )
 	mkdir -p libxc-$(XEN_TARGET_ARCH)
 	[ -h libxc-$(XEN_TARGET_ARCH)/Makefile ] || ( cd libxc-$(XEN_TARGET_ARCH) && \
 	  ln -sf $(XEN_ROOT)/tools/libxc/*.h . && \
@@ -381,12 +387,21 @@ libs-$(XEN_TARGET_ARCH)/gnttab/libxengnttab.a: mk-headers-$(XEN_TARGET_ARCH) $(N
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/gnttab
 
 #######
+# libxencall
+#######
+
+.PHONY: libxencall
+libxencall: libs-$(XEN_TARGET_ARCH)/call/libxencall.a
+libs-$(XEN_TARGET_ARCH)/call/libxencall.a: mk-headers-$(XEN_TARGET_ARCH) $(NEWLIB_STAMPFILE)
+	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/call
+
+#######
 # libxc
 #######
 
 .PHONY: libxc
 libxc: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a libxc-$(XEN_TARGET_ARCH)/libxenguest.a
-libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn libxengnttab cross-zlib
+libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn libxengnttab libxencall cross-zlib
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= CONFIG_LIBXC_MINIOS=y -C libxc-$(XEN_TARGET_ARCH)
 
  libxc-$(XEN_TARGET_ARCH)/libxenguest.a: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a
diff --git a/tools/Makefile b/tools/Makefile
index f373e71..e5bbc98 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -263,6 +263,7 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/evtchn \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/gnttab \
+		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/call \
 		$(QEMU_UPSTREAM_RPATH)" \
 		--bindir=$(LIBEXEC_BIN) \
 		--datadir=$(SHAREDIR)/qemu-xen \
diff --git a/tools/Rules.mk b/tools/Rules.mk
index 379990f..18e3688 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -13,6 +13,7 @@ XEN_INCLUDE        = $(XEN_ROOT)/tools/include
 XEN_LIBXENTOOLLOG  = $(XEN_ROOT)/tools/libs/toollog
 XEN_LIBXENEVTCHN   = $(XEN_ROOT)/tools/libs/evtchn
 XEN_LIBXENGNTTAB   = $(XEN_ROOT)/tools/libs/gnttab
+XEN_LIBXENCALL     = $(XEN_ROOT)/tools/libs/call
 XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
 XEN_XENLIGHT       = $(XEN_ROOT)/tools/libxl
 XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
@@ -98,8 +99,12 @@ CFLAGS_libxengntshr = -I$(XEN_LIBXENGNTTAB)/include $(CFLAGS_xeninclude)
 LDLIBS_libxengntshr = $(XEN_LIBXENGNTTAB)/libxengnttab$(libextension)
 SHLIB_libxengntshr  = -Wl,-rpath-link=$(XEN_LIBXENGNTTAB)
 
+CFLAGS_libxencall = -I$(XEN_LIBXENCALL)/include $(CFLAGS_xeninclude)
+LDLIBS_libxencall = $(XEN_LIBXENCALL)/libxencall$(libextension)
+SHLIB_libxencall  = -Wl,-rpath-link=$(XEN_LIBXENCALL)
+
 CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_xeninclude)
-SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn) $(SHLIB_libxengnttab) $(SHLIB_libxengntshr)
+SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn) $(SHLIB_libxengnttab) $(SHLIB_libxengntshr) $(SHLIB_libxencall)
 LDLIBS_libxenctrl = $(SHDEPS_libxenctrl) $(XEN_LIBXC)/libxenctrl$(libextension)
 SHLIB_libxenctrl  = $(SHDEPS_libxenctrl) -Wl,-rpath-link=$(XEN_LIBXC)
 
diff --git a/tools/libs/Makefile b/tools/libs/Makefile
index 00156ae..f4f5d57 100644
--- a/tools/libs/Makefile
+++ b/tools/libs/Makefile
@@ -5,5 +5,6 @@ SUBDIRS-y :=
 SUBDIRS-y += toollog
 SUBDIRS-y += evtchn
 SUBDIRS-y += gnttab
+SUBDIRS-y += call
 
 all clean install distclean: %: subdirs-%
diff --git a/tools/libs/call/Makefile b/tools/libs/call/Makefile
new file mode 100644
index 0000000..9402ea5
--- /dev/null
+++ b/tools/libs/call/Makefile
@@ -0,0 +1,71 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+SHLIB_LDFLAGS += -Wl,--version-script=libxencall.map
+
+CFLAGS   += -Werror -Wmissing-prototypes
+CFLAGS   += -I./include $(CFLAGS_xeninclude)
+CFLAGS   += $(CFLAGS_libxentoollog)
+
+SRCS-y                 += core.c buffer.c
+SRCS-$(CONFIG_Linux)   += linux.c
+SRCS-$(CONFIG_FreeBSD) += freebsd.c
+SRCS-$(CONFIG_SunOS)   += solaris.c
+SRCS-$(CONFIG_NetBSD)  += netbsd.c
+SRCS-$(CONFIG_MiniOS)  += minios.c
+
+LIB_OBJS := $(patsubst %.c,%.o,$(SRCS-y))
+PIC_OBJS := $(patsubst %.c,%.opic,$(SRCS-y))
+
+LIB := libxencall.a
+ifneq ($(nosharedlibs),y)
+LIB += libxencall.so
+endif
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build:
+	$(MAKE) libs
+
+.PHONY: libs
+libs: headers.chk $(LIB)
+
+headers.chk: $(wildcard include/*.h)
+
+libxencall.a: $(LIB_OBJS)
+	$(AR) rc $@ $^
+
+libxencall.so: libxencall.so.$(MAJOR)
+	$(SYMLINK_SHLIB) $< $@
+libxencall.so.$(MAJOR): libxencall.so.$(MAJOR).$(MINOR)
+	$(SYMLINK_SHLIB) $< $@
+
+libxencall.so.$(MAJOR).$(MINOR): $(PIC_OBJS) libxencall.map
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxencall.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(PIC_OBJS) $(LDLIBS_libxentoollog) $(APPEND_LDFLAGS)
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(libdir)
+	$(INSTALL_DIR) $(DESTDIR)$(includedir)
+	$(INSTALL_SHLIB) libxencall.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
+	$(INSTALL_DATA) libxencall.a $(DESTDIR)$(libdir)
+	$(SYMLINK_SHLIB) libxencall.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxencall.so.$(MAJOR)
+	$(SYMLINK_SHLIB) libxencall.so.$(MAJOR) $(DESTDIR)$(libdir)/libxencall.so
+	$(INSTALL_DATA) include/xencall.h $(DESTDIR)$(includedir)
+
+.PHONY: TAGS
+TAGS:
+	etags -t *.c *.h
+
+.PHONY: clean
+clean:
+	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
+	rm -f libxencall.so.$(MAJOR).$(MINOR) libxencall.so.$(MAJOR)
+	rm -f headers.chk
+
+.PHONY: distclean
+distclean: clean
diff --git a/tools/libs/call/buffer.c b/tools/libs/call/buffer.c
new file mode 100644
index 0000000..1a1b27a
--- /dev/null
+++ b/tools/libs/call/buffer.c
@@ -0,0 +1,192 @@
+/*
+ * 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 <string.h>
+#include <pthread.h>
+
+#include "private.h"
+
+#define DBGPRINTF(_m...) \
+	xtl_log(xcall->logger, XTL_DEBUG, -1, "xencall:buffer", _m)
+
+#define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
+
+pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;
+
+static void cache_lock(xencall_handle *xcall)
+{
+    int saved_errno = errno;
+    if ( xcall->flags & XENCALL_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_lock(&cache_mutex);
+    /* Ignore pthread errors. */
+    errno = saved_errno;
+}
+
+static void cache_unlock(xencall_handle *xcall)
+{
+    int saved_errno = errno;
+    if ( xcall->flags & XENCALL_OPENFLAG_NON_REENTRANT )
+        return;
+    pthread_mutex_unlock(&cache_mutex);
+    /* Ignore pthread errors. */
+    errno = saved_errno;
+}
+
+static void *cache_alloc(xencall_handle *xcall, size_t nr_pages)
+{
+    void *p = NULL;
+
+    cache_lock(xcall);
+
+    xcall->buffer_total_allocations++;
+    xcall->buffer_current_allocations++;
+    if ( xcall->buffer_current_allocations > xcall->buffer_maximum_allocations )
+        xcall->buffer_maximum_allocations = xcall->buffer_current_allocations;
+
+    if ( nr_pages > 1 )
+    {
+        xcall->buffer_cache_toobig++;
+    }
+    else if ( xcall->buffer_cache_nr > 0 )
+    {
+        p = xcall->buffer_cache[--xcall->buffer_cache_nr];
+        xcall->buffer_cache_hits++;
+    }
+    else
+    {
+        xcall->buffer_cache_misses++;
+    }
+
+    cache_unlock(xcall);
+
+    return p;
+}
+
+static int cache_free(xencall_handle *xcall, void *p, size_t nr_pages)
+{
+    int rc = 0;
+
+    cache_lock(xcall);
+
+    xcall->buffer_total_releases++;
+    xcall->buffer_current_allocations--;
+
+    if ( nr_pages == 1 &&
+	 xcall->buffer_cache_nr < BUFFER_CACHE_SIZE )
+    {
+        xcall->buffer_cache[xcall->buffer_cache_nr++] = p;
+        rc = 1;
+    }
+
+    cache_unlock(xcall);
+
+    return rc;
+}
+
+void buffer_release_cache(xencall_handle *xcall)
+{
+    void *p;
+
+    cache_lock(xcall);
+
+    DBGPRINTF("total allocations:%d total releases:%d",
+              xcall->buffer_total_allocations,
+              xcall->buffer_total_releases);
+    DBGPRINTF("current allocations:%d maximum allocations:%d",
+              xcall->buffer_current_allocations,
+              xcall->buffer_maximum_allocations);
+    DBGPRINTF("cache current size:%d",
+              xcall->buffer_cache_nr);
+    DBGPRINTF("cache hits:%d misses:%d toobig:%d",
+              xcall->buffer_cache_hits,
+              xcall->buffer_cache_misses,
+              xcall->buffer_cache_toobig);
+
+    while ( xcall->buffer_cache_nr > 0 )
+    {
+        p = xcall->buffer_cache[--xcall->buffer_cache_nr];
+        osdep_free_pages(xcall, p, 1);
+    }
+
+    cache_unlock(xcall);
+}
+
+void *xencall_alloc_buffer_pages(xencall_handle *xcall, size_t nr_pages)
+{
+    void *p = cache_alloc(xcall, nr_pages);
+
+    if ( !p )
+        p = osdep_alloc_pages(xcall, nr_pages);
+
+    if (!p)
+        return NULL;
+
+    memset(p, 0, nr_pages * PAGE_SIZE);
+
+    return p;
+}
+
+void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages)
+{
+    if ( p == NULL )
+        return;
+
+    if ( !cache_free(xcall, p, nr_pages) )
+        osdep_free_pages(xcall, p, nr_pages);
+}
+
+struct allocation_header {
+    int nr_pages;
+};
+
+void *xencall_alloc_buffer(xencall_handle *xcall, size_t size)
+{
+    size_t actual_size = ROUNDUP(size + sizeof(struct allocation_header), PAGE_SHIFT);
+    int nr_pages = actual_size >> PAGE_SHIFT;
+    struct allocation_header *hdr;
+
+    hdr = xencall_alloc_buffer_pages(xcall, nr_pages);
+    if ( hdr == NULL )
+        return NULL;
+
+    hdr->nr_pages = nr_pages;
+
+    return (void *)(hdr+1);
+}
+
+void xencall_free_buffer(xencall_handle *xcall, void *p)
+{
+    struct allocation_header *hdr;
+
+    if (p == NULL)
+        return;
+
+    hdr = p;
+    --hdr;
+
+    xencall_free_buffer_pages(xcall, hdr, hdr->nr_pages);
+}
+
+/*
+ * 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/call/core.c b/tools/libs/call/core.c
new file mode 100644
index 0000000..a342871
--- /dev/null
+++ b/tools/libs/call/core.c
@@ -0,0 +1,147 @@
+/*
+ * 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 <stdlib.h>
+
+#include "private.h"
+
+xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
+{
+	xencall_handle *xcall = malloc(sizeof(*xcall));
+    int rc;
+
+    if (!xcall) return NULL;
+
+    xcall->flags = open_flags;
+    xcall->buffer_cache_nr = 0;
+
+    xcall->buffer_total_allocations = 0;
+    xcall->buffer_total_releases = 0;
+    xcall->buffer_current_allocations = 0;
+    xcall->buffer_maximum_allocations = 0;
+    xcall->buffer_cache_hits = 0;
+    xcall->buffer_cache_misses = 0;
+    xcall->buffer_cache_toobig = 0;
+    xcall->logger = logger;
+    xcall->logger_tofree = NULL;
+
+    if (!xcall->logger) {
+        xcall->logger = xcall->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xcall->logger) goto err;
+    }
+
+    rc = osdep_xencall_open(xcall);
+    if ( rc  < 0 ) goto err;
+
+    return xcall;
+
+err:
+    osdep_xencall_close(xcall);
+    xtl_logger_destroy(xcall->logger_tofree);
+    free(xcall);
+    return NULL;
+}
+
+int xencall_close(xencall_handle *xcall)
+{
+    int rc;
+
+    if ( !xcall )
+        return 0;
+
+    rc = osdep_xencall_close(xcall);
+    buffer_release_cache(xcall);
+    xtl_logger_destroy(xcall->logger_tofree);
+    free(xcall);
+    return rc;
+}
+
+int xencall0(xencall_handle *xcall, unsigned int op)
+{
+    privcmd_hypercall_t call = {
+        .op = op,
+    };
+
+    return osdep_hypercall(xcall, &call);
+}
+
+int xencall1(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1)
+{
+    privcmd_hypercall_t call = {
+        .op = op,
+        .arg = { arg1 },
+    };
+
+    return osdep_hypercall(xcall, &call);
+}
+
+int xencall2(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2)
+{
+    privcmd_hypercall_t call = {
+        .op = op,
+        .arg = { arg1, arg2 },
+    };
+
+    return osdep_hypercall(xcall, &call);
+}
+
+int xencall3(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2, uint64_t arg3)
+{
+    privcmd_hypercall_t call = {
+        .op = op,
+        .arg = { arg1, arg2, arg3},
+    };
+
+    return osdep_hypercall(xcall, &call);
+}
+
+int xencall4(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2, uint64_t arg3,
+             uint64_t arg4)
+{
+    privcmd_hypercall_t call = {
+        .op = op,
+        .arg = { arg1, arg2, arg3, arg4 },
+    };
+
+    return osdep_hypercall(xcall, &call);
+}
+
+int xencall5(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2, uint64_t arg3,
+             uint64_t arg4, uint64_t arg5)
+{
+    privcmd_hypercall_t call = {
+        .op = op,
+        .arg = { arg1, arg2, arg3, arg4, arg5 },
+    };
+
+    return osdep_hypercall(xcall, &call);
+}
+
+/*
+ * 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/call/freebsd.c b/tools/libs/call/freebsd.c
new file mode 100644
index 0000000..2413966
--- /dev/null
+++ b/tools/libs/call/freebsd.c
@@ -0,0 +1,140 @@
+ /******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split from xc_freebsd_osdep.c
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+
+#include "private.h"
+
+#define PRIVCMD_DEV     "/dev/xen/privcmd"
+
+int osdep_xencall_open(xencall_handle *xcall)
+{
+    int flags, saved_errno;
+    int fd = open(PRIVCMD_DEV, O_RDWR);
+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command interface "
+               PRIVCMD_DEV);
+        return -1;
+    }
+
+    /*
+     * Although we return the file handle as the 'xc handle' the API
+     * does not specify / guarentee that this integer is in fact
+     * a file handle. Thus we must take responsiblity to ensure
+     * it doesn't propagate (ie leak) outside the process.
+     */
+    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
+    {
+        PERROR("Could not get file handle flags");
+        goto error;
+    }
+
+    flags |= FD_CLOEXEC;
+
+    if ( fcntl(fd, F_SETFD, flags) < 0 )
+    {
+        PERROR("Could not set file handle flags");
+        goto error;
+    }
+
+    xcall->fd = fd;
+    return 0;
+
+ error:
+    saved_errno = errno;
+    close(fd);
+    errno = saved_errno;
+
+    return -1;
+}
+
+int osdep_xencall_close(xencall_handle *xcall)
+{
+    int fd = xcall->fd;
+    if ( fd == -1 )
+        return 0;
+    return close(fd);
+}
+
+int osdep_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
+{
+    int fd = xcall->fd;
+    int ret;
+
+    ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
+
+    return (ret == 0) ? hypercall->retval : ret;
+}
+
+void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
+{
+    size_t size = npages * PAGE_SIZE;
+    void *p;
+
+    /* Address returned by mmap is page aligned. */
+    p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
+             -1, 0);
+    if (p == NULL)
+        return NULL;
+
+    /*
+     * Since FreeBSD doesn't have the MAP_LOCKED flag,
+     * lock memory using mlock.
+     */
+    if ( mlock(p, size) < 0 )
+    {
+        munmap(p, size);
+        return NULL;
+    }
+
+    return p;
+}
+
+void osdep_free_pages(xencall_handle *xcall, void *ptr, size_t npages)
+{
+    int saved_errno = errno;
+    /* Unlock pages */
+    munlock(ptr, npages * PAGE_SIZE);
+
+    munmap(ptr, npages * PAGE_SIZE);
+    /* We MUST propagate the hypercall errno, not unmap call's. */
+    errno = saved_errno;
+}
+
+/*
+ * 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/call/include/xencall.h b/tools/libs/call/include/xencall.h
new file mode 100644
index 0000000..0d91aa8
--- /dev/null
+++ b/tools/libs/call/include/xencall.h
@@ -0,0 +1,84 @@
+/*
+ * 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/>.
+ */
+#ifndef XENCALL_H
+#define XENCALL_H
+
+/*
+ * This library allows you to make arbitrary hypercalls (subject to
+ * sufficient permission for the process and the domain itself). Note
+ * that while the library interface is stable the hypercalls are
+ * subject to their own rules.
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+
+/* Callers who don't care don't need to #include <xentoollog.h> */
+typedef struct xentoollog_logger xentoollog_logger;
+
+typedef struct xencall_handle xencall_handle;
+
+/*
+ */
+#define XENCALL_OPENFLAG_NON_REENTRANT (1U<<0)
+
+/*
+ * Return a handle onto the hypercall driver.  Logs errors.
+ */
+xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags);
+
+/*
+ * Close a handle previously allocated with xencall_open().
+ */
+int xencall_close(xencall_handle *xcall);
+
+/*
+ * Call hypercalls with varying numbers of arguments.
+ */
+int xencall0(xencall_handle *xcall, unsigned int op);
+int xencall1(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1);
+int xencall2(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2);
+int xencall3(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2, uint64_t arg3);
+int xencall4(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2, uint64_t arg3,
+             uint64_t arg4);
+int xencall5(xencall_handle *xcall, unsigned int op,
+             uint64_t arg1, uint64_t arg2, uint64_t arg3,
+             uint64_t arg4, uint64_t arg5);
+
+/*
+ * Allocate and free memory which is suitable for use as a pointer
+ * argument to a hypercall.
+ */
+void *xencall_alloc_buffer_pages(xencall_handle *xcall, size_t nr_pages);
+void xencall_free_buffer_pages(xencall_handle *xcall, void *p, size_t nr_pages);
+
+void *xencall_alloc_buffer(xencall_handle *xcall, size_t size);
+void xencall_free_buffer(xencall_handle *xcall, void *p);
+
+#endif
+
+/*
+ * 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/call/libxencall.map b/tools/libs/call/libxencall.map
new file mode 100644
index 0000000..2f96144
--- /dev/null
+++ b/tools/libs/call/libxencall.map
@@ -0,0 +1,19 @@
+VERS_1.0 {
+	global:
+		xencall_open;
+		xencall_close;
+
+		xencall0;
+		xencall1;
+		xencall2;
+		xencall3;
+		xencall4;
+		xencall5;
+		xencall6;
+
+		xencall_alloc_buffer;
+		xencall_free_buffer;
+		xencall_alloc_buffer_pages;
+		xencall_free_buffer_pages;
+	local: *; /* Do not expose anything by default */
+};
diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
new file mode 100644
index 0000000..55e1e83
--- /dev/null
+++ b/tools/libs/call/linux.c
@@ -0,0 +1,138 @@
+/*
+ * 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/>.
+ *
+ * Split out from xc_linus_osdep.c:
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/mman.h>
+#include <sys/ioctl.h>
+
+#include "private.h"
+
+int osdep_xencall_open(xencall_handle *xcall)
+{
+    int flags, saved_errno;
+    int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
+
+    if ( fd == -1 && ( errno == ENOENT || errno == ENXIO || errno == ENODEV ))
+    {
+        /* Fallback to /proc/xen/privcmd */
+        fd = open("/proc/xen/privcmd", O_RDWR);
+    }
+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command interface");
+        return -1;
+    }
+
+    /* Although we return the file handle as the 'xc handle' the API
+       does not specify / guarentee that this integer is in fact
+       a file handle. Thus we must take responsiblity to ensure
+       it doesn't propagate (ie leak) outside the process */
+    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
+    {
+        PERROR("Could not get file handle flags");
+        goto error;
+    }
+
+    flags |= FD_CLOEXEC;
+
+    if ( fcntl(fd, F_SETFD, flags) < 0 )
+    {
+        PERROR("Could not set file handle flags");
+        goto error;
+    }
+
+    xcall->fd = fd;
+    return 0;
+
+ error:
+    saved_errno = errno;
+    close(fd);
+    errno = saved_errno;
+    return -1;
+}
+
+int osdep_xencall_close(xencall_handle *xcall)
+{
+    int fd = xcall->fd;
+    if (fd == -1)
+        return 0;
+    return close(fd);
+}
+
+int osdep_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
+{
+    return ioctl(xcall->fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
+}
+
+void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
+{
+    size_t size = npages * PAGE_SIZE;
+    void *p;
+    int rc, saved_errno;
+
+    /* Address returned by mmap is page aligned. */
+    p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
+    if ( p == MAP_FAILED )
+    {
+        PERROR("xc_alloc_hypercall_buffer: mmap failed");
+        return NULL;
+    }
+
+    /* Do not copy the VMA to child process on fork. Avoid the page being COW
+        on hypercall. */
+    rc = madvise(p, npages * PAGE_SIZE, MADV_DONTFORK);
+    if ( rc < 0 )
+    {
+        PERROR("xc_alloc_hypercall_buffer: madvise failed");
+        goto out;
+    }
+
+    return p;
+
+out:
+    saved_errno = errno;
+    (void)munmap(p, size);
+    errno = saved_errno;
+    return NULL;
+}
+
+void osdep_free_pages(xencall_handle *xcall, void *ptr, size_t npages)
+{
+    int saved_errno = errno;
+    /* Recover the VMA flags. Maybe it's not necessary */
+    madvise(ptr, npages * PAGE_SIZE, MADV_DOFORK);
+
+    munmap(ptr, npages * PAGE_SIZE);
+    /* We MUST propagate the hypercall errno, not unmap call's. */
+    errno = saved_errno;
+}
+
+/*
+ * 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/call/minios.c b/tools/libs/call/minios.c
new file mode 100644
index 0000000..3bee7be
--- /dev/null
+++ b/tools/libs/call/minios.c
@@ -0,0 +1,81 @@
+/*
+ * 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/>.
+ *
+ * Split out from xc_minios.c
+ *
+ * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
+ */
+
+#include <mini-os/types.h>
+#include <mini-os/os.h>
+#include <mini-os/mm.h>
+#include <mini-os/lib.h>
+
+#include <errno.h>
+#include <malloc.h>
+
+#include "private.h"
+
+int osdep_xencall_open(xencall_handle *xcall)
+{
+    /* No fd required */
+    return 0;
+}
+
+int osdep_xencall_close(xencall_handle *xcall)
+{
+    return 0;
+}
+
+int osdep_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
+{
+    multicall_entry_t call;
+    int i, ret;
+
+    call.op = hypercall->op;
+    for (i = 0; i < 5; i++)
+        call.args[i] = hypercall->arg[i];
+
+    ret = HYPERVISOR_multicall(&call, 1);
+
+    if (ret < 0) {
+	errno = -ret;
+	return -1;
+    }
+    if ((long) call.result < 0) {
+        errno = - (long) call.result;
+        return -1;
+    }
+    return call.result;
+}
+
+void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
+{
+    return memalign(PAGE_SIZE, npages * PAGE_SIZE);
+}
+
+void osdep_free_pages(xencall_handle *xcall, void *ptr, size_t npages)
+{
+    free(ptr);
+}
+
+/*
+ * 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/call/netbsd.c b/tools/libs/call/netbsd.c
new file mode 100644
index 0000000..2aa02f1
--- /dev/null
+++ b/tools/libs/call/netbsd.c
@@ -0,0 +1,121 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split from xc_netbsd.c
+ */
+
+#include "xc_private.h"
+
+#include <unistd.h>
+#include <fcntl.h>
+#include <malloc.h>
+#include <sys/mman.h>
+
+int osdep_xencall_open(xencall_handle *xcall)
+{
+    int flags, saved_errno;
+    int fd = open("/kern/xen/privcmd", O_RDWR);
+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command interface");
+        return -1;
+    }
+
+    /* Although we return the file handle as the 'xc handle' the API
+       does not specify / guarentee that this integer is in fact
+       a file handle. Thus we must take responsiblity to ensure
+       it doesn't propagate (ie leak) outside the process */
+    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
+    {
+        PERROR("Could not get file handle flags");
+        goto error;
+    }
+    flags |= FD_CLOEXEC;
+    if ( fcntl(fd, F_SETFD, flags) < 0 )
+    {
+        PERROR("Could not set file handle flags");
+        goto error;
+    }
+
+    xcall->fd = fd;
+    return 0;
+
+ error:
+    saved_errno = errno;
+    close(fd);
+    errno = saved_errno;
+    return -1;
+}
+
+int osdep_xencall_close(xencall_handle *xcall)
+{
+    int fd = xcall->fd;
+    return close(fd);
+}
+
+void *osdep_alloc_hypercall_buffer(xencall_handle *xcall, size_t npages)
+{
+    size_t size = npages * XC_PAGE_SIZE;
+    void *p;
+
+    p = xc_memalign(xcall, XC_PAGE_SIZE, size);
+    if (!p)
+        return NULL;
+
+    if ( mlock(p, size) < 0 )
+    {
+        free(p);
+        return NULL;
+    }
+    return p;
+}
+
+void osdep_free_hypercall_buffer(xencall_handle *xcall, void *ptr,
+                                 size_t npages)
+{
+    (void) munlock(ptr, npages * XC_PAGE_SIZE);
+    free(ptr);
+}
+
+int do_xen_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
+{
+    int fd = xcall->fd;
+    int error = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
+
+    /*
+     * Since NetBSD ioctl can only return 0 on success or < 0 on
+     * error, if we want to return a value from ioctl we should
+     * do so by setting hypercall->retval, to mimic Linux ioctl
+     * implementation.
+     */
+    if (error < 0)
+        return error;
+    else
+        return hypercall->retval;
+}
+
+/*
+ * 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/call/private.h b/tools/libs/call/private.h
new file mode 100644
index 0000000..37dd15f
--- /dev/null
+++ b/tools/libs/call/private.h
@@ -0,0 +1,68 @@
+#ifndef XENCALL_PRIVATE_H
+#define XENCALL_PRIVATE_H
+
+#include <xentoollog.h>
+
+#include <xencall.h>
+
+#include <xen/xen.h>
+#include <xen/sys/privcmd.h>
+
+#ifndef PAGE_SHIFT /* Mini-os, Yukk */
+#define PAGE_SHIFT           12
+#endif
+#ifndef __MINIOS__ /* Yukk */
+#define PAGE_SIZE            (1UL << PAGE_SHIFT)
+#define PAGE_MASK            (~(PAGE_SIZE-1))
+#endif
+
+struct xencall_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    unsigned flags;
+    int fd;
+
+    /*
+     * A simple cache of unused, single page, hypercall buffers
+     *
+     * Protected by a global lock.
+     */
+#define BUFFER_CACHE_SIZE 4
+    int buffer_cache_nr;
+    void *buffer_cache[BUFFER_CACHE_SIZE];
+
+    /*
+     * Hypercall buffer statistics. All protected by the global
+     * buffer_cache lock.
+     */
+    int buffer_total_allocations;
+    int buffer_total_releases;
+    int buffer_current_allocations;
+    int buffer_maximum_allocations;
+    int buffer_cache_hits;
+    int buffer_cache_misses;
+    int buffer_cache_toobig;
+};
+
+int osdep_xencall_open(xencall_handle *xcall);
+int osdep_xencall_close(xencall_handle *xcall);
+
+int osdep_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall);
+
+void *osdep_alloc_pages(xencall_handle *xcall, size_t nr_pages);
+void osdep_free_pages(xencall_handle *xcall, void *p, size_t nr_pages);
+
+void buffer_release_cache(xencall_handle *xcall);
+
+#define PERROR(_f...) xtl_log(xcall->logger, XTL_ERROR, errno, "xencall", _f)
+
+#endif
+
+/*
+ * 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/call/solaris.c b/tools/libs/call/solaris.c
new file mode 100644
index 0000000..945d867
--- /dev/null
+++ b/tools/libs/call/solaris.c
@@ -0,0 +1,97 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split from xc_solaris.c
+ */
+
+#include "xc_private.h"
+
+#include <xen/memory.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <malloc.h>
+
+int osdep_xencall_open(xencall_handle *xcall)
+{
+    int flags, saved_errno;
+    int fd = open("/dev/xen/privcmd", O_RDWR);
+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command interface");
+        return -1;
+    }
+
+    /* Although we return the file handle as the 'xc handle' the API
+       does not specify / guarentee that this integer is in fact
+       a file handle. Thus we must take responsiblity to ensure
+       it doesn't propagate (ie leak) outside the process */
+    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
+    {
+        PERROR("Could not get file handle flags");
+        goto error;
+    }
+    flags |= FD_CLOEXEC;
+    if ( fcntl(fd, F_SETFD, flags) < 0 )
+    {
+        PERROR("Could not set file handle flags");
+        goto error;
+    }
+
+    xcall->fd = fd;
+    return 0;
+
+ error:
+    saved_errno = errno;
+    close(fd);
+    errno = saved_errno;
+    return -1;
+}
+
+int osdep_xencall_close(xencall_handle *xcall)
+{
+    int fd = xcall->fd;
+    return close(fd);
+}
+
+void *osdep_alloc_hypercall_buffer(xencall_handle *xcall, size_t npages)
+{
+    return xc_memalign(xcall, XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
+}
+
+void osdep_free_hypercall_buffer(xencall_handle *xcall, void *ptr,
+                                 size_t npages)
+{
+    free(ptr);
+}
+
+int do_xen_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
+{
+    int fd = xcall->fd;
+    return ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index 3305fdd..a122f73 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -122,6 +122,11 @@ GUEST_PIC_OBJS := $(patsubst %.c,%.opic,$(GUEST_SRCS-y))
 $(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) \
 $(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h
 
+# libxenguest includes xc_private.h, so needs this despite not using
+# this functionality directly.
+$(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) \
+$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): CFLAGS += $(CFLAGS_libxencall)
+
 $(CTRL_LIB_OBJS) $(CTRL_PIC_OBJS): CFLAGS += $(CFLAGS_libxengnttab) $(CFLAGS_libxengntshr)
 
 LIB := libxenctrl.a
@@ -202,7 +207,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
 	$(SYMLINK_SHLIB) $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(LDLIBS_libxencall) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 # libxenguest
 
diff --git a/tools/libxc/xc_altp2m.c b/tools/libxc/xc_altp2m.c
index 87a0fdf..0639632 100644
--- a/tools/libxc/xc_altp2m.c
+++ b/tools/libxc/xc_altp2m.c
@@ -27,22 +27,18 @@
 int xc_altp2m_get_domain_state(xc_interface *handle, domid_t dom, bool *state)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_get_domain_state;
     arg->domain = dom;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     if ( !rc )
         *state = arg->u.domain_state.state;
@@ -54,23 +50,19 @@ int xc_altp2m_get_domain_state(xc_interface *handle, domid_t dom, bool *state)
 int xc_altp2m_set_domain_state(xc_interface *handle, domid_t dom, bool state)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_set_domain_state;
     arg->domain = dom;
     arg->u.domain_state.state = state;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(handle, arg);
     return rc;
@@ -81,24 +73,20 @@ int xc_altp2m_set_vcpu_enable_notify(xc_interface *handle, domid_t domid,
                                      uint32_t vcpuid, xen_pfn_t gfn)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_vcpu_enable_notify;
     arg->domain = domid;
     arg->u.enable_notify.vcpu_id = vcpuid;
     arg->u.enable_notify.gfn = gfn;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(handle, arg);
     return rc;
@@ -108,24 +96,20 @@ int xc_altp2m_create_view(xc_interface *handle, domid_t domid,
                           xenmem_access_t default_access, uint16_t *view_id)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_create_p2m;
     arg->domain = domid;
     arg->u.view.view = -1;
     arg->u.view.hvmmem_default_access = default_access;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     if ( !rc )
         *view_id = arg->u.view.view;
@@ -138,23 +122,19 @@ int xc_altp2m_destroy_view(xc_interface *handle, domid_t domid,
                            uint16_t view_id)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_destroy_p2m;
     arg->domain = domid;
     arg->u.view.view = view_id;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(handle, arg);
     return rc;
@@ -165,23 +145,19 @@ int xc_altp2m_switch_to_view(xc_interface *handle, domid_t domid,
                              uint16_t view_id)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_switch_p2m;
     arg->domain = domid;
     arg->u.view.view = view_id;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(handle, arg);
     return rc;
@@ -192,17 +168,12 @@ int xc_altp2m_set_mem_access(xc_interface *handle, domid_t domid,
                              xenmem_access_t access)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_set_mem_access;
     arg->domain = domid;
@@ -210,7 +181,8 @@ int xc_altp2m_set_mem_access(xc_interface *handle, domid_t domid,
     arg->u.set_mem_access.hvmmem_access = access;
     arg->u.set_mem_access.gfn = gfn;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(handle, arg);
     return rc;
@@ -221,17 +193,12 @@ int xc_altp2m_change_gfn(xc_interface *handle, domid_t domid,
                          xen_pfn_t new_gfn)
 {
     int rc;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_altp2m_op_t, arg);
 
     arg = xc_hypercall_buffer_alloc(handle, arg, sizeof(*arg));
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_altp2m;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->version = HVMOP_ALTP2M_INTERFACE_VERSION;
     arg->cmd = HVMOP_altp2m_change_gfn;
     arg->domain = domid;
@@ -239,7 +206,8 @@ int xc_altp2m_change_gfn(xc_interface *handle, domid_t domid,
     arg->u.change_gfn.old_gfn = old_gfn;
     arg->u.change_gfn.new_gfn = new_gfn;
 
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op, HVMOP_altp2m,
+		  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(handle, arg);
     return rc;
diff --git a/tools/libxc/xc_domain.c b/tools/libxc/xc_domain.c
index 96506d5..8fde415 100644
--- a/tools/libxc/xc_domain.c
+++ b/tools/libxc/xc_domain.c
@@ -122,7 +122,6 @@ int xc_domain_shutdown(xc_interface *xch,
                        int reason)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(sched_remote_shutdown_t, arg);
 
     arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
@@ -132,13 +131,11 @@ int xc_domain_shutdown(xc_interface *xch,
         goto out1;
     }
 
-    hypercall.op     = __HYPERVISOR_sched_op;
-    hypercall.arg[0] = (unsigned long)SCHEDOP_remote_shutdown;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
     arg->domain_id = domid;
     arg->reason = reason;
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_sched_op,
+                   SCHEDOP_remote_shutdown,
+                   HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -561,7 +558,6 @@ int xc_watchdog(xc_interface *xch,
                 uint32_t timeout)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(sched_watchdog_t, arg);
 
     arg = xc_hypercall_buffer_alloc(xch, arg, sizeof(*arg));
@@ -571,13 +567,12 @@ int xc_watchdog(xc_interface *xch,
         goto out1;
     }
 
-    hypercall.op     = __HYPERVISOR_sched_op;
-    hypercall.arg[0] = (unsigned long)SCHEDOP_watchdog;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
     arg->id = id;
     arg->timeout = timeout;
 
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_sched_op,
+                   SCHEDOP_watchdog,
+                   HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -1356,7 +1351,6 @@ static inline int xc_hvm_param_deprecated_check(uint32_t param)
 
 int xc_hvm_param_set(xc_interface *handle, domid_t dom, uint32_t param, uint64_t value)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_param_t, arg);
     int rc = xc_hvm_param_deprecated_check(param);
 
@@ -1367,20 +1361,18 @@ int xc_hvm_param_set(xc_interface *handle, domid_t dom, uint32_t param, uint64_t
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_set_param;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
     arg->domid = dom;
     arg->index = param;
     arg->value = value;
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_set_param,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
     xc_hypercall_buffer_free(handle, arg);
     return rc;
 }
 
 int xc_hvm_param_get(xc_interface *handle, domid_t dom, uint32_t param, uint64_t *value)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_param_t, arg);
     int rc = xc_hvm_param_deprecated_check(param);
 
@@ -1391,12 +1383,11 @@ int xc_hvm_param_get(xc_interface *handle, domid_t dom, uint32_t param, uint64_t
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_get_param;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
     arg->domid = dom;
     arg->index = param;
-    rc = do_xen_hypercall(handle, &hypercall);
+    rc = xencall2(handle->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_get_param,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
     *value = arg->value;
     xc_hypercall_buffer_free(handle, arg);
     return rc;
@@ -1424,7 +1415,6 @@ int xc_hvm_create_ioreq_server(xc_interface *xch,
                                int handle_bufioreq,
                                ioservid_t *id)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_create_ioreq_server_t, arg);
     int rc;
 
@@ -1432,14 +1422,12 @@ int xc_hvm_create_ioreq_server(xc_interface *xch,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_create_ioreq_server;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->handle_bufioreq = handle_bufioreq;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_create_ioreq_server,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     *id = arg->id;
 
@@ -1454,7 +1442,6 @@ int xc_hvm_get_ioreq_server_info(xc_interface *xch,
                                  xen_pfn_t *bufioreq_pfn,
                                  evtchn_port_t *bufioreq_port)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_get_ioreq_server_info_t, arg);
     int rc;
 
@@ -1462,14 +1449,12 @@ int xc_hvm_get_ioreq_server_info(xc_interface *xch,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_get_ioreq_server_info;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_get_ioreq_server_info,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
     if ( rc != 0 )
         goto done;
 
@@ -1491,7 +1476,6 @@ int xc_hvm_map_io_range_to_ioreq_server(xc_interface *xch, domid_t domid,
                                         ioservid_t id, int is_mmio,
                                         uint64_t start, uint64_t end)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
     int rc;
 
@@ -1499,17 +1483,15 @@ int xc_hvm_map_io_range_to_ioreq_server(xc_interface *xch, domid_t domid,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
     arg->type = is_mmio ? HVMOP_IO_RANGE_MEMORY : HVMOP_IO_RANGE_PORT;
     arg->start = start;
     arg->end = end;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_map_io_range_to_ioreq_server,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
     return rc;
@@ -1519,7 +1501,6 @@ int xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,
                                             ioservid_t id, int is_mmio,
                                             uint64_t start, uint64_t end)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
     int rc;
 
@@ -1527,17 +1508,15 @@ int xc_hvm_unmap_io_range_from_ioreq_server(xc_interface *xch, domid_t domid,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
     arg->type = is_mmio ? HVMOP_IO_RANGE_MEMORY : HVMOP_IO_RANGE_PORT;
     arg->start = start;
     arg->end = end;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_unmap_io_range_from_ioreq_server,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
     return rc;
@@ -1548,7 +1527,6 @@ int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t domid,
                                       uint8_t bus, uint8_t device,
                                       uint8_t function)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
     int rc;
 
@@ -1561,10 +1539,6 @@ int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t domid,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_map_io_range_to_ioreq_server;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
     arg->type = HVMOP_IO_RANGE_PCI;
@@ -1578,7 +1552,9 @@ int xc_hvm_map_pcidev_to_ioreq_server(xc_interface *xch, domid_t domid,
                                            (uint64_t)device,
                                            (uint64_t)function);
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_map_io_range_to_ioreq_server,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
     return rc;
@@ -1589,7 +1565,6 @@ int xc_hvm_unmap_pcidev_from_ioreq_server(xc_interface *xch, domid_t domid,
                                           uint8_t bus, uint8_t device,
                                           uint8_t function)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_io_range_t, arg);
     int rc;
 
@@ -1602,10 +1577,6 @@ int xc_hvm_unmap_pcidev_from_ioreq_server(xc_interface *xch, domid_t domid,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_unmap_io_range_from_ioreq_server;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
     arg->type = HVMOP_IO_RANGE_PCI;
@@ -1614,7 +1585,9 @@ int xc_hvm_unmap_pcidev_from_ioreq_server(xc_interface *xch, domid_t domid,
                                            (uint64_t)device,
                                            (uint64_t)function);
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_unmap_io_range_from_ioreq_server,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
     return rc;
@@ -1624,7 +1597,6 @@ int xc_hvm_destroy_ioreq_server(xc_interface *xch,
                                 domid_t domid,
                                 ioservid_t id)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_destroy_ioreq_server_t, arg);
     int rc;
 
@@ -1632,14 +1604,12 @@ int xc_hvm_destroy_ioreq_server(xc_interface *xch,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_destroy_ioreq_server;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_destroy_ioreq_server,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
     return rc;
@@ -1650,7 +1620,6 @@ int xc_hvm_set_ioreq_server_state(xc_interface *xch,
                                   ioservid_t id,
                                   int enabled)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_hvm_set_ioreq_server_state_t, arg);
     int rc;
 
@@ -1658,15 +1627,13 @@ int xc_hvm_set_ioreq_server_state(xc_interface *xch,
     if ( arg == NULL )
         return -1;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_set_ioreq_server_state;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = domid;
     arg->id = id;
     arg->enabled = !!enabled;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_set_ioreq_server_state,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
     return rc;
diff --git a/tools/libxc/xc_evtchn.c b/tools/libxc/xc_evtchn.c
index ae2fe1a..53f7605 100644
--- a/tools/libxc/xc_evtchn.c
+++ b/tools/libxc/xc_evtchn.c
@@ -25,7 +25,6 @@ static int do_evtchn_op(xc_interface *xch, int cmd, void *arg,
                         size_t arg_size, int silently_fail)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(arg, arg_size, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     if ( xc_hypercall_bounce_pre(xch, arg) )
@@ -34,11 +33,9 @@ static int do_evtchn_op(xc_interface *xch, int cmd, void *arg,
         goto out;
     }
 
-    hypercall.op     = __HYPERVISOR_event_channel_op;
-    hypercall.arg[0] = cmd;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
-    if ((ret = do_xen_hypercall(xch, &hypercall)) < 0 && !silently_fail)
+    ret = xencall2(xch->xcall, __HYPERVISOR_event_channel_op,
+                   cmd, HYPERCALL_BUFFER_AS_ARG(arg));
+    if ( ret < 0 && !silently_fail )
         ERROR("do_evtchn_op: HYPERVISOR_event_channel_op failed: %d", ret);
 
     xc_hypercall_bounce_post(xch, arg);
diff --git a/tools/libxc/xc_flask.c b/tools/libxc/xc_flask.c
index b533656..ec52b0f 100644
--- a/tools/libxc/xc_flask.c
+++ b/tools/libxc/xc_flask.c
@@ -37,7 +37,6 @@
 int xc_flask_op(xc_interface *xch, xen_flask_op_t *op)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(op, sizeof(*op), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     op->interface_version = XEN_FLASK_INTERFACE_VERSION;
@@ -48,10 +47,9 @@ int xc_flask_op(xc_interface *xch, xen_flask_op_t *op)
         goto out;
     }
 
-    hypercall.op     = __HYPERVISOR_xsm_op;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(op);
-
-    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
+    ret = xencall1(xch->xcall, __HYPERVISOR_xsm_op,
+                   HYPERCALL_BUFFER_AS_ARG(op));
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             fprintf(stderr, "XSM operation failed!\n");
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c
index f6a2ccd..6b440ee 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libxc/xc_freebsd_osdep.c
@@ -88,53 +88,6 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-/*------------------------ Privcmd hypercall interface -----------------------*/
-void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
-{
-    size_t size = npages * XC_PAGE_SIZE;
-    void *p;
-
-    /* Address returned by mmap is page aligned. */
-    p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS,
-             -1, 0);
-    if (p == NULL)
-        return NULL;
-
-    /*
-     * Since FreeBSD doesn't have the MAP_LOCKED flag,
-     * lock memory using mlock.
-     */
-    if ( mlock(p, size) < 0 )
-    {
-        munmap(p, size);
-        return NULL;
-    }
-
-    return p;
-}
-
-void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
-{
-
-    int saved_errno = errno;
-    /* Unlock pages */
-    munlock(ptr, npages * XC_PAGE_SIZE);
-
-    munmap(ptr, npages * XC_PAGE_SIZE);
-    /* We MUST propagate the hypercall errno, not unmap call's. */
-    errno = saved_errno;
-}
-
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
-{
-    int fd = xch->privcmdfd;
-    int ret;
-
-    ret = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
-
-    return (ret == 0) ? hypercall->retval : ret;
-}
-
 /*----------------------- Privcmd foreign map interface ----------------------*/
 void *xc_map_foreign_bulk(xc_interface *xch,
                           uint32_t dom, int prot,
diff --git a/tools/libxc/xc_gnttab.c b/tools/libxc/xc_gnttab.c
index dd32aa2..af53fac 100644
--- a/tools/libxc/xc_gnttab.c
+++ b/tools/libxc/xc_gnttab.c
@@ -21,7 +21,6 @@
 int xc_gnttab_op(xc_interface *xch, int cmd, void * op, int op_size, int count)
 {
     int ret = 0;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(op, count * op_size, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     if ( xc_hypercall_bounce_pre(xch, op) )
@@ -30,12 +29,8 @@ int xc_gnttab_op(xc_interface *xch, int cmd, void * op, int op_size, int count)
         goto out1;
     }
 
-    hypercall.op = __HYPERVISOR_grant_table_op;
-    hypercall.arg[0] = cmd;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(op);
-    hypercall.arg[2] = count;
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall3(xch->xcall,  __HYPERVISOR_grant_table_op,
+                   cmd, HYPERCALL_BUFFER_AS_ARG(op), count);
 
     xc_hypercall_bounce_post(xch, op);
 
diff --git a/tools/libxc/xc_hcall_buf.c b/tools/libxc/xc_hcall_buf.c
index a0e66cf..a57895b 100644
--- a/tools/libxc/xc_hcall_buf.c
+++ b/tools/libxc/xc_hcall_buf.c
@@ -17,7 +17,6 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <pthread.h>
 
 #include "xc_private.h"
 #include "xg_private.h"
@@ -28,163 +27,38 @@ xc_hypercall_buffer_t XC__HYPERCALL_BUFFER_NAME(HYPERCALL_BUFFER_NULL) = {
     HYPERCALL_BUFFER_INIT_NO_BOUNCE
 };
 
-pthread_mutex_t hypercall_buffer_cache_mutex = PTHREAD_MUTEX_INITIALIZER;
-
-static void hypercall_buffer_cache_lock(xc_interface *xch)
-{
-    int saved_errno = errno;
-    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
-        return;
-    pthread_mutex_lock(&hypercall_buffer_cache_mutex);
-    /* Ignore pthread errors. */
-    errno = saved_errno;
-}
-
-static void hypercall_buffer_cache_unlock(xc_interface *xch)
-{
-    int saved_errno = errno;
-    if ( xch->flags & XC_OPENFLAG_NON_REENTRANT )
-        return;
-    pthread_mutex_unlock(&hypercall_buffer_cache_mutex);
-    /* Ignore pthread errors. */
-    errno = saved_errno;
-}
-
-static void *hypercall_buffer_cache_alloc(xc_interface *xch, int nr_pages)
-{
-    void *p = NULL;
-
-    hypercall_buffer_cache_lock(xch);
-
-    xch->hypercall_buffer_total_allocations++;
-    xch->hypercall_buffer_current_allocations++;
-    if ( xch->hypercall_buffer_current_allocations > xch->hypercall_buffer_maximum_allocations )
-        xch->hypercall_buffer_maximum_allocations = xch->hypercall_buffer_current_allocations;
-
-    if ( nr_pages > 1 )
-    {
-        xch->hypercall_buffer_cache_toobig++;
-    }
-    else if ( xch->hypercall_buffer_cache_nr > 0 )
-    {
-        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
-        xch->hypercall_buffer_cache_hits++;
-    }
-    else
-    {
-        xch->hypercall_buffer_cache_misses++;
-    }
-
-    hypercall_buffer_cache_unlock(xch);
-
-    return p;
-}
-
-static int hypercall_buffer_cache_free(xc_interface *xch, void *p, int nr_pages)
-{
-    int rc = 0;
-
-    hypercall_buffer_cache_lock(xch);
-
-    xch->hypercall_buffer_total_releases++;
-    xch->hypercall_buffer_current_allocations--;
-
-    if ( nr_pages == 1 && xch->hypercall_buffer_cache_nr < HYPERCALL_BUFFER_CACHE_SIZE )
-    {
-        xch->hypercall_buffer_cache[xch->hypercall_buffer_cache_nr++] = p;
-        rc = 1;
-    }
-
-    hypercall_buffer_cache_unlock(xch);
-
-    return rc;
-}
-
-void xc__hypercall_buffer_cache_release(xc_interface *xch)
-{
-    void *p;
-
-    hypercall_buffer_cache_lock(xch);
-
-    DBGPRINTF("hypercall buffer: total allocations:%d total releases:%d",
-              xch->hypercall_buffer_total_allocations,
-              xch->hypercall_buffer_total_releases);
-    DBGPRINTF("hypercall buffer: current allocations:%d maximum allocations:%d",
-              xch->hypercall_buffer_current_allocations,
-              xch->hypercall_buffer_maximum_allocations);
-    DBGPRINTF("hypercall buffer: cache current size:%d",
-              xch->hypercall_buffer_cache_nr);
-    DBGPRINTF("hypercall buffer: cache hits:%d misses:%d toobig:%d",
-              xch->hypercall_buffer_cache_hits,
-              xch->hypercall_buffer_cache_misses,
-              xch->hypercall_buffer_cache_toobig);
-
-    while ( xch->hypercall_buffer_cache_nr > 0 )
-    {
-        p = xch->hypercall_buffer_cache[--xch->hypercall_buffer_cache_nr];
-        osdep_free_hypercall_buffer(xch, p, 1);
-    }
-
-    hypercall_buffer_cache_unlock(xch);
-}
-
 void *xc__hypercall_buffer_alloc_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
 {
-    void *p = hypercall_buffer_cache_alloc(xch, nr_pages);
-
-    if ( !p )
-        p = osdep_alloc_hypercall_buffer(xch, nr_pages);
+    void *p = xencall_alloc_buffer_pages(xch->xcall, nr_pages);
 
     if (!p)
         return NULL;
 
     b->hbuf = p;
 
-    memset(p, 0, nr_pages * PAGE_SIZE);
-
     return b->hbuf;
 }
 
 void xc__hypercall_buffer_free_pages(xc_interface *xch, xc_hypercall_buffer_t *b, int nr_pages)
 {
-    if ( b->hbuf == NULL )
-        return;
-
-    if ( !hypercall_buffer_cache_free(xch, b->hbuf, nr_pages) )
-        osdep_free_hypercall_buffer(xch, b->hbuf, nr_pages);
+    xencall_free_buffer_pages(xch->xcall, b->hbuf, nr_pages);
 }
 
-struct allocation_header {
-    int nr_pages;
-};
-
 void *xc__hypercall_buffer_alloc(xc_interface *xch, xc_hypercall_buffer_t *b, size_t size)
 {
-    size_t actual_size = ROUNDUP(size + sizeof(struct allocation_header), PAGE_SHIFT);
-    int nr_pages = actual_size >> PAGE_SHIFT;
-    struct allocation_header *hdr;
+    void *p = xencall_alloc_buffer(xch->xcall, size);
 
-    hdr = xc__hypercall_buffer_alloc_pages(xch, b, nr_pages);
-    if ( hdr == NULL )
+    if (!p)
         return NULL;
 
-    b->hbuf = (void *)(hdr+1);
+    b->hbuf = p;
 
-    hdr->nr_pages = nr_pages;
     return b->hbuf;
 }
 
 void xc__hypercall_buffer_free(xc_interface *xch, xc_hypercall_buffer_t *b)
 {
-    struct allocation_header *hdr;
-
-    if (b->hbuf == NULL)
-        return;
-
-    hdr = b->hbuf;
-    b->hbuf = --hdr;
-
-    xc__hypercall_buffer_free_pages(xch, b, hdr->nr_pages);
+    xencall_free_buffer(xch->xcall, b->hbuf);
 }
 
 int xc__hypercall_bounce_pre(xc_interface *xch, xc_hypercall_buffer_t *b)
diff --git a/tools/libxc/xc_kexec.c b/tools/libxc/xc_kexec.c
index a49cffb..1cceb5d 100644
--- a/tools/libxc/xc_kexec.c
+++ b/tools/libxc/xc_kexec.c
@@ -14,7 +14,6 @@
 
 int xc_kexec_exec(xc_interface *xch, int type)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_kexec_exec_t, exec);
     int ret = -1;
 
@@ -27,11 +26,9 @@ int xc_kexec_exec(xc_interface *xch, int type)
 
     exec->type = type;
 
-    hypercall.op = __HYPERVISOR_kexec_op;
-    hypercall.arg[0] = KEXEC_CMD_kexec;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(exec);
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_kexec_op,
+		   KEXEC_CMD_kexec,
+		   HYPERCALL_BUFFER_AS_ARG(exec));
 
 out:
     xc_hypercall_buffer_free(xch, exec);
@@ -42,7 +39,6 @@ out:
 int xc_kexec_get_range(xc_interface *xch, int range,  int nr,
                        uint64_t *size, uint64_t *start)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_kexec_range_t, get_range);
     int ret = -1;
 
@@ -56,11 +52,9 @@ int xc_kexec_get_range(xc_interface *xch, int range,  int nr,
     get_range->range = range;
     get_range->nr = nr;
 
-    hypercall.op = __HYPERVISOR_kexec_op;
-    hypercall.arg[0] = KEXEC_CMD_kexec_get_range;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(get_range);
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_kexec_op,
+		   KEXEC_CMD_kexec_get_range,
+		   HYPERCALL_BUFFER_AS_ARG(get_range));
 
     *size = get_range->size;
     *start = get_range->start;
@@ -76,7 +70,6 @@ int xc_kexec_load(xc_interface *xch, uint8_t type, uint16_t arch,
                   uint32_t nr_segments, xen_kexec_segment_t *segments)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(segments, sizeof(*segments) * nr_segments,
                              XC_HYPERCALL_BUFFER_BOUNCE_IN);
     DECLARE_HYPERCALL_BUFFER(xen_kexec_load_t, load);
@@ -99,11 +92,9 @@ int xc_kexec_load(xc_interface *xch, uint8_t type, uint16_t arch,
     load->nr_segments = nr_segments;
     set_xen_guest_handle(load->segments.h, segments);
 
-    hypercall.op = __HYPERVISOR_kexec_op;
-    hypercall.arg[0] = KEXEC_CMD_kexec_load;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(load);
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_kexec_op,
+		   KEXEC_CMD_kexec_load,
+		   HYPERCALL_BUFFER_AS_ARG(load));
 
 out:
     xc_hypercall_buffer_free(xch, load);
@@ -114,7 +105,6 @@ out:
 
 int xc_kexec_unload(xc_interface *xch, int type)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(xen_kexec_unload_t, unload);
     int ret = -1;
 
@@ -127,11 +117,9 @@ int xc_kexec_unload(xc_interface *xch, int type)
 
     unload->type = type;
 
-    hypercall.op = __HYPERVISOR_kexec_op;
-    hypercall.arg[0] = KEXEC_CMD_kexec_unload;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(unload);
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_kexec_op,
+		   KEXEC_CMD_kexec_unload,
+		   HYPERCALL_BUFFER_AS_ARG(unload));
 
 out:
     xc_hypercall_buffer_free(xch, unload);
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 9f4c707..e68c495 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -91,55 +91,6 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
-{
-    size_t size = npages * XC_PAGE_SIZE;
-    void *p;
-    int rc, saved_errno;
-
-    /* Address returned by mmap is page aligned. */
-    p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
-    if ( p == MAP_FAILED )
-    {
-        PERROR("xc_alloc_hypercall_buffer: mmap failed");
-        return NULL;
-    }
-
-    /* Do not copy the VMA to child process on fork. Avoid the page being COW
-        on hypercall. */
-    rc = madvise(p, npages * XC_PAGE_SIZE, MADV_DONTFORK);
-    if ( rc < 0 )
-    {
-        PERROR("xc_alloc_hypercall_buffer: madvise failed");
-        goto out;
-    }
-
-    return p;
-
-out:
-    saved_errno = errno;
-    (void)munmap(p, size);
-    errno = saved_errno;
-    return NULL;
-}
-
-void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
-{
-    int saved_errno = errno;
-    /* Recover the VMA flags. Maybe it's not necessary */
-    madvise(ptr, npages * XC_PAGE_SIZE, MADV_DOFORK);
-
-    munmap(ptr, npages * XC_PAGE_SIZE);
-    /* We MUST propagate the hypercall errno, not unmap call's. */
-    errno = saved_errno;
-}
-
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
-{
-    int fd = xch->privcmdfd;
-    return ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
-}
-
 static int xc_map_foreign_batch_single(int fd, uint32_t dom,
                                        xen_pfn_t *mfn, unsigned long addr)
 {
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index 047e13b..e3c8241 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -61,38 +61,6 @@ void minios_interface_close_fd(int fd)
     files[fd].type = FTYPE_NONE;
 }
 
-void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
-{
-    return xc_memalign(xch, PAGE_SIZE, npages * PAGE_SIZE);
-}
-
-void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
-{
-    free(ptr);
-}
-
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
-{
-    multicall_entry_t call;
-    int i, ret;
-
-    call.op = hypercall->op;
-    for (i = 0; i < ARRAY_SIZE(hypercall->arg); i++)
-	call.args[i] = hypercall->arg[i];
-
-    ret = HYPERVISOR_multicall(&call, 1);
-
-    if (ret < 0) {
-	errno = -ret;
-	return -1;
-    }
-    if ((long) call.result < 0) {
-        errno = - (long) call.result;
-        return -1;
-    }
-    return call.result;
-}
-
 void *xc_map_foreign_bulk(xc_interface *xch,
                           uint32_t dom, int prot,
                           const xen_pfn_t *arr, int *err, unsigned int num)
diff --git a/tools/libxc/xc_misc.c b/tools/libxc/xc_misc.c
index c613545..124537b 100644
--- a/tools/libxc/xc_misc.c
+++ b/tools/libxc/xc_misc.c
@@ -323,7 +323,6 @@ int xc_sched_id(xc_interface *xch,
 int xc_mca_op(xc_interface *xch, struct xen_mc *mc)
 {
     int ret = 0;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(mc, sizeof(*mc), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     if ( xc_hypercall_bounce_pre(xch, mc) )
@@ -333,9 +332,9 @@ int xc_mca_op(xc_interface *xch, struct xen_mc *mc)
     }
     mc->interface_version = XEN_MCA_INTERFACE_VERSION;
 
-    hypercall.op = __HYPERVISOR_mca;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(mc);
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall1(xch->xcall, __HYPERVISOR_mca,
+                   HYPERCALL_BUFFER_AS_ARG(mc));
+
     xc_hypercall_bounce_post(xch, mc);
     return ret;
 }
@@ -471,7 +470,6 @@ int xc_hvm_set_pci_intx_level(
     uint8_t domain, uint8_t bus, uint8_t device, uint8_t intx,
     unsigned int level)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_set_pci_intx_level, arg);
     int rc;
 
@@ -482,10 +480,6 @@ int xc_hvm_set_pci_intx_level(
         return -1;
     }
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_set_pci_intx_level;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid  = dom;
     arg->domain = domain;
     arg->bus    = bus;
@@ -493,7 +487,9 @@ int xc_hvm_set_pci_intx_level(
     arg->intx   = intx;
     arg->level  = level;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_set_pci_intx_level,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -505,7 +501,6 @@ int xc_hvm_set_isa_irq_level(
     uint8_t isa_irq,
     unsigned int level)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_set_isa_irq_level, arg);
     int rc;
 
@@ -516,15 +511,13 @@ int xc_hvm_set_isa_irq_level(
         return -1;
     }
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_set_isa_irq_level;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid   = dom;
     arg->isa_irq = isa_irq;
     arg->level   = level;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_set_isa_irq_level,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -534,7 +527,6 @@ int xc_hvm_set_isa_irq_level(
 int xc_hvm_set_pci_link_route(
     xc_interface *xch, domid_t dom, uint8_t link, uint8_t isa_irq)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_set_pci_link_route, arg);
     int rc;
 
@@ -545,15 +537,13 @@ int xc_hvm_set_pci_link_route(
         return -1;
     }
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_set_pci_link_route;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid   = dom;
     arg->link    = link;
     arg->isa_irq = isa_irq;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_set_pci_link_route,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -563,7 +553,6 @@ int xc_hvm_set_pci_link_route(
 int xc_hvm_inject_msi(
     xc_interface *xch, domid_t dom, uint64_t addr, uint32_t data)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_inject_msi, arg);
     int rc;
 
@@ -574,15 +563,13 @@ int xc_hvm_inject_msi(
         return -1;
     }
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_inject_msi;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid = dom;
     arg->addr  = addr;
     arg->data  = data;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_inject_msi,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -594,7 +581,6 @@ int xc_hvm_track_dirty_vram(
     uint64_t first_pfn, uint64_t nr,
     unsigned long *dirty_bitmap)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(dirty_bitmap, (nr+7) / 8, XC_HYPERCALL_BUFFER_BOUNCE_OUT);
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_track_dirty_vram, arg);
     int rc;
@@ -607,16 +593,14 @@ int xc_hvm_track_dirty_vram(
         goto out;
     }
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_track_dirty_vram;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid     = dom;
     arg->first_pfn = first_pfn;
     arg->nr        = nr;
     set_xen_guest_handle(arg->dirty_bitmap, dirty_bitmap);
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_track_dirty_vram,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
 out:
     xc_hypercall_buffer_free(xch, arg);
@@ -627,7 +611,6 @@ out:
 int xc_hvm_modified_memory(
     xc_interface *xch, domid_t dom, uint64_t first_pfn, uint64_t nr)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_modified_memory, arg);
     int rc;
 
@@ -638,15 +621,13 @@ int xc_hvm_modified_memory(
         return -1;
     }
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_modified_memory;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
     arg->domid     = dom;
     arg->first_pfn = first_pfn;
     arg->nr        = nr;
 
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_modified_memory,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -656,7 +637,6 @@ int xc_hvm_modified_memory(
 int xc_hvm_set_mem_type(
     xc_interface *xch, domid_t dom, hvmmem_type_t mem_type, uint64_t first_pfn, uint64_t nr)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_set_mem_type, arg);
     int rc;
 
@@ -672,11 +652,9 @@ int xc_hvm_set_mem_type(
     arg->first_pfn    = first_pfn;
     arg->nr           = nr;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_set_mem_type;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_set_mem_type,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
@@ -688,7 +666,6 @@ int xc_hvm_inject_trap(
     uint32_t type, uint32_t error_code, uint32_t insn_len,
     uint64_t cr2)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER(struct xen_hvm_inject_trap, arg);
     int rc;
 
@@ -707,11 +684,9 @@ int xc_hvm_inject_trap(
     arg->insn_len    = insn_len;
     arg->cr2         = cr2;
 
-    hypercall.op     = __HYPERVISOR_hvm_op;
-    hypercall.arg[0] = HVMOP_inject_trap;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
-    rc = do_xen_hypercall(xch, &hypercall);
+    rc = xencall2(xch->xcall, __HYPERVISOR_hvm_op,
+                  HVMOP_inject_trap,
+                  HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_buffer_free(xch, arg);
 
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index 9abb3b6..5e3b343 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -67,46 +67,6 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
-{
-    size_t size = npages * XC_PAGE_SIZE;
-    void *p;
-
-    p = xc_memalign(xch, XC_PAGE_SIZE, size);
-    if (!p)
-        return NULL;
-
-    if ( mlock(p, size) < 0 )
-    {
-        free(p);
-        return NULL;
-    }
-    return p;
-}
-
-void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
-{
-    (void) munlock(ptr, npages * XC_PAGE_SIZE);
-    free(ptr);
-}
-
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
-{
-    int fd = xch->privcmdfd;
-    int error = ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
-
-    /*
-     * Since NetBSD ioctl can only return 0 on success or < 0 on
-     * error, if we want to return a value from ioctl we should
-     * do so by setting hypercall->retval, to mimic Linux ioctl
-     * implementation.
-     */
-    if (error < 0)
-        return error;
-    else
-        return hypercall->retval;
-}
-
 void *xc_map_foreign_bulk(xc_interface *xch,
                           uint32_t dom, int prot,
                           const xen_pfn_t *arr, int *err, unsigned int num)
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index de5873e..ca4c17a 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -39,16 +39,6 @@ struct xc_interface_core *xc_interface_open(xentoollog_logger *logger,
     xch->error_handler   = logger;           xch->error_handler_tofree   = 0;
     xch->dombuild_logger = dombuild_logger;  xch->dombuild_logger_tofree = 0;
 
-    xch->hypercall_buffer_cache_nr = 0;
-
-    xch->hypercall_buffer_total_allocations = 0;
-    xch->hypercall_buffer_total_releases = 0;
-    xch->hypercall_buffer_current_allocations = 0;
-    xch->hypercall_buffer_maximum_allocations = 0;
-    xch->hypercall_buffer_cache_hits = 0;
-    xch->hypercall_buffer_cache_misses = 0;
-    xch->hypercall_buffer_cache_toobig = 0;
-
     if (!xch->error_handler) {
         xch->error_handler = xch->error_handler_tofree =
             (xentoollog_logger*)
@@ -65,14 +55,22 @@ struct xc_interface_core *xc_interface_open(xentoollog_logger *logger,
     }
     *xch = xch_buf;
 
-    if (!(open_flags & XC_OPENFLAG_DUMMY)) {
-        if ( osdep_privcmd_open(xch) < 0 )
-            goto err;
-    }
+    if (open_flags & XC_OPENFLAG_DUMMY)
+        return xch; /* We are done */
+
+    if ( osdep_privcmd_open(xch) < 0 )
+        goto err;
+
+    xch->xcall = xencall_open(xch->error_handler,
+        open_flags & XC_OPENFLAG_NON_REENTRANT ? XENCALL_OPENFLAG_NON_REENTRANT : 0U);
+
+    if ( xch->xcall == NULL )
+        goto err;
 
     return xch;
 
  err:
+    osdep_privcmd_close(xch);
     xtl_logger_destroy(xch->error_handler_tofree);
     if (xch != &xch_buf) free(xch);
     return NULL;
@@ -85,11 +83,12 @@ int xc_interface_close(xc_interface *xch)
     if (!xch)
         return 0;
 
+    rc = xencall_close(xch->xcall);
+    if (rc) PERROR("Could not close xencall interface");
+
     rc = osdep_privcmd_close(xch);
     if (rc) PERROR("Could not close hypervisor interface");
 
-    xc__hypercall_buffer_cache_release(xch);
-
     xtl_logger_destroy(xch->dombuild_logger_tofree);
     xtl_logger_destroy(xch->error_handler_tofree);
 
@@ -228,7 +227,6 @@ int xc_mmuext_op(
     unsigned int nr_ops,
     domid_t dom)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(op, nr_ops*sizeof(*op), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
     long ret = -1;
 
@@ -238,13 +236,9 @@ int xc_mmuext_op(
         goto out1;
     }
 
-    hypercall.op     = __HYPERVISOR_mmuext_op;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(op);
-    hypercall.arg[1] = (unsigned long)nr_ops;
-    hypercall.arg[2] = (unsigned long)0;
-    hypercall.arg[3] = (unsigned long)dom;
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall4(xch->xcall, __HYPERVISOR_mmuext_op,
+                   HYPERCALL_BUFFER_AS_ARG(op),
+                   nr_ops, 0, dom);
 
     xc_hypercall_bounce_post(xch, op);
 
@@ -254,8 +248,7 @@ int xc_mmuext_op(
 
 static int flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu)
 {
-    int err = 0;
-    DECLARE_HYPERCALL;
+    int rc, err = 0;
     DECLARE_NAMED_HYPERCALL_BOUNCE(updates, mmu->updates, mmu->idx*sizeof(*mmu->updates), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     if ( mmu->idx == 0 )
@@ -268,13 +261,10 @@ static int flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu)
         goto out;
     }
 
-    hypercall.op     = __HYPERVISOR_mmu_update;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(updates);
-    hypercall.arg[1] = (unsigned long)mmu->idx;
-    hypercall.arg[2] = 0;
-    hypercall.arg[3] = mmu->subject;
-
-    if ( do_xen_hypercall(xch, &hypercall) < 0 )
+    rc = xencall4(xch->xcall, __HYPERVISOR_mmu_update,
+                  HYPERCALL_BUFFER_AS_ARG(updates),
+                  mmu->idx, 0, mmu->subject);
+    if ( rc < 0 )
     {
         ERROR("Failure when submitting mmu updates");
         err = 1;
@@ -317,7 +307,6 @@ int xc_flush_mmu_updates(xc_interface *xch, struct xc_mmu *mmu)
 
 long do_memory_op(xc_interface *xch, int cmd, void *arg, size_t len)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(arg, len, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
     long ret = -1;
 
@@ -327,11 +316,8 @@ long do_memory_op(xc_interface *xch, int cmd, void *arg, size_t len)
         goto out1;
     }
 
-    hypercall.op     = __HYPERVISOR_memory_op;
-    hypercall.arg[0] = (unsigned long) cmd;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(arg);
-
-    ret = do_xen_hypercall(xch, &hypercall);
+    ret = xencall2(xch->xcall, __HYPERVISOR_memory_op,
+                   cmd, HYPERCALL_BUFFER_AS_ARG(arg));
 
     xc_hypercall_bounce_post(xch, arg);
  out1:
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index 30157cf..c93df7f 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -30,6 +30,7 @@
 
 #include "_paths.h"
 #include "xenctrl.h"
+#include <xencall.h>
 
 #include <xen/sys/privcmd.h>
 
@@ -53,7 +54,6 @@ struct iovec {
 #include <sys/uio.h>
 #endif
 
-#define DECLARE_HYPERCALL privcmd_hypercall_t hypercall
 #define DECLARE_DOMCTL struct xen_domctl domctl
 #define DECLARE_SYSCTL struct xen_sysctl sysctl
 #define DECLARE_PHYSDEV_OP struct physdev_op physdev_op
@@ -94,29 +94,11 @@ struct xc_interface_core {
     FILE *dombuild_logger_file;
     const char *currently_progress_reporting;
 
-    /*
-     * A simple cache of unused, single page, hypercall buffers
-     *
-     * Protected by a global lock.
-     */
-#define HYPERCALL_BUFFER_CACHE_SIZE 4
-    int hypercall_buffer_cache_nr;
-    void *hypercall_buffer_cache[HYPERCALL_BUFFER_CACHE_SIZE];
-
-    /*
-     * Hypercall buffer statistics. All protected by the global
-     * hypercall_buffer_cache lock.
-     */
-    int hypercall_buffer_total_allocations;
-    int hypercall_buffer_total_releases;
-    int hypercall_buffer_current_allocations;
-    int hypercall_buffer_maximum_allocations;
-    int hypercall_buffer_cache_hits;
-    int hypercall_buffer_cache_misses;
-    int hypercall_buffer_cache_toobig;
-
     /* Privcmd interface */
     int privcmdfd;
+
+    /* Hypercall interface */
+    xencall_handle *xcall;
 };
 
 int osdep_privcmd_open(xc_interface *xch);
@@ -232,24 +214,16 @@ void xc__hypercall_buffer_cache_release(xc_interface *xch);
  * Hypercall interfaces.
  */
 
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall);
-
 static inline int do_xen_version(xc_interface *xch, int cmd, xc_hypercall_buffer_t *dest)
 {
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER_ARGUMENT(dest);
-
-    hypercall.op     = __HYPERVISOR_xen_version;
-    hypercall.arg[0] = (unsigned long) cmd;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(dest);
-
-    return do_xen_hypercall(xch, &hypercall);
+    return xencall2(xch->xcall, __HYPERVISOR_xen_version,
+                    cmd, HYPERCALL_BUFFER_AS_ARG(dest));
 }
 
 static inline int do_physdev_op(xc_interface *xch, int cmd, void *op, size_t len)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(op, len, XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     if ( xc_hypercall_bounce_pre(xch, op) )
@@ -258,11 +232,9 @@ static inline int do_physdev_op(xc_interface *xch, int cmd, void *op, size_t len
         goto out1;
     }
 
-    hypercall.op = __HYPERVISOR_physdev_op;
-    hypercall.arg[0] = (unsigned long) cmd;
-    hypercall.arg[1] = HYPERCALL_BUFFER_AS_ARG(op);
-
-    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
+    ret = xencall2(xch->xcall, __HYPERVISOR_physdev_op,
+                   cmd, HYPERCALL_BUFFER_AS_ARG(op));
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             DPRINTF("physdev operation failed -- need to"
@@ -277,7 +249,6 @@ out1:
 static inline int do_domctl(xc_interface *xch, struct xen_domctl *domctl)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(domctl, sizeof(*domctl), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     domctl->interface_version = XEN_DOMCTL_INTERFACE_VERSION;
@@ -288,10 +259,9 @@ static inline int do_domctl(xc_interface *xch, struct xen_domctl *domctl)
         goto out1;
     }
 
-    hypercall.op     = __HYPERVISOR_domctl;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(domctl);
-
-    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
+    ret = xencall1(xch->xcall, __HYPERVISOR_domctl,
+                   HYPERCALL_BUFFER_AS_ARG(domctl));
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             DPRINTF("domctl operation failed -- need to"
@@ -306,7 +276,6 @@ static inline int do_domctl(xc_interface *xch, struct xen_domctl *domctl)
 static inline int do_sysctl(xc_interface *xch, struct xen_sysctl *sysctl)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(sysctl, sizeof(*sysctl), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     sysctl->interface_version = XEN_SYSCTL_INTERFACE_VERSION;
@@ -317,9 +286,9 @@ static inline int do_sysctl(xc_interface *xch, struct xen_sysctl *sysctl)
         goto out1;
     }
 
-    hypercall.op     = __HYPERVISOR_sysctl;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(sysctl);
-    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
+    ret = xencall1(xch->xcall, __HYPERVISOR_sysctl,
+                   HYPERCALL_BUFFER_AS_ARG(sysctl));
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             DPRINTF("sysctl operation failed -- need to"
@@ -335,7 +304,6 @@ static inline int do_platform_op(xc_interface *xch,
                                  struct xen_platform_op *platform_op)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(platform_op, sizeof(*platform_op),
                              XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
@@ -347,9 +315,9 @@ static inline int do_platform_op(xc_interface *xch,
         return -1;
     }
 
-    hypercall.op     = __HYPERVISOR_platform_op;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(platform_op);
-    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
+    ret = xencall1(xch->xcall, __HYPERVISOR_platform_op,
+                   HYPERCALL_BUFFER_AS_ARG(platform_op));
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             DPRINTF("platform operation failed -- need to"
@@ -365,13 +333,11 @@ static inline int do_multicall_op(xc_interface *xch,
                                   uint32_t nr_calls)
 {
     int ret = -1;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BUFFER_ARGUMENT(call_list);
 
-    hypercall.op     = __HYPERVISOR_multicall;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(call_list);
-    hypercall.arg[1] = nr_calls;
-    if ( (ret = do_xen_hypercall(xch, &hypercall)) < 0 )
+    ret = xencall2(xch->xcall, __HYPERVISOR_multicall,
+                   HYPERCALL_BUFFER_AS_ARG(call_list), nr_calls);
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             DPRINTF("multicall operation failed -- need to"
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 6f84b82..18622fa 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -67,22 +67,6 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages)
-{
-    return xc_memalign(xch, XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
-}
-
-static void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages)
-{
-    free(ptr);
-}
-
-int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
-{
-    int fd = xch->privcmdfd;
-    return ioctl(fd, IOCTL_PRIVCMD_HYPERCALL, hypercall);
-}
-
 void *xc_map_foreign_batch(xc_interface *xch,
                           uint32_t dom, int prot,
                           xen_pfn_t *arr, int num)
diff --git a/tools/libxc/xc_tmem.c b/tools/libxc/xc_tmem.c
index 8f4c0cc..4e5c278 100644
--- a/tools/libxc/xc_tmem.c
+++ b/tools/libxc/xc_tmem.c
@@ -23,7 +23,6 @@
 static int do_tmem_op(xc_interface *xch, tmem_op_t *op)
 {
     int ret;
-    DECLARE_HYPERCALL;
     DECLARE_HYPERCALL_BOUNCE(op, sizeof(*op), XC_HYPERCALL_BUFFER_BOUNCE_BOTH);
 
     if ( xc_hypercall_bounce_pre(xch, op) )
@@ -32,9 +31,9 @@ static int do_tmem_op(xc_interface *xch, tmem_op_t *op)
         return -EFAULT;
     }
 
-    hypercall.op = __HYPERVISOR_tmem_op;
-    hypercall.arg[0] = HYPERCALL_BUFFER_AS_ARG(op);
-    if ((ret = do_xen_hypercall(xch, &hypercall)) < 0)
+    ret = xencall1(xch->xcall, __HYPERVISOR_tmem_op,
+                   HYPERCALL_BUFFER_AS_ARG(op));
+    if ( ret < 0 )
     {
         if ( errno == EACCES )
             DPRINTF("tmem operation failed -- need to"
diff --git a/tools/misc/Makefile b/tools/misc/Makefile
index cf6a475..a2ef0ec 100644
--- a/tools/misc/Makefile
+++ b/tools/misc/Makefile
@@ -87,12 +87,12 @@ xenlockprof: xenlockprof.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
 
 # xen-hptool incorrectly uses libxc internals
-xen-hptool.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc
+xen-hptool.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc $(CFLAGS_libxencall)
 xen-hptool: xen-hptool.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
 
 # xen-mfndump incorrectly uses libxc internals
-xen-mfndump.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc
+xen-mfndump.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc $(CFLAGS_libxencall)
 xen-mfndump: xen-mfndump.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(APPEND_LDFLAGS)
 
diff --git a/tools/xcutils/Makefile b/tools/xcutils/Makefile
index 2d1f112..e127af8 100644
--- a/tools/xcutils/Makefile
+++ b/tools/xcutils/Makefile
@@ -16,7 +16,7 @@ PROGRAMS = readnotes lsevtchn
 CFLAGS += -Werror
 
 # incorrectly uses libxc internals
-CFLAGS_readnotes.o  := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) -I$(XEN_ROOT)/tools/libxc
+CFLAGS_readnotes.o  := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) -I$(XEN_ROOT)/tools/libxc $(CFLAGS_libxencall)
 CFLAGS_lsevtchn.o   := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl)
 
 .PHONY: all
diff --git a/tools/xenpaging/Makefile b/tools/xenpaging/Makefile
index d491867..64876b3 100644
--- a/tools/xenpaging/Makefile
+++ b/tools/xenpaging/Makefile
@@ -2,7 +2,7 @@ XEN_ROOT=$(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 # xenpaging.c and file_ops.c incorrectly use libxc internals
-CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) -I$(XEN_ROOT)/tools/libxc
+CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) -I$(XEN_ROOT)/tools/libxc $(CFLAGS_libxencall)
 LDLIBS += $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
 LDFLAGS += $(PTHREAD_LDFLAGS)
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 08/29] tools/libxc: drop xc_map_foreign_bulk_compat wrappers
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (6 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 07/29] tools: Refactor hypercall calling wrappers into libxencall Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 09/29] tools: Remove xc_map_foreign_batch Ian Campbell
                     ` (20 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

On Solaris and NetBSD xc_map_foreign_bulk is implemented by calling
xc_map_foreign_bulk_compat and xc_map_foreign_bulk_compat is exposed
as a symbol by libxenctrl.so.

Remove these wrappers and turn the compat function into the real thing
surrounded by the appropriate ifdef.

As this is a compat function all new ports should instead implement
xc_map_foreign_bulk properly, hence the ifdef should never be
expanded.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxc/xc_foreign_memory.c | 13 +++++++++----
 tools/libxc/xc_netbsd.c         |  7 -------
 tools/libxc/xc_private.h        |  5 -----
 tools/libxc/xc_solaris.c        |  7 -------
 4 files changed, 9 insertions(+), 23 deletions(-)

diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index 9c705b6..b205bca 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -50,10 +50,14 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
     return res;
 }
 
-/* stub for all not yet converted OSes */
-void *xc_map_foreign_bulk_compat(xc_interface *xch,
-                                 uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, unsigned int num)
+/*
+ * stub for all not yet converted OSes (NetBSD and Solaris). New OSes should
+ * just implement xc_map_foreign_bulk.
+ */
+#if defined(__NetBSD__) || defined(__sun__)
+void *xc_map_foreign_bulk(xc_interface *xch,
+                          uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err, unsigned int num)
 {
     xen_pfn_t *pfn;
     unsigned int i;
@@ -90,6 +94,7 @@ void *xc_map_foreign_bulk_compat(xc_interface *xch,
 
     return ret;
 }
+#endif
 
 /*
  * Local variables:
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index 5e3b343..d7f7f31 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -67,13 +67,6 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-void *xc_map_foreign_bulk(xc_interface *xch,
-                          uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err, unsigned int num)
-{
-    return xc_map_foreign_bulk_compat(xch, dom, prot, arr, err, num);
-}
-
 void *xc_map_foreign_batch(xc_interface *xch,
                            uint32_t dom, int prot,
                            xen_pfn_t *arr, int num)
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index c93df7f..ecf2451 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -107,11 +107,6 @@ int osdep_privcmd_close(xc_interface *xch);
 void *osdep_alloc_hypercall_buffer(xc_interface *xch, int npages);
 void osdep_free_hypercall_buffer(xc_interface *xch, void *ptr, int npages);
 
-/* Stub for not yet converted OSes */
-void *xc_map_foreign_bulk_compat(xc_interface *xch,
-                                 uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, unsigned int num);
-
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 18622fa..5e72dee 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -94,13 +94,6 @@ void *xc_map_foreign_batch(xc_interface *xch,
 
 }
 
-void *xc_map_foreign_bulk(xc_interface *xch,
-                          uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err, unsigned int num)
-{
-    return xc_map_foreign_bulk_compat(xch, dom, prot, arr, err, num);
-}
-
 void *xc_map_foreign_range(xc_interface *xch,
                            uint32_t dom,
                            int size, int prot,
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
@ 2016-01-15 13:22 Ian Campbell
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                   ` (8 more replies)
  0 siblings, 9 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

In <1431963008.4944.80.camel@citrix.com> I proposed stabilising some
parts of the libxenctrl API/ABI by disaggregating into separate
libraries.

This is v8 of that set of series against:
    xen
    qemu-xen
    qemu-xen-traditional
    mini-os

NB: Samuel+minios-devel will only get the mini-os side and Stefano+qemu
-devel the qemu-xen side.

The code for all repos can be found in:

git://xenbits.xen.org/people/ianc/libxenctrl-split/xen.git                  v8
git://xenbits.xen.org/people/ianc/libxenctrl-split/qemu-xen.git             v8
git://xenbits.xen.org/people/ianc/libxenctrl-split/qemu-xen-traditional.git v8
git://xenbits.xen.org/people/ianc/libxenctrl-split/mini-os.git              v8

The tip of the xen.git branch contains an extra patch hacking Config.mk
to point to all the others above, which should get the correct things for
the HEAD of the branch, but not further back in time.

The new libraries here are:

 * libxentoollog: Common logging infrastructure (already in tree)
 * libxenevtchn: Userspace access to evtchns (via /dev/xen/evtchn etc)
 * libxengnttab: Userspace access to grant tables (via /dev/xen/gnt??? etc)
 * libxencall: Making hypercalls (i.e. the IOCTL_PRIVCMD_HYPERCALL type
   functionality)
 * libxenforeignmemory: Privileged mappings of foreign memory
   (IOCTL_PRIVCMD_MMAP et al)

The first three were actually pretty distinct within libxenctrl already and
have not changed in quite some time.

Although the other two are somewhat new they are based on top of long
standing stable ioctls, which gives me some confidence.

Nonetheless I would appreciate extra review of at least the interface
headers of all of these with a particular eye to the suitability of these
interfaces being maintained in an ABI (_B_, not _P_) stable way going
forward.

Still to come would be libraries for specific out of tree purposes
(device model, kexec), which would be adding new library at the same
level as libxc I think, rather than underneath, i.e. also using the
libraries split out here, but hopefully not libxenctrl itself.

The new libraries use linker version-scripts to hopefully make future
ABI changes be possible in a compatible way.

Since last time:

 * Some early bits went in.
 * Rebased
 * Clean up the *.so in clean, added distclean targets to each lib
 * On the QEMU side use CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 as the
   gate for this new setup (dropped a Reviewed-by).

Even with the dropped acks mini-os and qemu-xen-trad are fully acked (by
Samuel+Wei and Ian J respectively), while qemu-xen and xen are mostly acked
(but had a few dropped acks since last time).

Summary for qemu-xen.git:
 R	xen_console: correctly cleanup primary console on teardown.
 R	xen: Switch to libxenevtchn interface for compat shims.
 R	xen: Switch to libxengnttab interface for compat shims.
 R	xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
  M	xen: Switch uses of xc_map_foreign_{pages,bulk} to use libxenforeignmemory API.
  M	xen: Use stable library interfaces when they are available.
A	xen: domainbuild: reopen libxenctrl interface after forking for domain watcher.
 R	xen: make it possible to build without the Xen PV domain builder

(A == Acked by Stefano, R == Reviewed by Stefano, M == Modified in v8)

NB: qemu-xen-traditional.git, mini-os.git and xen.git are intertwined, but
the qemu-xen.git part is independent and should be applied after all the
rest of these series.

Summary for xen.git:

 W	tools/libxc: Remove osdep indirection for xc_evtchn
MWI	tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
 W	tools: Arrange to check public headers for ANSI compatiblity
 W	tools/libxc: Remove osdep indirection for xc_gnt{shr,tab}
MW	tools: Refactor /dev/xen/gnt{dev,shr} wrappers into libxengnttab.
 W    S	tools/libxc: Remove osdep indirection for privcmd
MW	tools: Refactor hypercall calling wrappers into libxencall.
 W	tools/libxc: drop xc_map_foreign_bulk_compat wrappers
 W   G	tools: Remove xc_map_foreign_batch
 W	tools: Implement xc_map_foreign_range(s) in terms of common helper
MWI	tools: Refactor foreign memory mapping into libxenforeignmemory
 WI	tools/libs/foreignmemory: provide xenforeignmemory_unmap.
 WI	tools/libs/foreignmemory: use size_t for size arguments.
	tools/libs/foreignmemory: Mention restrictions on fork in docs.
 WI	tools/libs/foreignmemory: Support err == NULL to map.
 WI	tools/libs/foreignmemory: pull array length argument to map forward
 WI	tools/libs/evtchn: Review and update doc comments.
N	tools/libs/evtchn: Use uint32_t for domid arguments
 W	tools/libs: Clean up hard tabs.
    D	tools/libs/gnttab: Extensive updates to API documentation.
 W	tools/libs/call: Update some log messages to not refer to xc.
 WIR	tools/libs/call: Describe return values and error semantics for xencall*
 W	tools/libs/call: Avoid xc_memalign in netbsd and solaris backends
	tools/libs/call: linux: touch newly allocated pages after madvise lockdown
	tools/libs/{call,evtchn}: Document requirements around forking.
M  R	tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
 W	tools: Update CFLAGS for qemu-xen to allow it to use new libraries
N	tools/libs/*: Introduce APIs to restrict handles to a specific domain.

N == New in v8
W == Acked by Wei
R == Acked by Roger
I == Acked by Ian J
D == Acked by Daniel
G == Acked by George
S == Acked by Dave (Scott)
M == Modified (in all cases minor enough that I didn't drop acks)

Therefore needing attention from Ian and/or Wei are:

	tools/libs/foreignmemory: Mention restrictions on fork in docs.
N	tools/libs/evtchn: Use uint32_t for domid arguments
    D	tools/libs/gnttab: Extensive updates to API documentation.
	
tools/libs/call: linux: touch newly allocated pages after madvise l
	
tools/libs/{call,evtchn}: Document requirements around forking.
   R	
tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
N	tools/libs/*:
Introduce APIs to restrict handles to a specific doma

The whole thing has been build tested on Linux (incl stubdoms), and on
FreeBSD. I have runtime tested on Linux with qemu-xen, qemu-xen-trad and
stubdoms.

Neither NetBSD nor Solaris have been tested at all. It's certainly not
impossible that I've not got the #includes in the new files quite right.

http://xenbits.xen.org/people/ianc/libxenctrl-split/v8.html is the document
I've been using to try and track what I'm doing. It may not be all that
useful. The history of it is in the v8-with-doc branch of the xen.git
linked to above.

Ian.


_______________________________________________
Minios-devel mailing list
Minios-devel@lists.xenproject.org
http://lists.xenproject.org/cgi-bin/mailman/listinfo/minios-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 09/29] tools: Remove xc_map_foreign_batch
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (7 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 08/29] tools/libxc: drop xc_map_foreign_bulk_compat wrappers Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 10/29] tools: Implement xc_map_foreign_range(s) in terms of common helper Ian Campbell
                     ` (19 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: George Dunlap, Ian Campbell

It can trivially be replaced by xc_map_foreign_pages which is the
interface I want to move to going forward (by standardising on _bulk
but handling err=NULL as _pages does).

The callers of _batch are checking a mixture of a NULL return or
looking to see if the top nibble of the (usually sole) mfn they pass
has been modified to be non-zero to detect errors. _pages never
modifies the mfn it was given (it's const) and returns NULL on
failure, so adjust the error handling where necessary. Some callers
use a copy of the mfn array, for reuse on failure with _batch, which
is no longer necessary as _pages doesn't modify the array, however I
haven't cleaned that up here.

This reduces the twist maze of xc_map_foreign_* by one, which will be
useful when trying to come up with an underlying stable interface.

NetBSD and Solaris implemented xc_map_foreign_bulk in terms of
xc_map_foreign_batch via a compat layer, so xc_map_foreign_batch
becomes an internal osdep for them.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: George Dunlap <george.dunlap@citrix.com>
Cc: George Dunlap <george.dunlap@eu.citrix.com>
---
v7: Don't mention new ports implementing _bulk, which is confusing and
    will be obsolete a couple of patches later.
v6: Switch to xc_map_foreign_pages not xc_map_foreign_bulk, since the
    former has more similar error handling semantics to the current
    usage.

    Dropped acks.
---
 tools/libxc/include/xenctrl.h   | 10 -------
 tools/libxc/xc_foreign_memory.c |  4 ++-
 tools/libxc/xc_linux_osdep.c    | 59 +++--------------------------------------
 tools/libxc/xc_minios.c         | 22 ---------------
 tools/libxc/xc_netbsd.c         | 10 +++----
 tools/libxc/xc_solaris.c        |  6 ++---
 tools/libxc/xc_vm_event.c       | 18 ++++++++++---
 tools/xenmon/xenbaked.c         | 12 ++++++++-
 tools/xenpaging/xenpaging.c     | 14 +++++-----
 tools/xentrace/xentrace.c       |  3 ++-
 10 files changed, 48 insertions(+), 110 deletions(-)

diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 78400d3..cb41c07 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -1387,16 +1387,6 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
                            const xen_pfn_t *arr, int num );
 
 /**
- * DEPRECATED - use xc_map_foreign_bulk() instead.
- *
- * Like xc_map_foreign_pages(), except it can succeeed partially.
- * When a page cannot be mapped, its PFN in @arr is or'ed with
- * 0xF0000000 to indicate the error.
- */
-void *xc_map_foreign_batch(xc_interface *xch, uint32_t dom, int prot,
-                           xen_pfn_t *arr, int num );
-
-/**
  * Like xc_map_foreign_pages(), except it can succeed partially.
  * When a page cannot be mapped, its respective field in @err is
  * set to the corresponding errno value.
diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index b205bca..2413e75 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -55,6 +55,8 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
  * just implement xc_map_foreign_bulk.
  */
 #if defined(__NetBSD__) || defined(__sun__)
+void *osdep_map_foreign_batch(xc_interface *xch, uint32_t dom, int prot,
+                              xen_pfn_t *arr, int num );
 void *xc_map_foreign_bulk(xc_interface *xch,
                           uint32_t dom, int prot,
                           const xen_pfn_t *arr, int *err, unsigned int num)
@@ -75,7 +77,7 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     }
 
     memcpy(pfn, arr, num * sizeof(*arr));
-    ret = xc_map_foreign_batch(xch, dom, prot, pfn, num);
+    ret = osdep_map_foreign_batch(xch, dom, prot, pfn, num);
 
     if (ret) {
         for (i = 0; i < num; ++i)
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index e68c495..39c88ce 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -91,8 +91,8 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-static int xc_map_foreign_batch_single(int fd, uint32_t dom,
-                                       xen_pfn_t *mfn, unsigned long addr)
+static int map_foreign_batch_single(int fd, uint32_t dom,
+                                    xen_pfn_t *mfn, unsigned long addr)
 {
     privcmd_mmapbatch_t ioctlx;
     int rc;
@@ -113,59 +113,6 @@ static int xc_map_foreign_batch_single(int fd, uint32_t dom,
     return rc;
 }
 
-void *xc_map_foreign_batch(xc_interface *xch,
-                           uint32_t dom, int prot,
-                           xen_pfn_t *arr, int num)
-{
-    int fd = xch->privcmdfd;
-    privcmd_mmapbatch_t ioctlx;
-    void *addr;
-    int rc;
-
-    addr = mmap(NULL, num << XC_PAGE_SHIFT, prot, MAP_SHARED, fd, 0);
-    if ( addr == MAP_FAILED )
-    {
-        PERROR("xc_map_foreign_batch: mmap failed");
-        return NULL;
-    }
-
-    ioctlx.num = num;
-    ioctlx.dom = dom;
-    ioctlx.addr = (unsigned long)addr;
-    ioctlx.arr = arr;
-
-    rc = ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx);
-    if ( (rc < 0) && (errno == ENOENT) )
-    {
-        int i;
-
-        for ( i = 0; i < num; i++ )
-        {
-            if ( (arr[i] & PRIVCMD_MMAPBATCH_MFN_ERROR) ==
-                           PRIVCMD_MMAPBATCH_PAGED_ERROR )
-            {
-                unsigned long paged_addr = (unsigned long)addr + (i << XC_PAGE_SHIFT);
-                rc = xc_map_foreign_batch_single(fd, dom, &arr[i],
-                                                 paged_addr);
-                if ( rc < 0 )
-                    goto out;
-            }
-        }
-    }
-
- out:
-    if ( rc < 0 )
-    {
-        int saved_errno = errno;
-        PERROR("xc_map_foreign_batch: ioctl failed");
-        (void)munmap(addr, num << XC_PAGE_SHIFT);
-        errno = saved_errno;
-        return NULL;
-    }
-
-    return addr;
-}
-
 /*
  * Retry mmap of all paged gfns in batches
  * retuns < 0 on fatal error
@@ -305,7 +252,7 @@ void *xc_map_foreign_bulk(xc_interface *xch,
                     err[i] = rc ?: -EINVAL;
                     continue;
                 }
-                rc = xc_map_foreign_batch_single(fd, dom, pfn + i,
+                rc = map_foreign_batch_single(fd, dom, pfn + i,
                         (unsigned long)addr + ((unsigned long)i<<XC_PAGE_SHIFT));
                 if ( rc < 0 )
                 {
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index e3c8241..3ea3124 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -73,28 +73,6 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
 }
 
-void *xc_map_foreign_batch(xc_interface *xch,
-                           uint32_t dom, int prot,
-                           xen_pfn_t *arr, int num)
-{
-    unsigned long pt_prot = 0;
-    int err[num];
-    int i;
-    unsigned long addr;
-
-    if (prot & PROT_READ)
-	pt_prot = L1_PROT_RO;
-    if (prot & PROT_WRITE)
-	pt_prot = L1_PROT;
-
-    addr = (unsigned long) map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
-    for (i = 0; i < num; i++) {
-        if (err[i])
-            arr[i] |= 0xF0000000;
-    }
-    return (void *) addr;
-}
-
 void *xc_map_foreign_range(xc_interface *xch,
                            uint32_t dom,
                            int size, int prot,
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index d7f7f31..6a7ff9f 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -67,16 +67,16 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-void *xc_map_foreign_batch(xc_interface *xch,
-                           uint32_t dom, int prot,
-                           xen_pfn_t *arr, int num)
+void *osdep_map_foreign_batch(xc_interface *xch,
+                              uint32_t dom, int prot,
+                              xen_pfn_t *arr, int num)
 {
     int fd = xch->privcmdfd;
     privcmd_mmapbatch_t ioctlx;
     void *addr;
     addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_ANON | MAP_SHARED, -1, 0);
     if ( addr == MAP_FAILED ) {
-        PERROR("xc_map_foreign_batch: mmap failed");
+        PERROR("osdep_map_foreign_batch: mmap failed");
         return NULL;
     }
 
@@ -87,7 +87,7 @@ void *xc_map_foreign_batch(xc_interface *xch,
     if ( ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
     {
         int saved_errno = errno;
-        PERROR("xc_map_foreign_batch: ioctl failed");
+        PERROR("osdep_map_foreign_batch: ioctl failed");
         (void)munmap(addr, num*XC_PAGE_SIZE);
         errno = saved_errno;
         return NULL;
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 5e72dee..2df7a06 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -67,9 +67,9 @@ int osdep_privcmd_close(xc_interface *xch)
     return close(fd);
 }
 
-void *xc_map_foreign_batch(xc_interface *xch,
-                          uint32_t dom, int prot,
-                          xen_pfn_t *arr, int num)
+void *osdep_map_foreign_batch(xc_interface *xch,
+                              uint32_t dom, int prot,
+                              xen_pfn_t *arr, int num)
 {
     int fd = xch->privcmdfd;
     privcmd_mmapbatch_t ioctlx;
diff --git a/tools/libxc/xc_vm_event.c b/tools/libxc/xc_vm_event.c
index 2fef96a..d2d99e4 100644
--- a/tools/libxc/xc_vm_event.c
+++ b/tools/libxc/xc_vm_event.c
@@ -72,9 +72,9 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param,
 
     ring_pfn = pfn;
     mmap_pfn = pfn;
-    ring_page = xc_map_foreign_batch(xch, domain_id, PROT_READ | PROT_WRITE,
+    ring_page = xc_map_foreign_pages(xch, domain_id, PROT_READ | PROT_WRITE,
                                      &mmap_pfn, 1);
-    if ( mmap_pfn & XEN_DOMCTL_PFINFO_XTAB )
+    if ( !ring_page )
     {
         /* Map failed, populate ring page */
         rc1 = xc_domain_populate_physmap_exact(xch, domain_id, 1, 0, 0,
@@ -86,9 +86,9 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param,
         }
 
         mmap_pfn = ring_pfn;
-        ring_page = xc_map_foreign_batch(xch, domain_id, PROT_READ | PROT_WRITE,
+        ring_page = xc_map_foreign_pages(xch, domain_id, PROT_READ | PROT_WRITE,
                                          &mmap_pfn, 1);
-        if ( mmap_pfn & XEN_DOMCTL_PFINFO_XTAB )
+        if ( !ring_page )
         {
             PERROR("Could not map the ring page\n");
             goto out;
@@ -156,3 +156,13 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param,
 
     return ring_page;
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/xenmon/xenbaked.c b/tools/xenmon/xenbaked.c
index e4602ef..da46126 100644
--- a/tools/xenmon/xenbaked.c
+++ b/tools/xenmon/xenbaked.c
@@ -411,7 +411,7 @@ static struct t_struct *map_tbufs(unsigned long tbufs_mfn, unsigned int num,
         for ( j=0; j<tbufs.t_info->tbuf_size; j++)
             pfn_list[j] = (xen_pfn_t)mfn_list[j];
 
-        tbufs.meta[i] = xc_map_foreign_batch(xc_handle, DOMID_XEN,
+        tbufs.meta[i] = xc_map_foreign_pages(xc_handle, DOMID_XEN,
                                              PROT_READ | PROT_WRITE,
                                              pfn_list,
                                              tbufs.t_info->tbuf_size);
@@ -1175,3 +1175,13 @@ static int process_record(int cpu, struct t_rec *r)
 
     return 4 + (r->cycles_included ? 8 : 0) + (r->extra_u32 * 4);
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/xenpaging/xenpaging.c b/tools/xenpaging/xenpaging.c
index df99c6a..c4bc713 100644
--- a/tools/xenpaging/xenpaging.c
+++ b/tools/xenpaging/xenpaging.c
@@ -342,9 +342,9 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[])
                         HVM_PARAM_PAGING_RING_PFN, &ring_pfn);
     mmap_pfn = ring_pfn;
     paging->vm_event.ring_page = 
-        xc_map_foreign_batch(xch, paging->vm_event.domain_id, 
-                                PROT_READ | PROT_WRITE, &mmap_pfn, 1);
-    if ( mmap_pfn & XEN_DOMCTL_PFINFO_XTAB )
+        xc_map_foreign_pages(xch, paging->vm_event.domain_id,
+                             PROT_READ | PROT_WRITE, &mmap_pfn, 1);
+    if ( !paging->vm_event.ring_page )
     {
         /* Map failed, populate ring page */
         rc = xc_domain_populate_physmap_exact(paging->xc_handle, 
@@ -356,11 +356,11 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[])
             goto err;
         }
 
-        mmap_pfn = ring_pfn;
         paging->vm_event.ring_page = 
-            xc_map_foreign_batch(xch, paging->vm_event.domain_id, 
-                                    PROT_READ | PROT_WRITE, &mmap_pfn, 1);
-        if ( mmap_pfn & XEN_DOMCTL_PFINFO_XTAB )
+            xc_map_foreign_pages(xch, paging->vm_event.domain_id,
+                                 PROT_READ | PROT_WRITE,
+                                 &mmap_pfn, 1);
+        if ( !paging->vm_event.ring_page )
         {
             PERROR("Could not map the ring page\n");
             goto err;
diff --git a/tools/xentrace/xentrace.c b/tools/xentrace/xentrace.c
index c970d42..6cbe0ac 100644
--- a/tools/xentrace/xentrace.c
+++ b/tools/xentrace/xentrace.c
@@ -508,7 +508,7 @@ static struct t_struct *map_tbufs(unsigned long tbufs_mfn, unsigned int num,
         for ( j=0; j<tbufs.t_info->tbuf_size; j++)
             pfn_list[j] = (xen_pfn_t)mfn_list[j];
 
-        tbufs.meta[i] = xc_map_foreign_batch(xc_handle, DOMID_XEN,
+        tbufs.meta[i] = xc_map_foreign_pages(xc_handle, DOMID_XEN,
                                              PROT_READ | PROT_WRITE,
                                              pfn_list,
                                              tbufs.t_info->tbuf_size);
@@ -1221,6 +1221,7 @@ int main(int argc, char **argv)
 
     return ret;
 }
+
 /*
  * Local variables:
  * mode: C
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 10/29] tools: Implement xc_map_foreign_range(s) in terms of common helper
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (8 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 09/29] tools: Remove xc_map_foreign_batch Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory Ian Campbell
                     ` (18 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Both Linux and FreeBSD already implemented these functions using
identical helpers based on xc_map_foreign_pages. Make one copy of
these common helpers and switch all OSes to use them, even those which
previously had a specific lower level implementation of this
functionality.

This is makes two fewer low level interfaces to think about.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libxc/xc_foreign_memory.c | 50 +++++++++++++++++++++++++++++
 tools/libxc/xc_freebsd_osdep.c  | 50 -----------------------------
 tools/libxc/xc_linux_osdep.c    | 49 -----------------------------
 tools/libxc/xc_minios.c         | 43 -------------------------
 tools/libxc/xc_netbsd.c         | 70 -----------------------------------------
 tools/libxc/xc_solaris.c        | 67 ---------------------------------------
 6 files changed, 50 insertions(+), 279 deletions(-)

diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index 2413e75..d1130e6 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -50,6 +50,56 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
     return res;
 }
 
+void *xc_map_foreign_range(xc_interface *xch,
+                           uint32_t dom, int size, int prot,
+                           unsigned long mfn)
+{
+    xen_pfn_t *arr;
+    int num;
+    int i;
+    void *ret;
+
+    num = (size + XC_PAGE_SIZE - 1) >> XC_PAGE_SHIFT;
+    arr = calloc(num, sizeof(xen_pfn_t));
+    if ( arr == NULL )
+        return NULL;
+
+    for ( i = 0; i < num; i++ )
+        arr[i] = mfn + i;
+
+    ret = xc_map_foreign_pages(xch, dom, prot, arr, num);
+    free(arr);
+    return ret;
+}
+
+void *xc_map_foreign_ranges(xc_interface *xch,
+                            uint32_t dom, size_t size,
+                            int prot, size_t chunksize,
+                            privcmd_mmap_entry_t entries[],
+                            int nentries)
+{
+    xen_pfn_t *arr;
+    int num_per_entry;
+    int num;
+    int i;
+    int j;
+    void *ret;
+
+    num_per_entry = chunksize >> XC_PAGE_SHIFT;
+    num = num_per_entry * nentries;
+    arr = calloc(num, sizeof(xen_pfn_t));
+    if ( arr == NULL )
+        return NULL;
+
+    for ( i = 0; i < nentries; i++ )
+        for ( j = 0; j < num_per_entry; j++ )
+            arr[i * num_per_entry + j] = entries[i].mfn + j;
+
+    ret = xc_map_foreign_pages(xch, dom, prot, arr, num);
+    free(arr);
+    return ret;
+}
+
 /*
  * stub for all not yet converted OSes (NetBSD and Solaris). New OSes should
  * just implement xc_map_foreign_bulk.
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c
index 6b440ee..7745d28 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libxc/xc_freebsd_osdep.c
@@ -125,56 +125,6 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     return addr;
 }
 
-void *xc_map_foreign_range(xc_interface *xch,
-                           uint32_t dom, int size, int prot,
-                           unsigned long mfn)
-{
-    xen_pfn_t *arr;
-    int num;
-    int i;
-    void *ret;
-
-    num = (size + XC_PAGE_SIZE - 1) >> XC_PAGE_SHIFT;
-    arr = calloc(num, sizeof(xen_pfn_t));
-    if ( arr == NULL )
-        return NULL;
-
-    for ( i = 0; i < num; i++ )
-        arr[i] = mfn + i;
-
-    ret = xc_map_foreign_pages(xch, dom, prot, arr, num);
-    free(arr);
-    return ret;
-}
-
-void *xc_map_foreign_ranges(xc_interface *xch,
-                            uint32_t dom, size_t size,
-                            int prot, size_t chunksize,
-                            privcmd_mmap_entry_t entries[],
-                            int nentries)
-{
-    xen_pfn_t *arr;
-    int num_per_entry;
-    int num;
-    int i;
-    int j;
-    void *ret;
-
-    num_per_entry = chunksize >> XC_PAGE_SHIFT;
-    num = num_per_entry * nentries;
-    arr = calloc(num, sizeof(xen_pfn_t));
-    if ( arr == NULL )
-        return NULL;
-
-    for ( i = 0; i < nentries; i++ )
-        for ( j = 0; j < num_per_entry; j++ )
-            arr[i * num_per_entry + j] = entries[i].mfn + j;
-
-    ret = xc_map_foreign_pages(xch, dom, prot, arr, num);
-    free(arr);
-    return ret;
-}
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 39c88ce..acf44ce 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -290,55 +290,6 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     return addr;
 }
 
-void *xc_map_foreign_range(xc_interface *xch,
-                           uint32_t dom, int size, int prot,
-                           unsigned long mfn)
-{
-    xen_pfn_t *arr;
-    int num;
-    int i;
-    void *ret;
-
-    num = (size + XC_PAGE_SIZE - 1) >> XC_PAGE_SHIFT;
-    arr = calloc(num, sizeof(xen_pfn_t));
-    if ( arr == NULL )
-        return NULL;
-
-    for ( i = 0; i < num; i++ )
-        arr[i] = mfn + i;
-
-    ret = xc_map_foreign_pages(xch, dom, prot, arr, num);
-    free(arr);
-    return ret;
-}
-
-void *xc_map_foreign_ranges(xc_interface *xch,
-                            uint32_t dom, size_t size, int prot,
-                            size_t chunksize, privcmd_mmap_entry_t entries[],
-                            int nentries)
-{
-    xen_pfn_t *arr;
-    int num_per_entry;
-    int num;
-    int i;
-    int j;
-    void *ret;
-
-    num_per_entry = chunksize >> XC_PAGE_SHIFT;
-    num = num_per_entry * nentries;
-    arr = calloc(num, sizeof(xen_pfn_t));
-    if ( arr == NULL )
-        return NULL;
-
-    for ( i = 0; i < nentries; i++ )
-        for ( j = 0; j < num_per_entry; j++ )
-            arr[i * num_per_entry + j] = entries[i].mfn + j;
-
-    ret = xc_map_foreign_pages(xch, dom, prot, arr, num);
-    free(arr);
-    return ret;
-}
-
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index 3ea3124..d8ca948 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -73,49 +73,6 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
 }
 
-void *xc_map_foreign_range(xc_interface *xch,
-                           uint32_t dom,
-                           int size, int prot,
-                           unsigned long mfn)
-{
-    unsigned long pt_prot = 0;
-
-    if (prot & PROT_READ)
-	pt_prot = L1_PROT_RO;
-    if (prot & PROT_WRITE)
-	pt_prot = L1_PROT;
-
-    assert(!(size % getpagesize()));
-    return map_frames_ex(&mfn, size / getpagesize(), 0, 1, 1, dom, NULL, pt_prot);
-}
-
-void *xc_map_foreign_ranges(xc_interface *xch,
-                            uint32_t dom,
-                            size_t size, int prot, size_t chunksize,
-                            privcmd_mmap_entry_t entries[], int nentries)
-{
-    unsigned long *mfns;
-    int i, j, n;
-    unsigned long pt_prot = 0;
-    void *ret;
-
-    if (prot & PROT_READ)
-	pt_prot = L1_PROT_RO;
-    if (prot & PROT_WRITE)
-	pt_prot = L1_PROT;
-
-    mfns = malloc((size / XC_PAGE_SIZE) * sizeof(*mfns));
-
-    n = 0;
-    for (i = 0; i < nentries; i++)
-        for (j = 0; j < chunksize / XC_PAGE_SIZE; j++)
-            mfns[n++] = entries[i].mfn + j;
-
-    ret = map_frames_ex(mfns, n, 1, 0, 1, dom, NULL, pt_prot);
-    free(mfns);
-    return ret;
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush)
 {
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index 6a7ff9f..3470bc4 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -96,76 +96,6 @@ void *osdep_map_foreign_batch(xc_interface *xch,
 
 }
 
-void *xc_map_foreign_range(xc_interface *xch,
-                           uint32_t dom,
-                           int size, int prot,
-                           unsigned long mfn)
-{
-    int fd = xch->privcmdfd;
-    privcmd_mmap_t ioctlx;
-    privcmd_mmap_entry_t entry;
-    void *addr;
-    addr = mmap(NULL, size, prot, MAP_ANON | MAP_SHARED, -1, 0);
-    if ( addr == MAP_FAILED ) {
-        PERROR("xc_map_foreign_range: mmap failed");
-        return NULL;
-    }
-
-    ioctlx.num=1;
-    ioctlx.dom=dom;
-    ioctlx.entry=&entry;
-    entry.va=(unsigned long) addr;
-    entry.mfn=mfn;
-    entry.npages=(size+XC_PAGE_SIZE-1)>>XC_PAGE_SHIFT;
-    if ( ioctl(fd, IOCTL_PRIVCMD_MMAP, &ioctlx) < 0 )
-    {
-        int saved_errno = errno;
-        PERROR("xc_map_foreign_range: ioctl failed");
-        (void)munmap(addr, size);
-        errno = saved_errno;
-        return NULL;
-    }
-    return addr;
-}
-
-void *xc_map_foreign_ranges(xc_interface *xch,
-                            uint32_t dom,
-                            size_t size, int prot, size_t chunksize,
-                            privcmd_mmap_entry_t entries[], int nentries)
-{
-    int fd = xch->privcmdfd;
-	privcmd_mmap_t ioctlx;
-	int i, rc;
-	void *addr;
-
-	addr = mmap(NULL, size, prot, MAP_ANON | MAP_SHARED, -1, 0);
-	if (addr == MAP_FAILED)
-		goto mmap_failed;
-
-	for (i = 0; i < nentries; i++) {
-		entries[i].va = (uintptr_t)addr + (i * chunksize);
-		entries[i].npages = chunksize >> XC_PAGE_SHIFT;
-	}
-
-	ioctlx.num   = nentries;
-	ioctlx.dom   = dom;
-	ioctlx.entry = entries;
-
-	rc = ioctl(fd, IOCTL_PRIVCMD_MMAP, &ioctlx);
-	if (rc)
-		goto ioctl_failed;
-
-	return addr;
-
-ioctl_failed:
-	rc = munmap(addr, size);
-	if (rc == -1)
-		ERROR("%s: error in error path\n", __FUNCTION__);
-
-mmap_failed:
-	return NULL;
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 2df7a06..d686867 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -94,73 +94,6 @@ void *osdep_map_foreign_batch(xc_interface *xch,
 
 }
 
-void *xc_map_foreign_range(xc_interface *xch,
-                           uint32_t dom,
-                           int size, int prot,
-                           unsigned long mfn)
-{
-    int fd = xch->privcmdfd;
-    privcmd_mmap_t ioctlx;
-    privcmd_mmap_entry_t entry;
-    void *addr;
-    addr = mmap(NULL, size, prot, MAP_SHARED, fd, 0);
-    if ( addr == MAP_FAILED )
-        return NULL;
-
-    ioctlx.num=1;
-    ioctlx.dom=dom;
-    ioctlx.entry=&entry;
-    entry.va=(unsigned long) addr;
-    entry.mfn=mfn;
-    entry.npages=(size+XC_PAGE_SIZE-1)>>XC_PAGE_SHIFT;
-    if ( ioctl(fd, IOCTL_PRIVCMD_MMAP, &ioctlx) < 0 )
-    {
-        int saved_errno = errno;
-        (void)munmap(addr, size);
-        errno = saved_errno;
-        return NULL;
-    }
-    return addr;
-}
-
-void *xc_map_foreign_ranges(xc_interface *xch,
-                            uint32_t dom,
-                            size_t size, int prot, size_t chunksize,
-                            privcmd_mmap_entry_t entries[], int nentries)
-{
-    int fd = xch->privcmdfd;
-    privcmd_mmap_t ioctlx;
-    int i, rc;
-    void *addr;
-
-    addr = mmap(NULL, size, prot, MAP_SHARED, fd, 0);
-    if (addr == MAP_FAILED)
-        goto mmap_failed;
-
-    for (i = 0; i < nentries; i++) {
-        entries[i].va = (uintptr_t)addr + (i * chunksize);
-        entries[i].npages = chunksize >> XC_PAGE_SHIFT;
-    }
-
-    ioctlx.num   = nentries;
-    ioctlx.dom   = dom;
-    ioctlx.entry = entries;
-
-    rc = ioctl(fd, IOCTL_PRIVCMD_MMAP, &ioctlx);
-    if (rc)
-        goto ioctl_failed;
-
-    return addr;
-
-ioctl_failed:
-    rc = munmap(addr, size);
-    if (rc == -1)
-        PERROR("%s: error in error path", __FUNCTION__);
-
-mmap_failed:
-    return NULL;
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (9 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 10/29] tools: Implement xc_map_foreign_range(s) in terms of common helper Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-25 12:01     ` Olaf Hering
  2016-01-15 13:22   ` [PATCH XEN v8 12/29] tools/libs/foreignmemory: provide xenforeignmemory_unmap Ian Campbell
                     ` (17 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

libxenforeignmemory will provide a stable API and ABI for mapping
foreign domain memory (subject to appropriate privileges).

The new library exposes an interface equivalent to
xc_map_foreign_memory_bulk, which all the other
xc_map_foreign_memory_* functions (which remain in libxc) are
implemented in terms of.

Upon request (via #define XC_WANT_COMPAT_MAP_FOREIGN_API) libxenctrl
will provide a compat API for the old names. This is used by qemu-xen
and qemu-trad as well as various in tree things (which required
de-dupping various #includes in some too to get the #define before the
first).

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
Must be applied with:
  - "qemu-xen-traditional: qemu-xen-traditional: Add
    libxenforeignmemory to rpath-link" and a corresponding QEMU_TAG
    update folded here.
  - "mini-os: mini-os: Include libxenforeignmemory with libxc" and a
    corresponding bump to MINIOS_UPSTREAM_REVISION folded in here.

v5: Allow close(NULL)

v6: Define XC_WANT_COMPAT_MAP_FOREIGN_API for xen-mceinj.c and memshrtool.c
    Fix typo in type name in compat.c, and get the functions names to
    match.
    Use an osdep layer for _map, to allow for future common code.

v7: Correct comment in compat.h #error due to switch to osdep layer
    for map in v6
    Added mk-headers-$(XEN_TARGET_ARCH) build dependency instead of
    open coding the recursion.

v8: Remove *.so on clean, add distclean target.
---
 .gitignore                                         |   2 +
 stubdom/Makefile                                   |  17 +++-
 tools/Makefile                                     |   2 +
 tools/Rules.mk                                     |  11 ++-
 tools/console/daemon/utils.c                       |   1 -
 tools/console/daemon/utils.h                       |   1 +
 tools/libs/Makefile                                |   1 +
 tools/libs/foreignmemory/Makefile                  |  71 ++++++++++++++
 tools/libs/foreignmemory/compat.c                  |  72 ++++++++++++++
 tools/libs/foreignmemory/core.c                    |  84 +++++++++++++++++
 .../foreignmemory/freebsd.c}                       |  33 +++----
 .../libs/foreignmemory/include/xenforeignmemory.h  |  71 ++++++++++++++
 tools/libs/foreignmemory/libxenforeignmemory.map   |   7 ++
 .../foreignmemory/linux.c}                         |  70 ++++++--------
 tools/libs/foreignmemory/minios.c                  |  62 ++++++++++++
 tools/libs/foreignmemory/netbsd.c                  | 105 +++++++++++++++++++++
 tools/libs/foreignmemory/private.h                 |  53 +++++++++++
 tools/libs/foreignmemory/solaris.c                 |  93 ++++++++++++++++++
 tools/libxc/Makefile                               |   8 +-
 tools/libxc/include/xenctrl.h                      |  26 -----
 tools/libxc/include/xenctrl_compat.h               |  36 +++++++
 tools/libxc/xc_foreign_memory.c                    |  49 +---------
 tools/libxc/xc_minios.c                            |  29 ------
 tools/libxc/xc_netbsd.c                            |  73 --------------
 tools/libxc/xc_private.c                           |  13 +--
 tools/libxc/xc_private.h                           |  11 ++-
 tools/libxc/xc_solaris.c                           |  73 --------------
 tools/libxc/xc_sr_restore.c                        |   4 +-
 tools/libxc/xc_sr_save.c                           |   4 +-
 tools/libxc/xg_private.h                           |   3 +-
 tools/libxl/libxl_internal.h                       |   1 +
 tools/misc/xen-mfndump.c                           |   1 +
 tools/ocaml/libs/xc/xenctrl_stubs.c                |   1 +
 tools/python/xen/lowlevel/xc/xc.c                  |   2 +
 tools/tests/mce-test/tools/xen-mceinj.c            |   1 +
 tools/tests/mem-sharing/memshrtool.c               |   1 +
 tools/xenmon/xenbaked.c                            |   1 +
 tools/xenpaging/pagein.c                           |   1 -
 tools/xenpaging/xenpaging.c                        |   1 -
 tools/xenpaging/xenpaging.h                        |   2 +
 tools/xenstore/xenstored_core.c                    |   1 -
 tools/xenstore/xenstored_core.h                    |   1 +
 tools/xentrace/xenctx.c                            |   3 +-
 tools/xentrace/xentrace.c                          |   1 +
 44 files changed, 770 insertions(+), 333 deletions(-)
 create mode 100644 tools/libs/foreignmemory/Makefile
 create mode 100644 tools/libs/foreignmemory/compat.c
 create mode 100644 tools/libs/foreignmemory/core.c
 rename tools/{libxc/xc_freebsd_osdep.c => libs/foreignmemory/freebsd.c} (76%)
 create mode 100644 tools/libs/foreignmemory/include/xenforeignmemory.h
 create mode 100644 tools/libs/foreignmemory/libxenforeignmemory.map
 rename tools/{libxc/xc_linux_osdep.c => libs/foreignmemory/linux.c} (83%)
 create mode 100644 tools/libs/foreignmemory/minios.c
 create mode 100644 tools/libs/foreignmemory/netbsd.c
 create mode 100644 tools/libs/foreignmemory/private.h
 create mode 100644 tools/libs/foreignmemory/solaris.c

diff --git a/.gitignore b/.gitignore
index 55ecfb7..9eb2d30 100644
--- a/.gitignore
+++ b/.gitignore
@@ -64,6 +64,7 @@ stubdom/libs-*
 stubdom/libxc-*
 stubdom/libxencall-*
 stubdom/libxenevtchn-*
+stubdom/libxenforeignmemory-*
 stubdom/libxengnttab-*
 stubdom/libxentoollog-*
 stubdom/lwip-*
@@ -93,6 +94,7 @@ tools/libs/toollog/headers.chk
 tools/libs/evtchn/headers.chk
 tools/libs/gnttab/headers.chk
 tools/libs/call/headers.chk
+tools/libs/foreignmemory/headers.chk
 tools/blktap2/daemon/blktapctrl
 tools/blktap2/drivers/img2qcow
 tools/blktap2/drivers/lock-util
diff --git a/stubdom/Makefile b/stubdom/Makefile
index 5bf4ed6..6f68e98 100644
--- a/stubdom/Makefile
+++ b/stubdom/Makefile
@@ -337,6 +337,12 @@ mk-headers-$(XEN_TARGET_ARCH): $(IOEMU_LINKFARM_TARGET)
 	  ln -sf $(XEN_ROOT)/tools/libs/call/include/*.h include/ && \
 	  ln -sf $(XEN_ROOT)/tools/libs/call/*.c . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/call/Makefile . )
+	mkdir -p libs-$(XEN_TARGET_ARCH)/foreignmemory/include
+	[ -h libs-$(XEN_TARGET_ARCH)/foreignmemory/Makefile ] || ( cd libs-$(XEN_TARGET_ARCH)/foreignmemory && \
+	  ln -sf $(XEN_ROOT)/tools/libs/foreignmemory/*.h . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/foreignmemory/include/*.h include/ && \
+	  ln -sf $(XEN_ROOT)/tools/libs/foreignmemory/*.c . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/foreignmemory/Makefile . )
 	mkdir -p libxc-$(XEN_TARGET_ARCH)
 	[ -h libxc-$(XEN_TARGET_ARCH)/Makefile ] || ( cd libxc-$(XEN_TARGET_ARCH) && \
 	  ln -sf $(XEN_ROOT)/tools/libxc/*.h . && \
@@ -396,12 +402,21 @@ libs-$(XEN_TARGET_ARCH)/call/libxencall.a: mk-headers-$(XEN_TARGET_ARCH) $(NEWLI
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/call
 
 #######
+# libxenforeignmemory
+#######
+
+.PHONY: libxenforeignmemory
+libxenforeignmemory: libs-$(XEN_TARGET_ARCH)/foreignmemory/libxenforeignmemory.a
+libs-$(XEN_TARGET_ARCH)/foreignmemory/libxenforeignmemory.a: mk-headers-$(XEN_TARGET_ARCH) $(NEWLIB_STAMPFILE)
+	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/foreignmemory
+
+#######
 # libxc
 #######
 
 .PHONY: libxc
 libxc: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a libxc-$(XEN_TARGET_ARCH)/libxenguest.a
-libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn libxengnttab libxencall cross-zlib
+libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn libxengnttab libxencall libxenforeignmemory cross-zlib
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= CONFIG_LIBXC_MINIOS=y -C libxc-$(XEN_TARGET_ARCH)
 
  libxc-$(XEN_TARGET_ARCH)/libxenguest.a: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a
diff --git a/tools/Makefile b/tools/Makefile
index e5bbc98..c03f6db 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -250,6 +250,7 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		--source-path=$$source \
 		--extra-cflags="-DXC_WANT_COMPAT_EVTCHN_API=1 \
 		-DXC_WANT_COMPAT_GNTTAB_API=1 \
+		-DXC_WANT_COMPAT_MAP_FOREIGN_API=1 \
 		-I$(XEN_ROOT)/tools/include \
 		-I$(XEN_ROOT)/tools/libs/toollog/include \
 		-I$(XEN_ROOT)/tools/libs/evtchn/include \
@@ -264,6 +265,7 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/evtchn \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/gnttab \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/call \
+		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/foreignmemory \
 		$(QEMU_UPSTREAM_RPATH)" \
 		--bindir=$(LIBEXEC_BIN) \
 		--datadir=$(SHAREDIR)/qemu-xen \
diff --git a/tools/Rules.mk b/tools/Rules.mk
index 18e3688..d47fa45 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -14,6 +14,7 @@ XEN_LIBXENTOOLLOG  = $(XEN_ROOT)/tools/libs/toollog
 XEN_LIBXENEVTCHN   = $(XEN_ROOT)/tools/libs/evtchn
 XEN_LIBXENGNTTAB   = $(XEN_ROOT)/tools/libs/gnttab
 XEN_LIBXENCALL     = $(XEN_ROOT)/tools/libs/call
+XEN_LIBXENFOREIGNMEMORY = $(XEN_ROOT)/tools/libs/foreignmemory
 XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
 XEN_XENLIGHT       = $(XEN_ROOT)/tools/libxl
 XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
@@ -103,12 +104,16 @@ CFLAGS_libxencall = -I$(XEN_LIBXENCALL)/include $(CFLAGS_xeninclude)
 LDLIBS_libxencall = $(XEN_LIBXENCALL)/libxencall$(libextension)
 SHLIB_libxencall  = -Wl,-rpath-link=$(XEN_LIBXENCALL)
 
-CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_xeninclude)
-SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn) $(SHLIB_libxengnttab) $(SHLIB_libxengntshr) $(SHLIB_libxencall)
+CFLAGS_libxenforeignmemory = -I$(XEN_LIBXENFOREIGNMEMORY)/include $(CFLAGS_xeninclude)
+LDLIBS_libxenforeignmemory = $(XEN_LIBXENFOREIGNMEMORY)/libxenforeignmemory$(libextension)
+SHLIB_libxenforeignmemory  = -Wl,-rpath-link=$(XEN_LIBXENFOREIGNMEMORY)
+
+CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_libxenforeignmemory) $(CFLAGS_xeninclude)
+SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn) $(SHLIB_libxengnttab) $(SHLIB_libxengntshr) $(SHLIB_libxencall) $(SHLIB_libxenforeignmemory)
 LDLIBS_libxenctrl = $(SHDEPS_libxenctrl) $(XEN_LIBXC)/libxenctrl$(libextension)
 SHLIB_libxenctrl  = $(SHDEPS_libxenctrl) -Wl,-rpath-link=$(XEN_LIBXC)
 
-CFLAGS_libxenguest = -I$(XEN_LIBXC)/include $(CFLAGS_libxenevtchn) $(CFLAGS_xeninclude)
+CFLAGS_libxenguest = -I$(XEN_LIBXC)/include $(CFLAGS_libxenevtchn) $(CFLAGS_libxenforeignmemory) $(CFLAGS_xeninclude)
 SHDEPS_libxenguest = $(SHLIB_libxenevtchn)
 LDLIBS_libxenguest = $(SHDEPS_libxenguest) $(XEN_LIBXC)/libxenguest$(libextension)
 SHLIB_libxenguest  = $(SHDEPS_libxenguest) -Wl,-rpath-link=$(XEN_LIBXC)
diff --git a/tools/console/daemon/utils.c b/tools/console/daemon/utils.c
index 644f6af..97d7798 100644
--- a/tools/console/daemon/utils.c
+++ b/tools/console/daemon/utils.c
@@ -33,7 +33,6 @@
 #include <string.h>
 #include <signal.h>
 
-#include "xenctrl.h"
 #include "utils.h"
 
 struct xs_handle *xs;
diff --git a/tools/console/daemon/utils.h b/tools/console/daemon/utils.h
index 1295822..a010ac4 100644
--- a/tools/console/daemon/utils.h
+++ b/tools/console/daemon/utils.h
@@ -23,6 +23,7 @@
 #include <stdbool.h>
 #include <syslog.h>
 #include <stdio.h>
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 
 #include <xenstore.h>
diff --git a/tools/libs/Makefile b/tools/libs/Makefile
index f4f5d57..dc5726d 100644
--- a/tools/libs/Makefile
+++ b/tools/libs/Makefile
@@ -6,5 +6,6 @@ SUBDIRS-y += toollog
 SUBDIRS-y += evtchn
 SUBDIRS-y += gnttab
 SUBDIRS-y += call
+SUBDIRS-y += foreignmemory
 
 all clean install distclean: %: subdirs-%
diff --git a/tools/libs/foreignmemory/Makefile b/tools/libs/foreignmemory/Makefile
new file mode 100644
index 0000000..f062f45
--- /dev/null
+++ b/tools/libs/foreignmemory/Makefile
@@ -0,0 +1,71 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+SHLIB_LDFLAGS += -Wl,--version-script=libxenforeignmemory.map
+
+CFLAGS   += -Werror -Wmissing-prototypes
+CFLAGS   += -I./include $(CFLAGS_xeninclude)
+CFLAGS   += $(CFLAGS_libxentoollog)
+
+SRCS-y                 += core.c
+SRCS-$(CONFIG_Linux)   += linux.c
+SRCS-$(CONFIG_FreeBSD) += freebsd.c
+SRCS-$(CONFIG_SunOS)   += compat.c solaris.c
+SRCS-$(CONFIG_NetBSD)  += compat.c netbsd.c
+SRCS-$(CONFIG_MiniOS)  += minios.c
+
+LIB_OBJS := $(patsubst %.c,%.o,$(SRCS-y))
+PIC_OBJS := $(patsubst %.c,%.opic,$(SRCS-y))
+
+LIB := libxenforeignmemory.a
+ifneq ($(nosharedlibs),y)
+LIB += libxenforeignmemory.so
+endif
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build:
+	$(MAKE) libs
+
+.PHONY: libs
+libs: headers.chk $(LIB)
+
+headers.chk: $(wildcard include/*.h)
+
+libxenforeignmemory.a: $(LIB_OBJS)
+	$(AR) rc $@ $^
+
+libxenforeignmemory.so: libxenforeignmemory.so.$(MAJOR)
+	$(SYMLINK_SHLIB) $< $@
+libxenforeignmemory.so.$(MAJOR): libxenforeignmemory.so.$(MAJOR).$(MINOR)
+	$(SYMLINK_SHLIB) $< $@
+
+libxenforeignmemory.so.$(MAJOR).$(MINOR): $(PIC_OBJS) libxenforeignmemory.map
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenforeignmemory.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(PIC_OBJS) $(LDLIBS_libxentoollog) $(APPEND_LDFLAGS)
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(libdir)
+	$(INSTALL_DIR) $(DESTDIR)$(includedir)
+	$(INSTALL_SHLIB) libxenforeignmemory.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
+	$(INSTALL_DATA) libxenforeignmemory.a $(DESTDIR)$(libdir)
+	$(SYMLINK_SHLIB) libxenforeignmemory.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenforeignmemory.so.$(MAJOR)
+	$(SYMLINK_SHLIB) libxenforeignmemory.so.$(MAJOR) $(DESTDIR)$(libdir)/libxenforeignmemory.so
+	$(INSTALL_DATA) include/xenforeignmemory.h $(DESTDIR)$(includedir)
+
+.PHONY: TAGS
+TAGS:
+	etags -t *.c *.h
+
+.PHONY: clean
+clean:
+	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
+	rm -f libxenforeignmemory.so.$(MAJOR).$(MINOR) libxenforeignmemory.so.$(MAJOR)
+	rm -f headers.chk
+
+.PHONY: distclean
+distclean: clean
diff --git a/tools/libs/foreignmemory/compat.c b/tools/libs/foreignmemory/compat.c
new file mode 100644
index 0000000..b8c6fc6
--- /dev/null
+++ b/tools/libs/foreignmemory/compat.c
@@ -0,0 +1,72 @@
+/* Compatibility functions for mapping foreign domain's memory.
+ *
+ * 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/>.
+ *
+ * Split out from xc_foreign_memory.c
+ */
+#if !defined(__NetBSD__) && !defined(__sun__)
+#error Please implement osdep_xenforeignmemory_map directly for new ports.
+#endif
+
+#include "private.h"
+
+void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
+                                 uint32_t dom, int prot,
+                                 const xen_pfn_t *arr, int *err, unsigned int num)
+{
+    xen_pfn_t *pfn;
+    unsigned int i;
+    void *ret;
+
+    if ((int)num <= 0) {
+        errno = EINVAL;
+        return NULL;
+    }
+
+    pfn = malloc(num * sizeof(*pfn));
+    if (!pfn) {
+        errno = ENOMEM;
+        return NULL;
+    }
+
+    memcpy(pfn, arr, num * sizeof(*arr));
+    ret = osdep_map_foreign_batch(fmem, dom, prot, pfn, num);
+
+    if (ret) {
+        for (i = 0; i < num; ++i)
+            switch (pfn[i] ^ arr[i]) {
+            case 0:
+                err[i] = 0;
+                break;
+            default:
+                err[i] = -EINVAL;
+                break;
+            }
+    } else
+        memset(err, 0, num * sizeof(*err));
+
+    free(pfn);
+
+    return ret;
+}
+
+/*
+ * 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/foreignmemory/core.c b/tools/libs/foreignmemory/core.c
new file mode 100644
index 0000000..21dc7ee
--- /dev/null
+++ b/tools/libs/foreignmemory/core.c
@@ -0,0 +1,84 @@
+/*
+ * 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 <stdlib.h>
+
+#include "private.h"
+
+xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
+                                               unsigned open_flags)
+{
+    xenforeignmemory_handle *fmem = malloc(sizeof(*fmem));
+    int rc;
+
+    if (!fmem) return NULL;
+
+    fmem->logger = logger;
+    fmem->logger_tofree = NULL;
+
+    if (!fmem->logger) {
+        fmem->logger = fmem->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!fmem->logger) goto err;
+    }
+
+    rc = osdep_xenforeignmemory_open(fmem);
+    if ( rc  < 0 ) goto err;
+
+    return fmem;
+
+err:
+    osdep_xenforeignmemory_close(fmem);
+    xtl_logger_destroy(fmem->logger_tofree);
+    free(fmem);
+    return NULL;
+}
+
+int xenforeignmemory_close(xenforeignmemory_handle *fmem)
+{
+    int rc;
+
+    if ( !fmem )
+        return 0;
+
+    rc = osdep_xenforeignmemory_close(fmem);
+    xtl_logger_destroy(fmem->logger_tofree);
+    free(fmem);
+    return rc;
+}
+
+void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
+                           uint32_t dom, int prot,
+                           const xen_pfn_t *arr, int *err, size_t num)
+{
+    return osdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num);
+}
+
+int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                           void *addr, size_t num)
+{
+    return osdep_xenforeignmemory_unmap(fmem, addr, num);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libs/foreignmemory/freebsd.c
similarity index 76%
rename from tools/libxc/xc_freebsd_osdep.c
rename to tools/libs/foreignmemory/freebsd.c
index 7745d28..4b2815d 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -3,9 +3,6 @@
  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  * Use is subject to license terms.
  *
- * xc_gnttab functions:
- * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
- *
  * 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;
@@ -30,14 +27,11 @@
 #include <sys/mman.h>
 #include <sys/ioctl.h>
 
-#include <xen/memory.h>
-
-#include "xc_private.h"
+#include "private.h"
 
 #define PRIVCMD_DEV     "/dev/xen/privcmd"
 
-/*------------------------- Privcmd device interface -------------------------*/
-int osdep_privcmd_open(xc_interface *xch)
+int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
 {
     int flags, saved_errno;
     int fd = open(PRIVCMD_DEV, O_RDWR);
@@ -46,7 +40,7 @@ int osdep_privcmd_open(xc_interface *xch)
     {
         PERROR("Could not obtain handle on privileged command interface "
                PRIVCMD_DEV);
-        return -1
+        return -1;
     }
 
     /*
@@ -69,7 +63,7 @@ int osdep_privcmd_open(xc_interface *xch)
         goto error;
     }
 
-    xch->privcmdfd = fd;
+    fmem->fd = fd;
     return 0;
 
  error:
@@ -80,26 +74,25 @@ int osdep_privcmd_open(xc_interface *xch)
     return -1;
 }
 
-int osdep_privcmd_close(xc_interface *xch)
+int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
 {
-    int fd = xch->privcmdfd;
+    int fd = fmem->fd;
     if ( fd == -1 )
         return 0;
     return close(fd);
 }
 
-/*----------------------- Privcmd foreign map interface ----------------------*/
-void *xc_map_foreign_bulk(xc_interface *xch,
-                          uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err,
-                          unsigned int num)
+void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
+                                 uint32_t dom, int prot,
+                                 const xen_pfn_t *arr, int *err,
+                                 unsigned int num)
 {
-    int fd = xch->privcmdfd;
+    int fd = fmem->fd;
     privcmd_mmapbatch_t ioctlx;
     void *addr;
     int rc;
 
-    addr = mmap(NULL, num << XC_PAGE_SHIFT, prot, MAP_SHARED, fd, 0);
+    addr = mmap(NULL, num << PAGE_SHIFT, prot, MAP_SHARED, fd, 0);
     if ( addr == MAP_FAILED )
     {
         PERROR("xc_map_foreign_bulk: mmap failed");
@@ -117,7 +110,7 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     {
         int saved_errno = errno;
         PERROR("xc_map_foreign_bulk: ioctl failed");
-        (void)munmap(addr, num << XC_PAGE_SHIFT);
+        (void)munmap(addr, num << PAGE_SHIFT);
         errno = saved_errno;
         return NULL;
     }
diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
new file mode 100644
index 0000000..5b3fa7a
--- /dev/null
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -0,0 +1,71 @@
+/*
+ * 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/>.
+ */
+#ifndef XENFOREIGNMEMORY_H
+#define XENFOREIGNMEMORY_H
+
+/*
+ * This library allows you to map foreign domain memory, subject to
+ * permissions for both the process and the domain in which the
+ * process runs.
+ */
+
+#include <stdint.h>
+#include <stddef.h>
+
+#include <xen/xen.h>
+
+/* Callers who don't care don't need to #include <xentoollog.h> */
+typedef struct xentoollog_logger xentoollog_logger;
+
+typedef struct xenforeignmemory_handle xenforeignmemory_handle;
+
+/*
+ * Return a handle onto the hypercall driver.  Logs errors.
+ */
+xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
+                                               unsigned open_flags);
+
+/*
+ * Close a handle previously allocated with xenforeignmemory_open().
+ */
+int xenforeignmemory_close(xenforeignmemory_handle *fmem);
+
+/*
+ * Maps a range within one domain to a local address range.  Mappings
+ * should be unmapped with munmap and should follow the same rules as mmap
+ * regarding page alignment.
+ *
+ * prot is as for mmap(2).
+ *
+ * Can partially succeed. When a page cannot be mapped, its respective
+ * field in @err is set to the corresponding errno value.
+ *
+ * Returns NULL if no pages can be mapped.
+ */
+void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
+                           int prot, const xen_pfn_t *arr, int *err,
+                           unsigned int num);
+
+#endif
+
+/*
+ * 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/foreignmemory/libxenforeignmemory.map b/tools/libs/foreignmemory/libxenforeignmemory.map
new file mode 100644
index 0000000..11f0d2b
--- /dev/null
+++ b/tools/libs/foreignmemory/libxenforeignmemory.map
@@ -0,0 +1,7 @@
+VERS_1.0 {
+	global:
+		xenforeignmemory_open;
+		xenforeignmemory_close;
+		xenforeignmemory_map;
+	local: *; /* Do not expose anything by default */
+};
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libs/foreignmemory/linux.c
similarity index 83%
rename from tools/libxc/xc_linux_osdep.c
rename to tools/libs/foreignmemory/linux.c
index acf44ce..b403ca7 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libs/foreignmemory/linux.c
@@ -1,11 +1,4 @@
- /******************************************************************************
- *
- * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
- *
- * xc_gnttab functions:
- * Copyright (c) 2007-2008, D G Murray <Derek.Murray@cl.cam.ac.uk>
- *
+/*
  * 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;
@@ -18,27 +11,26 @@
  *
  * 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/>.
+ *
+ * Split out from xc_linus_osdep.c:
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  */
 
+#include <alloca.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <string.h>
 #include <unistd.h>
+#include <string.h>
 
 #include <sys/mman.h>
 #include <sys/ioctl.h>
 
-#include <xen/memory.h>
-
-#include "xenctrl.h"
-
-#include "xc_private.h"
+#include "private.h"
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
-int osdep_privcmd_open(xc_interface *xch)
+int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
 {
     int flags, saved_errno;
     int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
@@ -73,7 +65,7 @@ int osdep_privcmd_open(xc_interface *xch)
         goto error;
     }
 
-    xch->privcmdfd = fd;
+    fmem->fd = fd;
     return 0;
 
  error:
@@ -83,9 +75,9 @@ int osdep_privcmd_open(xc_interface *xch)
     return -1;
 }
 
-int osdep_privcmd_close(xc_interface *xch)
+int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
 {
-    int fd = xch->privcmdfd;
+    int fd = fmem->fd;
     if (fd == -1)
         return 0;
     return close(fd);
@@ -127,7 +119,7 @@ static int retry_paged(int fd, uint32_t dom, void *addr,
 {
     privcmd_mmapbatch_v2_t ioctlx;
     int rc, paged = 0, i = 0;
-    
+
     do
     {
         /* Skip gfns not in paging state */
@@ -142,10 +134,10 @@ static int retry_paged(int fd, uint32_t dom, void *addr,
         /* At least one gfn is still in paging state */
         ioctlx.num = 1;
         ioctlx.dom = dom;
-        ioctlx.addr = (unsigned long)addr + ((unsigned long)i<<XC_PAGE_SHIFT);
+        ioctlx.addr = (unsigned long)addr + ((unsigned long)i<<PAGE_SHIFT);
         ioctlx.arr = arr + i;
         ioctlx.err = err + i;
-        
+
         /* Assemble a batch of requests */
         while ( ++i < num )
         {
@@ -153,34 +145,34 @@ static int retry_paged(int fd, uint32_t dom, void *addr,
                 break;
             ioctlx.num++;
         }
-        
+
         /* Send request and abort on fatal error */
         rc = ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH_V2, &ioctlx);
         if ( rc < 0 && errno != ENOENT )
             goto out;
 
     } while ( i < num );
-    
+
     rc = paged;
 out:
     return rc;
 }
 
-void *xc_map_foreign_bulk(xc_interface *xch,
-                          uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err, unsigned int num)
+void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
+                                 uint32_t dom, int prot,
+                                 const xen_pfn_t *arr, int *err, unsigned int num)
 {
-    int fd = xch->privcmdfd;
+    int fd = fmem->fd;
     privcmd_mmapbatch_v2_t ioctlx;
     void *addr;
     unsigned int i;
     int rc;
 
-    addr = mmap(NULL, (unsigned long)num << XC_PAGE_SHIFT, prot, MAP_SHARED,
+    addr = mmap(NULL, (unsigned long)num << PAGE_SHIFT, prot, MAP_SHARED,
                 fd, 0);
     if ( addr == MAP_FAILED )
     {
-        PERROR("xc_map_foreign_bulk: mmap failed");
+        PERROR("mmap failed");
         return NULL;
     }
 
@@ -209,9 +201,9 @@ void *xc_map_foreign_bulk(xc_interface *xch,
          */
         privcmd_mmapbatch_t ioctlx;
         xen_pfn_t *pfn;
-        unsigned int pfn_arr_size = ROUNDUP((num * sizeof(*pfn)), XC_PAGE_SHIFT);
+        unsigned int pfn_arr_size = ROUNDUP((num * sizeof(*pfn)), PAGE_SHIFT);
 
-        if ( pfn_arr_size <= XC_PAGE_SIZE )
+        if ( pfn_arr_size <= PAGE_SIZE )
             pfn = alloca(num * sizeof(*pfn));
         else
         {
@@ -219,8 +211,8 @@ void *xc_map_foreign_bulk(xc_interface *xch,
                        MAP_PRIVATE | MAP_ANON | MAP_POPULATE, -1, 0);
             if ( pfn == MAP_FAILED )
             {
-                PERROR("xc_map_foreign_bulk: mmap of pfn array failed");
-                (void)munmap(addr, (unsigned long)num << XC_PAGE_SHIFT);
+                PERROR("mmap of pfn array failed");
+                (void)munmap(addr, (unsigned long)num << PAGE_SHIFT);
                 return NULL;
             }
         }
@@ -253,7 +245,7 @@ void *xc_map_foreign_bulk(xc_interface *xch,
                     continue;
                 }
                 rc = map_foreign_batch_single(fd, dom, pfn + i,
-                        (unsigned long)addr + ((unsigned long)i<<XC_PAGE_SHIFT));
+                        (unsigned long)addr + ((unsigned long)i<<PAGE_SHIFT));
                 if ( rc < 0 )
                 {
                     rc = -errno;
@@ -265,7 +257,7 @@ void *xc_map_foreign_bulk(xc_interface *xch,
             break;
         }
 
-        if ( pfn_arr_size > XC_PAGE_SIZE )
+        if ( pfn_arr_size > PAGE_SIZE )
             munmap(pfn, pfn_arr_size);
 
         if ( rc == -ENOENT && i == num )
@@ -281,8 +273,8 @@ void *xc_map_foreign_bulk(xc_interface *xch,
     {
         int saved_errno = errno;
 
-        PERROR("xc_map_foreign_bulk: ioctl failed");
-        (void)munmap(addr, (unsigned long)num << XC_PAGE_SHIFT);
+        PERROR("ioctl failed");
+        (void)munmap(addr, (unsigned long)num << PAGE_SHIFT);
         errno = saved_errno;
         return NULL;
     }
diff --git a/tools/libs/foreignmemory/minios.c b/tools/libs/foreignmemory/minios.c
new file mode 100644
index 0000000..a542e3d
--- /dev/null
+++ b/tools/libs/foreignmemory/minios.c
@@ -0,0 +1,62 @@
+/*
+ * 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/>.
+ *
+ * Split out from xc_minios.c
+ *
+ * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
+ */
+
+#include <mini-os/types.h>
+#include <mini-os/os.h>
+#include <mini-os/mm.h>
+#include <mini-os/lib.h>
+
+#include <errno.h>
+
+#include <sys/mman.h>
+
+#include "private.h"
+
+int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
+{
+    /* No fd required */
+    return 0;
+}
+
+int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
+{
+    return 0;
+}
+
+void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
+                                 uint32_t dom, int prot,
+                                 const xen_pfn_t *arr, int *err, unsigned int num)
+{
+    unsigned long pt_prot = 0;
+    if (prot & PROT_READ)
+	pt_prot = L1_PROT_RO;
+    if (prot & PROT_WRITE)
+	pt_prot = L1_PROT;
+    return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
+}
+
+/*
+ * 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/foreignmemory/netbsd.c b/tools/libs/foreignmemory/netbsd.c
new file mode 100644
index 0000000..704a096
--- /dev/null
+++ b/tools/libs/foreignmemory/netbsd.c
@@ -0,0 +1,105 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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 <unistd.h>
+#include <fcntl.h>
+#include <sys/mman.h>
+
+#include "private.h"
+
+int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
+{
+    int flags, saved_errno;
+    int fd = open("/kern/xen/privcmd", O_RDWR);
+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command interface");
+        return -1;
+    }
+
+    /* Although we return the file handle as the 'xc handle' the API
+       does not specify / guarentee that this integer is in fact
+       a file handle. Thus we must take responsiblity to ensure
+       it doesn't propagate (ie leak) outside the process */
+    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
+    {
+        PERROR("Could not get file handle flags");
+        goto error;
+    }
+    flags |= FD_CLOEXEC;
+    if ( fcntl(fd, F_SETFD, flags) < 0 )
+    {
+        PERROR("Could not set file handle flags");
+        goto error;
+    }
+
+    fmem->fd = fd;
+    return 0;
+
+ error:
+    saved_errno = errno;
+    close(fd);
+    errno = saved_errno;
+    return -1;
+}
+
+int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
+{
+    int fd = fmem->fd;
+    return close(fd);
+}
+
+void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
+                              int prot, xen_pfn_t *arr, int num)
+{
+    int fd = fmem->fd;
+    privcmd_mmapbatch_t ioctlx;
+    void *addr;
+    addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_ANON | MAP_SHARED, -1, 0);
+    if ( addr == MAP_FAILED ) {
+        PERROR("osdep_map_foreign_batch: mmap failed");
+        return NULL;
+    }
+
+    ioctlx.num=num;
+    ioctlx.dom=dom;
+    ioctlx.addr=(unsigned long)addr;
+    ioctlx.arr=arr;
+    if ( ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
+    {
+        int saved_errno = errno;
+        PERROR("osdep_map_foreign_batch: ioctl failed");
+        (void)munmap(addr, num*XC_PAGE_SIZE);
+        errno = saved_errno;
+        return NULL;
+    }
+    return addr;
+
+}
+
+/*
+ * 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/foreignmemory/private.h b/tools/libs/foreignmemory/private.h
new file mode 100644
index 0000000..36a71ec
--- /dev/null
+++ b/tools/libs/foreignmemory/private.h
@@ -0,0 +1,53 @@
+#ifndef XENFOREIGNMEMORY_PRIVATE_H
+#define XENFOREIGNMEMORY_PRIVATE_H
+
+#include <xentoollog.h>
+
+#include <xenforeignmemory.h>
+
+#include <xen/xen.h>
+#include <xen/sys/privcmd.h>
+
+#ifndef PAGE_SHIFT /* Mini-os, Yukk */
+#define PAGE_SHIFT           12
+#endif
+#ifndef __MINIOS__ /* Yukk */
+#define PAGE_SIZE            (1UL << PAGE_SHIFT)
+#define PAGE_MASK            (~(PAGE_SIZE-1))
+#endif
+
+struct xenforeignmemory_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    unsigned flags;
+    int fd;
+};
+
+int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem);
+int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem);
+
+void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
+                                 uint32_t dom, int prot,
+                                 const xen_pfn_t *arr, int *err, size_t num);
+int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                                 void *addr, size_t num);
+
+#if defined(__NetBSD__) || defined(__sun__)
+/* Strictly compat for those two only only */
+void *compat_mapforeign_batch(xenforeignmem_handle *fmem, uint32_t dom,
+                              int prot, xen_pfn_t *arr, int num);
+#endif
+
+#define PERROR(_f...) \
+	xtl_log(fmem->logger, XTL_ERROR, errno, "xenforeignmemory", _f)
+
+#endif
+
+/*
+ * 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/foreignmemory/solaris.c b/tools/libs/foreignmemory/solaris.c
new file mode 100644
index 0000000..f4dd634
--- /dev/null
+++ b/tools/libs/foreignmemory/solaris.c
@@ -0,0 +1,93 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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 "xc_private.h"
+
+#include <xen/memory.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <malloc.h>
+
+int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
+{
+    int flags, saved_errno;
+    int fd = open("/dev/xen/privcmd", O_RDWR);
+
+    if ( fd == -1 )
+    {
+        PERROR("Could not obtain handle on privileged command interface");
+        return -1;
+    }
+
+    /* Although we return the file handle as the 'xc handle' the API
+       does not specify / guarentee that this integer is in fact
+       a file handle. Thus we must take responsiblity to ensure
+       it doesn't propagate (ie leak) outside the process */
+    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
+    {
+        PERROR("Could not get file handle flags");
+        goto error;
+    }
+    flags |= FD_CLOEXEC;
+    if ( fcntl(fd, F_SETFD, flags) < 0 )
+    {
+        PERROR("Could not set file handle flags");
+        goto error;
+    }
+
+    fmem->fd = fd;
+    return 0;
+
+ error:
+    saved_errno = errno;
+    close(fd);
+    errno = saved_errno;
+    return -1;
+}
+
+int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
+{
+    int fd = fmem->fd;
+    return close(fd);
+}
+
+void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
+                              int prot, xen_pfn_t *arr, int num)
+{
+    int fd = fmem->fd;
+    privcmd_mmapbatch_t ioctlx;
+    void *addr;
+    addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_SHARED, fd, 0);
+    if ( addr == MAP_FAILED )
+        return NULL;
+
+    ioctlx.num=num;
+    ioctlx.dom=dom;
+    ioctlx.addr=(unsigned long)addr;
+    ioctlx.arr=arr;
+    if ( ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
+    {
+        int saved_errno = errno;
+        PERROR("XXXXXXXX");
+        (void)munmap(addr, num*XC_PAGE_SIZE);
+        errno = saved_errno;
+        return NULL;
+    }
+    return addr;
+}
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index a122f73..ca8bab7 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -42,8 +42,8 @@ CTRL_SRCS-y       += xc_kexec.c
 CTRL_SRCS-y       += xc_resource.c
 CTRL_SRCS-$(CONFIG_X86) += xc_psr.c
 CTRL_SRCS-$(CONFIG_X86) += xc_pagetab.c
-CTRL_SRCS-$(CONFIG_Linux) += xc_linux.c xc_linux_osdep.c
-CTRL_SRCS-$(CONFIG_FreeBSD) += xc_freebsd.c xc_freebsd_osdep.c
+CTRL_SRCS-$(CONFIG_Linux) += xc_linux.c
+CTRL_SRCS-$(CONFIG_FreeBSD) += xc_freebsd.c
 CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c
 CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c
 CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c
@@ -125,7 +125,7 @@ $(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.
 # libxenguest includes xc_private.h, so needs this despite not using
 # this functionality directly.
 $(CTRL_LIB_OBJS) $(GUEST_LIB_OBJS) \
-$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): CFLAGS += $(CFLAGS_libxencall)
+$(CTRL_PIC_OBJS) $(GUEST_PIC_OBJS): CFLAGS += $(CFLAGS_libxencall) $(CFLAGS_libxenforeignmemory)
 
 $(CTRL_LIB_OBJS) $(CTRL_PIC_OBJS): CFLAGS += $(CFLAGS_libxengnttab) $(CFLAGS_libxengntshr)
 
@@ -207,7 +207,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
 	$(SYMLINK_SHLIB) $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(LDLIBS_libxencall) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxengnttab) $(LDLIBS_libxengntshr) $(LDLIBS_libxencall) $(LDLIBS_libxenforeignmemory) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 # libxenguest
 
diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index cb41c07..76beafd 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -1369,32 +1369,6 @@ int xc_lockprof_query(xc_interface *xch,
 void *xc_memalign(xc_interface *xch, size_t alignment, size_t size);
 
 /**
- * Memory maps a range within one domain to a local address range.  Mappings
- * should be unmapped with munmap and should follow the same rules as mmap
- * regarding page alignment.  Returns NULL on failure.
- *
- * @parm xch a handle on an open hypervisor interface
- * @parm dom the domain to map memory from
- * @parm size the amount of memory to map (in multiples of page size)
- * @parm prot same flag as in mmap().
- * @parm mfn the frame address to map.
- */
-void *xc_map_foreign_range(xc_interface *xch, uint32_t dom,
-                            int size, int prot,
-                            unsigned long mfn );
-
-void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
-                           const xen_pfn_t *arr, int num );
-
-/**
- * Like xc_map_foreign_pages(), except it can succeed partially.
- * When a page cannot be mapped, its respective field in @err is
- * set to the corresponding errno value.
- */
-void *xc_map_foreign_bulk(xc_interface *xch, uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err, unsigned int num);
-
-/**
  * Translates a virtual address in the context of a given domain and
  * vcpu returning the GFN containing the address (that is, an MFN for 
  * PV guests, a PFN for HVM guests).  Returns 0 for failure.
diff --git a/tools/libxc/include/xenctrl_compat.h b/tools/libxc/include/xenctrl_compat.h
index d99fa11..54c23a4 100644
--- a/tools/libxc/include/xenctrl_compat.h
+++ b/tools/libxc/include/xenctrl_compat.h
@@ -12,6 +12,42 @@
 #ifndef XENCTRL_COMPAT_H
 #define XENCTRL_COMPAT_H
 
+#ifdef XC_WANT_COMPAT_MAP_FOREIGN_API
+/**
+ * Memory maps a range within one domain to a local address range.  Mappings
+ * should be unmapped with munmap and should follow the same rules as mmap
+ * regarding page alignment.  Returns NULL on failure.
+ *
+ * @parm xch a handle on an open hypervisor interface
+ * @parm dom the domain to map memory from
+ * @parm size the amount of memory to map (in multiples of page size)
+ * @parm prot same flag as in mmap().
+ * @parm mfn the frame address to map.
+ */
+void *xc_map_foreign_range(xc_interface *xch, uint32_t dom,
+                            int size, int prot,
+                            unsigned long mfn );
+
+void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
+                           const xen_pfn_t *arr, int num );
+
+/* Nothing within the library itself other than the compat wrapper
+ * itself should be using this, everything inside has access to
+ * xenforeignmemory_map().
+ */
+#if !defined(XC_INTERNAL_COMPAT_MAP_FOREIGN_API) || \
+     defined(XC_BUILDING_COMPAT_MAP_FOREIGN_API)
+/**
+ * Like xc_map_foreign_pages(), except it can succeed partially.
+ * When a page cannot be mapped, its respective field in @err is
+ * set to the corresponding errno value.
+ */
+void *xc_map_foreign_bulk(xc_interface *xch, uint32_t dom, int prot,
+                          const xen_pfn_t *arr, int *err, unsigned int num);
+#endif
+
+#endif
+
 #ifdef XC_WANT_COMPAT_EVTCHN_API
 
 typedef struct xenevtchn_handle xc_evtchn;
diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index d1130e6..1737c10 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -17,6 +17,7 @@
  * License along with this library; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#define XC_BUILDING_COMPAT_MAP_FOREIGN_API
 #include "xc_private.h"
 
 void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
@@ -34,7 +35,7 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
     if (!err)
         return NULL;
 
-    res = xc_map_foreign_bulk(xch, dom, prot, arr, err, num);
+    res = xenforeignmemory_map(xch->fmem, dom, prot, arr, err, num);
     if (res) {
         for (i = 0; i < num; i++) {
             if (err[i]) {
@@ -100,53 +101,11 @@ void *xc_map_foreign_ranges(xc_interface *xch,
     return ret;
 }
 
-/*
- * stub for all not yet converted OSes (NetBSD and Solaris). New OSes should
- * just implement xc_map_foreign_bulk.
- */
-#if defined(__NetBSD__) || defined(__sun__)
-void *osdep_map_foreign_batch(xc_interface *xch, uint32_t dom, int prot,
-                              xen_pfn_t *arr, int num );
-void *xc_map_foreign_bulk(xc_interface *xch,
-                          uint32_t dom, int prot,
+void *xc_map_foreign_bulk(xc_interface *xch, uint32_t dom, int prot,
                           const xen_pfn_t *arr, int *err, unsigned int num)
 {
-    xen_pfn_t *pfn;
-    unsigned int i;
-    void *ret;
-
-    if ((int)num <= 0) {
-        errno = EINVAL;
-        return NULL;
-    }
-
-    pfn = malloc(num * sizeof(*pfn));
-    if (!pfn) {
-        errno = ENOMEM;
-        return NULL;
-    }
-
-    memcpy(pfn, arr, num * sizeof(*arr));
-    ret = osdep_map_foreign_batch(xch, dom, prot, pfn, num);
-
-    if (ret) {
-        for (i = 0; i < num; ++i)
-            switch (pfn[i] ^ arr[i]) {
-            case 0:
-                err[i] = 0;
-                break;
-            default:
-                err[i] = -EINVAL;
-                break;
-            }
-    } else
-        memset(err, 0, num * sizeof(*err));
-
-    free(pfn);
-
-    return ret;
+    return xenforeignmemory_map(xch->fmem, dom, prot, arr, err, num);
 }
-#endif
 
 /*
  * Local variables:
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index d8ca948..1799daa 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -39,40 +39,11 @@ void minios_interface_close_fd(int fd);
 
 extern void minios_interface_close_fd(int fd);
 
-int osdep_privcmd_open(xc_interface *xch)
-{
-    int fd = alloc_fd(FTYPE_XC);
-
-    if ( fd == -1)
-        return -1;
-
-    xch->privcmdfd = fd;
-    return 0;
-}
-
-int osdep_privcmd_close(xc_interface *xch)
-{
-    int fd = xch->privcmdfd;
-    return close(fd);
-}
-
 void minios_interface_close_fd(int fd)
 {
     files[fd].type = FTYPE_NONE;
 }
 
-void *xc_map_foreign_bulk(xc_interface *xch,
-                          uint32_t dom, int prot,
-                          const xen_pfn_t *arr, int *err, unsigned int num)
-{
-    unsigned long pt_prot = 0;
-    if (prot & PROT_READ)
-	pt_prot = L1_PROT_RO;
-    if (prot & PROT_WRITE)
-	pt_prot = L1_PROT;
-    return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush)
 {
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index 3470bc4..3197993 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -22,79 +22,6 @@
 #include <unistd.h>
 #include <fcntl.h>
 #include <malloc.h>
-#include <sys/mman.h>
-
-int osdep_privcmd_open(xc_interface *xch)
-{
-    int flags, saved_errno;
-    int fd = open("/kern/xen/privcmd", O_RDWR);
-
-    if ( fd == -1 )
-    {
-        PERROR("Could not obtain handle on privileged command interface");
-        return -1;
-    }
-
-    /* Although we return the file handle as the 'xc handle' the API
-       does not specify / guarentee that this integer is in fact
-       a file handle. Thus we must take responsiblity to ensure
-       it doesn't propagate (ie leak) outside the process */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-    flags |= FD_CLOEXEC;
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
-    xch->privcmdfd = fd;
-    return 0;
-
- error:
-    saved_errno = errno;
-    close(fd);
-    errno = saved_errno;
-    return -1;
-}
-
-int osdep_privcmd_close(xc_interface *xch)
-{
-    int fd = xch->privcmdfd;
-    return close(fd);
-}
-
-void *osdep_map_foreign_batch(xc_interface *xch,
-                              uint32_t dom, int prot,
-                              xen_pfn_t *arr, int num)
-{
-    int fd = xch->privcmdfd;
-    privcmd_mmapbatch_t ioctlx;
-    void *addr;
-    addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_ANON | MAP_SHARED, -1, 0);
-    if ( addr == MAP_FAILED ) {
-        PERROR("osdep_map_foreign_batch: mmap failed");
-        return NULL;
-    }
-
-    ioctlx.num=num;
-    ioctlx.dom=dom;
-    ioctlx.addr=(unsigned long)addr;
-    ioctlx.arr=arr;
-    if ( ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
-    {
-        int saved_errno = errno;
-        PERROR("osdep_map_foreign_batch: ioctl failed");
-        (void)munmap(addr, num*XC_PAGE_SIZE);
-        errno = saved_errno;
-        return NULL;
-    }
-    return addr;
-
-}
 
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index ca4c17a..c41e433 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -58,11 +58,12 @@ struct xc_interface_core *xc_interface_open(xentoollog_logger *logger,
     if (open_flags & XC_OPENFLAG_DUMMY)
         return xch; /* We are done */
 
-    if ( osdep_privcmd_open(xch) < 0 )
-        goto err;
-
     xch->xcall = xencall_open(xch->error_handler,
         open_flags & XC_OPENFLAG_NON_REENTRANT ? XENCALL_OPENFLAG_NON_REENTRANT : 0U);
+    if ( xch->xcall == NULL )
+        goto err;
+
+    xch->fmem = xenforeignmemory_open(xch->error_handler, 0);
 
     if ( xch->xcall == NULL )
         goto err;
@@ -70,7 +71,7 @@ struct xc_interface_core *xc_interface_open(xentoollog_logger *logger,
     return xch;
 
  err:
-    osdep_privcmd_close(xch);
+    xencall_close(xch->xcall);
     xtl_logger_destroy(xch->error_handler_tofree);
     if (xch != &xch_buf) free(xch);
     return NULL;
@@ -86,8 +87,8 @@ int xc_interface_close(xc_interface *xch)
     rc = xencall_close(xch->xcall);
     if (rc) PERROR("Could not close xencall interface");
 
-    rc = osdep_privcmd_close(xch);
-    if (rc) PERROR("Could not close hypervisor interface");
+    rc = xenforeignmemory_close(xch->fmem);
+    if (rc) PERROR("Could not close foreign memory interface");
 
     xtl_logger_destroy(xch->dombuild_logger_tofree);
     xtl_logger_destroy(xch->error_handler_tofree);
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index ecf2451..aa8daf1 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -29,8 +29,13 @@
 #include <sys/ioctl.h>
 
 #include "_paths.h"
+
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
+#define XC_INTERNAL_COMPAT_MAP_FOREIGN_API
 #include "xenctrl.h"
+
 #include <xencall.h>
+#include <xenforeignmemory.h>
 
 #include <xen/sys/privcmd.h>
 
@@ -94,11 +99,11 @@ struct xc_interface_core {
     FILE *dombuild_logger_file;
     const char *currently_progress_reporting;
 
-    /* Privcmd interface */
-    int privcmdfd;
-
     /* Hypercall interface */
     xencall_handle *xcall;
+
+    /* Foreign mappings */
+    xenforeignmemory_handle *fmem;
 };
 
 int osdep_privcmd_open(xc_interface *xch);
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index d686867..5128f3f 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -19,81 +19,8 @@
 
 #include "xc_private.h"
 
-#include <xen/memory.h>
-#include <unistd.h>
-#include <fcntl.h>
 #include <malloc.h>
 
-int osdep_privcmd_open(xc_interface *xch)
-{
-    int flags, saved_errno;
-    int fd = open("/dev/xen/privcmd", O_RDWR);
-
-    if ( fd == -1 )
-    {
-        PERROR("Could not obtain handle on privileged command interface");
-        return -1;
-    }
-
-    /* Although we return the file handle as the 'xc handle' the API
-       does not specify / guarentee that this integer is in fact
-       a file handle. Thus we must take responsiblity to ensure
-       it doesn't propagate (ie leak) outside the process */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-    flags |= FD_CLOEXEC;
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
-    xch->privcmdfd = fd;
-    return 0;
-
- error:
-    saved_errno = errno;
-    close(fd);
-    errno = saved_errno;
-    return -1;
-}
-
-int osdep_privcmd_close(xc_interface *xch)
-{
-    int fd = xch->privcmdfd;
-    return close(fd);
-}
-
-void *osdep_map_foreign_batch(xc_interface *xch,
-                              uint32_t dom, int prot,
-                              xen_pfn_t *arr, int num)
-{
-    int fd = xch->privcmdfd;
-    privcmd_mmapbatch_t ioctlx;
-    void *addr;
-    addr = mmap(NULL, num*XC_PAGE_SIZE, prot, MAP_SHARED, fd, 0);
-    if ( addr == MAP_FAILED )
-        return NULL;
-
-    ioctlx.num=num;
-    ioctlx.dom=dom;
-    ioctlx.addr=(unsigned long)addr;
-    ioctlx.arr=arr;
-    if ( ioctl(fd, IOCTL_PRIVCMD_MMAPBATCH, &ioctlx) < 0 )
-    {
-        int saved_errno = errno;
-        PERROR("XXXXXXXX");
-        (void)munmap(addr, num*XC_PAGE_SIZE);
-        errno = saved_errno;
-        return NULL;
-    }
-    return addr;
-
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c
index 05159bb..8e9e9c7 100644
--- a/tools/libxc/xc_sr_restore.c
+++ b/tools/libxc/xc_sr_restore.c
@@ -316,8 +316,8 @@ static int process_page_data(struct xc_sr_context *ctx, unsigned count,
     if ( nr_pages == 0 )
         goto done;
 
-    mapping = guest_page = xc_map_foreign_bulk(
-        xch, ctx->domid, PROT_READ | PROT_WRITE,
+    mapping = guest_page = xenforeignmemory_map(xch->fmem,
+        ctx->domid, PROT_READ | PROT_WRITE,
         mfns, map_errs, nr_pages);
     if ( !mapping )
     {
diff --git a/tools/libxc/xc_sr_save.c b/tools/libxc/xc_sr_save.c
index 88d85ef..1ff9635 100644
--- a/tools/libxc/xc_sr_save.c
+++ b/tools/libxc/xc_sr_save.c
@@ -153,8 +153,8 @@ static int write_batch(struct xc_sr_context *ctx)
 
     if ( nr_pages > 0 )
     {
-        guest_mapping = xc_map_foreign_bulk(
-            xch, ctx->domid, PROT_READ, mfns, errors, nr_pages);
+        guest_mapping = xenforeignmemory_map(xch->fmem,
+            ctx->domid, PROT_READ, mfns, errors, nr_pages);
         if ( !guest_mapping )
         {
             PERROR("Failed to map guest pages");
diff --git a/tools/libxc/xg_private.h b/tools/libxc/xg_private.h
index a76d6b3..175c1e5 100644
--- a/tools/libxc/xg_private.h
+++ b/tools/libxc/xg_private.h
@@ -26,9 +26,8 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#include "xenctrl.h"
-#include "xenguest.h"
 #include "xc_private.h"
+#include "xenguest.h"
 
 #include <xen/memory.h>
 #include <xen/elfnote.h>
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 77bf251..cd0f4cc 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -50,6 +50,7 @@
 
 #include <xenevtchn.h>
 #include <xenstore.h>
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xenguest.h>
 #include <xc_dom.h>
diff --git a/tools/misc/xen-mfndump.c b/tools/misc/xen-mfndump.c
index ceeeaa9..1b22ad7 100644
--- a/tools/misc/xen-mfndump.c
+++ b/tools/misc/xen-mfndump.c
@@ -1,3 +1,4 @@
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xc_private.h>
 #include <xc_core.h>
diff --git a/tools/ocaml/libs/xc/xenctrl_stubs.c b/tools/ocaml/libs/xc/xenctrl_stubs.c
index e2fa7e4..74928e9 100644
--- a/tools/ocaml/libs/xc/xenctrl_stubs.c
+++ b/tools/ocaml/libs/xc/xenctrl_stubs.c
@@ -29,6 +29,7 @@
 #include <stdint.h>
 #include <string.h>
 
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 
 #include "mmap_stubs.h"
diff --git a/tools/python/xen/lowlevel/xc/xc.c b/tools/python/xen/lowlevel/xc/xc.c
index 6dd79e0..c40a4e9 100644
--- a/tools/python/xen/lowlevel/xc/xc.c
+++ b/tools/python/xen/lowlevel/xc/xc.c
@@ -5,6 +5,7 @@
  */
 
 #include <Python.h>
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <fcntl.h>
 #include <netinet/in.h>
@@ -15,6 +16,7 @@
 #include <netdb.h>
 #include <arpa/inet.h>
 
+#include <xen/elfnote.h>
 #include <xen/tmem.h>
 #include "xc_dom.h"
 #include <xen/hvm/hvm_info_table.h>
diff --git a/tools/tests/mce-test/tools/xen-mceinj.c b/tools/tests/mce-test/tools/xen-mceinj.c
index d22f0ad..061ec7c 100644
--- a/tools/tests/mce-test/tools/xen-mceinj.c
+++ b/tools/tests/mce-test/tools/xen-mceinj.c
@@ -30,6 +30,7 @@
 #include <signal.h>
 #include <stdarg.h>
 
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xenguest.h>
 #include <inttypes.h>
diff --git a/tools/tests/mem-sharing/memshrtool.c b/tools/tests/mem-sharing/memshrtool.c
index 6454bc3..437c7c9 100644
--- a/tools/tests/mem-sharing/memshrtool.c
+++ b/tools/tests/mem-sharing/memshrtool.c
@@ -11,6 +11,7 @@
 #include <string.h>
 #include <sys/mman.h>
 
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include "xenctrl.h"
 
 static int usage(const char* prog)
diff --git a/tools/xenmon/xenbaked.c b/tools/xenmon/xenbaked.c
index da46126..eacacb0 100644
--- a/tools/xenmon/xenbaked.c
+++ b/tools/xenmon/xenbaked.c
@@ -38,6 +38,7 @@
 #include <errno.h>
 #include <signal.h>
 #include <xenevtchn.h>
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xen/xen.h>
 #include <string.h>
diff --git a/tools/xenpaging/pagein.c b/tools/xenpaging/pagein.c
index 7cb0f33..2a298b4 100644
--- a/tools/xenpaging/pagein.c
+++ b/tools/xenpaging/pagein.c
@@ -1,6 +1,5 @@
 /* Trigger a page-in in a separate thread-of-execution to avoid deadlock */
 #include <pthread.h>
-#include <xc_private.h>
 #include "xenpaging.h"
 
 struct page_in_args {
diff --git a/tools/xenpaging/xenpaging.c b/tools/xenpaging/xenpaging.c
index c4bc713..0377507 100644
--- a/tools/xenpaging/xenpaging.c
+++ b/tools/xenpaging/xenpaging.c
@@ -27,7 +27,6 @@
 #include <signal.h>
 #include <unistd.h>
 #include <poll.h>
-#include <xc_private.h>
 #include <xenstore.h>
 #include <getopt.h>
 
diff --git a/tools/xenpaging/xenpaging.h b/tools/xenpaging/xenpaging.h
index d0f8d20..d6c8ee5 100644
--- a/tools/xenpaging/xenpaging.h
+++ b/tools/xenpaging/xenpaging.h
@@ -25,6 +25,8 @@
 
 
 #include <xenevtchn.h>
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
+#include <xenctrl.h>
 #include <xc_private.h>
 #include <xen/event_channel.h>
 #include <xen/vm_event.h>
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index f62c192..51fb0b3 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -50,7 +50,6 @@
 #include "xenstored_watch.h"
 #include "xenstored_transaction.h"
 #include "xenstored_domain.h"
-#include "xenctrl.h"
 #include "tdb.h"
 
 #include "hashtable.h"
diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h
index 624737d..3a497f7 100644
--- a/tools/xenstore/xenstored_core.h
+++ b/tools/xenstore/xenstored_core.h
@@ -19,6 +19,7 @@
 #ifndef _XENSTORED_CORE_H
 #define _XENSTORED_CORE_H
 
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
 #include <xengnttab.h>
 
diff --git a/tools/xentrace/xenctx.c b/tools/xentrace/xenctx.c
index 4ed120a..e647179 100644
--- a/tools/xentrace/xenctx.c
+++ b/tools/xentrace/xenctx.c
@@ -25,7 +25,8 @@
 #include <getopt.h>
 #include <limits.h>
 
-#include "xenctrl.h"
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
+#include <xenctrl.h>
 #include <xen/foreign/x86_32.h>
 #include <xen/foreign/x86_64.h>
 #include <xen/hvm/save.h>
diff --git a/tools/xentrace/xentrace.c b/tools/xentrace/xentrace.c
index 6cbe0ac..f09fe6c 100644
--- a/tools/xentrace/xentrace.c
+++ b/tools/xentrace/xentrace.c
@@ -30,6 +30,7 @@
 #include <xen/xen.h>
 #include <xen/trace.h>
 
+#define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenevtchn.h>
 #include <xenctrl.h>
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 12/29] tools/libs/foreignmemory: provide xenforeignmemory_unmap.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (10 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 13/29] tools/libs/foreignmemory: use size_t for size arguments Ian Campbell
                     ` (16 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

And require it be used instead of direct munmap.

This will allow e.g. Valgrind hooks to help track incorrect use of
foreign mappings.

Switch all uses of xenforeignmemory_map to use
xenforeignmemory_unmap, not that foreign mappings via the libxc compat
xc_map_foreign_* interface will not take advantage of this and will
need converting.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v4: xenforeignmemory_unmap takes pages not bytes, adjust callers.
v6: Document error behaviour
    Use an osdep layer, although there is no current need for common
    code on _unmap there is for _map so use this indirection for
    consistency.
---
 tools/libs/foreignmemory/freebsd.c                  |  6 ++++++
 tools/libs/foreignmemory/include/xenforeignmemory.h | 11 +++++++++--
 tools/libs/foreignmemory/libxenforeignmemory.map    |  1 +
 tools/libs/foreignmemory/linux.c                    |  6 ++++++
 tools/libs/foreignmemory/minios.c                   |  6 ++++++
 tools/libs/foreignmemory/netbsd.c                   |  6 ++++++
 tools/libs/foreignmemory/solaris.c                  |  6 ++++++
 tools/libxc/xc_sr_restore.c                         |  2 +-
 tools/libxc/xc_sr_save.c                            |  2 +-
 tools/libxc/xc_vm_event.c                           |  2 +-
 10 files changed, 43 insertions(+), 5 deletions(-)

diff --git a/tools/libs/foreignmemory/freebsd.c b/tools/libs/foreignmemory/freebsd.c
index 4b2815d..3c3ad09 100644
--- a/tools/libs/foreignmemory/freebsd.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -118,6 +118,12 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
     return addr;
 }
 
+int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                                 void *addr, unsigned int num)
+{
+	return munmap(addr, num << PAGE_SHIFT);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
index 5b3fa7a..b783af3 100644
--- a/tools/libs/foreignmemory/include/xenforeignmemory.h
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -44,8 +44,8 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem);
 
 /*
  * Maps a range within one domain to a local address range.  Mappings
- * should be unmapped with munmap and should follow the same rules as mmap
- * regarding page alignment.
+ * must be unmapped with xenforeignmemory_unmap and should follow the
+ * same rules as mmap regarding page alignment.
  *
  * prot is as for mmap(2).
  *
@@ -57,6 +57,13 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem);
 void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
                            int prot, const xen_pfn_t *arr, int *err,
                            unsigned int num);
+/*
+ * Unmap a mapping previous created with xenforeignmemory_map().
+ *
+ * Returns 0 on success on failure sets errno and returns -1.
+ */
+int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                           void *addr, unsigned int num);
 
 #endif
 
diff --git a/tools/libs/foreignmemory/libxenforeignmemory.map b/tools/libs/foreignmemory/libxenforeignmemory.map
index 11f0d2b..df206b3 100644
--- a/tools/libs/foreignmemory/libxenforeignmemory.map
+++ b/tools/libs/foreignmemory/libxenforeignmemory.map
@@ -3,5 +3,6 @@ VERS_1.0 {
 		xenforeignmemory_open;
 		xenforeignmemory_close;
 		xenforeignmemory_map;
+		xenforeignmemory_unmap;
 	local: *; /* Do not expose anything by default */
 };
diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/linux.c
index b403ca7..40ecd41 100644
--- a/tools/libs/foreignmemory/linux.c
+++ b/tools/libs/foreignmemory/linux.c
@@ -282,6 +282,12 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
     return addr;
 }
 
+int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                                 void *addr, unsigned int num)
+{
+    return munmap(addr, (unsigned long)num << PAGE_SHIFT);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/foreignmemory/minios.c b/tools/libs/foreignmemory/minios.c
index a542e3d..5fcb861 100644
--- a/tools/libs/foreignmemory/minios.c
+++ b/tools/libs/foreignmemory/minios.c
@@ -51,6 +51,12 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
     return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
 }
 
+int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                                 void *addr, unsigned int num)
+{
+	return munmap(addr, num << PAGE_SHIFT);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/foreignmemory/netbsd.c b/tools/libs/foreignmemory/netbsd.c
index 704a096..740c26f 100644
--- a/tools/libs/foreignmemory/netbsd.c
+++ b/tools/libs/foreignmemory/netbsd.c
@@ -94,6 +94,12 @@ void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
 
 }
 
+int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                                 void *addr, unsigned int num)
+{
+	return munmap(addr, num*XC_PAGE_SIZE);
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/foreignmemory/solaris.c b/tools/libs/foreignmemory/solaris.c
index f4dd634..966fccb 100644
--- a/tools/libs/foreignmemory/solaris.c
+++ b/tools/libs/foreignmemory/solaris.c
@@ -91,3 +91,9 @@ void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
     }
     return addr;
 }
+
+int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
+                                 void *addr, unsigned int num)
+{
+	return munmap(addr, num*XC_PAGE_SIZE);
+}
diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c
index 8e9e9c7..a334efd 100644
--- a/tools/libxc/xc_sr_restore.c
+++ b/tools/libxc/xc_sr_restore.c
@@ -378,7 +378,7 @@ static int process_page_data(struct xc_sr_context *ctx, unsigned count,
 
  err:
     if ( mapping )
-        munmap(mapping, nr_pages * PAGE_SIZE);
+        xenforeignmemory_unmap(xch->fmem, mapping, nr_pages);
 
     free(map_errs);
     free(mfns);
diff --git a/tools/libxc/xc_sr_save.c b/tools/libxc/xc_sr_save.c
index 1ff9635..bed1673 100644
--- a/tools/libxc/xc_sr_save.c
+++ b/tools/libxc/xc_sr_save.c
@@ -263,7 +263,7 @@ static int write_batch(struct xc_sr_context *ctx)
  err:
     free(rec_pfns);
     if ( guest_mapping )
-        munmap(guest_mapping, nr_pages_mapped * PAGE_SIZE);
+        xenforeignmemory_unmap(xch->fmem, guest_mapping, nr_pages_mapped);
     for ( i = 0; local_pages && i < nr_pfns; ++i )
         free(local_pages[i]);
     free(iov);
diff --git a/tools/libxc/xc_vm_event.c b/tools/libxc/xc_vm_event.c
index d2d99e4..57abce0 100644
--- a/tools/libxc/xc_vm_event.c
+++ b/tools/libxc/xc_vm_event.c
@@ -148,7 +148,7 @@ void *xc_vm_event_enable(xc_interface *xch, domid_t domain_id, int param,
         }
 
         if ( ring_page )
-            munmap(ring_page, XC_PAGE_SIZE);
+            xenforeignmemory_unmap(xch->fmem, ring_page, 1);
         ring_page = NULL;
 
         errno = saved_errno;
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 13/29] tools/libs/foreignmemory: use size_t for size arguments.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (11 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 12/29] tools/libs/foreignmemory: provide xenforeignmemory_unmap Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs Ian Campbell
                     ` (15 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Surprisingly it appears no callers need updating.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v4: New patch
---
 tools/libs/foreignmemory/compat.c                  |  2 +-
 tools/libs/foreignmemory/freebsd.c                 |  4 ++--
 .../libs/foreignmemory/include/xenforeignmemory.h  |  5 +++--
 tools/libs/foreignmemory/linux.c                   | 23 +++++++++++-----------
 tools/libs/foreignmemory/minios.c                  |  4 ++--
 tools/libs/foreignmemory/netbsd.c                  |  2 +-
 tools/libs/foreignmemory/solaris.c                 |  2 +-
 7 files changed, 22 insertions(+), 20 deletions(-)

diff --git a/tools/libs/foreignmemory/compat.c b/tools/libs/foreignmemory/compat.c
index b8c6fc6..039297c 100644
--- a/tools/libs/foreignmemory/compat.c
+++ b/tools/libs/foreignmemory/compat.c
@@ -23,7 +23,7 @@
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, unsigned int num)
+                                 const xen_pfn_t *arr, int *err, size_t num)
 {
     xen_pfn_t *pfn;
     unsigned int i;
diff --git a/tools/libs/foreignmemory/freebsd.c b/tools/libs/foreignmemory/freebsd.c
index 3c3ad09..ed26ebb 100644
--- a/tools/libs/foreignmemory/freebsd.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -85,7 +85,7 @@ int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
                                  const xen_pfn_t *arr, int *err,
-                                 unsigned int num)
+                                 size_t num)
 {
     int fd = fmem->fd;
     privcmd_mmapbatch_t ioctlx;
@@ -119,7 +119,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
 }
 
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
-                                 void *addr, unsigned int num)
+                                 void *addr, size_t num)
 {
 	return munmap(addr, num << PAGE_SHIFT);
 }
diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
index b783af3..04ff548 100644
--- a/tools/libs/foreignmemory/include/xenforeignmemory.h
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -56,14 +56,15 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem);
  */
 void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
                            int prot, const xen_pfn_t *arr, int *err,
-                           unsigned int num);
+                           size_t pages);
+
 /*
  * Unmap a mapping previous created with xenforeignmemory_map().
  *
  * Returns 0 on success on failure sets errno and returns -1.
  */
 int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
-                           void *addr, unsigned int num);
+                           void *addr, size_t pages);
 
 #endif
 
diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/linux.c
index 40ecd41..6620391 100644
--- a/tools/libs/foreignmemory/linux.c
+++ b/tools/libs/foreignmemory/linux.c
@@ -115,10 +115,11 @@ static int map_foreign_batch_single(int fd, uint32_t dom,
  * This will keep the request ring full and avoids delays.
  */
 static int retry_paged(int fd, uint32_t dom, void *addr,
-                       const xen_pfn_t *arr, int *err, unsigned int num)
+                       const xen_pfn_t *arr, int *err, size_t num)
 {
     privcmd_mmapbatch_v2_t ioctlx;
-    int rc, paged = 0, i = 0;
+    int rc, paged = 0;
+    size_t i = 0;
 
     do
     {
@@ -134,7 +135,7 @@ static int retry_paged(int fd, uint32_t dom, void *addr,
         /* At least one gfn is still in paging state */
         ioctlx.num = 1;
         ioctlx.dom = dom;
-        ioctlx.addr = (unsigned long)addr + ((unsigned long)i<<PAGE_SHIFT);
+        ioctlx.addr = (unsigned long)addr + (i<<PAGE_SHIFT);
         ioctlx.arr = arr + i;
         ioctlx.err = err + i;
 
@@ -160,15 +161,15 @@ out:
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, unsigned int num)
+                                 const xen_pfn_t *arr, int *err, size_t num)
 {
     int fd = fmem->fd;
     privcmd_mmapbatch_v2_t ioctlx;
     void *addr;
-    unsigned int i;
+    size_t i;
     int rc;
 
-    addr = mmap(NULL, (unsigned long)num << PAGE_SHIFT, prot, MAP_SHARED,
+    addr = mmap(NULL, num << PAGE_SHIFT, prot, MAP_SHARED,
                 fd, 0);
     if ( addr == MAP_FAILED )
     {
@@ -212,7 +213,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
             if ( pfn == MAP_FAILED )
             {
                 PERROR("mmap of pfn array failed");
-                (void)munmap(addr, (unsigned long)num << PAGE_SHIFT);
+                (void)munmap(addr, num << PAGE_SHIFT);
                 return NULL;
             }
         }
@@ -245,7 +246,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                     continue;
                 }
                 rc = map_foreign_batch_single(fd, dom, pfn + i,
-                        (unsigned long)addr + ((unsigned long)i<<PAGE_SHIFT));
+                        (unsigned long)addr + (i<<PAGE_SHIFT));
                 if ( rc < 0 )
                 {
                     rc = -errno;
@@ -274,7 +275,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
         int saved_errno = errno;
 
         PERROR("ioctl failed");
-        (void)munmap(addr, (unsigned long)num << PAGE_SHIFT);
+        (void)munmap(addr, num << PAGE_SHIFT);
         errno = saved_errno;
         return NULL;
     }
@@ -283,9 +284,9 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
 }
 
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
-                                 void *addr, unsigned int num)
+                                 void *addr, size_t num)
 {
-    return munmap(addr, (unsigned long)num << PAGE_SHIFT);
+    return munmap(addr, num << PAGE_SHIFT);
 }
 
 /*
diff --git a/tools/libs/foreignmemory/minios.c b/tools/libs/foreignmemory/minios.c
index 5fcb861..bdc1239 100644
--- a/tools/libs/foreignmemory/minios.c
+++ b/tools/libs/foreignmemory/minios.c
@@ -41,7 +41,7 @@ int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, unsigned int num)
+                                 const xen_pfn_t *arr, int *err, size_t num)
 {
     unsigned long pt_prot = 0;
     if (prot & PROT_READ)
@@ -52,7 +52,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
 }
 
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
-                                 void *addr, unsigned int num)
+                                 void *addr, size_t num)
 {
 	return munmap(addr, num << PAGE_SHIFT);
 }
diff --git a/tools/libs/foreignmemory/netbsd.c b/tools/libs/foreignmemory/netbsd.c
index 740c26f..d0d233a 100644
--- a/tools/libs/foreignmemory/netbsd.c
+++ b/tools/libs/foreignmemory/netbsd.c
@@ -95,7 +95,7 @@ void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
 }
 
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
-                                 void *addr, unsigned int num)
+                                 void *addr, size_t num)
 {
 	return munmap(addr, num*XC_PAGE_SIZE);
 }
diff --git a/tools/libs/foreignmemory/solaris.c b/tools/libs/foreignmemory/solaris.c
index 966fccb..f1c44bc 100644
--- a/tools/libs/foreignmemory/solaris.c
+++ b/tools/libs/foreignmemory/solaris.c
@@ -93,7 +93,7 @@ void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
 }
 
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
-                                 void *addr, unsigned int num)
+                                 void *addr, size_t num)
 {
 	return munmap(addr, num*XC_PAGE_SIZE);
 }
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (12 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 13/29] tools/libs/foreignmemory: use size_t for size arguments Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:22   ` [PATCH XEN v8 15/29] tools/libs/foreignmemory: Support err == NULL to map Ian Campbell
                     ` (14 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v6: Also discuss recovering the memory.

v7: Further clarifications regarding forking based on ML discussions.
    (Dropped Wei's ack)
---
 .../libs/foreignmemory/include/xenforeignmemory.h  | 33 +++++++++++++++++++++-
 1 file changed, 32 insertions(+), 1 deletion(-)

diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
index 04ff548..a6d1bdb 100644
--- a/tools/libs/foreignmemory/include/xenforeignmemory.h
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -32,13 +32,44 @@ typedef struct xentoollog_logger xentoollog_logger;
 typedef struct xenforeignmemory_handle xenforeignmemory_handle;
 
 /*
- * Return a handle onto the hypercall driver.  Logs errors.
+ * Return a handle onto the foreign memory mapping driver.  Logs errors.
+ *
+ * Note: After fork(2) a child process must not use any opened
+ * foreignmemory handle inherited from their parent, nor access any
+ * grant mapped areas associated with that handle.
+ *
+ * The child must open a new handle if they want to interact with
+ * foreignmemory.
+ *
+ * Calling exec(2) in a child will safely (and reliably) reclaim any
+ * resources which were allocated via a xenforeignmemory_handle in the
+ * parent.
+ *
+ * A child which does not call exec(2) may safely call
+ * xenforeignmemory_close() on a xenforeignmemory_handle inherited
+ * from their parent. This will attempt to reclaim any resources
+ * associated with that handle. Note that in some implementations this
+ * reclamation may not be completely effective, in this case any
+ * affected resources remain allocated.
+ *
+ * Calling xenforeignmemory_close() is the only safe operation on a
+ * xenforeignmemory_handle which has been inherited.
  */
 xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
                                                unsigned open_flags);
 
 /*
  * Close a handle previously allocated with xenforeignmemory_open().
+ *
+ * Under normal circumstances (i.e. not in the child after a fork)
+ * xenforeignmemory_unmap() should be used on all mappings allocated
+ * by xenforeignmemory_map() prior to closing the handle in order to
+ * free up resources associated with those mappings.
+ *
+ * This is the only function which may be safely called on a
+ * xenforeignmemory_handle in a child after a
+ * fork. xenforeignmemory_unmap() must not be called under such
+ * circumstances.
  */
 int xenforeignmemory_close(xenforeignmemory_handle *fmem);
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 15/29] tools/libs/foreignmemory: Support err == NULL to map.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (13 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 16/29] tools/libs/foreignmemory: pull array length argument to map forward Ian Campbell
                     ` (13 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

The existing xc_map_foreign_bulk-like interface encourages callers to
miss error checking for partial failure (by forgetting to scan the err
array).

Add support for passing err==NULL which behaves in a
xc_map_foreign_pages-like manner and returns a global error for any
failure.

While documenting this also clarify the overall behaviour and the
behaviour with err!=NULL.

With this the compat wrapper of xc_map_foreign_pages() can be
simplified.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v7: Check for NULL when allocating err.

v6: New
---
 tools/libs/foreignmemory/core.c                    | 33 +++++++++++++++++++++-
 .../libs/foreignmemory/include/xenforeignmemory.h  | 24 ++++++++++++++--
 tools/libxc/xc_foreign_memory.c                    | 22 +--------------
 3 files changed, 54 insertions(+), 25 deletions(-)

diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/core.c
index 21dc7ee..4e0541f 100644
--- a/tools/libs/foreignmemory/core.c
+++ b/tools/libs/foreignmemory/core.c
@@ -14,6 +14,8 @@
  */
 
 #include <stdlib.h>
+#include <assert.h>
+#include <errno.h>
 
 #include "private.h"
 
@@ -64,7 +66,36 @@ void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
                            uint32_t dom, int prot,
                            const xen_pfn_t *arr, int *err, size_t num)
 {
-    return osdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num);
+    void *ret;
+    int *err_to_free = NULL;
+
+    if ( err == NULL )
+        err = err_to_free = malloc(num * sizeof(int));
+
+    if ( err == NULL )
+        return NULL;
+
+    ret = osdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num);
+
+    if ( ret == 0 && err_to_free )
+    {
+        int i;
+
+        for ( i = 0 ; i < num ; i++ )
+        {
+            if ( err[i] )
+            {
+                errno = -err[i];
+                (void)osdep_xenforeignmemory_unmap(fmem, ret, num);
+                ret = NULL;
+                break;
+            }
+        }
+    }
+
+    free(err_to_free);
+
+    return ret;
 }
 
 int xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
index a6d1bdb..0ec6325 100644
--- a/tools/libs/foreignmemory/include/xenforeignmemory.h
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -80,10 +80,28 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem);
  *
  * prot is as for mmap(2).
  *
- * Can partially succeed. When a page cannot be mapped, its respective
- * field in @err is set to the corresponding errno value.
+ * @arr is an array of @pages gfns to be mapped linearly in the local
+ * address range. @err is an (optional) output array used to report
+ * per-page errors, as errno values.
  *
- * Returns NULL if no pages can be mapped.
+ * If @err is given (is non-NULL) then the mapping may partially
+ * succeed and return a valid pointer while also using @err to
+ * indicate the success (0) or failure (errno value) of the individual
+ * pages. The global errno thread local variable is not valid in this
+ * case.
+ *
+ * If @err is not given (is NULL) then on failure to map any page any
+ * successful mappings will be undone and NULL will be returned. errno
+ * will be set to correspond to the first failure (which may not be
+ * the most critical).
+ *
+ * It is also possible to return NULL due to a complete failure,
+ * i.e. failure to even attempt the mapping, in this case the global
+ * errno will have been set and the contents of @err (if given) is
+ * invalid.
+ *
+ * Note that it is also possible to return non-NULL with the contents
+ * of @err indicating failure to map every page.
  */
 void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
                            int prot, const xen_pfn_t *arr, int *err,
diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index 1737c10..4b24388 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -23,32 +23,12 @@
 void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
                            const xen_pfn_t *arr, int num)
 {
-    void *res;
-    int i, *err;
-
     if (num < 0) {
         errno = EINVAL;
         return NULL;
     }
 
-    err = malloc(num * sizeof(*err));
-    if (!err)
-        return NULL;
-
-    res = xenforeignmemory_map(xch->fmem, dom, prot, arr, err, num);
-    if (res) {
-        for (i = 0; i < num; i++) {
-            if (err[i]) {
-                errno = -err[i];
-                munmap(res, num * PAGE_SIZE);
-                res = NULL;
-                break;
-            }
-        }
-    }
-
-    free(err);
-    return res;
+    return xenforeignmemory_map(xch->fmem, dom, prot, arr, NULL, num);
 }
 
 void *xc_map_foreign_range(xc_interface *xch,
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 16/29] tools/libs/foreignmemory: pull array length argument to map forward
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (14 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 15/29] tools/libs/foreignmemory: Support err == NULL to map Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 17/29] tools/libs/evtchn: Review and update doc comments Ian Campbell
                     ` (12 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

By having the "num" argument before the page and error arrays we can
potentially use a variable-length-array argument ("int pages[num]") in
the function prototype.

However VLAs are a C99 feature and we are currently targetting C89 and
later, so we don't actually make use of this here, merely arrange that
we can switch to VLAs in the future without changing the function ABI.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v6: New
---
 tools/libs/foreignmemory/compat.c                   | 4 ++--
 tools/libs/foreignmemory/core.c                     | 5 +++--
 tools/libs/foreignmemory/freebsd.c                  | 4 ++--
 tools/libs/foreignmemory/include/xenforeignmemory.h | 4 ++--
 tools/libs/foreignmemory/linux.c                    | 3 ++-
 tools/libs/foreignmemory/minios.c                   | 3 ++-
 tools/libs/foreignmemory/private.h                  | 3 ++-
 tools/libxc/xc_foreign_memory.c                     | 4 ++--
 tools/libxc/xc_sr_restore.c                         | 2 +-
 tools/libxc/xc_sr_save.c                            | 2 +-
 10 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/tools/libs/foreignmemory/compat.c b/tools/libs/foreignmemory/compat.c
index 039297c..b79ec1a 100644
--- a/tools/libs/foreignmemory/compat.c
+++ b/tools/libs/foreignmemory/compat.c
@@ -22,8 +22,8 @@
 #include "private.h"
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
-                                 uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, size_t num)
+                                 uint32_t dom, int prot, size_t num,
+                                 const xen_pfn_t arr[/*num*/], int err[/*num*/])
 {
     xen_pfn_t *pfn;
     unsigned int i;
diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/core.c
index 4e0541f..cfb0a73 100644
--- a/tools/libs/foreignmemory/core.c
+++ b/tools/libs/foreignmemory/core.c
@@ -64,7 +64,8 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem)
 
 void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
                            uint32_t dom, int prot,
-                           const xen_pfn_t *arr, int *err, size_t num)
+                           size_t num,
+                           const xen_pfn_t arr[/*num*/], int err[/*num*/])
 {
     void *ret;
     int *err_to_free = NULL;
@@ -75,7 +76,7 @@ void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
     if ( err == NULL )
         return NULL;
 
-    ret = osdep_xenforeignmemory_map(fmem, dom, prot, arr, err, num);
+    ret = osdep_xenforeignmemory_map(fmem, dom, prot, num, arr, err);
 
     if ( ret == 0 && err_to_free )
     {
diff --git a/tools/libs/foreignmemory/freebsd.c b/tools/libs/foreignmemory/freebsd.c
index ed26ebb..703754f 100644
--- a/tools/libs/foreignmemory/freebsd.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -84,8 +84,8 @@ int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err,
-                                 size_t num)
+                                 size_t num,
+                                 const xen_pfn_t arr[/*num*/], int err[/*num*/])
 {
     int fd = fmem->fd;
     privcmd_mmapbatch_t ioctlx;
diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
index 0ec6325..3724c63 100644
--- a/tools/libs/foreignmemory/include/xenforeignmemory.h
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -104,8 +104,8 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem);
  * of @err indicating failure to map every page.
  */
 void *xenforeignmemory_map(xenforeignmemory_handle *fmem, uint32_t dom,
-                           int prot, const xen_pfn_t *arr, int *err,
-                           size_t pages);
+                           int prot, size_t pages,
+                           const xen_pfn_t arr[/*pages*/], int err[/*pages*/]);
 
 /*
  * Unmap a mapping previous created with xenforeignmemory_map().
diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/linux.c
index 6620391..32b6def 100644
--- a/tools/libs/foreignmemory/linux.c
+++ b/tools/libs/foreignmemory/linux.c
@@ -161,7 +161,8 @@ out:
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, size_t num)
+                                 size_t num,
+                                 const xen_pfn_t arr[/*num*/], int err[/*num*/])
 {
     int fd = fmem->fd;
     privcmd_mmapbatch_v2_t ioctlx;
diff --git a/tools/libs/foreignmemory/minios.c b/tools/libs/foreignmemory/minios.c
index bdc1239..ca5ba71 100644
--- a/tools/libs/foreignmemory/minios.c
+++ b/tools/libs/foreignmemory/minios.c
@@ -41,7 +41,8 @@ int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, size_t num)
+                                 size_t num,
+                                 const xen_pfn_t arr[/*num*/], int err[/*num*/])
 {
     unsigned long pt_prot = 0;
     if (prot & PROT_READ)
diff --git a/tools/libs/foreignmemory/private.h b/tools/libs/foreignmemory/private.h
index 36a71ec..cd732fd 100644
--- a/tools/libs/foreignmemory/private.h
+++ b/tools/libs/foreignmemory/private.h
@@ -27,7 +27,8 @@ int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem);
 
 void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
                                  uint32_t dom, int prot,
-                                 const xen_pfn_t *arr, int *err, size_t num);
+                                 size_t num,
+                                 const xen_pfn_t arr[num], int err[num]);
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
                                  void *addr, size_t num);
 
diff --git a/tools/libxc/xc_foreign_memory.c b/tools/libxc/xc_foreign_memory.c
index 4b24388..4053d26 100644
--- a/tools/libxc/xc_foreign_memory.c
+++ b/tools/libxc/xc_foreign_memory.c
@@ -28,7 +28,7 @@ void *xc_map_foreign_pages(xc_interface *xch, uint32_t dom, int prot,
         return NULL;
     }
 
-    return xenforeignmemory_map(xch->fmem, dom, prot, arr, NULL, num);
+    return xenforeignmemory_map(xch->fmem, dom, prot, num, arr, NULL);
 }
 
 void *xc_map_foreign_range(xc_interface *xch,
@@ -84,7 +84,7 @@ void *xc_map_foreign_ranges(xc_interface *xch,
 void *xc_map_foreign_bulk(xc_interface *xch, uint32_t dom, int prot,
                           const xen_pfn_t *arr, int *err, unsigned int num)
 {
-    return xenforeignmemory_map(xch->fmem, dom, prot, arr, err, num);
+    return xenforeignmemory_map(xch->fmem, dom, prot, num, arr, err);
 }
 
 /*
diff --git a/tools/libxc/xc_sr_restore.c b/tools/libxc/xc_sr_restore.c
index a334efd..d6d2397 100644
--- a/tools/libxc/xc_sr_restore.c
+++ b/tools/libxc/xc_sr_restore.c
@@ -318,7 +318,7 @@ static int process_page_data(struct xc_sr_context *ctx, unsigned count,
 
     mapping = guest_page = xenforeignmemory_map(xch->fmem,
         ctx->domid, PROT_READ | PROT_WRITE,
-        mfns, map_errs, nr_pages);
+        nr_pages, mfns, map_errs);
     if ( !mapping )
     {
         rc = -1;
diff --git a/tools/libxc/xc_sr_save.c b/tools/libxc/xc_sr_save.c
index bed1673..ab76498 100644
--- a/tools/libxc/xc_sr_save.c
+++ b/tools/libxc/xc_sr_save.c
@@ -154,7 +154,7 @@ static int write_batch(struct xc_sr_context *ctx)
     if ( nr_pages > 0 )
     {
         guest_mapping = xenforeignmemory_map(xch->fmem,
-            ctx->domid, PROT_READ, mfns, errors, nr_pages);
+            ctx->domid, PROT_READ, nr_pages, mfns, errors);
         if ( !guest_mapping )
         {
             PERROR("Failed to map guest pages");
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 17/29] tools/libs/evtchn: Review and update doc comments.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (15 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 16/29] tools/libs/foreignmemory: pull array length argument to map forward Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 18/29] tools/libs/evtchn: Use uint32_t for domid arguments Ian Campbell
                     ` (11 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Remove the reference to pre-4.1, since this is now a new library.

Fixup references to xc.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
 tools/libs/evtchn/include/xenevtchn.h | 6 ++----
 1 file changed, 2 insertions(+), 4 deletions(-)

diff --git a/tools/libs/evtchn/include/xenevtchn.h b/tools/libs/evtchn/include/xenevtchn.h
index 3380fa3..60da2a3 100644
--- a/tools/libs/evtchn/include/xenevtchn.h
+++ b/tools/libs/evtchn/include/xenevtchn.h
@@ -46,11 +46,9 @@ typedef struct xentoollog_logger xentoollog_logger;
  * which case errno will be set appropriately.
  *
  * Note:
- * After fork a child process must not use any opened xc evtchn
+ * After fork a child process must not use any opened evtchn
  * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
- *
- * Before Xen pre-4.1 this function would sometimes report errors with perror.
+ * they want to interact with evtchn.
  */
 /* Currently no flags are defined */
 xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 18/29] tools/libs/evtchn: Use uint32_t for domid arguments
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (16 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 17/29] tools/libs/evtchn: Review and update doc comments Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:22   ` [PATCH XEN v8 19/29] tools/libs: Clean up hard tabs Ian Campbell
                     ` (10 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v8: New
---
 tools/libs/evtchn/freebsd.c           | 4 ++--
 tools/libs/evtchn/include/xenevtchn.h | 4 ++--
 tools/libs/evtchn/linux.c             | 4 ++--
 tools/libs/evtchn/minios.c            | 4 ++--
 tools/libs/evtchn/netbsd.c            | 4 ++--
 tools/libs/evtchn/solaris.c           | 4 ++--
 6 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/tools/libs/evtchn/freebsd.c b/tools/libs/evtchn/freebsd.c
index 636f052..6479f7c 100644
--- a/tools/libs/evtchn/freebsd.c
+++ b/tools/libs/evtchn/freebsd.c
@@ -62,7 +62,7 @@ int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid)
 {
     int ret, fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
@@ -74,7 +74,7 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int do
 }
 
 evtchn_port_or_error_t
-xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid, evtchn_port_t remote_port)
+xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint_32 domid, evtchn_port_t remote_port)
 {
     int ret, fd = xce->fd;
     struct ioctl_evtchn_bind_interdomain bind;
diff --git a/tools/libs/evtchn/include/xenevtchn.h b/tools/libs/evtchn/include/xenevtchn.h
index 60da2a3..428d54c 100644
--- a/tools/libs/evtchn/include/xenevtchn.h
+++ b/tools/libs/evtchn/include/xenevtchn.h
@@ -86,14 +86,14 @@ int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port);
  * domain ID, or -1 on failure, in which case errno will be set appropriately.
  */
 evtchn_port_or_error_t
-xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid);
+xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid);
 
 /*
  * Returns a new event port bound to the remote port for the given domain ID,
  * or -1 on failure, in which case errno will be set appropriately.
  */
 evtchn_port_or_error_t
-xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
                            evtchn_port_t remote_port);
 
 /*
diff --git a/tools/libs/evtchn/linux.c b/tools/libs/evtchn/linux.c
index 27fd6e9..76cf0ac 100644
--- a/tools/libs/evtchn/linux.c
+++ b/tools/libs/evtchn/linux.c
@@ -61,7 +61,7 @@ int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
 }
 
 evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
-                                                   int domid)
+                                                   uint32_t domid)
 {
     int fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
@@ -72,7 +72,7 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
 }
 
 evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
-                                                  int domid,
+                                                  uint32_t domid,
                                                   evtchn_port_t remote_port)
 {
     int fd = xce->fd;
diff --git a/tools/libs/evtchn/minios.c b/tools/libs/evtchn/minios.c
index b839cd0..773942d 100644
--- a/tools/libs/evtchn/minios.c
+++ b/tools/libs/evtchn/minios.c
@@ -128,7 +128,7 @@ static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
     wake_up(&event_queue);
 }
 
-evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid)
 {
     int fd = xce->fd;
     struct evtchn_port_info *port_info;
@@ -155,7 +155,7 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int do
     return port;
 }
 
-evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
                                                   evtchn_port_t remote_port)
 {
     int fd = xce->fd;
diff --git a/tools/libs/evtchn/netbsd.c b/tools/libs/evtchn/netbsd.c
index c4123fe..1472ca6 100644
--- a/tools/libs/evtchn/netbsd.c
+++ b/tools/libs/evtchn/netbsd.c
@@ -62,7 +62,7 @@ int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, int domid)
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, uint32_t domid)
 {
     int fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
@@ -77,7 +77,7 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, int d
 	return -1;
 }
 
-evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
                                                   evtchn_port_t remote_port)
 {
     int fd = xce->fd;
diff --git a/tools/libs/evtchn/solaris.c b/tools/libs/evtchn/solaris.c
index 114cefb..b4720cd 100644
--- a/tools/libs/evtchn/solaris.c
+++ b/tools/libs/evtchn/solaris.c
@@ -65,7 +65,7 @@ int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
     return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
 }
 
-evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32_t domid)
 {
     int fd = xce->fd;
     struct ioctl_evtchn_bind_unbound_port bind;
@@ -75,7 +75,7 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int do
     return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
 }
 
-evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
                                                   evtchn_port_t remote_port)
 {
     int fd = xce->fd;
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 19/29] tools/libs: Clean up hard tabs.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (17 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 18/29] tools/libs/evtchn: Use uint32_t for domid arguments Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-15 13:22   ` [PATCH XEN v8 20/29] tools/libs/gnttab: Extensive updates to API documentation Ian Campbell
                     ` (9 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

These were wrong in the context of libxc before this code was
extracted, clean them up.

Also add some emacs magic blocks

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libs/call/buffer.c              |  4 ++--
 tools/libs/call/core.c                |  2 +-
 tools/libs/call/minios.c              |  4 ++--
 tools/libs/evtchn/minios.c            | 28 ++++++++++++++--------------
 tools/libs/evtchn/netbsd.c            | 12 ++++++------
 tools/libs/foreignmemory/freebsd.c    |  2 +-
 tools/libs/foreignmemory/minios.c     |  6 +++---
 tools/libs/foreignmemory/netbsd.c     |  2 +-
 tools/libs/foreignmemory/private.h    |  2 +-
 tools/libs/foreignmemory/solaris.c    | 12 +++++++++++-
 tools/libs/gnttab/include/xengnttab.h |  4 ++--
 tools/libs/gnttab/linux.c             |  6 +++---
 tools/libs/toollog/xtl_logger_stdio.c |  2 +-
 13 files changed, 48 insertions(+), 38 deletions(-)

diff --git a/tools/libs/call/buffer.c b/tools/libs/call/buffer.c
index 1a1b27a..2d8fc29 100644
--- a/tools/libs/call/buffer.c
+++ b/tools/libs/call/buffer.c
@@ -20,7 +20,7 @@
 #include "private.h"
 
 #define DBGPRINTF(_m...) \
-	xtl_log(xcall->logger, XTL_DEBUG, -1, "xencall:buffer", _m)
+    xtl_log(xcall->logger, XTL_DEBUG, -1, "xencall:buffer", _m)
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
@@ -86,7 +86,7 @@ static int cache_free(xencall_handle *xcall, void *p, size_t nr_pages)
     xcall->buffer_current_allocations--;
 
     if ( nr_pages == 1 &&
-	 xcall->buffer_cache_nr < BUFFER_CACHE_SIZE )
+         xcall->buffer_cache_nr < BUFFER_CACHE_SIZE )
     {
         xcall->buffer_cache[xcall->buffer_cache_nr++] = p;
         rc = 1;
diff --git a/tools/libs/call/core.c b/tools/libs/call/core.c
index a342871..bbf88de 100644
--- a/tools/libs/call/core.c
+++ b/tools/libs/call/core.c
@@ -19,7 +19,7 @@
 
 xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags)
 {
-	xencall_handle *xcall = malloc(sizeof(*xcall));
+    xencall_handle *xcall = malloc(sizeof(*xcall));
     int rc;
 
     if (!xcall) return NULL;
diff --git a/tools/libs/call/minios.c b/tools/libs/call/minios.c
index 3bee7be..f04688f 100644
--- a/tools/libs/call/minios.c
+++ b/tools/libs/call/minios.c
@@ -50,8 +50,8 @@ int osdep_hypercall(xencall_handle *xcall, privcmd_hypercall_t *hypercall)
     ret = HYPERVISOR_multicall(&call, 1);
 
     if (ret < 0) {
-	errno = -ret;
-	return -1;
+        errno = -ret;
+        return -1;
     }
     if ((long) call.result < 0) {
         errno = - (long) call.result;
diff --git a/tools/libs/evtchn/minios.c b/tools/libs/evtchn/minios.c
index 773942d..2c89952 100644
--- a/tools/libs/evtchn/minios.c
+++ b/tools/libs/evtchn/minios.c
@@ -103,8 +103,8 @@ int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
     ret = notify_remote_via_evtchn(port);
 
     if (ret < 0) {
-	errno = -ret;
-	ret = -1;
+        errno = -ret;
+        ret = -1;
     }
     return ret;
 }
@@ -138,16 +138,16 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, uint32
     assert(get_current() == main_thread);
     port_info = port_alloc(fd);
     if (port_info == NULL)
-	return -1;
+        return -1;
 
     printf("xenevtchn_bind_unbound_port(%d)", domid);
     ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)fd, &port);
     printf(" = %d\n", ret);
 
     if (ret < 0) {
-	port_dealloc(port_info);
-	errno = -ret;
-	return -1;
+        port_dealloc(port_info);
+        errno = -ret;
+        return -1;
     }
     port_info->bound = 1;
     port_info->port = port;
@@ -166,16 +166,16 @@ evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_
     assert(get_current() == main_thread);
     port_info = port_alloc(fd);
     if (port_info == NULL)
-	return -1;
+        return -1;
 
     printf("xenevtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
     ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)fd, &local_port);
     printf(" = %d\n", ret);
 
     if (ret < 0) {
-	port_dealloc(port_info);
-	errno = -ret;
-	return -1;
+        port_dealloc(port_info);
+        errno = -ret;
+        return -1;
     }
     port_info->bound = 1;
     port_info->port = local_port;
@@ -208,15 +208,15 @@ evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int v
     assert(get_current() == main_thread);
     port_info = port_alloc(fd);
     if (port_info == NULL)
-	return -1;
+        return -1;
 
     printf("xenevtchn_bind_virq(%d)", virq);
     port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)fd);
 
     if (port < 0) {
-	port_dealloc(port_info);
-	errno = -port;
-	return -1;
+        port_dealloc(port_info);
+        errno = -port;
+        return -1;
     }
     port_info->bound = 1;
     port_info->port = port;
diff --git a/tools/libs/evtchn/netbsd.c b/tools/libs/evtchn/netbsd.c
index 1472ca6..77ae51f 100644
--- a/tools/libs/evtchn/netbsd.c
+++ b/tools/libs/evtchn/netbsd.c
@@ -72,9 +72,9 @@ evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, uint3
 
     ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
     if (ret == 0)
-	return bind.port;
+        return bind.port;
     else
-	return -1;
+        return -1;
 }
 
 evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_t domid,
@@ -89,9 +89,9 @@ evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, uint32_
 
     ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
     if (ret == 0)
-	return bind.port;
+        return bind.port;
     else
-	return -1;
+        return -1;
 }
 
 int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
@@ -114,9 +114,9 @@ evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int v
 
     err = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
     if (err)
-	return -1;
+        return -1;
     else
-	return bind.port;
+        return bind.port;
 }
 
 evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
diff --git a/tools/libs/foreignmemory/freebsd.c b/tools/libs/foreignmemory/freebsd.c
index 703754f..38138dc 100644
--- a/tools/libs/foreignmemory/freebsd.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -121,7 +121,7 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
                                  void *addr, size_t num)
 {
-	return munmap(addr, num << PAGE_SHIFT);
+    return munmap(addr, num << PAGE_SHIFT);
 }
 
 /*
diff --git a/tools/libs/foreignmemory/minios.c b/tools/libs/foreignmemory/minios.c
index ca5ba71..6dc97bd 100644
--- a/tools/libs/foreignmemory/minios.c
+++ b/tools/libs/foreignmemory/minios.c
@@ -46,16 +46,16 @@ void *osdep_xenforeignmemory_map(xenforeignmemory_handle *fmem,
 {
     unsigned long pt_prot = 0;
     if (prot & PROT_READ)
-	pt_prot = L1_PROT_RO;
+        pt_prot = L1_PROT_RO;
     if (prot & PROT_WRITE)
-	pt_prot = L1_PROT;
+        pt_prot = L1_PROT;
     return map_frames_ex(arr, num, 1, 0, 1, dom, err, pt_prot);
 }
 
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
                                  void *addr, size_t num)
 {
-	return munmap(addr, num << PAGE_SHIFT);
+    return munmap(addr, num << PAGE_SHIFT);
 }
 
 /*
diff --git a/tools/libs/foreignmemory/netbsd.c b/tools/libs/foreignmemory/netbsd.c
index d0d233a..08f4964 100644
--- a/tools/libs/foreignmemory/netbsd.c
+++ b/tools/libs/foreignmemory/netbsd.c
@@ -97,7 +97,7 @@ void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
                                  void *addr, size_t num)
 {
-	return munmap(addr, num*XC_PAGE_SIZE);
+    return munmap(addr, num*XC_PAGE_SIZE);
 }
 
 /*
diff --git a/tools/libs/foreignmemory/private.h b/tools/libs/foreignmemory/private.h
index cd732fd..9cc7814 100644
--- a/tools/libs/foreignmemory/private.h
+++ b/tools/libs/foreignmemory/private.h
@@ -39,7 +39,7 @@ void *compat_mapforeign_batch(xenforeignmem_handle *fmem, uint32_t dom,
 #endif
 
 #define PERROR(_f...) \
-	xtl_log(fmem->logger, XTL_ERROR, errno, "xenforeignmemory", _f)
+    xtl_log(fmem->logger, XTL_ERROR, errno, "xenforeignmemory", _f)
 
 #endif
 
diff --git a/tools/libs/foreignmemory/solaris.c b/tools/libs/foreignmemory/solaris.c
index f1c44bc..e925a29 100644
--- a/tools/libs/foreignmemory/solaris.c
+++ b/tools/libs/foreignmemory/solaris.c
@@ -95,5 +95,15 @@ void *osdep_map_foreign_batch(xenforeignmem_handle *fmem, uint32_t dom,
 int osdep_xenforeignmemory_unmap(xenforeignmemory_handle *fmem,
                                  void *addr, size_t num)
 {
-	return munmap(addr, num*XC_PAGE_SIZE);
+    return munmap(addr, num*XC_PAGE_SIZE);
 }
+
+/*
+ * 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/gnttab/include/xengnttab.h b/tools/libs/gnttab/include/xengnttab.h
index 700a5f1..1e07672 100644
--- a/tools/libs/gnttab/include/xengnttab.h
+++ b/tools/libs/gnttab/include/xengnttab.h
@@ -142,7 +142,7 @@ int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count);
  *      of grants.
  */
 int xengnttab_set_max_grants(xengnttab_handle *xgt,
-			     uint32_t count);
+                             uint32_t count);
 
 /*
  * Grant Sharing Interface (allocating and granting pages)
@@ -160,7 +160,7 @@ typedef struct xengntdev_handle xengntshr_handle;
  *
  */
 xengntshr_handle *xengntshr_open(xentoollog_logger *logger,
-			  unsigned open_flags);
+                                 unsigned open_flags);
 
 /*
  * Close a handle previously allocated with xengntshr_open().
diff --git a/tools/libs/gnttab/linux.c b/tools/libs/gnttab/linux.c
index 768119a..8dbdff8 100644
--- a/tools/libs/gnttab/linux.c
+++ b/tools/libs/gnttab/linux.c
@@ -294,9 +294,9 @@ void *osdep_gntshr_share_pages(xengntshr_handle *xgs,
         err = ioctl(fd, IOCTL_GNTALLOC_SET_UNMAP_NOTIFY, &notify);
     if (err) {
         GSERROR(xgs->logger, "ioctl SET_UNMAP_NOTIFY failed");
-		munmap(area, count * PAGE_SIZE);
-		area = NULL;
-	}
+        munmap(area, count * PAGE_SIZE);
+        area = NULL;
+    }
 
     memcpy(refs, gref_info->gref_ids, count * sizeof(uint32_t));
 
diff --git a/tools/libs/toollog/xtl_logger_stdio.c b/tools/libs/toollog/xtl_logger_stdio.c
index f9c5bd8..52dfbf5 100644
--- a/tools/libs/toollog/xtl_logger_stdio.c
+++ b/tools/libs/toollog/xtl_logger_stdio.c
@@ -135,7 +135,7 @@ static void stdiostream_progress(struct xentoollog_logger *logger_in,
     newpel = fprintf(lg->f, "%s%s" "%s: %lu/%lu  %3d%%%s",
                      context?context:"", context?": ":"",
                      doing_what, done, total, percent,
-		     done == total ? "\n" : "");
+                     done == total ? "\n" : "");
 
     extra_erase = lg->progress_erase_len - newpel;
     if (extra_erase > 0)
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 20/29] tools/libs/gnttab: Extensive updates to API documentation.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (18 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 19/29] tools/libs: Clean up hard tabs Ian Campbell
@ 2016-01-15 13:22   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:23   ` [PATCH XEN v8 21/29] tools/libs/call: Update some log messages to not refer to xc Ian Campbell
                     ` (8 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Daniel De Graaf, Ian Campbell

In particular around error handling, behaviour on fork and the unmap
notification mechanism.

Behaviour of xengnttab_map_*grant_refs and xengntshr_share_pages on
partial failure has been confirmed/inferred (by inspection) on Linux
and Mini-os (the only two known implementations. Likewise the
behaviour of the notification mechanism has been confirmed/inferred
(by inspection) of the Linux implementation (currently the only
implementation) and libvchan (primary known user).

These updates are not folded into "tools: Refactor
/dev/xen/gnt{dev,shr} wrappers into libxengnttab." to try and reduce
the amount of non-movement changes in that patch.

While I'm not convinced by javadoc/doxygen cause the existing comments
which appear to use that syntax to have the appropriate /** marker.

Also fix a typo in a code comment.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Daniel De Graaf <dgdegra@tycho.nsa.gov>
Cc: Daniel De Graaf <dgdegra@tycho.nsa.gov>
---

v7: Typo.
    Updates based on Ian Jackson's review.
    Even more updates to the discussion of forking based on ML
    discussion.
v6: Rewrapped a comment.
    Incorported much review from Ian on the API, retitled (was:
    "tools/libs/gnttab: Review and update doc comments.") and dropped
    acks
---
 tools/libs/gnttab/include/xengnttab.h | 201 +++++++++++++++++++++++++++++-----
 tools/libs/gnttab/linux.c             |  12 +-
 2 files changed, 181 insertions(+), 32 deletions(-)

diff --git a/tools/libs/gnttab/include/xengnttab.h b/tools/libs/gnttab/include/xengnttab.h
index 1e07672..7bf8462 100644
--- a/tools/libs/gnttab/include/xengnttab.h
+++ b/tools/libs/gnttab/include/xengnttab.h
@@ -31,28 +31,124 @@
 typedef struct xentoollog_logger xentoollog_logger;
 
 /*
+ * PRODUCING AND CONSUMING GRANT REFERENCES
+ * ========================================
+ *
+ * The xengnttab library contains two distinct interfaces, each with
+ * their own distinct handle type and entry points. The represent the
+ * two sides of the grant table interface, producer (gntshr) and
+ * consumer (gnttab).
+ *
+ * The xengnttab_* interfaces take a xengnttab_handle and provide
+ * mechanisms for consuming (i.e. mapping or copying to/from) grant
+ * references provided by a peer.
+ *
+ * The xengntshr_* interfaces take a xengntshr_handle and provide a
+ * mechanism to produce grantable memory and grant references to that
+ * memory, which can be handed to some peer.
+ *
+ * UNMAP NOTIFICATION
+ * ==================
+ *
+ * The xengnt{tab,shr}_*_notify interfaces implement a cooperative
+ * interface which is intended to allow the underlying kernel
+ * interfaces to attempt to notify the peer to perform graceful
+ * teardown upon failure (i.e. crash or exit) of the process on their
+ * end.
+ *
+ * These interfaces operate on a single page only and are intended for
+ * use on the main shared-ring page of a protocol. It is assumed that
+ * on teardown both ends would automatically teardown all grants
+ * associated with the protocol in addition to the shared ring itself.
+ *
+ * Each end is able to optionally nominate a byte offset within the
+ * shared page or an event channel or both. On exit of the process the
+ * underlying kernel driver will zero the byte at the given offset and
+ * signal the event channel.
+ *
+ * The event channel can be the same event channel used for regular
+ * ring progress notifications, or may be a dedicated event channel.
+ *
+ * Both ends may share the same notification byte offset within the
+ * shared page, or may have dedicated "client" and "server" status
+ * bytes.
+ *
+ * Since the byte is cleared on shutdown the protocol must use 0 as
+ * the "closed/dead" status, but is permitted to use any other non-0
+ * values to indicate various other "live" states (waiting for
+ * connection, connected, etc).
+ *
+ * Both ends are permitted to modify (including clear) their
+ * respective status bytes and to signal the event channel themselves
+ * from userspace.
+ *
+ * Depending on the mechanisms which have been registered an
+ * the peer may receive a shutdown notification as:
+ *
+ *   - An event channel notification on a dedicated event channel
+ *   - Observation of the other ends's status byte being cleared
+ *     (whether in response to an explicit notification or in the
+ *     course of normal operation).
+ *
+ * The mechanism should be defined as part of the specific ring
+ * protocol.
+ *
+ * Upon receiving notification of the peer is expected to teardown any
+ * resources (and in particular any grant mappings) in a timely
+ * manner.
+ *
+ * NOTE: this protocol is intended to allow for better error behaviour
+ * and recovery between two cooperating peers. It does not cover the
+ * case of a malivious peer who may continue to hold resources open.
+ */
+
+/*
  * Grant Table Interface (making use of grants from other domains)
  */
 
 typedef struct xengntdev_handle xengnttab_handle;
 
 /*
- * Note:
- * After fork a child process must not use any opened xc gnttab
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
+ * Returns a handle onto the grant table driver.  Logs errors.
+ *
+ * Note: After fork(2) a child process must not use any opened gnttab
+ * handle inherited from their parent, nor access any grant mapped
+ * areas associated with that handle.
  *
- * Return an fd onto the grant table driver.  Logs errors.
+ * The child must open a new handle if they want to interact with
+ * gnttab.
+ *
+ * Calling exec(2) in a child will safely (and reliably) reclaim any
+ * resources which were allocated via a xengnttab_handle in the parent.
+ *
+ * A child which does not call exec(2) may safely call
+ * xengnttab_close() on a xengnttab_handle inherited from their
+ * parent. This will attempt to reclaim any resources associated with
+ * that handle. Note that in some implementations this reclamation may
+ * not be completely effective, in this case any affected resources
+ * remain allocated.
+ *
+ * Calling xengnttab_close() is the only safe operation on a
+ * xengnttab_handle which has been inherited. xengnttab_unmap() must
+ * not be called under such circumstances.
  */
 xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags);
 
 /*
- * Close a handle previously allocated with xengnttab_open().
- * Never logs errors.
+ * Close a handle previously allocated with xengnttab_open(),
+ * including unmaping any current grant maps.  Never logs errors.
+ *
+ * Under normal circumstances (i.e. not in the child after a fork)
+ * xengnttab_unmap() should be used on all mappings allocated through
+ * a xengnttab_handle prior to closing the handle in order to free up
+ * resources associated with those mappings.
+ *
+ * This is the only function which may be safely called on a
+ * xengnttab_handle in a child after a fork.
  */
 int xengnttab_close(xengnttab_handle *xgt);
 
-/*
+/**
  * Memory maps a grant reference from one domain to a local address range.
  * Mappings should be unmapped with xengnttab_unmap.  Logs errors.
  *
@@ -71,6 +167,10 @@ void *xengnttab_map_grant_ref(xengnttab_handle *xgt,
  * contiguous local address range. Mappings should be unmapped with
  * xengnttab_unmap.  Logs errors.
  *
+ * On failure (including partial failure) sets errno and returns
+ * NULL. On partial failure no mappings are established (any partial
+ * work is undone).
+ *
  * @parm xgt a handle on an open grant table interface
  * @parm count the number of grant references to be mapped
  * @parm domids an array of @count domain IDs by which the corresponding @refs
@@ -89,6 +189,9 @@ void *xengnttab_map_grant_refs(xengnttab_handle *xgt,
  * contiguous local address range. Mappings should be unmapped with
  * xengnttab_unmap.  Logs errors.
  *
+ * This call is equivalent to calling @xengnttab_map_grant_refs with a
+ * @domids array with every entry set to @domid.
+ *
  * @parm xgt a handle on an open grant table interface
  * @parm count the number of grant references to be mapped
  * @parm domid the domain to map memory from
@@ -109,6 +212,11 @@ void *xengnttab_map_domain_grant_refs(xengnttab_handle *xgt,
  * unmapped, the byte at the given offset will be zeroed and a wakeup will be
  * sent to the given event channel.  Logs errors.
  *
+ * On failure sets errno and returns NULL.
+ *
+ * If notify_offset or notify_port are requested and cannot be set up
+ * an error will be returned and no mapping will be made.
+ *
  * @parm xgt a handle on an open grant table interface
  * @parm domid the domain to map memory from
  * @parm ref the grant reference ID to map
@@ -124,15 +232,20 @@ void *xengnttab_map_grant_ref_notify(xengnttab_handle *xgt,
                                      uint32_t notify_offset,
                                      evtchn_port_t notify_port);
 
-/*
- * Unmaps the @count pages starting at @start_address, which were mapped by a
- * call to xengnttab_map_grant_ref or xengnttab_map_grant_refs. Never logs.
+/**
+ * Unmaps the @count pages starting at @start_address, which were
+ * mapped by a call to xengnttab_map_grant_ref,
+ * xengnttab_map_grant_refs or xengnttab_map_grant_ref_notify. Never
+ * logs.
+ *
+ * If the mapping was made using xengnttab_map_grant_ref_notify() with
+ * either notify_offset or notify_port then the peer will be notified.
  */
 int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count);
 
-/*
- * Sets the maximum number of grants that may be mapped by the given instance
- * to @count.  Never logs.
+/**
+ * Sets the maximum number of grants that may be mapped by the given
+ * instance to @count.  Never logs.
  *
  * N.B. This function must be called after opening the handle, and before any
  *      other functions are invoked on it.
@@ -142,22 +255,37 @@ int xengnttab_unmap(xengnttab_handle *xgt, void *start_address, uint32_t count);
  *      of grants.
  */
 int xengnttab_set_max_grants(xengnttab_handle *xgt,
-                             uint32_t count);
+                             uint32_t nr_grants);
 
 /*
- * Grant Sharing Interface (allocating and granting pages)
+ * Grant Sharing Interface (allocating and granting pages to others)
  */
 
 typedef struct xengntdev_handle xengntshr_handle;
 
 /*
- * Return an fd onto the grant sharing driver.  Logs errors.
+ * Returns a handle onto the grant sharing driver.  Logs errors.
+ *
+ * Note: After fork(2) a child process must not use any opened gntshr
+ * handle inherited from their parent, nor access any grant mapped
+ * areas associated with that handle.
+ *
+ * The child must open a new handle if they want to interact with
+ * gntshr.
  *
- * Note:
- * After fork a child process must not use any opened xc gntshr
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
+ * Calling exec(2) in a child will safely (and reliably) reclaim any
+ * resources which were allocated via a xengntshr_handle in the
+ * parent.
  *
+ * A child which does not call exec(2) may safely call
+ * xengntshr_close() on a xengntshr_handle inherited from their
+ * parent. This will attempt to reclaim any resources associated with
+ * that handle. Note that in some implementations this reclamation may
+ * not be completely effective, in this case any affected resources
+ * remain allocated.
+ *
+ * Calling xengntshr_close() is the only safe operation on a
+ * xengntshr_handle which has been inherited.
  */
 xengntshr_handle *xengntshr_open(xentoollog_logger *logger,
                                  unsigned open_flags);
@@ -165,11 +293,26 @@ xengntshr_handle *xengntshr_open(xentoollog_logger *logger,
 /*
  * Close a handle previously allocated with xengntshr_open().
  * Never logs errors.
+ *
+ * Under normal circumstances (i.e. not in the child after a fork)
+ * xengntshr_unmap() should be used on all mappings allocated through
+ * a xengnttab_handle prior to closing the handle in order to free up
+ * resources associated with those mappings.
+ *
+ * xengntshr_close() is the only function which may be safely called
+ * on a xengntshr_handle in a child after a fork. xengntshr_unshare()
+ * must not be called under such circumstances.
  */
 int xengntshr_close(xengntshr_handle *xgs);
 
-/*
- * Creates and shares pages with another domain.
+/**
+ * Allocates and shares pages with another domain.
+ *
+ * On failure sets errno and returns NULL. No allocations will be made.
+ *
+ * This library only provides functionality for sharing memory
+ * allocated via this call, memory from elsewhere (malloc, mmap etc)
+ * cannot be shared here.
  *
  * @parm xgs a handle to an open grant sharing instance
  * @parm domid the domain to share memory with
@@ -181,7 +324,7 @@ int xengntshr_close(xengntshr_handle *xgs);
 void *xengntshr_share_pages(xengntshr_handle *xgs, uint32_t domid,
                             int count, uint32_t *refs, int writable);
 
-/*
+/**
  * Creates and shares a page with another domain, with unmap notification.
  *
  * @parm xgs a handle to an open grant sharing instance
@@ -197,9 +340,13 @@ void *xengntshr_share_page_notify(xengntshr_handle *xgs, uint32_t domid,
                                   uint32_t *ref, int writable,
                                   uint32_t notify_offset,
                                   evtchn_port_t notify_port);
-/*
- * Unmaps the @count pages starting at @start_address, which were mapped by a
- * call to xengntshr_share_*. Never logs.
+
+/**
+ * Unmaps the @count pages starting at @start_address, which were
+ * mapped by a call to xengntshr_share_*. Never logs.
+ *
+ * If the mapping was made using xengntshr_share_page_notify() with
+ * either notify_offset or notify_port then the peer will be notified.
  */
 int xengntshr_unshare(xengntshr_handle *xgs, void *start_address, uint32_t count);
 
diff --git a/tools/libs/gnttab/linux.c b/tools/libs/gnttab/linux.c
index 8dbdff8..be04295 100644
--- a/tools/libs/gnttab/linux.c
+++ b/tools/libs/gnttab/linux.c
@@ -132,12 +132,14 @@ void *osdep_gnttab_grant_map(xengnttab_handle *xgt,
     if (addr == MAP_FAILED && errno == EAGAIN)
     {
         /*
-         * The grant hypercall can return EAGAIN if the granted page is
-         * swapped out. Since the paging daemon may be in the same domain, the
-         * hypercall cannot block without causing a deadlock.
+         * The grant hypercall can return EAGAIN if the granted page
+         * is swapped out. Since the paging daemon may be in the same
+         * domain, the hypercall cannot block without causing a
+         * deadlock.
          *
-         * Because there are no notificaitons when the page is swapped in, wait
-         * a bit before retrying, and hope that the page will arrive eventually.
+         * Because there are no notifications when the page is swapped
+         * in, wait a bit before retrying, and hope that the page will
+         * arrive eventually.
          */
         usleep(1000);
         goto retry;
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 21/29] tools/libs/call: Update some log messages to not refer to xc.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (19 preceding siblings ...)
  2016-01-15 13:22   ` [PATCH XEN v8 20/29] tools/libs/gnttab: Extensive updates to API documentation Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH XEN v8 22/29] tools/libs/call: Describe return values and error semantics for xencall* Ian Campbell
                     ` (7 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libs/call/linux.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
index 55e1e83..3641e41 100644
--- a/tools/libs/call/linux.c
+++ b/tools/libs/call/linux.c
@@ -94,7 +94,7 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
     p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
     if ( p == MAP_FAILED )
     {
-        PERROR("xc_alloc_hypercall_buffer: mmap failed");
+        PERROR("alloc_pages: mmap failed");
         return NULL;
     }
 
@@ -103,7 +103,7 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
     rc = madvise(p, npages * PAGE_SIZE, MADV_DONTFORK);
     if ( rc < 0 )
     {
-        PERROR("xc_alloc_hypercall_buffer: madvise failed");
+        PERROR("alloc_pages: madvise failed");
         goto out;
     }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 22/29] tools/libs/call: Describe return values and error semantics for xencall*
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (20 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 21/29] tools/libs/call: Update some log messages to not refer to xc Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH XEN v8 23/29] tools/libs/call: Avoid xc_memalign in netbsd and solaris backends Ian Campbell
                     ` (6 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell, Roger Pau Monné

This behaviour has been confirmed by inspection on:

 - Linux
 - NetBSD & FreeBSD (NB: hcall->retval is the hypercall return value
   only for values >= 0. For negative values the underlying privcmd
   driver translates the value from Xen to {Net,Free}BSD errno space
   and returns it as the result of the ioctl, which becomes
   ret=-1/errno=EFOO in userspace)
 - MiniOS (which takes care of errno in this library)

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Roger Pau Monné <roger.pau@citrix.com>
Cc: Roger Pau Monné <roger.pau@citrix.com>
---
v7: Noted NetBSD behaviour
v6: New patch
---
 tools/libs/call/include/xencall.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/tools/libs/call/include/xencall.h b/tools/libs/call/include/xencall.h
index 0d91aa8..3f325f0 100644
--- a/tools/libs/call/include/xencall.h
+++ b/tools/libs/call/include/xencall.h
@@ -46,6 +46,21 @@ int xencall_close(xencall_handle *xcall);
 
 /*
  * Call hypercalls with varying numbers of arguments.
+ *
+ * On success the return value of the hypercall is the return value of
+ * the xencall function.  On error these functions set errno and
+ * return -1.
+ *
+ * The errno values will be either:
+ * - The Xen hypercall error return (from xen/include/public/errno.h)
+ *   translated into the corresponding local value for that POSIX error.
+ * - An errno value produced by the OS driver or the library
+ *   implementation. Such values may be defined by POSIX or by the OS.
+ *
+ * Note that under some circumstances it will not be possible to tell
+ * whether an error came from Xen or from the OS/library.
+ *
+ * These functions never log.
  */
 int xencall0(xencall_handle *xcall, unsigned int op);
 int xencall1(xencall_handle *xcall, unsigned int op,
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 23/29] tools/libs/call: Avoid xc_memalign in netbsd and solaris backends
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (21 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 22/29] tools/libs/call: Describe return values and error semantics for xencall* Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown Ian Campbell
                     ` (5 subsequent siblings)
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

These are already arch specific, so just use the appropriate
interfaces (as determined by looking at the xc_memalign backend).

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/libs/call/netbsd.c  | 4 ++--
 tools/libs/call/solaris.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/libs/call/netbsd.c b/tools/libs/call/netbsd.c
index 2aa02f1..e96fbf1 100644
--- a/tools/libs/call/netbsd.c
+++ b/tools/libs/call/netbsd.c
@@ -74,8 +74,8 @@ void *osdep_alloc_hypercall_buffer(xencall_handle *xcall, size_t npages)
     size_t size = npages * XC_PAGE_SIZE;
     void *p;
 
-    p = xc_memalign(xcall, XC_PAGE_SIZE, size);
-    if (!p)
+    ret = posix_memalign(&p, XC_PAGE_SIZE, size);
+    if ( ret != 0 || !p )
         return NULL;
 
     if ( mlock(p, size) < 0 )
diff --git a/tools/libs/call/solaris.c b/tools/libs/call/solaris.c
index 945d867..5aa330e 100644
--- a/tools/libs/call/solaris.c
+++ b/tools/libs/call/solaris.c
@@ -71,7 +71,7 @@ int osdep_xencall_close(xencall_handle *xcall)
 
 void *osdep_alloc_hypercall_buffer(xencall_handle *xcall, size_t npages)
 {
-    return xc_memalign(xcall, XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
+    return memalign(XC_PAGE_SIZE, npages * XC_PAGE_SIZE);
 }
 
 void osdep_free_hypercall_buffer(xencall_handle *xcall, void *ptr,
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (22 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 23/29] tools/libs/call: Avoid xc_memalign in netbsd and solaris backends Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:23   ` [PATCH XEN v8 25/29] tools/libs/{call, evtchn}: Document requirements around forking Ian Campbell
                     ` (4 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

This avoids a potential issue with a fork after allocation but before
madvise.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v7: New, replacing "tools/libs/call: linux: avoid forking between mmap
    and madvise".
---
 tools/libs/call/linux.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
index 3641e41..651f380 100644
--- a/tools/libs/call/linux.c
+++ b/tools/libs/call/linux.c
@@ -88,7 +88,7 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
 {
     size_t size = npages * PAGE_SIZE;
     void *p;
-    int rc, saved_errno;
+    int rc, i, saved_errno;
 
     /* Address returned by mmap is page aligned. */
     p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
@@ -107,6 +107,18 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
         goto out;
     }
 
+    /*
+     * Touch each page in turn to force them to be un-CoWed, in case a
+     * fork happened in another thread at an inopportune moment
+     * above. The madvise() will prevent any subsequent fork calls from
+     * causing the same problem.
+     */
+    for ( i = 0; i < npages ; i++ )
+    {
+        char *c = (char *)p + (i*PAGE_SIZE);
+        *c = 0;
+    }
+
     return p;
 
 out:
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 25/29] tools/libs/{call, evtchn}: Document requirements around forking.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (23 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:23   ` [PATCH XEN v8 26/29] tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD Ian Campbell
                     ` (3 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

Much like for gnttab and foreignmemory xencall hypercall buffers need
care.

Evtchn is a bit simpler (no magic mappings) but may not work from
parent + child simultaneously, document "parent only" since it is
consistent with the others.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v7: New
---
 tools/libs/call/include/xencall.h     | 28 ++++++++++++++++++++++++++++
 tools/libs/evtchn/include/xenevtchn.h | 23 +++++++++++++++++++----
 2 files changed, 47 insertions(+), 4 deletions(-)

diff --git a/tools/libs/call/include/xencall.h b/tools/libs/call/include/xencall.h
index 3f325f0..559624a 100644
--- a/tools/libs/call/include/xencall.h
+++ b/tools/libs/call/include/xencall.h
@@ -36,11 +36,39 @@ typedef struct xencall_handle xencall_handle;
 
 /*
  * Return a handle onto the hypercall driver.  Logs errors.
+ * *
+ * Note: After fork(2) a child process must not use any opened
+ * xencall handle inherited from their parent, nor access any
+ * hypercall argument buffers associated with that handle.
+ *
+ * The child must open a new handle if they want to interact with
+ * xencall.
+ *
+ * Calling exec(2) in a child will safely (and reliably) reclaim any
+ * resources which were allocated via a xencall_handle in the parent.
+ *
+ * A child which does not call exec(2) may safely call xencall_close()
+ * on a xencall_handle inherited from their parent. This will attempt
+ * to reclaim any resources associated with that handle. Note that in
+ * some implementations this reclamation may not be completely
+ * effective, in this case any affected resources remain allocated.
+ *
+ * Calling xencall_close() is the only safe operation on a
+ * xencall_handle which has been inherited.
  */
 xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags);
 
 /*
  * Close a handle previously allocated with xencall_open().
+ *
+ * Under normal circumstances (i.e. not in the child after a fork) any
+ * allocated hypercall argument buffers should be freed using the
+ * appropriate xencall_free_*() prior to closing the handle in order
+ * to free up resources associated with those mappings.
+ *
+ * This is the only function which may be safely called on a
+ * xencall_handle in a child after a fork. xencall_free_*() must not
+ * be called under such circumstances.
  */
 int xencall_close(xencall_handle *xcall);
 
diff --git a/tools/libs/evtchn/include/xenevtchn.h b/tools/libs/evtchn/include/xenevtchn.h
index 428d54c..4d26161 100644
--- a/tools/libs/evtchn/include/xenevtchn.h
+++ b/tools/libs/evtchn/include/xenevtchn.h
@@ -45,10 +45,25 @@ typedef struct xentoollog_logger xentoollog_logger;
  * Return a handle to the event channel driver, or NULL on failure, in
  * which case errno will be set appropriately.
  *
- * Note:
- * After fork a child process must not use any opened evtchn
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with evtchn.
+ * Note: After fork(2) a child process must not use any opened evtchn
+ * handle inherited from their parent, nor access any grant mapped
+ * areas associated with that handle.
+ *
+ * The child must open a new handle if they want to interact with
+ * evtchn.
+ *
+ * Calling exec(2) in a child will safely (and reliably) reclaim any
+ * allocated resources via a xenevtchn_handle in the parent.
+ *
+ * A child which does not call exec(2) may safely call
+ * xenevtchn_close() on a xenevtchn_handle inherited from their
+ * parent. This will attempt to reclaim any resources associated with
+ * that handle. Note that in some implementations this reclamation may
+ * not be completely effective, in this case any affected resources
+ * remain allocated.
+ *
+ * Calling xenevtchn_close() is the only safe operation on a
+ * xenevtchn_handle which has been inherited.
  */
 /* Currently no flags are defined */
 xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 26/29] tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (24 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 25/29] tools/libs/{call, evtchn}: Document requirements around forking Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:23   ` [PATCH XEN v8 27/29] tools: Update CFLAGS for qemu-xen to allow it to use new libraries Ian Campbell
                     ` (2 subsequent siblings)
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell, jbeulich, Roger.Pau

In some cases this replaces an FD_CLOEXEC dance, in others it is new.

Linux has had O_CLOEXEC since 2.6.23 (October 2007), so we can rely on
it from Xen 4.7 I think. Some libc headers may still lack the
definition, so we take care of that if need be by defining to 0 (on
the premise that such an old glibc might barf on O_CLOEXEC even if the
kernel may or may not be so old).

All stable versions of FreeBSD support O_CLOEXEC (10.2, 9.3 and 8.4),
and we assume the libc there does too.

Remove various comments about having to take responsibility for this
(since really it is just hygiene, politeness, not a requirement) and
the reasons for using O_CLOEXEC seem pretty straightforward.

Backends for other OSes are untouched.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Roger Pau Monné <roger.pau@citrix.com>
Cc: Roger.Pau@citrix.com
Cc: jbeulich@suse.com
---
v6: New

v7: New, replaces "tools/libs/call: Use O_CLOEXEC when opening
    /dev/xen/privcmd on Linux"

v8: Define compat O_CLOEXEC of 0.
---
 tools/libs/call/freebsd.c          | 30 ++++++++----------------------
 tools/libs/call/linux.c            | 38 +++++++++++---------------------------
 tools/libs/evtchn/freebsd.c        |  2 +-
 tools/libs/evtchn/linux.c          |  6 +++++-
 tools/libs/foreignmemory/freebsd.c | 24 ++----------------------
 tools/libs/foreignmemory/linux.c   | 36 +++++++++---------------------------
 tools/libs/gnttab/linux.c          |  6 +++++-
 7 files changed, 41 insertions(+), 101 deletions(-)

diff --git a/tools/libs/call/freebsd.c b/tools/libs/call/freebsd.c
index 2413966..b3cbccd 100644
--- a/tools/libs/call/freebsd.c
+++ b/tools/libs/call/freebsd.c
@@ -35,8 +35,14 @@
 
 int osdep_xencall_open(xencall_handle *xcall)
 {
-    int flags, saved_errno;
-    int fd = open(PRIVCMD_DEV, O_RDWR);
+    int saved_errno;
+    int fd = open(PRIVCMD_DEV, O_RDWR|O_CLOEXEC);
+
+    /*
+     * This file descriptor is opaque to the caller, thus we are
+     * polite and try and ensure it doesn't propagate (ie leak)
+     * outside the process, by using O_CLOEXEC.
+     */
 
     if ( fd == -1 )
     {
@@ -45,26 +51,6 @@ int osdep_xencall_open(xencall_handle *xcall)
         return -1;
     }
 
-    /*
-     * Although we return the file handle as the 'xc handle' the API
-     * does not specify / guarentee that this integer is in fact
-     * a file handle. Thus we must take responsiblity to ensure
-     * it doesn't propagate (ie leak) outside the process.
-     */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     xcall->fd = fd;
     return 0;
 
diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
index 651f380..e8e0311 100644
--- a/tools/libs/call/linux.c
+++ b/tools/libs/call/linux.c
@@ -26,15 +26,23 @@
 
 #include "private.h"
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_xencall_open(xencall_handle *xcall)
 {
-    int flags, saved_errno;
-    int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
+    int fd;
+
+    /*
+     * Prefer the newer interface.
+     */
+    fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
 
     if ( fd == -1 && ( errno == ENOENT || errno == ENXIO || errno == ENODEV ))
     {
         /* Fallback to /proc/xen/privcmd */
-        fd = open("/proc/xen/privcmd", O_RDWR);
+        fd = open("/proc/xen/privcmd", O_RDWR|O_CLOEXEC);
     }
 
     if ( fd == -1 )
@@ -43,32 +51,8 @@ int osdep_xencall_open(xencall_handle *xcall)
         return -1;
     }
 
-    /* Although we return the file handle as the 'xc handle' the API
-       does not specify / guarentee that this integer is in fact
-       a file handle. Thus we must take responsiblity to ensure
-       it doesn't propagate (ie leak) outside the process */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     xcall->fd = fd;
     return 0;
-
- error:
-    saved_errno = errno;
-    close(fd);
-    errno = saved_errno;
-    return -1;
 }
 
 int osdep_xencall_close(xencall_handle *xcall)
diff --git a/tools/libs/evtchn/freebsd.c b/tools/libs/evtchn/freebsd.c
index 6479f7c..ddf221d 100644
--- a/tools/libs/evtchn/freebsd.c
+++ b/tools/libs/evtchn/freebsd.c
@@ -32,7 +32,7 @@
 
 int osdep_evtchn_open(xenevtchn_handle *xce)
 {
-    int fd = open(EVTCHN_DEV, O_RDWR);
+    int fd = open(EVTCHN_DEV, O_RDWR|O_CLOEXEC);
     if ( fd == -1 )
         return -1;
     xce->fd = fd;
diff --git a/tools/libs/evtchn/linux.c b/tools/libs/evtchn/linux.c
index 76cf0ac..0a3c6e1 100644
--- a/tools/libs/evtchn/linux.c
+++ b/tools/libs/evtchn/linux.c
@@ -28,9 +28,13 @@
 
 #include "private.h"
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_evtchn_open(xenevtchn_handle *xce)
 {
-    int fd = open("/dev/xen/evtchn", O_RDWR);
+    int fd = open("/dev/xen/evtchn", O_RDWR|O_CLOEXEC);
     if ( fd == -1 )
         return -1;
     xce->fd = fd;
diff --git a/tools/libs/foreignmemory/freebsd.c b/tools/libs/foreignmemory/freebsd.c
index 38138dc..7bf3939 100644
--- a/tools/libs/foreignmemory/freebsd.c
+++ b/tools/libs/foreignmemory/freebsd.c
@@ -33,8 +33,8 @@
 
 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
 {
-    int flags, saved_errno;
-    int fd = open(PRIVCMD_DEV, O_RDWR);
+    int saved_errno;
+    int fd = open(PRIVCMD_DEV, O_RDWR|O_CLOEXEC);
 
     if ( fd == -1 )
     {
@@ -43,26 +43,6 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }
 
-    /*
-     * Although we return the file handle as the 'xc handle' the API
-     * does not specify / guarentee that this integer is in fact
-     * a file handle. Thus we must take responsiblity to ensure
-     * it doesn't propagate (ie leak) outside the process.
-     */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     fmem->fd = fd;
     return 0;
 
diff --git a/tools/libs/foreignmemory/linux.c b/tools/libs/foreignmemory/linux.c
index 32b6def..423c744 100644
--- a/tools/libs/foreignmemory/linux.c
+++ b/tools/libs/foreignmemory/linux.c
@@ -30,15 +30,21 @@
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
 {
-    int flags, saved_errno;
-    int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
+    int fd;
+
+    /* prefer this newer interface */
+    fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
 
     if ( fd == -1 && ( errno == ENOENT || errno == ENXIO || errno == ENODEV ))
     {
         /* Fallback to /proc/xen/privcmd */
-        fd = open("/proc/xen/privcmd", O_RDWR);
+        fd = open("/proc/xen/privcmd", O_RDWR|O_CLOEXEC);
     }
 
     if ( fd == -1 )
@@ -47,32 +53,8 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }
 
-    /* Although we return the file handle as the 'xc handle' the API
-       does not specify / guarentee that this integer is in fact
-       a file handle. Thus we must take responsiblity to ensure
-       it doesn't propagate (ie leak) outside the process */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     fmem->fd = fd;
     return 0;
-
- error:
-    saved_errno = errno;
-    close(fd);
-    errno = saved_errno;
-    return -1;
 }
 
 int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
diff --git a/tools/libs/gnttab/linux.c b/tools/libs/gnttab/linux.c
index be04295..7b0fba4 100644
--- a/tools/libs/gnttab/linux.c
+++ b/tools/libs/gnttab/linux.c
@@ -43,9 +43,13 @@
 #define PAGE_SIZE            (1UL << PAGE_SHIFT)
 #define PAGE_MASK            (~(PAGE_SIZE-1))
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_gnttab_open(xengnttab_handle *xgt)
 {
-    int fd = open(DEVXEN "gntdev", O_RDWR);
+    int fd = open(DEVXEN "gntdev", O_RDWR|O_CLOEXEC);
     if ( fd == -1 )
         return -1;
     xgt->fd = fd;
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 27/29] tools: Update CFLAGS for qemu-xen to allow it to use new libraries
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (25 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 26/29] tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain Ian Campbell
  2016-01-15 13:23   ` [PATCH XEN v8 29/29] HACK: Update Config.mk to pull all the right bits from my xenbits trees Ian Campbell
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

This means adding -L for libxen{evtchn,gnttab,foreignmemory} so that
it can link them directly (rather than using the libxenctrl compat
layer exposed via -rpath-link). Also add -I for libxenforeignmemory.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
 tools/Makefile | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/tools/Makefile b/tools/Makefile
index c03f6db..3575f16 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -255,12 +255,16 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		-I$(XEN_ROOT)/tools/libs/toollog/include \
 		-I$(XEN_ROOT)/tools/libs/evtchn/include \
 		-I$(XEN_ROOT)/tools/libs/gnttab/include \
+		-I$(XEN_ROOT)/tools/libs/foreignmemory/include \
 		-I$(XEN_ROOT)/tools/libxc/include \
 		-I$(XEN_ROOT)/tools/xenstore/include \
 		-I$(XEN_ROOT)/tools/xenstore/compat/include \
 		$(EXTRA_CFLAGS_QEMU_XEN)" \
 		--extra-ldflags="-L$(XEN_ROOT)/tools/libxc \
 		-L$(XEN_ROOT)/tools/xenstore \
+		-L$(XEN_ROOT)/tools/libs/evtchn \
+		-L$(XEN_ROOT)/tools/libs/gnttab \
+		-L$(XEN_ROOT)/tools/libs/foreignmemory \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/evtchn \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/gnttab \
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain.
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (26 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 27/29] tools: Update CFLAGS for qemu-xen to allow it to use new libraries Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-19 13:24     ` Wei Liu
  2016-01-15 13:23   ` [PATCH XEN v8 29/29] HACK: Update Config.mk to pull all the right bits from my xenbits trees Ian Campbell
  28 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

These are intended to allow user space processes (in particular QEMU)
to lock down all the handles at start of day and then drop the
privileges which would allow them to open any new unrestricted handles
(e.g. setuid or similar). This will reduce the privileges which taking
over such a process would gain an attacker wrt other domains in the
system.

These are currently unimplemented on all platforms, however the API
semantics are defined as the basis for discussion, and so that
consumers can rely on this interface always having been present rather
than requiring compile time API checks.

It is expected that these will be implemented by adding new ioctl
calls on the underlying driver and that the restrictions will be
enforced at the kernel interface layer (most likely by the kernel
itself).

For evtchn, foreignmemory, gnttab and gntshr this is hopefully
reasonably straightforward.

For call it is not so clear cut. Clearly the kernel cannot enforce
these restrictions for hypercalls which are not stable (domctl et al)
so they can never be on the whitelist. It may also be that potential
users would like to restrict the handle further than just a given
target domain, i.e. to a specific set of functionality (e.g. "things a
device model might reasonably do"). I think we will also need some way
to discover whether a given set of interfaces is available to a
restricted handle, in order to support the addition of new
functionality.

Notes:

- On many (all?) platforms libxencall and libxenforeignmemory are
  implemented by the same underlying privcmd driver. The platform
  level ioctl interface should support restricting the handle to only
  one or the other.
- On platforms with multiple privilege mapping ioctl variants should
  consider only allowing the newest/currently preferred one on a
  restricted handle. e.g. on Linux this would allow
  IOCTL_PRIVCMD_MMAPBATCH_V2 but not IOCTL_PRIVCMD_MMAPBATCH. (Of
  course any subsequently introduced _V3 would be subject to
  compatibility concerns)

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v8: New

This applies on top of the Xen portion of "Begin to disentangle
libxenctrl and provide some stable libraries", v7, plus a couple of
minor fixes which will be in v8. All of this can be found in the
"vwip" branch of the tree referenced by that series at
git://xenbits.xen.org/people/ianc/libxenctrl-split/xen.git.
---
 tools/libs/call/core.c                             |  7 +++
 tools/libs/call/include/xencall.h                  | 34 ++++++++++++++
 tools/libs/call/libxencall.map                     |  2 +
 tools/libs/evtchn/core.c                           |  7 +++
 tools/libs/evtchn/include/xenevtchn.h              | 36 +++++++++++++++
 tools/libs/evtchn/libxenevtchn.map                 |  2 +
 tools/libs/foreignmemory/core.c                    |  7 +++
 .../libs/foreignmemory/include/xenforeignmemory.h  | 22 +++++++++
 tools/libs/foreignmemory/libxenforeignmemory.map   |  3 ++
 tools/libs/gnttab/gntshr_core.c                    |  8 ++++
 tools/libs/gnttab/gnttab_core.c                    |  7 +++
 tools/libs/gnttab/include/xengnttab.h              | 52 ++++++++++++++++++++++
 tools/libs/gnttab/libxengnttab.map                 |  8 +++-
 13 files changed, 193 insertions(+), 2 deletions(-)

diff --git a/tools/libs/call/core.c b/tools/libs/call/core.c
index bbf88de..07283da 100644
--- a/tools/libs/call/core.c
+++ b/tools/libs/call/core.c
@@ -14,6 +14,7 @@
  */
 
 #include <stdlib.h>
+#include <errno.h>
 
 #include "private.h"
 
@@ -70,6 +71,12 @@ int xencall_close(xencall_handle *xcall)
     return rc;
 }
 
+int xencall_restrict_target(xencall_handle *xcall, uint32_t domid)
+{
+    errno = ENOSYS;
+    return -1;
+}
+
 int xencall0(xencall_handle *xcall, unsigned int op)
 {
     privcmd_hypercall_t call = {
diff --git a/tools/libs/call/include/xencall.h b/tools/libs/call/include/xencall.h
index 559624a..47c394d 100644
--- a/tools/libs/call/include/xencall.h
+++ b/tools/libs/call/include/xencall.h
@@ -73,6 +73,40 @@ xencall_handle *xencall_open(xentoollog_logger *logger, unsigned open_flags);
 int xencall_close(xencall_handle *xcall);
 
 /*
+ * Attempt to restrict the given xcall handle to only be able to
+ * target the given domain.
+ *
+ * On success returns 0, after which only hypercalls which are on a
+ * platform specific whitelist can be called and the arguments will be
+ * audited by the platform to ensure that the target domain is
+ * domid.
+ *
+ * Subsequent attempts to call any hypercall not on the platform
+ * specific whitelist will return -1 setting errno to ENOSYS.
+ *
+ * Subsequent attempts to call any hypercall on the platform specific
+ * whitelist with any other target domain return -1 setting errno to
+ * EPERM.
+ *
+ * These restrictions will be implemented by the platform in a way
+ * which cannot be circumvented by a userspace process. Further
+ * privilege drops (such as using setuid(2) etc) may also be required
+ * to prevent a compromised process from simply opening a second
+ * handle
+ *
+ * XXX which hypercalls are restricted, per platform list, do we need
+ * a way to probe? Do we want to be able to restrict to particular
+ * subsets of whitelisted hypercalls?
+ *
+ * On failure returns -1 and sets errno:
+ *   ENOSYS: The platform is not able to support restricting the
+ *           target domain.
+ *   Other: The platform should be able to support restricting the
+ *          target domain, but was unable to do so.
+ */
+int xencall_restrict_target(xencall_handle *xcall, uint32_t domid);
+
+/*
  * Call hypercalls with varying numbers of arguments.
  *
  * On success the return value of the hypercall is the return value of
diff --git a/tools/libs/call/libxencall.map b/tools/libs/call/libxencall.map
index 2f96144..d39f88e 100644
--- a/tools/libs/call/libxencall.map
+++ b/tools/libs/call/libxencall.map
@@ -3,6 +3,8 @@ VERS_1.0 {
 		xencall_open;
 		xencall_close;
 
+		xencall_restrict_target;
+
 		xencall0;
 		xencall1;
 		xencall2;
diff --git a/tools/libs/evtchn/core.c b/tools/libs/evtchn/core.c
index c31e08c..5f68f52 100644
--- a/tools/libs/evtchn/core.c
+++ b/tools/libs/evtchn/core.c
@@ -15,6 +15,7 @@
 
 #include <unistd.h>
 #include <stdlib.h>
+#include <errno.h>
 
 #include "private.h"
 
@@ -61,6 +62,12 @@ int xenevtchn_close(xenevtchn_handle *xce)
     return rc;
 }
 
+int xenevtchn_restrict_target(xenevtchn_handle *xce, uint32_t domid)
+{
+    errno = ENOSYS;
+    return -1;
+}
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libs/evtchn/include/xenevtchn.h b/tools/libs/evtchn/include/xenevtchn.h
index 4d26161..d67a4e4 100644
--- a/tools/libs/evtchn/include/xenevtchn.h
+++ b/tools/libs/evtchn/include/xenevtchn.h
@@ -74,6 +74,42 @@ xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags)
 int xenevtchn_close(xenevtchn_handle *xce);
 
 /*
+ * Attempt to restrict the given evtchn handle to only operate on the
+ * given domain.
+ *
+ * On success returns 0, after which:
+ *
+ * - Any operations which take a peer domain as an argument can only
+ *   be called with the specified target domain. Subsequent attempts
+ *   to call any such interface with another domain will return -1
+ *   setting errno to EPERM.
+ *
+ * - Any operations which take an evtchn_port_t are not restricted
+ *   other than by the requirement to have previously bound that
+ *   evtchn to the handle. Therefore users of the restrict interface
+ *   should take care not to bind any event channels relating to other
+ *   domains prior to enforcing the restriction. The restrictions on
+ *   xenevtchn_bind_*() (which take a domain id, see previous point)
+ *   suffice to prevent any new such bindings being created.
+ *
+ * - xenevtchn_bind_virq is not permitted and will return -1 setting
+ *   errno to EPERM.
+ *
+ * These restrictions will be implemented by the platform in a way
+ * which cannot be circumvented by a userspace process. Further
+ * privilege drops (such as using setuid(2) etc) may also be required
+ * to prevent a compromised process from simply opening a second
+ * handle
+ *
+ * On failure returns -1 and sets errno:
+ *   ENOSYS: The platform is not able to support restricting the
+ *           target domain.
+ *   Other: The platform should be able to support restricting the
+ *          target domain, but was unable to do so.
+ */
+int xenevtchn_restrict_target(xenevtchn_handle *xce, uint32_t domid);
+
+/*
  * Return an fd that can be select()ed on.
  *
  * Note that due to bugs, setting this fd to non blocking may not
diff --git a/tools/libs/evtchn/libxenevtchn.map b/tools/libs/evtchn/libxenevtchn.map
index 625a1e2..08e9dd5 100644
--- a/tools/libs/evtchn/libxenevtchn.map
+++ b/tools/libs/evtchn/libxenevtchn.map
@@ -3,6 +3,8 @@ VERS_1.0 {
 		xenevtchn_open;
 		xenevtchn_close;
 
+		xenevtchn_restrict_target;
+
 		xenevtchn_fd;
 
 		xenevtchn_bind_unbound_port;
diff --git a/tools/libs/foreignmemory/core.c b/tools/libs/foreignmemory/core.c
index cfb0a73..73e8034 100644
--- a/tools/libs/foreignmemory/core.c
+++ b/tools/libs/foreignmemory/core.c
@@ -62,6 +62,13 @@ int xenforeignmemory_close(xenforeignmemory_handle *fmem)
     return rc;
 }
 
+int xenforeignmemory_restrict_target(xenforeignmemory_handle *fmem,
+                                     uint32_t domid)
+{
+    errno = ENOSYS;
+    return -1;
+}
+
 void *xenforeignmemory_map(xenforeignmemory_handle *fmem,
                            uint32_t dom, int prot,
                            size_t num,
diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
index 3724c63..350ca75 100644
--- a/tools/libs/foreignmemory/include/xenforeignmemory.h
+++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
@@ -74,6 +74,28 @@ xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
 int xenforeignmemory_close(xenforeignmemory_handle *fmem);
 
 /*
+ * Attempt to restrict the given handle to only target the given
+ * domain.
+ *
+ * On success returns 0, after which calls to xenforeignmemory_map
+ * which pass a domain other than the given domain will return -1
+ * setting errno to EPERM.
+ *
+ * This restriction will be implemented by the platform in a way which
+ * cannot be circumvented by a userspace process. Further privilege
+ * drops (such as using setuid(2) etc) may also be required to prevent
+ * a compromised process from simply opening a second handle
+ *
+ * On failure returns -1 and sets errno:
+ *   ENOSYS: The platform is not able to support restricting the
+ *           target domain.
+ *   Other: The platform should be able to support restricting the
+ *          target domain, but was unable to do so.
+ */
+int xenforeignmemory_restrict_target(xenforeignmemory_handle *fmem,
+                                     uint32_t domid);
+
+/*
  * Maps a range within one domain to a local address range.  Mappings
  * must be unmapped with xenforeignmemory_unmap and should follow the
  * same rules as mmap regarding page alignment.
diff --git a/tools/libs/foreignmemory/libxenforeignmemory.map b/tools/libs/foreignmemory/libxenforeignmemory.map
index df206b3..dc1e0a1 100644
--- a/tools/libs/foreignmemory/libxenforeignmemory.map
+++ b/tools/libs/foreignmemory/libxenforeignmemory.map
@@ -2,6 +2,9 @@ VERS_1.0 {
 	global:
 		xenforeignmemory_open;
 		xenforeignmemory_close;
+
+		xenforeignmemory_restrict_target;
+
 		xenforeignmemory_map;
 		xenforeignmemory_unmap;
 	local: *; /* Do not expose anything by default */
diff --git a/tools/libs/gnttab/gntshr_core.c b/tools/libs/gnttab/gntshr_core.c
index 7f6bf9d..0347a16 100644
--- a/tools/libs/gnttab/gntshr_core.c
+++ b/tools/libs/gnttab/gntshr_core.c
@@ -19,6 +19,7 @@
  */
 
 #include <stdlib.h>
+#include <errno.h>
 
 #include "private.h"
 
@@ -64,6 +65,13 @@ int xengntshr_close(xengntshr_handle *xgs)
     free(xgs);
     return rc;
 }
+
+int xengntshr_restrict_target(xengntshr_handle *xgs, uint32_t domid)
+{
+    errno = ENOSYS;
+    return -1;
+}
+
 void *xengntshr_share_pages(xengntshr_handle *xcg, uint32_t domid,
                             int count, uint32_t *refs, int writable)
 {
diff --git a/tools/libs/gnttab/gnttab_core.c b/tools/libs/gnttab/gnttab_core.c
index 5d0474d..77ce1670 100644
--- a/tools/libs/gnttab/gnttab_core.c
+++ b/tools/libs/gnttab/gnttab_core.c
@@ -19,6 +19,7 @@
  */
 
 #include <stdlib.h>
+#include <errno.h>
 
 #include "private.h"
 
@@ -65,6 +66,12 @@ int xengnttab_close(xengnttab_handle *xgt)
     return rc;
 }
 
+int xengnttab_restrict_target(xengnttab_handle *xgt, uint32_t domid)
+{
+    errno = ENOSYS;
+    return -1;
+}
+
 int xengnttab_set_max_grants(xengnttab_handle *xgt, uint32_t count)
 {
     return osdep_gnttab_set_max_grants(xgt, count);
diff --git a/tools/libs/gnttab/include/xengnttab.h b/tools/libs/gnttab/include/xengnttab.h
index 7bf8462..8d0d26c 100644
--- a/tools/libs/gnttab/include/xengnttab.h
+++ b/tools/libs/gnttab/include/xengnttab.h
@@ -148,6 +148,33 @@ xengnttab_handle *xengnttab_open(xentoollog_logger *logger, unsigned open_flags)
  */
 int xengnttab_close(xengnttab_handle *xgt);
 
+/*
+ * Attempt to restrict the given handle to only target the given
+ * domain.
+ *
+ * On success returns 0, after which:
+ *
+ * - Calls to xengnttab_map_*() which are passed a domain other than
+ *   the given domain (either as an argument or as any member of a
+ *   domid array argument, regardless of the validity of other members
+ *   of the array) will return -1 setting errno to EPERM.
+ *
+ * - Calls to xengnttab_set_max_grants() will return -1 having set
+ *   errno to EPERM.
+ *
+ * This restriction will be implemented by the platform in a way which
+ * cannot be circumvented by a userspace process. Further privilege
+ * drops (such as using setuid(2) etc) may also be required to prevent
+ * a compromised process from simply opening a second handle
+ *
+ * On failure returns -1 and sets errno:
+ *   ENOSYS: The platform is not able to support restricting the
+ *           target domain.
+ *   Other: The platform should be able to support restricting the
+ *          target domain, but was unable to do so.
+ */
+int xengnttab_restrict_target(xengnttab_handle *xgt, uint32_t domid);
+
 /**
  * Memory maps a grant reference from one domain to a local address range.
  * Mappings should be unmapped with xengnttab_unmap.  Logs errors.
@@ -305,6 +332,31 @@ xengntshr_handle *xengntshr_open(xentoollog_logger *logger,
  */
 int xengntshr_close(xengntshr_handle *xgs);
 
+/*
+ * Attempt to restrict the given handle to only target the given
+ * domain.
+ *
+ * On success returns 0, after which:
+ *
+ * - Calls to xengntshr_share_*() which are passed a domain other than
+ *   the given domain will return -1 setting errno to EPERM.
+ *
+ * - Calls to xengnttab_set_max_grants() will return -1 having set
+ *   errno to EPERM.
+ *
+ * This restriction will be implemented by the platform in a way which
+ * cannot be circumvented by a userspace process. Further privilege
+ * drops (such as using setuid(2) etc) may also be required to prevent
+ * a compromised process from simply opening a second handle
+ *
+ * On failure returns -1 and sets errno:
+ *   ENOSYS: The platform is not able to support restricting the
+ *           target domain.
+ *   Other: The platform should be able to support restricting the
+ *          target domain, but was unable to do so.
+ */
+int xengntshr_restrict_target(xengntshr_handle *xgs, uint32_t domid);
+
 /**
  * Allocates and shares pages with another domain.
  *
diff --git a/tools/libs/gnttab/libxengnttab.map b/tools/libs/gnttab/libxengnttab.map
index 66e8c12..c3d7d49 100644
--- a/tools/libs/gnttab/libxengnttab.map
+++ b/tools/libs/gnttab/libxengnttab.map
@@ -3,6 +3,8 @@ VERS_1.0 {
 		xengnttab_open;
 		xengnttab_close;
 
+		xengnttab_restrict_target;
+
 		xengnttab_set_max_grants;
 
 		xengnttab_map_domain_grant_refs;
@@ -11,10 +13,12 @@ VERS_1.0 {
 		xengnttab_map_grant_refs;
 
 		xengnttab_unmap;
-		
+
 		xengntshr_open;
 		xengntshr_close;
-		
+
+		xengntshr_restrict_target;
+
 		xengntshr_share_page_notify;
 		xengntshr_share_pages;
 		
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH XEN v8 29/29] HACK: Update Config.mk to pull all the right bits from my xenbits trees
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
                     ` (27 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  28 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

v4: Config.mk instead of .config
---
 Config.mk | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)

diff --git a/Config.mk b/Config.mk
index d654af8..364e790 100644
--- a/Config.mk
+++ b/Config.mk
@@ -238,22 +238,22 @@ endif
 
 ifeq ($(GIT_HTTP),y)
 OVMF_UPSTREAM_URL ?= http://xenbits.xen.org/git-http/ovmf.git
-QEMU_UPSTREAM_URL ?= http://xenbits.xen.org/git-http/qemu-xen.git
-QEMU_TRADITIONAL_URL ?= http://xenbits.xen.org/git-http/qemu-xen-traditional.git
+QEMU_UPSTREAM_URL ?= http://xenbits.xen.org/git-http/people/ianc/libxenctrl-split/qemu-xen.git
+QEMU_TRADITIONAL_URL ?= http://xenbits.xen.org/git-http/people/ianc/libxenctrl-split/qemu-xen-traditional.git
 SEABIOS_UPSTREAM_URL ?= http://xenbits.xen.org/git-http/seabios.git
-MINIOS_UPSTREAM_URL ?= http://xenbits.xen.org/git-http/mini-os.git
+MINIOS_UPSTREAM_URL ?= http://xenbits.xen.org/git-http/people/ianc/libxenctrl-split/mini-os.git
 else
 OVMF_UPSTREAM_URL ?= git://xenbits.xen.org/ovmf.git
-QEMU_UPSTREAM_URL ?= git://xenbits.xen.org/qemu-xen.git
-QEMU_TRADITIONAL_URL ?= git://xenbits.xen.org/qemu-xen-traditional.git
+QEMU_UPSTREAM_URL ?= git://xenbits.xen.org/people/ianc/libxenctrl-split/qemu-xen.git
+QEMU_TRADITIONAL_URL ?= git://xenbits.xen.org/people/ianc/libxenctrl-split/qemu-xen-traditional.git
 SEABIOS_UPSTREAM_URL ?= git://xenbits.xen.org/seabios.git
-MINIOS_UPSTREAM_URL ?= git://xenbits.xen.org/mini-os.git
+MINIOS_UPSTREAM_URL ?= git://xenbits.xen.org/people/ianc/libxenctrl-split/mini-os.git
 endif
 OVMF_UPSTREAM_REVISION ?= 52a99493cce88a9d4ec8a02d7f1bd1a1001ce60d
-QEMU_UPSTREAM_REVISION ?= master
-MINIOS_UPSTREAM_REVISION ?= d25773c8afa2f4dbbb466116daeb60159ddd22bd
-# Thu Dec 3 11:23:25 2015 +0000
-# mini-os: Include libxentoollog with libxc
+QEMU_UPSTREAM_REVISION ?= origin/v8
+MINIOS_UPSTREAM_REVISION ?= origin/v8
+# Mon Nov 23 16:34:31 2015 +0000
+# Add a .gitignore
 
 SEABIOS_UPSTREAM_REVISION ?= 3403ac4313812752be6e6aac35239ca6888a8cab
 # Mon Dec 28 13:50:41 2015 +0100
@@ -262,7 +262,7 @@ SEABIOS_UPSTREAM_REVISION ?= 3403ac4313812752be6e6aac35239ca6888a8cab
 ETHERBOOT_NICS ?= rtl8139 8086100e
 
 
-QEMU_TRADITIONAL_REVISION ?= 569eac99e8ddccd15fe78e8a3af5622afe780e3b
+QEMU_TRADITIONAL_REVISION ?= origin/v8
 # Mon Jan 4 15:34:29 2016 +0000
 # MSI-X: avoid array overrun upon MSI-X table writes
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
  2016-01-15 13:23 ` [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
@ 2016-01-15 13:23 ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                     ` (9 more replies)
  2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
                   ` (5 subsequent siblings)
  8 siblings, 10 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

We intend to stabilise some parts of the libxenctrl interface by
splitting out some functionality into separate stable libraries.

This is the qemu-xen part of the first phase of that change.

This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
super-intro mail covering all of the related patch series and which
contains more details.

Ian Campbell (8):
  xen_console: correctly cleanup primary console on teardown.
  xen: Switch to libxenevtchn interface for compat shims.
  xen: Switch to libxengnttab interface for compat shims.
  xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
  xen: Switch uses of xc_map_foreign_{pages,bulk} to use
    libxenforeignmemory API.
  xen: Use stable library interfaces when they are available.
  xen: domainbuild: reopen libxenctrl interface after forking for domain
    watcher.
  xen: make it possible to build without the Xen PV domain builder

 configure                    |  70 ++++++++++++++++++++
 hw/block/xen_disk.c          |  38 +++++------
 hw/char/xen_console.c        |  19 +++---
 hw/display/xenfb.c           |  28 ++++----
 hw/net/xen_nic.c             |  18 +++---
 hw/xen/xen_backend.c         |  44 +++++++------
 hw/xenpv/Makefile.objs       |   4 +-
 hw/xenpv/xen_domainbuild.c   |   9 ++-
 hw/xenpv/xen_machine_pv.c    |  15 +++--
 include/hw/xen/xen_backend.h |   5 +-
 include/hw/xen/xen_common.h  | 149 ++++++++++++++++++++++++++++++++++---------
 xen-common.c                 |   6 ++
 xen-hvm.c                    |  39 +++++------
 xen-mapcache.c               |   6 +-
 14 files changed, 315 insertions(+), 135 deletions(-)

-- 
2.1.4

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
  2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
@ 2016-01-15 13:23 ` Ian Campbell
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
                   ` (6 subsequent siblings)
  8 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

We intend to stabilise some parts of the libxenctrl interface by
splitting out some functionality into separate stable libraries.

This is the qemu-xen part of the first phase of that change.

This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
super-intro mail covering all of the related patch series and which
contains more details.

Ian Campbell (8):
  xen_console: correctly cleanup primary console on teardown.
  xen: Switch to libxenevtchn interface for compat shims.
  xen: Switch to libxengnttab interface for compat shims.
  xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
  xen: Switch uses of xc_map_foreign_{pages,bulk} to use
    libxenforeignmemory API.
  xen: Use stable library interfaces when they are available.
  xen: domainbuild: reopen libxenctrl interface after forking for domain
    watcher.
  xen: make it possible to build without the Xen PV domain builder

 configure                    |  70 ++++++++++++++++++++
 hw/block/xen_disk.c          |  38 +++++------
 hw/char/xen_console.c        |  19 +++---
 hw/display/xenfb.c           |  28 ++++----
 hw/net/xen_nic.c             |  18 +++---
 hw/xen/xen_backend.c         |  44 +++++++------
 hw/xenpv/Makefile.objs       |   4 +-
 hw/xenpv/xen_domainbuild.c   |   9 ++-
 hw/xenpv/xen_machine_pv.c    |  15 +++--
 include/hw/xen/xen_backend.h |   5 +-
 include/hw/xen/xen_common.h  | 149 ++++++++++++++++++++++++++++++++++---------
 xen-common.c                 |   6 ++
 xen-hvm.c                    |  39 +++++------
 xen-mapcache.c               |   6 +-
 14 files changed, 315 insertions(+), 135 deletions(-)

-- 
2.1.4

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 1/8] xen_console: correctly cleanup primary console on teardown.
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

All of the work in con_disconnect applies to the primary console case
(when xendev->dev is NULL). Therefore remove the early check and bail
and allow it to fall through. All of the existing code is correctly
conditional already.

The ->dev and ->gnttabdev handles are either both set or neither. For
consistency with con_initialise() with to the former here too.

With this con_initialise and con_disconnect now mirror each other.

Fix up a hard tab in the function while editing.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>

---
v4: New patch based on feedback to "xen: Switch uses of
xc_map_foreign_bulk to use libxenforeignmemory API."
---
 hw/char/xen_console.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index eb7f450..63ade33 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -265,9 +265,6 @@ static void con_disconnect(struct XenDevice *xendev)
 {
     struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
 
-    if (!xendev->dev) {
-        return;
-    }
     if (con->chr) {
         qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL);
         qemu_chr_fe_release(con->chr);
@@ -275,12 +272,12 @@ static void con_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&con->xendev);
 
     if (con->sring) {
-        if (!xendev->gnttabdev) {
+        if (!xendev->dev) {
             munmap(con->sring, XC_PAGE_SIZE);
         } else {
             xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
         }
-	con->sring = NULL;
+        con->sring = NULL;
     }
 }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 1/8] xen_console: correctly cleanup primary console on teardown.
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

All of the work in con_disconnect applies to the primary console case
(when xendev->dev is NULL). Therefore remove the early check and bail
and allow it to fall through. All of the existing code is correctly
conditional already.

The ->dev and ->gnttabdev handles are either both set or neither. For
consistency with con_initialise() with to the former here too.

With this con_initialise and con_disconnect now mirror each other.

Fix up a hard tab in the function while editing.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>

---
v4: New patch based on feedback to "xen: Switch uses of
xc_map_foreign_bulk to use libxenforeignmemory API."
---
 hw/char/xen_console.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index eb7f450..63ade33 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -265,9 +265,6 @@ static void con_disconnect(struct XenDevice *xendev)
 {
     struct XenConsole *con = container_of(xendev, struct XenConsole, xendev);
 
-    if (!xendev->dev) {
-        return;
-    }
     if (con->chr) {
         qemu_chr_add_handlers(con->chr, NULL, NULL, NULL, NULL);
         qemu_chr_fe_release(con->chr);
@@ -275,12 +272,12 @@ static void con_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&con->xendev);
 
     if (con->sring) {
-        if (!xendev->gnttabdev) {
+        if (!xendev->dev) {
             munmap(con->sring, XC_PAGE_SIZE);
         } else {
             xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
         }
-	con->sring = NULL;
+        con->sring = NULL;
     }
 }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 2/8] xen: Switch to libxenevtchn interface for compat shims.
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxenevtchn which provides access to event
channels.

In preparation for this switch the compatibility layer in xen_common.h
(which support building with older versions of Xen) to use what will
be the new library API. This means that the evtchn shim will disappear
for versions of Xen which include libxenevtchn.

To simplify things for the <= 4.0.0 support we wrap the int fd in a
malloc(sizeof int) such that the handle is always a pointer. This
leads to less typedef headaches and the need for
XC_HANDLER_INITIAL_VALUE etc for these interfaces.

Note that this patch does not add any support for actually using
libxenevtchn, it just adjusts the existing shims.

Note that xc_evtchn_alloc_unbound functionality remains in libxenctrl,
since that functionality is not exposed by /dev/xen/evtchn.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v4: Ran checkpatch, fixed all errors
    Allocate correct size for handle (i.e. not size of the ptr)
---
 hw/xen/xen_backend.c         | 31 ++++++++++++++++---------------
 include/hw/xen/xen_backend.h |  2 +-
 include/hw/xen/xen_common.h  | 44 ++++++++++++++++++++++++++++++++++----------
 xen-hvm.c                    | 25 +++++++++++++------------
 4 files changed, 64 insertions(+), 38 deletions(-)

diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
index 2510e2e..ae2a1f0 100644
--- a/hw/xen/xen_backend.c
+++ b/hw/xen/xen_backend.c
@@ -243,19 +243,19 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     xendev->debug      = debug;
     xendev->local_port = -1;
 
-    xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
-    if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
+    xendev->evtchndev = xenevtchn_open(NULL, 0);
+    if (xendev->evtchndev == NULL) {
         xen_be_printf(NULL, 0, "can't open evtchn device\n");
         g_free(xendev);
         return NULL;
     }
-    fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
+    fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
         xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
         if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
             xen_be_printf(NULL, 0, "can't open gnttab device\n");
-            xc_evtchn_close(xendev->evtchndev);
+            xenevtchn_close(xendev->evtchndev);
             g_free(xendev);
             return NULL;
         }
@@ -306,8 +306,8 @@ static struct XenDevice *xen_be_del_xendev(int dom, int dev)
             g_free(xendev->fe);
         }
 
-        if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
-            xc_evtchn_close(xendev->evtchndev);
+        if (xendev->evtchndev != NULL) {
+            xenevtchn_close(xendev->evtchndev);
         }
         if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
             xc_gnttab_close(xendev->gnttabdev);
@@ -691,13 +691,14 @@ static void xen_be_evtchn_event(void *opaque)
     struct XenDevice *xendev = opaque;
     evtchn_port_t port;
 
-    port = xc_evtchn_pending(xendev->evtchndev);
+    port = xenevtchn_pending(xendev->evtchndev);
     if (port != xendev->local_port) {
-        xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
+        xen_be_printf(xendev, 0,
+                      "xenevtchn_pending returned %d (expected %d)\n",
                       port, xendev->local_port);
         return;
     }
-    xc_evtchn_unmask(xendev->evtchndev, port);
+    xenevtchn_unmask(xendev->evtchndev, port);
 
     if (xendev->ops->event) {
         xendev->ops->event(xendev);
@@ -740,14 +741,14 @@ int xen_be_bind_evtchn(struct XenDevice *xendev)
     if (xendev->local_port != -1) {
         return 0;
     }
-    xendev->local_port = xc_evtchn_bind_interdomain
+    xendev->local_port = xenevtchn_bind_interdomain
         (xendev->evtchndev, xendev->dom, xendev->remote_port);
     if (xendev->local_port == -1) {
-        xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
+        xen_be_printf(xendev, 0, "xenevtchn_bind_interdomain failed\n");
         return -1;
     }
     xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev),
                         xen_be_evtchn_event, NULL, xendev);
     return 0;
 }
@@ -757,15 +758,15 @@ void xen_be_unbind_evtchn(struct XenDevice *xendev)
     if (xendev->local_port == -1) {
         return;
     }
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
-    xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
+    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
+    xenevtchn_unbind(xendev->evtchndev, xendev->local_port);
     xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
     xendev->local_port = -1;
 }
 
 int xen_be_send_notify(struct XenDevice *xendev)
 {
-    return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
+    return xenevtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
 /*
diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
index 3b4125e..a90314f 100644
--- a/include/hw/xen/xen_backend.h
+++ b/include/hw/xen/xen_backend.h
@@ -46,7 +46,7 @@ struct XenDevice {
     int                remote_port;
     int                local_port;
 
-    XenEvtchn          evtchndev;
+    xenevtchn_handle   *evtchndev;
     XenGnttab          gnttabdev;
 
     struct XenDevOps   *ops;
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 4ac0c6f..5c51b44 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -39,17 +39,38 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 410
 
 typedef int XenXC;
-typedef int XenEvtchn;
+typedef int xenevtchn_handle;
 typedef int XenGnttab;
 
 #  define XC_INTERFACE_FMT "%i"
 #  define XC_HANDLER_INITIAL_VALUE    -1
 
-static inline XenEvtchn xen_xc_evtchn_open(void *logger,
-                                           unsigned int open_flags)
+static inline xenevtchn_handle *xenevtchn_open(void *logger,
+                                               unsigned int open_flags)
 {
-    return xc_evtchn_open();
+    xenevtchn_handle *h = malloc(sizeof(*h));
+    if (!h) {
+        return NULL;
+    }
+    *h = xc_evtchn_open();
+    if (*h == -1) {
+        free(h);
+        h = NULL;
+    }
+    return h;
 }
+static inline int xenevtchn_close(xenevtchn_handle *h)
+{
+    int rc = xc_evtchn_close(*h);
+    free(h);
+    return rc;
+}
+#define xenevtchn_fd(h) xc_evtchn_fd(*h)
+#define xenevtchn_pending(h) xc_evtchn_pending(*h)
+#define xenevtchn_notify(h, p) xc_evtchn_notify(*h, p)
+#define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(*h, d, p)
+#define xenevtchn_unmask(h, p) xc_evtchn_unmask(*h, p)
+#define xenevtchn_unbind(h, p) xc_evtchn_unmask(*h, p)
 
 static inline XenGnttab xen_xc_gnttab_open(void *logger,
                                            unsigned int open_flags)
@@ -108,17 +129,20 @@ static inline void xs_close(struct xs_handle *xsh)
 #else
 
 typedef xc_interface *XenXC;
-typedef xc_evtchn *XenEvtchn;
+typedef xc_evtchn xenevtchn_handle;
 typedef xc_gnttab *XenGnttab;
 
 #  define XC_INTERFACE_FMT "%p"
 #  define XC_HANDLER_INITIAL_VALUE    NULL
 
-static inline XenEvtchn xen_xc_evtchn_open(void *logger,
-                                           unsigned int open_flags)
-{
-    return xc_evtchn_open(logger, open_flags);
-}
+#define xenevtchn_open(l, f) xc_evtchn_open(l, f);
+#define xenevtchn_close(h) xc_evtchn_close(h)
+#define xenevtchn_fd(h) xc_evtchn_fd(h)
+#define xenevtchn_pending(h) xc_evtchn_pending(h)
+#define xenevtchn_notify(h, p) xc_evtchn_notify(h, p)
+#define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(h, d, p)
+#define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p)
+#define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p)
 
 static inline XenGnttab xen_xc_gnttab_open(void *logger,
                                            unsigned int open_flags)
diff --git a/xen-hvm.c b/xen-hvm.c
index 2a93390..6227e17 100644
--- a/xen-hvm.c
+++ b/xen-hvm.c
@@ -109,7 +109,7 @@ typedef struct XenIOState {
     /* evtchn local port for buffered io */
     evtchn_port_t bufioreq_local_port;
     /* the evtchn fd for polling */
-    XenEvtchn xce_handle;
+    xenevtchn_handle *xce_handle;
     /* which vcpu we are serving */
     int send_vcpu;
 
@@ -715,7 +715,7 @@ static ioreq_t *cpu_get_ioreq(XenIOState *state)
     int i;
     evtchn_port_t port;
 
-    port = xc_evtchn_pending(state->xce_handle);
+    port = xenevtchn_pending(state->xce_handle);
     if (port == state->bufioreq_local_port) {
         timer_mod(state->buffered_io_timer,
                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
@@ -734,7 +734,7 @@ static ioreq_t *cpu_get_ioreq(XenIOState *state)
         }
 
         /* unmask the wanted port again */
-        xc_evtchn_unmask(state->xce_handle, port);
+        xenevtchn_unmask(state->xce_handle, port);
 
         /* get the io packet from shared memory */
         state->send_vcpu = i;
@@ -1041,7 +1041,7 @@ static void handle_buffered_io(void *opaque)
                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
     } else {
         timer_del(state->buffered_io_timer);
-        xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
+        xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
     }
 }
 
@@ -1085,7 +1085,8 @@ static void cpu_handle_ioreq(void *opaque)
         }
 
         req->state = STATE_IORESP_READY;
-        xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
+        xenevtchn_notify(state->xce_handle,
+                         state->ioreq_local_port[state->send_vcpu]);
     }
 }
 
@@ -1093,8 +1094,8 @@ static void xen_main_loop_prepare(XenIOState *state)
 {
     int evtchn_fd = -1;
 
-    if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
-        evtchn_fd = xc_evtchn_fd(state->xce_handle);
+    if (state->xce_handle != NULL) {
+        evtchn_fd = xenevtchn_fd(state->xce_handle);
     }
 
     state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
@@ -1132,7 +1133,7 @@ static void xen_exit_notifier(Notifier *n, void *data)
 {
     XenIOState *state = container_of(n, XenIOState, exit);
 
-    xc_evtchn_close(state->xce_handle);
+    xenevtchn_close(state->xce_handle);
     xs_daemon_close(state->xenstore);
 }
 
@@ -1207,8 +1208,8 @@ int xen_hvm_init(PCMachineState *pcms,
 
     state = g_malloc0(sizeof (XenIOState));
 
-    state->xce_handle = xen_xc_evtchn_open(NULL, 0);
-    if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
+    state->xce_handle = xenevtchn_open(NULL, 0);
+    if (state->xce_handle == NULL) {
         perror("xen: event channel open");
         return -1;
     }
@@ -1288,7 +1289,7 @@ int xen_hvm_init(PCMachineState *pcms,
 
     /* FIXME: how about if we overflow the page here? */
     for (i = 0; i < max_cpus; i++) {
-        rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
+        rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
                                         xen_vcpu_eport(state->shared_page, i));
         if (rc == -1) {
             fprintf(stderr, "shared evtchn %d bind error %d\n", i, errno);
@@ -1297,7 +1298,7 @@ int xen_hvm_init(PCMachineState *pcms,
         state->ioreq_local_port[i] = rc;
     }
 
-    rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
+    rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
                                     bufioreq_evtchn);
     if (rc == -1) {
         fprintf(stderr, "buffered evtchn bind error %d\n", errno);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 2/8] xen: Switch to libxenevtchn interface for compat shims.
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxenevtchn which provides access to event
channels.

In preparation for this switch the compatibility layer in xen_common.h
(which support building with older versions of Xen) to use what will
be the new library API. This means that the evtchn shim will disappear
for versions of Xen which include libxenevtchn.

To simplify things for the <= 4.0.0 support we wrap the int fd in a
malloc(sizeof int) such that the handle is always a pointer. This
leads to less typedef headaches and the need for
XC_HANDLER_INITIAL_VALUE etc for these interfaces.

Note that this patch does not add any support for actually using
libxenevtchn, it just adjusts the existing shims.

Note that xc_evtchn_alloc_unbound functionality remains in libxenctrl,
since that functionality is not exposed by /dev/xen/evtchn.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v4: Ran checkpatch, fixed all errors
    Allocate correct size for handle (i.e. not size of the ptr)
---
 hw/xen/xen_backend.c         | 31 ++++++++++++++++---------------
 include/hw/xen/xen_backend.h |  2 +-
 include/hw/xen/xen_common.h  | 44 ++++++++++++++++++++++++++++++++++----------
 xen-hvm.c                    | 25 +++++++++++++------------
 4 files changed, 64 insertions(+), 38 deletions(-)

diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
index 2510e2e..ae2a1f0 100644
--- a/hw/xen/xen_backend.c
+++ b/hw/xen/xen_backend.c
@@ -243,19 +243,19 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     xendev->debug      = debug;
     xendev->local_port = -1;
 
-    xendev->evtchndev = xen_xc_evtchn_open(NULL, 0);
-    if (xendev->evtchndev == XC_HANDLER_INITIAL_VALUE) {
+    xendev->evtchndev = xenevtchn_open(NULL, 0);
+    if (xendev->evtchndev == NULL) {
         xen_be_printf(NULL, 0, "can't open evtchn device\n");
         g_free(xendev);
         return NULL;
     }
-    fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
+    fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
         xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
         if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
             xen_be_printf(NULL, 0, "can't open gnttab device\n");
-            xc_evtchn_close(xendev->evtchndev);
+            xenevtchn_close(xendev->evtchndev);
             g_free(xendev);
             return NULL;
         }
@@ -306,8 +306,8 @@ static struct XenDevice *xen_be_del_xendev(int dom, int dev)
             g_free(xendev->fe);
         }
 
-        if (xendev->evtchndev != XC_HANDLER_INITIAL_VALUE) {
-            xc_evtchn_close(xendev->evtchndev);
+        if (xendev->evtchndev != NULL) {
+            xenevtchn_close(xendev->evtchndev);
         }
         if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
             xc_gnttab_close(xendev->gnttabdev);
@@ -691,13 +691,14 @@ static void xen_be_evtchn_event(void *opaque)
     struct XenDevice *xendev = opaque;
     evtchn_port_t port;
 
-    port = xc_evtchn_pending(xendev->evtchndev);
+    port = xenevtchn_pending(xendev->evtchndev);
     if (port != xendev->local_port) {
-        xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
+        xen_be_printf(xendev, 0,
+                      "xenevtchn_pending returned %d (expected %d)\n",
                       port, xendev->local_port);
         return;
     }
-    xc_evtchn_unmask(xendev->evtchndev, port);
+    xenevtchn_unmask(xendev->evtchndev, port);
 
     if (xendev->ops->event) {
         xendev->ops->event(xendev);
@@ -740,14 +741,14 @@ int xen_be_bind_evtchn(struct XenDevice *xendev)
     if (xendev->local_port != -1) {
         return 0;
     }
-    xendev->local_port = xc_evtchn_bind_interdomain
+    xendev->local_port = xenevtchn_bind_interdomain
         (xendev->evtchndev, xendev->dom, xendev->remote_port);
     if (xendev->local_port == -1) {
-        xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
+        xen_be_printf(xendev, 0, "xenevtchn_bind_interdomain failed\n");
         return -1;
     }
     xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev),
                         xen_be_evtchn_event, NULL, xendev);
     return 0;
 }
@@ -757,15 +758,15 @@ void xen_be_unbind_evtchn(struct XenDevice *xendev)
     if (xendev->local_port == -1) {
         return;
     }
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
-    xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
+    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
+    xenevtchn_unbind(xendev->evtchndev, xendev->local_port);
     xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
     xendev->local_port = -1;
 }
 
 int xen_be_send_notify(struct XenDevice *xendev)
 {
-    return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
+    return xenevtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
 /*
diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
index 3b4125e..a90314f 100644
--- a/include/hw/xen/xen_backend.h
+++ b/include/hw/xen/xen_backend.h
@@ -46,7 +46,7 @@ struct XenDevice {
     int                remote_port;
     int                local_port;
 
-    XenEvtchn          evtchndev;
+    xenevtchn_handle   *evtchndev;
     XenGnttab          gnttabdev;
 
     struct XenDevOps   *ops;
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 4ac0c6f..5c51b44 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -39,17 +39,38 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 410
 
 typedef int XenXC;
-typedef int XenEvtchn;
+typedef int xenevtchn_handle;
 typedef int XenGnttab;
 
 #  define XC_INTERFACE_FMT "%i"
 #  define XC_HANDLER_INITIAL_VALUE    -1
 
-static inline XenEvtchn xen_xc_evtchn_open(void *logger,
-                                           unsigned int open_flags)
+static inline xenevtchn_handle *xenevtchn_open(void *logger,
+                                               unsigned int open_flags)
 {
-    return xc_evtchn_open();
+    xenevtchn_handle *h = malloc(sizeof(*h));
+    if (!h) {
+        return NULL;
+    }
+    *h = xc_evtchn_open();
+    if (*h == -1) {
+        free(h);
+        h = NULL;
+    }
+    return h;
 }
+static inline int xenevtchn_close(xenevtchn_handle *h)
+{
+    int rc = xc_evtchn_close(*h);
+    free(h);
+    return rc;
+}
+#define xenevtchn_fd(h) xc_evtchn_fd(*h)
+#define xenevtchn_pending(h) xc_evtchn_pending(*h)
+#define xenevtchn_notify(h, p) xc_evtchn_notify(*h, p)
+#define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(*h, d, p)
+#define xenevtchn_unmask(h, p) xc_evtchn_unmask(*h, p)
+#define xenevtchn_unbind(h, p) xc_evtchn_unmask(*h, p)
 
 static inline XenGnttab xen_xc_gnttab_open(void *logger,
                                            unsigned int open_flags)
@@ -108,17 +129,20 @@ static inline void xs_close(struct xs_handle *xsh)
 #else
 
 typedef xc_interface *XenXC;
-typedef xc_evtchn *XenEvtchn;
+typedef xc_evtchn xenevtchn_handle;
 typedef xc_gnttab *XenGnttab;
 
 #  define XC_INTERFACE_FMT "%p"
 #  define XC_HANDLER_INITIAL_VALUE    NULL
 
-static inline XenEvtchn xen_xc_evtchn_open(void *logger,
-                                           unsigned int open_flags)
-{
-    return xc_evtchn_open(logger, open_flags);
-}
+#define xenevtchn_open(l, f) xc_evtchn_open(l, f);
+#define xenevtchn_close(h) xc_evtchn_close(h)
+#define xenevtchn_fd(h) xc_evtchn_fd(h)
+#define xenevtchn_pending(h) xc_evtchn_pending(h)
+#define xenevtchn_notify(h, p) xc_evtchn_notify(h, p)
+#define xenevtchn_bind_interdomain(h, d, p) xc_evtchn_bind_interdomain(h, d, p)
+#define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p)
+#define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p)
 
 static inline XenGnttab xen_xc_gnttab_open(void *logger,
                                            unsigned int open_flags)
diff --git a/xen-hvm.c b/xen-hvm.c
index 2a93390..6227e17 100644
--- a/xen-hvm.c
+++ b/xen-hvm.c
@@ -109,7 +109,7 @@ typedef struct XenIOState {
     /* evtchn local port for buffered io */
     evtchn_port_t bufioreq_local_port;
     /* the evtchn fd for polling */
-    XenEvtchn xce_handle;
+    xenevtchn_handle *xce_handle;
     /* which vcpu we are serving */
     int send_vcpu;
 
@@ -715,7 +715,7 @@ static ioreq_t *cpu_get_ioreq(XenIOState *state)
     int i;
     evtchn_port_t port;
 
-    port = xc_evtchn_pending(state->xce_handle);
+    port = xenevtchn_pending(state->xce_handle);
     if (port == state->bufioreq_local_port) {
         timer_mod(state->buffered_io_timer,
                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
@@ -734,7 +734,7 @@ static ioreq_t *cpu_get_ioreq(XenIOState *state)
         }
 
         /* unmask the wanted port again */
-        xc_evtchn_unmask(state->xce_handle, port);
+        xenevtchn_unmask(state->xce_handle, port);
 
         /* get the io packet from shared memory */
         state->send_vcpu = i;
@@ -1041,7 +1041,7 @@ static void handle_buffered_io(void *opaque)
                 BUFFER_IO_MAX_DELAY + qemu_clock_get_ms(QEMU_CLOCK_REALTIME));
     } else {
         timer_del(state->buffered_io_timer);
-        xc_evtchn_unmask(state->xce_handle, state->bufioreq_local_port);
+        xenevtchn_unmask(state->xce_handle, state->bufioreq_local_port);
     }
 }
 
@@ -1085,7 +1085,8 @@ static void cpu_handle_ioreq(void *opaque)
         }
 
         req->state = STATE_IORESP_READY;
-        xc_evtchn_notify(state->xce_handle, state->ioreq_local_port[state->send_vcpu]);
+        xenevtchn_notify(state->xce_handle,
+                         state->ioreq_local_port[state->send_vcpu]);
     }
 }
 
@@ -1093,8 +1094,8 @@ static void xen_main_loop_prepare(XenIOState *state)
 {
     int evtchn_fd = -1;
 
-    if (state->xce_handle != XC_HANDLER_INITIAL_VALUE) {
-        evtchn_fd = xc_evtchn_fd(state->xce_handle);
+    if (state->xce_handle != NULL) {
+        evtchn_fd = xenevtchn_fd(state->xce_handle);
     }
 
     state->buffered_io_timer = timer_new_ms(QEMU_CLOCK_REALTIME, handle_buffered_io,
@@ -1132,7 +1133,7 @@ static void xen_exit_notifier(Notifier *n, void *data)
 {
     XenIOState *state = container_of(n, XenIOState, exit);
 
-    xc_evtchn_close(state->xce_handle);
+    xenevtchn_close(state->xce_handle);
     xs_daemon_close(state->xenstore);
 }
 
@@ -1207,8 +1208,8 @@ int xen_hvm_init(PCMachineState *pcms,
 
     state = g_malloc0(sizeof (XenIOState));
 
-    state->xce_handle = xen_xc_evtchn_open(NULL, 0);
-    if (state->xce_handle == XC_HANDLER_INITIAL_VALUE) {
+    state->xce_handle = xenevtchn_open(NULL, 0);
+    if (state->xce_handle == NULL) {
         perror("xen: event channel open");
         return -1;
     }
@@ -1288,7 +1289,7 @@ int xen_hvm_init(PCMachineState *pcms,
 
     /* FIXME: how about if we overflow the page here? */
     for (i = 0; i < max_cpus; i++) {
-        rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
+        rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
                                         xen_vcpu_eport(state->shared_page, i));
         if (rc == -1) {
             fprintf(stderr, "shared evtchn %d bind error %d\n", i, errno);
@@ -1297,7 +1298,7 @@ int xen_hvm_init(PCMachineState *pcms,
         state->ioreq_local_port[i] = rc;
     }
 
-    rc = xc_evtchn_bind_interdomain(state->xce_handle, xen_domid,
+    rc = xenevtchn_bind_interdomain(state->xce_handle, xen_domid,
                                     bufioreq_evtchn);
     if (rc == -1) {
         fprintf(stderr, "buffered evtchn bind error %d\n", errno);
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 3/8] xen: Switch to libxengnttab interface for compat shims.
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxengnttab which provides access to grant
tables.

In preparation for this switch the compatibility layer in xen_common.h
(which support building with older versions of Xen) to use what will
be the new library API. This means that the gnttab shim will disappear
for versions of Xen which include libxengnttab.

To simplify things for the <= 4.0.0 support we wrap the int fd in a
malloc(sizeof int) such that the handle is always a pointer. This
leads to less typedef headaches and the need for
XC_HANDLER_INITIAL_VALUE etc for these interfaces.

Note that this patch does not add any support for actually using
libxengnttab, it just adjusts the existing shims.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v4: Ran checkpatch, fixed all errors
    Allocate correct size for handle (i.e. not size of the ptr)
    Rebase onto "xen_console: correctly cleanup primary console on
    teardown."

v5: Rebase over b4f72e31b924 "hw/net/xen_nic.c: Free 'netdev->txs'
when map 'netdev->rxs' fails" which added a new gnttab_munmap.

v7: s/gnttab_munmap/gnttab_unmap/ to reflect change in the library.
---
 hw/block/xen_disk.c          | 38 ++++++++++++++++++++------------------
 hw/char/xen_console.c        |  4 ++--
 hw/net/xen_nic.c             | 18 +++++++++---------
 hw/xen/xen_backend.c         | 10 +++++-----
 include/hw/xen/xen_backend.h |  2 +-
 include/hw/xen/xen_common.h  | 42 ++++++++++++++++++++++++++++++++----------
 6 files changed, 69 insertions(+), 45 deletions(-)

diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
index a48e726..326c948 100644
--- a/hw/block/xen_disk.c
+++ b/hw/block/xen_disk.c
@@ -171,11 +171,11 @@ static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
 static void destroy_grant(gpointer pgnt)
 {
     PersistentGrant *grant = pgnt;
-    XenGnttab gnt = grant->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = grant->blkdev->xendev.gnttabdev;
 
-    if (xc_gnttab_munmap(gnt, grant->page, 1) != 0) {
+    if (xengnttab_unmap(gnt, grant->page, 1) != 0) {
         xen_be_printf(&grant->blkdev->xendev, 0,
-                      "xc_gnttab_munmap failed: %s\n",
+                      "xengnttab_unmap failed: %s\n",
                       strerror(errno));
     }
     grant->blkdev->persistent_gnt_count--;
@@ -188,11 +188,11 @@ static void remove_persistent_region(gpointer data, gpointer dev)
 {
     PersistentRegion *region = data;
     struct XenBlkDev *blkdev = dev;
-    XenGnttab gnt = blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = blkdev->xendev.gnttabdev;
 
-    if (xc_gnttab_munmap(gnt, region->addr, region->num) != 0) {
+    if (xengnttab_unmap(gnt, region->addr, region->num) != 0) {
         xen_be_printf(&blkdev->xendev, 0,
-                      "xc_gnttab_munmap region %p failed: %s\n",
+                      "xengnttab_unmap region %p failed: %s\n",
                       region->addr, strerror(errno));
     }
     xen_be_printf(&blkdev->xendev, 3,
@@ -327,7 +327,7 @@ err:
 
 static void ioreq_unmap(struct ioreq *ioreq)
 {
-    XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = ioreq->blkdev->xendev.gnttabdev;
     int i;
 
     if (ioreq->num_unmap == 0 || ioreq->mapped == 0) {
@@ -337,8 +337,9 @@ static void ioreq_unmap(struct ioreq *ioreq)
         if (!ioreq->pages) {
             return;
         }
-        if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
-            xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
+        if (xengnttab_unmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
+            xen_be_printf(&ioreq->blkdev->xendev, 0,
+                          "xengnttab_unmap failed: %s\n",
                           strerror(errno));
         }
         ioreq->blkdev->cnt_map -= ioreq->num_unmap;
@@ -348,8 +349,9 @@ static void ioreq_unmap(struct ioreq *ioreq)
             if (!ioreq->page[i]) {
                 continue;
             }
-            if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
-                xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
+            if (xengnttab_unmap(gnt, ioreq->page[i], 1) != 0) {
+                xen_be_printf(&ioreq->blkdev->xendev, 0,
+                              "xengnttab_unmap failed: %s\n",
                               strerror(errno));
             }
             ioreq->blkdev->cnt_map--;
@@ -361,7 +363,7 @@ static void ioreq_unmap(struct ioreq *ioreq)
 
 static int ioreq_map(struct ioreq *ioreq)
 {
-    XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = ioreq->blkdev->xendev.gnttabdev;
     uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
     uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
     void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
@@ -412,7 +414,7 @@ static int ioreq_map(struct ioreq *ioreq)
     }
 
     if (batch_maps && new_maps) {
-        ioreq->pages = xc_gnttab_map_grant_refs
+        ioreq->pages = xengnttab_map_grant_refs
             (gnt, new_maps, domids, refs, ioreq->prot);
         if (ioreq->pages == NULL) {
             xen_be_printf(&ioreq->blkdev->xendev, 0,
@@ -428,7 +430,7 @@ static int ioreq_map(struct ioreq *ioreq)
         ioreq->blkdev->cnt_map += new_maps;
     } else if (new_maps)  {
         for (i = 0; i < new_maps; i++) {
-            ioreq->page[i] = xc_gnttab_map_grant_ref
+            ioreq->page[i] = xengnttab_map_grant_ref
                 (gnt, domids[i], refs[i], ioreq->prot);
             if (ioreq->page[i] == NULL) {
                 xen_be_printf(&ioreq->blkdev->xendev, 0,
@@ -778,9 +780,9 @@ static void blk_alloc(struct XenDevice *xendev)
     if (xen_mode != XEN_EMULATE) {
         batch_maps = 1;
     }
-    if (xc_gnttab_set_max_grants(xendev->gnttabdev,
+    if (xengnttab_set_max_grants(xendev->gnttabdev,
             MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
-        xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
+        xen_be_printf(xendev, 0, "xengnttab_set_max_grants failed: %s\n",
                       strerror(errno));
     }
 }
@@ -990,7 +992,7 @@ static int blk_connect(struct XenDevice *xendev)
         }
     }
 
-    blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
+    blkdev->sring = xengnttab_map_grant_ref(blkdev->xendev.gnttabdev,
                                             blkdev->xendev.dom,
                                             blkdev->ring_ref,
                                             PROT_READ | PROT_WRITE);
@@ -1055,7 +1057,7 @@ static void blk_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&blkdev->xendev);
 
     if (blkdev->sring) {
-        xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
+        xengnttab_unmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
         blkdev->cnt_map--;
         blkdev->sring = NULL;
     }
diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index 63ade33..ac1b324 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -233,7 +233,7 @@ static int con_initialise(struct XenDevice *xendev)
                                           PROT_READ|PROT_WRITE,
                                           con->ring_ref);
     } else {
-        con->sring = xc_gnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
+        con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
                                              PROT_READ|PROT_WRITE);
     }
@@ -275,7 +275,7 @@ static void con_disconnect(struct XenDevice *xendev)
         if (!xendev->dev) {
             munmap(con->sring, XC_PAGE_SIZE);
         } else {
-            xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
+            xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
         }
         con->sring = NULL;
     }
diff --git a/hw/net/xen_nic.c b/hw/net/xen_nic.c
index 0da16b4..3ffb07d 100644
--- a/hw/net/xen_nic.c
+++ b/hw/net/xen_nic.c
@@ -168,7 +168,7 @@ static void net_tx_packets(struct XenNetDev *netdev)
                           (txreq.flags & NETTXF_more_data)      ? " more_data"      : "",
                           (txreq.flags & NETTXF_extra_info)     ? " extra_info"     : "");
 
-            page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+            page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                            netdev->xendev.dom,
                                            txreq.gref, PROT_READ);
             if (page == NULL) {
@@ -190,7 +190,7 @@ static void net_tx_packets(struct XenNetDev *netdev)
                 qemu_send_packet(qemu_get_queue(netdev->nic),
                                  page + txreq.offset, txreq.size);
             }
-            xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
+            xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
             net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
         }
         if (!netdev->tx_work) {
@@ -260,7 +260,7 @@ static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size
     memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
     netdev->rx_ring.req_cons = ++rc;
 
-    page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+    page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                    netdev->xendev.dom,
                                    rxreq.gref, PROT_WRITE);
     if (page == NULL) {
@@ -270,7 +270,7 @@ static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size
         return -1;
     }
     memcpy(page + NET_IP_ALIGN, buf, size);
-    xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
+    xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
     net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
 
     return size;
@@ -342,19 +342,19 @@ static int net_connect(struct XenDevice *xendev)
         return -1;
     }
 
-    netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+    netdev->txs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                           netdev->xendev.dom,
                                           netdev->tx_ring_ref,
                                           PROT_READ | PROT_WRITE);
     if (!netdev->txs) {
         return -1;
     }
-    netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+    netdev->rxs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                           netdev->xendev.dom,
                                           netdev->rx_ring_ref,
                                           PROT_READ | PROT_WRITE);
     if (!netdev->rxs) {
-        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
+        xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
         netdev->txs = NULL;
         return -1;
     }
@@ -379,11 +379,11 @@ static void net_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&netdev->xendev);
 
     if (netdev->txs) {
-        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
+        xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
         netdev->txs = NULL;
     }
     if (netdev->rxs) {
-        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
+        xengnttab_unmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
         netdev->rxs = NULL;
     }
 }
diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
index ae2a1f0..966e34f 100644
--- a/hw/xen/xen_backend.c
+++ b/hw/xen/xen_backend.c
@@ -252,15 +252,15 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
-        xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
-        if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
+        xendev->gnttabdev = xengnttab_open(NULL, 0);
+        if (xendev->gnttabdev == NULL) {
             xen_be_printf(NULL, 0, "can't open gnttab device\n");
             xenevtchn_close(xendev->evtchndev);
             g_free(xendev);
             return NULL;
         }
     } else {
-        xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
+        xendev->gnttabdev = NULL;
     }
 
     QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
@@ -309,8 +309,8 @@ static struct XenDevice *xen_be_del_xendev(int dom, int dev)
         if (xendev->evtchndev != NULL) {
             xenevtchn_close(xendev->evtchndev);
         }
-        if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
-            xc_gnttab_close(xendev->gnttabdev);
+        if (xendev->gnttabdev != NULL) {
+            xengnttab_close(xendev->gnttabdev);
         }
 
         QTAILQ_REMOVE(&xendevs, xendev, next);
diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
index a90314f..8e8857b 100644
--- a/include/hw/xen/xen_backend.h
+++ b/include/hw/xen/xen_backend.h
@@ -47,7 +47,7 @@ struct XenDevice {
     int                local_port;
 
     xenevtchn_handle   *evtchndev;
-    XenGnttab          gnttabdev;
+    xengnttab_handle   *gnttabdev;
 
     struct XenDevOps   *ops;
     QTAILQ_ENTRY(XenDevice) next;
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 5c51b44..8f38310 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -40,7 +40,7 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
 
 typedef int XenXC;
 typedef int xenevtchn_handle;
-typedef int XenGnttab;
+typedef int xengnttab_handle;
 
 #  define XC_INTERFACE_FMT "%i"
 #  define XC_HANDLER_INITIAL_VALUE    -1
@@ -72,11 +72,31 @@ static inline int xenevtchn_close(xenevtchn_handle *h)
 #define xenevtchn_unmask(h, p) xc_evtchn_unmask(*h, p)
 #define xenevtchn_unbind(h, p) xc_evtchn_unmask(*h, p)
 
-static inline XenGnttab xen_xc_gnttab_open(void *logger,
-                                           unsigned int open_flags)
+static inline xengnttab_handle *xengnttab_open(void *logger,
+                                               unsigned int open_flags)
 {
-    return xc_gnttab_open();
+    xengnttab_handle *h = malloc(sizeof(*h));
+    if (!h) {
+        return NULL;
+    }
+    *h = xc_gnttab_open();
+    if (*h == -1) {
+        free(h);
+        h = NULL;
+    }
+    return h;
 }
+static inline int xengnttab_close(xengnttab_handle *h)
+{
+    int rc = xc_gnttab_close(*h);
+    free(h);
+    return rc;
+}
+#define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(*h, n)
+#define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(*h, d, r, p)
+#define xengnttab_map_grant_refs(h, c, d, r, p) \
+    xc_gnttab_map_grant_refs(*h, c, d, r, p)
+#define xengnttab_unmap(h, a, n) xc_gnttab_munmap(*h, a, n)
 
 static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
                                           unsigned int open_flags)
@@ -130,7 +150,7 @@ static inline void xs_close(struct xs_handle *xsh)
 
 typedef xc_interface *XenXC;
 typedef xc_evtchn xenevtchn_handle;
-typedef xc_gnttab *XenGnttab;
+typedef xc_gnttab xengnttab_handle;
 
 #  define XC_INTERFACE_FMT "%p"
 #  define XC_HANDLER_INITIAL_VALUE    NULL
@@ -144,11 +164,13 @@ typedef xc_gnttab *XenGnttab;
 #define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p)
 #define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p)
 
-static inline XenGnttab xen_xc_gnttab_open(void *logger,
-                                           unsigned int open_flags)
-{
-    return xc_gnttab_open(logger, open_flags);
-}
+#define xengnttab_open(l, f) xc_gnttab_open(l, f)
+#define xengnttab_close(h) xc_gnttab_close(h)
+#define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(h, n)
+#define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(h, d, r, p)
+#define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n)
+#define xengnttab_map_grant_refs(h, c, d, r, p) \
+    xc_gnttab_map_grant_refs(h, c, d, r, p)
 
 static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
                                           unsigned int open_flags)
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 3/8] xen: Switch to libxengnttab interface for compat shims.
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxengnttab which provides access to grant
tables.

In preparation for this switch the compatibility layer in xen_common.h
(which support building with older versions of Xen) to use what will
be the new library API. This means that the gnttab shim will disappear
for versions of Xen which include libxengnttab.

To simplify things for the <= 4.0.0 support we wrap the int fd in a
malloc(sizeof int) such that the handle is always a pointer. This
leads to less typedef headaches and the need for
XC_HANDLER_INITIAL_VALUE etc for these interfaces.

Note that this patch does not add any support for actually using
libxengnttab, it just adjusts the existing shims.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v4: Ran checkpatch, fixed all errors
    Allocate correct size for handle (i.e. not size of the ptr)
    Rebase onto "xen_console: correctly cleanup primary console on
    teardown."

v5: Rebase over b4f72e31b924 "hw/net/xen_nic.c: Free 'netdev->txs'
when map 'netdev->rxs' fails" which added a new gnttab_munmap.

v7: s/gnttab_munmap/gnttab_unmap/ to reflect change in the library.
---
 hw/block/xen_disk.c          | 38 ++++++++++++++++++++------------------
 hw/char/xen_console.c        |  4 ++--
 hw/net/xen_nic.c             | 18 +++++++++---------
 hw/xen/xen_backend.c         | 10 +++++-----
 include/hw/xen/xen_backend.h |  2 +-
 include/hw/xen/xen_common.h  | 42 ++++++++++++++++++++++++++++++++----------
 6 files changed, 69 insertions(+), 45 deletions(-)

diff --git a/hw/block/xen_disk.c b/hw/block/xen_disk.c
index a48e726..326c948 100644
--- a/hw/block/xen_disk.c
+++ b/hw/block/xen_disk.c
@@ -171,11 +171,11 @@ static gint int_cmp(gconstpointer a, gconstpointer b, gpointer user_data)
 static void destroy_grant(gpointer pgnt)
 {
     PersistentGrant *grant = pgnt;
-    XenGnttab gnt = grant->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = grant->blkdev->xendev.gnttabdev;
 
-    if (xc_gnttab_munmap(gnt, grant->page, 1) != 0) {
+    if (xengnttab_unmap(gnt, grant->page, 1) != 0) {
         xen_be_printf(&grant->blkdev->xendev, 0,
-                      "xc_gnttab_munmap failed: %s\n",
+                      "xengnttab_unmap failed: %s\n",
                       strerror(errno));
     }
     grant->blkdev->persistent_gnt_count--;
@@ -188,11 +188,11 @@ static void remove_persistent_region(gpointer data, gpointer dev)
 {
     PersistentRegion *region = data;
     struct XenBlkDev *blkdev = dev;
-    XenGnttab gnt = blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = blkdev->xendev.gnttabdev;
 
-    if (xc_gnttab_munmap(gnt, region->addr, region->num) != 0) {
+    if (xengnttab_unmap(gnt, region->addr, region->num) != 0) {
         xen_be_printf(&blkdev->xendev, 0,
-                      "xc_gnttab_munmap region %p failed: %s\n",
+                      "xengnttab_unmap region %p failed: %s\n",
                       region->addr, strerror(errno));
     }
     xen_be_printf(&blkdev->xendev, 3,
@@ -327,7 +327,7 @@ err:
 
 static void ioreq_unmap(struct ioreq *ioreq)
 {
-    XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = ioreq->blkdev->xendev.gnttabdev;
     int i;
 
     if (ioreq->num_unmap == 0 || ioreq->mapped == 0) {
@@ -337,8 +337,9 @@ static void ioreq_unmap(struct ioreq *ioreq)
         if (!ioreq->pages) {
             return;
         }
-        if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
-            xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
+        if (xengnttab_unmap(gnt, ioreq->pages, ioreq->num_unmap) != 0) {
+            xen_be_printf(&ioreq->blkdev->xendev, 0,
+                          "xengnttab_unmap failed: %s\n",
                           strerror(errno));
         }
         ioreq->blkdev->cnt_map -= ioreq->num_unmap;
@@ -348,8 +349,9 @@ static void ioreq_unmap(struct ioreq *ioreq)
             if (!ioreq->page[i]) {
                 continue;
             }
-            if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0) {
-                xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
+            if (xengnttab_unmap(gnt, ioreq->page[i], 1) != 0) {
+                xen_be_printf(&ioreq->blkdev->xendev, 0,
+                              "xengnttab_unmap failed: %s\n",
                               strerror(errno));
             }
             ioreq->blkdev->cnt_map--;
@@ -361,7 +363,7 @@ static void ioreq_unmap(struct ioreq *ioreq)
 
 static int ioreq_map(struct ioreq *ioreq)
 {
-    XenGnttab gnt = ioreq->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = ioreq->blkdev->xendev.gnttabdev;
     uint32_t domids[BLKIF_MAX_SEGMENTS_PER_REQUEST];
     uint32_t refs[BLKIF_MAX_SEGMENTS_PER_REQUEST];
     void *page[BLKIF_MAX_SEGMENTS_PER_REQUEST];
@@ -412,7 +414,7 @@ static int ioreq_map(struct ioreq *ioreq)
     }
 
     if (batch_maps && new_maps) {
-        ioreq->pages = xc_gnttab_map_grant_refs
+        ioreq->pages = xengnttab_map_grant_refs
             (gnt, new_maps, domids, refs, ioreq->prot);
         if (ioreq->pages == NULL) {
             xen_be_printf(&ioreq->blkdev->xendev, 0,
@@ -428,7 +430,7 @@ static int ioreq_map(struct ioreq *ioreq)
         ioreq->blkdev->cnt_map += new_maps;
     } else if (new_maps)  {
         for (i = 0; i < new_maps; i++) {
-            ioreq->page[i] = xc_gnttab_map_grant_ref
+            ioreq->page[i] = xengnttab_map_grant_ref
                 (gnt, domids[i], refs[i], ioreq->prot);
             if (ioreq->page[i] == NULL) {
                 xen_be_printf(&ioreq->blkdev->xendev, 0,
@@ -778,9 +780,9 @@ static void blk_alloc(struct XenDevice *xendev)
     if (xen_mode != XEN_EMULATE) {
         batch_maps = 1;
     }
-    if (xc_gnttab_set_max_grants(xendev->gnttabdev,
+    if (xengnttab_set_max_grants(xendev->gnttabdev,
             MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
-        xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
+        xen_be_printf(xendev, 0, "xengnttab_set_max_grants failed: %s\n",
                       strerror(errno));
     }
 }
@@ -990,7 +992,7 @@ static int blk_connect(struct XenDevice *xendev)
         }
     }
 
-    blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
+    blkdev->sring = xengnttab_map_grant_ref(blkdev->xendev.gnttabdev,
                                             blkdev->xendev.dom,
                                             blkdev->ring_ref,
                                             PROT_READ | PROT_WRITE);
@@ -1055,7 +1057,7 @@ static void blk_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&blkdev->xendev);
 
     if (blkdev->sring) {
-        xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
+        xengnttab_unmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
         blkdev->cnt_map--;
         blkdev->sring = NULL;
     }
diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index 63ade33..ac1b324 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -233,7 +233,7 @@ static int con_initialise(struct XenDevice *xendev)
                                           PROT_READ|PROT_WRITE,
                                           con->ring_ref);
     } else {
-        con->sring = xc_gnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
+        con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
                                              PROT_READ|PROT_WRITE);
     }
@@ -275,7 +275,7 @@ static void con_disconnect(struct XenDevice *xendev)
         if (!xendev->dev) {
             munmap(con->sring, XC_PAGE_SIZE);
         } else {
-            xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
+            xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
         }
         con->sring = NULL;
     }
diff --git a/hw/net/xen_nic.c b/hw/net/xen_nic.c
index 0da16b4..3ffb07d 100644
--- a/hw/net/xen_nic.c
+++ b/hw/net/xen_nic.c
@@ -168,7 +168,7 @@ static void net_tx_packets(struct XenNetDev *netdev)
                           (txreq.flags & NETTXF_more_data)      ? " more_data"      : "",
                           (txreq.flags & NETTXF_extra_info)     ? " extra_info"     : "");
 
-            page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+            page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                            netdev->xendev.dom,
                                            txreq.gref, PROT_READ);
             if (page == NULL) {
@@ -190,7 +190,7 @@ static void net_tx_packets(struct XenNetDev *netdev)
                 qemu_send_packet(qemu_get_queue(netdev->nic),
                                  page + txreq.offset, txreq.size);
             }
-            xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
+            xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
             net_tx_response(netdev, &txreq, NETIF_RSP_OKAY);
         }
         if (!netdev->tx_work) {
@@ -260,7 +260,7 @@ static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size
     memcpy(&rxreq, RING_GET_REQUEST(&netdev->rx_ring, rc), sizeof(rxreq));
     netdev->rx_ring.req_cons = ++rc;
 
-    page = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+    page = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                    netdev->xendev.dom,
                                    rxreq.gref, PROT_WRITE);
     if (page == NULL) {
@@ -270,7 +270,7 @@ static ssize_t net_rx_packet(NetClientState *nc, const uint8_t *buf, size_t size
         return -1;
     }
     memcpy(page + NET_IP_ALIGN, buf, size);
-    xc_gnttab_munmap(netdev->xendev.gnttabdev, page, 1);
+    xengnttab_unmap(netdev->xendev.gnttabdev, page, 1);
     net_rx_response(netdev, &rxreq, NETIF_RSP_OKAY, NET_IP_ALIGN, size, 0);
 
     return size;
@@ -342,19 +342,19 @@ static int net_connect(struct XenDevice *xendev)
         return -1;
     }
 
-    netdev->txs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+    netdev->txs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                           netdev->xendev.dom,
                                           netdev->tx_ring_ref,
                                           PROT_READ | PROT_WRITE);
     if (!netdev->txs) {
         return -1;
     }
-    netdev->rxs = xc_gnttab_map_grant_ref(netdev->xendev.gnttabdev,
+    netdev->rxs = xengnttab_map_grant_ref(netdev->xendev.gnttabdev,
                                           netdev->xendev.dom,
                                           netdev->rx_ring_ref,
                                           PROT_READ | PROT_WRITE);
     if (!netdev->rxs) {
-        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
+        xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
         netdev->txs = NULL;
         return -1;
     }
@@ -379,11 +379,11 @@ static void net_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&netdev->xendev);
 
     if (netdev->txs) {
-        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->txs, 1);
+        xengnttab_unmap(netdev->xendev.gnttabdev, netdev->txs, 1);
         netdev->txs = NULL;
     }
     if (netdev->rxs) {
-        xc_gnttab_munmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
+        xengnttab_unmap(netdev->xendev.gnttabdev, netdev->rxs, 1);
         netdev->rxs = NULL;
     }
 }
diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
index ae2a1f0..966e34f 100644
--- a/hw/xen/xen_backend.c
+++ b/hw/xen/xen_backend.c
@@ -252,15 +252,15 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
-        xendev->gnttabdev = xen_xc_gnttab_open(NULL, 0);
-        if (xendev->gnttabdev == XC_HANDLER_INITIAL_VALUE) {
+        xendev->gnttabdev = xengnttab_open(NULL, 0);
+        if (xendev->gnttabdev == NULL) {
             xen_be_printf(NULL, 0, "can't open gnttab device\n");
             xenevtchn_close(xendev->evtchndev);
             g_free(xendev);
             return NULL;
         }
     } else {
-        xendev->gnttabdev = XC_HANDLER_INITIAL_VALUE;
+        xendev->gnttabdev = NULL;
     }
 
     QTAILQ_INSERT_TAIL(&xendevs, xendev, next);
@@ -309,8 +309,8 @@ static struct XenDevice *xen_be_del_xendev(int dom, int dev)
         if (xendev->evtchndev != NULL) {
             xenevtchn_close(xendev->evtchndev);
         }
-        if (xendev->gnttabdev != XC_HANDLER_INITIAL_VALUE) {
-            xc_gnttab_close(xendev->gnttabdev);
+        if (xendev->gnttabdev != NULL) {
+            xengnttab_close(xendev->gnttabdev);
         }
 
         QTAILQ_REMOVE(&xendevs, xendev, next);
diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
index a90314f..8e8857b 100644
--- a/include/hw/xen/xen_backend.h
+++ b/include/hw/xen/xen_backend.h
@@ -47,7 +47,7 @@ struct XenDevice {
     int                local_port;
 
     xenevtchn_handle   *evtchndev;
-    XenGnttab          gnttabdev;
+    xengnttab_handle   *gnttabdev;
 
     struct XenDevOps   *ops;
     QTAILQ_ENTRY(XenDevice) next;
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 5c51b44..8f38310 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -40,7 +40,7 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
 
 typedef int XenXC;
 typedef int xenevtchn_handle;
-typedef int XenGnttab;
+typedef int xengnttab_handle;
 
 #  define XC_INTERFACE_FMT "%i"
 #  define XC_HANDLER_INITIAL_VALUE    -1
@@ -72,11 +72,31 @@ static inline int xenevtchn_close(xenevtchn_handle *h)
 #define xenevtchn_unmask(h, p) xc_evtchn_unmask(*h, p)
 #define xenevtchn_unbind(h, p) xc_evtchn_unmask(*h, p)
 
-static inline XenGnttab xen_xc_gnttab_open(void *logger,
-                                           unsigned int open_flags)
+static inline xengnttab_handle *xengnttab_open(void *logger,
+                                               unsigned int open_flags)
 {
-    return xc_gnttab_open();
+    xengnttab_handle *h = malloc(sizeof(*h));
+    if (!h) {
+        return NULL;
+    }
+    *h = xc_gnttab_open();
+    if (*h == -1) {
+        free(h);
+        h = NULL;
+    }
+    return h;
 }
+static inline int xengnttab_close(xengnttab_handle *h)
+{
+    int rc = xc_gnttab_close(*h);
+    free(h);
+    return rc;
+}
+#define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(*h, n)
+#define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(*h, d, r, p)
+#define xengnttab_map_grant_refs(h, c, d, r, p) \
+    xc_gnttab_map_grant_refs(*h, c, d, r, p)
+#define xengnttab_unmap(h, a, n) xc_gnttab_munmap(*h, a, n)
 
 static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
                                           unsigned int open_flags)
@@ -130,7 +150,7 @@ static inline void xs_close(struct xs_handle *xsh)
 
 typedef xc_interface *XenXC;
 typedef xc_evtchn xenevtchn_handle;
-typedef xc_gnttab *XenGnttab;
+typedef xc_gnttab xengnttab_handle;
 
 #  define XC_INTERFACE_FMT "%p"
 #  define XC_HANDLER_INITIAL_VALUE    NULL
@@ -144,11 +164,13 @@ typedef xc_gnttab *XenGnttab;
 #define xenevtchn_unmask(h, p) xc_evtchn_unmask(h, p)
 #define xenevtchn_unbind(h, p) xc_evtchn_unbind(h, p)
 
-static inline XenGnttab xen_xc_gnttab_open(void *logger,
-                                           unsigned int open_flags)
-{
-    return xc_gnttab_open(logger, open_flags);
-}
+#define xengnttab_open(l, f) xc_gnttab_open(l, f)
+#define xengnttab_close(h) xc_gnttab_close(h)
+#define xengnttab_set_max_grants(h, n) xc_gnttab_set_max_grants(h, n)
+#define xengnttab_map_grant_ref(h, d, r, p) xc_gnttab_map_grant_ref(h, d, r, p)
+#define xengnttab_unmap(h, a, n) xc_gnttab_munmap(h, a, n)
+#define xengnttab_map_grant_refs(h, c, d, r, p) \
+    xc_gnttab_map_grant_refs(h, c, d, r, p)
 
 static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
                                           unsigned int open_flags)
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxenforeignmemory which provides access to
privileged foreign mappings and which will provide an interface
equivalent to xc_map_foreign_{pages,bulk}.

In preparation for this switch all uses of xc_map_foreign_range to
xc_map_foreign_pages. This is trivial because size was always
XC_PAGE_SIZE so the necessary adjustments are trivial:

  * Pass &mfn (an array of length 1) instead of mfn. The function
    takes a pointer to const, so there is no possibily of mfn changing
    due to this change.
  * Pass nr_pages=1 instead of size=XC_PAGE_SIZE

There is one wrinkle in xen_console.c:con_initialise() where
con->ring_ref is an int but can in some code paths (when !xendev->dev)
be treated as an mfn. I think this is an existing latent truncation
hazard on platforms where xen_pfn_t is 64-bit and int is 32-bit (e.g.
amd64, both arm* variants). I'm unsure under what circumstances
xendev->dev can be NULL or if anything elsewhere ensures the value
fits into an int. For now I just use a temporary xen_pfn_t to in
effect upcast the pointer from int* to xen_pfn_t*.

In xenfb.c:common_bind we now explicitly launder the mfn into a
xen_pfn_t, so it has the correct type to be passed to
xc_map_foreign_pages and doesn't provoke warnings on 32-bit x86.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v7: Correct type mismatch (int64 vs xen_pfn_t) on 32 bit x86 in
    xenfb.c, by explicitly laundering via a xen_pfn_t variable.

v6: Switch to xc_map_foreign_pages rather than _bulk. Switching to
    _bulk without checking the value of err[0] risked missing errors.
    The new xenforeignmemory_map coming later in this series will
    DTRT with err==NULL.

    Dropped Stefano's Reviewed-by due to this change.

v4: Ran checkpatch, fixed all errors
    Mention the const-ness of the mfn array in the commit log

fixup!xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
---
 hw/char/xen_console.c |  8 ++++----
 hw/display/xenfb.c    | 15 ++++++++-------
 xen-hvm.c             | 14 +++++++-------
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index ac1b324..3e8a57b 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -228,10 +228,10 @@ static int con_initialise(struct XenDevice *xendev)
 	con->buffer.max_capacity = limit;
 
     if (!xendev->dev) {
-        con->sring = xc_map_foreign_range(xen_xc, con->xendev.dom,
-                                          XC_PAGE_SIZE,
-                                          PROT_READ|PROT_WRITE,
-                                          con->ring_ref);
+        xen_pfn_t mfn = con->ring_ref;
+        con->sring = xc_map_foreign_pages(xen_xc, con->xendev.dom,
+                                         PROT_READ|PROT_WRITE,
+                                         &mfn, 1);
     } else {
         con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
index 4e2a27a..bb8f6b3 100644
--- a/hw/display/xenfb.c
+++ b/hw/display/xenfb.c
@@ -95,23 +95,24 @@ struct XenFB {
 
 static int common_bind(struct common *c)
 {
-    uint64_t mfn;
+    uint64_t val;
+    xen_pfn_t mfn;
 
-    if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn) == -1)
+    if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val) == -1)
 	return -1;
-    assert(mfn == (xen_pfn_t)mfn);
+    mfn = (xen_pfn_t)val;
+    assert(val == mfn);
 
     if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
 	return -1;
 
-    c->page = xc_map_foreign_range(xen_xc, c->xendev.dom,
-				   XC_PAGE_SIZE,
-				   PROT_READ | PROT_WRITE, mfn);
+    c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom,
+                                   PROT_READ | PROT_WRITE, &mfn, 1);
     if (c->page == NULL)
 	return -1;
 
     xen_be_bind_evtchn(&c->xendev);
-    xen_be_printf(&c->xendev, 1, "ring mfn %"PRIx64", remote-port %d, local-port %d\n",
+    xen_be_printf(&c->xendev, 1, "ring mfn %"PRI_xen_pfn", remote-port %d, local-port %d\n",
 		  mfn, c->xendev.remote_port, c->xendev.local_port);
 
     return 0;
diff --git a/xen-hvm.c b/xen-hvm.c
index 6227e17..1f729f6 100644
--- a/xen-hvm.c
+++ b/xen-hvm.c
@@ -1247,8 +1247,9 @@ int xen_hvm_init(PCMachineState *pcms,
     DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
 
-    state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
-                                              PROT_READ|PROT_WRITE, ioreq_pfn);
+    state->shared_page = xc_map_foreign_pages(xen_xc, xen_domid,
+                                              PROT_READ|PROT_WRITE,
+                                              &ioreq_pfn, 1);
     if (state->shared_page == NULL) {
         hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
                  errno, xen_xc);
@@ -1258,8 +1259,8 @@ int xen_hvm_init(PCMachineState *pcms,
     if (!rc) {
         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
         state->shared_vmport_page =
-            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
-                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+            xc_map_foreign_pages(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
+                                 &ioreq_pfn, 1);
         if (state->shared_vmport_page == NULL) {
             hw_error("map shared vmport IO page returned error %d handle="
                      XC_INTERFACE_FMT, errno, xen_xc);
@@ -1268,10 +1269,9 @@ int xen_hvm_init(PCMachineState *pcms,
         hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
     }
 
-    state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid,
-                                                   XC_PAGE_SIZE,
+    state->buffered_io_page = xc_map_foreign_pages(xen_xc, xen_domid,
                                                    PROT_READ|PROT_WRITE,
-                                                   bufioreq_pfn);
+                                                   &bufioreq_pfn, 1);
     if (state->buffered_io_page == NULL) {
         hw_error("map buffered IO page returned error %d", errno);
     }
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxenforeignmemory which provides access to
privileged foreign mappings and which will provide an interface
equivalent to xc_map_foreign_{pages,bulk}.

In preparation for this switch all uses of xc_map_foreign_range to
xc_map_foreign_pages. This is trivial because size was always
XC_PAGE_SIZE so the necessary adjustments are trivial:

  * Pass &mfn (an array of length 1) instead of mfn. The function
    takes a pointer to const, so there is no possibily of mfn changing
    due to this change.
  * Pass nr_pages=1 instead of size=XC_PAGE_SIZE

There is one wrinkle in xen_console.c:con_initialise() where
con->ring_ref is an int but can in some code paths (when !xendev->dev)
be treated as an mfn. I think this is an existing latent truncation
hazard on platforms where xen_pfn_t is 64-bit and int is 32-bit (e.g.
amd64, both arm* variants). I'm unsure under what circumstances
xendev->dev can be NULL or if anything elsewhere ensures the value
fits into an int. For now I just use a temporary xen_pfn_t to in
effect upcast the pointer from int* to xen_pfn_t*.

In xenfb.c:common_bind we now explicitly launder the mfn into a
xen_pfn_t, so it has the correct type to be passed to
xc_map_foreign_pages and doesn't provoke warnings on 32-bit x86.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v7: Correct type mismatch (int64 vs xen_pfn_t) on 32 bit x86 in
    xenfb.c, by explicitly laundering via a xen_pfn_t variable.

v6: Switch to xc_map_foreign_pages rather than _bulk. Switching to
    _bulk without checking the value of err[0] risked missing errors.
    The new xenforeignmemory_map coming later in this series will
    DTRT with err==NULL.

    Dropped Stefano's Reviewed-by due to this change.

v4: Ran checkpatch, fixed all errors
    Mention the const-ness of the mfn array in the commit log

fixup!xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
---
 hw/char/xen_console.c |  8 ++++----
 hw/display/xenfb.c    | 15 ++++++++-------
 xen-hvm.c             | 14 +++++++-------
 3 files changed, 19 insertions(+), 18 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index ac1b324..3e8a57b 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -228,10 +228,10 @@ static int con_initialise(struct XenDevice *xendev)
 	con->buffer.max_capacity = limit;
 
     if (!xendev->dev) {
-        con->sring = xc_map_foreign_range(xen_xc, con->xendev.dom,
-                                          XC_PAGE_SIZE,
-                                          PROT_READ|PROT_WRITE,
-                                          con->ring_ref);
+        xen_pfn_t mfn = con->ring_ref;
+        con->sring = xc_map_foreign_pages(xen_xc, con->xendev.dom,
+                                         PROT_READ|PROT_WRITE,
+                                         &mfn, 1);
     } else {
         con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
index 4e2a27a..bb8f6b3 100644
--- a/hw/display/xenfb.c
+++ b/hw/display/xenfb.c
@@ -95,23 +95,24 @@ struct XenFB {
 
 static int common_bind(struct common *c)
 {
-    uint64_t mfn;
+    uint64_t val;
+    xen_pfn_t mfn;
 
-    if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &mfn) == -1)
+    if (xenstore_read_fe_uint64(&c->xendev, "page-ref", &val) == -1)
 	return -1;
-    assert(mfn == (xen_pfn_t)mfn);
+    mfn = (xen_pfn_t)val;
+    assert(val == mfn);
 
     if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
 	return -1;
 
-    c->page = xc_map_foreign_range(xen_xc, c->xendev.dom,
-				   XC_PAGE_SIZE,
-				   PROT_READ | PROT_WRITE, mfn);
+    c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom,
+                                   PROT_READ | PROT_WRITE, &mfn, 1);
     if (c->page == NULL)
 	return -1;
 
     xen_be_bind_evtchn(&c->xendev);
-    xen_be_printf(&c->xendev, 1, "ring mfn %"PRIx64", remote-port %d, local-port %d\n",
+    xen_be_printf(&c->xendev, 1, "ring mfn %"PRI_xen_pfn", remote-port %d, local-port %d\n",
 		  mfn, c->xendev.remote_port, c->xendev.local_port);
 
     return 0;
diff --git a/xen-hvm.c b/xen-hvm.c
index 6227e17..1f729f6 100644
--- a/xen-hvm.c
+++ b/xen-hvm.c
@@ -1247,8 +1247,9 @@ int xen_hvm_init(PCMachineState *pcms,
     DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
 
-    state->shared_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
-                                              PROT_READ|PROT_WRITE, ioreq_pfn);
+    state->shared_page = xc_map_foreign_pages(xen_xc, xen_domid,
+                                              PROT_READ|PROT_WRITE,
+                                              &ioreq_pfn, 1);
     if (state->shared_page == NULL) {
         hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
                  errno, xen_xc);
@@ -1258,8 +1259,8 @@ int xen_hvm_init(PCMachineState *pcms,
     if (!rc) {
         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
         state->shared_vmport_page =
-            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
-                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+            xc_map_foreign_pages(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
+                                 &ioreq_pfn, 1);
         if (state->shared_vmport_page == NULL) {
             hw_error("map shared vmport IO page returned error %d handle="
                      XC_INTERFACE_FMT, errno, xen_xc);
@@ -1268,10 +1269,9 @@ int xen_hvm_init(PCMachineState *pcms,
         hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
     }
 
-    state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid,
-                                                   XC_PAGE_SIZE,
+    state->buffered_io_page = xc_map_foreign_pages(xen_xc, xen_domid,
                                                    PROT_READ|PROT_WRITE,
-                                                   bufioreq_pfn);
+                                                   &bufioreq_pfn, 1);
     if (state->buffered_io_page == NULL) {
         hw_error("map buffered IO page returned error %d", errno);
     }
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 5/8] xen: Switch uses of xc_map_foreign_{pages, bulk} to use libxenforeignmemory API.
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxenforeignmemory which provides access to
privileged foreign mappings and which will provide an interface
equivalent to xc_map_foreign_{pages,bulk}.

The new xenforeignmemory_map() function behaves like
xc_map_foreign_pages() when the err argument is NULL and like
xc_map_foreign_bulk() when err is non-NULL, which maps into the shim
here onto checking err == NULL and calling the appropriate old
function.

Note that xenforeignmemory_map() takes the number of pages before the
arrays themselves, in order to support potentially future use of
variable-length-arrays in the prototype (in the future, when Xen's
baseline toolchain requirements are new enough to ensure VLAs are
supported).

In preparation for adding support for libxenforeignmemory add support
to the <=4.0 and <=4.6 compat code in xen_common.h to allow us to
switch to using the new API. These shims will disappear for versions
of Xen which include libxenforeignmemory.

Since libxenforeignmemory will have its own handle type but for <= 4.6
the functionality is provided by using a libxenctrl handle we
introduce a new global xen_fmem alongside the existing xen_xc. In fact
we make xen_fmem a pointer to the existing xen_xc, which then works
correctly with both <=4.0 (xc handle is an int) and <=4.6 (xc handle
is a pointer). In the latter case xen_fmem is actually a double
indirect pointer, but it all falls out in the wash.

Unlike libxenctrl libxenforeignmemory has an explicit unmap function,
rather than just specifying that munmap should be used, so the unmap
paths are updated to use xenforeignmemory_unmap, which is a shim for
munmap on these versions of xen. The mappings in xen-hvm.c do not
appear to be unmapped (which makes sense for a qemu-dm process)

In fb_disconnect this results in a change from simply mmap over the
existing mapping (with an implicit munmap) to expliclty unmapping with
xenforeignmemory_unmap and then mapping the required anonymous memory
in the same hole. I don't think this is a problem since any other
thread which was racily touching this region would already be running
the risk of hitting the mapping halfway through the call. If this is
thought to be a problem then we could consider adding an extra API to
the libxenforeignmemory interface to replace a foreign mapping with
anonymous shared memory, but I'd prefer not to.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v8: Move two copies of compat xenforeignmemory_{open,unmap} to the
    common location. Guard with version 471 (which will be added in
    the next patch)
v7: Move two copies of compat xenforeignmemory_map to a common location.
    Note that the existing xen_domain_create #ifdef will be covered by
    a CONFIG_XEN_PV_DOMAIN_BUILD in a later patch, and so is not a
    suitable place to put this function.
v6: Handle _pages as well as _bulk, due to changes in the previous
    patches, including the dropping of "xen: Switch uses of
    xc_map_foreign_pages into xc_map_foreign_bulk" which previous preceded
    this patch and the change of "xen: Switch uses of
    xc_map_foreign_range into xc_map_foreign_bulk" into "xen: Switch
    uses of xc_map_foreign_range into xc_map_foreign_pages".

    Handle reordering of arguments to xenforeignmemory_map().

    Dropped Stefano's Reviewed-by due to these changes.

v4: Rebase onto "xen_console: correctly cleanup primary console on
    teardown."

    xenforeignmemory_unmap takes pages not bytes

    Compat wrapper for xenforeignmemory_open instead of ifdef in code.

    Run check patch and fix most issues. I did not fix:

ERROR: do not initialise globals to 0 or NULL
+xenforeignmemory_handle *xen_fmem = NULL;

=> This is consistent with all of the existing declarations.

ERROR: need consistent spacing around '*' (ctx:WxV)
+typedef xc_interface *xenforeignmemory_handle;

=> I think this is a false +ve since this is a pointer "*" not a multiple "*".

reorder args to xenforeignmemory_map
---
 hw/char/xen_console.c        |  8 ++++----
 hw/display/xenfb.c           | 17 +++++++++--------
 hw/xen/xen_backend.c         |  3 ++-
 include/hw/xen/xen_backend.h |  1 +
 include/hw/xen/xen_common.h  | 25 +++++++++++++++++++++++++
 xen-common.c                 |  6 ++++++
 xen-hvm.c                    | 12 ++++++------
 xen-mapcache.c               |  6 +++---
 8 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index 3e8a57b..b92d0c6 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -229,9 +229,9 @@ static int con_initialise(struct XenDevice *xendev)
 
     if (!xendev->dev) {
         xen_pfn_t mfn = con->ring_ref;
-        con->sring = xc_map_foreign_pages(xen_xc, con->xendev.dom,
-                                         PROT_READ|PROT_WRITE,
-                                         &mfn, 1);
+        con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
+                                          PROT_READ|PROT_WRITE,
+                                          1, &mfn, NULL);
     } else {
         con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
@@ -273,7 +273,7 @@ static void con_disconnect(struct XenDevice *xendev)
 
     if (con->sring) {
         if (!xendev->dev) {
-            munmap(con->sring, XC_PAGE_SIZE);
+            xenforeignmemory_unmap(xen_fmem, con->sring, 1);
         } else {
             xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
         }
diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
index bb8f6b3..a42971c 100644
--- a/hw/display/xenfb.c
+++ b/hw/display/xenfb.c
@@ -106,8 +106,8 @@ static int common_bind(struct common *c)
     if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
 	return -1;
 
-    c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom,
-                                   PROT_READ | PROT_WRITE, &mfn, 1);
+    c->page = xenforeignmemory_map(xen_fmem, c->xendev.dom,
+                                   PROT_READ | PROT_WRITE, 1, &mfn, NULL);
     if (c->page == NULL)
 	return -1;
 
@@ -122,7 +122,7 @@ static void common_unbind(struct common *c)
 {
     xen_be_unbind_evtchn(&c->xendev);
     if (c->page) {
-	munmap(c->page, XC_PAGE_SIZE);
+        xenforeignmemory_unmap(xen_fmem, c->page, 1);
 	c->page = NULL;
     }
 }
@@ -495,15 +495,15 @@ static int xenfb_map_fb(struct XenFB *xenfb)
     fbmfns = g_malloc0(sizeof(xen_pfn_t) * xenfb->fbpages);
 
     xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
-    map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
-			       PROT_READ, pgmfns, n_fbdirs);
+    map = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
+                               PROT_READ, n_fbdirs, pgmfns, NULL);
     if (map == NULL)
 	goto out;
     xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
-    munmap(map, n_fbdirs * XC_PAGE_SIZE);
+    xenforeignmemory_unmap(xen_fmem, map, n_fbdirs);
 
-    xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
-            PROT_READ, fbmfns, xenfb->fbpages);
+    xenfb->pixels = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
+            PROT_READ, xenfb->fbpages, fbmfns, NULL);
     if (xenfb->pixels == NULL)
 	goto out;
 
@@ -912,6 +912,7 @@ static void fb_disconnect(struct XenDevice *xendev)
      *   Replacing the framebuffer with anonymous shared memory
      *   instead.  This releases the guest pages and keeps qemu happy.
      */
+    xenforeignmemory_unmap(xen_fmem, fb->pixels, fb->fbpages);
     fb->pixels = mmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE,
                       PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
                       -1, 0);
diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
index 966e34f..caa459c 100644
--- a/hw/xen/xen_backend.c
+++ b/hw/xen/xen_backend.c
@@ -45,6 +45,7 @@
 
 /* public */
 XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
+xenforeignmemory_handle *xen_fmem = NULL;
 struct xs_handle *xenstore = NULL;
 const char *xen_protocol;
 
@@ -717,7 +718,7 @@ int xen_be_init(void)
 
     qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
 
-    if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
+    if (xen_xc == XC_HANDLER_INITIAL_VALUE || xen_fmem == NULL) {
         /* Check if xen_init() have been called */
         goto err;
     }
diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
index 8e8857b..e0d52ee 100644
--- a/include/hw/xen/xen_backend.h
+++ b/include/hw/xen/xen_backend.h
@@ -57,6 +57,7 @@ struct XenDevice {
 
 /* variables */
 extern XenXC xen_xc;
+extern xenforeignmemory_handle *xen_fmem;
 extern struct xs_handle *xenstore;
 extern const char *xen_protocol;
 
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 8f38310..95275b3 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -41,6 +41,7 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
 typedef int XenXC;
 typedef int xenevtchn_handle;
 typedef int xengnttab_handle;
+typedef int xenforeignmemory_handle;
 
 #  define XC_INTERFACE_FMT "%i"
 #  define XC_HANDLER_INITIAL_VALUE    -1
@@ -104,6 +105,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open();
 }
 
+/* See below for xenforeignmemory_* APIs */
+
 static inline int xc_fd(int xen_xc)
 {
     return xen_xc;
@@ -149,6 +152,7 @@ static inline void xs_close(struct xs_handle *xsh)
 #else
 
 typedef xc_interface *XenXC;
+typedef xc_interface *xenforeignmemory_handle;
 typedef xc_evtchn xenevtchn_handle;
 typedef xc_gnttab xengnttab_handle;
 
@@ -178,6 +182,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+/* See below for xenforeignmemory_* APIs */
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
@@ -501,4 +507,23 @@ static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
 }
 #endif
 
+#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
+
+#define xenforeignmemory_open(l, f) &xen_xc
+
+static inline void *xenforeignmemory_map(XenXC *h, uint32_t dom,
+                                         int prot, size_t pages,
+                                         const xen_pfn_t arr[/*pages*/],
+                                         int err[/*pages*/])
+{
+    if (err)
+        return xc_map_foreign_bulk(*h, dom, prot, arr, err, pages);
+    else
+        return xc_map_foreign_pages(*h, dom, prot, arr, pages);
+}
+
+#define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE)
+
+#endif
+
 #endif /* QEMU_HW_XEN_COMMON_H */
diff --git a/xen-common.c b/xen-common.c
index 0dcdbc3..6cd2959 100644
--- a/xen-common.c
+++ b/xen-common.c
@@ -118,6 +118,12 @@ static int xen_init(MachineState *ms)
         xen_be_printf(NULL, 0, "can't open xen interface\n");
         return -1;
     }
+    xen_fmem = xenforeignmemory_open(0, 0);
+    if (xen_fmem == NULL) {
+        xen_be_printf(NULL, 0, "can't open xen fmem interface\n");
+        xc_interface_close(xen_xc);
+        return -1;
+    }
     qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
 
     global_state_set_optional();
diff --git a/xen-hvm.c b/xen-hvm.c
index 1f729f6..66ee835 100644
--- a/xen-hvm.c
+++ b/xen-hvm.c
@@ -1247,9 +1247,9 @@ int xen_hvm_init(PCMachineState *pcms,
     DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
 
-    state->shared_page = xc_map_foreign_pages(xen_xc, xen_domid,
+    state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
                                               PROT_READ|PROT_WRITE,
-                                              &ioreq_pfn, 1);
+                                              1, &ioreq_pfn, NULL);
     if (state->shared_page == NULL) {
         hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
                  errno, xen_xc);
@@ -1259,8 +1259,8 @@ int xen_hvm_init(PCMachineState *pcms,
     if (!rc) {
         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
         state->shared_vmport_page =
-            xc_map_foreign_pages(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
-                                 &ioreq_pfn, 1);
+            xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
+                                 1, &ioreq_pfn, NULL);
         if (state->shared_vmport_page == NULL) {
             hw_error("map shared vmport IO page returned error %d handle="
                      XC_INTERFACE_FMT, errno, xen_xc);
@@ -1269,9 +1269,9 @@ int xen_hvm_init(PCMachineState *pcms,
         hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
     }
 
-    state->buffered_io_page = xc_map_foreign_pages(xen_xc, xen_domid,
+    state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
                                                    PROT_READ|PROT_WRITE,
-                                                   &bufioreq_pfn, 1);
+                                                   1, &bufioreq_pfn, NULL);
     if (state->buffered_io_page == NULL) {
         hw_error("map buffered IO page returned error %d", errno);
     }
diff --git a/xen-mapcache.c b/xen-mapcache.c
index 97fece2..4a04378 100644
--- a/xen-mapcache.c
+++ b/xen-mapcache.c
@@ -176,10 +176,10 @@ static void xen_remap_bucket(MapCacheEntry *entry,
         pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
     }
 
-    vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
-                                     pfns, err, nb_pfn);
+    vaddr_base = xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
+                                      nb_pfn, pfns, err);
     if (vaddr_base == NULL) {
-        perror("xc_map_foreign_bulk");
+        perror("xenforeignmemory_map");
         exit(-1);
     }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 5/8] xen: Switch uses of xc_map_foreign_{pages, bulk} to use libxenforeignmemory API.
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

One such library will be libxenforeignmemory which provides access to
privileged foreign mappings and which will provide an interface
equivalent to xc_map_foreign_{pages,bulk}.

The new xenforeignmemory_map() function behaves like
xc_map_foreign_pages() when the err argument is NULL and like
xc_map_foreign_bulk() when err is non-NULL, which maps into the shim
here onto checking err == NULL and calling the appropriate old
function.

Note that xenforeignmemory_map() takes the number of pages before the
arrays themselves, in order to support potentially future use of
variable-length-arrays in the prototype (in the future, when Xen's
baseline toolchain requirements are new enough to ensure VLAs are
supported).

In preparation for adding support for libxenforeignmemory add support
to the <=4.0 and <=4.6 compat code in xen_common.h to allow us to
switch to using the new API. These shims will disappear for versions
of Xen which include libxenforeignmemory.

Since libxenforeignmemory will have its own handle type but for <= 4.6
the functionality is provided by using a libxenctrl handle we
introduce a new global xen_fmem alongside the existing xen_xc. In fact
we make xen_fmem a pointer to the existing xen_xc, which then works
correctly with both <=4.0 (xc handle is an int) and <=4.6 (xc handle
is a pointer). In the latter case xen_fmem is actually a double
indirect pointer, but it all falls out in the wash.

Unlike libxenctrl libxenforeignmemory has an explicit unmap function,
rather than just specifying that munmap should be used, so the unmap
paths are updated to use xenforeignmemory_unmap, which is a shim for
munmap on these versions of xen. The mappings in xen-hvm.c do not
appear to be unmapped (which makes sense for a qemu-dm process)

In fb_disconnect this results in a change from simply mmap over the
existing mapping (with an implicit munmap) to expliclty unmapping with
xenforeignmemory_unmap and then mapping the required anonymous memory
in the same hole. I don't think this is a problem since any other
thread which was racily touching this region would already be running
the risk of hitting the mapping halfway through the call. If this is
thought to be a problem then we could consider adding an extra API to
the libxenforeignmemory interface to replace a foreign mapping with
anonymous shared memory, but I'd prefer not to.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
v8: Move two copies of compat xenforeignmemory_{open,unmap} to the
    common location. Guard with version 471 (which will be added in
    the next patch)
v7: Move two copies of compat xenforeignmemory_map to a common location.
    Note that the existing xen_domain_create #ifdef will be covered by
    a CONFIG_XEN_PV_DOMAIN_BUILD in a later patch, and so is not a
    suitable place to put this function.
v6: Handle _pages as well as _bulk, due to changes in the previous
    patches, including the dropping of "xen: Switch uses of
    xc_map_foreign_pages into xc_map_foreign_bulk" which previous preceded
    this patch and the change of "xen: Switch uses of
    xc_map_foreign_range into xc_map_foreign_bulk" into "xen: Switch
    uses of xc_map_foreign_range into xc_map_foreign_pages".

    Handle reordering of arguments to xenforeignmemory_map().

    Dropped Stefano's Reviewed-by due to these changes.

v4: Rebase onto "xen_console: correctly cleanup primary console on
    teardown."

    xenforeignmemory_unmap takes pages not bytes

    Compat wrapper for xenforeignmemory_open instead of ifdef in code.

    Run check patch and fix most issues. I did not fix:

ERROR: do not initialise globals to 0 or NULL
+xenforeignmemory_handle *xen_fmem = NULL;

=> This is consistent with all of the existing declarations.

ERROR: need consistent spacing around '*' (ctx:WxV)
+typedef xc_interface *xenforeignmemory_handle;

=> I think this is a false +ve since this is a pointer "*" not a multiple "*".

reorder args to xenforeignmemory_map
---
 hw/char/xen_console.c        |  8 ++++----
 hw/display/xenfb.c           | 17 +++++++++--------
 hw/xen/xen_backend.c         |  3 ++-
 include/hw/xen/xen_backend.h |  1 +
 include/hw/xen/xen_common.h  | 25 +++++++++++++++++++++++++
 xen-common.c                 |  6 ++++++
 xen-hvm.c                    | 12 ++++++------
 xen-mapcache.c               |  6 +++---
 8 files changed, 56 insertions(+), 22 deletions(-)

diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
index 3e8a57b..b92d0c6 100644
--- a/hw/char/xen_console.c
+++ b/hw/char/xen_console.c
@@ -229,9 +229,9 @@ static int con_initialise(struct XenDevice *xendev)
 
     if (!xendev->dev) {
         xen_pfn_t mfn = con->ring_ref;
-        con->sring = xc_map_foreign_pages(xen_xc, con->xendev.dom,
-                                         PROT_READ|PROT_WRITE,
-                                         &mfn, 1);
+        con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
+                                          PROT_READ|PROT_WRITE,
+                                          1, &mfn, NULL);
     } else {
         con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
@@ -273,7 +273,7 @@ static void con_disconnect(struct XenDevice *xendev)
 
     if (con->sring) {
         if (!xendev->dev) {
-            munmap(con->sring, XC_PAGE_SIZE);
+            xenforeignmemory_unmap(xen_fmem, con->sring, 1);
         } else {
             xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
         }
diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
index bb8f6b3..a42971c 100644
--- a/hw/display/xenfb.c
+++ b/hw/display/xenfb.c
@@ -106,8 +106,8 @@ static int common_bind(struct common *c)
     if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
 	return -1;
 
-    c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom,
-                                   PROT_READ | PROT_WRITE, &mfn, 1);
+    c->page = xenforeignmemory_map(xen_fmem, c->xendev.dom,
+                                   PROT_READ | PROT_WRITE, 1, &mfn, NULL);
     if (c->page == NULL)
 	return -1;
 
@@ -122,7 +122,7 @@ static void common_unbind(struct common *c)
 {
     xen_be_unbind_evtchn(&c->xendev);
     if (c->page) {
-	munmap(c->page, XC_PAGE_SIZE);
+        xenforeignmemory_unmap(xen_fmem, c->page, 1);
 	c->page = NULL;
     }
 }
@@ -495,15 +495,15 @@ static int xenfb_map_fb(struct XenFB *xenfb)
     fbmfns = g_malloc0(sizeof(xen_pfn_t) * xenfb->fbpages);
 
     xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
-    map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
-			       PROT_READ, pgmfns, n_fbdirs);
+    map = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
+                               PROT_READ, n_fbdirs, pgmfns, NULL);
     if (map == NULL)
 	goto out;
     xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
-    munmap(map, n_fbdirs * XC_PAGE_SIZE);
+    xenforeignmemory_unmap(xen_fmem, map, n_fbdirs);
 
-    xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
-            PROT_READ, fbmfns, xenfb->fbpages);
+    xenfb->pixels = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
+            PROT_READ, xenfb->fbpages, fbmfns, NULL);
     if (xenfb->pixels == NULL)
 	goto out;
 
@@ -912,6 +912,7 @@ static void fb_disconnect(struct XenDevice *xendev)
      *   Replacing the framebuffer with anonymous shared memory
      *   instead.  This releases the guest pages and keeps qemu happy.
      */
+    xenforeignmemory_unmap(xen_fmem, fb->pixels, fb->fbpages);
     fb->pixels = mmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE,
                       PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
                       -1, 0);
diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
index 966e34f..caa459c 100644
--- a/hw/xen/xen_backend.c
+++ b/hw/xen/xen_backend.c
@@ -45,6 +45,7 @@
 
 /* public */
 XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
+xenforeignmemory_handle *xen_fmem = NULL;
 struct xs_handle *xenstore = NULL;
 const char *xen_protocol;
 
@@ -717,7 +718,7 @@ int xen_be_init(void)
 
     qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
 
-    if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
+    if (xen_xc == XC_HANDLER_INITIAL_VALUE || xen_fmem == NULL) {
         /* Check if xen_init() have been called */
         goto err;
     }
diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
index 8e8857b..e0d52ee 100644
--- a/include/hw/xen/xen_backend.h
+++ b/include/hw/xen/xen_backend.h
@@ -57,6 +57,7 @@ struct XenDevice {
 
 /* variables */
 extern XenXC xen_xc;
+extern xenforeignmemory_handle *xen_fmem;
 extern struct xs_handle *xenstore;
 extern const char *xen_protocol;
 
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 8f38310..95275b3 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -41,6 +41,7 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
 typedef int XenXC;
 typedef int xenevtchn_handle;
 typedef int xengnttab_handle;
+typedef int xenforeignmemory_handle;
 
 #  define XC_INTERFACE_FMT "%i"
 #  define XC_HANDLER_INITIAL_VALUE    -1
@@ -104,6 +105,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open();
 }
 
+/* See below for xenforeignmemory_* APIs */
+
 static inline int xc_fd(int xen_xc)
 {
     return xen_xc;
@@ -149,6 +152,7 @@ static inline void xs_close(struct xs_handle *xsh)
 #else
 
 typedef xc_interface *XenXC;
+typedef xc_interface *xenforeignmemory_handle;
 typedef xc_evtchn xenevtchn_handle;
 typedef xc_gnttab xengnttab_handle;
 
@@ -178,6 +182,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
 
+/* See below for xenforeignmemory_* APIs */
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
@@ -501,4 +507,23 @@ static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
 }
 #endif
 
+#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
+
+#define xenforeignmemory_open(l, f) &xen_xc
+
+static inline void *xenforeignmemory_map(XenXC *h, uint32_t dom,
+                                         int prot, size_t pages,
+                                         const xen_pfn_t arr[/*pages*/],
+                                         int err[/*pages*/])
+{
+    if (err)
+        return xc_map_foreign_bulk(*h, dom, prot, arr, err, pages);
+    else
+        return xc_map_foreign_pages(*h, dom, prot, arr, pages);
+}
+
+#define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE)
+
+#endif
+
 #endif /* QEMU_HW_XEN_COMMON_H */
diff --git a/xen-common.c b/xen-common.c
index 0dcdbc3..6cd2959 100644
--- a/xen-common.c
+++ b/xen-common.c
@@ -118,6 +118,12 @@ static int xen_init(MachineState *ms)
         xen_be_printf(NULL, 0, "can't open xen interface\n");
         return -1;
     }
+    xen_fmem = xenforeignmemory_open(0, 0);
+    if (xen_fmem == NULL) {
+        xen_be_printf(NULL, 0, "can't open xen fmem interface\n");
+        xc_interface_close(xen_xc);
+        return -1;
+    }
     qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
 
     global_state_set_optional();
diff --git a/xen-hvm.c b/xen-hvm.c
index 1f729f6..66ee835 100644
--- a/xen-hvm.c
+++ b/xen-hvm.c
@@ -1247,9 +1247,9 @@ int xen_hvm_init(PCMachineState *pcms,
     DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
     DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
 
-    state->shared_page = xc_map_foreign_pages(xen_xc, xen_domid,
+    state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
                                               PROT_READ|PROT_WRITE,
-                                              &ioreq_pfn, 1);
+                                              1, &ioreq_pfn, NULL);
     if (state->shared_page == NULL) {
         hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
                  errno, xen_xc);
@@ -1259,8 +1259,8 @@ int xen_hvm_init(PCMachineState *pcms,
     if (!rc) {
         DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
         state->shared_vmport_page =
-            xc_map_foreign_pages(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
-                                 &ioreq_pfn, 1);
+            xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
+                                 1, &ioreq_pfn, NULL);
         if (state->shared_vmport_page == NULL) {
             hw_error("map shared vmport IO page returned error %d handle="
                      XC_INTERFACE_FMT, errno, xen_xc);
@@ -1269,9 +1269,9 @@ int xen_hvm_init(PCMachineState *pcms,
         hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
     }
 
-    state->buffered_io_page = xc_map_foreign_pages(xen_xc, xen_domid,
+    state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
                                                    PROT_READ|PROT_WRITE,
-                                                   &bufioreq_pfn, 1);
+                                                   1, &bufioreq_pfn, NULL);
     if (state->buffered_io_page == NULL) {
         hw_error("map buffered IO page returned error %d", errno);
     }
diff --git a/xen-mapcache.c b/xen-mapcache.c
index 97fece2..4a04378 100644
--- a/xen-mapcache.c
+++ b/xen-mapcache.c
@@ -176,10 +176,10 @@ static void xen_remap_bucket(MapCacheEntry *entry,
         pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
     }
 
-    vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
-                                     pfns, err, nb_pfn);
+    vaddr_base = xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
+                                      nb_pfn, pfns, err);
     if (vaddr_base == NULL) {
-        perror("xc_map_foreign_bulk");
+        perror("xenforeignmemory_map");
         exit(-1);
     }
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 6/8] xen: Use stable library interfaces when they are available.
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

Specifically libxenevtchn, libxengnttab and libxenforeignmemory.

Previous patches have already laid the groundwork for using these by
switching the existing compatibility shims to reflect the intefaces to
these libraries.

So all which remains is to update configure to detect the libraries
and enable their use. Although they are notionally independent we take
an all or nothing approach to the three libraries since they were
added at the same time.

The only non-obvious bit is that we now open a proper xenforeignmemory
handle for xen_fmem instead of reusing the xen_xc handle.

Build tested with 4.0 .. 4.6 (inclusive) and the patches targetting
4.7 which adds these libraries.

This uses CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 to cover the
introduction of these new interfaces.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---

v8: Use CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 for new interfaces.
    Dropped Reviewed-by.

v6: Two minor formatting things.
    Rebase onto "xen: fix usage of xc_domain_create in domain
    builder", required adjusting the configure script changes.

    The rebase wasn't entirely trivial (semantically), so dropped
    Stefano's reviwed by.

v5: Remove ifdef check when undeffing the compat stuff.
    s/now way/no way/

v4: xenforeignmemory_open is now a compat wrapper, so no ifdef.

    Simplify configury by asserting that interface version 470 will
    always have the libraries (lack of them makes it 460).

    Ran checkpatch and fixed everything except:

ERROR: need consistent spacing around '*' (ctx:WxV)
+typedef xc_interface *XenXC;

Which I think is a false +ve.

Simpler configure stuff
---
 configure                   | 55 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/xen/xen_common.h | 35 +++++++++++++++++++++++++++--
 2 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 44ac9ab..9ead31d 100755
--- a/configure
+++ b/configure
@@ -1938,6 +1938,7 @@ fi
 
 if test "$xen" != "no" ; then
   xen_libs="-lxenstore -lxenctrl -lxenguest"
+  xen_stable_libs="-lxenforeignmemory -lxengnttab -lxenevtchn"
 
   # First we test whether Xen headers and libraries are available.
   # If no, we are done and there is no Xen support.
@@ -1960,6 +1961,57 @@ EOF
   # Xen unstable
   elif
       cat > $TMPC <<EOF &&
+/*
+ * If we have stable libs the we don't want the libxc compat
+ * layers, regardless of what CFLAGS we may have been given.
+ */
+#undef XC_WANT_COMPAT_EVTCHN_API
+#undef XC_WANT_COMPAT_GNTTAB_API
+#undef XC_WANT_COMPAT_MAP_FOREIGN_API
+#include <xenctrl.h>
+#include <xenstore.h>
+#include <xenevtchn.h>
+#include <xengnttab.h>
+#include <xenforeignmemory.h>
+#include <stdint.h>
+#include <xen/hvm/hvm_info_table.h>
+#if !defined(HVM_MAX_VCPUS)
+# error HVM_MAX_VCPUS not defined
+#endif
+int main(void) {
+  xc_interface *xc = NULL;
+  xenforeignmemory_handle *xfmem;
+  xenevtchn_handle *xe;
+  xengnttab_handle *xg;
+  xen_domain_handle_t handle;
+
+  xs_daemon_open();
+
+  xc = xc_interface_open(0, 0, 0);
+  xc_hvm_set_mem_type(0, 0, HVMMEM_ram_ro, 0, 0);
+  xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
+  xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
+  xc_hvm_create_ioreq_server(xc, 0, HVM_IOREQSRV_BUFIOREQ_ATOMIC, NULL);
+  xc_domain_create(xc, 0, handle, 0, NULL, NULL);
+
+  xfmem = xenforeignmemory_open(0, 0);
+  xenforeignmemory_map(xfmem, 0, 0, 0, 0, 0);
+
+  xe = xenevtchn_open(0, 0);
+  xenevtchn_fd(xe);
+
+  xg = xengnttab_open(0, 0);
+  xengnttab_map_grant_ref(xg, 0, 0, 0);
+
+  return 0;
+}
+EOF
+      compile_prog "" "$xen_libs $xen_stable_libs"
+    then
+    xen_ctrl_version=471
+    xen=yes
+  elif
+      cat > $TMPC <<EOF &&
 #include <xenctrl.h>
 #include <stdint.h>
 int main(void) {
@@ -2153,6 +2205,9 @@ EOF
   fi
 
   if test "$xen" = yes; then
+    if test $xen_ctrl_version -ge 471  ; then
+      libs_softmmu="$xen_stable_libs $libs_softmmu"
+    fi
     libs_softmmu="$xen_libs $libs_softmmu"
   fi
 fi
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 95275b3..19f1577 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -6,6 +6,15 @@
 #include <stddef.h>
 #include <inttypes.h>
 
+/*
+ * If we have new enough libxenctrl then we do not want/need these compat
+ * interfaces, despite what the user supplied cflags might say. They
+ * must be undefined before including xenctrl.h
+ */
+#undef XC_WANT_COMPAT_EVTCHN_API
+#undef XC_WANT_COMPAT_GNTTAB_API
+#undef XC_WANT_COMPAT_MAP_FOREIGN_API
+
 #include <xenctrl.h>
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 420
 #  include <xs.h>
@@ -148,8 +157,8 @@ static inline void xs_close(struct xs_handle *xsh)
 }
 
 
-/* Xen 4.1 */
-#else
+/* Xen 4.1 thru 4.6 */
+#elif CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
 
 typedef xc_interface *XenXC;
 typedef xc_interface *xenforeignmemory_handle;
@@ -184,6 +193,28 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 
 /* See below for xenforeignmemory_* APIs */
 
+/* FIXME There is no way to have the xen fd */
+static inline int xc_fd(xc_interface *xen_xc)
+{
+    return -1;
+}
+#else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
+
+typedef xc_interface *XenXC;
+
+#  define XC_INTERFACE_FMT "%p"
+#  define XC_HANDLER_INITIAL_VALUE    NULL
+
+#include <xenevtchn.h>
+#include <xengnttab.h>
+#include <xenforeignmemory.h>
+
+static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
+                                          unsigned int open_flags)
+{
+    return xc_interface_open(logger, dombuild_logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 6/8] xen: Use stable library interfaces when they are available.
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

In Xen 4.7 we are refactoring parts libxenctrl into a number of
separate libraries which will provide backward and forward API and ABI
compatiblity.

Specifically libxenevtchn, libxengnttab and libxenforeignmemory.

Previous patches have already laid the groundwork for using these by
switching the existing compatibility shims to reflect the intefaces to
these libraries.

So all which remains is to update configure to detect the libraries
and enable their use. Although they are notionally independent we take
an all or nothing approach to the three libraries since they were
added at the same time.

The only non-obvious bit is that we now open a proper xenforeignmemory
handle for xen_fmem instead of reusing the xen_xc handle.

Build tested with 4.0 .. 4.6 (inclusive) and the patches targetting
4.7 which adds these libraries.

This uses CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 to cover the
introduction of these new interfaces.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---

v8: Use CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 for new interfaces.
    Dropped Reviewed-by.

v6: Two minor formatting things.
    Rebase onto "xen: fix usage of xc_domain_create in domain
    builder", required adjusting the configure script changes.

    The rebase wasn't entirely trivial (semantically), so dropped
    Stefano's reviwed by.

v5: Remove ifdef check when undeffing the compat stuff.
    s/now way/no way/

v4: xenforeignmemory_open is now a compat wrapper, so no ifdef.

    Simplify configury by asserting that interface version 470 will
    always have the libraries (lack of them makes it 460).

    Ran checkpatch and fixed everything except:

ERROR: need consistent spacing around '*' (ctx:WxV)
+typedef xc_interface *XenXC;

Which I think is a false +ve.

Simpler configure stuff
---
 configure                   | 55 +++++++++++++++++++++++++++++++++++++++++++++
 include/hw/xen/xen_common.h | 35 +++++++++++++++++++++++++++--
 2 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/configure b/configure
index 44ac9ab..9ead31d 100755
--- a/configure
+++ b/configure
@@ -1938,6 +1938,7 @@ fi
 
 if test "$xen" != "no" ; then
   xen_libs="-lxenstore -lxenctrl -lxenguest"
+  xen_stable_libs="-lxenforeignmemory -lxengnttab -lxenevtchn"
 
   # First we test whether Xen headers and libraries are available.
   # If no, we are done and there is no Xen support.
@@ -1960,6 +1961,57 @@ EOF
   # Xen unstable
   elif
       cat > $TMPC <<EOF &&
+/*
+ * If we have stable libs the we don't want the libxc compat
+ * layers, regardless of what CFLAGS we may have been given.
+ */
+#undef XC_WANT_COMPAT_EVTCHN_API
+#undef XC_WANT_COMPAT_GNTTAB_API
+#undef XC_WANT_COMPAT_MAP_FOREIGN_API
+#include <xenctrl.h>
+#include <xenstore.h>
+#include <xenevtchn.h>
+#include <xengnttab.h>
+#include <xenforeignmemory.h>
+#include <stdint.h>
+#include <xen/hvm/hvm_info_table.h>
+#if !defined(HVM_MAX_VCPUS)
+# error HVM_MAX_VCPUS not defined
+#endif
+int main(void) {
+  xc_interface *xc = NULL;
+  xenforeignmemory_handle *xfmem;
+  xenevtchn_handle *xe;
+  xengnttab_handle *xg;
+  xen_domain_handle_t handle;
+
+  xs_daemon_open();
+
+  xc = xc_interface_open(0, 0, 0);
+  xc_hvm_set_mem_type(0, 0, HVMMEM_ram_ro, 0, 0);
+  xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
+  xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
+  xc_hvm_create_ioreq_server(xc, 0, HVM_IOREQSRV_BUFIOREQ_ATOMIC, NULL);
+  xc_domain_create(xc, 0, handle, 0, NULL, NULL);
+
+  xfmem = xenforeignmemory_open(0, 0);
+  xenforeignmemory_map(xfmem, 0, 0, 0, 0, 0);
+
+  xe = xenevtchn_open(0, 0);
+  xenevtchn_fd(xe);
+
+  xg = xengnttab_open(0, 0);
+  xengnttab_map_grant_ref(xg, 0, 0, 0);
+
+  return 0;
+}
+EOF
+      compile_prog "" "$xen_libs $xen_stable_libs"
+    then
+    xen_ctrl_version=471
+    xen=yes
+  elif
+      cat > $TMPC <<EOF &&
 #include <xenctrl.h>
 #include <stdint.h>
 int main(void) {
@@ -2153,6 +2205,9 @@ EOF
   fi
 
   if test "$xen" = yes; then
+    if test $xen_ctrl_version -ge 471  ; then
+      libs_softmmu="$xen_stable_libs $libs_softmmu"
+    fi
     libs_softmmu="$xen_libs $libs_softmmu"
   fi
 fi
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 95275b3..19f1577 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -6,6 +6,15 @@
 #include <stddef.h>
 #include <inttypes.h>
 
+/*
+ * If we have new enough libxenctrl then we do not want/need these compat
+ * interfaces, despite what the user supplied cflags might say. They
+ * must be undefined before including xenctrl.h
+ */
+#undef XC_WANT_COMPAT_EVTCHN_API
+#undef XC_WANT_COMPAT_GNTTAB_API
+#undef XC_WANT_COMPAT_MAP_FOREIGN_API
+
 #include <xenctrl.h>
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 420
 #  include <xs.h>
@@ -148,8 +157,8 @@ static inline void xs_close(struct xs_handle *xsh)
 }
 
 
-/* Xen 4.1 */
-#else
+/* Xen 4.1 thru 4.6 */
+#elif CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
 
 typedef xc_interface *XenXC;
 typedef xc_interface *xenforeignmemory_handle;
@@ -184,6 +193,28 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 
 /* See below for xenforeignmemory_* APIs */
 
+/* FIXME There is no way to have the xen fd */
+static inline int xc_fd(xc_interface *xen_xc)
+{
+    return -1;
+}
+#else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
+
+typedef xc_interface *XenXC;
+
+#  define XC_INTERFACE_FMT "%p"
+#  define XC_HANDLER_INITIAL_VALUE    NULL
+
+#include <xenevtchn.h>
+#include <xengnttab.h>
+#include <xenforeignmemory.h>
+
+static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
+                                          unsigned int open_flags)
+{
+    return xc_interface_open(logger, dombuild_logger, open_flags);
+}
+
 /* FIXME There is now way to have the xen fd */
 static inline int xc_fd(xc_interface *xen_xc)
 {
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 7/8] xen: domainbuild: reopen libxenctrl interface after forking for domain watcher.
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

Using an existing libxenctrl handle after a fork was never
particularly safe (especially if foreign mappings existed at the time
of the fork) and the xc fd has been unavailable for many releases.

Reopen the handle after fork and therefore do away with xc_fd().

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
The fact that xc_fd hasn't been useful since at least Xen 4.1 makes me
question the utility of this domainbuild in QEMU. Perhaps we should
just nuke it?
---
 hw/xenpv/xen_domainbuild.c  |  9 ++++++---
 include/hw/xen/xen_common.h | 17 -----------------
 2 files changed, 6 insertions(+), 20 deletions(-)

diff --git a/hw/xenpv/xen_domainbuild.c b/hw/xenpv/xen_domainbuild.c
index ac0e5ac..f9be029 100644
--- a/hw/xenpv/xen_domainbuild.c
+++ b/hw/xenpv/xen_domainbuild.c
@@ -174,12 +174,15 @@ static int xen_domain_watcher(void)
     for (i = 3; i < n; i++) {
         if (i == fd[0])
             continue;
-        if (i == xc_fd(xen_xc)) {
-            continue;
-        }
         close(i);
     }
 
+    /*
+     * Reopen xc interface, since the original is unsafe after fork
+     * and was closed above.
+     */
+    xen_xc = xc_interface_open(0, 0, 0);
+
     /* ignore term signals */
     signal(SIGINT,  SIG_IGN);
     signal(SIGTERM, SIG_IGN);
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 19f1577..be7a915 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -116,12 +116,6 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 
 /* See below for xenforeignmemory_* APIs */
 
-static inline int xc_fd(int xen_xc)
-{
-    return xen_xc;
-}
-
-
 static inline int xc_domain_populate_physmap_exact
     (XenXC xc_handle, uint32_t domid, unsigned long nr_extents,
      unsigned int extent_order, unsigned int mem_flags, xen_pfn_t *extent_start)
@@ -193,11 +187,6 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 
 /* See below for xenforeignmemory_* APIs */
 
-/* FIXME There is no way to have the xen fd */
-static inline int xc_fd(xc_interface *xen_xc)
-{
-    return -1;
-}
 #else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
 
 typedef xc_interface *XenXC;
@@ -214,12 +203,6 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 {
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
-
-/* FIXME There is now way to have the xen fd */
-static inline int xc_fd(xc_interface *xen_xc)
-{
-    return -1;
-}
 #endif
 
 /* Xen before 4.2 */
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 7/8] xen: domainbuild: reopen libxenctrl interface after forking for domain watcher.
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

Using an existing libxenctrl handle after a fork was never
particularly safe (especially if foreign mappings existed at the time
of the fork) and the xc fd has been unavailable for many releases.

Reopen the handle after fork and therefore do away with xc_fd().

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
The fact that xc_fd hasn't been useful since at least Xen 4.1 makes me
question the utility of this domainbuild in QEMU. Perhaps we should
just nuke it?
---
 hw/xenpv/xen_domainbuild.c  |  9 ++++++---
 include/hw/xen/xen_common.h | 17 -----------------
 2 files changed, 6 insertions(+), 20 deletions(-)

diff --git a/hw/xenpv/xen_domainbuild.c b/hw/xenpv/xen_domainbuild.c
index ac0e5ac..f9be029 100644
--- a/hw/xenpv/xen_domainbuild.c
+++ b/hw/xenpv/xen_domainbuild.c
@@ -174,12 +174,15 @@ static int xen_domain_watcher(void)
     for (i = 3; i < n; i++) {
         if (i == fd[0])
             continue;
-        if (i == xc_fd(xen_xc)) {
-            continue;
-        }
         close(i);
     }
 
+    /*
+     * Reopen xc interface, since the original is unsafe after fork
+     * and was closed above.
+     */
+    xen_xc = xc_interface_open(0, 0, 0);
+
     /* ignore term signals */
     signal(SIGINT,  SIG_IGN);
     signal(SIGTERM, SIG_IGN);
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index 19f1577..be7a915 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -116,12 +116,6 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 
 /* See below for xenforeignmemory_* APIs */
 
-static inline int xc_fd(int xen_xc)
-{
-    return xen_xc;
-}
-
-
 static inline int xc_domain_populate_physmap_exact
     (XenXC xc_handle, uint32_t domid, unsigned long nr_extents,
      unsigned int extent_order, unsigned int mem_flags, xen_pfn_t *extent_start)
@@ -193,11 +187,6 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 
 /* See below for xenforeignmemory_* APIs */
 
-/* FIXME There is no way to have the xen fd */
-static inline int xc_fd(xc_interface *xen_xc)
-{
-    return -1;
-}
 #else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
 
 typedef xc_interface *XenXC;
@@ -214,12 +203,6 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
 {
     return xc_interface_open(logger, dombuild_logger, open_flags);
 }
-
-/* FIXME There is now way to have the xen fd */
-static inline int xc_fd(xc_interface *xen_xc)
-{
-    return -1;
-}
 #endif
 
 /* Xen before 4.2 */
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [Qemu-devel] [PATCH QEMU-XEN v8 8/8] xen: make it possible to build without the Xen PV domain builder
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23     ` Ian Campbell
  2016-01-15 13:23     ` Ian Campbell
                       ` (8 subsequent siblings)
  9 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

Until the previous patch this relied on xc_fd(), which was only
implemented for Xen 4.0 and earlier.

Given this wasn't working since Xen 4.0 I have marked this as disabled
by default.

Removing this support drops the use of a bunch of symbols from
libxenctrl, specifically:

  - xc_domain_create
  - xc_domain_destroy
  - xc_domain_getinfo
  - xc_domain_max_vcpus
  - xc_domain_setmaxmem
  - xc_domain_unpause
  - xc_evtchn_alloc_unbound
  - xc_linux_build

This is another step towards only using Xen libraries which provide a
stable inteface.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v6: Rebase onto "xen: fix usage of xc_domain_create in domain
    builder" (trivial, Reviewed-by retained)

v5: XEN_CREATE entirely wihtin CONFIG_XEN_PV_DOMAIN_BUILD ifdef.
    Simplify configure'ry.

v4: Fixed all checkpatch errors.
    Disabled by default.
---
 configure                   | 15 +++++++++++++++
 hw/xenpv/Makefile.objs      |  4 +++-
 hw/xenpv/xen_machine_pv.c   | 15 +++++++++++----
 include/hw/xen/xen_common.h |  2 ++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index 9ead31d..3506e44 100755
--- a/configure
+++ b/configure
@@ -250,6 +250,7 @@ vnc_jpeg=""
 vnc_png=""
 xen=""
 xen_ctrl_version=""
+xen_pv_domain_build="no"
 xen_pci_passthrough=""
 linux_aio=""
 cap_ng=""
@@ -927,6 +928,10 @@ for opt do
   ;;
   --enable-xen-pci-passthrough) xen_pci_passthrough="yes"
   ;;
+  --disable-xen-pv-domain-build) xen_pv_domain_build="no"
+  ;;
+  --enable-xen-pv-domain-build) xen_pv_domain_build="yes"
+  ;;
   --disable-brlapi) brlapi="no"
   ;;
   --enable-brlapi) brlapi="yes"
@@ -2229,6 +2234,12 @@ if test "$xen_pci_passthrough" != "no"; then
   fi
 fi
 
+if test "$xen_pv_domain_build" = "yes" &&
+   test "$xen" != "yes"; then
+    error_exit "User requested Xen PV domain builder support" \
+	       "which requires Xen support."
+fi
+
 ##########################################
 # libtool probe
 
@@ -4848,6 +4859,7 @@ fi
 echo "xen support       $xen"
 if test "$xen" = "yes" ; then
   echo "xen ctrl version  $xen_ctrl_version"
+  echo "pv dom build      $xen_pv_domain_build"
 fi
 echo "brlapi support    $brlapi"
 echo "bluez  support    $bluez"
@@ -5219,6 +5231,9 @@ fi
 if test "$xen" = "yes" ; then
   echo "CONFIG_XEN_BACKEND=y" >> $config_host_mak
   echo "CONFIG_XEN_CTRL_INTERFACE_VERSION=$xen_ctrl_version" >> $config_host_mak
+  if test "$xen_pv_domain_build" = "yes" ; then
+    echo "CONFIG_XEN_PV_DOMAIN_BUILD=y" >> $config_host_mak
+  fi
 fi
 if test "$linux_aio" = "yes" ; then
   echo "CONFIG_LINUX_AIO=y" >> $config_host_mak
diff --git a/hw/xenpv/Makefile.objs b/hw/xenpv/Makefile.objs
index 49f6e9e..bbf5873 100644
--- a/hw/xenpv/Makefile.objs
+++ b/hw/xenpv/Makefile.objs
@@ -1,2 +1,4 @@
 # Xen PV machine support
-obj-$(CONFIG_XEN) += xen_domainbuild.o xen_machine_pv.o
+obj-$(CONFIG_XEN) += xen_machine_pv.o
+# Xen PV machine builder support
+obj-$(CONFIG_XEN_PV_DOMAIN_BUILD) += xen_domainbuild.o
diff --git a/hw/xenpv/xen_machine_pv.c b/hw/xenpv/xen_machine_pv.c
index 23d6ef0..3250b94 100644
--- a/hw/xenpv/xen_machine_pv.c
+++ b/hw/xenpv/xen_machine_pv.c
@@ -30,9 +30,6 @@
 
 static void xen_init_pv(MachineState *machine)
 {
-    const char *kernel_filename = machine->kernel_filename;
-    const char *kernel_cmdline = machine->kernel_cmdline;
-    const char *initrd_filename = machine->initrd_filename;
     DriveInfo *dinfo;
     int i;
 
@@ -46,17 +43,27 @@ static void xen_init_pv(MachineState *machine)
     case XEN_ATTACH:
         /* nothing to do, xend handles everything */
         break;
-    case XEN_CREATE:
+#ifdef CONFIG_XEN_PV_DOMAIN_BUILD
+    case XEN_CREATE: {
+        const char *kernel_filename = machine->kernel_filename;
+        const char *kernel_cmdline = machine->kernel_cmdline;
+        const char *initrd_filename = machine->initrd_filename;
         if (xen_domain_build_pv(kernel_filename, initrd_filename,
                                 kernel_cmdline) < 0) {
             fprintf(stderr, "xen pv domain creation failed\n");
             exit(1);
         }
         break;
+    }
+#endif
     case XEN_EMULATE:
         fprintf(stderr, "xen emulation not implemented (yet)\n");
         exit(1);
         break;
+    default:
+        fprintf(stderr, "unhandled xen_mode %d\n", xen_mode);
+        exit(1);
+        break;
     }
 
     xen_be_register("console", &xen_console_ops);
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index be7a915..0d83891 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -505,6 +505,7 @@ static inline int xen_xc_domain_add_to_physmap(XenXC xch, uint32_t domid,
 }
 #endif
 
+#ifdef CONFIG_XEN_PV_DOMAIN_BUILD
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 470
 static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
                                     xen_domain_handle_t handle, uint32_t flags,
@@ -520,6 +521,7 @@ static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
     return xc_domain_create(xc, ssidref, handle, flags, pdomid, NULL);
 }
 #endif
+#endif
 
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN v8 8/8] xen: make it possible to build without the Xen PV domain builder
@ 2016-01-15 13:23     ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: Ian Campbell, qemu-devel, stefano.stabellini

Until the previous patch this relied on xc_fd(), which was only
implemented for Xen 4.0 and earlier.

Given this wasn't working since Xen 4.0 I have marked this as disabled
by default.

Removing this support drops the use of a bunch of symbols from
libxenctrl, specifically:

  - xc_domain_create
  - xc_domain_destroy
  - xc_domain_getinfo
  - xc_domain_max_vcpus
  - xc_domain_setmaxmem
  - xc_domain_unpause
  - xc_evtchn_alloc_unbound
  - xc_linux_build

This is another step towards only using Xen libraries which provide a
stable inteface.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>
---
v6: Rebase onto "xen: fix usage of xc_domain_create in domain
    builder" (trivial, Reviewed-by retained)

v5: XEN_CREATE entirely wihtin CONFIG_XEN_PV_DOMAIN_BUILD ifdef.
    Simplify configure'ry.

v4: Fixed all checkpatch errors.
    Disabled by default.
---
 configure                   | 15 +++++++++++++++
 hw/xenpv/Makefile.objs      |  4 +++-
 hw/xenpv/xen_machine_pv.c   | 15 +++++++++++----
 include/hw/xen/xen_common.h |  2 ++
 4 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index 9ead31d..3506e44 100755
--- a/configure
+++ b/configure
@@ -250,6 +250,7 @@ vnc_jpeg=""
 vnc_png=""
 xen=""
 xen_ctrl_version=""
+xen_pv_domain_build="no"
 xen_pci_passthrough=""
 linux_aio=""
 cap_ng=""
@@ -927,6 +928,10 @@ for opt do
   ;;
   --enable-xen-pci-passthrough) xen_pci_passthrough="yes"
   ;;
+  --disable-xen-pv-domain-build) xen_pv_domain_build="no"
+  ;;
+  --enable-xen-pv-domain-build) xen_pv_domain_build="yes"
+  ;;
   --disable-brlapi) brlapi="no"
   ;;
   --enable-brlapi) brlapi="yes"
@@ -2229,6 +2234,12 @@ if test "$xen_pci_passthrough" != "no"; then
   fi
 fi
 
+if test "$xen_pv_domain_build" = "yes" &&
+   test "$xen" != "yes"; then
+    error_exit "User requested Xen PV domain builder support" \
+	       "which requires Xen support."
+fi
+
 ##########################################
 # libtool probe
 
@@ -4848,6 +4859,7 @@ fi
 echo "xen support       $xen"
 if test "$xen" = "yes" ; then
   echo "xen ctrl version  $xen_ctrl_version"
+  echo "pv dom build      $xen_pv_domain_build"
 fi
 echo "brlapi support    $brlapi"
 echo "bluez  support    $bluez"
@@ -5219,6 +5231,9 @@ fi
 if test "$xen" = "yes" ; then
   echo "CONFIG_XEN_BACKEND=y" >> $config_host_mak
   echo "CONFIG_XEN_CTRL_INTERFACE_VERSION=$xen_ctrl_version" >> $config_host_mak
+  if test "$xen_pv_domain_build" = "yes" ; then
+    echo "CONFIG_XEN_PV_DOMAIN_BUILD=y" >> $config_host_mak
+  fi
 fi
 if test "$linux_aio" = "yes" ; then
   echo "CONFIG_LINUX_AIO=y" >> $config_host_mak
diff --git a/hw/xenpv/Makefile.objs b/hw/xenpv/Makefile.objs
index 49f6e9e..bbf5873 100644
--- a/hw/xenpv/Makefile.objs
+++ b/hw/xenpv/Makefile.objs
@@ -1,2 +1,4 @@
 # Xen PV machine support
-obj-$(CONFIG_XEN) += xen_domainbuild.o xen_machine_pv.o
+obj-$(CONFIG_XEN) += xen_machine_pv.o
+# Xen PV machine builder support
+obj-$(CONFIG_XEN_PV_DOMAIN_BUILD) += xen_domainbuild.o
diff --git a/hw/xenpv/xen_machine_pv.c b/hw/xenpv/xen_machine_pv.c
index 23d6ef0..3250b94 100644
--- a/hw/xenpv/xen_machine_pv.c
+++ b/hw/xenpv/xen_machine_pv.c
@@ -30,9 +30,6 @@
 
 static void xen_init_pv(MachineState *machine)
 {
-    const char *kernel_filename = machine->kernel_filename;
-    const char *kernel_cmdline = machine->kernel_cmdline;
-    const char *initrd_filename = machine->initrd_filename;
     DriveInfo *dinfo;
     int i;
 
@@ -46,17 +43,27 @@ static void xen_init_pv(MachineState *machine)
     case XEN_ATTACH:
         /* nothing to do, xend handles everything */
         break;
-    case XEN_CREATE:
+#ifdef CONFIG_XEN_PV_DOMAIN_BUILD
+    case XEN_CREATE: {
+        const char *kernel_filename = machine->kernel_filename;
+        const char *kernel_cmdline = machine->kernel_cmdline;
+        const char *initrd_filename = machine->initrd_filename;
         if (xen_domain_build_pv(kernel_filename, initrd_filename,
                                 kernel_cmdline) < 0) {
             fprintf(stderr, "xen pv domain creation failed\n");
             exit(1);
         }
         break;
+    }
+#endif
     case XEN_EMULATE:
         fprintf(stderr, "xen emulation not implemented (yet)\n");
         exit(1);
         break;
+    default:
+        fprintf(stderr, "unhandled xen_mode %d\n", xen_mode);
+        exit(1);
+        break;
     }
 
     xen_be_register("console", &xen_console_ops);
diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
index be7a915..0d83891 100644
--- a/include/hw/xen/xen_common.h
+++ b/include/hw/xen/xen_common.h
@@ -505,6 +505,7 @@ static inline int xen_xc_domain_add_to_physmap(XenXC xch, uint32_t domid,
 }
 #endif
 
+#ifdef CONFIG_XEN_PV_DOMAIN_BUILD
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 470
 static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
                                     xen_domain_handle_t handle, uint32_t flags,
@@ -520,6 +521,7 @@ static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
     return xc_domain_create(xc, ssidref, handle, flags, pdomid, NULL);
 }
 #endif
+#endif
 
 #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN-TRADITIONAL v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (2 preceding siblings ...)
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
@ 2016-01-15 13:23 ` Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 1/4] qemu-xen-traditional: Use libxenevtchn Ian Campbell
                     ` (3 more replies)
  2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (4 subsequent siblings)
  8 siblings, 4 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

We intend to stabilise some parts of the libxenctrl interface by
splitting out some functionality into separate stable libraries.

This is the qemu-xen-traditional part of the first phase of that change.

This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
super-intro mail covering all of the related patch series and which
contains more details.

Ian Campbell (4):
  qemu-xen-traditional: Use libxenevtchn
  qemu-xen-traditional: Use libxengnttab
  qemu-xen-traditional: Add libxencall to rpath-link
  qemu-xen-traditional: Add libxenforeignmemory to rpath-link

 hw/xen_backend.c  | 30 +++++++++++++++---------------
 hw/xen_backend.h  |  4 ++--
 hw/xen_common.h   |  2 ++
 hw/xen_console.c  |  4 ++--
 hw/xen_disk.c     | 24 ++++++++++++------------
 i386-dm/helper2.c | 19 ++++++++++---------
 xen-hooks.mak     |  7 +++++++
 7 files changed, 50 insertions(+), 40 deletions(-)

-- 
2.1.4

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN-TRADITIONAL v8 1/4] qemu-xen-traditional: Use libxenevtchn
  2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 2/4] qemu-xen-traditional: Use libxengnttab Ian Campbell
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

/dev/xen/evtchn related wrappers have been moved out of libxenctrl
into their own library.

Note that i386-dm/helper2.c's xc_interface * was always really an
xc_evtchn *, it's just they used to be typedefs to the same thing...

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v3: Library moved to tools/libs/
---
 hw/xen_backend.c  | 26 +++++++++++++-------------
 hw/xen_backend.h  |  2 +-
 hw/xen_common.h   |  1 +
 i386-dm/helper2.c | 19 ++++++++++---------
 xen-hooks.mak     |  2 ++
 5 files changed, 27 insertions(+), 23 deletions(-)

diff --git a/hw/xen_backend.c b/hw/xen_backend.c
index 92b3506..40f6887 100644
--- a/hw/xen_backend.c
+++ b/hw/xen_backend.c
@@ -208,19 +208,19 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     xendev->debug      = debug;
     xendev->local_port = -1;
 
-    xendev->evtchndev = xc_evtchn_open(NULL, 0);
+    xendev->evtchndev = xenevtchn_open(NULL, 0);
     if (xendev->evtchndev == NULL) {
 	xen_be_printf(NULL, 0, "can't open evtchn device\n");
 	qemu_free(xendev);
 	return NULL;
     }
-    fcntl(xc_evtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
+    fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
 	xendev->gnttabdev = xc_gnttab_open(NULL, 0);
 	if (xendev->gnttabdev == NULL) {
 	    xen_be_printf(NULL, 0, "can't open gnttab device\n");
-	    xc_evtchn_close(xendev->evtchndev);
+	    xenevtchn_close(xendev->evtchndev);
 	    qemu_free(xendev);
 	    return NULL;
 	}
@@ -268,7 +268,7 @@ static struct XenDevice *xen_be_del_xendev(int dom, int dev)
 	}
 
 	if (xendev->evtchndev != NULL)
-	    xc_evtchn_close(xendev->evtchndev);
+	    xenevtchn_close(xendev->evtchndev);
 	if (xendev->gnttabdev != NULL)
 	    xc_gnttab_close(xendev->gnttabdev);
 
@@ -630,13 +630,13 @@ static void xen_be_evtchn_event(void *opaque)
     struct XenDevice *xendev = opaque;
     evtchn_port_t port;
 
-    port = xc_evtchn_pending(xendev->evtchndev);
+    port = xenevtchn_pending(xendev->evtchndev);
     if (port != xendev->local_port) {
-	xen_be_printf(xendev, 0, "xc_evtchn_pending returned %d (expected %d)\n",
+	xen_be_printf(xendev, 0, "xenevtchn_pending returned %d (expected %d)\n",
 		      port, xendev->local_port);
 	return;
     }
-    xc_evtchn_unmask(xendev->evtchndev, port);
+    xenevtchn_unmask(xendev->evtchndev, port);
 
     if (xendev->ops->event)
 	xendev->ops->event(xendev);
@@ -683,14 +683,14 @@ int xen_be_bind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port != -1)
 	return 0;
-    xendev->local_port = xc_evtchn_bind_interdomain
+    xendev->local_port = xenevtchn_bind_interdomain
 	(xendev->evtchndev, xendev->dom, xendev->remote_port);
     if (xendev->local_port == -1) {
-	xen_be_printf(xendev, 0, "xc_evtchn_bind_interdomain failed\n");
+	xen_be_printf(xendev, 0, "xenevtchn_bind_interdomain failed\n");
 	return -1;
     }
     xen_be_printf(xendev, 2, "bind evtchn port %d\n", xendev->local_port);
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev),
+    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev),
 			xen_be_evtchn_event, NULL, xendev);
     return 0;
 }
@@ -699,15 +699,15 @@ void xen_be_unbind_evtchn(struct XenDevice *xendev)
 {
     if (xendev->local_port == -1)
 	return;
-    qemu_set_fd_handler(xc_evtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
-    xc_evtchn_unbind(xendev->evtchndev, xendev->local_port);
+    qemu_set_fd_handler(xenevtchn_fd(xendev->evtchndev), NULL, NULL, NULL);
+    xenevtchn_unbind(xendev->evtchndev, xendev->local_port);
     xen_be_printf(xendev, 2, "unbind evtchn port %d\n", xendev->local_port);
     xendev->local_port = -1;
 }
 
 int xen_be_send_notify(struct XenDevice *xendev)
 {
-    return xc_evtchn_notify(xendev->evtchndev, xendev->local_port);
+    return xenevtchn_notify(xendev->evtchndev, xendev->local_port);
 }
 
 /*
diff --git a/hw/xen_backend.h b/hw/xen_backend.h
index e421391..60f18f8 100644
--- a/hw/xen_backend.h
+++ b/hw/xen_backend.h
@@ -44,7 +44,7 @@ struct XenDevice {
     int                remote_port;
     int                local_port;
 
-    xc_evtchn          *evtchndev;
+    xenevtchn_handle   *evtchndev;
     xc_gnttab          *gnttabdev;
 
     struct XenDevOps   *ops;
diff --git a/hw/xen_common.h b/hw/xen_common.h
index a615052..cee908f 100644
--- a/hw/xen_common.h
+++ b/hw/xen_common.h
@@ -4,6 +4,7 @@
 #include <stddef.h>
 #include <inttypes.h>
 
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xenstore.h>
 #include <xen/io/xenbus.h>
diff --git a/i386-dm/helper2.c b/i386-dm/helper2.c
index ede3105..2706f2e 100644
--- a/i386-dm/helper2.c
+++ b/i386-dm/helper2.c
@@ -48,6 +48,7 @@
 #include <limits.h>
 #include <fcntl.h>
 
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xen/hvm/ioreq.h>
 #include <xen/hvm/hvm_info_table.h>
@@ -104,7 +105,7 @@ buffered_iopage_t *buffered_io_page = NULL;
 QEMUTimer *buffered_io_timer;
 
 /* the evtchn fd for polling */
-xc_interface *xce_handle = NULL;
+xenevtchn_handle *xce_handle = NULL;
 
 /* which vcpu we are serving */
 int send_vcpu = 0;
@@ -141,7 +142,7 @@ CPUX86State *cpu_x86_init(const char *cpu_model)
 
         cpu_single_env = env;
 
-        xce_handle = xc_evtchn_open(NULL, 0);
+        xce_handle = xenevtchn_open(NULL, 0);
         if (xce_handle == NULL) {
             perror("open");
             return NULL;
@@ -149,7 +150,7 @@ CPUX86State *cpu_x86_init(const char *cpu_model)
 
         /* FIXME: how about if we overflow the page here? */
         for (i = 0; i < vcpus; i++) {
-            rc = xc_evtchn_bind_interdomain(
+            rc = xenevtchn_bind_interdomain(
                 xce_handle, domid, shared_page->vcpu_ioreq[i].vp_eport);
             if (rc == -1) {
                 fprintf(logfile, "bind interdomain ioctl error %d\n", errno);
@@ -164,7 +165,7 @@ CPUX86State *cpu_x86_init(const char *cpu_model)
                     errno);
             return NULL;
         }
-        rc = xc_evtchn_bind_interdomain(xce_handle, domid, (uint32_t)bufioreq_evtchn);
+        rc = xenevtchn_bind_interdomain(xce_handle, domid, (uint32_t)bufioreq_evtchn);
         if (rc == -1) {
             fprintf(logfile, "bind interdomain ioctl error %d\n", errno);
             return NULL;
@@ -278,7 +279,7 @@ static ioreq_t *cpu_get_ioreq(void)
     int i;
     evtchn_port_t port;
 
-    port = xc_evtchn_pending(xce_handle);
+    port = xenevtchn_pending(xce_handle);
     if (port == bufioreq_local_port) {
         qemu_mod_timer(buffered_io_timer,
                 BUFFER_IO_MAX_DELAY + qemu_get_clock(rt_clock));
@@ -296,7 +297,7 @@ static ioreq_t *cpu_get_ioreq(void)
         }
 
         // unmask the wanted port again
-        xc_evtchn_unmask(xce_handle, port);
+        xenevtchn_unmask(xce_handle, port);
 
         //get the io packet from shared memory
         send_vcpu = i;
@@ -539,7 +540,7 @@ static void handle_buffered_io(void *opaque)
                 BUFFER_IO_MAX_DELAY + qemu_get_clock(rt_clock));
     } else {
         qemu_del_timer(buffered_io_timer);
-        xc_evtchn_unmask(xce_handle, bufioreq_local_port);
+        xenevtchn_unmask(xce_handle, bufioreq_local_port);
     }
 }
 
@@ -583,7 +584,7 @@ static void cpu_handle_ioreq(void *opaque)
 	}
 
         req->state = STATE_IORESP_READY;
-        xc_evtchn_notify(xce_handle, ioreq_local_port[send_vcpu]);
+        xenevtchn_notify(xce_handle, ioreq_local_port[send_vcpu]);
     }
 }
 
@@ -592,7 +593,7 @@ int xen_pause_requested;
 int main_loop(void)
 {
     CPUState *env = cpu_single_env;
-    int evtchn_fd = xce_handle == NULL ? -1 : xc_evtchn_fd(xce_handle);
+    int evtchn_fd = xce_handle == NULL ? -1 : xenevtchn_fd(xce_handle);
     char *qemu_file;
     fd_set fds;
 
diff --git a/xen-hooks.mak b/xen-hooks.mak
index bf7f027..18259d4 100644
--- a/xen-hooks.mak
+++ b/xen-hooks.mak
@@ -1,4 +1,5 @@
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/toollog/include
+CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/evtchn/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libxc/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/xenstore/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/include
@@ -18,6 +19,7 @@ endif
 
 CFLAGS += $(CMDLINE_CFLAGS)
 
+LIBS += -L$(XEN_ROOT)/tools/libs/evtchn -lxenevtchn
 LIBS += -L$(XEN_ROOT)/tools/libxc -lxenctrl -lxenguest
 LIBS += -L$(XEN_ROOT)/tools/xenstore -lxenstore
 LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN-TRADITIONAL v8 2/4] qemu-xen-traditional: Use libxengnttab
  2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 1/4] qemu-xen-traditional: Use libxenevtchn Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 3/4] qemu-xen-traditional: Add libxencall to rpath-link Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 4/4] qemu-xen-traditional: Add libxenforeignmemory " Ian Campbell
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

/dev/xen/gntdev related wrappers have been moved out of libxenctrl
into their own library.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v3: Library moved to tools/libs/

v7: s/gnttab_munmap/gnttab_unmap/ to reflect change to the library
API.
---
 hw/xen_backend.c |  4 ++--
 hw/xen_backend.h |  2 +-
 hw/xen_common.h  |  1 +
 hw/xen_console.c |  4 ++--
 hw/xen_disk.c    | 24 ++++++++++++------------
 xen-hooks.mak    |  2 ++
 6 files changed, 20 insertions(+), 17 deletions(-)

diff --git a/hw/xen_backend.c b/hw/xen_backend.c
index 40f6887..97d25da 100644
--- a/hw/xen_backend.c
+++ b/hw/xen_backend.c
@@ -217,7 +217,7 @@ static struct XenDevice *xen_be_get_xendev(const char *type, int dom, int dev,
     fcntl(xenevtchn_fd(xendev->evtchndev), F_SETFD, FD_CLOEXEC);
 
     if (ops->flags & DEVOPS_FLAG_NEED_GNTDEV) {
-	xendev->gnttabdev = xc_gnttab_open(NULL, 0);
+	xendev->gnttabdev = xengnttab_open(NULL, 0);
 	if (xendev->gnttabdev == NULL) {
 	    xen_be_printf(NULL, 0, "can't open gnttab device\n");
 	    xenevtchn_close(xendev->evtchndev);
@@ -270,7 +270,7 @@ static struct XenDevice *xen_be_del_xendev(int dom, int dev)
 	if (xendev->evtchndev != NULL)
 	    xenevtchn_close(xendev->evtchndev);
 	if (xendev->gnttabdev != NULL)
-	    xc_gnttab_close(xendev->gnttabdev);
+	    xengnttab_close(xendev->gnttabdev);
 
 	TAILQ_REMOVE(&xendevs, xendev, next);
 	qemu_free(xendev);
diff --git a/hw/xen_backend.h b/hw/xen_backend.h
index 60f18f8..ba1e12f 100644
--- a/hw/xen_backend.h
+++ b/hw/xen_backend.h
@@ -45,7 +45,7 @@ struct XenDevice {
     int                local_port;
 
     xenevtchn_handle   *evtchndev;
-    xc_gnttab          *gnttabdev;
+    xengnttab_handle          *gnttabdev;
 
     struct XenDevOps   *ops;
     TAILQ_ENTRY(XenDevice) next;
diff --git a/hw/xen_common.h b/hw/xen_common.h
index cee908f..cc48892 100644
--- a/hw/xen_common.h
+++ b/hw/xen_common.h
@@ -5,6 +5,7 @@
 #include <inttypes.h>
 
 #include <xenevtchn.h>
+#include <xengnttab.h>
 #include <xenctrl.h>
 #include <xenstore.h>
 #include <xen/io/xenbus.h>
diff --git a/hw/xen_console.c b/hw/xen_console.c
index 80beb31..0bb4469 100644
--- a/hw/xen_console.c
+++ b/hw/xen_console.c
@@ -230,7 +230,7 @@ static int con_initialise(struct XenDevice *xendev)
                                           PROT_READ|PROT_WRITE,
                                           con->ring_ref);
     else
-        con->sring = xc_gnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
+        con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
                                              con->ring_ref,
                                              PROT_READ|PROT_WRITE);
     if (!con->sring)
@@ -263,7 +263,7 @@ static void con_disconnect(struct XenDevice *xendev)
         if (!xendev->gnttabdev)
 	    munmap(con->sring, XC_PAGE_SIZE);
         else
-            xc_gnttab_munmap(xendev->gnttabdev, con->sring, 1);
+            xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
 	con->sring = NULL;
     }
 }
diff --git a/hw/xen_disk.c b/hw/xen_disk.c
index 250d806..bc78748 100644
--- a/hw/xen_disk.c
+++ b/hw/xen_disk.c
@@ -266,7 +266,7 @@ err:
 
 static void ioreq_unmap(struct ioreq *ioreq)
 {
-    xc_gnttab *gnt = ioreq->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = ioreq->blkdev->xendev.gnttabdev;
     int i;
 
     if (ioreq->v.niov == 0)
@@ -274,8 +274,8 @@ static void ioreq_unmap(struct ioreq *ioreq)
     if (batch_maps) {
 	if (!ioreq->pages)
 	    return;
-	if (xc_gnttab_munmap(gnt, ioreq->pages, ioreq->v.niov) != 0)
-	    xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
+	if (xengnttab_unmap(gnt, ioreq->pages, ioreq->v.niov) != 0)
+	    xen_be_printf(&ioreq->blkdev->xendev, 0, "xengnttab_unmap failed: %s\n",
 			  strerror(errno));
 	ioreq->blkdev->cnt_map -= ioreq->v.niov;
 	ioreq->pages = NULL;
@@ -283,8 +283,8 @@ static void ioreq_unmap(struct ioreq *ioreq)
 	for (i = 0; i < ioreq->v.niov; i++) {
 	    if (!ioreq->page[i])
 		continue;
-	    if (xc_gnttab_munmap(gnt, ioreq->page[i], 1) != 0)
-		xen_be_printf(&ioreq->blkdev->xendev, 0, "xc_gnttab_munmap failed: %s\n",
+	    if (xengnttab_unmap(gnt, ioreq->page[i], 1) != 0)
+		xen_be_printf(&ioreq->blkdev->xendev, 0, "xengnttab_unmap failed: %s\n",
 			      strerror(errno));
 	    ioreq->blkdev->cnt_map--;
 	    ioreq->page[i] = NULL;
@@ -294,13 +294,13 @@ static void ioreq_unmap(struct ioreq *ioreq)
 
 static int ioreq_map(struct ioreq *ioreq)
 {
-    xc_gnttab *gnt = ioreq->blkdev->xendev.gnttabdev;
+    xengnttab_handle *gnt = ioreq->blkdev->xendev.gnttabdev;
     int i;
 
     if (ioreq->v.niov == 0)
         return 0;
     if (batch_maps) {
-	ioreq->pages = xc_gnttab_map_grant_refs
+	ioreq->pages = xengnttab_map_grant_refs
 	    (gnt, ioreq->v.niov, ioreq->domids, ioreq->refs, ioreq->prot);
 	if (ioreq->pages == NULL) {
 	    xen_be_printf(&ioreq->blkdev->xendev, 0,
@@ -314,7 +314,7 @@ static int ioreq_map(struct ioreq *ioreq)
 	ioreq->blkdev->cnt_map += ioreq->v.niov;
     } else  {
 	for (i = 0; i < ioreq->v.niov; i++) {
-	    ioreq->page[i] = xc_gnttab_map_grant_ref
+	    ioreq->page[i] = xengnttab_map_grant_ref
 		(gnt, ioreq->domids[i], ioreq->refs[i], ioreq->prot);
 	    if (ioreq->page[i] == NULL) {
 		xen_be_printf(&ioreq->blkdev->xendev, 0,
@@ -611,9 +611,9 @@ static void blk_alloc(struct XenDevice *xendev)
     blkdev->bh = qemu_bh_new(blk_bh, blkdev);
     if (xen_mode != XEN_EMULATE)
         batch_maps = 1;
-    if (xc_gnttab_set_max_grants(xendev->gnttabdev,
+    if (xengnttab_set_max_grants(xendev->gnttabdev,
             MAX_GRANTS(max_requests, BLKIF_MAX_SEGMENTS_PER_REQUEST)) < 0) {
-        xen_be_printf(xendev, 0, "xc_gnttab_set_max_grants failed: %s\n",
+        xen_be_printf(xendev, 0, "xengnttab_set_max_grants failed: %s\n",
                       strerror(errno));
     }
 }
@@ -734,7 +734,7 @@ static int blk_connect(struct XenDevice *xendev)
             blkdev->protocol = BLKIF_PROTOCOL_X86_64;
     }
 
-    blkdev->sring = xc_gnttab_map_grant_ref(blkdev->xendev.gnttabdev,
+    blkdev->sring = xengnttab_map_grant_ref(blkdev->xendev.gnttabdev,
 					    blkdev->xendev.dom,
 					    blkdev->ring_ref,
 					    PROT_READ | PROT_WRITE);
@@ -787,7 +787,7 @@ static void blk_disconnect(struct XenDevice *xendev)
     xen_be_unbind_evtchn(&blkdev->xendev);
 
     if (blkdev->sring) {
-	xc_gnttab_munmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
+	xengnttab_unmap(blkdev->xendev.gnttabdev, blkdev->sring, 1);
 	blkdev->cnt_map--;
 	blkdev->sring = NULL;
     }
diff --git a/xen-hooks.mak b/xen-hooks.mak
index 18259d4..179a6b7 100644
--- a/xen-hooks.mak
+++ b/xen-hooks.mak
@@ -1,5 +1,6 @@
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/toollog/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/evtchn/include
+CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/gnttab/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libxc/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/xenstore/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/include
@@ -20,6 +21,7 @@ endif
 CFLAGS += $(CMDLINE_CFLAGS)
 
 LIBS += -L$(XEN_ROOT)/tools/libs/evtchn -lxenevtchn
+LIBS += -L$(XEN_ROOT)/tools/libs/gnttab -lxengnttab
 LIBS += -L$(XEN_ROOT)/tools/libxc -lxenctrl -lxenguest
 LIBS += -L$(XEN_ROOT)/tools/xenstore -lxenstore
 LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN-TRADITIONAL v8 3/4] qemu-xen-traditional: Add libxencall to rpath-link
  2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 1/4] qemu-xen-traditional: Use libxenevtchn Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 2/4] qemu-xen-traditional: Use libxengnttab Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 4/4] qemu-xen-traditional: Add libxenforeignmemory " Ian Campbell
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

libxenctrl links against this library

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v3: Library moved to tools/libs
---
 xen-hooks.mak | 1 +
 1 file changed, 1 insertion(+)

diff --git a/xen-hooks.mak b/xen-hooks.mak
index 179a6b7..229d642 100644
--- a/xen-hooks.mak
+++ b/xen-hooks.mak
@@ -25,6 +25,7 @@ LIBS += -L$(XEN_ROOT)/tools/libs/gnttab -lxengnttab
 LIBS += -L$(XEN_ROOT)/tools/libxc -lxenctrl -lxenguest
 LIBS += -L$(XEN_ROOT)/tools/xenstore -lxenstore
 LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog
+LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/call
 
 LDFLAGS := $(CFLAGS) $(LDFLAGS)
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH QEMU-XEN-TRADITIONAL v8 4/4] qemu-xen-traditional: Add libxenforeignmemory to rpath-link
  2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
                     ` (2 preceding siblings ...)
  2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 3/4] qemu-xen-traditional: Add libxencall to rpath-link Ian Campbell
@ 2016-01-15 13:23   ` Ian Campbell
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel; +Cc: Ian Campbell

libxenctrl links against this library.

Also, request the compat xc_map_foreign API from libxc.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---
v3: Library moved to tools/libs/
---
 xen-hooks.mak | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/xen-hooks.mak b/xen-hooks.mak
index 229d642..c1ea4be 100644
--- a/xen-hooks.mak
+++ b/xen-hooks.mak
@@ -1,6 +1,7 @@
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/toollog/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/evtchn/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libs/gnttab/include
+CPPFLAGS+= -DXC_WANT_COMPAT_MAP_FOREIGN_API
 CPPFLAGS+= -I$(XEN_ROOT)/tools/libxc/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/xenstore/include
 CPPFLAGS+= -I$(XEN_ROOT)/tools/include
@@ -26,6 +27,7 @@ LIBS += -L$(XEN_ROOT)/tools/libxc -lxenctrl -lxenguest
 LIBS += -L$(XEN_ROOT)/tools/xenstore -lxenstore
 LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog
 LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/call
+LIBS += -Wl,-rpath-link=$(XEN_ROOT)/tools/libs/foreignmemory
 
 LDFLAGS := $(CFLAGS) $(LDFLAGS)
 
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (3 preceding siblings ...)
  2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
@ 2016-01-15 13:23 ` Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 1/4] mini-os: Include libxenevtchn with libxc Ian Campbell
                     ` (3 more replies)
  2016-01-19 15:44 ` [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (3 subsequent siblings)
  8 siblings, 4 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:23 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: minios-devel, samuel.thibault, Ian Campbell

We intend to stabilise some parts of the libxenctrl interface by
splitting out some functionality into separate stable libraries.

This is the mini-os part of the first phase of that change.

This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
super-intro mail covering all of the related patch series and which
contains more details.

Ian Campbell (4):
  mini-os: Include libxenevtchn with libxc
  mini-os: Include libxengnttab with libxc
  mini-os: Include libxencall with libxc
  mini-os: Include libxenforeignmemory with libxc

 Makefile | 4 ++++
 1 file changed, 4 insertions(+)

-- 
2.1.4

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [PATCH MINI-OS v8 1/4] mini-os: Include libxenevtchn with libxc
  2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
@ 2016-01-15 13:24   ` Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 2/4] mini-os: Include libxengnttab " Ian Campbell
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:24 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: minios-devel, samuel.thibault, Ian Campbell

libxenevtchn has just been split out from libxc. From mini-os's point
of view we don't care about the distinction, so keep things simple by
just including libxenevtchn if libxc is enabled.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
v2: Adjust for libs/$lib layout.
---
 Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Makefile b/Makefile
index daee46c..d1d8dc4 100644
--- a/Makefile
+++ b/Makefile
@@ -166,6 +166,7 @@ OBJS := $(filter-out $(OBJ_DIR)/lwip%.o $(LWO), $(OBJS))
 ifeq ($(libc),y)
 ifeq ($(CONFIG_XC),y)
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/toollog -whole-archive -lxentoollog -no-whole-archive
+APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/evtchn -whole-archive -lxenevtchn -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libxc-$(MINIOS_TARGET_ARCH) -whole-archive -lxenguest -lxenctrl -no-whole-archive
 endif
 APP_LDLIBS += -lpci
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH MINI-OS v8 2/4] mini-os: Include libxengnttab with libxc
  2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 1/4] mini-os: Include libxenevtchn with libxc Ian Campbell
@ 2016-01-15 13:24   ` Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 3/4] mini-os: Include libxencall " Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 4/4] mini-os: Include libxenforeignmemory " Ian Campbell
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:24 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: minios-devel, samuel.thibault, Ian Campbell

libxengnttab has just been split out from libxc. From mini-os's point
of view we don't care about the distinction, so keep things simple by
just including libxengnttab if libxc is enabled.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
v2: Adjust for libs/$lib layout.
---
 Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Makefile b/Makefile
index d1d8dc4..521f647 100644
--- a/Makefile
+++ b/Makefile
@@ -167,6 +167,7 @@ ifeq ($(libc),y)
 ifeq ($(CONFIG_XC),y)
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/toollog -whole-archive -lxentoollog -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/evtchn -whole-archive -lxenevtchn -no-whole-archive
+APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/gnttab -whole-archive -lxengnttab -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libxc-$(MINIOS_TARGET_ARCH) -whole-archive -lxenguest -lxenctrl -no-whole-archive
 endif
 APP_LDLIBS += -lpci
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH MINI-OS v8 3/4] mini-os: Include libxencall with libxc
  2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 1/4] mini-os: Include libxenevtchn with libxc Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 2/4] mini-os: Include libxengnttab " Ian Campbell
@ 2016-01-15 13:24   ` Ian Campbell
  2016-01-15 13:24   ` [PATCH MINI-OS v8 4/4] mini-os: Include libxenforeignmemory " Ian Campbell
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:24 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: minios-devel, samuel.thibault, Ian Campbell

libxencall has just been split out from libxc. From mini-os's point
of view we don't care about the distinction, so keep things simple by
just including libxencall if libxc is enabled.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
v2: Adjust for libs/$lib layout.
---
 Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Makefile b/Makefile
index 521f647..c900540 100644
--- a/Makefile
+++ b/Makefile
@@ -168,6 +168,7 @@ ifeq ($(CONFIG_XC),y)
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/toollog -whole-archive -lxentoollog -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/evtchn -whole-archive -lxenevtchn -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/gnttab -whole-archive -lxengnttab -no-whole-archive
+APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/call -whole-archive -lxencall -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libxc-$(MINIOS_TARGET_ARCH) -whole-archive -lxenguest -lxenctrl -no-whole-archive
 endif
 APP_LDLIBS += -lpci
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH MINI-OS v8 4/4] mini-os: Include libxenforeignmemory with libxc
  2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                     ` (2 preceding siblings ...)
  2016-01-15 13:24   ` [PATCH MINI-OS v8 3/4] mini-os: Include libxencall " Ian Campbell
@ 2016-01-15 13:24   ` Ian Campbell
  3 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:24 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel
  Cc: minios-devel, samuel.thibault, Ian Campbell

libxenforeignmemory has just been split out from libxc. From mini-os's
point of view we don't care about the distinction, so keep things
simple by just including libxenforeignmemory if libxc is enabled.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Samuel Thibault <samuel.thibault@ens-lyon.org>
Acked-by: Wei Liu <wei.liu2@citrix.com>
---
v2: Adjust for libs/$lib layout.
---
 Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/Makefile b/Makefile
index c900540..cfe015a 100644
--- a/Makefile
+++ b/Makefile
@@ -169,6 +169,7 @@ APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/toollog -whole-ar
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/evtchn -whole-archive -lxenevtchn -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/gnttab -whole-archive -lxengnttab -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/call -whole-archive -lxencall -no-whole-archive
+APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libs-$(MINIOS_TARGET_ARCH)/foreignmemory -whole-archive -lxenforeignmemory -no-whole-archive
 APP_LDLIBS += -L$(XEN_ROOT)/stubdom/libxc-$(MINIOS_TARGET_ARCH) -whole-archive -lxenguest -lxenctrl -no-whole-archive
 endif
 APP_LDLIBS += -lpci
-- 
2.1.4

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [PATCH QEMU-XEN v8 5/8] xen: Switch uses of xc_map_foreign_{pages, bulk} to use libxenforeignmemory API.
  2016-01-15 13:23     ` Ian Campbell
  (?)
@ 2016-01-15 14:43     ` Stefano Stabellini
  -1 siblings, 0 replies; 113+ messages in thread
From: Stefano Stabellini @ 2016-01-15 14:43 UTC (permalink / raw)
  To: Ian Campbell
  Cc: wei.liu2, stefano.stabellini, ian.jackson, qemu-devel, xen-devel

On Fri, 15 Jan 2016, Ian Campbell wrote:
> In Xen 4.7 we are refactoring parts libxenctrl into a number of
> separate libraries which will provide backward and forward API and ABI
> compatiblity.
> 
> One such library will be libxenforeignmemory which provides access to
> privileged foreign mappings and which will provide an interface
> equivalent to xc_map_foreign_{pages,bulk}.
> 
> The new xenforeignmemory_map() function behaves like
> xc_map_foreign_pages() when the err argument is NULL and like
> xc_map_foreign_bulk() when err is non-NULL, which maps into the shim
> here onto checking err == NULL and calling the appropriate old
> function.
> 
> Note that xenforeignmemory_map() takes the number of pages before the
> arrays themselves, in order to support potentially future use of
> variable-length-arrays in the prototype (in the future, when Xen's
> baseline toolchain requirements are new enough to ensure VLAs are
> supported).
> 
> In preparation for adding support for libxenforeignmemory add support
> to the <=4.0 and <=4.6 compat code in xen_common.h to allow us to
> switch to using the new API. These shims will disappear for versions
> of Xen which include libxenforeignmemory.
> 
> Since libxenforeignmemory will have its own handle type but for <= 4.6
> the functionality is provided by using a libxenctrl handle we
> introduce a new global xen_fmem alongside the existing xen_xc. In fact
> we make xen_fmem a pointer to the existing xen_xc, which then works
> correctly with both <=4.0 (xc handle is an int) and <=4.6 (xc handle
> is a pointer). In the latter case xen_fmem is actually a double
> indirect pointer, but it all falls out in the wash.
> 
> Unlike libxenctrl libxenforeignmemory has an explicit unmap function,
> rather than just specifying that munmap should be used, so the unmap
> paths are updated to use xenforeignmemory_unmap, which is a shim for
> munmap on these versions of xen. The mappings in xen-hvm.c do not
> appear to be unmapped (which makes sense for a qemu-dm process)
> 
> In fb_disconnect this results in a change from simply mmap over the
> existing mapping (with an implicit munmap) to expliclty unmapping with
> xenforeignmemory_unmap and then mapping the required anonymous memory
> in the same hole. I don't think this is a problem since any other
> thread which was racily touching this region would already be running
> the risk of hitting the mapping halfway through the call. If this is
> thought to be a problem then we could consider adding an extra API to
> the libxenforeignmemory interface to replace a foreign mapping with
> anonymous shared memory, but I'd prefer not to.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>


> ---
> v8: Move two copies of compat xenforeignmemory_{open,unmap} to the
>     common location. Guard with version 471 (which will be added in
>     the next patch)
> v7: Move two copies of compat xenforeignmemory_map to a common location.
>     Note that the existing xen_domain_create #ifdef will be covered by
>     a CONFIG_XEN_PV_DOMAIN_BUILD in a later patch, and so is not a
>     suitable place to put this function.
> v6: Handle _pages as well as _bulk, due to changes in the previous
>     patches, including the dropping of "xen: Switch uses of
>     xc_map_foreign_pages into xc_map_foreign_bulk" which previous preceded
>     this patch and the change of "xen: Switch uses of
>     xc_map_foreign_range into xc_map_foreign_bulk" into "xen: Switch
>     uses of xc_map_foreign_range into xc_map_foreign_pages".
> 
>     Handle reordering of arguments to xenforeignmemory_map().
> 
>     Dropped Stefano's Reviewed-by due to these changes.
> 
> v4: Rebase onto "xen_console: correctly cleanup primary console on
>     teardown."
> 
>     xenforeignmemory_unmap takes pages not bytes
> 
>     Compat wrapper for xenforeignmemory_open instead of ifdef in code.
> 
>     Run check patch and fix most issues. I did not fix:
> 
> ERROR: do not initialise globals to 0 or NULL
> +xenforeignmemory_handle *xen_fmem = NULL;
> 
> => This is consistent with all of the existing declarations.
> 
> ERROR: need consistent spacing around '*' (ctx:WxV)
> +typedef xc_interface *xenforeignmemory_handle;
> 
> => I think this is a false +ve since this is a pointer "*" not a multiple "*".
> 
> reorder args to xenforeignmemory_map
>
> ---
>
>  hw/char/xen_console.c        |  8 ++++----
>  hw/display/xenfb.c           | 17 +++++++++--------
>  hw/xen/xen_backend.c         |  3 ++-
>  include/hw/xen/xen_backend.h |  1 +
>  include/hw/xen/xen_common.h  | 25 +++++++++++++++++++++++++
>  xen-common.c                 |  6 ++++++
>  xen-hvm.c                    | 12 ++++++------
>  xen-mapcache.c               |  6 +++---
>  8 files changed, 56 insertions(+), 22 deletions(-)
> 
> diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
> index 3e8a57b..b92d0c6 100644
> --- a/hw/char/xen_console.c
> +++ b/hw/char/xen_console.c
> @@ -229,9 +229,9 @@ static int con_initialise(struct XenDevice *xendev)
>  
>      if (!xendev->dev) {
>          xen_pfn_t mfn = con->ring_ref;
> -        con->sring = xc_map_foreign_pages(xen_xc, con->xendev.dom,
> -                                         PROT_READ|PROT_WRITE,
> -                                         &mfn, 1);
> +        con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
> +                                          PROT_READ|PROT_WRITE,
> +                                          1, &mfn, NULL);
>      } else {
>          con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
>                                               con->ring_ref,
> @@ -273,7 +273,7 @@ static void con_disconnect(struct XenDevice *xendev)
>  
>      if (con->sring) {
>          if (!xendev->dev) {
> -            munmap(con->sring, XC_PAGE_SIZE);
> +            xenforeignmemory_unmap(xen_fmem, con->sring, 1);
>          } else {
>              xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
>          }
> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
> index bb8f6b3..a42971c 100644
> --- a/hw/display/xenfb.c
> +++ b/hw/display/xenfb.c
> @@ -106,8 +106,8 @@ static int common_bind(struct common *c)
>      if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
>  	return -1;
>  
> -    c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom,
> -                                   PROT_READ | PROT_WRITE, &mfn, 1);
> +    c->page = xenforeignmemory_map(xen_fmem, c->xendev.dom,
> +                                   PROT_READ | PROT_WRITE, 1, &mfn, NULL);
>      if (c->page == NULL)
>  	return -1;
>  
> @@ -122,7 +122,7 @@ static void common_unbind(struct common *c)
>  {
>      xen_be_unbind_evtchn(&c->xendev);
>      if (c->page) {
> -	munmap(c->page, XC_PAGE_SIZE);
> +        xenforeignmemory_unmap(xen_fmem, c->page, 1);
>  	c->page = NULL;
>      }
>  }
> @@ -495,15 +495,15 @@ static int xenfb_map_fb(struct XenFB *xenfb)
>      fbmfns = g_malloc0(sizeof(xen_pfn_t) * xenfb->fbpages);
>  
>      xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
> -    map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
> -			       PROT_READ, pgmfns, n_fbdirs);
> +    map = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
> +                               PROT_READ, n_fbdirs, pgmfns, NULL);
>      if (map == NULL)
>  	goto out;
>      xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
> -    munmap(map, n_fbdirs * XC_PAGE_SIZE);
> +    xenforeignmemory_unmap(xen_fmem, map, n_fbdirs);
>  
> -    xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
> -            PROT_READ, fbmfns, xenfb->fbpages);
> +    xenfb->pixels = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
> +            PROT_READ, xenfb->fbpages, fbmfns, NULL);
>      if (xenfb->pixels == NULL)
>  	goto out;
>  
> @@ -912,6 +912,7 @@ static void fb_disconnect(struct XenDevice *xendev)
>       *   Replacing the framebuffer with anonymous shared memory
>       *   instead.  This releases the guest pages and keeps qemu happy.
>       */
> +    xenforeignmemory_unmap(xen_fmem, fb->pixels, fb->fbpages);
>      fb->pixels = mmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE,
>                        PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
>                        -1, 0);
> diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
> index 966e34f..caa459c 100644
> --- a/hw/xen/xen_backend.c
> +++ b/hw/xen/xen_backend.c
> @@ -45,6 +45,7 @@
>  
>  /* public */
>  XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
> +xenforeignmemory_handle *xen_fmem = NULL;
>  struct xs_handle *xenstore = NULL;
>  const char *xen_protocol;
>  
> @@ -717,7 +718,7 @@ int xen_be_init(void)
>  
>      qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
>  
> -    if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
> +    if (xen_xc == XC_HANDLER_INITIAL_VALUE || xen_fmem == NULL) {
>          /* Check if xen_init() have been called */
>          goto err;
>      }
> diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
> index 8e8857b..e0d52ee 100644
> --- a/include/hw/xen/xen_backend.h
> +++ b/include/hw/xen/xen_backend.h
> @@ -57,6 +57,7 @@ struct XenDevice {
>  
>  /* variables */
>  extern XenXC xen_xc;
> +extern xenforeignmemory_handle *xen_fmem;
>  extern struct xs_handle *xenstore;
>  extern const char *xen_protocol;
>  
> diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
> index 8f38310..95275b3 100644
> --- a/include/hw/xen/xen_common.h
> +++ b/include/hw/xen/xen_common.h
> @@ -41,6 +41,7 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
>  typedef int XenXC;
>  typedef int xenevtchn_handle;
>  typedef int xengnttab_handle;
> +typedef int xenforeignmemory_handle;
>  
>  #  define XC_INTERFACE_FMT "%i"
>  #  define XC_HANDLER_INITIAL_VALUE    -1
> @@ -104,6 +105,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
>      return xc_interface_open();
>  }
>  
> +/* See below for xenforeignmemory_* APIs */
> +
>  static inline int xc_fd(int xen_xc)
>  {
>      return xen_xc;
> @@ -149,6 +152,7 @@ static inline void xs_close(struct xs_handle *xsh)
>  #else
>  
>  typedef xc_interface *XenXC;
> +typedef xc_interface *xenforeignmemory_handle;
>  typedef xc_evtchn xenevtchn_handle;
>  typedef xc_gnttab xengnttab_handle;
>  
> @@ -178,6 +182,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
>      return xc_interface_open(logger, dombuild_logger, open_flags);
>  }
>  
> +/* See below for xenforeignmemory_* APIs */
> +
>  /* FIXME There is now way to have the xen fd */
>  static inline int xc_fd(xc_interface *xen_xc)
>  {
> @@ -501,4 +507,23 @@ static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
>  }
>  #endif
>  
> +#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
> +
> +#define xenforeignmemory_open(l, f) &xen_xc
> +
> +static inline void *xenforeignmemory_map(XenXC *h, uint32_t dom,
> +                                         int prot, size_t pages,
> +                                         const xen_pfn_t arr[/*pages*/],
> +                                         int err[/*pages*/])
> +{
> +    if (err)
> +        return xc_map_foreign_bulk(*h, dom, prot, arr, err, pages);
> +    else
> +        return xc_map_foreign_pages(*h, dom, prot, arr, pages);
> +}
> +
> +#define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE)
> +
> +#endif
> +
>  #endif /* QEMU_HW_XEN_COMMON_H */
> diff --git a/xen-common.c b/xen-common.c
> index 0dcdbc3..6cd2959 100644
> --- a/xen-common.c
> +++ b/xen-common.c
> @@ -118,6 +118,12 @@ static int xen_init(MachineState *ms)
>          xen_be_printf(NULL, 0, "can't open xen interface\n");
>          return -1;
>      }
> +    xen_fmem = xenforeignmemory_open(0, 0);
> +    if (xen_fmem == NULL) {
> +        xen_be_printf(NULL, 0, "can't open xen fmem interface\n");
> +        xc_interface_close(xen_xc);
> +        return -1;
> +    }
>      qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
>  
>      global_state_set_optional();
> diff --git a/xen-hvm.c b/xen-hvm.c
> index 1f729f6..66ee835 100644
> --- a/xen-hvm.c
> +++ b/xen-hvm.c
> @@ -1247,9 +1247,9 @@ int xen_hvm_init(PCMachineState *pcms,
>      DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
>      DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
>  
> -    state->shared_page = xc_map_foreign_pages(xen_xc, xen_domid,
> +    state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
>                                                PROT_READ|PROT_WRITE,
> -                                              &ioreq_pfn, 1);
> +                                              1, &ioreq_pfn, NULL);
>      if (state->shared_page == NULL) {
>          hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
>                   errno, xen_xc);
> @@ -1259,8 +1259,8 @@ int xen_hvm_init(PCMachineState *pcms,
>      if (!rc) {
>          DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
>          state->shared_vmport_page =
> -            xc_map_foreign_pages(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
> -                                 &ioreq_pfn, 1);
> +            xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
> +                                 1, &ioreq_pfn, NULL);
>          if (state->shared_vmport_page == NULL) {
>              hw_error("map shared vmport IO page returned error %d handle="
>                       XC_INTERFACE_FMT, errno, xen_xc);
> @@ -1269,9 +1269,9 @@ int xen_hvm_init(PCMachineState *pcms,
>          hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
>      }
>  
> -    state->buffered_io_page = xc_map_foreign_pages(xen_xc, xen_domid,
> +    state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
>                                                     PROT_READ|PROT_WRITE,
> -                                                   &bufioreq_pfn, 1);
> +                                                   1, &bufioreq_pfn, NULL);
>      if (state->buffered_io_page == NULL) {
>          hw_error("map buffered IO page returned error %d", errno);
>      }
> diff --git a/xen-mapcache.c b/xen-mapcache.c
> index 97fece2..4a04378 100644
> --- a/xen-mapcache.c
> +++ b/xen-mapcache.c
> @@ -176,10 +176,10 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>          pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
>      }
>  
> -    vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
> -                                     pfns, err, nb_pfn);
> +    vaddr_base = xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
> +                                      nb_pfn, pfns, err);
>      if (vaddr_base == NULL) {
> -        perror("xc_map_foreign_bulk");
> +        perror("xenforeignmemory_map");
>          exit(-1);
>      }
>  
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH QEMU-XEN v8 5/8] xen: Switch uses of xc_map_foreign_{pages, bulk} to use libxenforeignmemory API.
  2016-01-15 13:23     ` Ian Campbell
  (?)
  (?)
@ 2016-01-15 14:43     ` Stefano Stabellini
  -1 siblings, 0 replies; 113+ messages in thread
From: Stefano Stabellini @ 2016-01-15 14:43 UTC (permalink / raw)
  To: Ian Campbell
  Cc: wei.liu2, stefano.stabellini, ian.jackson, qemu-devel, xen-devel

On Fri, 15 Jan 2016, Ian Campbell wrote:
> In Xen 4.7 we are refactoring parts libxenctrl into a number of
> separate libraries which will provide backward and forward API and ABI
> compatiblity.
> 
> One such library will be libxenforeignmemory which provides access to
> privileged foreign mappings and which will provide an interface
> equivalent to xc_map_foreign_{pages,bulk}.
> 
> The new xenforeignmemory_map() function behaves like
> xc_map_foreign_pages() when the err argument is NULL and like
> xc_map_foreign_bulk() when err is non-NULL, which maps into the shim
> here onto checking err == NULL and calling the appropriate old
> function.
> 
> Note that xenforeignmemory_map() takes the number of pages before the
> arrays themselves, in order to support potentially future use of
> variable-length-arrays in the prototype (in the future, when Xen's
> baseline toolchain requirements are new enough to ensure VLAs are
> supported).
> 
> In preparation for adding support for libxenforeignmemory add support
> to the <=4.0 and <=4.6 compat code in xen_common.h to allow us to
> switch to using the new API. These shims will disappear for versions
> of Xen which include libxenforeignmemory.
> 
> Since libxenforeignmemory will have its own handle type but for <= 4.6
> the functionality is provided by using a libxenctrl handle we
> introduce a new global xen_fmem alongside the existing xen_xc. In fact
> we make xen_fmem a pointer to the existing xen_xc, which then works
> correctly with both <=4.0 (xc handle is an int) and <=4.6 (xc handle
> is a pointer). In the latter case xen_fmem is actually a double
> indirect pointer, but it all falls out in the wash.
> 
> Unlike libxenctrl libxenforeignmemory has an explicit unmap function,
> rather than just specifying that munmap should be used, so the unmap
> paths are updated to use xenforeignmemory_unmap, which is a shim for
> munmap on these versions of xen. The mappings in xen-hvm.c do not
> appear to be unmapped (which makes sense for a qemu-dm process)
> 
> In fb_disconnect this results in a change from simply mmap over the
> existing mapping (with an implicit munmap) to expliclty unmapping with
> xenforeignmemory_unmap and then mapping the required anonymous memory
> in the same hole. I don't think this is a problem since any other
> thread which was racily touching this region would already be running
> the risk of hitting the mapping halfway through the call. If this is
> thought to be a problem then we could consider adding an extra API to
> the libxenforeignmemory interface to replace a foreign mapping with
> anonymous shared memory, but I'd prefer not to.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>


> ---
> v8: Move two copies of compat xenforeignmemory_{open,unmap} to the
>     common location. Guard with version 471 (which will be added in
>     the next patch)
> v7: Move two copies of compat xenforeignmemory_map to a common location.
>     Note that the existing xen_domain_create #ifdef will be covered by
>     a CONFIG_XEN_PV_DOMAIN_BUILD in a later patch, and so is not a
>     suitable place to put this function.
> v6: Handle _pages as well as _bulk, due to changes in the previous
>     patches, including the dropping of "xen: Switch uses of
>     xc_map_foreign_pages into xc_map_foreign_bulk" which previous preceded
>     this patch and the change of "xen: Switch uses of
>     xc_map_foreign_range into xc_map_foreign_bulk" into "xen: Switch
>     uses of xc_map_foreign_range into xc_map_foreign_pages".
> 
>     Handle reordering of arguments to xenforeignmemory_map().
> 
>     Dropped Stefano's Reviewed-by due to these changes.
> 
> v4: Rebase onto "xen_console: correctly cleanup primary console on
>     teardown."
> 
>     xenforeignmemory_unmap takes pages not bytes
> 
>     Compat wrapper for xenforeignmemory_open instead of ifdef in code.
> 
>     Run check patch and fix most issues. I did not fix:
> 
> ERROR: do not initialise globals to 0 or NULL
> +xenforeignmemory_handle *xen_fmem = NULL;
> 
> => This is consistent with all of the existing declarations.
> 
> ERROR: need consistent spacing around '*' (ctx:WxV)
> +typedef xc_interface *xenforeignmemory_handle;
> 
> => I think this is a false +ve since this is a pointer "*" not a multiple "*".
> 
> reorder args to xenforeignmemory_map
>
> ---
>
>  hw/char/xen_console.c        |  8 ++++----
>  hw/display/xenfb.c           | 17 +++++++++--------
>  hw/xen/xen_backend.c         |  3 ++-
>  include/hw/xen/xen_backend.h |  1 +
>  include/hw/xen/xen_common.h  | 25 +++++++++++++++++++++++++
>  xen-common.c                 |  6 ++++++
>  xen-hvm.c                    | 12 ++++++------
>  xen-mapcache.c               |  6 +++---
>  8 files changed, 56 insertions(+), 22 deletions(-)
> 
> diff --git a/hw/char/xen_console.c b/hw/char/xen_console.c
> index 3e8a57b..b92d0c6 100644
> --- a/hw/char/xen_console.c
> +++ b/hw/char/xen_console.c
> @@ -229,9 +229,9 @@ static int con_initialise(struct XenDevice *xendev)
>  
>      if (!xendev->dev) {
>          xen_pfn_t mfn = con->ring_ref;
> -        con->sring = xc_map_foreign_pages(xen_xc, con->xendev.dom,
> -                                         PROT_READ|PROT_WRITE,
> -                                         &mfn, 1);
> +        con->sring = xenforeignmemory_map(xen_fmem, con->xendev.dom,
> +                                          PROT_READ|PROT_WRITE,
> +                                          1, &mfn, NULL);
>      } else {
>          con->sring = xengnttab_map_grant_ref(xendev->gnttabdev, con->xendev.dom,
>                                               con->ring_ref,
> @@ -273,7 +273,7 @@ static void con_disconnect(struct XenDevice *xendev)
>  
>      if (con->sring) {
>          if (!xendev->dev) {
> -            munmap(con->sring, XC_PAGE_SIZE);
> +            xenforeignmemory_unmap(xen_fmem, con->sring, 1);
>          } else {
>              xengnttab_unmap(xendev->gnttabdev, con->sring, 1);
>          }
> diff --git a/hw/display/xenfb.c b/hw/display/xenfb.c
> index bb8f6b3..a42971c 100644
> --- a/hw/display/xenfb.c
> +++ b/hw/display/xenfb.c
> @@ -106,8 +106,8 @@ static int common_bind(struct common *c)
>      if (xenstore_read_fe_int(&c->xendev, "event-channel", &c->xendev.remote_port) == -1)
>  	return -1;
>  
> -    c->page = xc_map_foreign_pages(xen_xc, c->xendev.dom,
> -                                   PROT_READ | PROT_WRITE, &mfn, 1);
> +    c->page = xenforeignmemory_map(xen_fmem, c->xendev.dom,
> +                                   PROT_READ | PROT_WRITE, 1, &mfn, NULL);
>      if (c->page == NULL)
>  	return -1;
>  
> @@ -122,7 +122,7 @@ static void common_unbind(struct common *c)
>  {
>      xen_be_unbind_evtchn(&c->xendev);
>      if (c->page) {
> -	munmap(c->page, XC_PAGE_SIZE);
> +        xenforeignmemory_unmap(xen_fmem, c->page, 1);
>  	c->page = NULL;
>      }
>  }
> @@ -495,15 +495,15 @@ static int xenfb_map_fb(struct XenFB *xenfb)
>      fbmfns = g_malloc0(sizeof(xen_pfn_t) * xenfb->fbpages);
>  
>      xenfb_copy_mfns(mode, n_fbdirs, pgmfns, pd);
> -    map = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
> -			       PROT_READ, pgmfns, n_fbdirs);
> +    map = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
> +                               PROT_READ, n_fbdirs, pgmfns, NULL);
>      if (map == NULL)
>  	goto out;
>      xenfb_copy_mfns(mode, xenfb->fbpages, fbmfns, map);
> -    munmap(map, n_fbdirs * XC_PAGE_SIZE);
> +    xenforeignmemory_unmap(xen_fmem, map, n_fbdirs);
>  
> -    xenfb->pixels = xc_map_foreign_pages(xen_xc, xenfb->c.xendev.dom,
> -            PROT_READ, fbmfns, xenfb->fbpages);
> +    xenfb->pixels = xenforeignmemory_map(xen_fmem, xenfb->c.xendev.dom,
> +            PROT_READ, xenfb->fbpages, fbmfns, NULL);
>      if (xenfb->pixels == NULL)
>  	goto out;
>  
> @@ -912,6 +912,7 @@ static void fb_disconnect(struct XenDevice *xendev)
>       *   Replacing the framebuffer with anonymous shared memory
>       *   instead.  This releases the guest pages and keeps qemu happy.
>       */
> +    xenforeignmemory_unmap(xen_fmem, fb->pixels, fb->fbpages);
>      fb->pixels = mmap(fb->pixels, fb->fbpages * XC_PAGE_SIZE,
>                        PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON,
>                        -1, 0);
> diff --git a/hw/xen/xen_backend.c b/hw/xen/xen_backend.c
> index 966e34f..caa459c 100644
> --- a/hw/xen/xen_backend.c
> +++ b/hw/xen/xen_backend.c
> @@ -45,6 +45,7 @@
>  
>  /* public */
>  XenXC xen_xc = XC_HANDLER_INITIAL_VALUE;
> +xenforeignmemory_handle *xen_fmem = NULL;
>  struct xs_handle *xenstore = NULL;
>  const char *xen_protocol;
>  
> @@ -717,7 +718,7 @@ int xen_be_init(void)
>  
>      qemu_set_fd_handler(xs_fileno(xenstore), xenstore_update, NULL, NULL);
>  
> -    if (xen_xc == XC_HANDLER_INITIAL_VALUE) {
> +    if (xen_xc == XC_HANDLER_INITIAL_VALUE || xen_fmem == NULL) {
>          /* Check if xen_init() have been called */
>          goto err;
>      }
> diff --git a/include/hw/xen/xen_backend.h b/include/hw/xen/xen_backend.h
> index 8e8857b..e0d52ee 100644
> --- a/include/hw/xen/xen_backend.h
> +++ b/include/hw/xen/xen_backend.h
> @@ -57,6 +57,7 @@ struct XenDevice {
>  
>  /* variables */
>  extern XenXC xen_xc;
> +extern xenforeignmemory_handle *xen_fmem;
>  extern struct xs_handle *xenstore;
>  extern const char *xen_protocol;
>  
> diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
> index 8f38310..95275b3 100644
> --- a/include/hw/xen/xen_common.h
> +++ b/include/hw/xen/xen_common.h
> @@ -41,6 +41,7 @@ static inline void *xc_map_foreign_bulk(int xc_handle, uint32_t dom, int prot,
>  typedef int XenXC;
>  typedef int xenevtchn_handle;
>  typedef int xengnttab_handle;
> +typedef int xenforeignmemory_handle;
>  
>  #  define XC_INTERFACE_FMT "%i"
>  #  define XC_HANDLER_INITIAL_VALUE    -1
> @@ -104,6 +105,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
>      return xc_interface_open();
>  }
>  
> +/* See below for xenforeignmemory_* APIs */
> +
>  static inline int xc_fd(int xen_xc)
>  {
>      return xen_xc;
> @@ -149,6 +152,7 @@ static inline void xs_close(struct xs_handle *xsh)
>  #else
>  
>  typedef xc_interface *XenXC;
> +typedef xc_interface *xenforeignmemory_handle;
>  typedef xc_evtchn xenevtchn_handle;
>  typedef xc_gnttab xengnttab_handle;
>  
> @@ -178,6 +182,8 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
>      return xc_interface_open(logger, dombuild_logger, open_flags);
>  }
>  
> +/* See below for xenforeignmemory_* APIs */
> +
>  /* FIXME There is now way to have the xen fd */
>  static inline int xc_fd(xc_interface *xen_xc)
>  {
> @@ -501,4 +507,23 @@ static inline int xen_domain_create(XenXC xc, uint32_t ssidref,
>  }
>  #endif
>  
> +#if CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
> +
> +#define xenforeignmemory_open(l, f) &xen_xc
> +
> +static inline void *xenforeignmemory_map(XenXC *h, uint32_t dom,
> +                                         int prot, size_t pages,
> +                                         const xen_pfn_t arr[/*pages*/],
> +                                         int err[/*pages*/])
> +{
> +    if (err)
> +        return xc_map_foreign_bulk(*h, dom, prot, arr, err, pages);
> +    else
> +        return xc_map_foreign_pages(*h, dom, prot, arr, pages);
> +}
> +
> +#define xenforeignmemory_unmap(h, p, s) munmap(p, s * XC_PAGE_SIZE)
> +
> +#endif
> +
>  #endif /* QEMU_HW_XEN_COMMON_H */
> diff --git a/xen-common.c b/xen-common.c
> index 0dcdbc3..6cd2959 100644
> --- a/xen-common.c
> +++ b/xen-common.c
> @@ -118,6 +118,12 @@ static int xen_init(MachineState *ms)
>          xen_be_printf(NULL, 0, "can't open xen interface\n");
>          return -1;
>      }
> +    xen_fmem = xenforeignmemory_open(0, 0);
> +    if (xen_fmem == NULL) {
> +        xen_be_printf(NULL, 0, "can't open xen fmem interface\n");
> +        xc_interface_close(xen_xc);
> +        return -1;
> +    }
>      qemu_add_vm_change_state_handler(xen_change_state_handler, NULL);
>  
>      global_state_set_optional();
> diff --git a/xen-hvm.c b/xen-hvm.c
> index 1f729f6..66ee835 100644
> --- a/xen-hvm.c
> +++ b/xen-hvm.c
> @@ -1247,9 +1247,9 @@ int xen_hvm_init(PCMachineState *pcms,
>      DPRINTF("buffered io page at pfn %lx\n", bufioreq_pfn);
>      DPRINTF("buffered io evtchn is %x\n", bufioreq_evtchn);
>  
> -    state->shared_page = xc_map_foreign_pages(xen_xc, xen_domid,
> +    state->shared_page = xenforeignmemory_map(xen_fmem, xen_domid,
>                                                PROT_READ|PROT_WRITE,
> -                                              &ioreq_pfn, 1);
> +                                              1, &ioreq_pfn, NULL);
>      if (state->shared_page == NULL) {
>          hw_error("map shared IO page returned error %d handle=" XC_INTERFACE_FMT,
>                   errno, xen_xc);
> @@ -1259,8 +1259,8 @@ int xen_hvm_init(PCMachineState *pcms,
>      if (!rc) {
>          DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
>          state->shared_vmport_page =
> -            xc_map_foreign_pages(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
> -                                 &ioreq_pfn, 1);
> +            xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
> +                                 1, &ioreq_pfn, NULL);
>          if (state->shared_vmport_page == NULL) {
>              hw_error("map shared vmport IO page returned error %d handle="
>                       XC_INTERFACE_FMT, errno, xen_xc);
> @@ -1269,9 +1269,9 @@ int xen_hvm_init(PCMachineState *pcms,
>          hw_error("get vmport regs pfn returned error %d, rc=%d", errno, rc);
>      }
>  
> -    state->buffered_io_page = xc_map_foreign_pages(xen_xc, xen_domid,
> +    state->buffered_io_page = xenforeignmemory_map(xen_fmem, xen_domid,
>                                                     PROT_READ|PROT_WRITE,
> -                                                   &bufioreq_pfn, 1);
> +                                                   1, &bufioreq_pfn, NULL);
>      if (state->buffered_io_page == NULL) {
>          hw_error("map buffered IO page returned error %d", errno);
>      }
> diff --git a/xen-mapcache.c b/xen-mapcache.c
> index 97fece2..4a04378 100644
> --- a/xen-mapcache.c
> +++ b/xen-mapcache.c
> @@ -176,10 +176,10 @@ static void xen_remap_bucket(MapCacheEntry *entry,
>          pfns[i] = (address_index << (MCACHE_BUCKET_SHIFT-XC_PAGE_SHIFT)) + i;
>      }
>  
> -    vaddr_base = xc_map_foreign_bulk(xen_xc, xen_domid, PROT_READ|PROT_WRITE,
> -                                     pfns, err, nb_pfn);
> +    vaddr_base = xenforeignmemory_map(xen_fmem, xen_domid, PROT_READ|PROT_WRITE,
> +                                      nb_pfn, pfns, err);
>      if (vaddr_base == NULL) {
> -        perror("xc_map_foreign_bulk");
> +        perror("xenforeignmemory_map");
>          exit(-1);
>      }
>  
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [PATCH QEMU-XEN v8 6/8] xen: Use stable library interfaces when they are available.
  2016-01-15 13:23     ` Ian Campbell
@ 2016-01-15 14:43       ` Stefano Stabellini
  -1 siblings, 0 replies; 113+ messages in thread
From: Stefano Stabellini @ 2016-01-15 14:43 UTC (permalink / raw)
  To: Ian Campbell
  Cc: wei.liu2, stefano.stabellini, ian.jackson, qemu-devel, xen-devel

On Fri, 15 Jan 2016, Ian Campbell wrote:
> In Xen 4.7 we are refactoring parts libxenctrl into a number of
> separate libraries which will provide backward and forward API and ABI
> compatiblity.
> 
> Specifically libxenevtchn, libxengnttab and libxenforeignmemory.
> 
> Previous patches have already laid the groundwork for using these by
> switching the existing compatibility shims to reflect the intefaces to
> these libraries.
> 
> So all which remains is to update configure to detect the libraries
> and enable their use. Although they are notionally independent we take
> an all or nothing approach to the three libraries since they were
> added at the same time.
> 
> The only non-obvious bit is that we now open a proper xenforeignmemory
> handle for xen_fmem instead of reusing the xen_xc handle.
> 
> Build tested with 4.0 .. 4.6 (inclusive) and the patches targetting
> 4.7 which adds these libraries.
> 
> This uses CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 to cover the
> introduction of these new interfaces.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>


> ---
> 
> v8: Use CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 for new interfaces.
>     Dropped Reviewed-by.
> 
> v6: Two minor formatting things.
>     Rebase onto "xen: fix usage of xc_domain_create in domain
>     builder", required adjusting the configure script changes.
> 
>     The rebase wasn't entirely trivial (semantically), so dropped
>     Stefano's reviwed by.
> 
> v5: Remove ifdef check when undeffing the compat stuff.
>     s/now way/no way/
> 
> v4: xenforeignmemory_open is now a compat wrapper, so no ifdef.
> 
>     Simplify configury by asserting that interface version 470 will
>     always have the libraries (lack of them makes it 460).
> 
>     Ran checkpatch and fixed everything except:
> 
> ERROR: need consistent spacing around '*' (ctx:WxV)
> +typedef xc_interface *XenXC;
> 
> Which I think is a false +ve.
> 
> Simpler configure stuff
> ---
>  configure                   | 55 +++++++++++++++++++++++++++++++++++++++++++++
>  include/hw/xen/xen_common.h | 35 +++++++++++++++++++++++++++--
>  2 files changed, 88 insertions(+), 2 deletions(-)
> 
> diff --git a/configure b/configure
> index 44ac9ab..9ead31d 100755
> --- a/configure
> +++ b/configure
> @@ -1938,6 +1938,7 @@ fi
>  
>  if test "$xen" != "no" ; then
>    xen_libs="-lxenstore -lxenctrl -lxenguest"
> +  xen_stable_libs="-lxenforeignmemory -lxengnttab -lxenevtchn"
>  
>    # First we test whether Xen headers and libraries are available.
>    # If no, we are done and there is no Xen support.
> @@ -1960,6 +1961,57 @@ EOF
>    # Xen unstable
>    elif
>        cat > $TMPC <<EOF &&
> +/*
> + * If we have stable libs the we don't want the libxc compat
> + * layers, regardless of what CFLAGS we may have been given.
> + */
> +#undef XC_WANT_COMPAT_EVTCHN_API
> +#undef XC_WANT_COMPAT_GNTTAB_API
> +#undef XC_WANT_COMPAT_MAP_FOREIGN_API
> +#include <xenctrl.h>
> +#include <xenstore.h>
> +#include <xenevtchn.h>
> +#include <xengnttab.h>
> +#include <xenforeignmemory.h>
> +#include <stdint.h>
> +#include <xen/hvm/hvm_info_table.h>
> +#if !defined(HVM_MAX_VCPUS)
> +# error HVM_MAX_VCPUS not defined
> +#endif
> +int main(void) {
> +  xc_interface *xc = NULL;
> +  xenforeignmemory_handle *xfmem;
> +  xenevtchn_handle *xe;
> +  xengnttab_handle *xg;
> +  xen_domain_handle_t handle;
> +
> +  xs_daemon_open();
> +
> +  xc = xc_interface_open(0, 0, 0);
> +  xc_hvm_set_mem_type(0, 0, HVMMEM_ram_ro, 0, 0);
> +  xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
> +  xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
> +  xc_hvm_create_ioreq_server(xc, 0, HVM_IOREQSRV_BUFIOREQ_ATOMIC, NULL);
> +  xc_domain_create(xc, 0, handle, 0, NULL, NULL);
> +
> +  xfmem = xenforeignmemory_open(0, 0);
> +  xenforeignmemory_map(xfmem, 0, 0, 0, 0, 0);
> +
> +  xe = xenevtchn_open(0, 0);
> +  xenevtchn_fd(xe);
> +
> +  xg = xengnttab_open(0, 0);
> +  xengnttab_map_grant_ref(xg, 0, 0, 0);
> +
> +  return 0;
> +}
> +EOF
> +      compile_prog "" "$xen_libs $xen_stable_libs"
> +    then
> +    xen_ctrl_version=471
> +    xen=yes
> +  elif
> +      cat > $TMPC <<EOF &&
>  #include <xenctrl.h>
>  #include <stdint.h>
>  int main(void) {
> @@ -2153,6 +2205,9 @@ EOF
>    fi
>  
>    if test "$xen" = yes; then
> +    if test $xen_ctrl_version -ge 471  ; then
> +      libs_softmmu="$xen_stable_libs $libs_softmmu"
> +    fi
>      libs_softmmu="$xen_libs $libs_softmmu"
>    fi
>  fi
> diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
> index 95275b3..19f1577 100644
> --- a/include/hw/xen/xen_common.h
> +++ b/include/hw/xen/xen_common.h
> @@ -6,6 +6,15 @@
>  #include <stddef.h>
>  #include <inttypes.h>
>  
> +/*
> + * If we have new enough libxenctrl then we do not want/need these compat
> + * interfaces, despite what the user supplied cflags might say. They
> + * must be undefined before including xenctrl.h
> + */
> +#undef XC_WANT_COMPAT_EVTCHN_API
> +#undef XC_WANT_COMPAT_GNTTAB_API
> +#undef XC_WANT_COMPAT_MAP_FOREIGN_API
> +
>  #include <xenctrl.h>
>  #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 420
>  #  include <xs.h>
> @@ -148,8 +157,8 @@ static inline void xs_close(struct xs_handle *xsh)
>  }
>  
>  
> -/* Xen 4.1 */
> -#else
> +/* Xen 4.1 thru 4.6 */
> +#elif CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
>  
>  typedef xc_interface *XenXC;
>  typedef xc_interface *xenforeignmemory_handle;
> @@ -184,6 +193,28 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
>  
>  /* See below for xenforeignmemory_* APIs */
>  
> +/* FIXME There is no way to have the xen fd */
> +static inline int xc_fd(xc_interface *xen_xc)
> +{
> +    return -1;
> +}
> +#else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
> +
> +typedef xc_interface *XenXC;
> +
> +#  define XC_INTERFACE_FMT "%p"
> +#  define XC_HANDLER_INITIAL_VALUE    NULL
> +
> +#include <xenevtchn.h>
> +#include <xengnttab.h>
> +#include <xenforeignmemory.h>
> +
> +static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
> +                                          unsigned int open_flags)
> +{
> +    return xc_interface_open(logger, dombuild_logger, open_flags);
> +}
> +
>  /* FIXME There is now way to have the xen fd */
>  static inline int xc_fd(xc_interface *xen_xc)
>  {
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH QEMU-XEN v8 6/8] xen: Use stable library interfaces when they are available.
@ 2016-01-15 14:43       ` Stefano Stabellini
  0 siblings, 0 replies; 113+ messages in thread
From: Stefano Stabellini @ 2016-01-15 14:43 UTC (permalink / raw)
  To: Ian Campbell
  Cc: wei.liu2, stefano.stabellini, ian.jackson, qemu-devel, xen-devel

On Fri, 15 Jan 2016, Ian Campbell wrote:
> In Xen 4.7 we are refactoring parts libxenctrl into a number of
> separate libraries which will provide backward and forward API and ABI
> compatiblity.
> 
> Specifically libxenevtchn, libxengnttab and libxenforeignmemory.
> 
> Previous patches have already laid the groundwork for using these by
> switching the existing compatibility shims to reflect the intefaces to
> these libraries.
> 
> So all which remains is to update configure to detect the libraries
> and enable their use. Although they are notionally independent we take
> an all or nothing approach to the three libraries since they were
> added at the same time.
> 
> The only non-obvious bit is that we now open a proper xenforeignmemory
> handle for xen_fmem instead of reusing the xen_xc handle.
> 
> Build tested with 4.0 .. 4.6 (inclusive) and the patches targetting
> 4.7 which adds these libraries.
> 
> This uses CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 to cover the
> introduction of these new interfaces.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Reviewed-by: Stefano Stabellini <stefano.stabellini@eu.citrix.com>


> ---
> 
> v8: Use CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 for new interfaces.
>     Dropped Reviewed-by.
> 
> v6: Two minor formatting things.
>     Rebase onto "xen: fix usage of xc_domain_create in domain
>     builder", required adjusting the configure script changes.
> 
>     The rebase wasn't entirely trivial (semantically), so dropped
>     Stefano's reviwed by.
> 
> v5: Remove ifdef check when undeffing the compat stuff.
>     s/now way/no way/
> 
> v4: xenforeignmemory_open is now a compat wrapper, so no ifdef.
> 
>     Simplify configury by asserting that interface version 470 will
>     always have the libraries (lack of them makes it 460).
> 
>     Ran checkpatch and fixed everything except:
> 
> ERROR: need consistent spacing around '*' (ctx:WxV)
> +typedef xc_interface *XenXC;
> 
> Which I think is a false +ve.
> 
> Simpler configure stuff
> ---
>  configure                   | 55 +++++++++++++++++++++++++++++++++++++++++++++
>  include/hw/xen/xen_common.h | 35 +++++++++++++++++++++++++++--
>  2 files changed, 88 insertions(+), 2 deletions(-)
> 
> diff --git a/configure b/configure
> index 44ac9ab..9ead31d 100755
> --- a/configure
> +++ b/configure
> @@ -1938,6 +1938,7 @@ fi
>  
>  if test "$xen" != "no" ; then
>    xen_libs="-lxenstore -lxenctrl -lxenguest"
> +  xen_stable_libs="-lxenforeignmemory -lxengnttab -lxenevtchn"
>  
>    # First we test whether Xen headers and libraries are available.
>    # If no, we are done and there is no Xen support.
> @@ -1960,6 +1961,57 @@ EOF
>    # Xen unstable
>    elif
>        cat > $TMPC <<EOF &&
> +/*
> + * If we have stable libs the we don't want the libxc compat
> + * layers, regardless of what CFLAGS we may have been given.
> + */
> +#undef XC_WANT_COMPAT_EVTCHN_API
> +#undef XC_WANT_COMPAT_GNTTAB_API
> +#undef XC_WANT_COMPAT_MAP_FOREIGN_API
> +#include <xenctrl.h>
> +#include <xenstore.h>
> +#include <xenevtchn.h>
> +#include <xengnttab.h>
> +#include <xenforeignmemory.h>
> +#include <stdint.h>
> +#include <xen/hvm/hvm_info_table.h>
> +#if !defined(HVM_MAX_VCPUS)
> +# error HVM_MAX_VCPUS not defined
> +#endif
> +int main(void) {
> +  xc_interface *xc = NULL;
> +  xenforeignmemory_handle *xfmem;
> +  xenevtchn_handle *xe;
> +  xengnttab_handle *xg;
> +  xen_domain_handle_t handle;
> +
> +  xs_daemon_open();
> +
> +  xc = xc_interface_open(0, 0, 0);
> +  xc_hvm_set_mem_type(0, 0, HVMMEM_ram_ro, 0, 0);
> +  xc_domain_add_to_physmap(0, 0, XENMAPSPACE_gmfn, 0, 0);
> +  xc_hvm_inject_msi(xc, 0, 0xf0000000, 0x00000000);
> +  xc_hvm_create_ioreq_server(xc, 0, HVM_IOREQSRV_BUFIOREQ_ATOMIC, NULL);
> +  xc_domain_create(xc, 0, handle, 0, NULL, NULL);
> +
> +  xfmem = xenforeignmemory_open(0, 0);
> +  xenforeignmemory_map(xfmem, 0, 0, 0, 0, 0);
> +
> +  xe = xenevtchn_open(0, 0);
> +  xenevtchn_fd(xe);
> +
> +  xg = xengnttab_open(0, 0);
> +  xengnttab_map_grant_ref(xg, 0, 0, 0);
> +
> +  return 0;
> +}
> +EOF
> +      compile_prog "" "$xen_libs $xen_stable_libs"
> +    then
> +    xen_ctrl_version=471
> +    xen=yes
> +  elif
> +      cat > $TMPC <<EOF &&
>  #include <xenctrl.h>
>  #include <stdint.h>
>  int main(void) {
> @@ -2153,6 +2205,9 @@ EOF
>    fi
>  
>    if test "$xen" = yes; then
> +    if test $xen_ctrl_version -ge 471  ; then
> +      libs_softmmu="$xen_stable_libs $libs_softmmu"
> +    fi
>      libs_softmmu="$xen_libs $libs_softmmu"
>    fi
>  fi
> diff --git a/include/hw/xen/xen_common.h b/include/hw/xen/xen_common.h
> index 95275b3..19f1577 100644
> --- a/include/hw/xen/xen_common.h
> +++ b/include/hw/xen/xen_common.h
> @@ -6,6 +6,15 @@
>  #include <stddef.h>
>  #include <inttypes.h>
>  
> +/*
> + * If we have new enough libxenctrl then we do not want/need these compat
> + * interfaces, despite what the user supplied cflags might say. They
> + * must be undefined before including xenctrl.h
> + */
> +#undef XC_WANT_COMPAT_EVTCHN_API
> +#undef XC_WANT_COMPAT_GNTTAB_API
> +#undef XC_WANT_COMPAT_MAP_FOREIGN_API
> +
>  #include <xenctrl.h>
>  #if CONFIG_XEN_CTRL_INTERFACE_VERSION < 420
>  #  include <xs.h>
> @@ -148,8 +157,8 @@ static inline void xs_close(struct xs_handle *xsh)
>  }
>  
>  
> -/* Xen 4.1 */
> -#else
> +/* Xen 4.1 thru 4.6 */
> +#elif CONFIG_XEN_CTRL_INTERFACE_VERSION < 471
>  
>  typedef xc_interface *XenXC;
>  typedef xc_interface *xenforeignmemory_handle;
> @@ -184,6 +193,28 @@ static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
>  
>  /* See below for xenforeignmemory_* APIs */
>  
> +/* FIXME There is no way to have the xen fd */
> +static inline int xc_fd(xc_interface *xen_xc)
> +{
> +    return -1;
> +}
> +#else /* CONFIG_XEN_CTRL_INTERFACE_VERSION >= 471 */
> +
> +typedef xc_interface *XenXC;
> +
> +#  define XC_INTERFACE_FMT "%p"
> +#  define XC_HANDLER_INITIAL_VALUE    NULL
> +
> +#include <xenevtchn.h>
> +#include <xengnttab.h>
> +#include <xenforeignmemory.h>
> +
> +static inline XenXC xen_xc_interface_open(void *logger, void *dombuild_logger,
> +                                          unsigned int open_flags)
> +{
> +    return xc_interface_open(logger, dombuild_logger, open_flags);
> +}
> +
>  /* FIXME There is now way to have the xen fd */
>  static inline int xc_fd(xc_interface *xen_xc)
>  {
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
                     ` (7 preceding siblings ...)
  2016-01-15 13:23     ` Ian Campbell
@ 2016-01-15 14:44   ` Stefano Stabellini
  2016-01-15 15:08     ` Ian Campbell
  2016-01-15 15:08     ` [Qemu-devel] " Ian Campbell
  2016-01-15 14:44   ` Stefano Stabellini
  9 siblings, 2 replies; 113+ messages in thread
From: Stefano Stabellini @ 2016-01-15 14:44 UTC (permalink / raw)
  To: Ian Campbell
  Cc: wei.liu2, stefano.stabellini, ian.jackson, qemu-devel, xen-devel

What's the status of the libxc side changes? Is the interface stable
enough for me to commit this series?

On Fri, 15 Jan 2016, Ian Campbell wrote:
> We intend to stabilise some parts of the libxenctrl interface by
> splitting out some functionality into separate stable libraries.
> 
> This is the qemu-xen part of the first phase of that change.
> 
> This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
> super-intro mail covering all of the related patch series and which
> contains more details.
> 
> Ian Campbell (8):
>   xen_console: correctly cleanup primary console on teardown.
>   xen: Switch to libxenevtchn interface for compat shims.
>   xen: Switch to libxengnttab interface for compat shims.
>   xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
>   xen: Switch uses of xc_map_foreign_{pages,bulk} to use
>     libxenforeignmemory API.
>   xen: Use stable library interfaces when they are available.
>   xen: domainbuild: reopen libxenctrl interface after forking for domain
>     watcher.
>   xen: make it possible to build without the Xen PV domain builder
> 
>  configure                    |  70 ++++++++++++++++++++
>  hw/block/xen_disk.c          |  38 +++++------
>  hw/char/xen_console.c        |  19 +++---
>  hw/display/xenfb.c           |  28 ++++----
>  hw/net/xen_nic.c             |  18 +++---
>  hw/xen/xen_backend.c         |  44 +++++++------
>  hw/xenpv/Makefile.objs       |   4 +-
>  hw/xenpv/xen_domainbuild.c   |   9 ++-
>  hw/xenpv/xen_machine_pv.c    |  15 +++--
>  include/hw/xen/xen_backend.h |   5 +-
>  include/hw/xen/xen_common.h  | 149 ++++++++++++++++++++++++++++++++++---------
>  xen-common.c                 |   6 ++
>  xen-hvm.c                    |  39 +++++------
>  xen-mapcache.c               |   6 +-
>  14 files changed, 315 insertions(+), 135 deletions(-)
> 
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
                     ` (8 preceding siblings ...)
  2016-01-15 14:44   ` [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Stefano Stabellini
@ 2016-01-15 14:44   ` Stefano Stabellini
  9 siblings, 0 replies; 113+ messages in thread
From: Stefano Stabellini @ 2016-01-15 14:44 UTC (permalink / raw)
  To: Ian Campbell
  Cc: wei.liu2, stefano.stabellini, ian.jackson, qemu-devel, xen-devel

What's the status of the libxc side changes? Is the interface stable
enough for me to commit this series?

On Fri, 15 Jan 2016, Ian Campbell wrote:
> We intend to stabilise some parts of the libxenctrl interface by
> splitting out some functionality into separate stable libraries.
> 
> This is the qemu-xen part of the first phase of that change.
> 
> This mail is (or is intended to be) a reply to a "0/<VARIOUS>"
> super-intro mail covering all of the related patch series and which
> contains more details.
> 
> Ian Campbell (8):
>   xen_console: correctly cleanup primary console on teardown.
>   xen: Switch to libxenevtchn interface for compat shims.
>   xen: Switch to libxengnttab interface for compat shims.
>   xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
>   xen: Switch uses of xc_map_foreign_{pages,bulk} to use
>     libxenforeignmemory API.
>   xen: Use stable library interfaces when they are available.
>   xen: domainbuild: reopen libxenctrl interface after forking for domain
>     watcher.
>   xen: make it possible to build without the Xen PV domain builder
> 
>  configure                    |  70 ++++++++++++++++++++
>  hw/block/xen_disk.c          |  38 +++++------
>  hw/char/xen_console.c        |  19 +++---
>  hw/display/xenfb.c           |  28 ++++----
>  hw/net/xen_nic.c             |  18 +++---
>  hw/xen/xen_backend.c         |  44 +++++++------
>  hw/xenpv/Makefile.objs       |   4 +-
>  hw/xenpv/xen_domainbuild.c   |   9 ++-
>  hw/xenpv/xen_machine_pv.c    |  15 +++--
>  include/hw/xen/xen_backend.h |   5 +-
>  include/hw/xen/xen_common.h  | 149 ++++++++++++++++++++++++++++++++++---------
>  xen-common.c                 |   6 ++
>  xen-hvm.c                    |  39 +++++------
>  xen-mapcache.c               |   6 +-
>  14 files changed, 315 insertions(+), 135 deletions(-)
> 
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 14:44   ` [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Stefano Stabellini
  2016-01-15 15:08     ` Ian Campbell
@ 2016-01-15 15:08     ` Ian Campbell
  1 sibling, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 15:08 UTC (permalink / raw)
  To: Stefano Stabellini; +Cc: wei.liu2, ian.jackson, qemu-devel, xen-devel

On Fri, 2016-01-15 at 14:44 +0000, Stefano Stabellini wrote:
> What's the status of the libxc side changes? Is the interface stable
> enough for me to commit this series?

I'd recommend waiting. I'll ping you when it looks appropriate to apply
this series.

Thanks for the final acks on this sub-series!

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 14:44   ` [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Stefano Stabellini
@ 2016-01-15 15:08     ` Ian Campbell
  2016-01-15 15:08     ` [Qemu-devel] " Ian Campbell
  1 sibling, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 15:08 UTC (permalink / raw)
  To: Stefano Stabellini; +Cc: wei.liu2, ian.jackson, qemu-devel, xen-devel

On Fri, 2016-01-15 at 14:44 +0000, Stefano Stabellini wrote:
> What's the status of the libxc side changes? Is the interface stable
> enough for me to commit this series?

I'd recommend waiting. I'll ping you when it looks appropriate to apply
this series.

Thanks for the final acks on this sub-series!

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs.
  2016-01-15 13:22   ` [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  2016-01-19 13:34       ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, ian.jackson, xen-devel

On Fri, Jan 15, 2016 at 01:22:53PM +0000, Ian Campbell wrote:
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> ---
> v6: Also discuss recovering the memory.
> 
> v7: Further clarifications regarding forking based on ML discussions.
>     (Dropped Wei's ack)
> ---
>  .../libs/foreignmemory/include/xenforeignmemory.h  | 33 +++++++++++++++++++++-
>  1 file changed, 32 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h b/tools/libs/foreignmemory/include/xenforeignmemory.h
> index 04ff548..a6d1bdb 100644
> --- a/tools/libs/foreignmemory/include/xenforeignmemory.h
> +++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
> @@ -32,13 +32,44 @@ typedef struct xentoollog_logger xentoollog_logger;
>  typedef struct xenforeignmemory_handle xenforeignmemory_handle;
>  
>  /*
> - * Return a handle onto the hypercall driver.  Logs errors.
> + * Return a handle onto the foreign memory mapping driver.  Logs errors.
> + *
> + * Note: After fork(2) a child process must not use any opened
> + * foreignmemory handle inherited from their parent, nor access any
> + * grant mapped areas associated with that handle.
> + *
> + * The child must open a new handle if they want to interact with
> + * foreignmemory.
> + *
> + * Calling exec(2) in a child will safely (and reliably) reclaim any
> + * resources which were allocated via a xenforeignmemory_handle in the
> + * parent.
> + *
> + * A child which does not call exec(2) may safely call
> + * xenforeignmemory_close() on a xenforeignmemory_handle inherited
> + * from their parent. This will attempt to reclaim any resources
> + * associated with that handle. Note that in some implementations this
> + * reclamation may not be completely effective, in this case any
> + * affected resources remain allocated.
> + *
> + * Calling xenforeignmemory_close() is the only safe operation on a
> + * xenforeignmemory_handle which has been inherited.
>   */
>  xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger *logger,
>                                                 unsigned open_flags);
>  
>  /*
>   * Close a handle previously allocated with xenforeignmemory_open().
> + *
> + * Under normal circumstances (i.e. not in the child after a fork)
> + * xenforeignmemory_unmap() should be used on all mappings allocated

"Should" according to RFC 2119 has the connotation of "there might be a
valid reason to ignore such action". But after reading this passage I
think we should use "must" here?

Wei.

> + * by xenforeignmemory_map() prior to closing the handle in order to
> + * free up resources associated with those mappings.
> + *
> + * This is the only function which may be safely called on a
> + * xenforeignmemory_handle in a child after a
> + * fork. xenforeignmemory_unmap() must not be called under such
> + * circumstances.
>   */
>  int xenforeignmemory_close(xenforeignmemory_handle *fmem);
>  
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 18/29] tools/libs/evtchn: Use uint32_t for domid arguments
  2016-01-15 13:22   ` [PATCH XEN v8 18/29] tools/libs/evtchn: Use uint32_t for domid arguments Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, ian.jackson, xen-devel

On Fri, Jan 15, 2016 at 01:22:57PM +0000, Ian Campbell wrote:
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 20/29] tools/libs/gnttab: Extensive updates to API documentation.
  2016-01-15 13:22   ` [PATCH XEN v8 20/29] tools/libs/gnttab: Extensive updates to API documentation Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, Daniel De Graaf, ian.jackson, xen-devel

On Fri, Jan 15, 2016 at 01:22:59PM +0000, Ian Campbell wrote:
> In particular around error handling, behaviour on fork and the unmap
> notification mechanism.
> 
> Behaviour of xengnttab_map_*grant_refs and xengntshr_share_pages on
> partial failure has been confirmed/inferred (by inspection) on Linux
> and Mini-os (the only two known implementations. Likewise the
> behaviour of the notification mechanism has been confirmed/inferred
> (by inspection) of the Linux implementation (currently the only
> implementation) and libvchan (primary known user).
> 
> These updates are not folded into "tools: Refactor
> /dev/xen/gnt{dev,shr} wrappers into libxengnttab." to try and reduce
> the amount of non-movement changes in that patch.
> 
> While I'm not convinced by javadoc/doxygen cause the existing comments
> which appear to use that syntax to have the appropriate /** marker.
> 
> Also fix a typo in a code comment.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> Reviewed-by: Daniel De Graaf <dgdegra@tycho.nsa.gov>
> Cc: Daniel De Graaf <dgdegra@tycho.nsa.gov>

Since Daniel and Ian have done in depth review of this so I only skim it
this time. It looks sensible to me:

Acked-by: Wei Liu <wei.liu2@citrix.com>

(one typo below)

[...]
> + *
> + * NOTE: this protocol is intended to allow for better error behaviour
> + * and recovery between two cooperating peers. It does not cover the
> + * case of a malivious peer who may continue to hold resources open.

"malicious"

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-15 13:23   ` [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  2016-01-19 13:40       ` Ian Campbell
  2016-01-19 14:54       ` Roger Pau Monné
  0 siblings, 2 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, Roger Pau Monné, ian.jackson, xen-devel

On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
> This avoids a potential issue with a fork after allocation but before
> madvise.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> ---
> v7: New, replacing "tools/libs/call: linux: avoid forking between mmap
>     and madvise".
> ---
>  tools/libs/call/linux.c | 14 +++++++++++++-
>  1 file changed, 13 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
> index 3641e41..651f380 100644
> --- a/tools/libs/call/linux.c
> +++ b/tools/libs/call/linux.c

I didn't notice you only handled this for Linux until now.

I think FreeBSD and NetBSD need similar treatment, too? But then current
BSD* code doesn't even support DONTFORK in madvise.

Adding Roger for more input.

The changes in this patch look fine to me.

Wei.

> @@ -88,7 +88,7 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
>  {
>      size_t size = npages * PAGE_SIZE;
>      void *p;
> -    int rc, saved_errno;
> +    int rc, i, saved_errno;
>  
>      /* Address returned by mmap is page aligned. */
>      p = mmap(NULL, size, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS|MAP_LOCKED, -1, 0);
> @@ -107,6 +107,18 @@ void *osdep_alloc_pages(xencall_handle *xcall, size_t npages)
>          goto out;
>      }
>  
> +    /*
> +     * Touch each page in turn to force them to be un-CoWed, in case a
> +     * fork happened in another thread at an inopportune moment
> +     * above. The madvise() will prevent any subsequent fork calls from
> +     * causing the same problem.
> +     */
> +    for ( i = 0; i < npages ; i++ )
> +    {
> +        char *c = (char *)p + (i*PAGE_SIZE);
> +        *c = 0;
> +    }
> +
>      return p;
>  
>  out:
> -- 
> 2.1.4
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 25/29] tools/libs/{call, evtchn}: Document requirements around forking.
  2016-01-15 13:23   ` [PATCH XEN v8 25/29] tools/libs/{call, evtchn}: Document requirements around forking Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, ian.jackson, xen-devel

On Fri, Jan 15, 2016 at 01:23:04PM +0000, Ian Campbell wrote:
> Much like for gnttab and foreignmemory xencall hypercall buffers need
> care.
> 
> Evtchn is a bit simpler (no magic mappings) but may not work from
> parent + child simultaneously, document "parent only" since it is
> consistent with the others.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 26/29] tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
  2016-01-15 13:23   ` [PATCH XEN v8 26/29] tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, Roger.Pau, ian.jackson, jbeulich, xen-devel

On Fri, Jan 15, 2016 at 01:23:05PM +0000, Ian Campbell wrote:
> In some cases this replaces an FD_CLOEXEC dance, in others it is new.
> 
> Linux has had O_CLOEXEC since 2.6.23 (October 2007), so we can rely on
> it from Xen 4.7 I think. Some libc headers may still lack the
> definition, so we take care of that if need be by defining to 0 (on
> the premise that such an old glibc might barf on O_CLOEXEC even if the
> kernel may or may not be so old).
> 
> All stable versions of FreeBSD support O_CLOEXEC (10.2, 9.3 and 8.4),
> and we assume the libc there does too.
> 
> Remove various comments about having to take responsibility for this
> (since really it is just hygiene, politeness, not a requirement) and
> the reasons for using O_CLOEXEC seem pretty straightforward.
> 
> Backends for other OSes are untouched.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> Acked-by: Roger Pau Monné <roger.pau@citrix.com>

Acked-by: Wei Liu <wei.liu2@citrix.com>

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain.
  2016-01-15 13:23   ` [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain Ian Campbell
@ 2016-01-19 13:24     ` Wei Liu
  2016-01-19 13:44       ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Wei Liu @ 2016-01-19 13:24 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, ian.jackson, xen-devel

On Fri, Jan 15, 2016 at 01:23:07PM +0000, Ian Campbell wrote:
> These are intended to allow user space processes (in particular QEMU)
> to lock down all the handles at start of day and then drop the
> privileges which would allow them to open any new unrestricted handles
> (e.g. setuid or similar). This will reduce the privileges which taking
> over such a process would gain an attacker wrt other domains in the
> system.
> 
> These are currently unimplemented on all platforms, however the API
> semantics are defined as the basis for discussion, and so that
> consumers can rely on this interface always having been present rather
> than requiring compile time API checks.
> 
> It is expected that these will be implemented by adding new ioctl
> calls on the underlying driver and that the restrictions will be
> enforced at the kernel interface layer (most likely by the kernel
> itself).
> 
> For evtchn, foreignmemory, gnttab and gntshr this is hopefully
> reasonably straightforward.
> 
> For call it is not so clear cut. Clearly the kernel cannot enforce
> these restrictions for hypercalls which are not stable (domctl et al)
> so they can never be on the whitelist. It may also be that potential
> users would like to restrict the handle further than just a given
> target domain, i.e. to a specific set of functionality (e.g. "things a
> device model might reasonably do"). I think we will also need some way
> to discover whether a given set of interfaces is available to a
> restricted handle, in order to support the addition of new
> functionality.
> 
> Notes:
> 
> - On many (all?) platforms libxencall and libxenforeignmemory are
>   implemented by the same underlying privcmd driver. The platform
>   level ioctl interface should support restricting the handle to only
>   one or the other.

IIRC mini-os doesn't have ioctl. That would require some special
handling -- if we want to use the new API in qemu-trad, too.
We shall cross the bridge when we get there.

> - On platforms with multiple privilege mapping ioctl variants should
>   consider only allowing the newest/currently preferred one on a
>   restricted handle. e.g. on Linux this would allow
>   IOCTL_PRIVCMD_MMAPBATCH_V2 but not IOCTL_PRIVCMD_MMAPBATCH. (Of
>   course any subsequently introduced _V3 would be subject to
>   compatibility concerns)
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
[...]
>  /*
> + * Attempt to restrict the given xcall handle to only be able to
> + * target the given domain.
> + *
> + * On success returns 0, after which only hypercalls which are on a
> + * platform specific whitelist can be called and the arguments will be
> + * audited by the platform to ensure that the target domain is
> + * domid.
> + *
> + * Subsequent attempts to call any hypercall not on the platform
> + * specific whitelist will return -1 setting errno to ENOSYS.
> + *
> + * Subsequent attempts to call any hypercall on the platform specific
> + * whitelist with any other target domain return -1 setting errno to
> + * EPERM.
> + *
> + * These restrictions will be implemented by the platform in a way
> + * which cannot be circumvented by a userspace process. Further
> + * privilege drops (such as using setuid(2) etc) may also be required
> + * to prevent a compromised process from simply opening a second
> + * handle
> + *
> + * XXX which hypercalls are restricted, per platform list, do we need
> + * a way to probe? Do we want to be able to restrict to particular
> + * subsets of whitelisted hypercalls?
> + *

TBH given the semantics of this call is not yet clear I don't think we
should rush committing this interface.

Wei.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs.
  2016-01-19 13:24     ` Wei Liu
@ 2016-01-19 13:34       ` Ian Campbell
  2016-01-19 14:25         ` Wei Liu
  0 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 13:34 UTC (permalink / raw)
  To: Wei Liu; +Cc: ian.jackson, xen-devel

On Tue, 2016-01-19 at 13:24 +0000, Wei Liu wrote:
> On Fri, Jan 15, 2016 at 01:22:53PM +0000, Ian Campbell wrote:
> > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > ---
> > v6: Also discuss recovering the memory.
> > 
> > v7: Further clarifications regarding forking based on ML discussions.
> >     (Dropped Wei's ack)
> > ---
> >  .../libs/foreignmemory/include/xenforeignmemory.h  | 33
> > +++++++++++++++++++++-
> >  1 file changed, 32 insertions(+), 1 deletion(-)
> > 
> > diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h
> > b/tools/libs/foreignmemory/include/xenforeignmemory.h
> > index 04ff548..a6d1bdb 100644
> > --- a/tools/libs/foreignmemory/include/xenforeignmemory.h
> > +++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
> > @@ -32,13 +32,44 @@ typedef struct xentoollog_logger xentoollog_logger;
> >  typedef struct xenforeignmemory_handle xenforeignmemory_handle;
> >  
> >  /*
> > - * Return a handle onto the hypercall driver.  Logs errors.
> > + * Return a handle onto the foreign memory mapping driver.  Logs
> > errors.
> > + *
> > + * Note: After fork(2) a child process must not use any opened
> > + * foreignmemory handle inherited from their parent, nor access any
> > + * grant mapped areas associated with that handle.
> > + *
> > + * The child must open a new handle if they want to interact with
> > + * foreignmemory.
> > + *
> > + * Calling exec(2) in a child will safely (and reliably) reclaim any
> > + * resources which were allocated via a xenforeignmemory_handle in the
> > + * parent.
> > + *
> > + * A child which does not call exec(2) may safely call
> > + * xenforeignmemory_close() on a xenforeignmemory_handle inherited
> > + * from their parent. This will attempt to reclaim any resources
> > + * associated with that handle. Note that in some implementations this
> > + * reclamation may not be completely effective, in this case any
> > + * affected resources remain allocated.
> > + *
> > + * Calling xenforeignmemory_close() is the only safe operation on a
> > + * xenforeignmemory_handle which has been inherited.
> >   */
> >  xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger
> > *logger,
> >                                                 unsigned open_flags);
> >  
> >  /*
> >   * Close a handle previously allocated with xenforeignmemory_open().
> > + *
> > + * Under normal circumstances (i.e. not in the child after a fork)
> > + * xenforeignmemory_unmap() should be used on all mappings allocated
> 
> "Should" according to RFC 2119 has the connotation of "there might be a
> valid reason to ignore such action". But after reading this passage I
> think we should use "must" here?

RFC 2119 formally defines "SHOULD" not "should" (or "Should"), and in any
case in order to be subject to those formal definitions a document would
need to explicitly reference RFC 2119.

I think most readers of normal English prose would probably take "must" and
"should" to mean mostly the same thing.

If there are other reasons to resend I don't mind switching it to must, but
I don't think it is worth a resend of this mega-series for what is a minor
semantic quibble.

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 13:24     ` Wei Liu
@ 2016-01-19 13:40       ` Ian Campbell
  2016-01-19 14:26         ` Wei Liu
  2016-01-19 14:54       ` Roger Pau Monné
  1 sibling, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 13:40 UTC (permalink / raw)
  To: Wei Liu; +Cc: Roger Pau Monné, ian.jackson, xen-devel

On Tue, 2016-01-19 at 13:24 +0000, Wei Liu wrote:
> On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
> > This avoids a potential issue with a fork after allocation but before
> > madvise.
> > 
> > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > ---
> > v7: New, replacing "tools/libs/call: linux: avoid forking between mmap
> >     and madvise".
> > ---
> >  tools/libs/call/linux.c | 14 +++++++++++++-
> >  1 file changed, 13 insertions(+), 1 deletion(-)
> > 
> > diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
> > index 3641e41..651f380 100644
> > --- a/tools/libs/call/linux.c
> > +++ b/tools/libs/call/linux.c
> 
> I didn't notice you only handled this for Linux until now.
> 
> I think FreeBSD and NetBSD need similar treatment, too? But then current
> BSD* code doesn't even support DONTFORK in madvise.

I think any updates to the *BSD side should come as separate improvements.

If they are necessary at all, the Linux stuff comes from Linux making use
of particular behaviours on mlock()'s memory (specifically, IIRC, doing
things such as THP and NUMA balancing which can cause page faults on the
mlock'd pages despite them being locked) which are (possibly) allowed by
POSIX, but *BSD may not take the same interpretation.

> Adding Roger for more input.
> 
> The changes in this patch look fine to me.

May I take that as an Ack?


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain.
  2016-01-19 13:24     ` Wei Liu
@ 2016-01-19 13:44       ` Ian Campbell
  2016-01-19 14:30         ` Wei Liu
  0 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 13:44 UTC (permalink / raw)
  To: Wei Liu; +Cc: ian.jackson, xen-devel

On Tue, 2016-01-19 at 13:24 +0000, Wei Liu wrote:
> On Fri, Jan 15, 2016 at 01:23:07PM +0000, Ian Campbell wrote:
> > These are intended to allow user space processes (in particular QEMU)
> > to lock down all the handles at start of day and then drop the
> > privileges which would allow them to open any new unrestricted handles
> > (e.g. setuid or similar). This will reduce the privileges which taking
> > over such a process would gain an attacker wrt other domains in the
> > system.
> > 
> > These are currently unimplemented on all platforms, however the API
> > semantics are defined as the basis for discussion, and so that
> > consumers can rely on this interface always having been present rather
> > than requiring compile time API checks.
> > 
> > It is expected that these will be implemented by adding new ioctl
> > calls on the underlying driver and that the restrictions will be
> > enforced at the kernel interface layer (most likely by the kernel
> > itself).
> > 
> > For evtchn, foreignmemory, gnttab and gntshr this is hopefully
> > reasonably straightforward.
> > 
> > For call it is not so clear cut. Clearly the kernel cannot enforce
> > these restrictions for hypercalls which are not stable (domctl et al)
> > so they can never be on the whitelist. It may also be that potential
> > users would like to restrict the handle further than just a given
> > target domain, i.e. to a specific set of functionality (e.g. "things a
> > device model might reasonably do"). I think we will also need some way
> > to discover whether a given set of interfaces is available to a
> > restricted handle, in order to support the addition of new
> > functionality.
> > 
> > Notes:
> > 
> > - On many (all?) platforms libxencall and libxenforeignmemory are
> >   implemented by the same underlying privcmd driver. The platform
> >   level ioctl interface should support restricting the handle to only
> >   one or the other.
> 
> IIRC mini-os doesn't have ioctl. That would require some special
> handling

The actual implementation of this functionality would be OS specific and
therefore need to be in $os.c, where mini-os.c is under no obligation to
use an ioctl if it doesn't want to.

The only reason it is done in the common code here is to avoid adding a
dozen stubs prior to even one OS actually implementing this. I could add a
norestrict.c to each lib, put the stub there and link it on all platforms,
that would reduce the churn when someone comes to add the actual
functionality.

>  -- if we want to use the new API in qemu-trad, too.
> We shall cross the bridge when we get there.
> 
> > - On platforms with multiple privilege mapping ioctl variants should
> >   consider only allowing the newest/currently preferred one on a
> >   restricted handle. e.g. on Linux this would allow
> >   IOCTL_PRIVCMD_MMAPBATCH_V2 but not IOCTL_PRIVCMD_MMAPBATCH. (Of
> >   course any subsequently introduced _V3 would be subject to
> >   compatibility concerns)
> > 
> > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> [...]
> >  /*
> > + * Attempt to restrict the given xcall handle to only be able to
> > + * target the given domain.
> > + *
> > + * On success returns 0, after which only hypercalls which are on a
> > + * platform specific whitelist can be called and the arguments will be
> > + * audited by the platform to ensure that the target domain is
> > + * domid.
> > + *
> > + * Subsequent attempts to call any hypercall not on the platform
> > + * specific whitelist will return -1 setting errno to ENOSYS.
> > + *
> > + * Subsequent attempts to call any hypercall on the platform specific
> > + * whitelist with any other target domain return -1 setting errno to
> > + * EPERM.
> > + *
> > + * These restrictions will be implemented by the platform in a way
> > + * which cannot be circumvented by a userspace process. Further
> > + * privilege drops (such as using setuid(2) etc) may also be required
> > + * to prevent a compromised process from simply opening a second
> > + * handle
> > + *
> > + * XXX which hypercalls are restricted, per platform list, do we need
> > + * a way to probe? Do we want to be able to restrict to particular
> > + * subsets of whitelisted hypercalls?
> > + *
> 
> TBH given the semantics of this call is not yet clear I don't think we
> should rush committing this interface.

The intention was to try and get enough confidence that we could include
the call in the initial implementation such that applications could
unconditionally use it in the future.

If we can't manage a sufficient level of confidence in the proposed
interface then we should skip it for now of course.

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs.
  2016-01-19 13:34       ` Ian Campbell
@ 2016-01-19 14:25         ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 14:25 UTC (permalink / raw)
  To: Ian Campbell; +Cc: ian.jackson, Wei Liu, xen-devel

On Tue, Jan 19, 2016 at 01:34:58PM +0000, Ian Campbell wrote:
> On Tue, 2016-01-19 at 13:24 +0000, Wei Liu wrote:
> > On Fri, Jan 15, 2016 at 01:22:53PM +0000, Ian Campbell wrote:
> > > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > > ---
> > > v6: Also discuss recovering the memory.
> > > 
> > > v7: Further clarifications regarding forking based on ML discussions.
> > >     (Dropped Wei's ack)
> > > ---
> > >  .../libs/foreignmemory/include/xenforeignmemory.h  | 33
> > > +++++++++++++++++++++-
> > >  1 file changed, 32 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/tools/libs/foreignmemory/include/xenforeignmemory.h
> > > b/tools/libs/foreignmemory/include/xenforeignmemory.h
> > > index 04ff548..a6d1bdb 100644
> > > --- a/tools/libs/foreignmemory/include/xenforeignmemory.h
> > > +++ b/tools/libs/foreignmemory/include/xenforeignmemory.h
> > > @@ -32,13 +32,44 @@ typedef struct xentoollog_logger xentoollog_logger;
> > >  typedef struct xenforeignmemory_handle xenforeignmemory_handle;
> > >  
> > >  /*
> > > - * Return a handle onto the hypercall driver.  Logs errors.
> > > + * Return a handle onto the foreign memory mapping driver.  Logs
> > > errors.
> > > + *
> > > + * Note: After fork(2) a child process must not use any opened
> > > + * foreignmemory handle inherited from their parent, nor access any
> > > + * grant mapped areas associated with that handle.
> > > + *
> > > + * The child must open a new handle if they want to interact with
> > > + * foreignmemory.
> > > + *
> > > + * Calling exec(2) in a child will safely (and reliably) reclaim any
> > > + * resources which were allocated via a xenforeignmemory_handle in the
> > > + * parent.
> > > + *
> > > + * A child which does not call exec(2) may safely call
> > > + * xenforeignmemory_close() on a xenforeignmemory_handle inherited
> > > + * from their parent. This will attempt to reclaim any resources
> > > + * associated with that handle. Note that in some implementations this
> > > + * reclamation may not be completely effective, in this case any
> > > + * affected resources remain allocated.
> > > + *
> > > + * Calling xenforeignmemory_close() is the only safe operation on a
> > > + * xenforeignmemory_handle which has been inherited.
> > >   */
> > >  xenforeignmemory_handle *xenforeignmemory_open(xentoollog_logger
> > > *logger,
> > >                                                 unsigned open_flags);
> > >  
> > >  /*
> > >   * Close a handle previously allocated with xenforeignmemory_open().
> > > + *
> > > + * Under normal circumstances (i.e. not in the child after a fork)
> > > + * xenforeignmemory_unmap() should be used on all mappings allocated
> > 
> > "Should" according to RFC 2119 has the connotation of "there might be a
> > valid reason to ignore such action". But after reading this passage I
> > think we should use "must" here?
> 
> RFC 2119 formally defines "SHOULD" not "should" (or "Should"), and in any
> case in order to be subject to those formal definitions a document would
> need to explicitly reference RFC 2119.
> 
> I think most readers of normal English prose would probably take "must" and
> "should" to mean mostly the same thing.
> 
> If there are other reasons to resend I don't mind switching it to must, but
> I don't think it is worth a resend of this mega-series for what is a minor
> semantic quibble.
> 

No need to resend then.

Acked-by: Wei Liu <wei.liu2@citrix.com>

Wei.

> Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 13:40       ` Ian Campbell
@ 2016-01-19 14:26         ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 14:26 UTC (permalink / raw)
  To: Ian Campbell; +Cc: ian.jackson, Roger Pau Monné, Wei Liu, xen-devel

On Tue, Jan 19, 2016 at 01:40:55PM +0000, Ian Campbell wrote:
> On Tue, 2016-01-19 at 13:24 +0000, Wei Liu wrote:
> > On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
> > > This avoids a potential issue with a fork after allocation but before
> > > madvise.
> > > 
> > > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > > ---
> > > v7: New, replacing "tools/libs/call: linux: avoid forking between mmap
> > >     and madvise".
> > > ---
> > >  tools/libs/call/linux.c | 14 +++++++++++++-
> > >  1 file changed, 13 insertions(+), 1 deletion(-)
> > > 
> > > diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
> > > index 3641e41..651f380 100644
> > > --- a/tools/libs/call/linux.c
> > > +++ b/tools/libs/call/linux.c
> > 
> > I didn't notice you only handled this for Linux until now.
> > 
> > I think FreeBSD and NetBSD need similar treatment, too? But then current
> > BSD* code doesn't even support DONTFORK in madvise.
> 
> I think any updates to the *BSD side should come as separate improvements.
> 
> If they are necessary at all, the Linux stuff comes from Linux making use
> of particular behaviours on mlock()'s memory (specifically, IIRC, doing
> things such as THP and NUMA balancing which can cause page faults on the
> mlock'd pages despite them being locked) which are (possibly) allowed by
> POSIX, but *BSD may not take the same interpretation.
> 
> > Adding Roger for more input.
> > 
> > The changes in this patch look fine to me.
> 
> May I take that as an Ack?
> 

Yes.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain.
  2016-01-19 13:44       ` Ian Campbell
@ 2016-01-19 14:30         ` Wei Liu
  0 siblings, 0 replies; 113+ messages in thread
From: Wei Liu @ 2016-01-19 14:30 UTC (permalink / raw)
  To: Ian Campbell; +Cc: ian.jackson, Wei Liu, xen-devel

On Tue, Jan 19, 2016 at 01:44:53PM +0000, Ian Campbell wrote:
> On Tue, 2016-01-19 at 13:24 +0000, Wei Liu wrote:
> > On Fri, Jan 15, 2016 at 01:23:07PM +0000, Ian Campbell wrote:
> > > These are intended to allow user space processes (in particular QEMU)
> > > to lock down all the handles at start of day and then drop the
> > > privileges which would allow them to open any new unrestricted handles
> > > (e.g. setuid or similar). This will reduce the privileges which taking
> > > over such a process would gain an attacker wrt other domains in the
> > > system.
> > > 
> > > These are currently unimplemented on all platforms, however the API
> > > semantics are defined as the basis for discussion, and so that
> > > consumers can rely on this interface always having been present rather
> > > than requiring compile time API checks.
> > > 
> > > It is expected that these will be implemented by adding new ioctl
> > > calls on the underlying driver and that the restrictions will be
> > > enforced at the kernel interface layer (most likely by the kernel
> > > itself).
> > > 
> > > For evtchn, foreignmemory, gnttab and gntshr this is hopefully
> > > reasonably straightforward.
> > > 
> > > For call it is not so clear cut. Clearly the kernel cannot enforce
> > > these restrictions for hypercalls which are not stable (domctl et al)
> > > so they can never be on the whitelist. It may also be that potential
> > > users would like to restrict the handle further than just a given
> > > target domain, i.e. to a specific set of functionality (e.g. "things a
> > > device model might reasonably do"). I think we will also need some way
> > > to discover whether a given set of interfaces is available to a
> > > restricted handle, in order to support the addition of new
> > > functionality.
> > > 
> > > Notes:
> > > 
> > > - On many (all?) platforms libxencall and libxenforeignmemory are
> > >   implemented by the same underlying privcmd driver. The platform
> > >   level ioctl interface should support restricting the handle to only
> > >   one or the other.
> > 
> > IIRC mini-os doesn't have ioctl. That would require some special
> > handling
> 
> The actual implementation of this functionality would be OS specific and
> therefore need to be in $os.c, where mini-os.c is under no obligation to
> use an ioctl if it doesn't want to.
> 
> The only reason it is done in the common code here is to avoid adding a
> dozen stubs prior to even one OS actually implementing this. I could add a
> norestrict.c to each lib, put the stub there and link it on all platforms,
> that would reduce the churn when someone comes to add the actual
> functionality.
> 

I don't think you need to do that. Doing this in common code is fine by
me.

> >  -- if we want to use the new API in qemu-trad, too.
> > We shall cross the bridge when we get there.
> > 
> > > - On platforms with multiple privilege mapping ioctl variants should
> > >   consider only allowing the newest/currently preferred one on a
> > >   restricted handle. e.g. on Linux this would allow
> > >   IOCTL_PRIVCMD_MMAPBATCH_V2 but not IOCTL_PRIVCMD_MMAPBATCH. (Of
> > >   course any subsequently introduced _V3 would be subject to
> > >   compatibility concerns)
> > > 
> > > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > [...]
> > >  /*
> > > + * Attempt to restrict the given xcall handle to only be able to
> > > + * target the given domain.
> > > + *
> > > + * On success returns 0, after which only hypercalls which are on a
> > > + * platform specific whitelist can be called and the arguments will be
> > > + * audited by the platform to ensure that the target domain is
> > > + * domid.
> > > + *
> > > + * Subsequent attempts to call any hypercall not on the platform
> > > + * specific whitelist will return -1 setting errno to ENOSYS.
> > > + *
> > > + * Subsequent attempts to call any hypercall on the platform specific
> > > + * whitelist with any other target domain return -1 setting errno to
> > > + * EPERM.
> > > + *
> > > + * These restrictions will be implemented by the platform in a way
> > > + * which cannot be circumvented by a userspace process. Further
> > > + * privilege drops (such as using setuid(2) etc) may also be required
> > > + * to prevent a compromised process from simply opening a second
> > > + * handle
> > > + *
> > > + * XXX which hypercalls are restricted, per platform list, do we need
> > > + * a way to probe? Do we want to be able to restrict to particular
> > > + * subsets of whitelisted hypercalls?
> > > + *
> > 
> > TBH given the semantics of this call is not yet clear I don't think we
> > should rush committing this interface.
> 
> The intention was to try and get enough confidence that we could include
> the call in the initial implementation such that applications could
> unconditionally use it in the future.
> 
> If we can't manage a sufficient level of confidence in the proposed
> interface then we should skip it for now of course.
> 

Let's see what other people think about this particular function.

Wei.

> Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 13:24     ` Wei Liu
  2016-01-19 13:40       ` Ian Campbell
@ 2016-01-19 14:54       ` Roger Pau Monné
  2016-01-19 14:58         ` Wei Liu
  1 sibling, 1 reply; 113+ messages in thread
From: Roger Pau Monné @ 2016-01-19 14:54 UTC (permalink / raw)
  To: Wei Liu, Ian Campbell; +Cc: ian.jackson, xen-devel

El 19/01/16 a les 14.24, Wei Liu ha escrit:
> On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
>> This avoids a potential issue with a fork after allocation but before
>> madvise.
>>
>> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
>> ---
>> v7: New, replacing "tools/libs/call: linux: avoid forking between mmap
>>     and madvise".
>> ---
>>  tools/libs/call/linux.c | 14 +++++++++++++-
>>  1 file changed, 13 insertions(+), 1 deletion(-)
>>
>> diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
>> index 3641e41..651f380 100644
>> --- a/tools/libs/call/linux.c
>> +++ b/tools/libs/call/linux.c
> 
> I didn't notice you only handled this for Linux until now.
> 
> I think FreeBSD and NetBSD need similar treatment, too? But then current
> BSD* code doesn't even support DONTFORK in madvise.
> 
> Adding Roger for more input.

Hm, right, thanks for noticing this. I don't think FreeBSD needs a
similar treatment (pre-faulting), because mlock will remove any CoW when
making the pages wired.

Also, AFAICT we don't need to call madvise or minherit(2) because
mlock(2) already takes care of preventing the memory region from being
copied to the child on fork:

"Locked mappings are not inherited by the child process after a
fork(2)." [0]

So I think we are safe on the FreeBSD side.

Roger.

[0] https://www.freebsd.org/cgi/man.cgi?query=mlock

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 14:54       ` Roger Pau Monné
@ 2016-01-19 14:58         ` Wei Liu
  2016-01-19 15:03           ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Wei Liu @ 2016-01-19 14:58 UTC (permalink / raw)
  To: Roger Pau Monné; +Cc: ian.jackson, Wei Liu, Ian Campbell, xen-devel

On Tue, Jan 19, 2016 at 03:54:54PM +0100, Roger Pau Monné wrote:
> El 19/01/16 a les 14.24, Wei Liu ha escrit:
> > On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
> >> This avoids a potential issue with a fork after allocation but before
> >> madvise.
> >>
> >> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> >> ---
> >> v7: New, replacing "tools/libs/call: linux: avoid forking between mmap
> >>     and madvise".
> >> ---
> >>  tools/libs/call/linux.c | 14 +++++++++++++-
> >>  1 file changed, 13 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
> >> index 3641e41..651f380 100644
> >> --- a/tools/libs/call/linux.c
> >> +++ b/tools/libs/call/linux.c
> > 
> > I didn't notice you only handled this for Linux until now.
> > 
> > I think FreeBSD and NetBSD need similar treatment, too? But then current
> > BSD* code doesn't even support DONTFORK in madvise.
> > 
> > Adding Roger for more input.
> 
> Hm, right, thanks for noticing this. I don't think FreeBSD needs a
> similar treatment (pre-faulting), because mlock will remove any CoW when
> making the pages wired.
> 
> Also, AFAICT we don't need to call madvise or minherit(2) because
> mlock(2) already takes care of preventing the memory region from being
> copied to the child on fork:
> 
> "Locked mappings are not inherited by the child process after a
> fork(2)." [0]
> 
> So I think we are safe on the FreeBSD side.
> 

But what if the process forks between mmap and mlock? I think that
warrants touching the area like we do for Linux here.

Wei.

> Roger.
> 
> [0] https://www.freebsd.org/cgi/man.cgi?query=mlock
> 

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 14:58         ` Wei Liu
@ 2016-01-19 15:03           ` Ian Campbell
  2016-01-19 15:49             ` Wei Liu
  0 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 15:03 UTC (permalink / raw)
  To: Wei Liu, Roger Pau Monné; +Cc: ian.jackson, xen-devel

On Tue, 2016-01-19 at 14:58 +0000, Wei Liu wrote:
> On Tue, Jan 19, 2016 at 03:54:54PM +0100, Roger Pau Monné wrote:
> > El 19/01/16 a les 14.24, Wei Liu ha escrit:
> > > On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
> > > > This avoids a potential issue with a fork after allocation but
> > > > before
> > > > madvise.
> > > > 
> > > > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > > > ---
> > > > v7: New, replacing "tools/libs/call: linux: avoid forking between
> > > > mmap
> > > >     and madvise".
> > > > ---
> > > >  tools/libs/call/linux.c | 14 +++++++++++++-
> > > >  1 file changed, 13 insertions(+), 1 deletion(-)
> > > > 
> > > > diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
> > > > index 3641e41..651f380 100644
> > > > --- a/tools/libs/call/linux.c
> > > > +++ b/tools/libs/call/linux.c
> > > 
> > > I didn't notice you only handled this for Linux until now.
> > > 
> > > I think FreeBSD and NetBSD need similar treatment, too? But then
> > > current
> > > BSD* code doesn't even support DONTFORK in madvise.
> > > 
> > > Adding Roger for more input.
> > 
> > Hm, right, thanks for noticing this. I don't think FreeBSD needs a
> > similar treatment (pre-faulting), because mlock will remove any CoW
> > when
> > making the pages wired.
> > 
> > Also, AFAICT we don't need to call madvise or minherit(2) because
> > mlock(2) already takes care of preventing the memory region from being
> > copied to the child on fork:
> > 
> > "Locked mappings are not inherited by the child process after a
> > fork(2)." [0]
> > 
> > So I think we are safe on the FreeBSD side.
> > 
> 
> But what if the process forks between mmap and mlock? I think that
> warrants touching the area like we do for Linux here.

mlock guarantees the memory is populated, I think, which is equivalent to
touching it.

On Linux we use madvise not mlock, which doesn't make the same claims.

> 
Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (4 preceding siblings ...)
  2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
@ 2016-01-19 15:44 ` Ian Campbell
  2016-01-22 10:42   ` Ian Campbell
  2016-01-22 10:42   ` [Qemu-devel] " Ian Campbell
  2016-01-19 15:44 ` Ian Campbell
                   ` (2 subsequent siblings)
  8 siblings, 2 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 15:44 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:
> 
> Therefore needing attention from Ian and/or Wei are:
> 
> 	tools/libs/foreignmemory: Mention restrictions on fork in docs.
> N	tools/libs/evtchn: Use uint32_t for domid arguments
>     D	tools/libs/gnttab: Extensive updates to API documentation.
>       tools/libs/call: linux: touch newly allocated pages after madvise l
> 	tools/libs/{call,evtchn}: Document requirements around forking.
>    R	tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD

Thanks to Wei for acking all of these. This set of series is now ready to
go in, but we've not had a push for a little while and this is potentially
disruptive so I'm going to hold off for now until we get a push.

There are one or two patches which will require rebasing over Jeurgens
introduction of tools/helpers, I'll resend just those ones though (or at
least only the Xen part of this series).

Ian, I'll coordinate with you IRL regarding the push to the qemu-xen-trad
tree.

> N	tools/libs/*: Introduce APIs to restrict handles to a specific doma

It would be nice to either get this in for 4.7 or explicitly decide we
cannot, but I don't think it needs to block the rest of the series.

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (5 preceding siblings ...)
  2016-01-19 15:44 ` [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
@ 2016-01-19 15:44 ` Ian Campbell
  2016-01-22 14:14 ` [Qemu-devel] " Ian Campbell
  2016-01-22 14:14 ` Ian Campbell
  8 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 15:44 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:
> 
> Therefore needing attention from Ian and/or Wei are:
> 
> 	tools/libs/foreignmemory: Mention restrictions on fork in docs.
> N	tools/libs/evtchn: Use uint32_t for domid arguments
>     D	tools/libs/gnttab: Extensive updates to API documentation.
>       tools/libs/call: linux: touch newly allocated pages after madvise l
> 	tools/libs/{call,evtchn}: Document requirements around forking.
>    R	tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD

Thanks to Wei for acking all of these. This set of series is now ready to
go in, but we've not had a push for a little while and this is potentially
disruptive so I'm going to hold off for now until we get a push.

There are one or two patches which will require rebasing over Jeurgens
introduction of tools/helpers, I'll resend just those ones though (or at
least only the Xen part of this series).

Ian, I'll coordinate with you IRL regarding the push to the qemu-xen-trad
tree.

> N	tools/libs/*: Introduce APIs to restrict handles to a specific doma

It would be nice to either get this in for 4.7 or explicitly decide we
cannot, but I don't think it needs to block the rest of the series.

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 15:03           ` Ian Campbell
@ 2016-01-19 15:49             ` Wei Liu
  2016-01-19 15:59               ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Wei Liu @ 2016-01-19 15:49 UTC (permalink / raw)
  To: Ian Campbell; +Cc: ian.jackson, xen-devel, Wei Liu, Roger Pau Monné

On Tue, Jan 19, 2016 at 03:03:31PM +0000, Ian Campbell wrote:
> On Tue, 2016-01-19 at 14:58 +0000, Wei Liu wrote:
> > On Tue, Jan 19, 2016 at 03:54:54PM +0100, Roger Pau Monné wrote:
> > > El 19/01/16 a les 14.24, Wei Liu ha escrit:
> > > > On Fri, Jan 15, 2016 at 01:23:03PM +0000, Ian Campbell wrote:
> > > > > This avoids a potential issue with a fork after allocation but
> > > > > before
> > > > > madvise.
> > > > > 
> > > > > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> > > > > ---
> > > > > v7: New, replacing "tools/libs/call: linux: avoid forking between
> > > > > mmap
> > > > >     and madvise".
> > > > > ---
> > > > >  tools/libs/call/linux.c | 14 +++++++++++++-
> > > > >  1 file changed, 13 insertions(+), 1 deletion(-)
> > > > > 
> > > > > diff --git a/tools/libs/call/linux.c b/tools/libs/call/linux.c
> > > > > index 3641e41..651f380 100644
> > > > > --- a/tools/libs/call/linux.c
> > > > > +++ b/tools/libs/call/linux.c
> > > > 
> > > > I didn't notice you only handled this for Linux until now.
> > > > 
> > > > I think FreeBSD and NetBSD need similar treatment, too? But then
> > > > current
> > > > BSD* code doesn't even support DONTFORK in madvise.
> > > > 
> > > > Adding Roger for more input.
> > > 
> > > Hm, right, thanks for noticing this. I don't think FreeBSD needs a
> > > similar treatment (pre-faulting), because mlock will remove any CoW
> > > when
> > > making the pages wired.
> > > 
> > > Also, AFAICT we don't need to call madvise or minherit(2) because
> > > mlock(2) already takes care of preventing the memory region from being
> > > copied to the child on fork:
> > > 
> > > "Locked mappings are not inherited by the child process after a
> > > fork(2)." [0]
> > > 
> > > So I think we are safe on the FreeBSD side.
> > > 
> > 
> > But what if the process forks between mmap and mlock? I think that
> > warrants touching the area like we do for Linux here.
> 
> mlock guarantees the memory is populated, I think, which is equivalent to
> touching it.
> 
> On Linux we use madvise not mlock, which doesn't make the same claims.
> 

I see. I wonder why we didn't use mlock(2) in Linux too in the first
place.

Wei.

> > 
> Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown
  2016-01-19 15:49             ` Wei Liu
@ 2016-01-19 15:59               ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-19 15:59 UTC (permalink / raw)
  To: Wei Liu; +Cc: xen-devel, ian.jackson, Roger Pau Monné

On Tue, 2016-01-19 at 15:49 +0000, Wei Liu wrote:
> 
> I see. I wonder why we didn't use mlock(2) in Linux too in the first
> place.

We did, but mlock(2) on Linux doesn't guarantee that there will be no page
faults, it only guarantees that there will be no page faults which require
I/O to satisfy (~= it won't swap the page out).

Some text calls faults which require I/O "major" vs "minor" page faults
which don't need I/O to satisfy.

But Linux does things which can result in a supposedly locked PTE being not
present or not-writable, which results minor page faults. The main cause of
that is CoW due to fork (which affects both the parent and child). Other
ones could include being in the middle of moving pages between NUMA nodes
(current/source page is R/O) and THP (likewise content moving around).

BSD says (https://www.freebsd.org/cgi/man.cgi?query=mlock):

     After an mlock() system call, the indicated pages will cause neither a
     non-resident page nor address-translation fault until they	are unlocked.

which I think rules out both major and minor faults (i.e. faults of any
kind, which is what we need to pass the memory through to Xen).

Using madvise() instead of mlock() on Linux is really just a hack, in that
it gets us closer to the semantics we need, but isn't really the same.
Really we ought to provide a /dev/xen/foo from which memory can be
allocated with the right properties (much like /dev/xen/gntalloc does for
grantable memory).

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-19 15:44 ` [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
  2016-01-22 10:42   ` Ian Campbell
@ 2016-01-22 10:42   ` Ian Campbell
  1 sibling, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-22 10:42 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

On Tue, 2016-01-19 at 15:44 +0000, Ian Campbell wrote:
> On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:
> >  
> > Therefore needing attention from Ian and/or Wei are:
> > 
> > 	tools/libs/foreignmemory: Mention restrictions on fork in docs.
> > N	tools/libs/evtchn: Use uint32_t for domid arguments
> >     D	tools/libs/gnttab: Extensive updates to API documentation.
> >       tools/libs/call: linux: touch newly allocated pages after madvise
> > l
> > 	tools/libs/{call,evtchn}: Document requirements around forking.
> >    R	tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
> 
> Thanks to Wei for acking all of these. This set of series is now ready to
> go in, but we've not had a push for a little while and this is
> potentially
> disruptive so I'm going to hold off for now until we get a push.

We've now had a push in 78610 so I'm going to go ahead with applying this
mass of patches today.

> There are one or two patches which will require rebasing over Jeurgens
> introduction of tools/helpers, I'll resend just those ones though (or at
> least only the Xen part of this series).

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-19 15:44 ` [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
@ 2016-01-22 10:42   ` Ian Campbell
  2016-01-22 10:42   ` [Qemu-devel] " Ian Campbell
  1 sibling, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-22 10:42 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

On Tue, 2016-01-19 at 15:44 +0000, Ian Campbell wrote:
> On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:
> >  
> > Therefore needing attention from Ian and/or Wei are:
> > 
> > 	tools/libs/foreignmemory: Mention restrictions on fork in docs.
> > N	tools/libs/evtchn: Use uint32_t for domid arguments
> >     D	tools/libs/gnttab: Extensive updates to API documentation.
> >       tools/libs/call: linux: touch newly allocated pages after madvise
> > l
> > 	tools/libs/{call,evtchn}: Document requirements around forking.
> >    R	tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
> 
> Thanks to Wei for acking all of these. This set of series is now ready to
> go in, but we've not had a push for a little while and this is
> potentially
> disruptive so I'm going to hold off for now until we get a push.

We've now had a push in 78610 so I'm going to go ahead with applying this
mass of patches today.

> There are one or two patches which will require rebasing over Jeurgens
> introduction of tools/helpers, I'll resend just those ones though (or at
> least only the Xen part of this series).

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-15 13:22   ` [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn Ian Campbell
@ 2016-01-22 11:48     ` Ian Campbell
  2016-01-22 17:12     ` Boris Ostrovsky
  1 sibling, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-22 11:48 UTC (permalink / raw)
  To: ian.jackson, wei.liu2, xen-devel

On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:

This is the only patch which changed meaningfully in v9, so I'm just
posting it that one below FTR, intra-log is:

v9: Refactor over "xenstore: move init-xenstore-domain to
    tools/helpers" this highlighted that init-xenstore-domain has no
    need for LDLIBS_xenevtchn and never did, so that hunk is simply
    dropped.

Only other interesting thing in v9 would have been #05 "tools: Refactor
/dev/xen/gnt{dev,shr} wrappers into libxengnttab." which needed some
conflict resolution due to "xenstore: move init-xenstore-domain to
tools/helpers" but that was all.

Ian.

8<-------

From f63312c8d92c728ff50ee3e5777c2ef7e59aaf3f Mon Sep 17 00:00:00 2001
From: Ian Campbell <ian.campbell@citrix.com>
Date: Mon, 1 Jun 2015 16:20:09 +0100
Subject: [PATCH] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

libxenevtchn will provide a stable API and ABI for accessing the
evtchn device.

The functions are moved into the xenevtchn namespace to make a clean
break from libxc and avoid ambiguity regarding which interfaces are
stable.

All in-tree users are updated to use the new names.

Upon request (via #define XC_WANT_COMPAT_EVTCHN_API) libxenctrl will
provide a compat API for the old names. This is used by qemu-xen for
the time being. qemu-xen-traditional is updated in lockstep.

This leaves a few event channel related functions which go via privcmd
(EVTCHNOP) rather than ioctls on the /dev/xen/evtchn device in
libxenctrl. Specifically:

 - xc_evtchn_alloc_unbound
 - xc_evtchn_reset
 - xc_evtchn_status

Note that xc_evtchn_alloc_unbound's functionality is also provided by
xenevtchn_bind_unbound_port() (née xc_evtchn_bind_unbound_port) and is
probably redundant.

These functions do not appear to be needed by qemu-dm, qemu-pv
(provision of device model to HVM guests and PV backends respectively)
or by libvchan suggesting they are not needed by non-toolstack uses of
event channels. QEMU does use these in hw/xenpv/xen_domainbuild.c but
that is a "toolstack use".

The new library uses a version script to ensure that only expected
symbols are exported and to version them such that ABI guarantees can
be kept in the future.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
---

Must be applied with:

 - "qemu-xen-traditional: Use libxenevtchn" and a corresponding
   QEMU_TAG update folded here.
 - "mini-os: Include libxenevtchn with libxc"" and a corresponding
   bump to MINIOS_UPSTREAM_REVISION folded in here.

v2: Update doc at same time
    Removed some stray compat thing
v3: Moved to tools/libs/evtchn
v4: typedef the xenevtchn_handle in xenguest.h instead of #include, to
    avoid leaking the libxenevtchn namespace into callers who may not
    want it. (And also to avoid adding lots of -I to places like
    stubdom builds)
v5: Handle NULL passed to _close()
v6: Properly port xen-access to new API (instead of a single function call)
v7: Added mk-headers-$(XEN_TARGET_ARCH) build dependency instead of
    open coding the recursion.
v8: Remove *.so on clean, add distclean target.
v9: Refactor over "xenstore: move init-xenstore-domain to
    tools/helpers" this highlighted that init-xenstore-domain has no
    need for LDLIBS_xenevtchn and never did, so that hunk is simply
    dropped.
---
 .gitignore                                    |   1 +
 stubdom/Makefile                              |  17 +-
 tools/Makefile                                |   5 +-
 tools/Rules.mk                                |  14 +-
 tools/console/Makefile                        |   2 +
 tools/console/daemon/io.c                     |  43 ++--
 tools/libs/Makefile                           |   1 +
 tools/libs/evtchn/Makefile                    |  69 +++++++
 tools/libs/evtchn/core.c                      |  72 +++++++
 tools/libs/evtchn/freebsd.c                   | 138 +++++++++++++
 tools/libs/evtchn/include/xenevtchn.h         | 150 ++++++++++++++
 tools/libs/evtchn/libxenevtchn.map            |  19 ++
 tools/libs/evtchn/linux.c                     | 136 +++++++++++++
 tools/libs/evtchn/minios.c                    | 269 ++++++++++++++++++++++++++
 tools/libs/evtchn/netbsd.c                    | 147 ++++++++++++++
 tools/libs/evtchn/private.h                   |  25 +++
 tools/libs/evtchn/solaris.c                   | 135 +++++++++++++
 tools/libs/toollog/include/xentoollog.h       |  10 +
 tools/libs/toollog/xtl_core.c                 |  10 +
 tools/libs/toollog/xtl_logger_stdio.c         |  10 +
 tools/libvchan/Makefile                       |   6 +-
 tools/libvchan/init.c                         |  20 +-
 tools/libvchan/io.c                           |  12 +-
 tools/libvchan/libxenvchan.h                  |   3 +-
 tools/libxc/Makefile                          |   8 +-
 tools/libxc/include/xenctrl.h                 |  99 +---------
 tools/libxc/include/xenctrl_compat.h          |  48 +++++
 tools/libxc/include/xenguest.h                |  14 +-
 tools/libxc/xc_evtchn_compat.c                |  75 +++++++
 tools/libxc/xc_freebsd_osdep.c                | 101 ----------
 tools/libxc/xc_linux_osdep.c                  |  95 ---------
 tools/libxc/xc_minios.c                       | 221 ---------------------
 tools/libxc/xc_netbsd.c                       | 109 -----------
 tools/libxc/xc_private.c                      |  40 ----
 tools/libxc/xc_private.h                      |   7 -
 tools/libxc/xc_solaris.c                      |  97 ----------
 tools/libxc/xc_suspend.c                      |  18 +-
 tools/libxl/Makefile                          |   5 +-
 tools/libxl/libxl.c                           |   2 +-
 tools/libxl/libxl_dom_suspend.c               |   4 +-
 tools/libxl/libxl_event.c                     |  14 +-
 tools/libxl/libxl_internal.h                  |   5 +-
 tools/misc/Makefile                           |   7 +-
 tools/misc/xen-hptool.c                       |  13 +-
 tools/misc/xen-lowmemd.c                      |  15 +-
 tools/ocaml/libs/eventchn/Makefile            |   4 +-
 tools/ocaml/libs/eventchn/xeneventchn_stubs.c |  20 +-
 tools/python/setup.py                         |   7 +-
 tools/tests/xen-access/Makefile               |   3 +-
 tools/tests/xen-access/xen-access.c           |  21 +-
 tools/xcutils/Makefile                        |   4 +-
 tools/xenmon/Makefile                         |   2 +
 tools/xenmon/xenbaked.c                       |  13 +-
 tools/xenpaging/Makefile                      |   4 +-
 tools/xenpaging/xenpaging.c                   |  18 +-
 tools/xenpaging/xenpaging.h                   |   3 +-
 tools/xenstore/Makefile                       |   3 +-
 tools/xenstore/xenstored_core.c               |   6 +-
 tools/xenstore/xenstored_domain.c             |  25 +--
 tools/xentrace/Makefile                       |   5 +-
 tools/xentrace/xentrace.c                     |  13 +-
 61 files changed, 1538 insertions(+), 924 deletions(-)
 create mode 100644 tools/libs/evtchn/Makefile
 create mode 100644 tools/libs/evtchn/core.c
 create mode 100644 tools/libs/evtchn/freebsd.c
 create mode 100644 tools/libs/evtchn/include/xenevtchn.h
 create mode 100644 tools/libs/evtchn/libxenevtchn.map
 create mode 100644 tools/libs/evtchn/linux.c
 create mode 100644 tools/libs/evtchn/minios.c
 create mode 100644 tools/libs/evtchn/netbsd.c
 create mode 100644 tools/libs/evtchn/private.h
 create mode 100644 tools/libs/evtchn/solaris.c
 create mode 100644 tools/libxc/include/xenctrl_compat.h
 create mode 100644 tools/libxc/xc_evtchn_compat.c

diff --git a/.gitignore b/.gitignore
index 141c2be..e7af9f7 100644
--- a/.gitignore
+++ b/.gitignore
@@ -62,6 +62,7 @@ stubdom/ioemu
 stubdom/ioemu/
 stubdom/libs-*
 stubdom/libxc-*
+stubdom/libxenevtchn-*
 stubdom/libxentoollog-*
 stubdom/lwip-*
 stubdom/lwip/
diff --git a/stubdom/Makefile b/stubdom/Makefile
index 9c77205..702d66b 100644
--- a/stubdom/Makefile
+++ b/stubdom/Makefile
@@ -319,6 +319,12 @@ mk-headers-$(XEN_TARGET_ARCH): $(IOEMU_LINKFARM_TARGET)
 	  ln -sf $(XEN_ROOT)/tools/libs/toollog/include/*.h . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/toollog/*.c . && \
 	  ln -sf $(XEN_ROOT)/tools/libs/toollog/Makefile . )
+	mkdir -p libs-$(XEN_TARGET_ARCH)/evtchn/include
+	[ -h libs-$(XEN_TARGET_ARCH)/evtchn/Makefile ] || ( cd libs-$(XEN_TARGET_ARCH)/evtchn && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/*.h . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/include/*.h include/ && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/*.c . && \
+	  ln -sf $(XEN_ROOT)/tools/libs/evtchn/Makefile . )
 	mkdir -p libxc-$(XEN_TARGET_ARCH)
 	[ -h libxc-$(XEN_TARGET_ARCH)/Makefile ] || ( cd libxc-$(XEN_TARGET_ARCH) && \
 	  ln -sf $(XEN_ROOT)/tools/libxc/*.h . && \
@@ -351,12 +357,21 @@ libs-$(XEN_TARGET_ARCH)/toollog/libxentoollog.a: mk-headers-$(XEN_TARGET_ARCH) $
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/toollog
 
 #######
+# libxenevtchn
+#######
+
+.PHONY: libxenevtchn
+libxenevtchn: libs-$(XEN_TARGET_ARCH)/evtchn/libxenevtchn.a
+libs-$(XEN_TARGET_ARCH)/evtchn/libxenevtchn.a: mk-headers-$(XEN_TARGET_ARCH) $(NEWLIB_STAMPFILE)
+	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= -C libs-$(XEN_TARGET_ARCH)/evtchn
+
+#######
 # libxc
 #######
 
 .PHONY: libxc
 libxc: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a libxc-$(XEN_TARGET_ARCH)/libxenguest.a
-libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog cross-zlib
+libxc-$(XEN_TARGET_ARCH)/libxenctrl.a: mk-headers-$(XEN_TARGET_ARCH) libxentoollog libxenevtchn cross-zlib
 	CPPFLAGS="$(TARGET_CPPFLAGS)" CFLAGS="$(TARGET_CFLAGS)" $(MAKE) DESTDIR= CONFIG_LIBXC_MINIOS=y -C libxc-$(XEN_TARGET_ARCH)
 
  libxc-$(XEN_TARGET_ARCH)/libxenguest.a: libxc-$(XEN_TARGET_ARCH)/libxenctrl.a
diff --git a/tools/Makefile b/tools/Makefile
index ccd0ae7..55b5d44 100644
--- a/tools/Makefile
+++ b/tools/Makefile
@@ -249,8 +249,10 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		--libdir=$(LIBEXEC_LIB) \
 		--includedir=$(LIBEXEC_INC) \
 		--source-path=$$source \
-		--extra-cflags="-I$(XEN_ROOT)/tools/include \
+		--extra-cflags="-DXC_WANT_COMPAT_EVTCHN_API=1 \
+		-I$(XEN_ROOT)/tools/include \
 		-I$(XEN_ROOT)/tools/libs/toollog/include \
+		-I$(XEN_ROOT)/tools/libs/evtchn/include \
 		-I$(XEN_ROOT)/tools/libxc/include \
 		-I$(XEN_ROOT)/tools/xenstore/include \
 		-I$(XEN_ROOT)/tools/xenstore/compat/include \
@@ -258,6 +260,7 @@ subdir-all-qemu-xen-dir: qemu-xen-dir-find
 		--extra-ldflags="-L$(XEN_ROOT)/tools/libxc \
 		-L$(XEN_ROOT)/tools/xenstore \
 		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/toollog \
+		-Wl,-rpath-link=$(XEN_ROOT)/tools/libs/evtchn \
 		$(QEMU_UPSTREAM_RPATH)" \
 		--bindir=$(LIBEXEC_BIN) \
 		--datadir=$(SHAREDIR)/qemu-xen \
diff --git a/tools/Rules.mk b/tools/Rules.mk
index 0652e4b..75d02c4 100644
--- a/tools/Rules.mk
+++ b/tools/Rules.mk
@@ -11,6 +11,7 @@ INSTALL = $(XEN_ROOT)/tools/cross-install
 
 XEN_INCLUDE        = $(XEN_ROOT)/tools/include
 XEN_LIBXENTOOLLOG  = $(XEN_ROOT)/tools/libs/toollog
+XEN_LIBXENEVTCHN   = $(XEN_ROOT)/tools/libs/evtchn
 XEN_LIBXC          = $(XEN_ROOT)/tools/libxc
 XEN_XENLIGHT       = $(XEN_ROOT)/tools/libxl
 XEN_XENSTORE       = $(XEN_ROOT)/tools/xenstore
@@ -82,13 +83,18 @@ SHDEPS_libxentoollog =
 LDLIBS_libxentoollog = $(XEN_LIBXENTOOLLOG)/libxentoollog$(libextension)
 SHLIB_libxentoollog  = -Wl,-rpath-link=$(XEN_LIBXENTOOLLOG)
 
+CFLAGS_libxenevtchn = -I$(XEN_LIBXENEVTCHN)/include $(CFLAGS_xeninclude)
+SHDEPS_libxenevtchn =
+LDLIBS_libxenevtchn = $(XEN_LIBXENEVTCHN)/libxenevtchn$(libextension)
+SHLIB_libxenevtchn  = -Wl,-rpath-link=$(XEN_LIBXENEVTCHN)
+
 CFLAGS_libxenctrl = -I$(XEN_LIBXC)/include $(CFLAGS_libxentoollog) $(CFLAGS_xeninclude)
-SHDEPS_libxenctrl = $(SHLIB_libxentoollog)
+SHDEPS_libxenctrl = $(SHLIB_libxentoollog) $(SHLIB_libxenevtchn)
 LDLIBS_libxenctrl = $(SHDEPS_libxenctrl) $(XEN_LIBXC)/libxenctrl$(libextension)
 SHLIB_libxenctrl  = $(SHDEPS_libxenctrl) -Wl,-rpath-link=$(XEN_LIBXC)
 
-CFLAGS_libxenguest = -I$(XEN_LIBXC)/include $(CFLAGS_xeninclude)
-SHDEPS_libxenguest =
+CFLAGS_libxenguest = -I$(XEN_LIBXC)/include $(CFLAGS_libxenevtchn) $(CFLAGS_xeninclude)
+SHDEPS_libxenguest = $(SHLIB_libxenevtchn)
 LDLIBS_libxenguest = $(SHDEPS_libxenguest) $(XEN_LIBXC)/libxenguest$(libextension)
 SHLIB_libxenguest  = $(SHDEPS_libxenguest) -Wl,-rpath-link=$(XEN_LIBXC)
 
@@ -103,7 +109,7 @@ LDLIBS_libxenstat  = $(SHDEPS_libxenstat) $(XEN_LIBXENSTAT)/libxenstat$(libexten
 SHLIB_libxenstat   = $(SHDEPS_libxenstat) -Wl,-rpath-link=$(XEN_LIBXENSTAT)
 
 CFLAGS_libxenvchan = -I$(XEN_LIBVCHAN)
-SHDEPS_libxenvchan = $(SHLIB_libxenctrl) $(SHLIB_libxenstore)
+SHDEPS_libxenvchan = $(SHLIB_libxenctrl) $(SHLIB_libxenstore) $(SHLIB_libxenevtchn)
 LDLIBS_libxenvchan = $(SHDEPS_libxenvchan) $(XEN_LIBVCHAN)/libxenvchan$(libextension)
 SHLIB_libxenvchan  = $(SHDEPS_libxenvchan) -Wl,-rpath-link=$(XEN_LIBVCHAN)
 
diff --git a/tools/console/Makefile b/tools/console/Makefile
index 77e8f29..4b3a492 100644
--- a/tools/console/Makefile
+++ b/tools/console/Makefile
@@ -3,8 +3,10 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS  += -Werror
 
+CFLAGS  += $(CFLAGS_libxenevtchn)
 CFLAGS  += $(CFLAGS_libxenctrl)
 CFLAGS  += $(CFLAGS_libxenstore)
+LDLIBS += $(LDLIBS_libxenevtchn)
 LDLIBS += $(LDLIBS_libxenctrl)
 LDLIBS += $(LDLIBS_libxenstore)
 LDLIBS += $(SOCKET_LIBS)
diff --git a/tools/console/daemon/io.c b/tools/console/daemon/io.c
index cafc7b7..2f2e9c5 100644
--- a/tools/console/daemon/io.c
+++ b/tools/console/daemon/io.c
@@ -21,6 +21,7 @@
 
 #include "utils.h"
 #include "io.h"
+#include <xenevtchn.h>
 #include <xenstore.h>
 #include <xen/io/console.h>
 #include <xen/grant_table.h>
@@ -101,7 +102,7 @@ struct domain {
 	int ring_ref;
 	evtchn_port_or_error_t local_port;
 	evtchn_port_or_error_t remote_port;
-	xc_evtchn *xce_handle;
+	xenevtchn_handle *xce_handle;
 	int xce_pollfd_idx;
 	struct xencons_interface *interface;
 	int event_count;
@@ -185,7 +186,7 @@ static void buffer_append(struct domain *dom)
 
 	xen_mb();
 	intf->out_cons = cons;
-	xc_evtchn_notify(dom->xce_handle, dom->local_port);
+	xenevtchn_notify(dom->xce_handle, dom->local_port);
 
 	/* Get the data to the logfile as early as possible because if
 	 * no one is listening on the console pty then it will fill up
@@ -584,22 +585,22 @@ static int domain_create_ring(struct domain *dom)
 	dom->local_port = -1;
 	dom->remote_port = -1;
 	if (dom->xce_handle != NULL)
-		xc_evtchn_close(dom->xce_handle);
+		xenevtchn_close(dom->xce_handle);
 
 	/* Opening evtchn independently for each console is a bit
 	 * wasteful, but that's how the code is structured... */
-	dom->xce_handle = xc_evtchn_open(NULL, 0);
+	dom->xce_handle = xenevtchn_open(NULL, 0);
 	if (dom->xce_handle == NULL) {
 		err = errno;
 		goto out;
 	}
  
-	rc = xc_evtchn_bind_interdomain(dom->xce_handle,
+	rc = xenevtchn_bind_interdomain(dom->xce_handle,
 		dom->domid, remote_port);
 
 	if (rc == -1) {
 		err = errno;
-		xc_evtchn_close(dom->xce_handle);
+		xenevtchn_close(dom->xce_handle);
 		dom->xce_handle = NULL;
 		goto out;
 	}
@@ -609,7 +610,7 @@ static int domain_create_ring(struct domain *dom)
 	if (dom->master_fd == -1) {
 		if (!domain_create_tty(dom)) {
 			err = errno;
-			xc_evtchn_close(dom->xce_handle);
+			xenevtchn_close(dom->xce_handle);
 			dom->xce_handle = NULL;
 			dom->local_port = -1;
 			dom->remote_port = -1;
@@ -749,7 +750,7 @@ static void shutdown_domain(struct domain *d)
 	watch_domain(d, false);
 	domain_unmap_interface(d);
 	if (d->xce_handle != NULL)
-		xc_evtchn_close(d->xce_handle);
+		xenevtchn_close(d->xce_handle);
 	d->xce_handle = NULL;
 }
 
@@ -839,7 +840,7 @@ static void handle_tty_read(struct domain *dom)
 		}
 		xen_wmb();
 		intf->in_prod = prod;
-		xc_evtchn_notify(dom->xce_handle, dom->local_port);
+		xenevtchn_notify(dom->xce_handle, dom->local_port);
 	} else {
 		domain_close_tty(dom);
 		shutdown_domain(dom);
@@ -871,7 +872,7 @@ static void handle_ring_read(struct domain *dom)
 	if (dom->is_dead)
 		return;
 
-	if ((port = xc_evtchn_pending(dom->xce_handle)) == -1)
+	if ((port = xenevtchn_pending(dom->xce_handle)) == -1)
 		return;
 
 	dom->event_count++;
@@ -879,7 +880,7 @@ static void handle_ring_read(struct domain *dom)
 	buffer_append(dom);
 
 	if (dom->event_count < RATE_LIMIT_ALLOWANCE)
-		(void)xc_evtchn_unmask(dom->xce_handle, port);
+		(void)xenevtchn_unmask(dom->xce_handle, port);
 }
 
 static void handle_xs(void)
@@ -906,7 +907,7 @@ static void handle_xs(void)
 	free(vec);
 }
 
-static void handle_hv_logs(xc_evtchn *xce_handle, bool force)
+static void handle_hv_logs(xenevtchn_handle *xce_handle, bool force)
 {
 	static char buffer[1024*16];
 	char *bufptr = buffer;
@@ -914,7 +915,7 @@ static void handle_hv_logs(xc_evtchn *xce_handle, bool force)
 	static uint32_t index = 0;
 	evtchn_port_or_error_t port = -1;
 
-	if (!force && ((port = xc_evtchn_pending(xce_handle)) == -1))
+	if (!force && ((port = xenevtchn_pending(xce_handle)) == -1))
 		return;
 
 	do
@@ -938,7 +939,7 @@ static void handle_hv_logs(xc_evtchn *xce_handle, bool force)
 	} while (size == sizeof(buffer));
 
 	if (port != -1)
-		(void)xc_evtchn_unmask(xce_handle, port);
+		(void)xenevtchn_unmask(xce_handle, port);
 }
 
 static void handle_log_reload(void)
@@ -1006,10 +1007,10 @@ void handle_io(void)
 	evtchn_port_or_error_t log_hv_evtchn = -1;
 	int xce_pollfd_idx = -1;
 	int xs_pollfd_idx = -1;
-	xc_evtchn *xce_handle = NULL;
+	xenevtchn_handle *xce_handle = NULL;
 
 	if (log_hv) {
-		xce_handle = xc_evtchn_open(NULL, 0);
+		xce_handle = xenevtchn_open(NULL, 0);
 		if (xce_handle == NULL) {
 			dolog(LOG_ERR, "Failed to open xce handle: %d (%s)",
 			      errno, strerror(errno));
@@ -1018,7 +1019,7 @@ void handle_io(void)
 		log_hv_fd = create_hv_log();
 		if (log_hv_fd == -1)
 			goto out;
-		log_hv_evtchn = xc_evtchn_bind_virq(xce_handle, VIRQ_CON_RING);
+		log_hv_evtchn = xenevtchn_bind_virq(xce_handle, VIRQ_CON_RING);
 		if (log_hv_evtchn == -1) {
 			dolog(LOG_ERR, "Failed to bind to VIRQ_CON_RING: "
 			      "%d (%s)", errno, strerror(errno));
@@ -1047,7 +1048,7 @@ void handle_io(void)
 		xs_pollfd_idx = set_fds(xs_fileno(xs), POLLIN|POLLPRI);
 
 		if (log_hv)
-			xce_pollfd_idx = set_fds(xc_evtchn_fd(xce_handle),
+			xce_pollfd_idx = set_fds(xenevtchn_fd(xce_handle),
 						 POLLIN|POLLPRI);
 
 		if (clock_gettime(CLOCK_MONOTONIC, &ts) < 0)
@@ -1066,7 +1067,7 @@ void handle_io(void)
 			if ((now+5) > d->next_period) {
 				d->next_period = now + RATE_LIMIT_PERIOD;
 				if (d->event_count >= RATE_LIMIT_ALLOWANCE) {
-					(void)xc_evtchn_unmask(d->xce_handle, d->local_port);
+					(void)xenevtchn_unmask(d->xce_handle, d->local_port);
 				}
 				d->event_count = 0;
 			}
@@ -1082,7 +1083,7 @@ void handle_io(void)
 				if (discard_overflowed_data ||
 				    !d->buffer.max_capacity ||
 				    d->buffer.size < d->buffer.max_capacity) {
-					int evtchn_fd = xc_evtchn_fd(d->xce_handle);
+					int evtchn_fd = xenevtchn_fd(d->xce_handle);
 					d->xce_pollfd_idx = set_fds(evtchn_fd,
 								    POLLIN|POLLPRI);
 				}
@@ -1202,7 +1203,7 @@ void handle_io(void)
 		log_hv_fd = -1;
 	}
 	if (xce_handle != NULL) {
-		xc_evtchn_close(xce_handle);
+		xenevtchn_close(xce_handle);
 		xce_handle = NULL;
 	}
 	if (xcg_handle != NULL) {
diff --git a/tools/libs/Makefile b/tools/libs/Makefile
index 73be500..0e3f523 100644
--- a/tools/libs/Makefile
+++ b/tools/libs/Makefile
@@ -3,5 +3,6 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 SUBDIRS-y :=
 SUBDIRS-y += toollog
+SUBDIRS-y += evtchn
 
 all clean install distclean: %: subdirs-%
diff --git a/tools/libs/evtchn/Makefile b/tools/libs/evtchn/Makefile
new file mode 100644
index 0000000..46a807f
--- /dev/null
+++ b/tools/libs/evtchn/Makefile
@@ -0,0 +1,69 @@
+XEN_ROOT = $(CURDIR)/../../..
+include $(XEN_ROOT)/tools/Rules.mk
+
+MAJOR    = 1
+MINOR    = 0
+SHLIB_LDFLAGS += -Wl,--version-script=libxenevtchn.map
+
+CFLAGS   += -Werror -Wmissing-prototypes
+CFLAGS   += -I./include $(CFLAGS_xeninclude)
+CFLAGS   += $(CFLAGS_libxentoollog)
+
+SRCS-y                 += core.c
+SRCS-$(CONFIG_Linux)   += linux.c
+SRCS-$(CONFIG_FreeBSD) += freebsd.c
+SRCS-$(CONFIG_SunOS)   += solaris.c
+SRCS-$(CONFIG_NetBSD)  += netbsd.c
+SRCS-$(CONFIG_MiniOS)  += minios.c
+
+LIB_OBJS := $(patsubst %.c,%.o,$(SRCS-y))
+PIC_OBJS := $(patsubst %.c,%.opic,$(SRCS-y))
+
+LIB := libxenevtchn.a
+ifneq ($(nosharedlibs),y)
+LIB += libxenevtchn.so
+endif
+
+.PHONY: all
+all: build
+
+.PHONY: build
+build:
+	$(MAKE) libs
+
+.PHONY: libs
+libs: $(LIB)
+
+
+libxenevtchn.a: $(LIB_OBJS)
+	$(AR) rc $@ $^
+
+libxenevtchn.so: libxenevtchn.so.$(MAJOR)
+	$(SYMLINK_SHLIB) $< $@
+libxenevtchn.so.$(MAJOR): libxenevtchn.so.$(MAJOR).$(MINOR)
+	$(SYMLINK_SHLIB) $< $@
+
+libxenevtchn.so.$(MAJOR).$(MINOR): $(PIC_OBJS) libxenevtchn.map
+	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenevtchn.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(PIC_OBJS) $(LDLIBS_libxentoollog) $(APPEND_LDFLAGS)
+
+.PHONY: install
+install: build
+	$(INSTALL_DIR) $(DESTDIR)$(libdir)
+	$(INSTALL_DIR) $(DESTDIR)$(includedir)
+	$(INSTALL_SHLIB) libxenevtchn.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
+	$(INSTALL_DATA) libxenevtchn.a $(DESTDIR)$(libdir)
+	$(SYMLINK_SHLIB) libxenevtchn.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenevtchn.so.$(MAJOR)
+	$(SYMLINK_SHLIB) libxenevtchn.so.$(MAJOR) $(DESTDIR)$(libdir)/libxenevtchn.so
+	$(INSTALL_DATA) include/xenevtchn.h $(DESTDIR)$(includedir)
+
+.PHONY: TAGS
+TAGS:
+	etags -t *.c *.h
+
+.PHONY: clean
+clean:
+	rm -rf *.rpm $(LIB) *~ $(DEPS) $(LIB_OBJS) $(PIC_OBJS)
+	rm -f libxenevtchn.so.$(MAJOR).$(MINOR) libxenevtchn.so.$(MAJOR)
+
+.PHONY: distclean
+distclean: clean
diff --git a/tools/libs/evtchn/core.c b/tools/libs/evtchn/core.c
new file mode 100644
index 0000000..c31e08c
--- /dev/null
+++ b/tools/libs/evtchn/core.c
@@ -0,0 +1,72 @@
+/*
+ * 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 <unistd.h>
+#include <stdlib.h>
+
+#include "private.h"
+
+xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags)
+{
+    xenevtchn_handle *xce = malloc(sizeof(*xce));
+    int rc;
+
+    if (!xce) return NULL;
+
+    xce->fd = -1;
+    xce->logger = logger;
+    xce->logger_tofree  = NULL;
+
+    if (!xce->logger) {
+        xce->logger = xce->logger_tofree =
+            (xentoollog_logger*)
+            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
+        if (!xce->logger) goto err;
+    }
+
+    rc = osdep_evtchn_open(xce);
+    if ( rc  < 0 ) goto err;
+
+    return xce;
+
+err:
+    osdep_evtchn_close(xce);
+    xtl_logger_destroy(xce->logger_tofree);
+    free(xce);
+    return NULL;
+}
+
+int xenevtchn_close(xenevtchn_handle *xce)
+{
+    int rc;
+
+    if ( !xce )
+        return 0;
+
+    rc = osdep_evtchn_close(xce);
+    xtl_logger_destroy(xce->logger_tofree);
+    free(xce);
+    return rc;
+}
+
+/*
+ * 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/evtchn/freebsd.c b/tools/libs/evtchn/freebsd.c
new file mode 100644
index 0000000..636f052
--- /dev/null
+++ b/tools/libs/evtchn/freebsd.c
@@ -0,0 +1,138 @@
+ /******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split off from xc_freebsd_osdep.c
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+#define EVTCHN_DEV      "/dev/xen/evtchn"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = open(EVTCHN_DEV, O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+{
+    int ret, fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+
+    bind.remote_domain = domid;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+    return ( ret == 0 ) ? bind.port : ret;
+}
+
+evtchn_port_or_error_t
+xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid, evtchn_port_t remote_port)
+{
+    int ret, fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+    return ( ret == 0 ) ? bind.port : ret;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int ret, fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+
+    bind.virq = virq;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+    return ( ret == 0 ) ? bind.port : ret;
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+
+    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+    return 0;
+}
+
+/*
+ * 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/evtchn/include/xenevtchn.h b/tools/libs/evtchn/include/xenevtchn.h
new file mode 100644
index 0000000..3380fa3
--- /dev/null
+++ b/tools/libs/evtchn/include/xenevtchn.h
@@ -0,0 +1,150 @@
+/*
+ * 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/>.
+ *
+ * Split off from:
+ * xenctrl.h
+ *
+ * A library for low-level access to the Xen control interfaces.
+ *
+ * Copyright (c) 2003-2004, K A Fraser.
+ */
+
+#ifndef XENEVTCHN_H
+#define XENEVTCHN_H
+
+#include <stdint.h>
+
+#include <xen/event_channel.h>
+
+/* A port identifier is guaranteed to fit in 31 bits. */
+typedef int evtchn_port_or_error_t;
+
+typedef struct xenevtchn_handle xenevtchn_handle;
+
+/* Callers who don't care don't need to #include <xentoollog.h> */
+typedef struct xentoollog_logger xentoollog_logger;
+
+/*
+ * EVENT CHANNEL FUNCTIONS
+ *
+ * None of these do any logging.
+ */
+
+/*
+ * Return a handle to the event channel driver, or NULL on failure, in
+ * which case errno will be set appropriately.
+ *
+ * Note:
+ * After fork a child process must not use any opened xc evtchn
+ * handle inherited from their parent. They must open a new handle if
+ * they want to interact with xc.
+ *
+ * Before Xen pre-4.1 this function would sometimes report errors with perror.
+ */
+/* Currently no flags are defined */
+xenevtchn_handle *xenevtchn_open(xentoollog_logger *logger, unsigned open_flags);
+
+/*
+ * Close a handle previously allocated with xenevtchn_open().
+ */
+int xenevtchn_close(xenevtchn_handle *xce);
+
+/*
+ * Return an fd that can be select()ed on.
+ *
+ * Note that due to bugs, setting this fd to non blocking may not
+ * work: you would hope that it would result in xenevtchn_pending
+ * failing with EWOULDBLOCK if there are no events signaled, but in
+ * fact it may block.  (Bug is present in at least Linux 3.12, and
+ * perhaps on other platforms or later version.)
+ *
+ * To be safe, you must use poll() or select() before each call to
+ * xenevtchn_pending.  If you have multiple threads (or processes)
+ * sharing a single xce handle this will not work, and there is no
+ * straightforward workaround.  Please design your program some other
+ * way.
+ */
+int xenevtchn_fd(xenevtchn_handle *xce);
+
+/*
+ * Notify the given event channel. Returns -1 on failure, in which case
+ * errno will be set appropriately.
+ */
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port);
+
+/*
+ * Returns a new event port awaiting interdomain connection from the given
+ * domain ID, or -1 on failure, in which case errno will be set appropriately.
+ */
+evtchn_port_or_error_t
+xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid);
+
+/*
+ * Returns a new event port bound to the remote port for the given domain ID,
+ * or -1 on failure, in which case errno will be set appropriately.
+ */
+evtchn_port_or_error_t
+xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                           evtchn_port_t remote_port);
+
+/*
+ * Bind an event channel to the given VIRQ. Returns the event channel bound to
+ * the VIRQ, or -1 on failure, in which case errno will be set appropriately.
+ */
+evtchn_port_or_error_t
+xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq);
+
+/*
+ * Unbind the given event channel. Returns -1 on failure, in which case errno
+ * will be set appropriately.
+ */
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port);
+
+/*
+ * Return the next event channel to become pending, or -1 on failure, in which
+ * case errno will be set appropriately.
+ *
+ * At the hypervisor level the event channel will have been masked,
+ * and then cleared, by the underlying machinery (evtchn kernel
+ * driver, or equivalent).  So if the event channel is signaled again
+ * after it is returned here, it will be queued up, and delivered
+ * again after you unmask it.  (See the documentation in the Xen
+ * public header event_channel.h.)
+ *
+ * On receiving the notification from xenevtchn_pending, you should
+ * normally: check (by other means) what work needs doing; do the
+ * necessary work (if any); unmask the event channel with
+ * xenevtchn_unmask (if you want to receive any further
+ * notifications).
+ */
+evtchn_port_or_error_t
+xenevtchn_pending(xenevtchn_handle *xce);
+
+/*
+ * Unmask the given event channel. Returns -1 on failure, in which case errno
+ * will be set appropriately.
+ */
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port);
+
+#endif
+
+/*
+ * 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/evtchn/libxenevtchn.map b/tools/libs/evtchn/libxenevtchn.map
new file mode 100644
index 0000000..625a1e2
--- /dev/null
+++ b/tools/libs/evtchn/libxenevtchn.map
@@ -0,0 +1,19 @@
+VERS_1.0 {
+	global:
+		xenevtchn_open;
+		xenevtchn_close;
+
+		xenevtchn_fd;
+
+		xenevtchn_bind_unbound_port;
+		xenevtchn_bind_interdomain;
+		xenevtchn_bind_virq;
+
+		xenevtchn_notify;
+
+		xenevtchn_unbind;
+		xenevtchn_unmask;
+
+		xenevtchn_pending;
+	local: *; /* Do not expose anything by default */
+};
diff --git a/tools/libs/evtchn/linux.c b/tools/libs/evtchn/linux.c
new file mode 100644
index 0000000..27fd6e9
--- /dev/null
+++ b/tools/libs/evtchn/linux.c
@@ -0,0 +1,136 @@
+/*
+ * 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/>.
+ *
+ * Split out from xc_linus_osdep.c:
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ */
+
+#include <errno.h>
+#include <fcntl.h>
+#include <unistd.h>
+#include <stdlib.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = open("/dev/xen/evtchn", O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce,
+                                                   int domid)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+
+    bind.remote_domain = domid;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce,
+                                                  int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce,
+                                           unsigned int virq)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+
+    bind.virq = virq;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+
+    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
+        return -1;
+    return 0;
+}
+
+/*
+ * 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/evtchn/minios.c b/tools/libs/evtchn/minios.c
new file mode 100644
index 0000000..fb913a2
--- /dev/null
+++ b/tools/libs/evtchn/minios.c
@@ -0,0 +1,269 @@
+/******************************************************************************
+ *
+ * Copyright 2007-2008 Samuel Thibault <samuel.thibault@eu.citrix.com>.
+ * All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split off from xc_minios.c
+ */
+
+#include "xen-external/bsd-sys-queue.h"
+#include <mini-os/types.h>
+#include <mini-os/os.h>
+#include <mini-os/lib.h>
+#include <mini-os/events.h>
+#include <mini-os/wait.h>
+
+#include <sys/socket.h>
+
+#include <assert.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <inttypes.h>
+#include <malloc.h>
+
+#include "private.h"
+
+extern void minios_evtchn_close_fd(int fd);
+
+extern struct wait_queue_head event_queue;
+
+//void minios_evtchn_close_fd(int fd);
+
+/* XXX Note: This is not threadsafe */
+static struct evtchn_port_info* port_alloc(int fd) {
+    struct evtchn_port_info *port_info;
+    port_info = malloc(sizeof(struct evtchn_port_info));
+    if (port_info == NULL)
+        return NULL;
+    port_info->pending = 0;
+    port_info->port = -1;
+    port_info->bound = 0;
+
+    LIST_INSERT_HEAD(&files[fd].evtchn.ports, port_info, list);
+    return port_info;
+}
+
+static void port_dealloc(struct evtchn_port_info *port_info) {
+    if (port_info->bound)
+        unbind_evtchn(port_info->port);
+    LIST_REMOVE(port_info, list);
+    free(port_info);
+}
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = alloc_fd(FTYPE_EVTCHN);
+    if ( fd == -1 )
+        return -1;
+    LIST_INIT(&files[fd].evtchn.ports);
+    xce->fd = fd;
+    printf("evtchn_open() -> %d\n", fd);
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+void minios_evtchn_close_fd(int fd)
+{
+    struct evtchn_port_info *port_info, *tmp;
+    LIST_FOREACH_SAFE(port_info, &files[fd].evtchn.ports, list, tmp)
+        port_dealloc(port_info);
+
+    files[fd].type = FTYPE_NONE;
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int ret;
+
+    ret = notify_remote_via_evtchn(port);
+
+    if (ret < 0) {
+	errno = -ret;
+	ret = -1;
+    }
+    return ret;
+}
+
+static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
+{
+    int fd = (int)(intptr_t)data;
+    struct evtchn_port_info *port_info;
+    assert(files[fd].type == FTYPE_EVTCHN);
+    mask_evtchn(port);
+    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
+        if (port_info->port == port)
+            goto found;
+    }
+    printk("Unknown port for handle %d\n", fd);
+    return;
+
+ found:
+    port_info->pending = 1;
+    files[fd].read = 1;
+    wake_up(&event_queue);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    int ret;
+    evtchn_port_t port;
+
+    assert(get_current() == main_thread);
+    port_info = port_alloc(fd);
+    if (port_info == NULL)
+	return -1;
+
+    printf("xenevtchn_bind_unbound_port(%d)", domid);
+    ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)fd, &port);
+    printf(" = %d\n", ret);
+
+    if (ret < 0) {
+	port_dealloc(port_info);
+	errno = -ret;
+	return -1;
+    }
+    port_info->bound = 1;
+    port_info->port = port;
+    unmask_evtchn(port);
+    return port;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    evtchn_port_t local_port;
+    int ret;
+
+    assert(get_current() == main_thread);
+    port_info = port_alloc(fd);
+    if (port_info == NULL)
+	return -1;
+
+    printf("xenevtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
+    ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)fd, &local_port);
+    printf(" = %d\n", ret);
+
+    if (ret < 0) {
+	port_dealloc(port_info);
+	errno = -ret;
+	return -1;
+    }
+    port_info->bound = 1;
+    port_info->port = local_port;
+    unmask_evtchn(local_port);
+    return local_port;
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+
+    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
+        if (port_info->port == port) {
+            port_dealloc(port_info);
+            return 0;
+        }
+    }
+    printf("Warning: couldn't find port %"PRId32" for xc handle %x\n", port, fd);
+    errno = EINVAL;
+    return -1;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    evtchn_port_t port;
+
+    assert(get_current() == main_thread);
+    port_info = port_alloc(fd);
+    if (port_info == NULL)
+	return -1;
+
+    printf("xenevtchn_bind_virq(%d)", virq);
+    port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)fd);
+
+    if (port < 0) {
+	port_dealloc(port_info);
+	errno = -port;
+	return -1;
+    }
+    port_info->bound = 1;
+    port_info->port = port;
+    unmask_evtchn(port);
+    return port;
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    struct evtchn_port_info *port_info;
+    unsigned long flags;
+    evtchn_port_t ret = -1;
+
+    local_irq_save(flags);
+    files[fd].read = 0;
+
+    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
+        if (port_info->port != -1 && port_info->pending) {
+            if (ret == -1) {
+                ret = port_info->port;
+                port_info->pending = 0;
+            } else {
+                files[fd].read = 1;
+                break;
+            }
+        }
+    }
+    local_irq_restore(flags);
+    return ret;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    unmask_evtchn(port);
+    return 0;
+}
+
+/*
+ * 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/evtchn/netbsd.c b/tools/libs/evtchn/netbsd.c
new file mode 100644
index 0000000..c4123fe
--- /dev/null
+++ b/tools/libs/evtchn/netbsd.c
@@ -0,0 +1,147 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split out from xc_netbsd.c
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+#define EVTCHN_DEV_NAME  "/dev/xenevt"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
+    if ( fd == -1 )
+        return -1;
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle * xce, int domid)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+    int ret;
+
+    bind.remote_domain = domid;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+    if (ret == 0)
+	return bind.port;
+    else
+	return -1;
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+    int ret;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+    if (ret == 0)
+	return bind.port;
+    else
+	return -1;
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+    int err;
+
+    bind.virq = virq;
+
+    err = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+    if (err)
+	return -1;
+    else
+	return bind.port;
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    return write_exact(fd, (char *)&port, sizeof(port));
+}
+
+/*
+ * 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/evtchn/private.h b/tools/libs/evtchn/private.h
new file mode 100644
index 0000000..fcd0e96
--- /dev/null
+++ b/tools/libs/evtchn/private.h
@@ -0,0 +1,25 @@
+#ifndef XENEVTCHN_PRIVATE_H
+#define XENEVTCHN_PRIVATE_H
+
+#include <xentoollog.h>
+#include <xenevtchn.h>
+
+struct xenevtchn_handle {
+    xentoollog_logger *logger, *logger_tofree;
+    int fd;
+};
+
+int osdep_evtchn_open(xenevtchn_handle *xce);
+int osdep_evtchn_close(xenevtchn_handle *xce);
+
+#endif
+
+/*
+ * 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/evtchn/solaris.c b/tools/libs/evtchn/solaris.c
new file mode 100644
index 0000000..114cefb
--- /dev/null
+++ b/tools/libs/evtchn/solaris.c
@@ -0,0 +1,135 @@
+/******************************************************************************
+ *
+ * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
+ * Use is subject to license terms.
+ *
+ * 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/>.
+ *
+ * Split out from xc_solaris.c
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+
+#include <sys/ioctl.h>
+
+#include <xen/sys/evtchn.h>
+
+#include "private.h"
+
+int osdep_evtchn_open(xenevtchn_handle *xce)
+{
+    int fd;
+
+    if ( (fd = open("/dev/xen/evtchn", O_RDWR)) == -1 )
+    {
+        PERROR("Could not open event channel interface");
+        return -1;
+    }
+
+    xce->fd = fd;
+    return 0;
+}
+
+int osdep_evtchn_close(xenevtchn_handle *xce)
+{
+    if ( xce->fd == -1 )
+        return 0;
+
+    return close(xce->fd);
+}
+
+int xenevtchn_fd(xenevtchn_handle *xce)
+{
+    return xce->fd;
+}
+
+int xenevtchn_notify(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_notify notify;
+
+    notify.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_unbound_port(xenevtchn_handle *xce, int domid)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_unbound_port bind;
+
+    bind.remote_domain = domid;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_interdomain(xenevtchn_handle *xce, int domid,
+                                                  evtchn_port_t remote_port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_interdomain bind;
+
+    bind.remote_domain = domid;
+    bind.remote_port = remote_port;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
+}
+
+evtchn_port_or_error_t xenevtchn_bind_virq(xenevtchn_handle *xce, unsigned int virq)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_bind_virq bind;
+
+    bind.virq = virq;
+
+    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
+}
+
+int xenevtchn_unbind(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    struct ioctl_evtchn_unbind unbind;
+
+    unbind.port = port;
+
+    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
+}
+
+evtchn_port_or_error_t xenevtchn_pending(xenevtchn_handle *xce)
+{
+    int fd = xce->fd;
+    evtchn_port_t port;
+
+    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
+        return -1;
+
+    return port;
+}
+
+int xenevtchn_unmask(xenevtchn_handle *xce, evtchn_port_t port)
+{
+    int fd = xce->fd;
+    return write_exact(fd, (char *)&port, sizeof(port));
+}
+
+/*
+ * 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/toollog/include/xentoollog.h b/tools/libs/toollog/include/xentoollog.h
index 853e9c7..76f17fe 100644
--- a/tools/libs/toollog/include/xentoollog.h
+++ b/tools/libs/toollog/include/xentoollog.h
@@ -134,3 +134,13 @@ const char *xtl_level_to_string(xentoollog_level); /* never fails */
 
 
 #endif /* XENTOOLLOG_H */
+
+/*
+ * 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/toollog/xtl_core.c b/tools/libs/toollog/xtl_core.c
index c4724a0..099d2f3 100644
--- a/tools/libs/toollog/xtl_core.c
+++ b/tools/libs/toollog/xtl_core.c
@@ -81,3 +81,13 @@ void xtl_logger_destroy(struct xentoollog_logger *logger) {
     if (!logger) return;
     logger->destroy(logger);
 }
+
+/*
+ * 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/toollog/xtl_logger_stdio.c b/tools/libs/toollog/xtl_logger_stdio.c
index 0cd9206..f9c5bd8 100644
--- a/tools/libs/toollog/xtl_logger_stdio.c
+++ b/tools/libs/toollog/xtl_logger_stdio.c
@@ -190,3 +190,13 @@ xentoollog_logger_stdiostream *xtl_createlogger_stdiostream
 
     return XTL_NEW_LOGGER(stdiostream, newlogger);
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libvchan/Makefile b/tools/libvchan/Makefile
index 3c50fe6..84128a3 100644
--- a/tools/libvchan/Makefile
+++ b/tools/libvchan/Makefile
@@ -10,9 +10,9 @@ NODE_OBJS = node.o
 NODE2_OBJS = node-select.o
 
 LIBVCHAN_PIC_OBJS = $(patsubst %.o,%.opic,$(LIBVCHAN_OBJS))
-LIBVCHAN_LIBS = $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl)
-$(LIBVCHAN_OBJS) $(LIBVCHAN_PIC_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(CFLAGS_libxenctrl)
-$(NODE_OBJS) $(NODE2_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
+LIBVCHAN_LIBS = $(LDLIBS_libxenstore) $(LDLIBS_libxenctrl) $(LDLIBS_libxenevtchn)
+$(LIBVCHAN_OBJS) $(LIBVCHAN_PIC_OBJS): CFLAGS += $(CFLAGS_libxenstore) $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
+$(NODE_OBJS) $(NODE2_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
 
 MAJOR = 1.0
 MINOR = 0
diff --git a/tools/libvchan/init.c b/tools/libvchan/init.c
index 77be4e7..66cb103 100644
--- a/tools/libvchan/init.c
+++ b/tools/libvchan/init.c
@@ -216,25 +216,25 @@ static int init_evt_srv(struct libxenvchan *ctrl, int domain, xentoollog_logger
 {
 	evtchn_port_or_error_t port;
 
-	ctrl->event = xc_evtchn_open(logger, 0);
+	ctrl->event = xenevtchn_open(logger, 0);
 	if (!ctrl->event)
 		return -1;
 
-	port = xc_evtchn_bind_unbound_port(ctrl->event, domain);
+	port = xenevtchn_bind_unbound_port(ctrl->event, domain);
 	if (port < 0)
 		goto fail;
 	ctrl->event_port = port;
 
-	if (xc_evtchn_unmask(ctrl->event, ctrl->event_port))
+	if (xenevtchn_unmask(ctrl->event, ctrl->event_port))
 		goto fail;
 
 	return 0;
 
 fail:
 	if (port >= 0)
-		xc_evtchn_unbind(ctrl->event, port);
+		xenevtchn_unbind(ctrl->event, port);
 
-	xc_evtchn_close(ctrl->event);
+	xenevtchn_close(ctrl->event);
 	ctrl->event = NULL;
 
 	return -1;
@@ -346,26 +346,26 @@ static int init_evt_cli(struct libxenvchan *ctrl, int domain, xentoollog_logger
 {
 	evtchn_port_or_error_t port;
 
-	ctrl->event = xc_evtchn_open(logger, 0);
+	ctrl->event = xenevtchn_open(logger, 0);
 	if (!ctrl->event)
 		return -1;
 
-	port = xc_evtchn_bind_interdomain(ctrl->event,
+	port = xenevtchn_bind_interdomain(ctrl->event,
 		domain, ctrl->event_port);
 	if (port < 0)
 		goto fail;
 	ctrl->event_port = port;
 
-	if (xc_evtchn_unmask(ctrl->event, ctrl->event_port))
+	if (xenevtchn_unmask(ctrl->event, ctrl->event_port))
 		goto fail;
 
 	return 0;
 
 fail:
 	if (port >= 0)
-		xc_evtchn_unbind(ctrl->event, port);
+		xenevtchn_unbind(ctrl->event, port);
 
-	xc_evtchn_close(ctrl->event);
+	xenevtchn_close(ctrl->event);
 	ctrl->event = NULL;
 
 	return -1;
diff --git a/tools/libvchan/io.c b/tools/libvchan/io.c
index 381cc05..53393a5 100644
--- a/tools/libvchan/io.c
+++ b/tools/libvchan/io.c
@@ -105,7 +105,7 @@ static inline int send_notify(struct libxenvchan *ctrl, uint8_t bit)
 	notify = ctrl->is_server ? &ctrl->ring->srv_notify : &ctrl->ring->cli_notify;
 	prev = __sync_fetch_and_and(notify, ~bit);
 	if (prev & bit)
-		return xc_evtchn_notify(ctrl->event, ctrl->event_port);
+		return xenevtchn_notify(ctrl->event, ctrl->event_port);
 	else
 		return 0;
 }
@@ -196,10 +196,10 @@ int libxenvchan_buffer_space(struct libxenvchan *ctrl)
 
 int libxenvchan_wait(struct libxenvchan *ctrl)
 {
-	int ret = xc_evtchn_pending(ctrl->event);
+	int ret = xenevtchn_pending(ctrl->event);
 	if (ret < 0)
 		return -1;
-	xc_evtchn_unmask(ctrl->event, ret);
+	xenevtchn_unmask(ctrl->event, ret);
 	return 0;
 }
 
@@ -352,7 +352,7 @@ int libxenvchan_is_open(struct libxenvchan* ctrl)
 
 int libxenvchan_fd_for_select(struct libxenvchan *ctrl)
 {
-	return xc_evtchn_fd(ctrl->event);
+	return xenevtchn_fd(ctrl->event);
 }
 
 void libxenvchan_close(struct libxenvchan *ctrl)
@@ -374,8 +374,8 @@ void libxenvchan_close(struct libxenvchan *ctrl)
 	}
 	if (ctrl->event) {
 		if (ctrl->ring)
-			xc_evtchn_notify(ctrl->event, ctrl->event_port);
-		xc_evtchn_close(ctrl->event);
+			xenevtchn_notify(ctrl->event, ctrl->event_port);
+		xenevtchn_close(ctrl->event);
 	}
 	if (ctrl->is_server) {
 		if (ctrl->gntshr)
diff --git a/tools/libvchan/libxenvchan.h b/tools/libvchan/libxenvchan.h
index 0944a0e..1544378 100644
--- a/tools/libvchan/libxenvchan.h
+++ b/tools/libvchan/libxenvchan.h
@@ -44,6 +44,7 @@
 
 #include <xen/io/libxenvchan.h>
 #include <xen/sys/evtchn.h>
+#include <xenevtchn.h>
 #include <xenctrl.h>
 
 struct libxenvchan_ring {
@@ -71,7 +72,7 @@ struct libxenvchan {
 	/* Pointer to shared ring page */
 	struct vchan_interface *ring;
 	/* event channel interface */
-	xc_evtchn *event;
+	xenevtchn_handle *event;
 	uint32_t event_port;
 	/* informative flags: are we acting as server? */
 	int is_server:1;
diff --git a/tools/libxc/Makefile b/tools/libxc/Makefile
index 940708f..b8fc6a5 100644
--- a/tools/libxc/Makefile
+++ b/tools/libxc/Makefile
@@ -48,6 +48,7 @@ CTRL_SRCS-$(CONFIG_SunOS) += xc_solaris.c
 CTRL_SRCS-$(CONFIG_NetBSD) += xc_netbsd.c
 CTRL_SRCS-$(CONFIG_NetBSDRump) += xc_netbsd.c
 CTRL_SRCS-$(CONFIG_MiniOS) += xc_minios.c
+CTRL_SRCS-y       += xc_evtchn_compat.c
 
 GUEST_SRCS-y :=
 GUEST_SRCS-y += xg_private.c xc_suspend.c
@@ -111,6 +112,7 @@ CFLAGS-$(CONFIG_Linux) += -D_GNU_SOURCE
 
 CFLAGS	+= $(PTHREAD_CFLAGS)
 CFLAGS	+= $(CFLAGS_libxentoollog)
+CFLAGS	+= $(CFLAGS_libxenevtchn)
 
 CTRL_LIB_OBJS := $(patsubst %.c,%.o,$(CTRL_SRCS-y))
 CTRL_PIC_OBJS := $(patsubst %.c,%.opic,$(CTRL_SRCS-y))
@@ -164,7 +166,7 @@ install: build
 	$(INSTALL_DATA) libxenctrl.a $(DESTDIR)$(libdir)
 	$(SYMLINK_SHLIB) libxenctrl.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenctrl.so.$(MAJOR)
 	$(SYMLINK_SHLIB) libxenctrl.so.$(MAJOR) $(DESTDIR)$(libdir)/libxenctrl.so
-	$(INSTALL_DATA) include/xenctrl.h include/xenctrlosdep.h $(DESTDIR)$(includedir)
+	$(INSTALL_DATA) include/xenctrl.h include/xenctrl_compat.h include/xenctrlosdep.h $(DESTDIR)$(includedir)
 	$(INSTALL_SHLIB) libxenguest.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)
 	$(INSTALL_DATA) libxenguest.a $(DESTDIR)$(libdir)
 	$(SYMLINK_SHLIB) libxenguest.so.$(MAJOR).$(MINOR) $(DESTDIR)$(libdir)/libxenguest.so.$(MAJOR)
@@ -207,7 +209,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR)
 	$(SYMLINK_SHLIB) $< $@
 
 libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS)
-	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 # libxenguest
 
@@ -230,7 +232,7 @@ xc_dom_bzimageloader.opic: CFLAGS += $(call zlib-options,D)
 
 libxenguest.so.$(MAJOR).$(MINOR): COMPRESSION_LIBS = $(call zlib-options,l)
 libxenguest.so.$(MAJOR).$(MINOR): $(GUEST_PIC_OBJS) libxenctrl.so
-	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(GUEST_PIC_OBJS) $(COMPRESSION_LIBS) -lz $(LDLIBS_libxenctrl) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenguest.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $(GUEST_PIC_OBJS) $(COMPRESSION_LIBS) -lz $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(PTHREAD_LIBS) $(APPEND_LDFLAGS)
 
 xenctrl_osdep_ENOSYS.so: $(OSDEP_PIC_OBJS) libxenctrl.so
 	$(CC) $(LDFLAGS) $(SHLIB_LDFLAGS) -o $@ $(OSDEP_PIC_OBJS) $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
diff --git a/tools/libxc/include/xenctrl.h b/tools/libxc/include/xenctrl.h
index 1bf70f9..53f3033 100644
--- a/tools/libxc/include/xenctrl.h
+++ b/tools/libxc/include/xenctrl.h
@@ -117,7 +117,6 @@
  */
 
 typedef struct xc_interface_core xc_interface;
-typedef struct xenevtchn_handle xc_evtchn;
 typedef struct xc_interface_core xc_gnttab;
 typedef struct xc_interface_core xc_gntshr;
 
@@ -1085,7 +1084,6 @@ int xc_cpupool_movedomain(xc_interface *xch,
  */
 xc_cpumap_t xc_cpupool_freeinfo(xc_interface *xch);
 
-
 /*
  * EVENT CHANNEL FUNCTIONS
  *
@@ -1120,101 +1118,7 @@ int xc_evtchn_reset(xc_interface *xch,
 typedef struct evtchn_status xc_evtchn_status_t;
 int xc_evtchn_status(xc_interface *xch, xc_evtchn_status_t *status);
 
-/*
- * Return a handle to the event channel driver, or NULL on failure, in
- * which case errno will be set appropriately.
- *
- * Note:
- * After fork a child process must not use any opened xc evtchn
- * handle inherited from their parent. They must open a new handle if
- * they want to interact with xc.
- *
- * Before Xen pre-4.1 this function would sometimes report errors with perror.
- */
-xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
-                             unsigned open_flags);
 
-/*
- * Close a handle previously allocated with xc_evtchn_open().
- */
-int xc_evtchn_close(xc_evtchn *xce);
-
-/*
- * Return an fd that can be select()ed on.
- *
- * Note that due to bugs, setting this fd to non blocking may not
- * work: you would hope that it would result in xc_evtchn_pending
- * failing with EWOULDBLOCK if there are no events signaled, but in
- * fact it may block.  (Bug is present in at least Linux 3.12, and
- * perhaps on other platforms or later version.)
- *
- * To be safe, you must use poll() or select() before each call to
- * xc_evtchn_pending.  If you have multiple threads (or processes)
- * sharing a single xce handle this will not work, and there is no
- * straightforward workaround.  Please design your program some other
- * way.
- */
-int xc_evtchn_fd(xc_evtchn *xce);
-
-/*
- * Notify the given event channel. Returns -1 on failure, in which case
- * errno will be set appropriately.
- */
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port);
-
-/*
- * Returns a new event port awaiting interdomain connection from the given
- * domain ID, or -1 on failure, in which case errno will be set appropriately.
- */
-evtchn_port_or_error_t
-xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid);
-
-/*
- * Returns a new event port bound to the remote port for the given domain ID,
- * or -1 on failure, in which case errno will be set appropriately.
- */
-evtchn_port_or_error_t
-xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                           evtchn_port_t remote_port);
-
-/*
- * Bind an event channel to the given VIRQ. Returns the event channel bound to
- * the VIRQ, or -1 on failure, in which case errno will be set appropriately.
- */
-evtchn_port_or_error_t
-xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq);
-
-/*
- * Unbind the given event channel. Returns -1 on failure, in which case errno
- * will be set appropriately.
- */
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port);
-
-/*
- * Return the next event channel to become pending, or -1 on failure, in which
- * case errno will be set appropriately.
- *
- * At the hypervisor level the event channel will have been masked,
- * and then cleared, by the underlying machinery (evtchn kernel
- * driver, or equivalent).  So if the event channel is signaled again
- * after it is returned here, it will be queued up, and delivered
- * again after you unmask it.  (See the documentation in the Xen
- * public header event_channel.h.)
- *
- * On receiving the notification from xc_evtchn_pending, you should
- * normally: check (by other means) what work needs doing; do the
- * necessary work (if any); unmask the event channel with
- * xc_evtchn_unmask (if you want to receive any further
- * notifications).
- */
-evtchn_port_or_error_t
-xc_evtchn_pending(xc_evtchn *xce);
-
-/*
- * Unmask the given event channel. Returns -1 on failure, in which case errno
- * will be set appropriately.
- */
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port);
 
 int xc_physdev_pci_access_modify(xc_interface *xch,
                                  uint32_t domid,
@@ -2851,6 +2755,9 @@ int xc_psr_cat_get_l3_info(xc_interface *xch, uint32_t socket,
                            bool *cdp_enabled);
 #endif
 
+/* Compat shims */
+#include "xenctrl_compat.h"
+
 #endif /* XENCTRL_H */
 
 /*
diff --git a/tools/libxc/include/xenctrl_compat.h b/tools/libxc/include/xenctrl_compat.h
new file mode 100644
index 0000000..48daeb2
--- /dev/null
+++ b/tools/libxc/include/xenctrl_compat.h
@@ -0,0 +1,48 @@
+/*
+ * Compat shims for use of 3rd party consumers of libxenctrl
+ * functionality which has been split into separate libraries.
+ *
+ * New code should use the separate libraries.
+ *
+ * Each interface must be opted-into separately by defining:
+ *
+ * XC_WANT_COMPAT_EVTCHN_API
+ *  - Functions relating to /dev/xen/evtchn
+ */
+#ifndef XENCTRL_COMPAT_H
+#define XENCTRL_COMPAT_H
+
+#ifdef XC_WANT_COMPAT_EVTCHN_API
+
+typedef struct xenevtchn_handle xc_evtchn;
+
+xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
+                             unsigned open_flags);
+int xc_evtchn_close(xc_evtchn *xce);
+int xc_evtchn_fd(xc_evtchn *xce);
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port);
+evtchn_port_or_error_t
+xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid);
+evtchn_port_or_error_t
+xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                           evtchn_port_t remote_port);
+evtchn_port_or_error_t
+xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq);
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port);
+evtchn_port_or_error_t
+xc_evtchn_pending(xc_evtchn *xce);
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port);
+
+#endif /* XC_WANT_COMPAT_EVTCHN_API */
+
+#endif
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/include/xenguest.h b/tools/libxc/include/xenguest.h
index 8f918b1..050a537 100644
--- a/tools/libxc/include/xenguest.h
+++ b/tools/libxc/include/xenguest.h
@@ -34,6 +34,12 @@
 #define X86_64_B_SIZE   64 
 #define X86_32_B_SIZE   32
 
+/*
+ * User not using xc_suspend_* / xc_await_suspent may not want to
+ * include the full libxenevtchn API here.
+ */
+typedef struct xenevtchn_handle xenevtchn_handle;
+
 /* callbacks provided by xc_domain_save */
 struct save_callbacks {
     /* Called after expiration of checkpoint interval,
@@ -161,18 +167,18 @@ struct xc_hvm_firmware_module {
  * Sets *lockfd to -1.
  * Has deallocated everything even on error.
  */
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce, int domid, int suspend_evtchn, int *lockfd);
+int xc_suspend_evtchn_release(xc_interface *xch, xenevtchn_handle *xce, int domid, int suspend_evtchn, int *lockfd);
 
 /**
  * This function eats the initial notification.
  * xce must not be used for anything else
  * See xc_suspend_evtchn_init_sane re lockfd.
  */
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xenevtchn_handle *xce,
                                      int domid, int port, int *lockfd);
 
 /* xce must not be used for anything else */
-int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
+int xc_await_suspend(xc_interface *xch, xenevtchn_handle *xce, int suspend_evtchn);
 
 /**
  * The port will be signaled immediately after this call
@@ -181,7 +187,7 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn);
  * and fed to xc_suspend_evtchn_release.  (On error *lockfd is
  * undefined and xc_suspend_evtchn_release is not allowed.)
  */
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xenevtchn_handle *xce,
                                 int domid, int port, int *lockfd);
 
 int xc_mark_page_online(xc_interface *xch, unsigned long start,
diff --git a/tools/libxc/xc_evtchn_compat.c b/tools/libxc/xc_evtchn_compat.c
new file mode 100644
index 0000000..5d3e4ba
--- /dev/null
+++ b/tools/libxc/xc_evtchn_compat.c
@@ -0,0 +1,75 @@
+/*
+ * Compat shims for use of 3rd party consumers of libxenctrl xc_evtchn
+ * functionality which has been split into separate libraries.
+ */
+
+#include <xenevtchn.h>
+
+#define XC_WANT_COMPAT_EVTCHN_API
+#include "xenctrl.h"
+
+xc_evtchn *xc_evtchn_open(xentoollog_logger *logger,
+                          unsigned open_flags)
+{
+    return xenevtchn_open(logger, open_flags);
+}
+
+int xc_evtchn_close(xc_evtchn *xce)
+{
+    return xenevtchn_close(xce);
+}
+
+int xc_evtchn_fd(xc_evtchn *xce)
+{
+    return xenevtchn_fd(xce);
+}
+
+int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
+{
+    return xenevtchn_notify(xce, port);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
+{
+    return xenevtchn_bind_unbound_port(xce, domid);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
+                           evtchn_port_t remote_port)
+{
+    return xenevtchn_bind_interdomain(xce, domid, remote_port);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
+{
+    return xenevtchn_bind_virq(xce, virq);
+}
+
+int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
+{
+    return xenevtchn_unbind(xce, port);
+}
+
+evtchn_port_or_error_t
+xc_evtchn_pending(xc_evtchn *xce)
+{
+    return xenevtchn_pending(xce);
+}
+
+int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
+{
+    return xenevtchn_unmask(xce, port);
+}
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
diff --git a/tools/libxc/xc_freebsd_osdep.c b/tools/libxc/xc_freebsd_osdep.c
index 4323e16..339997c 100644
--- a/tools/libxc/xc_freebsd_osdep.c
+++ b/tools/libxc/xc_freebsd_osdep.c
@@ -31,13 +31,11 @@
 #include <sys/ioctl.h>
 
 #include <xen/memory.h>
-#include <xen/sys/evtchn.h>
 
 #include "xenctrl.h"
 #include "xenctrlosdep.h"
 
 #define PRIVCMD_DEV     "/dev/xen/privcmd"
-#define EVTCHN_DEV      "/dev/xen/evtchn"
 
 #define PERROR(_m, _a...) xc_osdep_log(xch,XTL_ERROR,XC_INTERNAL_ERROR,_m \
                   " (%d = %s)", ## _a , errno, xc_strerror(xch, errno))
@@ -251,105 +249,6 @@ static struct xc_osdep_ops freebsd_privcmd_ops = {
     },
 };
 
-/*-------------------------- Evtchn device interface -------------------------*/
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = open(EVTCHN_DEV, O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-/*------------------------------ Evtchn interface ----------------------------*/
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int ret, fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-
-    bind.remote_domain = domid;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-    return ( ret == 0 ) ? bind.port : ret;
-}
-
-evtchn_port_or_error_t
-xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid, evtchn_port_t remote_port)
-{
-    int ret, fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-    return ( ret == 0 ) ? bind.port : ret;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int ret, fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-
-    bind.virq = virq;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-    return ( ret == 0 ) ? bind.port : ret;
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-
-    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-    return 0;
-}
-
 /*---------------------------- FreeBSD interface -----------------------------*/
 static struct xc_osdep_ops *
 freebsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
diff --git a/tools/libxc/xc_linux_osdep.c b/tools/libxc/xc_linux_osdep.c
index 90544fb..65299d0 100644
--- a/tools/libxc/xc_linux_osdep.c
+++ b/tools/libxc/xc_linux_osdep.c
@@ -31,7 +31,6 @@
 #include <sys/ioctl.h>
 
 #include <xen/memory.h>
-#include <xen/sys/evtchn.h>
 #include <xen/sys/gntdev.h>
 #include <xen/sys/gntalloc.h>
 
@@ -461,100 +460,6 @@ static struct xc_osdep_ops linux_privcmd_ops = {
 
 #define DEVXEN "/dev/xen/"
 
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = open(DEVXEN "evtchn", O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-
-    bind.remote_domain = domid;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-
-    bind.virq = virq;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-
-    if ( write(fd, &port, sizeof(port)) != sizeof(port) )
-        return -1;
-    return 0;
-}
-
 static xc_osdep_handle linux_gnttab_open(xc_gnttab *xcg)
 {
     int fd = open(DEVXEN "gntdev", O_RDWR);
diff --git a/tools/libxc/xc_minios.c b/tools/libxc/xc_minios.c
index 4ace1b5..db7b344 100644
--- a/tools/libxc/xc_minios.c
+++ b/tools/libxc/xc_minios.c
@@ -19,14 +19,11 @@
  */
 
 #undef NDEBUG
-#include "xen-external/bsd-sys-queue.h"
 #include <mini-os/types.h>
 #include <mini-os/os.h>
 #include <mini-os/mm.h>
 #include <mini-os/lib.h>
 #include <mini-os/gntmap.h>
-#include <mini-os/events.h>
-#include <mini-os/wait.h>
 #include <sys/mman.h>
 
 #include <xen/memory.h>
@@ -41,13 +38,9 @@
 #include "xc_private.h"
 
 void minios_interface_close_fd(int fd);
-void minios_evtchn_close_fd(int fd);
 void minios_gnttab_close_fd(int fd);
 
 extern void minios_interface_close_fd(int fd);
-extern void minios_evtchn_close_fd(int fd);
-
-extern struct wait_queue_head event_queue;
 
 static xc_osdep_handle minios_privcmd_open(xc_interface *xch)
 {
@@ -197,220 +190,6 @@ static struct xc_osdep_ops minios_privcmd_ops = {
     },
 };
 
-
-/* XXX Note: This is not threadsafe */
-static struct evtchn_port_info* port_alloc(int fd) {
-    struct evtchn_port_info *port_info;
-    port_info = malloc(sizeof(struct evtchn_port_info));
-    if (port_info == NULL)
-        return NULL;
-    port_info->pending = 0;
-    port_info->port = -1;
-    port_info->bound = 0;
-
-    LIST_INSERT_HEAD(&files[fd].evtchn.ports, port_info, list);
-    return port_info;
-}
-
-static void port_dealloc(struct evtchn_port_info *port_info) {
-    if (port_info->bound)
-        unbind_evtchn(port_info->port);
-    LIST_REMOVE(port_info, list);
-    free(port_info);
-}
-
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = alloc_fd(FTYPE_EVTCHN);
-    if ( fd == -1 )
-        return -1;
-    LIST_INIT(&files[fd].evtchn.ports);
-    xce->fd = fd;
-    printf("evtchn_open() -> %d\n", fd);
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-void minios_evtchn_close_fd(int fd)
-{
-    struct evtchn_port_info *port_info, *tmp;
-    LIST_FOREACH_SAFE(port_info, &files[fd].evtchn.ports, list, tmp)
-        port_dealloc(port_info);
-
-    files[fd].type = FTYPE_NONE;
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int ret;
-
-    ret = notify_remote_via_evtchn(port);
-
-    if (ret < 0) {
-	errno = -ret;
-	ret = -1;
-    }
-    return ret;
-}
-
-static void evtchn_handler(evtchn_port_t port, struct pt_regs *regs, void *data)
-{
-    int fd = (int)(intptr_t)data;
-    struct evtchn_port_info *port_info;
-    assert(files[fd].type == FTYPE_EVTCHN);
-    mask_evtchn(port);
-    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
-        if (port_info->port == port)
-            goto found;
-    }
-    printk("Unknown port for handle %d\n", fd);
-    return;
-
- found:
-    port_info->pending = 1;
-    files[fd].read = 1;
-    wake_up(&event_queue);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    int ret;
-    evtchn_port_t port;
-
-    assert(get_current() == main_thread);
-    port_info = port_alloc(fd);
-    if (port_info == NULL)
-	return -1;
-
-    printf("xc_evtchn_bind_unbound_port(%d)", domid);
-    ret = evtchn_alloc_unbound(domid, evtchn_handler, (void*)(intptr_t)fd, &port);
-    printf(" = %d\n", ret);
-
-    if (ret < 0) {
-	port_dealloc(port_info);
-	errno = -ret;
-	return -1;
-    }
-    port_info->bound = 1;
-    port_info->port = port;
-    unmask_evtchn(port);
-    return port;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    evtchn_port_t local_port;
-    int ret;
-
-    assert(get_current() == main_thread);
-    port_info = port_alloc(fd);
-    if (port_info == NULL)
-	return -1;
-
-    printf("xc_evtchn_bind_interdomain(%d, %"PRId32")", domid, remote_port);
-    ret = evtchn_bind_interdomain(domid, remote_port, evtchn_handler, (void*)(intptr_t)fd, &local_port);
-    printf(" = %d\n", ret);
-
-    if (ret < 0) {
-	port_dealloc(port_info);
-	errno = -ret;
-	return -1;
-    }
-    port_info->bound = 1;
-    port_info->port = local_port;
-    unmask_evtchn(local_port);
-    return local_port;
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-
-    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
-        if (port_info->port == port) {
-            port_dealloc(port_info);
-            return 0;
-        }
-    }
-    printf("Warning: couldn't find port %"PRId32" for xc handle %x\n", port, fd);
-    errno = EINVAL;
-    return -1;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    evtchn_port_t port;
-
-    assert(get_current() == main_thread);
-    port_info = port_alloc(fd);
-    if (port_info == NULL)
-	return -1;
-
-    printf("xc_evtchn_bind_virq(%d)", virq);
-    port = bind_virq(virq, evtchn_handler, (void*)(intptr_t)fd);
-
-    if (port < 0) {
-	port_dealloc(port_info);
-	errno = -port;
-	return -1;
-    }
-    port_info->bound = 1;
-    port_info->port = port;
-    unmask_evtchn(port);
-    return port;
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    struct evtchn_port_info *port_info;
-    unsigned long flags;
-    evtchn_port_t ret = -1;
-
-    local_irq_save(flags);
-    files[fd].read = 0;
-
-    LIST_FOREACH(port_info, &files[fd].evtchn.ports, list) {
-        if (port_info->port != -1 && port_info->pending) {
-            if (ret == -1) {
-                ret = port_info->port;
-                port_info->pending = 0;
-            } else {
-                files[fd].read = 1;
-                break;
-            }
-        }
-    }
-    local_irq_restore(flags);
-    return ret;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    unmask_evtchn(port);
-    return 0;
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush)
 {
diff --git a/tools/libxc/xc_netbsd.c b/tools/libxc/xc_netbsd.c
index b1b828f..fe4f0a1 100644
--- a/tools/libxc/xc_netbsd.c
+++ b/tools/libxc/xc_netbsd.c
@@ -19,7 +19,6 @@
 
 #include "xc_private.h"
 
-#include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <malloc.h>
@@ -223,114 +222,6 @@ static struct xc_osdep_ops netbsd_privcmd_ops = {
     },
 };
 
-#define EVTCHN_DEV_NAME  "/dev/xenevt"
-
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd = open(EVTCHN_DEV_NAME, O_NONBLOCK|O_RDWR);
-    if ( fd == -1 )
-        return -1;
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce, xc_osdep_handle h)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn * xce, int domid)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-    int ret;
-
-    bind.remote_domain = domid;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-    if (ret == 0)
-	return bind.port;
-    else
-	return -1;
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-    int ret;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    ret = ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-    if (ret == 0)
-	return bind.port;
-    else
-	return -1;
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-    int err;
-
-    bind.virq = virq;
-
-    err = ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-    if (err)
-	return -1;
-    else
-	return bind.port;
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    return write_exact(fd, (char *)&port, sizeof(port));
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
diff --git a/tools/libxc/xc_private.c b/tools/libxc/xc_private.c
index cb79f22..56d54b6 100644
--- a/tools/libxc/xc_private.c
+++ b/tools/libxc/xc_private.c
@@ -251,46 +251,6 @@ int do_xen_hypercall(xc_interface *xch, privcmd_hypercall_t *hypercall)
     return xch->ops->u.privcmd.hypercall(xch, xch->ops_handle, hypercall);
 }
 
-xc_evtchn *xc_evtchn_open(xentoollog_logger *logger, unsigned open_flags)
-{
-    xc_evtchn *xce = malloc(sizeof(*xce));
-    int rc;
-
-    if (!xce) return NULL;
-
-    xce->fd = -1;
-    xce->logger = logger;
-    xce->logger_tofree  = NULL;
-
-    if (!xce->logger) {
-        xce->logger = xce->logger_tofree =
-            (xentoollog_logger*)
-            xtl_createlogger_stdiostream(stderr, XTL_PROGRESS, 0);
-        if (!xce->logger) goto err;
-    }
-
-    rc = osdep_evtchn_open(xce);
-    if ( rc  < 0 ) goto err;
-
-    return xce;
-
-err:
-    osdep_evtchn_close(xce);
-    xtl_logger_destroy(xce->logger_tofree);
-    free(xce);
-    return NULL;
-}
-
-int xc_evtchn_close(xc_evtchn *xce)
-{
-    int rc;
-
-    rc = osdep_evtchn_close(xce);
-    xtl_logger_destroy(xce->logger_tofree);
-    free(xce);
-    return rc;
-}
-
 xc_gnttab *xc_gnttab_open(xentoollog_logger *logger,
                              unsigned open_flags)
 {
diff --git a/tools/libxc/xc_private.h b/tools/libxc/xc_private.h
index f4d155a..a32accb 100644
--- a/tools/libxc/xc_private.h
+++ b/tools/libxc/xc_private.h
@@ -123,13 +123,6 @@ struct xc_interface_core {
     xc_osdep_handle  ops_handle; /* opaque data for xc_osdep_ops */
 };
 
-struct xenevtchn_handle {
-    xentoollog_logger *logger, *logger_tofree;
-    int fd;
-};
-int osdep_evtchn_open(xc_evtchn *xce);
-int osdep_evtchn_close(xc_evtchn *xce);
-
 void xc_report_error(xc_interface *xch, int code, const char *fmt, ...)
     __attribute__((format(printf,3,4)));
 void xc_reportv(xc_interface *xch, xentoollog_logger *lg, xentoollog_level,
diff --git a/tools/libxc/xc_solaris.c b/tools/libxc/xc_solaris.c
index 088228a..ed7987c 100644
--- a/tools/libxc/xc_solaris.c
+++ b/tools/libxc/xc_solaris.c
@@ -20,7 +20,6 @@
 #include "xc_private.h"
 
 #include <xen/memory.h>
-#include <xen/sys/evtchn.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <malloc.h>
@@ -194,102 +193,6 @@ static struct xc_osdep_ops solaris_privcmd_ops = {
     },
 };
 
-int osdep_evtchn_open(xc_evtchn *xce)
-{
-    int fd;
-
-    if ( (fd = open("/dev/xen/evtchn", O_RDWR)) == -1 )
-    {
-        PERROR("Could not open event channel interface");
-        return -1;
-    }
-
-    xce->fd = fd;
-    return 0;
-}
-
-int osdep_evtchn_close(xc_evtchn *xce)
-{
-    if ( xce->fd == -1 )
-        return 0;
-
-    return close(xce->fd);
-}
-
-int xc_evtchn_fd(xc_evtchn *xce)
-{
-    return xce->fd;
-}
-
-int xc_evtchn_notify(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_notify notify;
-
-    notify.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_NOTIFY, &notify);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_unbound_port(xc_evtchn *xce, int domid)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_unbound_port bind;
-
-    bind.remote_domain = domid;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_UNBOUND_PORT, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_interdomain(xc_evtchn *xce, int domid,
-                                                  evtchn_port_t remote_port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_interdomain bind;
-
-    bind.remote_domain = domid;
-    bind.remote_port = remote_port;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_INTERDOMAIN, &bind);
-}
-
-evtchn_port_or_error_t xc_evtchn_bind_virq(xc_evtchn *xce, unsigned int virq)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_bind_virq bind;
-
-    bind.virq = virq;
-
-    return ioctl(fd, IOCTL_EVTCHN_BIND_VIRQ, &bind);
-}
-
-int xc_evtchn_unbind(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    struct ioctl_evtchn_unbind unbind;
-
-    unbind.port = port;
-
-    return ioctl(fd, IOCTL_EVTCHN_UNBIND, &unbind);
-}
-
-evtchn_port_or_error_t xc_evtchn_pending(xc_evtchn *xce)
-{
-    int fd = xce->fd;
-    evtchn_port_t port;
-
-    if ( read_exact(fd, (char *)&port, sizeof(port)) == -1 )
-        return -1;
-
-    return port;
-}
-
-int xc_evtchn_unmask(xc_evtchn *xce, evtchn_port_t port)
-{
-    int fd = xce->fd;
-    return write_exact(fd, (char *)&port, sizeof(port));
-}
-
 /* Optionally flush file to disk and discard page cache */
 void discard_file_cache(xc_interface *xch, int fd, int flush) 
 {
diff --git a/tools/libxc/xc_suspend.c b/tools/libxc/xc_suspend.c
index bba36e7..8361c7b 100644
--- a/tools/libxc/xc_suspend.c
+++ b/tools/libxc/xc_suspend.c
@@ -16,6 +16,8 @@
 #include <unistd.h>
 #include <fcntl.h>
 
+#include <xenevtchn.h>
+
 #include "xc_private.h"
 #include "xenguest.h"
 
@@ -124,12 +126,12 @@ static int unlock_suspend_event(xc_interface *xch, int domid, int *lockfd)
     return -1;
 }
 
-int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
+int xc_await_suspend(xc_interface *xch, xenevtchn_handle *xce, int suspend_evtchn)
 {
     int rc;
 
     do {
-        rc = xc_evtchn_pending(xce);
+        rc = xenevtchn_pending(xce);
         if (rc < 0) {
             ERROR("error polling suspend notification channel: %d", rc);
             return -1;
@@ -137,7 +139,7 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
     } while (rc != suspend_evtchn);
 
     /* harmless for one-off suspend */
-    if (xc_evtchn_unmask(xce, suspend_evtchn) < 0)
+    if (xenevtchn_unmask(xce, suspend_evtchn) < 0)
         ERROR("failed to unmask suspend notification channel: %d", rc);
 
     return 0;
@@ -145,16 +147,16 @@ int xc_await_suspend(xc_interface *xch, xc_evtchn *xce, int suspend_evtchn)
 
 /* Internal callers are allowed to call this with suspend_evtchn<0
  * but *lockfd>0. */
-int xc_suspend_evtchn_release(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_release(xc_interface *xch, xenevtchn_handle *xce,
                               int domid, int suspend_evtchn, int *lockfd)
 {
     if (suspend_evtchn >= 0)
-        xc_evtchn_unbind(xce, suspend_evtchn);
+        xenevtchn_unbind(xce, suspend_evtchn);
 
     return unlock_suspend_event(xch, domid, lockfd);
 }
 
-int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_sane(xc_interface *xch, xenevtchn_handle *xce,
                                 int domid, int port, int *lockfd)
 {
     int rc, suspend_evtchn = -1;
@@ -164,7 +166,7 @@ int xc_suspend_evtchn_init_sane(xc_interface *xch, xc_evtchn *xce,
         goto cleanup;
     }
 
-    suspend_evtchn = xc_evtchn_bind_interdomain(xce, domid, port);
+    suspend_evtchn = xenevtchn_bind_interdomain(xce, domid, port);
     if (suspend_evtchn < 0) {
         ERROR("failed to bind suspend event channel: %d", suspend_evtchn);
         goto cleanup;
@@ -184,7 +186,7 @@ cleanup:
     return -1;
 }
 
-int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xc_evtchn *xce,
+int xc_suspend_evtchn_init_exclusive(xc_interface *xch, xenevtchn_handle *xce,
                                      int domid, int port, int *lockfd)
 {
     int suspend_evtchn;
diff --git a/tools/libxl/Makefile b/tools/libxl/Makefile
index b0cb9e4..620720e 100644
--- a/tools/libxl/Makefile
+++ b/tools/libxl/Makefile
@@ -20,12 +20,13 @@ LIBUUID_LIBS += -luuid
 endif
 
 LIBXL_LIBS =
-LIBXL_LIBS = $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)
+LIBXL_LIBS = $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(PTYFUNCS_LIBS) $(LIBUUID_LIBS)
 ifeq ($(CONFIG_REMUS_NETBUF),y)
 LIBXL_LIBS += $(LIBNL3_LIBS)
 endif
 
 CFLAGS_LIBXL += $(CFLAGS_libxentoollog)
+CFLAGS_LIBXL += $(CFLAGS_libxenevtchn)
 CFLAGS_LIBXL += $(CFLAGS_libxenctrl)
 CFLAGS_LIBXL += $(CFLAGS_libxenguest)
 CFLAGS_LIBXL += $(CFLAGS_libxenstore)
@@ -155,7 +156,7 @@ $(XL_OBJS): CFLAGS += $(CFLAGS_XL)
 $(XL_OBJS): CFLAGS += -include $(XEN_ROOT)/tools/config.h # libxl_json.h needs it.
 
 SAVE_HELPER_OBJS = libxl_save_helper.o _libxl_save_msgs_helper.o
-$(SAVE_HELPER_OBJS): CFLAGS += $(CFLAGS_libxenctrl)
+$(SAVE_HELPER_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenevtchn)
 
 PKG_CONFIG = xenlight.pc xlutil.pc
 
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 901e8eb..2bde0f5 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -186,7 +186,7 @@ int libxl_ctx_free(libxl_ctx *ctx)
     if (ctx->xch) xc_interface_close(ctx->xch);
     libxl_version_info_dispose(&ctx->version_info);
     if (ctx->xsh) xs_daemon_close(ctx->xsh);
-    if (ctx->xce) xc_evtchn_close(ctx->xce);
+    if (ctx->xce) xenevtchn_close(ctx->xce);
 
     libxl__poller_put(ctx, ctx->poller_app);
     ctx->poller_app = NULL;
diff --git a/tools/libxl/libxl_dom_suspend.c b/tools/libxl/libxl_dom_suspend.c
index 3313ad1..16f603f 100644
--- a/tools/libxl/libxl_dom_suspend.c
+++ b/tools/libxl/libxl_dom_suspend.c
@@ -109,9 +109,9 @@ static void domain_suspend_callback_common(libxl__egc *egc,
     if ((hvm_s_state == 0) && (dss->guest_evtchn.port >= 0)) {
         LOG(DEBUG, "issuing %s suspend request via event channel",
             dss->hvm ? "PVHVM" : "PV");
-        ret = xc_evtchn_notify(CTX->xce, dss->guest_evtchn.port);
+        ret = xenevtchn_notify(CTX->xce, dss->guest_evtchn.port);
         if (ret < 0) {
-            LOG(ERROR, "xc_evtchn_notify failed ret=%d", ret);
+            LOG(ERROR, "xenevtchn_notify failed ret=%d", ret);
             rc = ERROR_FAIL;
             goto err;
         }
diff --git a/tools/libxl/libxl_event.c b/tools/libxl/libxl_event.c
index 139e49a..8548365 100644
--- a/tools/libxl/libxl_event.c
+++ b/tools/libxl/libxl_event.c
@@ -739,7 +739,7 @@ static void evtchn_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
         /* OK, that's that workaround done.  We can actually check for
          * work for us to do: */
 
-        port = xc_evtchn_pending(CTX->xce);
+        port = xenevtchn_pending(CTX->xce);
         if (port < 0) {
             if (errno == EAGAIN)
                 break;
@@ -765,20 +765,20 @@ static void evtchn_fd_callback(libxl__egc *egc, libxl__ev_fd *ev,
 }
 
 int libxl__ctx_evtchn_init(libxl__gc *gc) {
-    xc_evtchn *xce;
+    xenevtchn_handle *xce;
     int rc, fd;
 
     if (CTX->xce)
         return 0;
 
-    xce = xc_evtchn_open(CTX->lg, 0);
+    xce = xenevtchn_open(CTX->lg, 0);
     if (!xce) {
         LOGE(ERROR,"cannot open libxc evtchn handle");
         rc = ERROR_FAIL;
         goto out;
     }
 
-    fd = xc_evtchn_fd(xce);
+    fd = xenevtchn_fd(xce);
     assert(fd >= 0);
 
     rc = libxl_fd_set_nonblock(CTX, fd, 1);
@@ -788,7 +788,7 @@ int libxl__ctx_evtchn_init(libxl__gc *gc) {
     return 0;
 
  out:
-    xc_evtchn_close(xce);
+    xenevtchn_close(xce);
     return rc;
 }
 
@@ -810,14 +810,14 @@ int libxl__ev_evtchn_wait(libxl__gc *gc, libxl__ev_evtchn *evev)
 
     if (!libxl__ev_fd_isregistered(&CTX->evtchn_efd)) {
         rc = libxl__ev_fd_register(gc, &CTX->evtchn_efd, evtchn_fd_callback,
-                                   xc_evtchn_fd(CTX->xce), POLLIN);
+                                   xenevtchn_fd(CTX->xce), POLLIN);
         if (rc) goto out;
     }
 
     if (evev->waiting)
         return 0;
 
-    r = xc_evtchn_unmask(CTX->xce, evev->port);
+    r = xenevtchn_unmask(CTX->xce, evev->port);
     if (r) {
         LOGE(ERROR,"cannot unmask event channel %d",evev->port);
         rc = ERROR_FAIL;
diff --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 233d44a..2198d07 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -48,6 +48,7 @@
 #include <sys/file.h>
 #include <sys/ioctl.h>
 
+#include <xenevtchn.h>
 #include <xenstore.h>
 #include <xenctrl.h>
 #include <xenguest.h>
@@ -448,7 +449,7 @@ struct libxl__ctx {
     uint32_t watch_counter; /* helps disambiguate slot reuse */
     libxl__ev_fd watch_efd;
 
-    xc_evtchn *xce; /* waiting must be done only with libxl__ev_evtchn* */
+    xenevtchn_handle *xce; /* waiting must be done only with libxl__ev_evtchn* */
     LIBXL_LIST_HEAD(, libxl__ev_evtchn) evtchns_waiting;
     libxl__ev_fd evtchn_efd;
 
@@ -925,7 +926,7 @@ static inline int libxl__ev_xswatch_isregistered(const libxl__ev_xswatch *xw)
  * When the event is signaled then the callback will be made, once.
  * Then you must call libxl__ev_evtchn_wait again, if desired.
  *
- * You must NOT call xc_evtchn_unmask.  wait will do that for you.
+ * You must NOT call xenevtchn_unmask.  wait will do that for you.
  *
  * Calling libxl__ev_evtchn_cancel will arrange for libxl to disregard
  * future occurrences of event.  Both libxl__ev_evtchn_wait and
diff --git a/tools/misc/Makefile b/tools/misc/Makefile
index c4490f3..cf6a475 100644
--- a/tools/misc/Makefile
+++ b/tools/misc/Makefile
@@ -4,6 +4,7 @@ include $(XEN_ROOT)/tools/Rules.mk
 CFLAGS += -Werror
 # Include configure output (config.h)
 CFLAGS += -include $(XEN_ROOT)/tools/config.h
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += $(CFLAGS_xeninclude)
 CFLAGS += $(CFLAGS_libxenstore)
@@ -88,18 +89,18 @@ xenlockprof: xenlockprof.o
 # xen-hptool incorrectly uses libxc internals
 xen-hptool.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc
 xen-hptool: xen-hptool.o
-	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
 
 # xen-mfndump incorrectly uses libxc internals
 xen-mfndump.o: CFLAGS += -I$(XEN_ROOT)/tools/libxc
 xen-mfndump: xen-mfndump.o
-	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(APPEND_LDFLAGS)
 
 xenwatchdogd: xenwatchdogd.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
 
 xen-lowmemd: xen-lowmemd.o
-	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
 
 gtraceview: gtraceview.o
 	$(CC) $(LDFLAGS) -o $@ $< $(CURSES_LIBS) $(TINFO_LIBS) $(APPEND_LDFLAGS)
diff --git a/tools/misc/xen-hptool.c b/tools/misc/xen-hptool.c
index c7561a9..ebcc9e8 100644
--- a/tools/misc/xen-hptool.c
+++ b/tools/misc/xen-hptool.c
@@ -1,3 +1,4 @@
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xc_private.h>
 #include <xc_core.h>
@@ -98,7 +99,7 @@ static int hp_mem_query_func(int argc, char *argv[])
     return ret;
 }
 
-static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
+static int suspend_guest(xc_interface *xch, xenevtchn_handle *xce, int domid,
                          int *evtchn, int *lockfd)
 {
     int port, rc, suspend_evtchn = -1;
@@ -123,7 +124,7 @@ static int suspend_guest(xc_interface *xch, xc_evtchn *xce, int domid,
     }
     *evtchn = suspend_evtchn;
 
-    rc = xc_evtchn_notify(xce, suspend_evtchn);
+    rc = xenevtchn_notify(xce, suspend_evtchn);
     if (rc < 0)
     {
         fprintf(stderr, "Failed to notify suspend channel: errno %d\n", rc);
@@ -198,8 +199,8 @@ static int hp_mem_offline_func(int argc, char *argv[])
                 else if (status & PG_OFFLINE_OWNED)
                 {
                     int result, suspend_evtchn = -1, suspend_lockfd = -1;
-                    xc_evtchn *xce;
-                    xce = xc_evtchn_open(NULL, 0);
+                    xenevtchn_handle *xce;
+                    xce = xenevtchn_open(NULL, 0);
 
                     if (xce == NULL)
                     {
@@ -214,7 +215,7 @@ static int hp_mem_offline_func(int argc, char *argv[])
                     {
                         fprintf(stderr, "Failed to suspend guest %d for"
                                 " mfn %lx\n", domid, mfn);
-                        xc_evtchn_close(xce);
+                        xenevtchn_close(xce);
                         return -1;
                     }
 
@@ -238,7 +239,7 @@ static int hp_mem_offline_func(int argc, char *argv[])
                     xc_domain_resume(xch, domid, 1);
                     xc_suspend_evtchn_release(xch, xce, domid,
                                               suspend_evtchn, &suspend_lockfd);
-                    xc_evtchn_close(xce);
+                    xenevtchn_close(xce);
                 }
                 break;
             }
diff --git a/tools/misc/xen-lowmemd.c b/tools/misc/xen-lowmemd.c
index 82ffd75..3200404 100644
--- a/tools/misc/xen-lowmemd.c
+++ b/tools/misc/xen-lowmemd.c
@@ -4,22 +4,23 @@
  */
 
 #include <stdio.h>
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xenstore.h>
 #include <stdlib.h>
 #include <string.h>
 
 static evtchn_port_t virq_port      = -1;
-static xc_evtchn *xce_handle        = NULL;
+static xenevtchn_handle *xce_handle = NULL;
 static xc_interface *xch            = NULL;
 static struct xs_handle *xs_handle  = NULL;
 
 void cleanup(void)
 {
     if (virq_port > -1)
-        xc_evtchn_unbind(xce_handle, virq_port);
+        xenevtchn_unbind(xce_handle, virq_port);
     if (xce_handle)
-        xc_evtchn_close(xce_handle);
+        xenevtchn_close(xce_handle);
     if (xch)
         xc_interface_close(xch);
     if (xs_handle)
@@ -94,7 +95,7 @@ int main(int argc, char *argv[])
         return 1;
     }
 
-	xce_handle = xc_evtchn_open(NULL, 0);
+	xce_handle = xenevtchn_open(NULL, 0);
 	if (xce_handle == NULL)
     {
         perror("Failed to open evtchn device");
@@ -108,7 +109,7 @@ int main(int argc, char *argv[])
         return 3;
     }
 
-	if ((rc = xc_evtchn_bind_virq(xce_handle, VIRQ_ENOMEM)) == -1)
+	if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_ENOMEM)) == -1)
     {
         perror("Failed to bind to domain exception virq port");
         return 4;
@@ -120,7 +121,7 @@ int main(int argc, char *argv[])
     {
         evtchn_port_t port;
 
-        if ((port = xc_evtchn_pending(xce_handle)) == -1)
+        if ((port = xenevtchn_pending(xce_handle)) == -1)
         {
             perror("Failed to listen for pending event channel");
             return 5;
@@ -134,7 +135,7 @@ int main(int argc, char *argv[])
             return 6;
         }
 
-        if (xc_evtchn_unmask(xce_handle, port) == -1)
+        if (xenevtchn_unmask(xce_handle, port) == -1)
         {
             perror("Failed to unmask port");
             return 7;
diff --git a/tools/ocaml/libs/eventchn/Makefile b/tools/ocaml/libs/eventchn/Makefile
index 2d8d618..154efd4 100644
--- a/tools/ocaml/libs/eventchn/Makefile
+++ b/tools/ocaml/libs/eventchn/Makefile
@@ -2,13 +2,13 @@ TOPLEVEL=$(CURDIR)/../..
 XEN_ROOT=$(TOPLEVEL)/../..
 include $(TOPLEVEL)/common.make
 
-CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_xeninclude)
+CFLAGS += $(CFLAGS_libxenevtchn) $(CFLAGS_xeninclude)
 
 OBJS = xeneventchn
 INTF = $(foreach obj, $(OBJS),$(obj).cmi)
 LIBS = xeneventchn.cma xeneventchn.cmxa
 
-LIBS_xeneventchn = $(LDLIBS_libxenctrl)
+LIBS_xeneventchn = $(LDLIBS_libxenevtchn)
 
 all: $(INTF) $(LIBS) $(PROGRAMS)
 
diff --git a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
index 5939e7c..c2d4737 100644
--- a/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
+++ b/tools/ocaml/libs/eventchn/xeneventchn_stubs.c
@@ -24,7 +24,7 @@
 #include <xen/sysctl.h>
 #include <xen/xen.h>
 #include <xen/sys/evtchn.h>
-#include <xenctrl.h>
+#include <xenevtchn.h>
 
 #define CAML_NAME_SPACE
 #include <caml/mlvalues.h>
@@ -34,14 +34,14 @@
 #include <caml/callback.h>
 #include <caml/fail.h>
 
-#define _H(__h) ((xc_evtchn *)(__h))
+#define _H(__h) ((xenevtchn_handle *)(__h))
 
 CAMLprim value stub_eventchn_init(void)
 {
 	CAMLparam0();
 	CAMLlocal1(result);
 
-	xc_evtchn *xce = xc_evtchn_open(NULL, XC_OPENFLAG_NON_REENTRANT);
+	xenevtchn_handle *xce = xenevtchn_open(NULL, 0);
 	if (xce == NULL)
 		caml_failwith("open failed");
 
@@ -55,7 +55,7 @@ CAMLprim value stub_eventchn_fd(value xce)
 	CAMLlocal1(result);
 	int fd;
 
-	fd = xc_evtchn_fd(_H(xce));
+	fd = xenevtchn_fd(_H(xce));
 	if (fd == -1)
 		caml_failwith("evtchn fd failed");
 
@@ -69,7 +69,7 @@ CAMLprim value stub_eventchn_notify(value xce, value port)
 	CAMLparam2(xce, port);
 	int rc;
 
-	rc = xc_evtchn_notify(_H(xce), Int_val(port));
+	rc = xenevtchn_notify(_H(xce), Int_val(port));
 	if (rc == -1)
 		caml_failwith("evtchn notify failed");
 
@@ -83,7 +83,7 @@ CAMLprim value stub_eventchn_bind_interdomain(value xce, value domid,
 	CAMLlocal1(port);
 	evtchn_port_or_error_t rc;
 
-	rc = xc_evtchn_bind_interdomain(_H(xce), Int_val(domid), Int_val(remote_port));
+	rc = xenevtchn_bind_interdomain(_H(xce), Int_val(domid), Int_val(remote_port));
 	if (rc == -1)
 		caml_failwith("evtchn bind_interdomain failed");
 	port = Val_int(rc);
@@ -97,7 +97,7 @@ CAMLprim value stub_eventchn_bind_dom_exc_virq(value xce)
 	CAMLlocal1(port);
 	evtchn_port_or_error_t rc;
 
-	rc = xc_evtchn_bind_virq(_H(xce), VIRQ_DOM_EXC);
+	rc = xenevtchn_bind_virq(_H(xce), VIRQ_DOM_EXC);
 	if (rc == -1)
 		caml_failwith("evtchn bind_dom_exc_virq failed");
 	port = Val_int(rc);
@@ -110,7 +110,7 @@ CAMLprim value stub_eventchn_unbind(value xce, value port)
 	CAMLparam2(xce, port);
 	int rc;
 
-	rc = xc_evtchn_unbind(_H(xce), Int_val(port));
+	rc = xenevtchn_unbind(_H(xce), Int_val(port));
 	if (rc == -1)
 		caml_failwith("evtchn unbind failed");
 
@@ -123,7 +123,7 @@ CAMLprim value stub_eventchn_pending(value xce)
 	CAMLlocal1(result);
 	evtchn_port_or_error_t port;
 
-	port = xc_evtchn_pending(_H(xce));
+	port = xenevtchn_pending(_H(xce));
 	if (port == -1)
 		caml_failwith("evtchn pending failed");
 	result = Val_int(port);
@@ -137,7 +137,7 @@ CAMLprim value stub_eventchn_unmask(value xce, value _port)
 	evtchn_port_t port;
 
 	port = Int_val(_port);
-	if (xc_evtchn_unmask(_H(xce), port))
+	if (xenevtchn_unmask(_H(xce), port))
 		caml_failwith("evtchn unmask failed");
 	CAMLreturn(Val_unit);
 }
diff --git a/tools/python/setup.py b/tools/python/setup.py
index 604b314..8faf1c0 100644
--- a/tools/python/setup.py
+++ b/tools/python/setup.py
@@ -8,13 +8,18 @@ extra_compile_args  = [ "-fno-strict-aliasing", "-Werror" ]
 
 PATH_XEN      = XEN_ROOT + "/tools/include"
 PATH_LIBXENTOOLLOG = XEN_ROOT + "/tools/libs/toollog"
+PATH_LIBXENEVTCHN = XEN_ROOT + "/tools/libs/evtchn"
 PATH_LIBXC    = XEN_ROOT + "/tools/libxc"
 PATH_LIBXL    = XEN_ROOT + "/tools/libxl"
 PATH_XENSTORE = XEN_ROOT + "/tools/xenstore"
 
 xc = Extension("xc",
                extra_compile_args = extra_compile_args,
-               include_dirs       = [ PATH_XEN, PATH_LIBXENTOOLLOG + "/include", PATH_LIBXC + "/include", "xen/lowlevel/xc" ],
+               include_dirs       = [ PATH_XEN,
+                                      PATH_LIBXENTOOLLOG + "/include",
+                                      PATH_LIBXENEVTCHN + "/include",
+                                      PATH_LIBXC + "/include",
+                                      "xen/lowlevel/xc" ],
                library_dirs       = [ PATH_LIBXC ],
                libraries          = [ "xenctrl", "xenguest" ],
                depends            = [ PATH_LIBXC + "/libxenctrl.so", PATH_LIBXC + "/libxenguest.so" ],
diff --git a/tools/tests/xen-access/Makefile b/tools/tests/xen-access/Makefile
index dd70f9f..25ff469 100644
--- a/tools/tests/xen-access/Makefile
+++ b/tools/tests/xen-access/Makefile
@@ -5,6 +5,7 @@ CFLAGS += -Werror
 
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += $(CFLAGS_libxenguest)
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_xeninclude)
 
 TARGETS-y := xen-access
@@ -24,6 +25,6 @@ clean:
 distclean: clean
 
 xen-access: xen-access.o Makefile
-	$(CC) -o $@ $< $(LDFLAGS) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest)
+	$(CC) -o $@ $< $(LDFLAGS) $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenevtchn)
 
 -include $(DEPS)
diff --git a/tools/tests/xen-access/xen-access.c b/tools/tests/xen-access/xen-access.c
index a52ca6e..7993947 100644
--- a/tools/tests/xen-access/xen-access.c
+++ b/tools/tests/xen-access/xen-access.c
@@ -39,6 +39,7 @@
 #include <sys/poll.h>
 
 #include <xenctrl.h>
+#include <xenevtchn.h>
 #include <xen/vm_event.h>
 
 #if defined(__arm__) || defined(__aarch64__)
@@ -54,7 +55,7 @@
 
 typedef struct vm_event {
     domid_t domain_id;
-    xc_evtchn *xce_handle;
+    xenevtchn_handle *xce_handle;
     int port;
     vm_event_back_ring_t back_ring;
     uint32_t evtchn_port;
@@ -77,9 +78,9 @@ static void close_handler(int sig)
     interrupted = sig;
 }
 
-int xc_wait_for_event_or_timeout(xc_interface *xch, xc_evtchn *xce, unsigned long ms)
+int xc_wait_for_event_or_timeout(xc_interface *xch, xenevtchn_handle *xce, unsigned long ms)
 {
-    struct pollfd fd = { .fd = xc_evtchn_fd(xce), .events = POLLIN | POLLERR };
+    struct pollfd fd = { .fd = xenevtchn_fd(xce), .events = POLLIN | POLLERR };
     int port;
     int rc;
 
@@ -95,14 +96,14 @@ int xc_wait_for_event_or_timeout(xc_interface *xch, xc_evtchn *xce, unsigned lon
 
     if ( rc == 1 )
     {
-        port = xc_evtchn_pending(xce);
+        port = xenevtchn_pending(xce);
         if ( port == -1 )
         {
             ERROR("Failed to read port from event channel");
             goto err;
         }
 
-        rc = xc_evtchn_unmask(xce, port);
+        rc = xenevtchn_unmask(xce, port);
         if ( rc != 0 )
         {
             ERROR("Failed to unmask event channel port");
@@ -143,7 +144,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     /* Unbind VIRQ */
     if ( evtchn_bind )
     {
-        rc = xc_evtchn_unbind(xenaccess->vm_event.xce_handle,
+        rc = xenevtchn_unbind(xenaccess->vm_event.xce_handle,
                               xenaccess->vm_event.port);
         if ( rc != 0 )
         {
@@ -155,7 +156,7 @@ int xenaccess_teardown(xc_interface *xch, xenaccess_t *xenaccess)
     /* Close event channel */
     if ( evtchn_open )
     {
-        rc = xc_evtchn_close(xenaccess->vm_event.xce_handle);
+        rc = xenevtchn_close(xenaccess->vm_event.xce_handle);
         if ( rc != 0 )
         {
             ERROR("Error closing event channel");
@@ -223,7 +224,7 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     mem_access_enable = 1;
 
     /* Open event channel */
-    xenaccess->vm_event.xce_handle = xc_evtchn_open(NULL, 0);
+    xenaccess->vm_event.xce_handle = xenevtchn_open(NULL, 0);
     if ( xenaccess->vm_event.xce_handle == NULL )
     {
         ERROR("Failed to open event channel");
@@ -232,7 +233,7 @@ xenaccess_t *xenaccess_init(xc_interface **xch_r, domid_t domain_id)
     evtchn_open = 1;
 
     /* Bind event notification */
-    rc = xc_evtchn_bind_interdomain(xenaccess->vm_event.xce_handle,
+    rc = xenevtchn_bind_interdomain(xenaccess->vm_event.xce_handle,
                                     xenaccess->vm_event.domain_id,
                                     xenaccess->vm_event.evtchn_port);
     if ( rc < 0 )
@@ -678,7 +679,7 @@ int main(int argc, char *argv[])
         }
 
         /* Tell Xen page is ready */
-        rc = xc_evtchn_notify(xenaccess->vm_event.xce_handle,
+        rc = xenevtchn_notify(xenaccess->vm_event.xce_handle,
                               xenaccess->vm_event.port);
 
         if ( rc != 0 )
diff --git a/tools/xcutils/Makefile b/tools/xcutils/Makefile
index fff519d..2d1f112 100644
--- a/tools/xcutils/Makefile
+++ b/tools/xcutils/Makefile
@@ -16,8 +16,8 @@ PROGRAMS = readnotes lsevtchn
 CFLAGS += -Werror
 
 # incorrectly uses libxc internals
-CFLAGS_readnotes.o  := $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) -I$(XEN_ROOT)/tools/libxc
-CFLAGS_lsevtchn.o   := $(CFLAGS_libxenctrl)
+CFLAGS_readnotes.o  := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) -I$(XEN_ROOT)/tools/libxc
+CFLAGS_lsevtchn.o   := $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl)
 
 .PHONY: all
 all: build
diff --git a/tools/xenmon/Makefile b/tools/xenmon/Makefile
index 20ea100..98056f2 100644
--- a/tools/xenmon/Makefile
+++ b/tools/xenmon/Makefile
@@ -14,8 +14,10 @@ XEN_ROOT=$(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS  += -Werror
+CFLAGS  += $(CFLAGS_libxenevtchn)
 CFLAGS  += $(CFLAGS_libxenctrl)
 LDLIBS  += $(LDLIBS_libxenctrl)
+LDLIBS  += $(LDLIBS_libxenevtchn)
 
 SCRIPTS = xenmon.py
 
diff --git a/tools/xenmon/xenbaked.c b/tools/xenmon/xenbaked.c
index 1ddb29b..e4602ef 100644
--- a/tools/xenmon/xenbaked.c
+++ b/tools/xenmon/xenbaked.c
@@ -37,6 +37,7 @@
 #include <unistd.h>
 #include <errno.h>
 #include <signal.h>
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xen/xen.h>
 #include <string.h>
@@ -267,7 +268,7 @@ static void log_event(int event_id)
 }
 
 int virq_port;
-xc_evtchn *xce_handle = NULL;
+xenevtchn_handle *xce_handle = NULL;
 
 /* Returns the event channel handle. */
 /* Stolen from xenstore code */
@@ -279,12 +280,12 @@ static int eventchn_init(void)
     if (0)
         return -1;
   
-    xce_handle = xc_evtchn_open(NULL, 0);
+    xce_handle = xenevtchn_open(NULL, 0);
 
     if (xce_handle == NULL)
         perror("Failed to open evtchn device");
   
-    if ((rc = xc_evtchn_bind_virq(xce_handle, VIRQ_TBUF)) == -1)
+    if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_TBUF)) == -1)
         perror("Failed to bind to domain exception virq port");
     virq_port = rc;
   
@@ -304,7 +305,7 @@ static void wait_for_event(void)
         return;
     }
 
-    evtchn_fd = xc_evtchn_fd(xce_handle);
+    evtchn_fd = xenevtchn_fd(xce_handle);
 
     FD_ZERO(&inset);
     FD_SET(evtchn_fd, &inset);
@@ -314,13 +315,13 @@ static void wait_for_event(void)
     ret = select(evtchn_fd+1, &inset, NULL, NULL, &tv);
   
     if ( (ret == 1) && FD_ISSET(evtchn_fd, &inset)) {
-        if ((port = xc_evtchn_pending(xce_handle)) == -1)
+        if ((port = xenevtchn_pending(xce_handle)) == -1)
             perror("Failed to read from event fd");
     
         //    if (port == virq_port)
         //      printf("got the event I was looking for\r\n");
 
-        if (xc_evtchn_unmask(xce_handle, port) == -1)
+        if (xenevtchn_unmask(xce_handle, port) == -1)
             perror("Failed to write to event fd");
     }
 }
diff --git a/tools/xenpaging/Makefile b/tools/xenpaging/Makefile
index e63d894..d491867 100644
--- a/tools/xenpaging/Makefile
+++ b/tools/xenpaging/Makefile
@@ -2,8 +2,8 @@ XEN_ROOT=$(CURDIR)/../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 # xenpaging.c and file_ops.c incorrectly use libxc internals
-CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) -I$(XEN_ROOT)/tools/libxc
-LDLIBS += $(LDLIBS_libxentoollog) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
+CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) -I$(XEN_ROOT)/tools/libxc
+LDLIBS += $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
 LDFLAGS += $(PTHREAD_LDFLAGS)
 
 POLICY    = default
diff --git a/tools/xenpaging/xenpaging.c b/tools/xenpaging/xenpaging.c
index b5ffee6..df99c6a 100644
--- a/tools/xenpaging/xenpaging.c
+++ b/tools/xenpaging/xenpaging.c
@@ -73,7 +73,7 @@ static void xenpaging_mem_paging_flush_ioemu_cache(struct xenpaging *paging)
 static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
 {
     xc_interface *xch = paging->xc_handle;
-    xc_evtchn *xce = paging->vm_event.xce_handle;
+    xenevtchn_handle *xce = paging->vm_event.xce_handle;
     char **vec, *val;
     unsigned int num;
     struct pollfd fd[2];
@@ -82,7 +82,7 @@ static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
     int timeout;
 
     /* Wait for event channel and xenstore */
-    fd[0].fd = xc_evtchn_fd(xce);
+    fd[0].fd = xenevtchn_fd(xce);
     fd[0].events = POLLIN | POLLERR;
     fd[1].fd = xs_fileno(paging->xs_handle);
     fd[1].events = POLLIN | POLLERR;
@@ -146,7 +146,7 @@ static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
     if ( rc && fd[0].revents & POLLIN )
     {
         DPRINTF("Got event from evtchn\n");
-        port = xc_evtchn_pending(xce);
+        port = xenevtchn_pending(xce);
         if ( port == -1 )
         {
             PERROR("Failed to read port from event channel");
@@ -154,7 +154,7 @@ static int xenpaging_wait_for_event_or_timeout(struct xenpaging *paging)
             goto err;
         }
 
-        rc = xc_evtchn_unmask(xce, port);
+        rc = xenevtchn_unmask(xce, port);
         if ( rc < 0 )
         {
             PERROR("Failed to unmask event channel port");
@@ -393,7 +393,7 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[])
     }
 
     /* Open event channel */
-    paging->vm_event.xce_handle = xc_evtchn_open(NULL, 0);
+    paging->vm_event.xce_handle = xenevtchn_open(NULL, 0);
     if ( paging->vm_event.xce_handle == NULL )
     {
         PERROR("Failed to open event channel");
@@ -401,7 +401,7 @@ static struct xenpaging *xenpaging_init(int argc, char *argv[])
     }
 
     /* Bind event notification */
-    rc = xc_evtchn_bind_interdomain(paging->vm_event.xce_handle,
+    rc = xenevtchn_bind_interdomain(paging->vm_event.xce_handle,
                                     paging->vm_event.domain_id,
                                     paging->vm_event.evtchn_port);
     if ( rc < 0 )
@@ -531,7 +531,7 @@ static void xenpaging_teardown(struct xenpaging *paging)
     }
 
     /* Unbind VIRQ */
-    rc = xc_evtchn_unbind(paging->vm_event.xce_handle, paging->vm_event.port);
+    rc = xenevtchn_unbind(paging->vm_event.xce_handle, paging->vm_event.port);
     if ( rc != 0 )
     {
         PERROR("Error unbinding event port");
@@ -539,7 +539,7 @@ static void xenpaging_teardown(struct xenpaging *paging)
     paging->vm_event.port = -1;
 
     /* Close event channel */
-    rc = xc_evtchn_close(paging->vm_event.xce_handle);
+    rc = xenevtchn_close(paging->vm_event.xce_handle);
     if ( rc != 0 )
     {
         PERROR("Error closing event channel");
@@ -692,7 +692,7 @@ static int xenpaging_resume_page(struct xenpaging *paging, vm_event_response_t *
     }
 
     /* Tell Xen page is ready */
-    return xc_evtchn_notify(paging->vm_event.xce_handle, paging->vm_event.port);
+    return xenevtchn_notify(paging->vm_event.xce_handle, paging->vm_event.port);
 }
 
 static int xenpaging_populate_page(struct xenpaging *paging, unsigned long gfn, int i)
diff --git a/tools/xenpaging/xenpaging.h b/tools/xenpaging/xenpaging.h
index c6ab77c..d0f8d20 100644
--- a/tools/xenpaging/xenpaging.h
+++ b/tools/xenpaging/xenpaging.h
@@ -24,6 +24,7 @@
 #define __XEN_PAGING2_H__
 
 
+#include <xenevtchn.h>
 #include <xc_private.h>
 #include <xen/event_channel.h>
 #include <xen/vm_event.h>
@@ -32,7 +33,7 @@
 
 struct vm_event {
     domid_t domain_id;
-    xc_evtchn *xce_handle;
+    xenevtchn_handle *xce_handle;
     int port;
     vm_event_back_ring_t back_ring;
     uint32_t evtchn_port;
diff --git a/tools/xenstore/Makefile b/tools/xenstore/Makefile
index 404d4cb..5970fde 100644
--- a/tools/xenstore/Makefile
+++ b/tools/xenstore/Makefile
@@ -9,6 +9,7 @@ CFLAGS += -I.
 # Include configure output (config.h)
 CFLAGS += -include $(XEN_ROOT)/tools/config.h
 CFLAGS += -I./include
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
 CFLAGS += -DXEN_LIB_STORED="\"$(XEN_LIB_STORED)\""
 
@@ -69,7 +70,7 @@ CFLAGS += -DHAVE_DTRACE=1
 endif
 
 xenstored: $(XENSTORED_OBJS)
-	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
+	$(CC) $^ $(LDFLAGS) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(SOCKET_LIBS) -o $@ $(APPEND_LDFLAGS)
 
 xenstored.a: $(XENSTORED_OBJS)
 	$(AR) cr $@ $^
diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index 3c0307e..f62c192 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -40,6 +40,8 @@
 #include <assert.h>
 #include <setjmp.h>
 
+#include <xenevtchn.h>
+
 #include "utils.h"
 #include "list.h"
 #include "talloc.h"
@@ -63,7 +65,7 @@
 #include <systemd/sd-daemon.h>
 #endif
 
-extern xc_evtchn *xce_handle; /* in xenstored_domain.c */
+extern xenevtchn_handle *xce_handle; /* in xenstored_domain.c */
 static int xce_pollfd_idx = -1;
 static struct pollfd *fds;
 static unsigned int current_array_size;
@@ -372,7 +374,7 @@ static void initialize_fds(int sock, int *p_sock_pollfd_idx,
 			set_fd(reopen_log_pipe[0], POLLIN|POLLPRI);
 
 	if (xce_handle != NULL)
-		xce_pollfd_idx = set_fd(xc_evtchn_fd(xce_handle),
+		xce_pollfd_idx = set_fd(xenevtchn_fd(xce_handle),
 					POLLIN|POLLPRI);
 
 	list_for_each_entry(conn, &connections, list) {
diff --git a/tools/xenstore/xenstored_domain.c b/tools/xenstore/xenstored_domain.c
index dcd6581..6ceec29 100644
--- a/tools/xenstore/xenstored_domain.c
+++ b/tools/xenstore/xenstored_domain.c
@@ -29,6 +29,7 @@
 #include "xenstored_transaction.h"
 #include "xenstored_watch.h"
 
+#include <xenevtchn.h>
 #include <xenctrl.h>
 #include <xen/grant_table.h>
 
@@ -36,7 +37,7 @@ static xc_interface **xc_handle;
 xc_gnttab **xcg_handle;
 static evtchn_port_t virq_port;
 
-xc_evtchn *xce_handle = NULL;
+xenevtchn_handle *xce_handle = NULL;
 
 struct domain
 {
@@ -128,7 +129,7 @@ static int writechn(struct connection *conn,
 	xen_mb();
 	intf->rsp_prod += len;
 
-	xc_evtchn_notify(xce_handle, conn->domain->port);
+	xenevtchn_notify(xce_handle, conn->domain->port);
 
 	return len;
 }
@@ -158,7 +159,7 @@ static int readchn(struct connection *conn, void *data, unsigned int len)
 	xen_mb();
 	intf->req_cons += len;
 
-	xc_evtchn_notify(xce_handle, conn->domain->port);
+	xenevtchn_notify(xce_handle, conn->domain->port);
 
 	return len;
 }
@@ -190,7 +191,7 @@ static int destroy_domain(void *_domain)
 	list_del(&domain->list);
 
 	if (domain->port) {
-		if (xc_evtchn_unbind(xce_handle, domain->port) == -1)
+		if (xenevtchn_unbind(xce_handle, domain->port) == -1)
 			eprintf("> Unbinding port %i failed!\n", domain->port);
 	}
 
@@ -239,13 +240,13 @@ void handle_event(void)
 {
 	evtchn_port_t port;
 
-	if ((port = xc_evtchn_pending(xce_handle)) == -1)
+	if ((port = xenevtchn_pending(xce_handle)) == -1)
 		barf_perror("Failed to read from event fd");
 
 	if (port == virq_port)
 		domain_cleanup();
 
-	if (xc_evtchn_unmask(xce_handle, port) == -1)
+	if (xenevtchn_unmask(xce_handle, port) == -1)
 		barf_perror("Failed to write to event fd");
 }
 
@@ -287,7 +288,7 @@ static struct domain *new_domain(void *context, unsigned int domid,
 	talloc_set_destructor(domain, destroy_domain);
 
 	/* Tell kernel we're interested in this event. */
-	rc = xc_evtchn_bind_interdomain(xce_handle, domid, port);
+	rc = xenevtchn_bind_interdomain(xce_handle, domid, port);
 	if (rc == -1)
 	    return NULL;
 	domain->port = rc;
@@ -392,8 +393,8 @@ void do_introduce(struct connection *conn, struct buffered_data *in)
 	} else if ((domain->mfn == mfn) && (domain->conn != conn)) {
 		/* Use XS_INTRODUCE for recreating the xenbus event-channel. */
 		if (domain->port)
-			xc_evtchn_unbind(xce_handle, domain->port);
-		rc = xc_evtchn_bind_interdomain(xce_handle, domid, port);
+			xenevtchn_unbind(xce_handle, domain->port);
+		rc = xenevtchn_bind_interdomain(xce_handle, domid, port);
 		domain->port = (rc == -1) ? 0 : rc;
 		domain->remote_port = port;
 	} else {
@@ -614,7 +615,7 @@ static int dom0_init(void)
 
 	talloc_steal(dom0->conn, dom0); 
 
-	xc_evtchn_notify(xce_handle, dom0->port); 
+	xenevtchn_notify(xce_handle, dom0->port); 
 
 	return 0; 
 }
@@ -643,7 +644,7 @@ void domain_init(void)
 	else
 		talloc_set_destructor(xcg_handle, close_xcg_handle);
 
-	xce_handle = xc_evtchn_open(NULL, 0);
+	xce_handle = xenevtchn_open(NULL, 0);
 
 	if (xce_handle == NULL)
 		barf_perror("Failed to open evtchn device");
@@ -651,7 +652,7 @@ void domain_init(void)
 	if (dom0_init() != 0) 
 		barf_perror("Failed to initialize dom0 state"); 
 
-	if ((rc = xc_evtchn_bind_virq(xce_handle, VIRQ_DOM_EXC)) == -1)
+	if ((rc = xenevtchn_bind_virq(xce_handle, VIRQ_DOM_EXC)) == -1)
 		barf_perror("Failed to bind to domain exception virq port");
 	virq_port = rc;
 }
diff --git a/tools/xentrace/Makefile b/tools/xentrace/Makefile
index 6c13cd1..0157be2 100644
--- a/tools/xentrace/Makefile
+++ b/tools/xentrace/Makefile
@@ -3,8 +3,11 @@ include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS += -Werror
 
+CFLAGS += $(CFLAGS_libxenevtchn)
 CFLAGS += $(CFLAGS_libxenctrl)
-LDLIBS += $(LDLIBS_libxenctrl) $(ARGP_LDFLAGS)
+LDLIBS += $(LDLIBS_libxenevtchn)
+LDLIBS += $(LDLIBS_libxenctrl)
+LDLIBS += $(ARGP_LDFLAGS)
 
 BIN-$(CONFIG_X86) = xenalyze
 BIN      = $(BIN-y)
diff --git a/tools/xentrace/xentrace.c b/tools/xentrace/xentrace.c
index 4ee1458..c970d42 100644
--- a/tools/xentrace/xentrace.c
+++ b/tools/xentrace/xentrace.c
@@ -30,6 +30,7 @@
 #include <xen/xen.h>
 #include <xen/trace.h>
 
+#include <xenevtchn.h>
 #include <xenctrl.h>
 
 #define PERROR(_m, _a...)                                       \
@@ -74,7 +75,7 @@ settings_t opts;
 int interrupted = 0; /* gets set if we get a SIGHUP */
 
 static xc_interface *xc_handle;
-static xc_evtchn *xce_handle = NULL;
+static xenevtchn_handle *xce_handle = NULL;
 static int virq_port = -1;
 static int outfd = 1;
 
@@ -602,13 +603,13 @@ static void event_init(void)
 {
     int rc;
 
-    xce_handle = xc_evtchn_open(NULL, 0);
+    xce_handle = xenevtchn_open(NULL, 0);
     if (xce_handle == NULL) {
         perror("event channel open");
         exit(EXIT_FAILURE);
     }
 
-    rc = xc_evtchn_bind_virq(xce_handle, VIRQ_TBUF);
+    rc = xenevtchn_bind_virq(xce_handle, VIRQ_TBUF);
     if (rc == -1) {
         PERROR("failed to bind to VIRQ port");
         exit(EXIT_FAILURE);
@@ -623,7 +624,7 @@ static void event_init(void)
 static void wait_for_event_or_timeout(unsigned long milliseconds)
 {
     int rc;
-    struct pollfd fd = { .fd = xc_evtchn_fd(xce_handle),
+    struct pollfd fd = { .fd = xenevtchn_fd(xce_handle),
                          .events = POLLIN | POLLERR };
     int port;
 
@@ -636,7 +637,7 @@ static void wait_for_event_or_timeout(unsigned long milliseconds)
     }
 
     if (rc == 1) {
-        port = xc_evtchn_pending(xce_handle);
+        port = xenevtchn_pending(xce_handle);
         if (port == -1) {
             PERROR("failed to read port from evtchn");
             exit(EXIT_FAILURE);
@@ -647,7 +648,7 @@ static void wait_for_event_or_timeout(unsigned long milliseconds)
                     port, virq_port);
             exit(EXIT_FAILURE);
         }
-        rc = xc_evtchn_unmask(xce_handle, port);
+        rc = xenevtchn_unmask(xce_handle, port);
         if (rc == -1) {
             PERROR("failed to write port to evtchn");
             exit(EXIT_FAILURE);
-- 
2.1.4


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* Re: [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (6 preceding siblings ...)
  2016-01-19 15:44 ` Ian Campbell
@ 2016-01-22 14:14 ` Ian Campbell
  2016-01-22 14:14 ` Ian Campbell
  8 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-22 14:14 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:
> In <1431963008.4944.80.camel@citrix.com> I proposed stabilising some
> parts of the libxenctrl API/ABI by disaggregating into separate
> libraries.
> 
> This is v8 of that set of series against:
>     xen
>     qemu-xen
>     qemu-xen-traditional
>     mini-os

I have now applied all xen, qemu-xen-traditional and mini-os, with the one
patch updated to v9 in the xen series (posted earlier today).

I omitted the final two patches from the Xen side:
[28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain.
[29/29] HACK: Update Config.mk to pull all the right bits from my xenbits trees

#28 isn't quite ready/agreed yet. (I hope #29 is obvious...)

Stefano, you can go ahead with the qemu-xen part whenever. Maybe it would
be worth waiting for the xen push gate to succeed first though? At least
waiting for a xen-unstable-smoke flight would be wise.

Ian, you weren't around to discuss the qemu-xen-traditional push with, but
I took a gamble that you would be ok with me doing so on this occasion. I
hope that's ok.

I modified the 4 patches with updated MINIOS_UPSTREAM_REVISION and
QEMU_TRADITIONAL_REVISION where called for.

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
  2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
                   ` (7 preceding siblings ...)
  2016-01-22 14:14 ` [Qemu-devel] " Ian Campbell
@ 2016-01-22 14:14 ` Ian Campbell
  8 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-22 14:14 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

On Fri, 2016-01-15 at 13:22 +0000, Ian Campbell wrote:
> In <1431963008.4944.80.camel@citrix.com> I proposed stabilising some
> parts of the libxenctrl API/ABI by disaggregating into separate
> libraries.
> 
> This is v8 of that set of series against:
>     xen
>     qemu-xen
>     qemu-xen-traditional
>     mini-os

I have now applied all xen, qemu-xen-traditional and mini-os, with the one
patch updated to v9 in the xen series (posted earlier today).

I omitted the final two patches from the Xen side:
[28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain.
[29/29] HACK: Update Config.mk to pull all the right bits from my xenbits trees

#28 isn't quite ready/agreed yet. (I hope #29 is obvious...)

Stefano, you can go ahead with the qemu-xen part whenever. Maybe it would
be worth waiting for the xen push gate to succeed first though? At least
waiting for a xen-unstable-smoke flight would be wise.

Ian, you weren't around to discuss the qemu-xen-traditional push with, but
I took a gamble that you would be ok with me doing so on this occasion. I
hope that's ok.

I modified the 4 patches with updated MINIOS_UPSTREAM_REVISION and
QEMU_TRADITIONAL_REVISION where called for.

Ian.


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-15 13:22   ` [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn Ian Campbell
  2016-01-22 11:48     ` Ian Campbell
@ 2016-01-22 17:12     ` Boris Ostrovsky
  2016-01-25  9:48       ` Ian Campbell
  1 sibling, 1 reply; 113+ messages in thread
From: Boris Ostrovsky @ 2016-01-22 17:12 UTC (permalink / raw)
  To: Ian Campbell, ian.jackson, wei.liu2, xen-devel

On 01/15/2016 08:22 AM, Ian Campbell wrote:
> libxenevtchn will provide a stable API and ABI for accessing the
> evtchn device.

I think this patch breaks the build:

root@ovs101> gcc  -O1 -fno-omit-frame-pointer -m64 -g 
-fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes 
-Wdeclaration-after-statement -Wno-unused-but-set-variable   -O0 -g3 
-D__XEN_TOOLS__ -MMD -MF .core.o.d -D_LARGEFILE_SOURCE 
-D_LARGEFILE64_SOURCE -fno-optimize-sibling-calls  -Werror 
-Wmissing-prototypes -I./include 
-I/root/xen/tools/libs/evtchn/../../../tools/include 
-I/root/xen/tools/libs/evtchn/../../../tools/libs/toollog/include 
-I/root/xen/tools/libs/evtchn/../../../tools/include  -c -o core.o core.c
In file included from private.h:5,
                  from core.c:19:
./include/xenevtchn.h:36: error: redefinition of typedef ‘xentoollog_logger’
/root/xen/tools/libs/evtchn/../../../tools/libs/toollog/include/xentoollog.h:44: 
note: previous declaration of ‘xentoollog_logger’ was here

-boris

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-22 17:12     ` Boris Ostrovsky
@ 2016-01-25  9:48       ` Ian Campbell
  2016-01-25 14:35         ` Ian Jackson
  0 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-25  9:48 UTC (permalink / raw)
  To: Boris Ostrovsky, ian.jackson, wei.liu2, xen-devel

On Fri, 2016-01-22 at 12:12 -0500, Boris Ostrovsky wrote:
> On 01/15/2016 08:22 AM, Ian Campbell wrote:
> > libxenevtchn will provide a stable API and ABI for accessing the
> > evtchn device.
> 
> I think this patch breaks the build:
> 
> root@ovs101> gcc  -O1 -fno-omit-frame-pointer -m64 -g 
> -fno-strict-aliasing -std=gnu99 -Wall -Wstrict-prototypes 
> -Wdeclaration-after-statement -Wno-unused-but-set-variable   -O0 -g3 
> -D__XEN_TOOLS__ -MMD -MF .core.o.d -D_LARGEFILE_SOURCE 
> -D_LARGEFILE64_SOURCE -fno-optimize-sibling-calls  -Werror 
> -Wmissing-prototypes -I./include 
> -I/root/xen/tools/libs/evtchn/../../../tools/include 
> -I/root/xen/tools/libs/evtchn/../../../tools/libs/toollog/include 
> -I/root/xen/tools/libs/evtchn/../../../tools/include  -c -o core.o core.c
> In file included from private.h:5,
>                   from core.c:19:
> ./include/xenevtchn.h:36: error: redefinition of typedef ‘xentoollog_logger’
> /root/xen/tools/libs/evtchn/../../../tools/libs/toollog/include/xentoollog.h:44: 
> note: previous declaration of ‘xentoollog_logger’ was here

Hrm, it seems to be OK by the end of the series, in that it built for me
before push and has passed smoke-test. So I may have introduced a bisection
hazard (sorry) but it's too late to fix that unfortunately.

Various of the tools/libs/*/include/*.h have a

    /* Callers who don't care don't need to #include <xentoollog.h> */
    typedef struct xentoollog_logger xentoollog_logger;

but since that typedef matches in all cases I think it is allowed to be
repeated like this, isn't it?

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory
  2016-01-15 13:22   ` [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory Ian Campbell
@ 2016-01-25 12:01     ` Olaf Hering
  2016-01-25 12:31       ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Olaf Hering @ 2016-01-25 12:01 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, ian.jackson, xen-devel

On Fri, Jan 15, Ian Campbell wrote:

> libxenforeignmemory will provide a stable API and ABI for mapping
> foreign domain memory (subject to appropriate privileges).

I think this will break my Xen pkg build:

[  333s] kdd-xen.c: In function 'kdd_access_physical_page':
[  333s] kdd-xen.c:508:15: warning: implicit declaration of function 'xc_map_foreign_range' [-Wimplicit-function-declaration]
[  333s]          map = xc_map_foreign_range(g->xc_handle,
[  333s]                ^
[  333s] kdd-xen.c:508:13: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
[  333s]          map = xc_map_foreign_range(g->xc_handle,
[  333s]              ^

3272230848f36eb5bbb660216898a90048a81d9f worked,
2e46e3f2539d026594ec1618e7df2c2bc8785b42 would start to fail.

Olaf

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory
  2016-01-25 12:01     ` Olaf Hering
@ 2016-01-25 12:31       ` Ian Campbell
  2016-01-25 12:44         ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 12:31 UTC (permalink / raw)
  To: Olaf Hering; +Cc: wei.liu2, ian.jackson, xen-devel

On Mon, 2016-01-25 at 13:01 +0100, Olaf Hering wrote:
> On Fri, Jan 15, Ian Campbell wrote:
> 
> > libxenforeignmemory will provide a stable API and ABI for mapping
> > foreign domain memory (subject to appropriate privileges).
> 
> I think this will break my Xen pkg build:
> 
> [  333s] kdd-xen.c: In function 'kdd_access_physical_page':
> [  333s] kdd-xen.c:508:15: warning: implicit declaration of function
> 'xc_map_foreign_range' [-Wimplicit-function-declaration]
> [  333s]          map = xc_map_foreign_range(g->xc_handle,
> [  333s]                ^
> [  333s] kdd-xen.c:508:13: warning: assignment makes pointer from integer
> without a cast [-Wint-conversion]
> [  333s]          map = xc_map_foreign_range(g->xc_handle,
> [  333s]              ^

I supposed this wasn't built by default and I missed it, but it looks to be
built unconditionally. I wonder why I didn't see this.

Defining XC_WANT_COMPAT_MAP_FOREIGN_API while building is one way to fix,
or else kdd would need to switch to the new interface. The latter is
obviously better long term, but the former is done for a bunch of stuff in
tree so it might as well be done here too.

The machine with the trees I used for this work is currently unavailable
(network issues in our office), but I'll see if I can reproduce.

Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory
  2016-01-25 12:31       ` Ian Campbell
@ 2016-01-25 12:44         ` Ian Campbell
  2016-01-25 12:45           ` [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces Ian Campbell
  2016-01-25 13:18           ` [PATCH] kdd: build using Werror Ian Campbell
  0 siblings, 2 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 12:44 UTC (permalink / raw)
  To: Olaf Hering; +Cc: ian.jackson, wei.liu2, xen-devel

On Mon, 2016-01-25 at 12:31 +0000, Ian Campbell wrote:
> On Mon, 2016-01-25 at 13:01 +0100, Olaf Hering wrote:
> > On Fri, Jan 15, Ian Campbell wrote:
> > 
> > > libxenforeignmemory will provide a stable API and ABI for mapping
> > > foreign domain memory (subject to appropriate privileges).
> > 
> > I think this will break my Xen pkg build:
> > 
> > [  333s] kdd-xen.c: In function 'kdd_access_physical_page':
> > [  333s] kdd-xen.c:508:15: warning: implicit declaration of function
> > 'xc_map_foreign_range' [-Wimplicit-function-declaration]
> > [  333s]          map = xc_map_foreign_range(g->xc_handle,
> > [  333s]                ^
> > [  333s] kdd-xen.c:508:13: warning: assignment makes pointer from
> > integer
> > without a cast [-Wint-conversion]
> > [  333s]          map = xc_map_foreign_range(g->xc_handle,
> > [  333s]              ^
> 
> I supposed this wasn't built by default and I missed it, but it looks to be
> built unconditionally. I wonder why I didn't see this.

Turns out this code doesn't use Werror, so it was failing but was lost in
the noise (e.g. you can find it in http://logs.test-lab.xenproject.org/osst
est/logs/78820/build-amd64/5.ts-xen-build.log)

> Defining XC_WANT_COMPAT_MAP_FOREIGN_API while building is one way to fix,
> or else kdd would need to switch to the new interface. The latter is
> obviously better long term, but the former is done for a bunch of stuff in
> tree so it might as well be done here too.
> 
> The machine with the trees I used for this work is currently unavailable
> (network issues in our office), but I'll see if I can reproduce.

I did, I'll send a patch taking the easy way out as a reply.

Ian.


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces.
  2016-01-25 12:44         ` Ian Campbell
@ 2016-01-25 12:45           ` Ian Campbell
  2016-01-25 13:25             ` Tim Deegan
  2016-01-25 14:00             ` Olaf Hering
  2016-01-25 13:18           ` [PATCH] kdd: build using Werror Ian Campbell
  1 sibling, 2 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 12:45 UTC (permalink / raw)
  To: tim; +Cc: wei.liu2, Olaf Hering, ian.jackson, Ian Campbell, xen-devel

This:

kdd-xen.c: In function 'kdd_access_physical_page':
kdd-xen.c:508:9: warning: implicit declaration of function 'xc_map_foreign_range' [-Wimplicit-function-declaration]
         map = xc_map_foreign_range(g->xc_handle,
         ^
kdd-xen.c:508:13: warning: assignment makes pointer from integer without a cast
         map = xc_map_foreign_range(g->xc_handle,
             ^

was caused by the refactoring of this functionality into
libxenforeignmemory.

Reported by: Olaf Hering <olaf@aepfle.de>
Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Cc: Tim Deegan <tim@xen.org>
---
 tools/debugger/kdd/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/debugger/kdd/Makefile b/tools/debugger/kdd/Makefile
index a79d7cf..72ad1b9 100644
--- a/tools/debugger/kdd/Makefile
+++ b/tools/debugger/kdd/Makefile
@@ -2,6 +2,7 @@ XEN_ROOT = $(CURDIR)/../../..
 include $(XEN_ROOT)/tools/Rules.mk
 
 CFLAGS  += $(CFLAGS_libxenctrl)
+CFLAGS  += -DXC_WANT_COMPAT_MAP_FOREIGN_API
 LDLIBS  += $(LDLIBS_libxenctrl)
 
 CFILES  := kdd.c kdd-xen.c
-- 
2.6.1

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* [PATCH] kdd: build using Werror
  2016-01-25 12:44         ` Ian Campbell
  2016-01-25 12:45           ` [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces Ian Campbell
@ 2016-01-25 13:18           ` Ian Campbell
  2016-01-25 13:20             ` Olaf Hering
                               ` (2 more replies)
  1 sibling, 3 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 13:18 UTC (permalink / raw)
  To: tim; +Cc: wei.liu2, Olaf Hering, ian.jackson, Ian Campbell, xen-devel

We build most of tools using Werror and there seems to be know
deliberate reason for this to be an exception.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
---
 tools/debugger/kdd/Makefile | 1 +
 1 file changed, 1 insertion(+)

diff --git a/tools/debugger/kdd/Makefile b/tools/debugger/kdd/Makefile
index 72ad1b9..f3a597d 100644
--- a/tools/debugger/kdd/Makefile
+++ b/tools/debugger/kdd/Makefile
@@ -1,6 +1,7 @@
 XEN_ROOT = $(CURDIR)/../../..
 include $(XEN_ROOT)/tools/Rules.mk
 
+CFLAGS  += -Werror
 CFLAGS  += $(CFLAGS_libxenctrl)
 CFLAGS  += -DXC_WANT_COMPAT_MAP_FOREIGN_API
 LDLIBS  += $(LDLIBS_libxenctrl)
-- 
2.6.1

^ permalink raw reply related	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: build using Werror
  2016-01-25 13:18           ` [PATCH] kdd: build using Werror Ian Campbell
@ 2016-01-25 13:20             ` Olaf Hering
  2016-01-25 13:24               ` Ian Campbell
  2016-01-25 13:25             ` Tim Deegan
  2016-01-25 14:37             ` Olaf Hering
  2 siblings, 1 reply; 113+ messages in thread
From: Olaf Hering @ 2016-01-25 13:20 UTC (permalink / raw)
  To: Ian Campbell; +Cc: ian.jackson, tim, wei.liu2, xen-devel

On Mon, Jan 25, Ian Campbell wrote:

> We build most of tools using Werror and there seems to be know
> deliberate reason for this to be an exception.

s/know/no/ ?


Acked-by: Olaf Hering <olaf@aepfle.de>

Olaf

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: build using Werror
  2016-01-25 13:20             ` Olaf Hering
@ 2016-01-25 13:24               ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 13:24 UTC (permalink / raw)
  To: Olaf Hering; +Cc: ian.jackson, tim, wei.liu2, xen-devel

On Mon, 2016-01-25 at 14:20 +0100, Olaf Hering wrote:
> On Mon, Jan 25, Ian Campbell wrote:
> 
> > We build most of tools using Werror and there seems to be know
> > deliberate reason for this to be an exception.
> 
> s/know/no/ ?

Yes, thanks!

I think I was going to write "I don't know of" and then changed my mind not
quite all the way.

> Acked-by: Olaf Hering <olaf@aepfle.de>

Thanks.

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces.
  2016-01-25 12:45           ` [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces Ian Campbell
@ 2016-01-25 13:25             ` Tim Deegan
  2016-01-25 14:22               ` Ian Campbell
  2016-01-25 14:00             ` Olaf Hering
  1 sibling, 1 reply; 113+ messages in thread
From: Tim Deegan @ 2016-01-25 13:25 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, Olaf Hering, ian.jackson, xen-devel

At 12:45 +0000 on 25 Jan (1453725932), Ian Campbell wrote:
> This:
> 
> kdd-xen.c: In function 'kdd_access_physical_page':
> kdd-xen.c:508:9: warning: implicit declaration of function 'xc_map_foreign_range' [-Wimplicit-function-declaration]
>          map = xc_map_foreign_range(g->xc_handle,
>          ^
> kdd-xen.c:508:13: warning: assignment makes pointer from integer without a cast
>          map = xc_map_foreign_range(g->xc_handle,
>              ^
> 
> was caused by the refactoring of this functionality into
> libxenforeignmemory.
> 
> Reported by: Olaf Hering <olaf@aepfle.de>
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Acked-by: Tim Deegan <tim@xen.org>

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: build using Werror
  2016-01-25 13:18           ` [PATCH] kdd: build using Werror Ian Campbell
  2016-01-25 13:20             ` Olaf Hering
@ 2016-01-25 13:25             ` Tim Deegan
  2016-01-25 14:22               ` Ian Campbell
  2016-01-25 14:37             ` Olaf Hering
  2 siblings, 1 reply; 113+ messages in thread
From: Tim Deegan @ 2016-01-25 13:25 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, Olaf Hering, ian.jackson, xen-devel

At 13:18 +0000 on 25 Jan (1453727891), Ian Campbell wrote:
> We build most of tools using Werror and there seems to be know
> deliberate reason for this to be an exception.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

Acked-by: Tim Deegan <tim@xen.org>

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces.
  2016-01-25 12:45           ` [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces Ian Campbell
  2016-01-25 13:25             ` Tim Deegan
@ 2016-01-25 14:00             ` Olaf Hering
  1 sibling, 0 replies; 113+ messages in thread
From: Olaf Hering @ 2016-01-25 14:00 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, tim, ian.jackson, xen-devel

On Mon, Jan 25, Ian Campbell wrote:

> Reported by: Olaf Hering <olaf@aepfle.de>
Tested-by: Olaf Hering <olaf@aepfle.de>

Thanks,
Olaf

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: build using Werror
  2016-01-25 13:25             ` Tim Deegan
@ 2016-01-25 14:22               ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 14:22 UTC (permalink / raw)
  To: Tim Deegan; +Cc: wei.liu2, Olaf Hering, ian.jackson, xen-devel

On Mon, 2016-01-25 at 13:25 +0000, Tim Deegan wrote:
> At 13:18 +0000 on 25 Jan (1453727891), Ian Campbell wrote:
> > We build most of tools using Werror and there seems to be know
> > deliberate reason for this to be an exception.
> > 
> > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> 
> Acked-by: Tim Deegan <tim@xen.org>

applied, thanks.

(FTR, I skipped my usual full set of usual pre-push build tests, since the
machines I use are unavailable right now. I have done an x86_64 make dist).

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces.
  2016-01-25 13:25             ` Tim Deegan
@ 2016-01-25 14:22               ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 14:22 UTC (permalink / raw)
  To: Tim Deegan; +Cc: wei.liu2, Olaf Hering, ian.jackson, xen-devel

On Mon, 2016-01-25 at 13:25 +0000, Tim Deegan wrote:
> At 12:45 +0000 on 25 Jan (1453725932), Ian Campbell wrote:
> > This:
> > 
> > kdd-xen.c: In function 'kdd_access_physical_page':
> > kdd-xen.c:508:9: warning: implicit declaration of function
> > 'xc_map_foreign_range' [-Wimplicit-function-declaration]
> >          map = xc_map_foreign_range(g->xc_handle,
> >          ^
> > kdd-xen.c:508:13: warning: assignment makes pointer from integer
> > without a cast
> >          map = xc_map_foreign_range(g->xc_handle,
> >              ^
> > 
> > was caused by the refactoring of this functionality into
> > libxenforeignmemory.
> > 
> > Reported by: Olaf Hering <olaf@aepfle.de>
> > Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
> 
> Acked-by: Tim Deegan <tim@xen.org>

thanks, applied.

(FTR, I skipped my usual full set of usual pre-push build tests, since the
machines I use are unavailable right now. I have done an x86_64 make dist).

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-25  9:48       ` Ian Campbell
@ 2016-01-25 14:35         ` Ian Jackson
  2016-01-25 14:39           ` Boris Ostrovsky
  2016-01-25 14:47           ` Ian Campbell
  0 siblings, 2 replies; 113+ messages in thread
From: Ian Jackson @ 2016-01-25 14:35 UTC (permalink / raw)
  To: Ian Campbell; +Cc: Boris Ostrovsky, wei.liu2, xen-devel

Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn."):
> Various of the tools/libs/*/include/*.h have a
> 
>     /* Callers who don't care don't need to #include <xentoollog.h> */
>     typedef struct xentoollog_logger xentoollog_logger;
> 
> but since that typedef matches in all cases I think it is allowed to be
> repeated like this, isn't it?

No, I'm afraid not.  It is permitted to repeatedly mention `struct
xentoollog_logger', but the typedef must only occur once in any
translation unit.

If it is desirable to let callers avoid including xentoollog.h, then
all those headers need to say:

  struct xentoollog_logger;
  int some_function(..., struct xentoollog_logger *lg, ...);

So in your patches something like:

  struct xentoollog_logger;

  xenevtchn_handle *xenevtchn_open(struct xentoollog_logger *logger,
                                   unsigned open_flags);

The separate `struct xentoollog_logger;' is needed because otherwise
the `struct xentoollog_logger *logger' in the formal parameters of
xenevtchn_open is a declaration, rather than a reference to a
previously-declared thing, and if it is a declaration its scope is
only the contained function prototype, so other mentions of `struct
xentoollog_logger' are treated as references to a different type.
This is, of course, insane.

Ian.

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH] kdd: build using Werror
  2016-01-25 13:18           ` [PATCH] kdd: build using Werror Ian Campbell
  2016-01-25 13:20             ` Olaf Hering
  2016-01-25 13:25             ` Tim Deegan
@ 2016-01-25 14:37             ` Olaf Hering
  2 siblings, 0 replies; 113+ messages in thread
From: Olaf Hering @ 2016-01-25 14:37 UTC (permalink / raw)
  To: Ian Campbell; +Cc: wei.liu2, tim, ian.jackson, xen-devel

On Mon, Jan 25, Ian Campbell wrote:

> We build most of tools using Werror and there seems to be know
> deliberate reason for this to be an exception.
> 
> Signed-off-by: Ian Campbell <ian.campbell@citrix.com>

This would have caught the kdd error.

Tested-by: Olaf Hering <olaf@aepfle.de>

Olaf

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-25 14:35         ` Ian Jackson
@ 2016-01-25 14:39           ` Boris Ostrovsky
  2016-01-25 14:47           ` Ian Campbell
  1 sibling, 0 replies; 113+ messages in thread
From: Boris Ostrovsky @ 2016-01-25 14:39 UTC (permalink / raw)
  To: Ian Jackson, Ian Campbell; +Cc: wei.liu2, xen-devel

On 01/25/2016 09:35 AM, Ian Jackson wrote:
> Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn."):
>> Various of the tools/libs/*/include/*.h have a
>>
>>      /* Callers who don't care don't need to #include <xentoollog.h> */
>>      typedef struct xentoollog_logger xentoollog_logger;
>>
>> but since that typedef matches in all cases I think it is allowed to be
>> repeated like this, isn't it?
> No, I'm afraid not.  It is permitted to repeatedly mention `struct
> xentoollog_logger', but the typedef must only occur once in any
> translation unit.
>
> If it is desirable to let callers avoid including xentoollog.h, then
> all those headers need to say:
>
>    struct xentoollog_logger;
>    int some_function(..., struct xentoollog_logger *lg, ...);
>
> So in your patches something like:
>
>    struct xentoollog_logger;
>
>    xenevtchn_handle *xenevtchn_open(struct xentoollog_logger *logger,
>                                     unsigned open_flags);
>
> The separate `struct xentoollog_logger;' is needed because otherwise
> the `struct xentoollog_logger *logger' in the formal parameters of
> xenevtchn_open is a declaration, rather than a reference to a
> previously-declared thing, and if it is a declaration its scope is
> only the contained function prototype, so other mentions of `struct
> xentoollog_logger' are treated as references to a different type.
> This is, of course, insane.
>

(I was just about to respond to IanC's earlier message)

It builds on my desktop (gcc 4.9.2) but not on an older setup (4.4.7). 
BTW, I should have explicitly said so: I haven't bisected to this commit 
--- it just looked like this ould be the culprit.

-boris

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-25 14:35         ` Ian Jackson
  2016-01-25 14:39           ` Boris Ostrovsky
@ 2016-01-25 14:47           ` Ian Campbell
  2016-01-25 14:49             ` Andrew Cooper
  1 sibling, 1 reply; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 14:47 UTC (permalink / raw)
  To: Ian Jackson; +Cc: Boris Ostrovsky, wei.liu2, xen-devel

On Mon, 2016-01-25 at 14:35 +0000, Ian Jackson wrote:
> Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v8 02/29] tools:
> Refactor /dev/xen/evtchn wrappers into libxenevtchn."):
> > Various of the tools/libs/*/include/*.h have a
> > 
> >     /* Callers who don't care don't need to #include <xentoollog.h> */
> >     typedef struct xentoollog_logger xentoollog_logger;
> > 
> > but since that typedef matches in all cases I think it is allowed to be
> > repeated like this, isn't it?
> 
> No, I'm afraid not.

:-/

>   It is permitted to repeatedly mention `struct
> xentoollog_logger', but the typedef must only occur once in any
> translation unit.

For some reason I thought the typedef's could be repeated so long as they
were identical.

Seems at least Debian's gcc tolerates such things (which makes me wonder
how many more instances of this might have snuck in, I guess folks like
Boris would have spotted them pretty quick).

> If it is desirable to let callers avoid including xentoollog.h, then
> all those headers need to say:
> 
>   struct xentoollog_logger;
>   int some_function(..., struct xentoollog_logger *lg, ...);
> 
> So in your patches something like:
> 
>   struct xentoollog_logger;
> 
>   xenevtchn_handle *xenevtchn_open(struct xentoollog_logger *logger,
>                                    unsigned open_flags);

I'll do something like this for tools/libs/*/include.

FYI the underlying patches are all in since Friday.


> 
> The separate `struct xentoollog_logger;' is needed because otherwise
> the `struct xentoollog_logger *logger' in the formal parameters of
> xenevtchn_open is a declaration, rather than a reference to a
> previously-declared thing, and if it is a declaration its scope is
> only the contained function prototype, so other mentions of `struct
> xentoollog_logger' are treated as references to a different type.
> This is, of course, insane.
> 
> Ian.

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-25 14:47           ` Ian Campbell
@ 2016-01-25 14:49             ` Andrew Cooper
  2016-01-25 15:28               ` Ian Campbell
  0 siblings, 1 reply; 113+ messages in thread
From: Andrew Cooper @ 2016-01-25 14:49 UTC (permalink / raw)
  To: Ian Campbell, Ian Jackson; +Cc: Boris Ostrovsky, wei.liu2, xen-devel

On 25/01/16 14:47, Ian Campbell wrote:
> On Mon, 2016-01-25 at 14:35 +0000, Ian Jackson wrote:
>> Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v8 02/29] tools:
>> Refactor /dev/xen/evtchn wrappers into libxenevtchn."):
>>> Various of the tools/libs/*/include/*.h have a
>>>
>>>     /* Callers who don't care don't need to #include <xentoollog.h> */
>>>     typedef struct xentoollog_logger xentoollog_logger;
>>>
>>> but since that typedef matches in all cases I think it is allowed to be
>>> repeated like this, isn't it?
>> No, I'm afraid not.
> :-/
>
>>   It is permitted to repeatedly mention `struct
>> xentoollog_logger', but the typedef must only occur once in any
>> translation unit.
> For some reason I thought the typedef's could be repeated so long as they
> were identical.
>
> Seems at least Debian's gcc tolerates such things (which makes me wonder
> how many more instances of this might have snuck in, I guess folks like
> Boris would have spotted them pretty quick).
>
>> If it is desirable to let callers avoid including xentoollog.h, then
>> all those headers need to say:
>>
>>   struct xentoollog_logger;
>>   int some_function(..., struct xentoollog_logger *lg, ...);
>>
>> So in your patches something like:
>>
>>   struct xentoollog_logger;
>>
>>   xenevtchn_handle *xenevtchn_open(struct xentoollog_logger *logger,
>>                                    unsigned open_flags);
> I'll do something like this for tools/libs/*/include.
>
> FYI the underlying patches are all in since Friday.

How about doing the header strictness check on all headers at one?  If
repeating typedefs is a gccism, that should catch it.

~Andrew

^ permalink raw reply	[flat|nested] 113+ messages in thread

* Re: [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
  2016-01-25 14:49             ` Andrew Cooper
@ 2016-01-25 15:28               ` Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-25 15:28 UTC (permalink / raw)
  To: Andrew Cooper, Ian Jackson; +Cc: Boris Ostrovsky, wei.liu2, xen-devel

On Mon, 2016-01-25 at 14:49 +0000, Andrew Cooper wrote:
> On 25/01/16 14:47, Ian Campbell wrote:
> > On Mon, 2016-01-25 at 14:35 +0000, Ian Jackson wrote:
> > > Ian Campbell writes ("Re: [Xen-devel] [PATCH XEN v8 02/29] tools:
> > > Refactor /dev/xen/evtchn wrappers into libxenevtchn."):
> > > > Various of the tools/libs/*/include/*.h have a
> > > > 
> > > >     /* Callers who don't care don't need to #include <xentoollog.h>
> > > > */
> > > >     typedef struct xentoollog_logger xentoollog_logger;
> > > > 
> > > > but since that typedef matches in all cases I think it is allowed
> > > > to be
> > > > repeated like this, isn't it?
> > > No, I'm afraid not.
> > :-/
> > 
> > >   It is permitted to repeatedly mention `struct
> > > xentoollog_logger', but the typedef must only occur once in any
> > > translation unit.
> > For some reason I thought the typedef's could be repeated so long as
> > they
> > were identical.
> > 
> > Seems at least Debian's gcc tolerates such things (which makes me
> > wonder
> > how many more instances of this might have snuck in, I guess folks like
> > Boris would have spotted them pretty quick).
> > 
> > > If it is desirable to let callers avoid including xentoollog.h, then
> > > all those headers need to say:
> > > 
> > >   struct xentoollog_logger;
> > >   int some_function(..., struct xentoollog_logger *lg, ...);
> > > 
> > > So in your patches something like:
> > > 
> > >   struct xentoollog_logger;
> > > 
> > >   xenevtchn_handle *xenevtchn_open(struct xentoollog_logger *logger,
> > >                                    unsigned open_flags);
> > I'll do something like this for tools/libs/*/include.
> > 
> > FYI the underlying patches are all in since Friday.
> 
> How about doing the header strictness check on all headers at one?  If
> repeating typedefs is a gccism, that should catch it.

AFAICT the issue here is that newer gcc tolerates duplicate typedefs under
many circumstances. Seems to take -pedantic-errors to make it not, and I
don't see another to enable the warning.

Ian.


_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

* [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries
@ 2016-01-15 13:22 Ian Campbell
  0 siblings, 0 replies; 113+ messages in thread
From: Ian Campbell @ 2016-01-15 13:22 UTC (permalink / raw)
  To: xen-devel
  Cc: Wei Liu, Stefano Stabellini, Ian Jackson, qemu-devel,
	minios-devel, samuel.thibault, Roger Pau Monne

In <1431963008.4944.80.camel@citrix.com> I proposed stabilising some
parts of the libxenctrl API/ABI by disaggregating into separate
libraries.

This is v8 of that set of series against:
    xen
    qemu-xen
    qemu-xen-traditional
    mini-os

NB: Samuel+minios-devel will only get the mini-os side and Stefano+qemu
-devel the qemu-xen side.

The code for all repos can be found in:

git://xenbits.xen.org/people/ianc/libxenctrl-split/xen.git                  v8
git://xenbits.xen.org/people/ianc/libxenctrl-split/qemu-xen.git             v8
git://xenbits.xen.org/people/ianc/libxenctrl-split/qemu-xen-traditional.git v8
git://xenbits.xen.org/people/ianc/libxenctrl-split/mini-os.git              v8

The tip of the xen.git branch contains an extra patch hacking Config.mk
to point to all the others above, which should get the correct things for
the HEAD of the branch, but not further back in time.

The new libraries here are:

 * libxentoollog: Common logging infrastructure (already in tree)
 * libxenevtchn: Userspace access to evtchns (via /dev/xen/evtchn etc)
 * libxengnttab: Userspace access to grant tables (via /dev/xen/gnt??? etc)
 * libxencall: Making hypercalls (i.e. the IOCTL_PRIVCMD_HYPERCALL type
   functionality)
 * libxenforeignmemory: Privileged mappings of foreign memory
   (IOCTL_PRIVCMD_MMAP et al)

The first three were actually pretty distinct within libxenctrl already and
have not changed in quite some time.

Although the other two are somewhat new they are based on top of long
standing stable ioctls, which gives me some confidence.

Nonetheless I would appreciate extra review of at least the interface
headers of all of these with a particular eye to the suitability of these
interfaces being maintained in an ABI (_B_, not _P_) stable way going
forward.

Still to come would be libraries for specific out of tree purposes
(device model, kexec), which would be adding new library at the same
level as libxc I think, rather than underneath, i.e. also using the
libraries split out here, but hopefully not libxenctrl itself.

The new libraries use linker version-scripts to hopefully make future
ABI changes be possible in a compatible way.

Since last time:

 * Some early bits went in.
 * Rebased
 * Clean up the *.so in clean, added distclean targets to each lib
 * On the QEMU side use CONFIG_XEN_CTRL_INTERFACE_VERSION == 471 as the
   gate for this new setup (dropped a Reviewed-by).

Even with the dropped acks mini-os and qemu-xen-trad are fully acked (by
Samuel+Wei and Ian J respectively), while qemu-xen and xen are mostly acked
(but had a few dropped acks since last time).

Summary for qemu-xen.git:
 R	xen_console: correctly cleanup primary console on teardown.
 R	xen: Switch to libxenevtchn interface for compat shims.
 R	xen: Switch to libxengnttab interface for compat shims.
 R	xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages
  M	xen: Switch uses of xc_map_foreign_{pages,bulk} to use libxenforeignmemory API.
  M	xen: Use stable library interfaces when they are available.
A	xen: domainbuild: reopen libxenctrl interface after forking for domain watcher.
 R	xen: make it possible to build without the Xen PV domain builder

(A == Acked by Stefano, R == Reviewed by Stefano, M == Modified in v8)

NB: qemu-xen-traditional.git, mini-os.git and xen.git are intertwined, but
the qemu-xen.git part is independent and should be applied after all the
rest of these series.

Summary for xen.git:

 W	tools/libxc: Remove osdep indirection for xc_evtchn
MWI	tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn.
 W	tools: Arrange to check public headers for ANSI compatiblity
 W	tools/libxc: Remove osdep indirection for xc_gnt{shr,tab}
MW	tools: Refactor /dev/xen/gnt{dev,shr} wrappers into libxengnttab.
 W    S	tools/libxc: Remove osdep indirection for privcmd
MW	tools: Refactor hypercall calling wrappers into libxencall.
 W	tools/libxc: drop xc_map_foreign_bulk_compat wrappers
 W   G	tools: Remove xc_map_foreign_batch
 W	tools: Implement xc_map_foreign_range(s) in terms of common helper
MWI	tools: Refactor foreign memory mapping into libxenforeignmemory
 WI	tools/libs/foreignmemory: provide xenforeignmemory_unmap.
 WI	tools/libs/foreignmemory: use size_t for size arguments.
	tools/libs/foreignmemory: Mention restrictions on fork in docs.
 WI	tools/libs/foreignmemory: Support err == NULL to map.
 WI	tools/libs/foreignmemory: pull array length argument to map forward
 WI	tools/libs/evtchn: Review and update doc comments.
N	tools/libs/evtchn: Use uint32_t for domid arguments
 W	tools/libs: Clean up hard tabs.
    D	tools/libs/gnttab: Extensive updates to API documentation.
 W	tools/libs/call: Update some log messages to not refer to xc.
 WIR	tools/libs/call: Describe return values and error semantics for xencall*
 W	tools/libs/call: Avoid xc_memalign in netbsd and solaris backends
	tools/libs/call: linux: touch newly allocated pages after madvise lockdown
	tools/libs/{call,evtchn}: Document requirements around forking.
M  R	tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
 W	tools: Update CFLAGS for qemu-xen to allow it to use new libraries
N	tools/libs/*: Introduce APIs to restrict handles to a specific domain.

N == New in v8
W == Acked by Wei
R == Acked by Roger
I == Acked by Ian J
D == Acked by Daniel
G == Acked by George
S == Acked by Dave (Scott)
M == Modified (in all cases minor enough that I didn't drop acks)

Therefore needing attention from Ian and/or Wei are:

	tools/libs/foreignmemory: Mention restrictions on fork in docs.
N	tools/libs/evtchn: Use uint32_t for domid arguments
    D	tools/libs/gnttab: Extensive updates to API documentation.
	
tools/libs/call: linux: touch newly allocated pages after madvise l
	
tools/libs/{call,evtchn}: Document requirements around forking.
   R	
tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
N	tools/libs/*:
Introduce APIs to restrict handles to a specific doma

The whole thing has been build tested on Linux (incl stubdoms), and on
FreeBSD. I have runtime tested on Linux with qemu-xen, qemu-xen-trad and
stubdoms.

Neither NetBSD nor Solaris have been tested at all. It's certainly not
impossible that I've not got the #includes in the new files quite right.

http://xenbits.xen.org/people/ianc/libxenctrl-split/v8.html is the document
I've been using to try and track what I'm doing. It may not be all that
useful. The history of it is in the v8-with-doc branch of the xen.git
linked to above.

Ian.


_______________________________________________
Minios-devel mailing list
Minios-devel@lists.xenproject.org
http://lists.xenproject.org/cgi-bin/mailman/listinfo/minios-devel

_______________________________________________
Xen-devel mailing list
Xen-devel@lists.xen.org
http://lists.xen.org/xen-devel

^ permalink raw reply	[flat|nested] 113+ messages in thread

end of thread, other threads:[~2016-01-25 15:28 UTC | newest]

Thread overview: 113+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-15 13:22 [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
2016-01-15 13:22 ` [PATCH XEN v8 00/29] " Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 01/29] tools/libxc: Remove osdep indirection for xc_evtchn Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 02/29] tools: Refactor /dev/xen/evtchn wrappers into libxenevtchn Ian Campbell
2016-01-22 11:48     ` Ian Campbell
2016-01-22 17:12     ` Boris Ostrovsky
2016-01-25  9:48       ` Ian Campbell
2016-01-25 14:35         ` Ian Jackson
2016-01-25 14:39           ` Boris Ostrovsky
2016-01-25 14:47           ` Ian Campbell
2016-01-25 14:49             ` Andrew Cooper
2016-01-25 15:28               ` Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 03/29] tools: Arrange to check public headers for ANSI compatiblity Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 04/29] tools/libxc: Remove osdep indirection for xc_gnt{shr, tab} Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 05/29] tools: Refactor /dev/xen/gnt{dev, shr} wrappers into libxengnttab Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 06/29] tools/libxc: Remove osdep indirection for privcmd Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 07/29] tools: Refactor hypercall calling wrappers into libxencall Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 08/29] tools/libxc: drop xc_map_foreign_bulk_compat wrappers Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 09/29] tools: Remove xc_map_foreign_batch Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 10/29] tools: Implement xc_map_foreign_range(s) in terms of common helper Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 11/29] tools: Refactor foreign memory mapping into libxenforeignmemory Ian Campbell
2016-01-25 12:01     ` Olaf Hering
2016-01-25 12:31       ` Ian Campbell
2016-01-25 12:44         ` Ian Campbell
2016-01-25 12:45           ` [PATCH] kdd: Opt in to libxc compat xc_map_foreign_* intefaces Ian Campbell
2016-01-25 13:25             ` Tim Deegan
2016-01-25 14:22               ` Ian Campbell
2016-01-25 14:00             ` Olaf Hering
2016-01-25 13:18           ` [PATCH] kdd: build using Werror Ian Campbell
2016-01-25 13:20             ` Olaf Hering
2016-01-25 13:24               ` Ian Campbell
2016-01-25 13:25             ` Tim Deegan
2016-01-25 14:22               ` Ian Campbell
2016-01-25 14:37             ` Olaf Hering
2016-01-15 13:22   ` [PATCH XEN v8 12/29] tools/libs/foreignmemory: provide xenforeignmemory_unmap Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 13/29] tools/libs/foreignmemory: use size_t for size arguments Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 14/29] tools/libs/foreignmemory: Mention restrictions on fork in docs Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-19 13:34       ` Ian Campbell
2016-01-19 14:25         ` Wei Liu
2016-01-15 13:22   ` [PATCH XEN v8 15/29] tools/libs/foreignmemory: Support err == NULL to map Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 16/29] tools/libs/foreignmemory: pull array length argument to map forward Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 17/29] tools/libs/evtchn: Review and update doc comments Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 18/29] tools/libs/evtchn: Use uint32_t for domid arguments Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-15 13:22   ` [PATCH XEN v8 19/29] tools/libs: Clean up hard tabs Ian Campbell
2016-01-15 13:22   ` [PATCH XEN v8 20/29] tools/libs/gnttab: Extensive updates to API documentation Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-15 13:23   ` [PATCH XEN v8 21/29] tools/libs/call: Update some log messages to not refer to xc Ian Campbell
2016-01-15 13:23   ` [PATCH XEN v8 22/29] tools/libs/call: Describe return values and error semantics for xencall* Ian Campbell
2016-01-15 13:23   ` [PATCH XEN v8 23/29] tools/libs/call: Avoid xc_memalign in netbsd and solaris backends Ian Campbell
2016-01-15 13:23   ` [PATCH XEN v8 24/29] tools/libs/call: linux: touch newly allocated pages after madvise lockdown Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-19 13:40       ` Ian Campbell
2016-01-19 14:26         ` Wei Liu
2016-01-19 14:54       ` Roger Pau Monné
2016-01-19 14:58         ` Wei Liu
2016-01-19 15:03           ` Ian Campbell
2016-01-19 15:49             ` Wei Liu
2016-01-19 15:59               ` Ian Campbell
2016-01-15 13:23   ` [PATCH XEN v8 25/29] tools/libs/{call, evtchn}: Document requirements around forking Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-15 13:23   ` [PATCH XEN v8 26/29] tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-15 13:23   ` [PATCH XEN v8 27/29] tools: Update CFLAGS for qemu-xen to allow it to use new libraries Ian Campbell
2016-01-15 13:23   ` [PATCH XEN v8 28/29] tools/libs/*: Introduce APIs to restrict handles to a specific domain Ian Campbell
2016-01-19 13:24     ` Wei Liu
2016-01-19 13:44       ` Ian Campbell
2016-01-19 14:30         ` Wei Liu
2016-01-15 13:23   ` [PATCH XEN v8 29/29] HACK: Update Config.mk to pull all the right bits from my xenbits trees Ian Campbell
2016-01-15 13:23 ` [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
2016-01-15 13:23 ` [Qemu-devel] " Ian Campbell
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 1/8] xen_console: correctly cleanup primary console on teardown Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 2/8] xen: Switch to libxenevtchn interface for compat shims Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 3/8] xen: Switch to libxengnttab " Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 4/8] xen: Switch uses of xc_map_foreign_range into xc_map_foreign_pages Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 5/8] xen: Switch uses of xc_map_foreign_{pages, bulk} to use libxenforeignmemory API Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 14:43     ` [Qemu-devel] " Stefano Stabellini
2016-01-15 14:43     ` Stefano Stabellini
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 6/8] xen: Use stable library interfaces when they are available Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 14:43     ` [Qemu-devel] " Stefano Stabellini
2016-01-15 14:43       ` Stefano Stabellini
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 7/8] xen: domainbuild: reopen libxenctrl interface after forking for domain watcher Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 13:23   ` [Qemu-devel] [PATCH QEMU-XEN v8 8/8] xen: make it possible to build without the Xen PV domain builder Ian Campbell
2016-01-15 13:23     ` Ian Campbell
2016-01-15 14:44   ` [Qemu-devel] [PATCH QEMU-XEN v8 0/8] Begin to disentangle libxenctrl and provide some stable libraries Stefano Stabellini
2016-01-15 15:08     ` Ian Campbell
2016-01-15 15:08     ` [Qemu-devel] " Ian Campbell
2016-01-15 14:44   ` Stefano Stabellini
2016-01-15 13:23 ` [PATCH QEMU-XEN-TRADITIONAL v8 0/4] " Ian Campbell
2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 1/4] qemu-xen-traditional: Use libxenevtchn Ian Campbell
2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 2/4] qemu-xen-traditional: Use libxengnttab Ian Campbell
2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 3/4] qemu-xen-traditional: Add libxencall to rpath-link Ian Campbell
2016-01-15 13:23   ` [PATCH QEMU-XEN-TRADITIONAL v8 4/4] qemu-xen-traditional: Add libxenforeignmemory " Ian Campbell
2016-01-15 13:23 ` [PATCH MINI-OS v8 0/4] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
2016-01-15 13:24   ` [PATCH MINI-OS v8 1/4] mini-os: Include libxenevtchn with libxc Ian Campbell
2016-01-15 13:24   ` [PATCH MINI-OS v8 2/4] mini-os: Include libxengnttab " Ian Campbell
2016-01-15 13:24   ` [PATCH MINI-OS v8 3/4] mini-os: Include libxencall " Ian Campbell
2016-01-15 13:24   ` [PATCH MINI-OS v8 4/4] mini-os: Include libxenforeignmemory " Ian Campbell
2016-01-19 15:44 ` [Qemu-devel] [Minios-devel] [PATCH v8 0/<VARIOUS>] Begin to disentangle libxenctrl and provide some stable libraries Ian Campbell
2016-01-22 10:42   ` Ian Campbell
2016-01-22 10:42   ` [Qemu-devel] " Ian Campbell
2016-01-19 15:44 ` Ian Campbell
2016-01-22 14:14 ` [Qemu-devel] " Ian Campbell
2016-01-22 14:14 ` Ian Campbell
2016-01-15 13:22 Ian Campbell

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.