ia64/xen-unstable

changeset 19047:ecf603780f56

libxc: Support cross-bitness guest when core-dumping

This patch allows core-dumping to work on a cross-bit host/guest
configuration, whereas previously that was not supported. It supports
both PV and FV guests. The core file format generated by the host,
needs to match that of the guest, so an alignment issue is addressed,
along with the p2m frame list handling being done according to the
guest size.

Signed-off-by: Bruce Rogers <brogers@novell.com>
author Keir Fraser <keir.fraser@citrix.com>
date Thu Jan 15 12:37:42 2009 +0000 (2009-01-15)
parents f6a455c9f01d
children e98032a016d6
files tools/libxc/xc_core.c tools/libxc/xc_core.h tools/libxc/xc_core_ia64.c tools/libxc/xc_core_x86.c tools/libxc/xc_core_x86.h
line diff
     1.1 --- a/tools/libxc/xc_core.c	Thu Jan 15 12:36:29 2009 +0000
     1.2 +++ b/tools/libxc/xc_core.c	Thu Jan 15 12:37:42 2009 +0000
     1.3 @@ -58,9 +58,6 @@
     1.4  /* number of pages to write at a time */
     1.5  #define DUMP_INCREMENT (4 * 1024)
     1.6  
     1.7 -/* Don't yet support cross-address-size core dump */
     1.8 -#define guest_width (sizeof (unsigned long))
     1.9 -
    1.10  /* string table */
    1.11  struct xc_core_strtab {
    1.12      char       *strings;
    1.13 @@ -240,7 +237,7 @@ xc_core_ehdr_init(Elf64_Ehdr *ehdr)
    1.14      ehdr->e_ident[EI_ABIVERSION] = EV_CURRENT;
    1.15  
    1.16      ehdr->e_type = ET_CORE;
    1.17 -    ehdr->e_machine = ELF_ARCH_MACHINE;
    1.18 +    /* e_machine will be filled in later */
    1.19      ehdr->e_version = EV_CURRENT;
    1.20      ehdr->e_entry = 0;
    1.21      ehdr->e_phoff = 0;
    1.22 @@ -359,7 +356,8 @@ elfnote_dump_core_header(
    1.23  }
    1.24  
    1.25  static int
    1.26 -elfnote_dump_xen_version(void *args, dumpcore_rtn_t dump_rtn, int xc_handle)
    1.27 +elfnote_dump_xen_version(void *args, dumpcore_rtn_t dump_rtn, int xc_handle,
    1.28 +                         unsigned int guest_width)
    1.29  {
    1.30      int sts;
    1.31      struct elfnote elfnote;
    1.32 @@ -371,6 +369,12 @@ elfnote_dump_xen_version(void *args, dum
    1.33      elfnote.descsz = sizeof(xen_version);
    1.34      elfnote.type = XEN_ELFNOTE_DUMPCORE_XEN_VERSION;
    1.35      elfnote_fill_xen_version(xc_handle, &xen_version);
    1.36 +    if (guest_width < sizeof(unsigned long))
    1.37 +    {
    1.38 +        // 32 bit elf file format differs in pagesize's alignment
    1.39 +        char *p = (char *)&xen_version.pagesize;
    1.40 +        memmove(p - 4, p, sizeof(xen_version.pagesize));
    1.41 +    }
    1.42      sts = dump_rtn(args, (char*)&elfnote, sizeof(elfnote));
    1.43      if ( sts != 0 )
    1.44          return sts;
    1.45 @@ -396,6 +400,24 @@ elfnote_dump_format_version(void *args, 
    1.46      return dump_rtn(args, (char*)&format_version, sizeof(format_version));
    1.47  }
    1.48  
    1.49 +static int
    1.50 +get_guest_width(int xc_handle,
    1.51 +                uint32_t domid,
    1.52 +                unsigned int *guest_width)
    1.53 +{
    1.54 +    DECLARE_DOMCTL;
    1.55 +
    1.56 +    memset(&domctl, 0, sizeof(domctl));
    1.57 +    domctl.domain = domid;
    1.58 +    domctl.cmd = XEN_DOMCTL_get_address_size;
    1.59 +
    1.60 +    if ( do_domctl(xc_handle, &domctl) != 0 )
    1.61 +        return 1;
    1.62 +        
    1.63 +    *guest_width = domctl.u.address_size.size / 8;
    1.64 +    return 0;
    1.65 +}
    1.66 +
    1.67  int
    1.68  xc_domain_dumpcore_via_callback(int xc_handle,
    1.69                                  uint32_t domid,
    1.70 @@ -403,7 +425,8 @@ xc_domain_dumpcore_via_callback(int xc_h
    1.71                                  dumpcore_rtn_t dump_rtn)
    1.72  {
    1.73      xc_dominfo_t info;
    1.74 -    shared_info_t *live_shinfo = NULL;
    1.75 +    shared_info_any_t *live_shinfo = NULL;
    1.76 +    unsigned int guest_width; 
    1.77  
    1.78      int nr_vcpus = 0;
    1.79      char *dump_mem, *dump_mem_start = NULL;
    1.80 @@ -437,6 +460,12 @@ xc_domain_dumpcore_via_callback(int xc_h
    1.81      uint16_t strtab_idx;
    1.82      struct xc_core_section_headers *sheaders = NULL;
    1.83      Elf64_Shdr *shdr;
    1.84 + 
    1.85 +    if ( get_guest_width(xc_handle, domid, &guest_width) != 0 )
    1.86 +    {
    1.87 +        PERROR("Could not get address size for domain");
    1.88 +        return sts;
    1.89 +    }
    1.90  
    1.91      xc_core_arch_context_init(&arch_ctxt);
    1.92      if ( (dump_mem_start = malloc(DUMP_INCREMENT*PAGE_SIZE)) == NULL )
    1.93 @@ -500,7 +529,7 @@ xc_domain_dumpcore_via_callback(int xc_h
    1.94              goto out;
    1.95          }
    1.96  
    1.97 -        sts = xc_core_arch_map_p2m(xc_handle, &info, live_shinfo,
    1.98 +        sts = xc_core_arch_map_p2m(xc_handle, guest_width, &info, live_shinfo,
    1.99                                     &p2m, &p2m_size);
   1.100          if ( sts != 0 )
   1.101              goto out;
   1.102 @@ -676,6 +705,7 @@ xc_domain_dumpcore_via_callback(int xc_h
   1.103      /* write out elf header */
   1.104      ehdr.e_shnum = sheaders->num;
   1.105      ehdr.e_shstrndx = strtab_idx;
   1.106 +    ehdr.e_machine = ELF_ARCH_MACHINE;
   1.107      sts = dump_rtn(args, (char*)&ehdr, sizeof(ehdr));
   1.108      if ( sts != 0 )
   1.109          goto out;
   1.110 @@ -697,7 +727,7 @@ xc_domain_dumpcore_via_callback(int xc_h
   1.111          goto out;
   1.112  
   1.113      /* elf note section: xen version */
   1.114 -    sts = elfnote_dump_xen_version(args, dump_rtn, xc_handle);
   1.115 +    sts = elfnote_dump_xen_version(args, dump_rtn, xc_handle, guest_width);
   1.116      if ( sts != 0 )
   1.117          goto out;
   1.118  
   1.119 @@ -757,9 +787,21 @@ xc_domain_dumpcore_via_callback(int xc_h
   1.120  
   1.121              if ( !auto_translated_physmap )
   1.122              {
   1.123 -                gmfn = p2m[i];
   1.124 -                if ( gmfn == INVALID_P2M_ENTRY )
   1.125 -                    continue;
   1.126 +                if ( guest_width >= sizeof(unsigned long) )
   1.127 +                {
   1.128 +                    if ( guest_width == sizeof(unsigned long) )
   1.129 +                        gmfn = p2m[i];
   1.130 +                    else
   1.131 +                        gmfn = ((uint64_t *)p2m)[i];
   1.132 +                    if ( gmfn == INVALID_P2M_ENTRY )
   1.133 +                        continue;
   1.134 +                }
   1.135 +                else
   1.136 +                {
   1.137 +                    gmfn = ((uint32_t *)p2m)[i];
   1.138 +                    if ( gmfn == (uint32_t)INVALID_P2M_ENTRY )
   1.139 +                       continue;
   1.140 +                }
   1.141  
   1.142                  p2m_array[j].pfn = i;
   1.143                  p2m_array[j].gmfn = gmfn;
   1.144 @@ -802,7 +844,7 @@ copy_done:
   1.145          /* When live dump-mode (-L option) is specified,
   1.146           * guest domain may reduce memory. pad with zero pages.
   1.147           */
   1.148 -        IPRINTF("j (%ld) != nr_pages (%ld)", j , nr_pages);
   1.149 +        IPRINTF("j (%ld) != nr_pages (%ld)", j, nr_pages);
   1.150          memset(dump_mem_start, 0, PAGE_SIZE);
   1.151          for (; j < nr_pages; j++) {
   1.152              sts = dump_rtn(args, dump_mem_start, PAGE_SIZE);
   1.153 @@ -891,7 +933,7 @@ xc_domain_dumpcore(int xc_handle,
   1.154      struct dump_args da;
   1.155      int sts;
   1.156  
   1.157 -    if ( (da.fd = open(corename, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR)) < 0 )
   1.158 +    if ( (da.fd = open(corename, O_CREAT|O_RDWR|O_TRUNC, S_IWUSR|S_IRUSR)) < 0 )
   1.159      {
   1.160          PERROR("Could not open corefile %s", corename);
   1.161          return -errno;
     2.1 --- a/tools/libxc/xc_core.h	Thu Jan 15 12:36:29 2009 +0000
     2.2 +++ b/tools/libxc/xc_core.h	Thu Jan 15 12:37:42 2009 +0000
     2.3 @@ -136,12 +136,12 @@ int xc_core_arch_auto_translated_physmap
     2.4  struct xc_core_arch_context;
     2.5  int xc_core_arch_memory_map_get(int xc_handle,
     2.6                                  struct xc_core_arch_context *arch_ctxt,
     2.7 -                                xc_dominfo_t *info, shared_info_t *live_shinfo,
     2.8 +                                xc_dominfo_t *info, shared_info_any_t *live_shinfo,
     2.9                                  xc_core_memory_map_t **mapp,
    2.10                                  unsigned int *nr_entries);
    2.11 -int xc_core_arch_map_p2m(int xc_handle, xc_dominfo_t *info,
    2.12 -                         shared_info_t *live_shinfo, xen_pfn_t **live_p2m,
    2.13 -                         unsigned long *pfnp);
    2.14 +int xc_core_arch_map_p2m(int xc_handle, unsigned int guest_width,
    2.15 +                         xc_dominfo_t *info, shared_info_any_t *live_shinfo,
    2.16 +                         xen_pfn_t **live_p2m, unsigned long *pfnp);
    2.17  
    2.18  
    2.19  #if defined (__i386__) || defined (__x86_64__)
     3.1 --- a/tools/libxc/xc_core_ia64.c	Thu Jan 15 12:36:29 2009 +0000
     3.2 +++ b/tools/libxc/xc_core_ia64.c	Thu Jan 15 12:37:42 2009 +0000
     3.3 @@ -235,7 +235,7 @@ old:
     3.4  }
     3.5  
     3.6  int
     3.7 -xc_core_arch_map_p2m(int xc_handle, xc_dominfo_t *info,
     3.8 +xc_core_arch_map_p2m(int xc_handle, unsigned int guest_width, xc_dominfo_t *info,
     3.9                       shared_info_t *live_shinfo, xen_pfn_t **live_p2m,
    3.10                       unsigned long *pfnp)
    3.11  {
     4.1 --- a/tools/libxc/xc_core_x86.c	Thu Jan 15 12:36:29 2009 +0000
     4.2 +++ b/tools/libxc/xc_core_x86.c	Thu Jan 15 12:37:42 2009 +0000
     4.3 @@ -20,9 +20,25 @@
     4.4  
     4.5  #include "xg_private.h"
     4.6  #include "xc_core.h"
     4.7 +#include "xc_e820.h"
     4.8  
     4.9 -/* Don't yet support cross-address-size core dump */
    4.10 -#define guest_width (sizeof (unsigned long))
    4.11 +#define GET_FIELD(_p, _f) ((guest_width==8) ? ((_p)->x64._f) : ((_p)->x32._f))
    4.12 +
    4.13 +#ifndef MAX
    4.14 +#define MAX(_a, _b) ((_a) >= (_b) ? (_a) : (_b))
    4.15 +#endif
    4.16 +
    4.17 +int
    4.18 +xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
    4.19 +                              unsigned long pfn)
    4.20 +{
    4.21 +    if ((pfn >= 0xa0 && pfn < 0xc0) /* VGA hole */
    4.22 +        || (pfn >= (HVM_BELOW_4G_MMIO_START >> PAGE_SHIFT)
    4.23 +            && pfn < (1ULL<<32) >> PAGE_SHIFT)) /* MMIO */
    4.24 +        return 0;
    4.25 +    return 1;
    4.26 +}
    4.27 +
    4.28  
    4.29  static int nr_gpfns(int xc_handle, domid_t domid)
    4.30  {
    4.31 @@ -37,7 +53,7 @@ xc_core_arch_auto_translated_physmap(con
    4.32  
    4.33  int
    4.34  xc_core_arch_memory_map_get(int xc_handle, struct xc_core_arch_context *unused,
    4.35 -                            xc_dominfo_t *info, shared_info_t *live_shinfo,
    4.36 +                            xc_dominfo_t *info, shared_info_any_t *live_shinfo,
    4.37                              xc_core_memory_map_t **mapp,
    4.38                              unsigned int *nr_entries)
    4.39  {
    4.40 @@ -60,17 +76,22 @@ xc_core_arch_memory_map_get(int xc_handl
    4.41  }
    4.42  
    4.43  int
    4.44 -xc_core_arch_map_p2m(int xc_handle, xc_dominfo_t *info,
    4.45 -                     shared_info_t *live_shinfo, xen_pfn_t **live_p2m,
    4.46 +xc_core_arch_map_p2m(int xc_handle, unsigned int guest_width, xc_dominfo_t *info,
    4.47 +                     shared_info_any_t *live_shinfo, xen_pfn_t **live_p2m,
    4.48                       unsigned long *pfnp)
    4.49  {
    4.50      /* Double and single indirect references to the live P2M table */
    4.51      xen_pfn_t *live_p2m_frame_list_list = NULL;
    4.52      xen_pfn_t *live_p2m_frame_list = NULL;
    4.53 +    /* Copies of the above. */
    4.54 +    xen_pfn_t *p2m_frame_list_list = NULL;
    4.55 +    xen_pfn_t *p2m_frame_list = NULL;
    4.56 +
    4.57      uint32_t dom = info->domid;
    4.58      unsigned long p2m_size = nr_gpfns(xc_handle, info->domid);
    4.59      int ret = -1;
    4.60      int err;
    4.61 +    int i;
    4.62  
    4.63      if ( p2m_size < info->nr_pages  )
    4.64      {
    4.65 @@ -80,7 +101,7 @@ xc_core_arch_map_p2m(int xc_handle, xc_d
    4.66  
    4.67      live_p2m_frame_list_list =
    4.68          xc_map_foreign_range(xc_handle, dom, PAGE_SIZE, PROT_READ,
    4.69 -                             live_shinfo->arch.pfn_to_mfn_frame_list_list);
    4.70 +                             GET_FIELD(live_shinfo, arch.pfn_to_mfn_frame_list_list));
    4.71  
    4.72      if ( !live_p2m_frame_list_list )
    4.73      {
    4.74 @@ -88,9 +109,28 @@ xc_core_arch_map_p2m(int xc_handle, xc_d
    4.75          goto out;
    4.76      }
    4.77  
    4.78 +    /* Get a local copy of the live_P2M_frame_list_list */
    4.79 +    if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) )
    4.80 +    {
    4.81 +        ERROR("Couldn't allocate p2m_frame_list_list array");
    4.82 +        goto out;
    4.83 +    }
    4.84 +    memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE);
    4.85 +
    4.86 +    /* Canonicalize guest's unsigned long vs ours */
    4.87 +    if ( guest_width > sizeof(unsigned long) )
    4.88 +        for ( i = 0; i < PAGE_SIZE/sizeof(unsigned long); i++ )
    4.89 +            if ( i < PAGE_SIZE/guest_width )
    4.90 +                p2m_frame_list_list[i] = ((uint64_t *)p2m_frame_list_list)[i];
    4.91 +            else
    4.92 +                p2m_frame_list_list[i] = 0;
    4.93 +    else if ( guest_width < sizeof(unsigned long) )
    4.94 +        for ( i = PAGE_SIZE/sizeof(unsigned long) - 1; i >= 0; i-- )
    4.95 +            p2m_frame_list_list[i] = ((uint32_t *)p2m_frame_list_list)[i];
    4.96 +
    4.97      live_p2m_frame_list =
    4.98          xc_map_foreign_pages(xc_handle, dom, PROT_READ,
    4.99 -                             live_p2m_frame_list_list,
   4.100 +                             p2m_frame_list_list,
   4.101                               P2M_FLL_ENTRIES);
   4.102  
   4.103      if ( !live_p2m_frame_list )
   4.104 @@ -99,8 +139,25 @@ xc_core_arch_map_p2m(int xc_handle, xc_d
   4.105          goto out;
   4.106      }
   4.107  
   4.108 +    /* Get a local copy of the live_P2M_frame_list */
   4.109 +    if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) )
   4.110 +    {
   4.111 +        ERROR("Couldn't allocate p2m_frame_list array");
   4.112 +        goto out;
   4.113 +    }
   4.114 +    memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE);
   4.115 +    memcpy(p2m_frame_list, live_p2m_frame_list, P2M_GUEST_FL_SIZE);
   4.116 +
   4.117 +    /* Canonicalize guest's unsigned long vs ours */
   4.118 +    if ( guest_width > sizeof(unsigned long) )
   4.119 +        for ( i = 0; i < P2M_FL_ENTRIES; i++ )
   4.120 +            p2m_frame_list[i] = ((uint64_t *)p2m_frame_list)[i];
   4.121 +    else if ( guest_width < sizeof(unsigned long) )
   4.122 +        for ( i = P2M_FL_ENTRIES - 1; i >= 0; i-- )
   4.123 +            p2m_frame_list[i] = ((uint32_t *)p2m_frame_list)[i];
   4.124 +
   4.125      *live_p2m = xc_map_foreign_pages(xc_handle, dom, PROT_READ,
   4.126 -                                    live_p2m_frame_list,
   4.127 +                                    p2m_frame_list,
   4.128                                      P2M_FL_ENTRIES);
   4.129  
   4.130      if ( !*live_p2m )
   4.131 @@ -122,6 +179,12 @@ out:
   4.132      if ( live_p2m_frame_list )
   4.133          munmap(live_p2m_frame_list, P2M_FLL_ENTRIES * PAGE_SIZE);
   4.134  
   4.135 +    if ( p2m_frame_list_list )
   4.136 +        free(p2m_frame_list_list);
   4.137 +
   4.138 +    if ( p2m_frame_list )
   4.139 +        free(p2m_frame_list);
   4.140 +
   4.141      errno = err;
   4.142      return ret;
   4.143  }
     5.1 --- a/tools/libxc/xc_core_x86.h	Thu Jan 15 12:36:29 2009 +0000
     5.2 +++ b/tools/libxc/xc_core_x86.h	Thu Jan 15 12:37:42 2009 +0000
     5.3 @@ -21,15 +21,8 @@
     5.4  #ifndef XC_CORE_X86_H
     5.5  #define XC_CORE_X86_H
     5.6  
     5.7 -#if defined(__i386__) || defined(__x86_64__)
     5.8  #define ELF_ARCH_DATA           ELFDATA2LSB
     5.9 -#if defined (__i386__)
    5.10 -# define ELF_ARCH_MACHINE       EM_386
    5.11 -#else
    5.12 -# define ELF_ARCH_MACHINE       EM_X86_64
    5.13 -#endif
    5.14 -#endif /* __i386__ or __x86_64__ */
    5.15 -
    5.16 +#define ELF_ARCH_MACHINE       (guest_width == 8 ? EM_X86_64 : EM_386)
    5.17  
    5.18  struct xc_core_arch_context {
    5.19      /* nothing */
    5.20 @@ -40,8 +33,10 @@ struct xc_core_arch_context {
    5.21  #define xc_core_arch_context_get(arch_ctxt, ctxt, xc_handle, domid) \
    5.22                                                                  (0)
    5.23  #define xc_core_arch_context_dump(arch_ctxt, args, dump_rtn)    (0)
    5.24 -#define xc_core_arch_gpfn_may_present(arch_ctxt, i)             (1)
    5.25  
    5.26 +int
    5.27 +xc_core_arch_gpfn_may_present(struct xc_core_arch_context *arch_ctxt,
    5.28 +                              unsigned long pfn);
    5.29  static inline int
    5.30  xc_core_arch_context_get_shdr(struct xc_core_arch_context *arch_ctxt, 
    5.31                                struct xc_core_section_headers *sheaders,