diff mbox series

[v4,17/31] tools/xenpaging: remove libxc internals

Message ID 20200828150747.25305-18-jgross@suse.com (mailing list archive)
State Superseded
Headers show
Series tools: move most libraries into tools/libs | expand

Commit Message

Jürgen Groß Aug. 28, 2020, 3:07 p.m. UTC
xenpaging is using libxc internals. Fix that.

Signed-off-by: Juergen Gross <jgross@suse.com>
Acked-by: Wei Liu <wl@xen.org>
---
 tools/xenpaging/Makefile         |  3 +-
 tools/xenpaging/file_ops.c       |  8 ++--
 tools/xenpaging/pagein.c         |  2 +-
 tools/xenpaging/policy_default.c |  2 +-
 tools/xenpaging/xenpaging.c      | 32 +++++++--------
 tools/xenpaging/xenpaging.h      | 70 +++++++++++++++++++++++++++++++-
 6 files changed, 92 insertions(+), 25 deletions(-)
diff mbox series

Patch

diff --git a/tools/xenpaging/Makefile b/tools/xenpaging/Makefile
index 968678c4e8..04743b335c 100644
--- a/tools/xenpaging/Makefile
+++ b/tools/xenpaging/Makefile
@@ -1,8 +1,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_libxencall)
+CFLAGS += $(CFLAGS_libxentoollog) $(CFLAGS_libxenevtchn) $(CFLAGS_libxenctrl) $(CFLAGS_libxenstore) $(PTHREAD_CFLAGS) $(CFLAGS_libxencall)
 LDLIBS += $(LDLIBS_libxentoollog) $(LDLIBS_libxenevtchn) $(LDLIBS_libxenctrl) $(LDLIBS_libxenstore) $(PTHREAD_LIBS)
 LDFLAGS += $(PTHREAD_LDFLAGS)
 
diff --git a/tools/xenpaging/file_ops.c b/tools/xenpaging/file_ops.c
index 8210f426df..301fb3427c 100644
--- a/tools/xenpaging/file_ops.c
+++ b/tools/xenpaging/file_ops.c
@@ -20,7 +20,7 @@ 
 
 
 #include <unistd.h>
-#include <xc_private.h>
+#include <xenctrl.h>
 
 static int file_op(int fd, void *page, int i,
                    ssize_t (*fn)(int, void *, size_t))
