Skip to contentSkip to conversation

xen-mfndump and some libxc cleanups

PATCH Superseded Dario Faggioli 3 participants 13 messages Jul 12, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover xen-mfndump and some libxc cleanups

Hi everyone,

I submitted some of the patches in this series before, as a part of the first RFC of the NUMA memory migration feature. While preparing another round of it, I thought there is enough 'meat' already for a separate series.

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.

Let me know what you think.

Thanks and Regards,
Dario

---

Dario Faggioli (6):
libxc: introduce xc_domain_get_address_size
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           |   21 --
 tools/libxc/xc_cpuid_x86.c      |    8 -
 tools/libxc/xc_dom_boot.c       |   28 +--
 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         |   23 +-
 tools/libxc/xenctrl.h           |   12 +
 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 -
 16 files changed, 677 insertions(+), 261 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 1/6 libxc: introduce xc_domain_get_address_size

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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
---
 tools/libxc/xc_core.c         |   21 ++-------------------
 tools/libxc/xc_cpuid_x86.c    |    8 +++-----
 tools/libxc/xc_domain.c       |   15 +++++++++++++++
 tools/libxc/xc_offline_page.c |    9 ++-------
 tools/libxc/xc_pagetab.c      |    8 +++-----
 tools/libxc/xc_resume.c       |   23 ++++++-----------------
 tools/libxc/xenctrl.h         |   12 ++++++++++++
 tools/libxc/xg_save_restore.h |    9 ++-------
 tools/xentrace/xenctx.c       |    9 +++------
 9 files changed, 48 insertions(+), 66 deletions(-)
tools/libxc/xc_core.c +2 -19
index 4207eed..c8bade5 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,11 +460,12 @@ 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_address_size(xch, domid, &dinfo->guest_width) != 0 )
     {
         PERROR("Could not get address size for domain");
         return sts;
     }
+    dinfo->guest_width /= 8;
 
     xc_core_arch_context_init(&arch_ctxt);
     if ( (dump_mem_start = malloc(DUMP_INCREMENT*PAGE_SIZE)) == NULL )
tools/libxc/xc_cpuid_x86.c +3 -5
index fa47787..99e3997 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_address_size(xch, domid, &guest_width);
+    guest_64bit = (guest_width == 64);
 
     /* Detecting Xen's atitude towards XSAVE */
     memset(&domctl, 0, sizeof(domctl));
tools/libxc/xc_domain.c +15 -0
index 3257e2a..d64d0bc 100644
@@ -270,6 +270,21 @@ out:
     return ret;
 }
 
+int xc_domain_get_address_size(xc_interface *xch, uint32_t domid,
+                               unsigned int *addr_size)
+{
+    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;
+
+    *addr_size = domctl.u.address_size.size;
+    return 0;
+}
 
 int xc_domain_getinfo(xc_interface *xch,
                       uint32_t first_domid,
tools/libxc/xc_offline_page.c +2 -7
index 36b9812..c5547a8 100644
@@ -193,20 +193,15 @@ 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_address_size(xch, domid, gwidth) != 0)
         return -1;
 
-    *gwidth = domctl.u.address_size.size / 8;
+    *gwidth /= 8;
 
     if (strstr(xen_caps, "xen-3.0-x86_64"))
         /* Depends on whether it's a compat 32-on-64 guest */
tools/libxc/xc_pagetab.c +3 -5
index 27c4e9f..5937a52 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_address_size(xch, dom, &gwidth) != 0)
             return 0;
-        if (domctl.u.address_size.size == 64) {
+        if (gwidth == 64) {
             pt_levels = 4;
             paddr = (uint64_t)xen_cr3_to_pfn_x86_64(ctx.x64.ctrlreg[3])
                 << PAGE_SHIFT;
tools/libxc/xc_resume.c +6 -17
index 1c43ec6..58ea395 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,9 +58,9 @@ 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_address_size(xch, domid, &dinfo->guest_width) )
             return -1;
+        dinfo->guest_width /= 8;
     }
 
     if ( (rc = xc_vcpu_getcontext(xch, domid, 0, &ctxt)) != 0 )
