Skip to contentSkip to conversation

some libxc cleanups and xen-mfndump

PATCH In review Dario Faggioli 2 participants 16 messages Sep 10, 2013 – Sep 13, 2013

lore
Share & tools
lore
#0 lore raw

v2 cover some libxc cleanups and xen-mfndump

Hi everyone,

This is take 2 of an old submission of mine, doing some basic libxc cleanups and introducing a debugging tool I called xen-mfndump.

Basically, there are a few places in libxc where we issue do_domctl()-s directly, instead of using the proper xc_xxx() wrapper. This make the code bigger, redundant, more difficult to understand (e.g., "why xc_xxx() is called here and is not called there?"), less consistent and less grep-able. The first 4 patches of this series address this.

The last one introduce a little tool I'm using for debugging. It allows one to see the M2P of the host, the P2M of a domain, some info about the PTEs, and perform basic searches and comparisons on them. I'm finding it very useful and, although I do not claim for my situation to be that common, I figured it could be nice to have it in the tree, so that is patch 6. Patch 5 is the small rework/additions necessary to make 6 possible.

Patches with an '*' next to their name below have been Reviewed-by Andrew Cooper.

Thanks and Regards,
Dario

---
Dario Faggioli (6):
libxc: introduce xc_domain_get_guest_width()
* libxc: use xc_vcpu_setcontext() instead of calling do_domctl()
libxc: use xc_vcpu_getinfo() instead of calling do_domctl()
* libxc: allow for ctxt to be NULL in xc_vcpu_setcontext
libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo) tools/misc: introduce xen-mfndump.

 tools/libxc/xc_core.c           |   20 --
 tools/libxc/xc_cpuid_x86.c      |    8 -
 tools/libxc/xc_dom_boot.c       |   31 +--
 tools/libxc/xc_dom_x86.c        |   13 -
 tools/libxc/xc_domain.c         |  149 +++++++++++++-
 tools/libxc/xc_domain_restore.c |   13 -
 tools/libxc/xc_offline_page.c   |  192 +++---------------
 tools/libxc/xc_pagetab.c        |    8 -
 tools/libxc/xc_private.c        |    8 -
 tools/libxc/xc_resume.c         |   21 --
 tools/libxc/xenctrl.h           |   13 +
 tools/libxc/xenguest.h          |   17 ++
 tools/libxc/xg_private.h        |    9 +
 tools/libxc/xg_save_restore.h   |    9 -
 tools/misc/Makefile             |    7 -
 tools/misc/xen-mfndump.c        |  425 +++++++++++++++++++++++++++++++++++++++
 tools/xentrace/xenctx.c         |    9 -
 17 files changed, 679 insertions(+), 273 deletions(-)
 create mode 100644 tools/misc/xen-mfndump.c
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)
#1 lore raw

PATCH v2 1/6 libxc: introduce xc_domain_get_guest_width()

As a wrapper to XEN_DOMCTL_get_address_size, and use it wherever the call was being issued directly via do_domctl(), saving quite some line of code.

Actually, the function returns the guest width in bytes, rather than directly what XEN_DOMCTL_get_address_size provides (which is a number of bits), since that is what it is useful almost everywhere.

---
Changes from v1:
* function renamed and made return bytes instead of bits that had then to be "/=8" in most of the call sites, as suggested during review.
* taking care of xc_dom_x86.c:xc_domain_get_native_protocol(), as requested during review

---
 tools/libxc/xc_core.c         |   20 +-------------------
 tools/libxc/xc_cpuid_x86.c    |    8 +++-----
 tools/libxc/xc_dom_x86.c      |   13 +++----------
 tools/libxc/xc_domain.c       |   16 ++++++++++++++++
 tools/libxc/xc_offline_page.c |    9 +--------
 tools/libxc/xc_pagetab.c      |    8 +++-----
 tools/libxc/xc_resume.c       |   21 ++++-----------------
 tools/libxc/xenctrl.h         |   13 +++++++++++++
 tools/libxc/xg_save_restore.h |    9 +--------
 tools/xentrace/xenctx.c       |    9 +++------
 10 files changed, 48 insertions(+), 78 deletions(-)
tools/libxc/xc_core.c +1 -19
index 4207eed..4bc1abb 100644
@@ -417,24 +417,6 @@ elfnote_dump_format_version(xc_interface *xch,
     return dump_rtn(xch, args, (char*)&format_version, sizeof(format_version));
 }
 
