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

[Xen-devel] [PATCH] libxc: use vcpu_guest_context_either_t instead of vcpu_guest_context_t



Hi,

There is some problem on libxc when we try to get the vcpu context.

This is happen with a 64 bits xen and a 32 bits dom0 when we try to get the vcpu context of a 64 bits guest. libxc uses the structure vcpu_guest_context_t which has been compiled in 32 bits, but in the hypervisor use the same structure compiled in 64 bits.
  - vcpu_guest_context_t in libxc : 2800
  - vcpu_guest_context_t in xen : 5168

We do a mlock of a right size (sizeof(vcpu_guest_context_either_t)) before doing the domctl so in the hypervisor when we copy the 64 bits guest context there is an overflow inside the dom0 memory.

I know that this patch is a little bit intrusive because that changes the libxc interface. May be there is smarter solution? I am waiting for your suggestion.

libxc: The following patch replace the libxc interface to use vcpu_guest_context_either_t (which is both 32 and 64 bits) instead of vcpu_guest_context_t.

Signed-off-by: Jean Guyader <jean.guyader@xxxxxxxxxxxxx>

--
Jean Guyader
diff -r 3da148fb7d9b tools/libxc/xc_core.c
--- a/tools/libxc/xc_core.c     Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_core.c     Thu Jun 19 14:59:57 2008 +0100
@@ -407,7 +407,7 @@ xc_domain_dumpcore_via_callback(int xc_h
 
     int nr_vcpus = 0;
     char *dump_mem, *dump_mem_start = NULL;
-    vcpu_guest_context_t  ctxt[MAX_VIRT_CPUS];
+    vcpu_guest_context_either_t  ctxt[MAX_VIRT_CPUS];
     struct xc_core_arch_context arch_ctxt;
     char dummy[PAGE_SIZE];
     int dummy_len;
diff -r 3da148fb7d9b tools/libxc/xc_domain.c
--- a/tools/libxc/xc_domain.c   Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_domain.c   Thu Jun 19 14:59:57 2008 +0100
@@ -298,7 +298,7 @@ int xc_vcpu_getcontext(int xc_handle,
 int xc_vcpu_getcontext(int xc_handle,
                        uint32_t domid,
                        uint32_t vcpu,
-                       vcpu_guest_context_t *ctxt)
+                       vcpu_guest_context_either_t *ctxt)
 {
     int rc;
     DECLARE_DOMCTL;
@@ -307,7 +307,7 @@ int xc_vcpu_getcontext(int xc_handle,
     domctl.cmd = XEN_DOMCTL_getvcpucontext;
     domctl.domain = (domid_t)domid;
     domctl.u.vcpucontext.vcpu   = (uint16_t)vcpu;
-    set_xen_guest_handle(domctl.u.vcpucontext.ctxt, ctxt);
+    set_xen_guest_handle(domctl.u.vcpucontext.ctxt, &ctxt->c);
 
     /*
      * We may be asked to lock either a 32-bit or a 64-bit context. Lock the
@@ -626,7 +626,7 @@ int xc_vcpu_setcontext(int xc_handle,
 int xc_vcpu_setcontext(int xc_handle,
                        uint32_t domid,
                        uint32_t vcpu,
-                       vcpu_guest_context_t *ctxt)
+                       vcpu_guest_context_either_t *ctxt)
 {
     DECLARE_DOMCTL;
     int rc;
@@ -635,7 +635,7 @@ int xc_vcpu_setcontext(int xc_handle,
     domctl.cmd = XEN_DOMCTL_setvcpucontext;
     domctl.domain = domid;
     domctl.u.vcpucontext.vcpu = vcpu;
-    set_xen_guest_handle(domctl.u.vcpucontext.ctxt, ctxt);
+    set_xen_guest_handle(domctl.u.vcpucontext.ctxt, &ctxt->c);
 
     /*
      * We may be asked to lock either a 32-bit or a 64-bit context. Lock the
diff -r 3da148fb7d9b tools/libxc/xc_domain_save.c
--- a/tools/libxc/xc_domain_save.c      Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_domain_save.c      Thu Jun 19 14:59:57 2008 +0100
@@ -735,7 +735,7 @@ static xen_pfn_t *map_and_save_p2m_table
         p2m_frame_list[i/FPP] = mfn_to_pfn(p2m_frame_list[i/FPP]);
     }
 
-    if ( xc_vcpu_getcontext(xc_handle, dom, 0, &ctxt.c) )
+    if ( xc_vcpu_getcontext(xc_handle, dom, 0, &ctxt) )
     {
         ERROR("Could not get vcpu context");
         goto out;
@@ -1536,7 +1536,7 @@ int xc_domain_save(int xc_handle, int io
         }
     }
 
-    if ( xc_vcpu_getcontext(xc_handle, dom, 0, &ctxt.c) )
+    if ( xc_vcpu_getcontext(xc_handle, dom, 0, &ctxt) )
     {
         ERROR("Could not get vcpu context");
         goto out;
@@ -1556,7 +1556,7 @@ int xc_domain_save(int xc_handle, int io
         if ( !(vcpumap & (1ULL << i)) )
             continue;
 
-        if ( (i != 0) && xc_vcpu_getcontext(xc_handle, dom, i, &ctxt.c) )
+        if ( (i != 0) && xc_vcpu_getcontext(xc_handle, dom, i, &ctxt) )
         {
             ERROR("No context for VCPU%d", i);
             goto out;
diff -r 3da148fb7d9b tools/libxc/xc_pagetab.c
--- a/tools/libxc/xc_pagetab.c  Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_pagetab.c  Thu Jun 19 14:59:57 2008 +0100
@@ -48,7 +48,7 @@ unsigned long xc_translate_foreign_addre
 unsigned long xc_translate_foreign_address(int xc_handle, uint32_t dom,
                                            int vcpu, unsigned long long virt )
 {
-    vcpu_guest_context_t ctx;
+    vcpu_guest_context_either_t ctx;
     unsigned long long cr3;
     void *pd, *pt, *pdppage = NULL, *pdp, *pml = NULL;
     unsigned long long pde, pte, pdpe, pmle;
@@ -78,7 +78,7 @@ unsigned long xc_translate_foreign_addre
         DPRINTF("failed to retreive vcpu context\n");
         goto out;
     }
-    cr3 = ((unsigned long long)xen_cr3_to_pfn(ctx.ctrlreg[3])) << PAGE_SHIFT;
+    cr3 = ((unsigned long long)xen_cr3_to_pfn(ctx.c.ctrlreg[3])) << PAGE_SHIFT;
 
     /* Page Map Level 4 */
 
diff -r 3da148fb7d9b tools/libxc/xc_private.h
--- a/tools/libxc/xc_private.h  Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_private.h  Thu Jun 19 14:59:57 2008 +0100
@@ -188,9 +188,9 @@ int xc_map_foreign_ranges(int xc_handle,
                           privcmd_mmap_entry_t *entries, int nr);
 
 void *map_domain_va_core(unsigned long domfd, int cpu, void *guest_va,
-                         vcpu_guest_context_t *ctxt);
+                         vcpu_guest_context_either_t *ctxt);
 int xc_waitdomain_core(int xc_handle, int domain, int *status,
-    int options, vcpu_guest_context_t *ctxt);
+    int options, vcpu_guest_context_either_t *ctxt);
 
 void bitmap_64_to_byte(uint8_t *bp, const uint64_t *lp, int nbits);
 void bitmap_byte_to_64(uint64_t *lp, const uint8_t *bp, int nbits);
diff -r 3da148fb7d9b tools/libxc/xc_ptrace.c
--- a/tools/libxc/xc_ptrace.c   Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_ptrace.c   Thu Jun 19 14:59:57 2008 +0100
@@ -40,9 +40,9 @@ static int current_isfile;
 static int current_isfile;
 static int current_is_hvm;
 
-static uint64_t                 online_cpumap;
-static uint64_t                 regs_valid;
-static vcpu_guest_context_t     ctxt[MAX_VIRT_CPUS];
+static uint64_t                         online_cpumap;
+static uint64_t                         regs_valid;
+static vcpu_guest_context_either_t      ctxt[MAX_VIRT_CPUS];
 
 extern int ffsll(long long int);
 #define FOREACH_CPU(cpumap, i)  for ( cpumap = online_cpumap; (i = 
ffsll(cpumap)); cpumap &= ~(1 << (index - 1)) )
@@ -96,9 +96,9 @@ xc_register_event_handler(thr_ev_handler
 }
 
 static inline int