@@ -120,7 +107,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 +135,8 @@ 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_address_size(xch, domid, &dinfo->guest_width);
+    dinfo->guest_width /= 8;
     if ( dinfo->guest_width != sizeof(long) )
     {
         ERROR("Cannot resume uncooperative cross-address-size guests");
tools/libxc/xenctrl.h +12 -0
index 388a9c3..907106e 100644
@@ -561,6 +561,18 @@ int xc_vcpu_getaffinity(xc_interface *xch,
                         int vcpu,
                         xc_cpumap_t cpumap);
 
+
+/**
+ * This function will return the address size 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_address_size(xc_interface *xch, uint32_t domid,
+                               unsigned int *addr_size);
+
+
 /**
  * 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 +2 -7
index 6512003..3c11c44 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,14 +312,10 @@ 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_address_size(xch, dom, guest_width) != 0)
         return 0; 
 
-    *guest_width = domctl.u.address_size.size / 8;
+    *guest_width /= 8;
 
     /* 64-bit tools will see the 64-bit hvirt_start, but 32-bit guests 
      * will be using the compat one. */
tools/xentrace/xenctx.c +3 -6
index 060e480..ae4d6a7 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_address_size(xenctx.xc_handle, xenctx.domid, &gw) )
+                ctxt_word_size = guest_word_size = gw / 8;
         }
     }
 #endif
#2 lore raw

Re: [PATCH 1/6] libxc: introduce xc_domain_get_address_size

Quoted 8 lines · Dario Faggioli

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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>

While this is certainly a sensible improvement, there seems to be some confusion in the code.

Quoted 28 lines
---
 tools/libxc/xc_core.c         |   21 ++-------------------
 tools/libxc/xc_cpuid_x86.c    |    8 +++-----
 tools/libxc/xc_domain.c       |   15 +++++++++++++++
 tools/libxc/xc_offline_page.c |    9 ++-------
 tools/libxc/xc_pagetab.c      |    8 +++-----
 tools/libxc/xc_resume.c       |   23 ++++++-----------------
 tools/libxc/xenctrl.h         |   12 ++++++++++++
 tools/libxc/xg_save_restore.h |    9 ++-------
 tools/xentrace/xenctx.c       |    9 +++------
 9 files changed, 48 insertions(+), 66 deletions(-)
tools/libxc/xc_core.c +0 -8
index 4207eed..c8bade5 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));

tools/libxc/xc_core.c elfnote_dump_format_version(xc_interface *xch, · line 437

Quoted 10 lines
tools/libxc/xc_core.c +0 -10
-    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;
-}
-

Here, guest_width is measured in bytes.