@@ -29,13 +29,13 @@  static int file_op(int fd, void *page, int i,
     int total = 0;
     int bytes;
 
-    offset = lseek(fd, offset << PAGE_SHIFT, SEEK_SET);
+    offset = lseek(fd, offset << XC_PAGE_SHIFT, SEEK_SET);
     if ( offset == (off_t)-1 )
         return -1;
 
-    while ( total < PAGE_SIZE )
+    while ( total < XC_PAGE_SIZE )
     {
-        bytes = fn(fd, page + total, PAGE_SIZE - total);
+        bytes = fn(fd, page + total, XC_PAGE_SIZE - total);
         if ( bytes <= 0 )
             return -1;
 
diff --git a/tools/xenpaging/pagein.c b/tools/xenpaging/pagein.c
index 2a298b436c..1c8a7d983d 100644
--- a/tools/xenpaging/pagein.c
+++ b/tools/xenpaging/pagein.c
@@ -43,7 +43,7 @@  static void *page_in(void *arg)
         /* Ignore errors */
         page = xc_map_foreign_pages(pia->xch, pia->dom, PROT_READ, gfns, num);
         if (page)
-            munmap(page, PAGE_SIZE * num);
+            munmap(page, XC_PAGE_SIZE * num);
     }
     page_in_possible = 0;
     pthread_exit(NULL);
diff --git a/tools/xenpaging/policy_default.c b/tools/xenpaging/policy_default.c
index 3324835957..979d251230 100644
--- a/tools/xenpaging/policy_default.c
+++ b/tools/xenpaging/policy_default.c
@@ -18,8 +18,8 @@ 
  * along with this program; If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <errno.h>
 
-#include "xc_bitops.h"
 #include "policy.h"
 
 
diff --git a/tools/xenpaging/xenpaging.c b/tools/xenpaging/xenpaging.c
index d0571cabac..33098046c2 100644
--- a/tools/xenpaging/xenpaging.c
+++ b/tools/xenpaging/xenpaging.c
@@ -30,7 +30,6 @@ 
 #include <xenstore.h>
 #include <getopt.h>
 
-#include "xc_bitops.h"
 #include "file_ops.h"
 #include "policy.h"
 #include "xenpaging.h"
@@ -183,12 +182,12 @@  static void *init_page(void)
     void *buffer;
 
     /* Allocated page memory */
-    errno = posix_memalign(&buffer, PAGE_SIZE, PAGE_SIZE);
+    errno = posix_memalign(&buffer, XC_PAGE_SIZE, XC_PAGE_SIZE);
     if ( errno != 0 )
         return NULL;
 
     /* Lock buffer in memory so it can't be paged out */
-    if ( mlock(buffer, PAGE_SIZE) < 0 )
+    if ( mlock(buffer, XC_PAGE_SIZE) < 0 )
     {
         free(buffer);
         buffer = NULL;
@@ -277,7 +276,6 @@  static struct xenpaging *xenpaging_init(int argc, char *argv[])
     struct xenpaging *paging;
     xc_domaininfo_t domain_info;
     xc_interface *xch = NULL;
-    xentoollog_logger *dbg = NULL;
     char *p;
     int rc;
     unsigned long ring_pfn, mmap_pfn;
@@ -291,12 +289,11 @@  static struct xenpaging *xenpaging_init(int argc, char *argv[])
     if ( xenpaging_getopts(paging, argc, argv) )
         goto err;
 
-    /* Enable debug output */
-    if ( paging->debug )
-        dbg = (xentoollog_logger *)xtl_createlogger_stdiostream(stderr, XTL_DEBUG, 0);
+    paging->logger = (xentoollog_logger *)xtl_createlogger_stdiostream(stderr,
+                         paging->debug ? XTL_DEBUG : XTL_PROGRESS, 0);
 
     /* Open connection to xen */
-    paging->xc_handle = xch = xc_interface_open(dbg, NULL, 0);
+    paging->xc_handle = xch = xc_interface_open(paging->logger, NULL, 0);
     if ( !xch )
         goto err;
 
@@ -416,7 +413,7 @@  static struct xenpaging *xenpaging_init(int argc, char *argv[])
     SHARED_RING_INIT((vm_event_sring_t *)paging->vm_event.ring_page);
     BACK_RING_INIT(&paging->vm_event.back_ring,
                    (vm_event_sring_t *)paging->vm_event.ring_page,
-                   PAGE_SIZE);
+                   XC_PAGE_SIZE);
 
     /* Now that the ring is set, remove it from the guest's physmap */
     if ( xc_domain_decrease_reservation_exact(xch, 
@@ -490,15 +487,17 @@  static struct xenpaging *xenpaging_init(int argc, char *argv[])
             xs_close(paging->xs_handle);
         if ( xch )
             xc_interface_close(xch);
+        if ( paging->logger )
+            xtl_logger_destroy(paging->logger);
         if ( paging->paging_buffer )
         {
-            munlock(paging->paging_buffer, PAGE_SIZE);
+            munlock(paging->paging_buffer, XC_PAGE_SIZE);
             free(paging->paging_buffer);
         }
 
         if ( paging->vm_event.ring_page )
         {
-            munmap(paging->vm_event.ring_page, PAGE_SIZE);
+            munmap(paging->vm_event.ring_page, XC_PAGE_SIZE);
         }
 
         free(dom_path);
@@ -523,7 +522,7 @@  static void xenpaging_teardown(struct xenpaging *paging)
 
     paging->xc_handle = NULL;
     /* Tear down domain paging in Xen */
-    munmap(paging->vm_event.ring_page, PAGE_SIZE);
+    munmap(paging->vm_event.ring_page, XC_PAGE_SIZE);
     rc = xc_mem_paging_disable(xch, paging->vm_event.domain_id);
     if ( rc != 0 )
     {
@@ -551,6 +550,8 @@  static void xenpaging_teardown(struct xenpaging *paging)
 
     /* Close connection to Xen */
     xc_interface_close(xch);
+
+    xtl_logger_destroy(paging->logger);
 }
 
 static void get_request(struct vm_event *vm_event, vm_event_request_t *req)
@@ -598,8 +599,7 @@  static int xenpaging_evict_page(struct xenpaging *paging, unsigned long gfn, int
     void *page;
     xen_pfn_t victim = gfn;
     int ret;
-
-    DECLARE_DOMCTL;
+    struct xen_domctl domctl;
 
     /* Nominate page */
     ret = xc_mem_paging_nominate(xch, paging->vm_event.domain_id, gfn);
@@ -627,13 +627,13 @@  static int xenpaging_evict_page(struct xenpaging *paging, unsigned long gfn, int
     if ( ret < 0 )
     {
         PERROR("Error copying page %lx", gfn);
-        munmap(page, PAGE_SIZE);
+        munmap(page, XC_PAGE_SIZE);
         ret = -1;
         goto out;
     }
 
     /* Release page */
-    munmap(page, PAGE_SIZE);
+    munmap(page, XC_PAGE_SIZE);
 
     /* Tell Xen to evict page */
     ret = xc_mem_paging_evict(xch, paging->vm_event.domain_id, gfn);
diff --git a/tools/xenpaging/xenpaging.h b/tools/xenpaging/xenpaging.h
index d6c8ee5221..e0fc7b31b8 100644
--- a/tools/xenpaging/xenpaging.h
+++ b/tools/xenpaging/xenpaging.h
@@ -23,11 +23,17 @@ 
 #ifndef __XEN_PAGING2_H__
 #define __XEN_PAGING2_H__
 
+#include <malloc.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <sys/mman.h>
 
 #include <xenevtchn.h>
 #define XC_WANT_COMPAT_MAP_FOREIGN_API
 #include <xenctrl.h>
-#include <xc_private.h>
+// #include <xc_private.h>
 #include <xen/event_channel.h>
 #include <xen/vm_event.h>
 
@@ -44,6 +50,7 @@  struct vm_event {
 
 struct xenpaging {
     xc_interface *xc_handle;
+    xentoollog_logger *logger;
     struct xs_handle *xs_handle;
 
     unsigned long *bitmap;
@@ -67,9 +74,70 @@  struct xenpaging {
     unsigned long pagein_queue[XENPAGING_PAGEIN_QUEUE_SIZE];
 };
 
+#define DPRINTF(msg, args...) xtl_log(paging->logger, XTL_DETAIL, 0,      \
+                                      "paging", msg, ## args)
+#define ERROR(msg, args...)   xtl_log(paging->logger, XTL_ERROR, -1,      \
+                                      "paging", msg, ## args)
+#define PERROR(msg, args...)  xtl_log(paging->logger, XTL_ERROR, -1,      \
+                                      "paging", msg "(%d = %s)", ## args, \
+                                      errno, strerror(errno))
+
 extern void create_page_in_thread(struct xenpaging *paging);
 extern void page_in_trigger(void);
 
+#define BITS_PER_LONG (sizeof(unsigned long) * 8)
+#define ORDER_LONG (sizeof(unsigned long) == 4 ? 5 : 6)
+
+#define BITMAP_ENTRY(_nr,_bmap) ((_bmap))[(_nr) / 8]
+#define BITMAP_SHIFT(_nr) ((_nr) % 8)
+
+static inline int bitmap_size(int nr_bits)
+{
+    return (nr_bits + 7) / 8;
+}
+
+static inline void *bitmap_alloc(int nr_bits)
+{
+    return calloc(1, bitmap_size(nr_bits));
+}
+
+static inline void bitmap_clear(void *addr, int nr_bits)
+{
+    memset(addr, 0, bitmap_size(nr_bits));
+}
+
+static inline int test_bit(int nr, const void *_addr)
+{
+    const char *addr = _addr;
+    return (BITMAP_ENTRY(nr, addr) >> BITMAP_SHIFT(nr)) & 1;
+}
+
+static inline void clear_bit(int nr, void *_addr)
+{
+    char *addr = _addr;
+    BITMAP_ENTRY(nr, addr) &= ~(1UL << BITMAP_SHIFT(nr));
+}
+
+static inline void set_bit(int nr, void *_addr)
+{
+    char *addr = _addr;
+    BITMAP_ENTRY(nr, addr) |= (1UL << BITMAP_SHIFT(nr));
+}
+
+static inline int test_and_clear_bit(int nr, void *addr)
+{
+    int oldbit = test_bit(nr, addr);
+    clear_bit(nr, addr);
+    return oldbit;
+}
+
+static inline int test_and_set_bit(int nr, void *addr)
+{
+    int oldbit = test_bit(nr, addr);
+    set_bit(nr, addr);
+    return oldbit;
+}
+
 #endif // __XEN_PAGING_H__