-paging_enabled(vcpu_guest_context_t *v)
+paging_enabled(vcpu_guest_context_either_t *v)
 {
-    unsigned long cr0 = v->ctrlreg[0];
+    unsigned long cr0 = v->c.ctrlreg[0];
     return (cr0 & X86_CR0_PE) && (cr0 & X86_CR0_PG);
 }
 
@@ -174,7 +174,7 @@ map_domain_va_32(
 
     l2 = xc_map_foreign_range(
          xc_handle, current_domid, PAGE_SIZE, PROT_READ,
-         xen_cr3_to_pfn(ctxt[cpu].ctrlreg[3]));
+         xen_cr3_to_pfn(ctxt[cpu].c.ctrlreg[3]));
     if ( l2 == NULL )
         return NULL;
 
@@ -216,7 +216,7 @@ map_domain_va_pae(
 
     l3 = xc_map_foreign_range(
         xc_handle, current_domid, PAGE_SIZE, PROT_READ,
-        xen_cr3_to_pfn(ctxt[cpu].ctrlreg[3]));
+        xen_cr3_to_pfn(ctxt[cpu].c.ctrlreg[3]));
     if ( l3 == NULL )
         return NULL;
 
@@ -494,26 +494,26 @@ xc_ptrace(
     case PTRACE_GETREGS:
         if (!current_isfile && fetch_regs(xc_handle, cpu, NULL))
             goto out_error;
-        SET_PT_REGS(pt, ctxt[cpu].user_regs);
+        SET_PT_REGS(pt, ctxt[cpu].c.user_regs);
         memcpy(data, &pt, sizeof(struct gdb_regs));
         break;
 
     case PTRACE_GETFPREGS:
         if (!current_isfile && fetch_regs(xc_handle, cpu, NULL)) 
                 goto out_error;
-        memcpy(data, &ctxt[cpu].fpu_ctxt, sizeof (elf_fpregset_t));
+        memcpy(data, &ctxt[cpu].c.fpu_ctxt, sizeof (elf_fpregset_t));
         break;
 
     case PTRACE_GETFPXREGS:
         if (!current_isfile && fetch_regs(xc_handle, cpu, NULL))
                 goto out_error;
-        memcpy(data, &ctxt[cpu].fpu_ctxt, sizeof(ctxt[cpu].fpu_ctxt));
+        memcpy(data, &ctxt[cpu].c.fpu_ctxt, sizeof(ctxt[cpu].c.fpu_ctxt));
         break;
 
     case PTRACE_SETREGS:
         if (current_isfile)
                 goto out_unsupported; /* XXX not yet supported */
-        SET_XC_REGS(((struct gdb_regs *)data), ctxt[cpu].user_regs);
+        SET_XC_REGS(((struct gdb_regs *)data), ctxt[cpu].c.user_regs);
         if ((retval = xc_vcpu_setcontext(xc_handle, current_domid, cpu,
                                 &ctxt[cpu])))
             goto out_error_domctl;
@@ -525,7 +525,7 @@ xc_ptrace(
         /*  XXX we can still have problems if the user switches threads
          *  during single-stepping - but that just seems retarded
          */
-        ctxt[cpu].user_regs.eflags |= PSL_T;
+        ctxt[cpu].c.user_regs.eflags |= PSL_T;
         if ((retval = xc_vcpu_setcontext(xc_handle, current_domid, cpu,
                                 &ctxt[cpu])))
             goto out_error_domctl;
@@ -542,9 +542,9 @@ xc_ptrace(
                 if (fetch_regs(xc_handle, cpu, NULL))
                     goto out_error;
                 /* Clear trace flag */
-                if ( ctxt[cpu].user_regs.eflags & PSL_T )
+                if ( ctxt[cpu].c.user_regs.eflags & PSL_T )
                 {
-                    ctxt[cpu].user_regs.eflags &= ~PSL_T;
+                    ctxt[cpu].c.user_regs.eflags &= ~PSL_T;
                     if ((retval = xc_vcpu_setcontext(xc_handle, current_domid,
                                                 cpu, &ctxt[cpu])))
                         goto out_error_domctl;
diff -r 3da148fb7d9b tools/libxc/xc_ptrace_core.c
--- a/tools/libxc/xc_ptrace_core.c      Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_ptrace_core.c      Thu Jun 19 14:59:57 2008 +0100
@@ -641,24 +641,24 @@ static const struct xc_core_format_type*
 
 void *
 map_domain_va_core(unsigned long domfd, int cpu, void *guest_va,
-                   vcpu_guest_context_t *ctxt)
+                   vcpu_guest_context_either_t *ctxt)
 {
     if (current_format_type == NULL)
         return NULL;
     return (current_format_type->map_domain_va_core)(domfd, cpu, guest_va,
-                                                     ctxt);
+                                                     &ctxt->c);
 }
 
 int
 xc_waitdomain_core(int xc_handle, int domfd, int *status, int options,
-                   vcpu_guest_context_t *ctxt)
+                   vcpu_guest_context_either_t *ctxt)
 {
     int ret;
     int i;
 
     for (i = 0; i < NR_FORMAT_TYPE; i++) {
         ret = (format_type[i].waitdomain_core)(xc_handle, domfd, status,
-                                               options, ctxt);
+                                               options, &ctxt->c);
         if (ret == 0) {
             current_format_type = &format_type[i];
             break;
diff -r 3da148fb7d9b tools/libxc/xc_resume.c
--- a/tools/libxc/xc_resume.c   Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xc_resume.c   Thu Jun 19 14:59:57 2008 +0100
@@ -39,7 +39,7 @@ static int modify_returncode(int xc_hand
         return -1;
     }
 
-    if ( (rc = xc_vcpu_getcontext(xc_handle, domid, 0, &ctxt.c)) != 0 )
+    if ( (rc = xc_vcpu_getcontext(xc_handle, domid, 0, &ctxt)) != 0 )
         return rc;
 
     if ( !info.hvm )
@@ -49,7 +49,7 @@ static int modify_returncode(int xc_hand
     else
         ctxt.x32.user_regs.eax = 1;
 
-    if ( (rc = xc_vcpu_setcontext(xc_handle, domid, 0, &ctxt.c)) != 0 )
+    if ( (rc = xc_vcpu_setcontext(xc_handle, domid, 0, &ctxt)) != 0 )
         return rc;
 
     return 0;
@@ -89,7 +89,7 @@ static int xc_domain_resume_any(int xc_h
     int i, rc = -1;
 #if defined(__i386__) || defined(__x86_64__)
     unsigned long mfn, p2m_size = 0;
-    vcpu_guest_context_t ctxt;
+    vcpu_guest_context_either_t ctxt;
     start_info_t *start_info;
     shared_info_t *shinfo = NULL;
     xen_pfn_t *p2m_frame_list_list = NULL;
@@ -167,7 +167,7 @@ static int xc_domain_resume_any(int xc_h
         goto out;
     }
 
-    mfn = ctxt.user_regs.edx;
+    mfn = ctxt.c.user_regs.edx;
 
     start_info = xc_map_foreign_range(xc_handle, domid, PAGE_SIZE,
                                       PROT_READ | PROT_WRITE, mfn);
diff -r 3da148fb7d9b tools/libxc/xenctrl.h
--- a/tools/libxc/xenctrl.h     Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xenctrl.h     Thu Jun 19 14:59:57 2008 +0100
@@ -30,6 +30,11 @@
 #include <xen/xsm/acm.h>
 #include <xen/xsm/acm_ops.h>
 #include <xen/xsm/flask_op.h>
+
+#if defined(__i386__) || defined(__x86_64__)
+#include <xen/foreign/x86_32.h>
+#include <xen/foreign/x86_64.h>
+#endif
 
 #ifdef __ia64__
 #define XC_PAGE_SHIFT           14
@@ -162,6 +167,35 @@ typedef struct xc_dominfo {
 } xc_dominfo_t;
 
 typedef xen_domctl_getdomaininfo_t xc_domaininfo_t;
+
+typedef union 
+{
+#if defined(__i386__) || defined(__x86_64__)
+    vcpu_guest_context_x86_64_t x64;
+    vcpu_guest_context_x86_32_t x32;   
+#endif
+    vcpu_guest_context_t c;
+} vcpu_guest_context_either_t;
+
+typedef union
+{
+#if defined(__i386__) || defined(__x86_64__)
+    shared_info_x86_64_t x64;
+    shared_info_x86_32_t x32;
+#endif
+    shared_info_t s;
+} shared_info_either_t;
+
+typedef union
+{
+#if defined(__i386__) || defined(__x86_64__)
+    start_info_x86_64_t x64;
+    start_info_x86_32_t x32;
+#endif
+    start_info_t s;
+} start_info_either_t;
+
+
 int xc_domain_create(int xc_handle,
                      uint32_t ssidref,
                      xen_domain_handle_t handle,
@@ -307,7 +341,7 @@ int xc_vcpu_setcontext(int xc_handle,
 int xc_vcpu_setcontext(int xc_handle,
                        uint32_t domid,
                        uint32_t vcpu,
-                       vcpu_guest_context_t *ctxt);
+                       vcpu_guest_context_either_t *ctxt);
 /**
  * This function will return information about one or more domains, using a
  * single hypercall.  The domain information will be stored into the supplied
@@ -368,7 +402,7 @@ int xc_vcpu_getcontext(int xc_handle,
 int xc_vcpu_getcontext(int xc_handle,
                        uint32_t domid,
                        uint32_t vcpu,
-                       vcpu_guest_context_t *ctxt);
+                       vcpu_guest_context_either_t *ctxt);
 
 typedef xen_domctl_getvcpuinfo_t xc_vcpuinfo_t;
 int xc_vcpu_getinfo(int xc_handle,
diff -r 3da148fb7d9b tools/libxc/xg_save_restore.h
--- a/tools/libxc/xg_save_restore.h     Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/libxc/xg_save_restore.h     Thu Jun 19 14:59:57 2008 +0100
@@ -112,28 +112,6 @@ static inline int get_platform_info(int 
 #define is_mapped(pfn_type) (!((pfn_type) & 0x80000000UL))
 
 
-/* 32-on-64 support: saving 32bit guests from 64bit tools and vice versa */
-typedef union 
-{
-    vcpu_guest_context_x86_64_t x64;
-    vcpu_guest_context_x86_32_t x32;   
-    vcpu_guest_context_t c;
-} vcpu_guest_context_either_t;
-
-typedef union 
-{
-    shared_info_x86_64_t x64;
-    shared_info_x86_32_t x32;   
-    shared_info_t s;
-} shared_info_either_t;
-
-typedef union 
-{
-    start_info_x86_64_t x64;
-    start_info_x86_32_t x32;   
-    start_info_t s;
-} start_info_either_t;
-
 #define GET_FIELD(_p, _f) ((guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))
 
 #define SET_FIELD(_p, _f, _v) do {              \
diff -r 3da148fb7d9b tools/xentrace/xenctx.c
--- a/tools/xentrace/xenctx.c   Thu Jun 19 11:09:10 2008 +0100
+++ b/tools/xentrace/xenctx.c   Thu Jun 19 14:59:57 2008 +0100
@@ -22,6 +22,8 @@
 #include <string.h>
 #include <inttypes.h>
 #include <getopt.h>
+#include <xen/foreign/x86_64.h>
+#include <xen/foreign/x86_32.h>
 
 #include "xenctrl.h"
 
@@ -702,7 +704,7 @@ void dump_ctx(int vcpu)
 void dump_ctx(int vcpu)
 {
     int ret;
-    vcpu_guest_context_t ctx;
+    vcpu_guest_context_either_t ctx;
     xc_dominfo_t dominfo;
 
     xc_handle = xc_interface_open(); /* for accessing control interface */
@@ -727,10 +729,10 @@ void dump_ctx(int vcpu)
         exit(-1);
     }
 
-    print_ctx(&ctx);
+    print_ctx(&ctx.c);
 #ifndef NO_TRANSLATION
-    if (is_kernel_text(INSTR_POINTER((&ctx.user_regs))))
-        print_stack(&ctx, vcpu);
+    if (is_kernel_text(INSTR_POINTER((&ctx.c.user_regs))))
+        print_stack(&ctx.c, vcpu);
 #endif
 
     if (!dominfo.paused) {
_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel

 


Rackspace

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