Quoted 4 lines
tools/libxc/xc_core.c +0 -0
 int
 xc_domain_dumpcore_via_callback(xc_interface *xch,
                                 uint32_t domid,

tools/libxc/xc_core.c xc_domain_dumpcore_via_callback(xc_interface *xch, · line 468

Quoted 10 lines
tools/libxc/xc_core.c +2 -1
@@ -478,11 +460,12 @@ 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_address_size(xch, domid, &dinfo->guest_width) != 0 )
     {
         PERROR("Could not get address size for domain");
         return sts;
     }
+    dinfo->guest_width /= 8;

This looks nasty (and frankly looks wrong to a cursory glance), and is because of the functional difference between get_guest_width() and xc_domain_get_address_size()

Quoted 15 lines
tools/libxc/xc_core.c +0 -0
     xc_core_arch_context_init(&arch_ctxt);
     if ( (dump_mem_start = malloc(DUMP_INCREMENT*PAGE_SIZE)) == NULL )
tools/libxc/xc_cpuid_x86.c +1 -0
index fa47787..99e3997 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;

tools/libxc/xc_cpuid_x86.c static void xc_cpuid_pv_policy( · line 447

Quoted 10 lines
tools/libxc/xc_cpuid_x86.c +2 -5
 
     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_address_size(xch, domid, &guest_width);
+    guest_64bit = (guest_width == 64);

guest_width here is now measured in bytes.

Also, error checking? I know the old code also failed in that regard.

Quoted 43 lines
tools/libxc/xc_cpuid_x86.c +0 -0
     /* Detecting Xen's atitude towards XSAVE */
     memset(&domctl, 0, sizeof(domctl));
tools/libxc/xc_domain.c +15 -0
index 3257e2a..d64d0bc 100644
@@ -270,6 +270,21 @@ out:
     return ret;
 }
 
+int xc_domain_get_address_size(xc_interface *xch, uint32_t domid,
+                               unsigned int *addr_size)
+{
+    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;
+
+    *addr_size = domctl.u.address_size.size;
+    return 0;
+}
 
 int xc_domain_getinfo(xc_interface *xch,
                       uint32_t first_domid,
tools/libxc/xc_offline_page.c +0 -1
index 36b9812..c5547a8 100644
@@ -193,20 +193,15 @@ 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;
 

tools/libxc/xc_offline_page.c static int get_pt_level(xc_interface *xch, uint32_t domid, · line 204

Quoted 10 lines
tools/libxc/xc_offline_page.c +2 -6
-    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_address_size(xch, domid, gwidth) != 0)
         return -1;
 
-    *gwidth = domctl.u.address_size.size / 8;
+    *gwidth /= 8;

gwidth is now again measured in bytes.

Quoted 13 lines
tools/libxc/xc_offline_page.c +0 -0
     if (strstr(xen_caps, "xen-3.0-x86_64"))
         /* Depends on whether it's a compat 32-on-64 guest */
tools/libxc/xc_pagetab.c +1 -1
index 27c4e9f..5937a52 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;

tools/libxc/xc_pagetab.c unsigned long xc_translate_foreign_address(xc_interface *xch, uint32_t dom, · line 60

Quoted 10 lines
tools/libxc/xc_pagetab.c +2 -4
         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_address_size(xch, dom, &gwidth) != 0)
             return 0;
-        if (domctl.u.address_size.size == 64) {
+        if (gwidth == 64) {

but in bits here. ( I shall ignore pointing out the same further down)

Quoted 121 lines
tools/libxc/xc_pagetab.c +0 -0
             pt_levels = 4;
             paddr = (uint64_t)xen_cr3_to_pfn_x86_64(ctx.x64.ctrlreg[3])
                 << PAGE_SHIFT;
tools/libxc/xc_resume.c +6 -17
index 1c43ec6..58ea395 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,9 +58,9 @@ 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_address_size(xch, domid, &dinfo->guest_width) )
             return -1;
+        dinfo->guest_width /= 8;
     }
 
     if ( (rc = xc_vcpu_getcontext(xch, domid, 0, &ctxt)) != 0 )
@@ -120,7 +107,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 +135,8 @@ 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_address_size(xch, domid, &dinfo->guest_width);
+    dinfo->guest_width /= 8;
     if ( dinfo->guest_width != sizeof(long) )
     {
         ERROR("Cannot resume uncooperative cross-address-size guests");
tools/libxc/xenctrl.h +12 -0
index 388a9c3..907106e 100644
@@ -561,6 +561,18 @@ int xc_vcpu_getaffinity(xc_interface *xch,
                         int vcpu,
                         xc_cpumap_t cpumap);
 
+
+/**
+ * This function will return the address size 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_address_size(xc_interface *xch, uint32_t domid,
+                               unsigned int *addr_size);
+
+
 /**
  * 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 +2 -7
index 6512003..3c11c44 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,14 +312,10 @@ 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_address_size(xch, dom, guest_width) != 0)
         return 0; 
 
-    *guest_width = domctl.u.address_size.size / 8;
+    *guest_width /= 8;
 
     /* 64-bit tools will see the 64-bit hvirt_start, but 32-bit guests 
      * will be using the compat one. */
tools/xentrace/xenctx.c +0 -2
index 060e480..ae4d6a7 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);

tools/xentrace/xenctx.c static void dump_ctx(int vcpu) · line 779

Quoted 10 lines
tools/xentrace/xenctx.c +3 -4
-            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_address_size(xenctx.xc_handle, xenctx.domid, &gw) )
+                ctxt_word_size = guest_word_size = gw / 8;
         }
     }
 #endif

