debuggers.hg

changeset 22876:c80960244942

libxc: break xc_get_physmem out into os-dependent files

NetBSD doesn't have sysconf(_SC_PHYS_PAGES).

Factor physmem() out into os-dependent files and rename it to
xc_get_physmem() so as not to pollute the namespace.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Acked-by: Ian Jackson <ian.jackson@eu.citrix.com>
Committed-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Christoph Egger <Christoph.Egger@amd.com>
date Thu Jan 27 19:03:42 2011 +0000 (2011-01-27)
parents 5222f128a83b
children e429b133278b
files tools/libxc/xc_dom_bzimageloader.c tools/libxc/xc_linux.c tools/libxc/xc_netbsd.c tools/libxc/xc_private.h
line diff
     1.1 --- a/tools/libxc/xc_dom_bzimageloader.c	Thu Jan 27 18:59:07 2011 +0000
     1.2 +++ b/tools/libxc/xc_dom_bzimageloader.c	Thu Jan 27 19:03:42 2011 +0000
     1.3 @@ -140,27 +140,6 @@ static int xc_try_bzip2_decode(
     1.4  
     1.5  #include <lzma.h>
     1.6  
     1.7 -static uint64_t physmem(void)
     1.8 -{
     1.9 -    uint64_t ret = 0;
    1.10 -    const long pagesize = sysconf(_SC_PAGESIZE);
    1.11 -    const long pages = sysconf(_SC_PHYS_PAGES);
    1.12 -
    1.13 -    if ( (pagesize != -1) || (pages != -1) )
    1.14 -    {
    1.15 -        /*
    1.16 -         * According to docs, pagesize * pages can overflow.
    1.17 -         * Simple case is 32-bit box with 4 GiB or more RAM,
    1.18 -         * which may report exactly 4 GiB of RAM, and "long"
    1.19 -         * being 32-bit will overflow. Casting to uint64_t
    1.20 -         * hopefully avoids overflows in the near future.
    1.21 -         */
    1.22 -        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
    1.23 -    }
    1.24 -
    1.25 -    return ret;
    1.26 -}
    1.27 -
    1.28  static int xc_try_lzma_decode(
    1.29      struct xc_dom_image *dom, void **blob, size_t *size)
    1.30  {
    1.31 @@ -173,7 +152,7 @@ static int xc_try_lzma_decode(
    1.32      int outsize;
    1.33      const char *msg;
    1.34  
    1.35 -    ret = lzma_alone_decoder(&stream, physmem() / 3);
    1.36 +    ret = lzma_alone_decoder(&stream, xc_get_physmem() / 3);
    1.37      if ( ret != LZMA_OK )
    1.38      {
    1.39          DOMPRINTF("LZMA: Failed to init stream decoder");
     2.1 --- a/tools/libxc/xc_linux.c	Thu Jan 27 18:59:07 2011 +0000
     2.2 +++ b/tools/libxc/xc_linux.c	Thu Jan 27 19:03:42 2011 +0000
     2.3 @@ -55,6 +55,27 @@ void discard_file_cache(xc_interface *xc
     2.4      errno = saved_errno;
     2.5  }
     2.6  
     2.7 +uint64_t xc_get_physmem(void)
     2.8 +{
     2.9 +    uint64_t ret = 0;
    2.10 +    const long pagesize = sysconf(_SC_PAGESIZE);
    2.11 +    const long pages = sysconf(_SC_PHYS_PAGES);
    2.12 +
    2.13 +    if ( (pagesize != -1) || (pages != -1) )
    2.14 +    {
    2.15 +        /*
    2.16 +         * According to docs, pagesize * pages can overflow.
    2.17 +         * Simple case is 32-bit box with 4 GiB or more RAM,
    2.18 +         * which may report exactly 4 GiB of RAM, and "long"
    2.19 +         * being 32-bit will overflow. Casting to uint64_t
    2.20 +         * hopefully avoids overflows in the near future.
    2.21 +         */
    2.22 +        ret = (uint64_t)(pagesize) * (uint64_t)(pages);
    2.23 +    }
    2.24 +
    2.25 +    return ret;
    2.26 +}
    2.27 +
    2.28  /*
    2.29   * Local variables:
    2.30   * mode: C
     3.1 --- a/tools/libxc/xc_netbsd.c	Thu Jan 27 18:59:07 2011 +0000
     3.2 +++ b/tools/libxc/xc_netbsd.c	Thu Jan 27 19:03:42 2011 +0000
     3.3 @@ -23,6 +23,9 @@
     3.4  #include <xen/sys/evtchn.h>
     3.5  #include <unistd.h>
     3.6  #include <fcntl.h>
     3.7 +#include <stdio.h>
     3.8 +#include <errno.h>
     3.9 +#include <sys/sysctl.h>
    3.10  
    3.11  static xc_osdep_handle netbsd_privcmd_open(xc_interface *xch)
    3.12  {
    3.13 @@ -351,6 +354,24 @@ void discard_file_cache(xc_interface *xc
    3.14      errno = saved_errno;
    3.15  }
    3.16  
    3.17 +uint64_t xc_get_physmem(void)
    3.18 +{
    3.19 +    int mib[2], rc;
    3.20 +    size_t len;
    3.21 +    uint64_t physmem;
    3.22 +
    3.23 +    mib[0] = CTL_HW;
    3.24 +    mib[1] = HW_PHYSMEM64;
    3.25 +    rc = sysctl(mib, 2, &physmem, &len, NULL, 0);
    3.26 +
    3.27 +    if (rc == -1) {
    3.28 +        /* PERROR("%s: Failed to get hw.physmem64: %s\n", strerror(errno)); */
    3.29 +        return 0;
    3.30 +    }
    3.31 +
    3.32 +    return physmem;
    3.33 +}
    3.34 +
    3.35  static struct xc_osdep_ops *netbsd_osdep_init(xc_interface *xch, enum xc_osdep_type type)
    3.36  {
    3.37      switch ( type )
     4.1 --- a/tools/libxc/xc_private.h	Thu Jan 27 18:59:07 2011 +0000
     4.2 +++ b/tools/libxc/xc_private.h	Thu Jan 27 19:03:42 2011 +0000
     4.3 @@ -275,6 +275,9 @@ void bitmap_byte_to_64(uint64_t *lp, con
     4.4  /* Optionally flush file to disk and discard page cache */
     4.5  void discard_file_cache(xc_interface *xch, int fd, int flush);
     4.6  
     4.7 +/* How much physical RAM is available? */
     4.8 +uint64_t xc_get_physmem(void);
     4.9 +
    4.10  #define MAX_MMU_UPDATES 1024
    4.11  struct xc_mmu {
    4.12      mmu_update_t updates[MAX_MMU_UPDATES];