-static int
-get_guest_width(xc_interface *xch,
-                uint32_t domid,
-                unsigned int *guest_width)
-{
-    DECLARE_DOMCTL;
-
-    memset(&domctl, 0, sizeof(domctl));
-    domctl.domain = domid;
-    domctl.cmd = XEN_DOMCTL_get_address_size;
-
-    if ( do_domctl(xch, &domctl) != 0 )
-        return 1;
-        
-    *guest_width = domctl.u.address_size.size / 8;
-    return 0;
-}
-
 int
 xc_domain_dumpcore_via_callback(xc_interface *xch,
                                 uint32_t domid,
@@ -478,7 +460,7 @@ xc_domain_dumpcore_via_callback(xc_interface *xch,
     struct xc_core_section_headers *sheaders = NULL;
     Elf64_Shdr *shdr;
  
-    if ( get_guest_width(xch, domid, &dinfo->guest_width) != 0 )
+    if ( xc_domain_get_guest_width(xch, domid, &dinfo->guest_width) != 0 )
     {
         PERROR("Could not get address size for domain");
         return sts;
tools/libxc/xc_cpuid_x86.c +3 -5
index fa47787..bbbf9b8 100644
@@ -436,17 +436,15 @@ static void xc_cpuid_pv_policy(
     const unsigned int *input, unsigned int *regs)
 {
     DECLARE_DOMCTL;
+    unsigned int guest_width;
     int guest_64bit, xen_64bit = hypervisor_is_64bit(xch);
     char brand[13];
     uint64_t xfeature_mask;
 
     xc_cpuid_brand_get(brand);
 
-    memset(&domctl, 0, sizeof(domctl));
-    domctl.domain = domid;
-    domctl.cmd = XEN_DOMCTL_get_address_size;
-    do_domctl(xch, &domctl);
-    guest_64bit = (domctl.u.address_size.size == 64);
+    xc_domain_get_guest_width(xch, domid, &guest_width);
+    guest_64bit = (guest_width == 8);
 
     /* Detecting Xen's atitude towards XSAVE */
     memset(&domctl, 0, sizeof(domctl));
tools/libxc/xc_dom_x86.c +3 -10
index 126c0f8..975656d 100644
@@ -54,24 +54,17 @@ const char *xc_domain_get_native_protocol(xc_interface *xch,
     int ret;
     uint32_t guest_width;
     const char *protocol;
-    DECLARE_DOMCTL;
 
-    memset(&domctl, 0, sizeof(domctl));
-    domctl.domain = domid;
-    domctl.cmd = XEN_DOMCTL_get_address_size;
-
-    ret = do_domctl(xch, &domctl);
+    ret = xc_domain_get_guest_width(xch, domid, &guest_width);
 
     if ( ret )
         return NULL;
 
-    guest_width = domctl.u.address_size.size;
-
     switch (guest_width) {
-    case 32: /* 32 bit guest */
+    case 4: /* 32 bit guest */
         protocol = XEN_IO_PROTO_ABI_X86_32;
         break;
-    case 64: /* 64 bit guest */
+    case 8: /* 64 bit guest */
         protocol = XEN_IO_PROTO_ABI_X86_64;
         break;
     default:
tools/libxc/xc_domain.c +16 -0
index 3257e2a..c0ce079 100644
@@ -270,6 +270,22 @@ out:
     return ret;
 }
 
+int xc_domain_get_guest_width(xc_interface *xch, uint32_t domid,
+                              unsigned int *guest_width)
+{
+    DECLARE_DOMCTL;
+
+    memset(&domctl, 0, sizeof(domctl));
+    domctl.domain = domid;
+    domctl.cmd = XEN_DOMCTL_get_address_size;
+
+    if ( do_domctl(xch, &domctl) != 0 )
+        return 1;
+
+    /* We want the result in bytes */
+    *guest_width = domctl.u.address_size.size / 8;
+    return 0;
+}
 
 int xc_domain_getinfo(xc_interface *xch,
                       uint32_t first_domid,
tools/libxc/xc_offline_page.c +1 -8
index 36b9812..1f6dbc6 100644
@@ -193,21 +193,14 @@ static int get_pt_level(xc_interface *xch, uint32_t domid,
                         unsigned int *pt_level,
                         unsigned int *gwidth)
 {
-    DECLARE_DOMCTL;
     xen_capabilities_info_t xen_caps = "";
 
     if (xc_version(xch, XENVER_capabilities, &xen_caps) != 0)
         return -1;
 
-    memset(&domctl, 0, sizeof(domctl));
-    domctl.domain = domid;
-    domctl.cmd = XEN_DOMCTL_get_address_size;
-
-    if ( do_domctl(xch, &domctl) != 0 )
+    if (xc_domain_get_guest_width(xch, domid, gwidth) != 0)
         return -1;
 
-    *gwidth = domctl.u.address_size.size / 8;
-
     if (strstr(xen_caps, "xen-3.0-x86_64"))
         /* Depends on whether it's a compat 32-on-64 guest */
         *pt_level = ( (*gwidth == 8) ? 4 : 3 );
tools/libxc/xc_pagetab.c +3 -5
index 27c4e9f..8525967 100644
@@ -51,15 +51,13 @@ unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom,
         pt_levels = (ctx.msr_efer&EFER_LMA) ? 4 : (ctx.cr4&CR4_PAE) ? 3 : 2;
         paddr = ctx.cr3 & ((pt_levels == 3) ? ~0x1full : ~0xfffull);
     } else {
-        DECLARE_DOMCTL;
+        unsigned int gwidth;
         vcpu_guest_context_any_t ctx;
         if (xc_vcpu_getcontext(xch, dom, vcpu, &ctx) != 0)
             return 0;
-        domctl.domain = dom;
-        domctl.cmd = XEN_DOMCTL_get_address_size;
-        if ( do_domctl(xch, &domctl) != 0 )
+        if (xc_domain_get_guest_width(xch, dom, &gwidth) != 0)
             return 0;
-        if (domctl.u.address_size.size == 64) {
+        if (gwidth == 8) {
             pt_levels = 4;
             paddr = (uint64_t)xen_cr3_to_pfn_x86_64(ctx.x64.ctrlreg[3])
                 << PAGE_SHIFT;
tools/libxc/xc_resume.c +4 -17
index 1c43ec6..cb61650 100644
@@ -24,19 +24,6 @@
 #include <xen/foreign/x86_64.h>
 #include <xen/hvm/params.h>
 
-static int pv_guest_width(xc_interface *xch, uint32_t domid)
-{
-    DECLARE_DOMCTL;
-    domctl.domain = domid;
-    domctl.cmd = XEN_DOMCTL_get_address_size;
-    if ( xc_domctl(xch, &domctl) != 0 )
-    {
-        PERROR("Could not get guest address size");
-        return -1;
-    }
-    return domctl.u.address_size.size / 8;
-}
-
 static int modify_returncode(xc_interface *xch, uint32_t domid)
 {
     vcpu_guest_context_any_t ctxt;
@@ -71,8 +58,7 @@ static int modify_returncode(xc_interface *xch, uint32_t domid)
     else
     {
         /* Probe PV guest address width. */
-        dinfo->guest_width = pv_guest_width(xch, domid);
-        if ( dinfo->guest_width < 0 )
+        if ( xc_domain_get_guest_width(xch, domid, &dinfo->guest_width) )
             return -1;
     }
 
@@ -120,7 +106,8 @@ static int xc_domain_resume_any(xc_interface *xch, uint32_t domid)
     xc_dominfo_t info;
     int i, rc = -1;
 #if defined(__i386__) || defined(__x86_64__)
-    struct domain_info_context _dinfo = { .p2m_size = 0 };
+    struct domain_info_context _dinfo = { .guest_width = 0,
+                                          .p2m_size = 0 };
     struct domain_info_context *dinfo = &_dinfo;
     unsigned long mfn;
     vcpu_guest_context_any_t ctxt;
@@ -147,7 +134,7 @@ static int xc_domain_resume_any(xc_interface *xch, uint32_t domid)
         return rc;
     }
 
-    dinfo->guest_width = pv_guest_width(xch, domid);
+    xc_domain_get_guest_width(xch, domid, &dinfo->guest_width);
     if ( dinfo->guest_width != sizeof(long) )
     {
         ERROR("Cannot resume uncooperative cross-address-size guests");
tools/libxc/xenctrl.h +13 -0
index f2cebaf..58d51f3 100644
@@ -560,6 +560,19 @@ int xc_vcpu_getaffinity(xc_interface *xch,
                         int vcpu,
                         xc_cpumap_t cpumap);
 
+
+/**
+ * This function will return the guest_width (in bytes) for the
+ * specified domain.
+ *
+ * @param xch a handle to an open hypervisor interface.
+ * @param domid the domain id one wants the address size width of.
+ * @param addr_size the address size.
+ */
+int xc_domain_get_guest_width(xc_interface *xch, uint32_t domid,
+                              unsigned int *guest_width);
+
+
 /**
  * This function will return information about one or more domains. It is
  * designed to iterate over the list of domains. If a single domain is
tools/libxc/xg_save_restore.h +1 -8
index 6512003..f859621 100644
@@ -301,7 +301,6 @@ static inline int get_platform_info(xc_interface *xch, uint32_t dom,
 {
     xen_capabilities_info_t xen_caps = "";
     xen_platform_parameters_t xen_params;
-    DECLARE_DOMCTL;
 
     if (xc_version(xch, XENVER_platform_parameters, &xen_params) != 0)
         return 0;
@@ -313,15 +312,9 @@ static inline int get_platform_info(xc_interface *xch, uint32_t dom,
 
     *hvirt_start = xen_params.virt_start;
 
-    memset(&domctl, 0, sizeof(domctl));
-    domctl.domain = dom;
-    domctl.cmd = XEN_DOMCTL_get_address_size;
-
-    if ( do_domctl(xch, &domctl) != 0 )
+    if ( xc_domain_get_guest_width(xch, dom, guest_width) != 0)
         return 0; 
 
-    *guest_width = domctl.u.address_size.size / 8;
-
     /* 64-bit tools will see the 64-bit hvirt_start, but 32-bit guests 
      * will be using the compat one. */
     if ( *guest_width < sizeof (unsigned long) )
tools/xentrace/xenctx.c +3 -6
index 060e480..1214185 100644
@@ -771,12 +771,9 @@ static void dump_ctx(int vcpu)
             }
             ctxt_word_size = (strstr(xen_caps, "xen-3.0-x86_64")) ? 8 : 4;
         } else {
-            struct xen_domctl domctl;
-            memset(&domctl, 0, sizeof domctl);
-            domctl.domain = xenctx.domid;
-            domctl.cmd = XEN_DOMCTL_get_address_size;
-            if (xc_domctl(xenctx.xc_handle, &domctl) == 0)
-                ctxt_word_size = guest_word_size = domctl.u.address_size.size / 8;
+            unsigned int gw;
+            if ( !xc_domain_get_guest_width(xenctx.xc_handle, xenctx.domid, &gw) )
+                ctxt_word_size = guest_word_size = gw;
         }
     }
 #endif
#2 lore raw

PATCH v2 2/6 libxc: use xc_vcpu_setcontext() instead of calling do_domctl()

In reply to Dario Faggioli

The wrapper is there already, so better use it in place of all the stuff required to issue a call to do_domctl() for XEN_DOMCTL_setvcpucontext.

---
 tools/libxc/xc_dom_boot.c       |   14 ++++----------
 tools/libxc/xc_domain_restore.c |    6 +-----
 2 files changed, 5 insertions(+), 15 deletions(-)
tools/libxc/xc_dom_boot.c +4 -10
index d4d57b4..cf509fa 100644
@@ -62,19 +62,13 @@ static int setup_hypercall_page(struct xc_dom_image *dom)
     return rc;
 }
 
-static int launch_vm(xc_interface *xch, domid_t domid, xc_hypercall_buffer_t *ctxt)
+static int launch_vm(xc_interface *xch, domid_t domid,
+                     vcpu_guest_context_any_t *ctxt)
 {
-    DECLARE_DOMCTL;
-    DECLARE_HYPERCALL_BUFFER_ARGUMENT(ctxt);
     int rc;
 
     xc_dom_printf(xch, "%s: called, ctxt=%p", __FUNCTION__, ctxt);
-    memset(&domctl, 0, sizeof(domctl));
-    domctl.cmd = XEN_DOMCTL_setvcpucontext;
-    domctl.domain = domid;
-    domctl.u.vcpucontext.vcpu = 0;
-    set_xen_guest_handle(domctl.u.vcpucontext.ctxt, ctxt);
-    rc = do_domctl(xch, &domctl);
+    rc = xc_vcpu_setcontext(xch, domid, 0, ctxt);
     if ( rc != 0 )
         xc_dom_panic(xch, XC_INTERNAL_ERROR,
                      "%s: SETVCPUCONTEXT failed (rc=%d)", __FUNCTION__, rc);
@@ -270,7 +264,7 @@ int xc_dom_boot_image(struct xc_dom_image *dom)
     if ( (rc = dom->arch_hooks->vcpu(dom, ctxt)) != 0 )
         return rc;
     xc_dom_unmap_all(dom);
-    rc = launch_vm(dom->xch, dom->guest_domid, HYPERCALL_BUFFER(ctxt));
+    rc = launch_vm(dom->xch, dom->guest_domid, ctxt);
 
     xc_hypercall_buffer_free(dom->xch, ctxt);
     return rc;
tools/libxc/xc_domain_restore.c +1 -5
index 63d36cd..41a63cb 100644
@@ -2113,11 +2113,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
             }
             ctxt->x64.ctrlreg[1] = FOLD_CR3(ctx->p2m[pfn]);
         }
-        domctl.cmd = XEN_DOMCTL_setvcpucontext;
-        domctl.domain = (domid_t)dom;
-        domctl.u.vcpucontext.vcpu = i;
-        set_xen_guest_handle(domctl.u.vcpucontext.ctxt, ctxt);
-        frc = xc_domctl(xch, &domctl);
+        frc = xc_vcpu_setcontext(xch, dom, i, ctxt);
         if ( frc != 0 )
         {
             PERROR("Couldn't build vcpu%d", i);
#3 lore raw

PATCH v2 3/6 libxc: use xc_vcpu_getinfo() instead of calling do_domctl()

In reply to Dario Faggioli

The wrapper is there already, so better use it in place of all the stuff required to issue a call to do_domctl() for XEN_DOMCTL_getdomaininfo.

---
Changes from v1:
* in xc_dom_boot_image(), distinguished the cases when the hypercall failed or did not find any domains, as requested during review.

---
 tools/libxc/xc_dom_boot.c       |   17 ++++++++---------
 tools/libxc/xc_domain_restore.c |    7 +++----
 tools/libxc/xc_private.c        |    8 +++-----
 3 files changed, 14 insertions(+), 18 deletions(-)
tools/libxc/xc_dom_boot.c +8 -9
index cf509fa..71e1897 100644
@@ -197,8 +197,8 @@ void *xc_dom_boot_domU_map(struct xc_dom_image *dom, xen_pfn_t pfn,
 
 int xc_dom_boot_image(struct xc_dom_image *dom)
 {
-    DECLARE_DOMCTL;
     DECLARE_HYPERCALL_BUFFER(vcpu_guest_context_any_t, ctxt);
+    xc_dominfo_t info;
     int rc;
 
     ctxt = xc_hypercall_buffer_alloc(dom->xch, ctxt, sizeof(*ctxt));
@@ -212,23 +212,22 @@ int xc_dom_boot_image(struct xc_dom_image *dom)
         return rc;
 
     /* collect some info */
-    domctl.cmd = XEN_DOMCTL_getdomaininfo;
-    domctl.domain = dom->guest_domid;
-    rc = do_domctl(dom->xch, &domctl);
-    if ( rc != 0 )
+    rc = xc_domain_getinfo(dom->xch, dom->guest_domid, 1, &info);
+    if ( rc < 0 )
     {
         xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
                      "%s: getdomaininfo failed (rc=%d)", __FUNCTION__, rc);
         return rc;
     }
-    if ( domctl.domain != dom->guest_domid )
+    if ( rc == 0 || info.domid != dom->guest_domid )
     {
         xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
-                     "%s: Huh? domid mismatch (%d != %d)", __FUNCTION__,
-                     domctl.domain, dom->guest_domid);
+                     "%s: Huh? No domains found (nr_domains=%d) "
+                     "or domid mismatch (%d != %d)", __FUNCTION__,
+                     rc, info.domid, dom->guest_domid);
         return -1;
     }
-    dom->shared_info_mfn = domctl.u.getdomaininfo.shared_info_frame;
+    dom->shared_info_mfn = info.shared_info_frame;
 
     /* sanity checks */
     if ( !xc_dom_compat_check(dom) )
tools/libxc/xc_domain_restore.c +3 -4
index 41a63cb..b418963 100644
@@ -1406,6 +1406,7 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
                       struct restore_callbacks *callbacks)
 {
     DECLARE_DOMCTL;
+    xc_dominfo_t info;
     int rc = 1, frc, i, j, n, m, pae_extended_cr3 = 0, ext_vcpucontext = 0;
     int vcpuextstate = 0;
     uint32_t vcpuextstate_size = 0;
@@ -1562,14 +1563,12 @@ int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
            ROUNDUP(MAX_BATCH_SIZE * sizeof(xen_pfn_t), PAGE_SHIFT)); 
 
     /* Get the domain's shared-info frame. */
-    domctl.cmd = XEN_DOMCTL_getdomaininfo;
-    domctl.domain = (domid_t)dom;
-    if ( xc_domctl(xch, &domctl) < 0 )
+    if ( xc_domain_getinfo(xch, (domid_t)dom, 1, &info) != 1 )
     {
         PERROR("Could not get information on new domain");
         goto out;
     }
-    shared_info_frame = domctl.u.getdomaininfo.shared_info_frame;
+    shared_info_frame = info.shared_info_frame;
 
     /* Mark all PFNs as invalid; we allocate on demand */
     for ( pfn = 0; pfn < dinfo->p2m_size; pfn++ )
tools/libxc/xc_private.c +3 -5
index acaf9e0..a260257 100644
@@ -609,11 +609,9 @@ int xc_get_pfn_list(xc_interface *xch,
 
 long xc_get_tot_pages(xc_interface *xch, uint32_t domid)
 {
-    DECLARE_DOMCTL;
-    domctl.cmd = XEN_DOMCTL_getdomaininfo;
-    domctl.domain = (domid_t)domid;
-    return (do_domctl(xch, &domctl) < 0) ?
-        -1 : domctl.u.getdomaininfo.tot_pages;
+    xc_dominfo_t info;
+    return (xc_domain_getinfo(xch, domid, 1, &info) != 1) ?
+        -1 : info.nr_pages;
 }
 
 int xc_copy_to_domain_page(xc_interface *xch,
#4 lore raw

PATCH v2 4/6 libxc: allow for ctxt to be NULL in xc_vcpu_setcontext

In reply to Dario Faggioli

Since, as can be seen in xen/common/domctl.c, that is legitimate (it results in Xen calling vcpu_reset() on the vcpu).

---
Changes from v1:
* clarified the changelog.

---
 tools/libxc/xc_domain.c |    6 ------
 1 file changed, 6 deletions(-)
tools/libxc/xc_domain.c +0 -6
index c0ce079..3210e34 100644
@@ -1141,12 +1141,6 @@ int xc_vcpu_setcontext(xc_interface *xch,
     DECLARE_HYPERCALL_BOUNCE(ctxt, sizeof(vcpu_guest_context_any_t), XC_HYPERCALL_BUFFER_BOUNCE_IN);
     int rc;
 
-    if (ctxt == NULL)
-    {
-        errno = EINVAL;
-        return -1;
-    }
-
     if ( xc_hypercall_bounce_pre(xch, ctxt) )
         return -1;
#5 lore raw

PATCH v2 5/6 libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

In reply to Dario Faggioli

And use it in xc_exchange_page(). This is basically because the following change need something really similar to the set of steps that are here abstracted in these two functions.

Despite of the change in the interface and in the signature of some functions, this is pure code motion. No functional changes involved.

---
 tools/libxc/xc_domain.c       |  127 ++++++++++++++++++++++++++++
 tools/libxc/xc_offline_page.c |  185 +++++++----------------------------------
 tools/libxc/xenguest.h        |   17 ++++
 tools/libxc/xg_private.h      |    9 ++
 4 files changed, 183 insertions(+), 155 deletions(-)
tools/libxc/xc_domain.c +127 -0
index 3210e34..81316d3 100644
@@ -21,6 +21,8 @@
  */
 
 #include "xc_private.h"
+#include "xc_core.h"
+#include "xg_private.h"
 #include "xg_save_restore.h"
 #include <xen/memory.h>
 #include <xen/hvm/hvm_op.h>
@@ -1477,6 +1479,131 @@ int xc_domain_bind_pt_isa_irq(
                                   PT_IRQ_TYPE_ISA, 0, 0, 0, machine_irq));
 }
 
+int xc_unmap_domain_meminfo(xc_interface *xch, struct xc_domain_meminfo *minfo)
+{
+    struct domain_info_context _di = { .guest_width = minfo->guest_width };
+    struct domain_info_context *dinfo = &_di;
+
+    free(minfo->pfn_type);
+    if ( minfo->p2m_table )
+        munmap(minfo->p2m_table, P2M_FLL_ENTRIES * PAGE_SIZE);
+    minfo->p2m_table = NULL;
+
+    return 0;
+}
+
+int xc_map_domain_meminfo(xc_interface *xch, int domid,
+                          struct xc_domain_meminfo *minfo)
+{
+    struct domain_info_context _di;
+    struct domain_info_context *dinfo = &_di;
+
+    xc_dominfo_t info;
+    shared_info_any_t *live_shinfo;
+    xen_capabilities_info_t xen_caps = "";
+    int i;
+
+    /* Only be initialized once */
+    if ( minfo->pfn_type || minfo->p2m_table )
+    {
+        errno = EINVAL;
+        return -1;
+    }
+
+    if ( xc_domain_getinfo(xch, domid, 1, &info) != 1 )
+    {
+        PERROR("Could not get domain info");
+        return -1;
+    }
+
+    if ( xc_domain_get_guest_width(xch, domid, &minfo->guest_width) )
+    {
+        PERROR("Could not get domain address size");
+        return -1;
+    }
+    _di.guest_width = minfo->guest_width;
+
+    /* Get page table levels (see get_platform_info() in xg_save_restore.h */
+    if ( xc_version(xch, XENVER_capabilities, &xen_caps) )
+    {
+        PERROR("Could not get Xen capabilities (for page table levels)");
+        return -1;
+    }
+    if ( strstr(xen_caps, "xen-3.0-x86_64") )
+        /* Depends on whether it's a compat 32-on-64 guest */
+        minfo->pt_levels = ( (minfo->guest_width == 8) ? 4 : 3 );
+    else if ( strstr(xen_caps, "xen-3.0-x86_32p") )
+        minfo->pt_levels = 3;
+    else if ( strstr(xen_caps, "xen-3.0-x86_32") )
+        minfo->pt_levels = 2;
+    else
+    {
+        errno = EFAULT;
+        return -1;
+    }
+
+    /* We need the shared info page for mapping the P2M */
+    live_shinfo = xc_map_foreign_range(xch, domid, PAGE_SIZE, PROT_READ,
+                                       info.shared_info_frame);
+    if ( !live_shinfo )
+    {
+        PERROR("Could not map the shared info frame (MFN 0x%lx)",
+               info.shared_info_frame);
+        return -1;
+    }
+
+    if ( xc_core_arch_map_p2m_writable(xch, minfo->guest_width, &info,
+                                       live_shinfo, &minfo->p2m_table,
+                                       &minfo->p2m_size) )
+    {
+        PERROR("Could not map the P2M table");
+        munmap(live_shinfo, PAGE_SIZE);
+        return -1;
+    }
+    munmap(live_shinfo, PAGE_SIZE);
+    _di.p2m_size = minfo->p2m_size;
+
+    /* Make space and prepare for getting the PFN types */
+    minfo->pfn_type = calloc(sizeof(*minfo->pfn_type), minfo->p2m_size);
+    if ( !minfo->pfn_type )
+    {
+        PERROR("Could not allocate memory for the PFN types");
+        goto failed;
+    }
+    for ( i = 0; i < minfo->p2m_size; i++ )
+        minfo->pfn_type[i] = pfn_to_mfn(i, minfo->p2m_table,
+                                        minfo->guest_width);
+
+    /* Retrieve PFN types in batches */
+    for ( i = 0; i < minfo->p2m_size ; i+=1024 )
+    {
+        int count = ((minfo->p2m_size - i ) > 1024 ) ?
+                        1024: (minfo->p2m_size - i);
+
+        if ( xc_get_pfn_type_batch(xch, domid, count, minfo->pfn_type + i) )
+        {
+            PERROR("Could not get %d-eth batch of PFN types", (i+1)/1024);
+            goto failed;
+        }
+    }
+
+    return 0;
+
+failed:
+    if ( minfo->pfn_type )
+    {
+        free(minfo->pfn_type);
+        minfo->pfn_type = NULL;
+    }
+    if ( minfo->p2m_table )
+    {
+        munmap(minfo->p2m_table, P2M_FLL_ENTRIES * PAGE_SIZE);
+        minfo->p2m_table = NULL;
+    }
+
+    return -1;
+}
+
 int xc_domain_memory_mapping(
     xc_interface *xch,
     uint32_t domid,
tools/libxc/xc_offline_page.c +30 -155

258 lines not on this page · raw message

tools/libxc/xenguest.h +17 -0
index 4714bd2..c12091f 100644
@@ -276,6 +276,23 @@ int xc_exchange_page(xc_interface *xch, int domid, xen_pfn_t mfn);
 
 
 /**
+ * Memory related information, such as PFN types, the P2M table,
+ * the guest word width and the guest page table levels.
+ */
+struct xc_domain_meminfo {
+    unsigned int pt_levels;
+    unsigned int guest_width;
+    xen_pfn_t *pfn_type;
+    xen_pfn_t *p2m_table;
+    unsigned long p2m_size;
+};
+
+int xc_map_domain_meminfo(xc_interface *xch, int domid,
+                          struct xc_domain_meminfo *minfo);
+
+int xc_unmap_domain_meminfo(xc_interface *xch, struct xc_domain_meminfo *mem);
+
+/**
  * This function map m2p table
  * @parm xch a handle to an open hypervisor interface
  * @parm max_mfn the max pfn
tools/libxc/xg_private.h +9 -0
index db02ccf..5ff2124 100644
@@ -136,6 +136,15 @@ struct domain_info_context {
     unsigned long p2m_size;
 };
 
+static inline xen_pfn_t pfn_to_mfn(xen_pfn_t pfn, xen_pfn_t *p2m, int gwidth)
+{
+  return ((xen_pfn_t) ((gwidth==8)?
+                       (((uint64_t *)p2m)[(pfn)]):
+                       ((((uint32_t *)p2m)[(pfn)]) == 0xffffffffU ?
+                            (-1UL) :
+                            (((uint32_t *)p2m)[(pfn)]))));
+}
+
 /* Number of xen_pfn_t in a page */
 #define FPP             (PAGE_SIZE/(dinfo->guest_width))
#6 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

tools/libxc/xg_private.h struct domain_info_context { · line 147

Quoted 9 lines · Dario Faggioli
tools/libxc/xg_private.h +9 -0
+static inline xen_pfn_t pfn_to_mfn(xen_pfn_t pfn, xen_pfn_t *p2m, int gwidth)
+{
+  return ((xen_pfn_t) ((gwidth==8)?
+                       (((uint64_t *)p2m)[(pfn)]):
+                       ((((uint32_t *)p2m)[(pfn)]) == 0xffffffffU ?
+                            (-1UL) :
+                            (((uint32_t *)p2m)[(pfn)]))));
+}
+

OMG. (since it's just moved no need to fix, but I couldn't let it pass).

#7 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

Quoted 13 lines · Ian Campbell

tools/libxc/xg_private.h struct domain_info_context { · line 147

Quoted 9 lines · Dario Faggioli
tools/libxc/xg_private.h +9 -0
+static inline xen_pfn_t pfn_to_mfn(xen_pfn_t pfn, xen_pfn_t *p2m, int gwidth)
+{
+  return ((xen_pfn_t) ((gwidth==8)?
+                       (((uint64_t *)p2m)[(pfn)]):
+                       ((((uint32_t *)p2m)[(pfn)]) == 0xffffffffU ?
+                            (-1UL) :
+                            (((uint32_t *)p2m)[(pfn)]))));
+}
+

OMG. (since it's just moved no need to fix, but I couldn't let it pass).

:-D

It is pure code motion, so I did not touch it, but I'm up for trying to refactor it a bit.

Will do in next version or, if the series gets in as is, in a follow up patch, ok?

Regards,
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#8 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

Quoted 2 lines · Dario Faggioli

Will do in next version or, if the series gets in as is, in a follow up patch, ok?

It's in my pre-commit build tests right now, so a follow up please.

Quoted 4 lines

Regards,
Dario

#9 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

Quoted 6 lines · Ian Campbell
Quoted 2 lines · Dario Faggioli

Will do in next version or, if the series gets in as is, in a follow up patch, ok?

It's in my pre-commit build tests right now, so a follow up please.

Nice, just seen it, thanks.

Follow up it is then.

Actually, I think I forgot something else, i.e., to add the new binary (xen-mfndump) to .gitignore.

I'm attaching a patch doing just that.

Sorry. :-(
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

commit 64e10283937b4d5e44e2b51dfd79f05f240a6172

Author: Dario Faggioli <[email protected]>
Date:   Wed Sep 11 10:38:08 2013 +0200

.gitignore: add tools/misc/xen-hptool

as ae763e4224304 ("tools/misc: introduce xen-mfndump") forgot to do so.

.gitignore +1 -0
index 8e539b4..3253675 100644
@@ -356,6 +356,7 @@ tools/include/xen-foreign/arm64.h
 
 .git
 tools/misc/xen-hptool
+tools/misc/xen-mfndump
 tools/libxl/_*.[ch]
 tools/libxl/testidl
 tools/libxl/testidl.c

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#10 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

Quoted 7 lines · Dario Faggioli
Quoted 6 lines · Ian Campbell
Quoted 2 lines · Dario Faggioli

Will do in next version or, if the series gets in as is, in a follow up patch, ok?

It's in my pre-commit build tests right now, so a follow up please.

Quoted 8 lines

Nice, just seen it, thanks.

Follow up it is then.

Actually, I think I forgot something else, i.e., to add the new binary (xen-mfndump) to .gitignore.

I'm attaching a patch doing just that.

Pushed, thanks.

#11 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

Quoted 10 lines · Ian Campbell
Quoted 9 lines · Dario Faggioli
Quoted 6 lines · Ian Campbell
Quoted 2 lines · Dario Faggioli

Will do in next version or, if the series gets in as is, in a follow up patch, ok?

It's in my pre-commit build tests right now, so a follow up please.

Nice, just seen it, thanks.

Quoted 8 lines
Quoted 6 lines

Follow up it is then.

Actually, I think I forgot something else, i.e., to add the new binary (xen-mfndump) to .gitignore.

I'm attaching a patch doing just that.

Pushed, thanks.

Without noticing that the title said hptool... Oh well.

#12 lore raw

Re: [PATCH v2 5/6] libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo)

Quoted 9 lines · Ian Campbell
Quoted 5 lines · Ian Campbell
Quoted 1 line · Dario Faggioli

I'm attaching a patch doing just that.

Pushed, thanks.

Without noticing that the title said hptool... Oh well.

Seriously? :-O

That puzzles me because I noticed that and fixed it in the patch file before attaching it... I'm positive about it!

I mean... I know it's to be my fault, but I really don't see what went wrong.

Anyway, sorry for that too. :-(
Dario

Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#13 lore raw

PATCH v2 6/6 tools/misc: introduce xen-mfndump.

In reply to Dario Faggioli

A little development and debugging tool, useful when looking for information about MFN to PFN mappings, MFN/PFN mappings in a guest's PTEs, etc.

This is what it can do as of now:

$ /usr/sbin/xen-mfndump
Usage: xen-mfndump <command> [args]
Commands:
  help                      show this help
  dump-m2p                  show M2P
  dump-p2m    <domid>       show P2M of <domid>
  dump-ptes   <domid> <mfn> show the PTEs in <mfn>
  lookup-pte  <domid> <mfn> find the PTE mapping <mfn>
  memcmp-mfns <domid1> <mfn1> <domid2> <mfn2>
                            (str)compare content of <mfn1> & <mfn2>
---
 tools/misc/Makefile      |    7 +
 tools/misc/xen-mfndump.c |  425 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 430 insertions(+), 2 deletions(-)
 create mode 100644 tools/misc/xen-mfndump.c
tools/misc/Makefile +5 -2
index 59def7a..17aeda5 100644
@@ -10,7 +10,7 @@ CFLAGS += $(CFLAGS_libxenstore)
 HDRS     = $(wildcard *.h)
 
 TARGETS-y := xenperf xenpm xen-tmem-list-parse gtraceview gtracestat xenlockprof xenwatchdogd xencov
-TARGETS-$(CONFIG_X86) += xen-detect xen-hvmctx xen-hvmcrash xen-lowmemd
+TARGETS-$(CONFIG_X86) += xen-detect xen-hvmctx xen-hvmcrash xen-lowmemd xen-mfndump
 TARGETS-$(CONFIG_MIGRATE) += xen-hptool
 TARGETS := $(TARGETS-y)
 
@@ -22,7 +22,7 @@ INSTALL_BIN := $(INSTALL_BIN-y)
 
 INSTALL_SBIN-y := xen-bugtool xen-python-path xenperf xenpm xen-tmem-list-parse gtraceview \
 	gtracestat xenlockprof xenwatchdogd xen-ringwatch xencov
-INSTALL_SBIN-$(CONFIG_X86) += xen-hvmctx xen-hvmcrash xen-lowmemd
+INSTALL_SBIN-$(CONFIG_X86) += xen-hvmctx xen-hvmcrash xen-lowmemd xen-mfndump
 INSTALL_SBIN-$(CONFIG_MIGRATE) += xen-hptool
 INSTALL_SBIN := $(INSTALL_SBIN-y)
 
@@ -75,6 +75,9 @@ xenlockprof: xenlockprof.o
 xen-hptool: xen-hptool.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
 
+xen-mfndump: xen-mfndump.o
+	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(APPEND_LDFLAGS)
+
 xenwatchdogd: xenwatchdogd.o
 	$(CC) $(LDFLAGS) -o $@ $< $(LDLIBS_libxenctrl) $(APPEND_LDFLAGS)
 
tools/misc/xen-mfndump.c new file +425 -0

425 lines not on this page · raw message

#14 lore raw
In reply to Dario Faggioli

[Amending the Cc-list, since I seem to have managed in including people
that shouldn't be bothered by changes like these (Jan, Keir, sorry), as well as missing someone that should instead have been there in the first place (IanJ sorry)]

Quoted 54 lines · Dario Faggioli

Hi everyone,

This is take 2 of an old submission of mine, doing some basic libxc cleanups and introducing a debugging tool I called xen-mfndump.

Basically, there are a few places in libxc where we issue do_domctl()-s directly, instead of using the proper xc_xxx() wrapper. This make the code bigger, redundant, more difficult to understand (e.g., "why xc_xxx() is called here and is not called there?"), less consistent and less grep-able. The first 4 patches of this series address this.

The last one introduce a little tool I'm using for debugging. It allows one to see the M2P of the host, the P2M of a domain, some info about the PTEs, and perform basic searches and comparisons on them. I'm finding it very useful and, although I do not claim for my situation to be that common, I figured it could be nice to have it in the tree, so that is patch 6. Patch 5 is the small rework/additions necessary to make 6 possible.

Patches with an '*' next to their name below have been Reviewed-by Andrew Cooper.

Thanks and Regards,
Dario

---
Dario Faggioli (6):
libxc: introduce xc_domain_get_guest_width()
* libxc: use xc_vcpu_setcontext() instead of calling do_domctl()
libxc: use xc_vcpu_getinfo() instead of calling do_domctl()
* libxc: allow for ctxt to be NULL in xc_vcpu_setcontext
libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo) tools/misc: introduce xen-mfndump.

 tools/libxc/xc_core.c           |   20 --
 tools/libxc/xc_cpuid_x86.c      |    8 -
 tools/libxc/xc_dom_boot.c       |   31 +--
 tools/libxc/xc_dom_x86.c        |   13 -
 tools/libxc/xc_domain.c         |  149 +++++++++++++-
 tools/libxc/xc_domain_restore.c |   13 -
 tools/libxc/xc_offline_page.c   |  192 +++---------------
 tools/libxc/xc_pagetab.c        |    8 -
 tools/libxc/xc_private.c        |    8 -
 tools/libxc/xc_resume.c         |   21 --
 tools/libxc/xenctrl.h           |   13 +
 tools/libxc/xenguest.h          |   17 ++
 tools/libxc/xg_private.h        |    9 +
 tools/libxc/xg_save_restore.h   |    9 -
 tools/misc/Makefile             |    7 -
 tools/misc/xen-mfndump.c        |  425 +++++++++++++++++++++++++++++++++++++++
 tools/xentrace/xenctx.c         |    9 -
 17 files changed, 679 insertions(+), 273 deletions(-)
 create mode 100644 tools/misc/xen-mfndump.c
Signature · 4 lines
<<This happens because I choose it to happen!>> (Raistlin Majere)
-----------------------------------------------------------------
Dario Faggioli, Ph.D, http://about.me/dario.faggioli
Senior Software Engineer, Citrix Systems R&D Ltd., Cambridge (UK)

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#15 lore raw
In reply to Dario Faggioli
Quoted 18 lines · Dario Faggioli

Hi everyone,

This is take 2 of an old submission of mine, doing some basic libxc cleanups and introducing a debugging tool I called xen-mfndump.

Basically, there are a few places in libxc where we issue do_domctl()-s directly, instead of using the proper xc_xxx() wrapper. This make the code bigger, redundant, more difficult to understand (e.g., "why xc_xxx() is called here and is not called there?"), less consistent and less grep-able. The first 4 patches of this series address this.

The last one introduce a little tool I'm using for debugging. It allows one to see the M2P of the host, the P2M of a domain, some info about the PTEs, and perform basic searches and comparisons on them. I'm finding it very useful and, although I do not claim for my situation to be that common, I figured it could be nice to have it in the tree, so that is patch 6. Patch 5 is the small rework/additions necessary to make 6 possible.

Quoted 2 lines

Patches with an '*' next to their name below have been Reviewed-by Andrew Cooper.

Acked + applied.

Quoted 34 lines

Thanks and Regards,
Dario

---
Dario Faggioli (6):
libxc: introduce xc_domain_get_guest_width()
* libxc: use xc_vcpu_setcontext() instead of calling do_domctl()
libxc: use xc_vcpu_getinfo() instead of calling do_domctl()
* libxc: allow for ctxt to be NULL in xc_vcpu_setcontext
libxc: introduce xc_map_domain_meminfo (and xc_unmap_domain_meminfo) tools/misc: introduce xen-mfndump.

 tools/libxc/xc_core.c           |   20 --
 tools/libxc/xc_cpuid_x86.c      |    8 -
 tools/libxc/xc_dom_boot.c       |   31 +--
 tools/libxc/xc_dom_x86.c        |   13 -
 tools/libxc/xc_domain.c         |  149 +++++++++++++-
 tools/libxc/xc_domain_restore.c |   13 -
 tools/libxc/xc_offline_page.c   |  192 +++---------------
 tools/libxc/xc_pagetab.c        |    8 -
 tools/libxc/xc_private.c        |    8 -
 tools/libxc/xc_resume.c         |   21 --
 tools/libxc/xenctrl.h           |   13 +
 tools/libxc/xenguest.h          |   17 ++
 tools/libxc/xg_private.h        |    9 +
 tools/libxc/xg_save_restore.h   |    9 -
 tools/misc/Makefile             |    7 -
 tools/misc/xen-mfndump.c        |  425 +++++++++++++++++++++++++++++++++++++++
 tools/xentrace/xenctx.c         |    9 -
 17 files changed, 679 insertions(+), 273 deletions(-)
 create mode 100644 tools/misc/xen-mfndump.c