Almost all of the "/= 8"s could be removed by moving it into xc_domain_get_address_size().

I suggest renaming xc_domain_get_address_size() to xc_domain_get_address_width() and changing the one location which checks against 64 (bits) to check against 8 (bytes).

~Andrew

Quoted 6 lines

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

#3 lore raw

Re: [PATCH 1/6] libxc: introduce xc_domain_get_address_size

In reply to Dario Faggioli
Quoted 4 lines · Dario Faggioli

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.

So while you're at it, could you please make use of your function in xc_dom_x86.c:xc_domain_get_native_protocol as well.

Wei.

Quoted 294 lines
Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
---
 tools/libxc/xc_core.c         |   21 ++-------------------
 tools/libxc/xc_cpuid_x86.c    |    8 +++-----
 tools/libxc/xc_domain.c       |   15 +++++++++++++++
 tools/libxc/xc_offline_page.c |    9 ++-------
 tools/libxc/xc_pagetab.c      |    8 +++-----
 tools/libxc/xc_resume.c       |   23 ++++++-----------------
 tools/libxc/xenctrl.h         |   12 ++++++++++++
 tools/libxc/xg_save_restore.h |    9 ++-------
 tools/xentrace/xenctx.c       |    9 +++------
 9 files changed, 48 insertions(+), 66 deletions(-)
tools/libxc/xc_core.c +2 -19
index 4207eed..c8bade5 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,11 +460,12 @@ 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_address_size(xch, domid, &dinfo->guest_width) != 0 )
     {
         PERROR("Could not get address size for domain");
         return sts;
     }
+    dinfo->guest_width /= 8;
 
     xc_core_arch_context_init(&arch_ctxt);
     if ( (dump_mem_start = malloc(DUMP_INCREMENT*PAGE_SIZE)) == NULL )
tools/libxc/xc_cpuid_x86.c +3 -5
index fa47787..99e3997 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_address_size(xch, domid, &guest_width);
+    guest_64bit = (guest_width == 64);
 
     /* Detecting Xen's atitude towards XSAVE */
     memset(&domctl, 0, sizeof(domctl));
tools/libxc/xc_domain.c +15 -0
index 3257e2a..d64d0bc 100644
@@ -270,6 +270,21 @@ out:
     return ret;
 }
 
+int xc_domain_get_address_size(xc_interface *xch, uint32_t domid,
+                               unsigned int *addr_size)
+{
+    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;
+
+    *addr_size = domctl.u.address_size.size;
+    return 0;
+}
 
 int xc_domain_getinfo(xc_interface *xch,
                       uint32_t first_domid,
tools/libxc/xc_offline_page.c +2 -7
index 36b9812..c5547a8 100644
@@ -193,20 +193,15 @@ 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_address_size(xch, domid, gwidth) != 0)
         return -1;
 
-    *gwidth = domctl.u.address_size.size / 8;
+    *gwidth /= 8;
 
     if (strstr(xen_caps, "xen-3.0-x86_64"))
         /* Depends on whether it's a compat 32-on-64 guest */
tools/libxc/xc_pagetab.c +3 -5
index 27c4e9f..5937a52 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_address_size(xch, dom, &gwidth) != 0)
             return 0;
-        if (domctl.u.address_size.size == 64) {
+        if (gwidth == 64) {
             pt_levels = 4;
             paddr = (uint64_t)xen_cr3_to_pfn_x86_64(ctx.x64.ctrlreg[3])
                 << PAGE_SHIFT;
tools/libxc/xc_resume.c +6 -17
index 1c43ec6..58ea395 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,9 +58,9 @@ 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_address_size(xch, domid, &dinfo->guest_width) )
             return -1;
+        dinfo->guest_width /= 8;
     }
 
     if ( (rc = xc_vcpu_getcontext(xch, domid, 0, &ctxt)) != 0 )
@@ -120,7 +107,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 +135,8 @@ 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_address_size(xch, domid, &dinfo->guest_width);
+    dinfo->guest_width /= 8;
     if ( dinfo->guest_width != sizeof(long) )
     {
         ERROR("Cannot resume uncooperative cross-address-size guests");
tools/libxc/xenctrl.h +12 -0
index 388a9c3..907106e 100644
@@ -561,6 +561,18 @@ int xc_vcpu_getaffinity(xc_interface *xch,
                         int vcpu,
                         xc_cpumap_t cpumap);
 
+
+/**
+ * This function will return the address size 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_address_size(xc_interface *xch, uint32_t domid,
+                               unsigned int *addr_size);
+
+
 /**
  * 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 +2 -7
index 6512003..3c11c44 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,14 +312,10 @@ 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_address_size(xch, dom, guest_width) != 0)
         return 0; 
 
-    *guest_width = domctl.u.address_size.size / 8;
+    *guest_width /= 8;
 
     /* 64-bit tools will see the 64-bit hvirt_start, but 32-bit guests 
      * will be using the compat one. */
tools/xentrace/xenctx.c +3 -6
index 060e480..ae4d6a7 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_address_size(xenctx.xc_handle, xenctx.domid, &gw) )
+                ctxt_word_size = guest_word_size = gw / 8;
         }
     }
 #endif

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

#4 lore raw

PATCH 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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
---
 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);
Reviewed-by on v1 2/6
#5 lore raw

Re: [PATCH 2/6] libxc: use xc_vcpu_setcontext() instead of calling do_domctl()

Quoted 8 lines · 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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
Quoted 64 lines
---
 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);

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

#6 lore raw

PATCH 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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
---
 tools/libxc/xc_dom_boot.c       |   14 ++++++--------
 tools/libxc/xc_domain_restore.c |    7 +++----
 tools/libxc/xc_private.c        |    8 +++-----
 3 files changed, 12 insertions(+), 17 deletions(-)
tools/libxc/xc_dom_boot.c +6 -8
index cf509fa..69b5c9c 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,21 @@ 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 != 1 )
     {
         xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
                      "%s: getdomaininfo failed (rc=%d)", __FUNCTION__, rc);
         return rc;
     }
-    if ( domctl.domain != dom->guest_domid )
+    if ( 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);
+                     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,
#7 lore raw

Re: [PATCH 3/6] libxc: use xc_vcpu_getinfo() instead of calling do_domctl()

Quoted 32 lines · 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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
---
 tools/libxc/xc_dom_boot.c       |   14 ++++++--------
 tools/libxc/xc_domain_restore.c |    7 +++----
 tools/libxc/xc_private.c        |    8 +++-----
 3 files changed, 12 insertions(+), 17 deletions(-)
tools/libxc/xc_dom_boot.c +1 -1
index cf509fa..69b5c9c 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,21 @@ int xc_dom_boot_image(struct xc_dom_image *dom)
         return rc;
 
     /* collect some info */

tools/libxc/xc_dom_boot.c int xc_dom_boot_image(struct xc_dom_image *dom) · line 220

Quoted 10 lines
tools/libxc/xc_dom_boot.c +2 -4
-    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 != 1 )
     {
         xc_dom_panic(dom->xch, XC_INTERNAL_ERROR,
                      "%s: getdomaininfo failed (rc=%d)", __FUNCTION__, rc);
         return rc;

With the rc from do_domctl() different to the rc from xc_domain_getinfo(), this error print and return is possibly wrong. An rc of 0 is a now a valid failure case, meaning "I cant find any domains".

~Andrew

Quoted 69 lines
tools/libxc/xc_dom_boot.c +3 -3
     }
-    if ( domctl.domain != dom->guest_domid )
+    if ( 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);
+                     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,

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

#8 lore raw

PATCH 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. Actually, it is the only way to have vcpu_reset() invoked from outside Xen.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Keir Fraser <[email protected]>
---
 tools/libxc/xc_domain.c |    6 ------
 1 file changed, 6 deletions(-)
tools/libxc/xc_domain.c +0 -6
index d64d0bc..a247426 100644
@@ -1140,12 +1140,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;
Reviewed-by on v1 4/6
#9 lore raw

Re: [PATCH 4/6] libxc: allow for ctxt to be NULL in xc_vcpu_setcontext

Quoted 10 lines · Dario Faggioli

Since, as can be seen in xen/common/domctl.c, that is legitimate. Actually, it is the only way to have vcpu_reset() invoked from outside Xen.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Keir Fraser <[email protected]>

"it is the only way to have vcpu_reset() invoked from outside Xen." without manually constructing a vcpu_op.

Still,

Quoted 27 lines
---
 tools/libxc/xc_domain.c |    6 ------
 1 file changed, 6 deletions(-)
tools/libxc/xc_domain.c +0 -6
index d64d0bc..a247426 100644
@@ -1140,12 +1140,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;
 

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

#10 lore raw

PATCH 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.

Cc Ian Jackson <[email protected]>
Cc Ian Campbell <[email protected]>
Cc George Dunlap <[email protected]>
---
 tools/libxc/xc_domain.c       |  128 ++++++++++++++++++++++++++++
 tools/libxc/xc_offline_page.c |  187 +++++++----------------------------------
 tools/libxc/xenguest.h        |   17 ++++
 tools/libxc/xg_private.h      |    9 ++
 4 files changed, 184 insertions(+), 157 deletions(-)
tools/libxc/xc_domain.c +128 -0
index a247426..c321e73 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>
@@ -1476,6 +1478,132 @@ 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_address_size(xch, domid, &minfo->guest_width) )
+    {
+        PERROR("Could not get domain address size");
+        return -1;
+    }
+    minfo->guest_width /= 8;
+    _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 -157

260 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))
#11 lore raw

PATCH 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>
Signed-off-by: Dario Faggioli <[email protected]>
Cc: Ian Jackson <[email protected]>
Cc: Ian Campbell <[email protected]>
Cc: George Dunlap <[email protected]>
Cc: Tim Deegan <[email protected]>
Cc: Jan Beulich <[email protected]>
Cc: Wei Liu <[email protected]>
---
 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 520ef80..59e8d36 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)
 
@@ -24,7 +24,7 @@ INSTALL_BIN := $(INSTALL_BIN-y)
 
 INSTALL_SBIN-y := xm xen-bugtool xen-python-path xend xenperf xsview 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)
 
@@ -77,6 +77,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

#12 lore raw
In reply to Dario Faggioli

[Cc-ing people for this e-mail too, as I apparently failed to do so with `stg mail']

Quoted 54 lines · Dario Faggioli

Hi everyone,

I submitted some of the patches in this series before, as a part of the first RFC of the NUMA memory migration feature. While preparing another round of it, I thought there is enough 'meat' already for a separate series.

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.

Let me know what you think.

Thanks and Regards,
Dario

---

Dario Faggioli (6):
libxc: introduce xc_domain_get_address_size
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           |   21 --
 tools/libxc/xc_cpuid_x86.c      |    8 -
 tools/libxc/xc_dom_boot.c       |   28 +--
 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         |   23 +-
 tools/libxc/xenctrl.h           |   12 +
 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 -
 16 files changed, 677 insertions(+), 261 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