ia64/xen-unstable

changeset 3434:2419f5c72fe5

bitkeeper revision 1.1159.220.2 (41e6700bFJzLjEYlNh48j_cSTbRy9A)

Code duplication cleanup: remove a number of duplicate identical
static functions in xc_*_{build,restore}.c, replaced with single
(non-static) copy in xc_private.c
author mafetter@fleming.research
date Thu Jan 13 12:56:43 2005 +0000 (2005-01-13)
parents 60d564971e17
children 0fd048d86eed
files tools/libxc/xc_linux_build.c tools/libxc/xc_linux_restore.c tools/libxc/xc_plan9_build.c tools/libxc/xc_private.c tools/libxc/xc_private.h tools/libxc/xc_vmx_build.c
line diff
     1.1 --- a/tools/libxc/xc_linux_build.c	Thu Jan 13 12:47:26 2005 +0000
     1.2 +++ b/tools/libxc/xc_linux_build.c	Thu Jan 13 12:56:43 2005 +0000
     1.3 @@ -41,52 +41,6 @@ loadelfsymtab(
     1.4      char *elfbase, int xch, u32 dom, unsigned long *parray,
     1.5      struct domain_setup_info *dsi);
     1.6  
     1.7 -static long get_tot_pages(int xc_handle, u32 domid)
     1.8 -{
     1.9 -    dom0_op_t op;
    1.10 -    op.cmd = DOM0_GETDOMAININFO;
    1.11 -    op.u.getdomaininfo.domain = (domid_t)domid;
    1.12 -    op.u.getdomaininfo.ctxt = NULL;
    1.13 -    return (do_dom0_op(xc_handle, &op) < 0) ? 
    1.14 -        -1 : op.u.getdomaininfo.tot_pages;
    1.15 -}
    1.16 -
    1.17 -static int get_pfn_list(int xc_handle,
    1.18 -                        u32 domid, 
    1.19 -                        unsigned long *pfn_buf, 
    1.20 -                        unsigned long max_pfns)
    1.21 -{
    1.22 -    dom0_op_t op;
    1.23 -    int ret;
    1.24 -    op.cmd = DOM0_GETMEMLIST;
    1.25 -    op.u.getmemlist.domain   = (domid_t)domid;
    1.26 -    op.u.getmemlist.max_pfns = max_pfns;
    1.27 -    op.u.getmemlist.buffer   = pfn_buf;
    1.28 -
    1.29 -    if ( mlock(pfn_buf, max_pfns * sizeof(unsigned long)) != 0 )
    1.30 -        return -1;
    1.31 -
    1.32 -    ret = do_dom0_op(xc_handle, &op);
    1.33 -
    1.34 -    (void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
    1.35 -
    1.36 -    return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
    1.37 -}
    1.38 -
    1.39 -static int copy_to_domain_page(int xc_handle,
    1.40 -                               u32 domid,
    1.41 -                               unsigned long dst_pfn, 
    1.42 -                               void *src_page)
    1.43 -{
    1.44 -    void *vaddr = xc_map_foreign_range(
    1.45 -        xc_handle, domid, PAGE_SIZE, PROT_WRITE, dst_pfn);
    1.46 -    if ( vaddr == NULL )
    1.47 -        return -1;
    1.48 -    memcpy(vaddr, src_page, PAGE_SIZE);
    1.49 -    munmap(vaddr, PAGE_SIZE);
    1.50 -    return 0;
    1.51 -}
    1.52 -
    1.53  static int setup_guestos(int xc_handle,
    1.54                           u32 dom,
    1.55                           char *image, unsigned long image_size,
    1.56 @@ -206,7 +160,7 @@ static int setup_guestos(int xc_handle,
    1.57          goto error_out;
    1.58      }
    1.59  
    1.60 -    if ( get_pfn_list(xc_handle, dom, page_array, nr_pages) != nr_pages )
    1.61 +    if ( xc_get_pfn_list(xc_handle, dom, page_array, nr_pages) != nr_pages )
    1.62      {
    1.63          PERROR("Could not get the page frame list");
    1.64          goto error_out;
    1.65 @@ -229,7 +183,7 @@ static int setup_guestos(int xc_handle,
    1.66                  PERROR("Error reading initrd image, could not");
    1.67                  goto error_out;
    1.68              }
    1.69 -            copy_to_domain_page(xc_handle, dom,
    1.70 +            xc_copy_to_domain_page(xc_handle, dom,
    1.71                                  page_array[i>>PAGE_SHIFT], page);
    1.72          }
    1.73      }
    1.74 @@ -364,69 +318,6 @@ static int setup_guestos(int xc_handle,
    1.75      return -1;
    1.76  }
    1.77  
    1.78 -static unsigned long get_filesz(int fd)
    1.79 -{
    1.80 -    u16 sig;
    1.81 -    u32 _sz = 0;
    1.82 -    unsigned long sz;
    1.83 -
    1.84 -    lseek(fd, 0, SEEK_SET);
    1.85 -    read(fd, &sig, sizeof(sig));
    1.86 -    sz = lseek(fd, 0, SEEK_END);
    1.87 -    if ( sig == 0x8b1f ) /* GZIP signature? */
    1.88 -    {
    1.89 -        lseek(fd, -4, SEEK_END);
    1.90 -        read(fd, &_sz, 4);
    1.91 -        sz = _sz;
    1.92 -    }
    1.93 -    lseek(fd, 0, SEEK_SET);
    1.94 -
    1.95 -    return sz;
    1.96 -}
    1.97 -
    1.98 -static char *read_kernel_image(const char *filename, unsigned long *size)
    1.99 -{
   1.100 -    int kernel_fd = -1;
   1.101 -    gzFile kernel_gfd = NULL;
   1.102 -    char *image = NULL;
   1.103 -    unsigned int bytes;
   1.104 -
   1.105 -    if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
   1.106 -    {
   1.107 -        PERROR("Could not open kernel image");
   1.108 -        goto out;
   1.109 -    }
   1.110 -
   1.111 -    *size = get_filesz(kernel_fd);
   1.112 -
   1.113 -    if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
   1.114 -    {
   1.115 -        PERROR("Could not allocate decompression state for state file");
   1.116 -        goto out;
   1.117 -    }
   1.118 -
   1.119 -    if ( (image = malloc(*size)) == NULL )
   1.120 -    {
   1.121 -        PERROR("Could not allocate memory for kernel image");
   1.122 -        goto out;
   1.123 -    }
   1.124 -
   1.125 -    if ( (bytes = gzread(kernel_gfd, image, *size)) != *size )
   1.126 -    {
   1.127 -        PERROR("Error reading kernel image, could not"
   1.128 -               " read the whole image (%d != %ld).", bytes, *size);
   1.129 -        free(image);
   1.130 -        image = NULL;
   1.131 -    }
   1.132 -
   1.133 - out:
   1.134 -    if ( kernel_gfd != NULL )
   1.135 -        gzclose(kernel_gfd);
   1.136 -    else if ( kernel_fd >= 0 )
   1.137 -        close(kernel_fd);
   1.138 -    return image;
   1.139 -}
   1.140 -
   1.141  int xc_linux_build(int xc_handle,
   1.142                     u32 domid,
   1.143                     const char *image_name,
   1.144 @@ -445,13 +336,13 @@ int xc_linux_build(int xc_handle,
   1.145      unsigned long image_size, initrd_size=0;
   1.146      unsigned long vstartinfo_start, vkern_entry;
   1.147  
   1.148 -    if ( (nr_pages = get_tot_pages(xc_handle, domid)) < 0 )
   1.149 +    if ( (nr_pages = xc_get_tot_pages(xc_handle, domid)) < 0 )
   1.150      {
   1.151          PERROR("Could not find total pages for domain");
   1.152          goto error_out;
   1.153      }
   1.154  
   1.155 -    if ( (image = read_kernel_image(image_name, &image_size)) == NULL )
   1.156 +    if ( (image = xc_read_kernel_image(image_name, &image_size)) == NULL )
   1.157          goto error_out;
   1.158  
   1.159      if ( (ramdisk_name != NULL) && (strlen(ramdisk_name) != 0) )
   1.160 @@ -462,7 +353,7 @@ int xc_linux_build(int xc_handle,
   1.161              goto error_out;
   1.162          }
   1.163  
   1.164 -        initrd_size = get_filesz(initrd_fd);
   1.165 +        initrd_size = xc_get_filesz(initrd_fd);
   1.166  
   1.167          if ( (initrd_gfd = gzdopen(initrd_fd, "rb")) == NULL )
   1.168          {
   1.169 @@ -747,27 +638,6 @@ loadelfimage(
   1.170      return 0;
   1.171  }
   1.172  
   1.173 -static void
   1.174 -map_memcpy(
   1.175 -    unsigned long dst, char *src, unsigned long size,
   1.176 -    int xch, u32 dom, unsigned long *parray, unsigned long vstart)
   1.177 -{
   1.178 -    char *va;
   1.179 -    unsigned long chunksz, done, pa;
   1.180 -
   1.181 -    for ( done = 0; done < size; done += chunksz )
   1.182 -    {
   1.183 -        pa = dst + done - vstart;
   1.184 -        va = xc_map_foreign_range(
   1.185 -            xch, dom, PAGE_SIZE, PROT_WRITE, parray[pa>>PAGE_SHIFT]);
   1.186 -        chunksz = size - done;
   1.187 -        if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
   1.188 -            chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
   1.189 -        memcpy(va + (pa & (PAGE_SIZE-1)), src + done, chunksz);
   1.190 -        munmap(va, PAGE_SIZE);
   1.191 -    }
   1.192 -}
   1.193 -
   1.194  #define ELFROUND (ELFSIZE / 8)
   1.195  
   1.196  static int
   1.197 @@ -818,7 +688,7 @@ loadelfsymtab(
   1.198               (shdr[h].sh_type == SHT_SYMTAB) )
   1.199          {
   1.200              if ( parray != NULL )
   1.201 -                map_memcpy(maxva, elfbase + shdr[h].sh_offset, shdr[h].sh_size,
   1.202 +                xc_map_memcpy(maxva, elfbase + shdr[h].sh_offset, shdr[h].sh_size,
   1.203                             xch, dom, parray, dsi->v_start);
   1.204  
   1.205              /* Mangled to be based on ELF header location. */
   1.206 @@ -850,7 +720,7 @@ loadelfsymtab(
   1.207          sym_ehdr->e_shstrndx = SHN_UNDEF;
   1.208  
   1.209          /* Copy total length, crafted ELF header and section header table */
   1.210 -        map_memcpy(symva, p, sizeof(int) + sizeof(Elf_Ehdr) +
   1.211 +        xc_map_memcpy(symva, p, sizeof(int) + sizeof(Elf_Ehdr) +
   1.212                     ehdr->e_shnum * sizeof(Elf_Shdr), xch, dom, parray,
   1.213                     dsi->v_start);
   1.214      }
     2.1 --- a/tools/libxc/xc_linux_restore.c	Thu Jan 13 12:47:26 2005 +0000
     2.2 +++ b/tools/libxc/xc_linux_restore.c	Thu Jan 13 12:56:43 2005 +0000
     2.3 @@ -19,31 +19,6 @@
     2.4  #define DPRINTF(_f, _a...) ((void)0)
     2.5  #endif
     2.6  
     2.7 -static int get_pfn_list(int xc_handle,
     2.8 -                        u32 domain_id, 
     2.9 -                        unsigned long *pfn_buf, 
    2.10 -                        unsigned long max_pfns)
    2.11 -{
    2.12 -    dom0_op_t op;
    2.13 -    int ret;
    2.14 -    op.cmd = DOM0_GETMEMLIST;
    2.15 -    op.u.getmemlist.domain   = (domid_t)domain_id;
    2.16 -    op.u.getmemlist.max_pfns = max_pfns;
    2.17 -    op.u.getmemlist.buffer   = pfn_buf;
    2.18 -
    2.19 -    if ( mlock(pfn_buf, max_pfns * sizeof(unsigned long)) != 0 )
    2.20 -    {
    2.21 -        PERROR("Could not lock pfn list buffer");
    2.22 -        return -1;
    2.23 -    }    
    2.24 -
    2.25 -    ret = do_dom0_op(xc_handle, &op);
    2.26 -
    2.27 -    (void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
    2.28 -
    2.29 -    return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
    2.30 -}
    2.31 -
    2.32  /** Read the vmconfig string from the state input.
    2.33   * It is stored as a 4-byte count 'n' followed by n bytes.
    2.34   * The config data is stored in a new string in 'ioctxt->vmconfig',
    2.35 @@ -220,7 +195,7 @@ int xc_linux_restore(int xc_handle, XcIO
    2.36      }
    2.37  
    2.38      /* Build the pfn-to-mfn table. We choose MFN ordering returned by Xen. */
    2.39 -    if ( get_pfn_list(xc_handle, dom, pfn_to_mfn_table, nr_pfns) != nr_pfns )
    2.40 +    if ( xc_get_pfn_list(xc_handle, dom, pfn_to_mfn_table, nr_pfns) != nr_pfns )
    2.41      {
    2.42          xcio_error(ioctxt, "Did not read correct number of frame "
    2.43                     "numbers for new dom");
     3.1 --- a/tools/libxc/xc_plan9_build.c	Thu Jan 13 12:47:26 2005 +0000
     3.2 +++ b/tools/libxc/xc_plan9_build.c	Thu Jan 13 12:56:43 2005 +0000
     3.3 @@ -132,52 +132,6 @@ static int
     3.4  #define L1_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED)
     3.5  #define L2_PROT (_PAGE_PRESENT|_PAGE_RW|_PAGE_ACCESSED|_PAGE_DIRTY|_PAGE_USER)
     3.6  
     3.7 -static long
     3.8 -get_tot_pages(int xc_handle, u32 domid)
     3.9 -{
    3.10 -	dom0_op_t op;
    3.11 -	op.cmd = DOM0_GETDOMAININFO;
    3.12 -	op.u.getdomaininfo.domain = (domid_t) domid;
    3.13 -	op.u.getdomaininfo.ctxt = NULL;
    3.14 -	return (do_dom0_op(xc_handle, &op) < 0) ?
    3.15 -	    -1 : op.u.getdomaininfo.tot_pages;
    3.16 -}
    3.17 -
    3.18 -static int
    3.19 -get_pfn_list(int xc_handle,
    3.20 -	     u32 domid, unsigned long *pfn_buf, unsigned long max_pfns)
    3.21 -{
    3.22 -	dom0_op_t op;
    3.23 -	int ret;
    3.24 -	op.cmd = DOM0_GETMEMLIST;
    3.25 -	op.u.getmemlist.domain = (domid_t) domid;
    3.26 -	op.u.getmemlist.max_pfns = max_pfns;
    3.27 -	op.u.getmemlist.buffer = pfn_buf;
    3.28 -
    3.29 -	if (mlock(pfn_buf, max_pfns * sizeof (unsigned long)) != 0)
    3.30 -		return -1;
    3.31 -
    3.32 -	ret = do_dom0_op(xc_handle, &op);
    3.33 -
    3.34 -	(void) munlock(pfn_buf, max_pfns * sizeof (unsigned long));
    3.35 -
    3.36 -#if 0
    3.37 -#ifdef DEBUG
    3.38 -	DPRINTF(("Ret for get_pfn_list is %d\n", ret));
    3.39 -	if (ret >= 0) {
    3.40 -		int i, j;
    3.41 -		for (i = 0; i < op.u.getmemlist.num_pfns; i += 16) {
    3.42 -			fprintf(stderr, "0x%x: ", i);
    3.43 -			for (j = 0; j < 16; j++)
    3.44 -				fprintf(stderr, "0x%lx ", pfn_buf[i + j]);
    3.45 -			fprintf(stderr, "\n");
    3.46 -		}
    3.47 -	}
    3.48 -#endif
    3.49 -#endif
    3.50 -	return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
    3.51 -}
    3.52 -
    3.53  static int
    3.54  setup_guestos(int xc_handle,
    3.55  	      u32 dom,
    3.56 @@ -216,7 +170,7 @@ setup_guestos(int xc_handle,
    3.57  		goto error_out;
    3.58  	}
    3.59  
    3.60 -	if (get_pfn_list(xc_handle, dom, cpage_array, tot_pages) != tot_pages) {
    3.61 +	if (xc_get_pfn_list(xc_handle, dom, cpage_array, tot_pages) != tot_pages) {
    3.62  		PERROR("Could not get the page frame list");
    3.63  		goto error_out;
    3.64  	}
    3.65 @@ -487,11 +441,11 @@ xc_plan9_build(int xc_handle,
    3.66  	full_execution_context_t st_ctxt, *ctxt = &st_ctxt;
    3.67  	unsigned long virt_startinfo_addr;
    3.68  
    3.69 -	if ((tot_pages = get_tot_pages(xc_handle, domid)) < 0) {
    3.70 +	if ((tot_pages = xc_get_tot_pages(xc_handle, domid)) < 0) {
    3.71  		PERROR("Could not find total pages for domain");
    3.72  		return 1;
    3.73  	}
    3.74 -	DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
    3.75 +	DPRINTF(("xc_get_tot_pages returns %ld pages\n", tot_pages));
    3.76  
    3.77  	kernel_fd = open(image_name, O_RDONLY);
    3.78  	if (kernel_fd < 0) {
    3.79 @@ -505,7 +459,7 @@ xc_plan9_build(int xc_handle,
    3.80  		return 1;
    3.81  	}
    3.82  
    3.83 -	DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
    3.84 +	DPRINTF(("xc_get_tot_pages returns %ld pages\n", tot_pages));
    3.85  	if (mlock(&st_ctxt, sizeof (st_ctxt))) {
    3.86  		PERROR("Unable to mlock ctxt");
    3.87  		return 1;
    3.88 @@ -519,7 +473,7 @@ xc_plan9_build(int xc_handle,
    3.89  		PERROR("Could not get info on domain");
    3.90  		goto error_out;
    3.91  	}
    3.92 -	DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
    3.93 +	DPRINTF(("xc_get_tot_pages returns %ld pages\n", tot_pages));
    3.94  
    3.95  	if (!(op.u.getdomaininfo.flags & DOMFLAGS_PAUSED)
    3.96  	    || (op.u.getdomaininfo.ctxt->pt_base != 0)) {
    3.97 @@ -527,7 +481,7 @@ xc_plan9_build(int xc_handle,
    3.98  		goto error_out;
    3.99  	}
   3.100  
   3.101 -	DPRINTF(("get_tot_pages returns %ld pages\n", tot_pages));
   3.102 +	DPRINTF(("xc_get_tot_pages returns %ld pages\n", tot_pages));
   3.103  	if (setup_guestos(xc_handle, domid, kernel_gfd, tot_pages,
   3.104  			  &virt_startinfo_addr,
   3.105  			  &load_addr, &st_ctxt, cmdline,
     4.1 --- a/tools/libxc/xc_private.c	Thu Jan 13 12:47:26 2005 +0000
     4.2 +++ b/tools/libxc/xc_private.c	Thu Jan 13 12:56:43 2005 +0000
     4.3 @@ -4,6 +4,7 @@
     4.4   * Helper functions for the rest of the library.
     4.5   */
     4.6  
     4.7 +#include <zlib.h>
     4.8  #include "xc_private.h"
     4.9  
    4.10  void *xc_map_foreign_batch(int xc_handle, u32 dom, int prot,
    4.11 @@ -201,5 +202,150 @@ unsigned long xc_get_m2p_start_mfn ( int
    4.12      return mfn;
    4.13  }
    4.14  
    4.15 +int xc_get_pfn_list(int xc_handle,
    4.16 +		 u32 domid, 
    4.17 +		 unsigned long *pfn_buf, 
    4.18 +		 unsigned long max_pfns)
    4.19 +{
    4.20 +    dom0_op_t op;
    4.21 +    int ret;
    4.22 +    op.cmd = DOM0_GETMEMLIST;
    4.23 +    op.u.getmemlist.domain   = (domid_t)domid;
    4.24 +    op.u.getmemlist.max_pfns = max_pfns;
    4.25 +    op.u.getmemlist.buffer   = pfn_buf;
    4.26  
    4.27  
    4.28 +    if ( mlock(pfn_buf, max_pfns * sizeof(unsigned long)) != 0 )
    4.29 +    {
    4.30 +        PERROR("Could not lock pfn list buffer");
    4.31 +        return -1;
    4.32 +    }    
    4.33 +
    4.34 +    ret = do_dom0_op(xc_handle, &op);
    4.35 +
    4.36 +    (void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
    4.37 +
    4.38 +#if 0
    4.39 +#ifdef DEBUG
    4.40 +	DPRINTF(("Ret for xc_get_pfn_list is %d\n", ret));
    4.41 +	if (ret >= 0) {
    4.42 +		int i, j;
    4.43 +		for (i = 0; i < op.u.getmemlist.num_pfns; i += 16) {
    4.44 +			fprintf(stderr, "0x%x: ", i);
    4.45 +			for (j = 0; j < 16; j++)
    4.46 +				fprintf(stderr, "0x%lx ", pfn_buf[i + j]);
    4.47 +			fprintf(stderr, "\n");
    4.48 +		}
    4.49 +	}
    4.50 +#endif
    4.51 +#endif
    4.52 +
    4.53 +    return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
    4.54 +}
    4.55 +
    4.56 +long xc_get_tot_pages(int xc_handle, u32 domid)
    4.57 +{
    4.58 +    dom0_op_t op;
    4.59 +    op.cmd = DOM0_GETDOMAININFO;
    4.60 +    op.u.getdomaininfo.domain = (domid_t)domid;
    4.61 +    op.u.getdomaininfo.ctxt = NULL;
    4.62 +    return (do_dom0_op(xc_handle, &op) < 0) ? 
    4.63 +        -1 : op.u.getdomaininfo.tot_pages;
    4.64 +}
    4.65 +
    4.66 +int xc_copy_to_domain_page(int xc_handle,
    4.67 +                                   u32 domid,
    4.68 +                                   unsigned long dst_pfn, 
    4.69 +                                   void *src_page)
    4.70 +{
    4.71 +    void *vaddr = xc_map_foreign_range(
    4.72 +        xc_handle, domid, PAGE_SIZE, PROT_WRITE, dst_pfn);
    4.73 +    if ( vaddr == NULL )
    4.74 +        return -1;
    4.75 +    memcpy(vaddr, src_page, PAGE_SIZE);
    4.76 +    munmap(vaddr, PAGE_SIZE);
    4.77 +    return 0;
    4.78 +}
    4.79 +
    4.80 +unsigned long xc_get_filesz(int fd)
    4.81 +{
    4.82 +    u16 sig;
    4.83 +    u32 _sz = 0;
    4.84 +    unsigned long sz;
    4.85 +
    4.86 +    lseek(fd, 0, SEEK_SET);
    4.87 +    read(fd, &sig, sizeof(sig));
    4.88 +    sz = lseek(fd, 0, SEEK_END);
    4.89 +    if ( sig == 0x8b1f ) /* GZIP signature? */
    4.90 +    {
    4.91 +        lseek(fd, -4, SEEK_END);
    4.92 +        read(fd, &_sz, 4);
    4.93 +        sz = _sz;
    4.94 +    }
    4.95 +    lseek(fd, 0, SEEK_SET);
    4.96 +
    4.97 +    return sz;
    4.98 +}
    4.99 +
   4.100 +char *xc_read_kernel_image(const char *filename, unsigned long *size)
   4.101 +{
   4.102 +    int kernel_fd = -1;
   4.103 +    gzFile kernel_gfd = NULL;
   4.104 +    char *image = NULL;
   4.105 +    unsigned int bytes;
   4.106 +
   4.107 +    if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
   4.108 +    {
   4.109 +        PERROR("Could not open kernel image");
   4.110 +        goto out;
   4.111 +    }
   4.112 +
   4.113 +    *size = xc_get_filesz(kernel_fd);
   4.114 +
   4.115 +    if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
   4.116 +    {
   4.117 +        PERROR("Could not allocate decompression state for state file");
   4.118 +        goto out;
   4.119 +    }
   4.120 +
   4.121 +    if ( (image = malloc(*size)) == NULL )
   4.122 +    {
   4.123 +        PERROR("Could not allocate memory for kernel image");
   4.124 +        goto out;
   4.125 +    }
   4.126 +
   4.127 +    if ( (bytes = gzread(kernel_gfd, image, *size)) != *size )
   4.128 +    {
   4.129 +        PERROR("Error reading kernel image, could not"
   4.130 +               " read the whole image (%d != %ld).", bytes, *size);
   4.131 +        free(image);
   4.132 +        image = NULL;
   4.133 +    }
   4.134 +
   4.135 + out:
   4.136 +    if ( kernel_gfd != NULL )
   4.137 +        gzclose(kernel_gfd);
   4.138 +    else if ( kernel_fd >= 0 )
   4.139 +        close(kernel_fd);
   4.140 +    return image;
   4.141 +}
   4.142 +
   4.143 +void xc_map_memcpy(unsigned long dst, char *src, unsigned long size,
   4.144 +                   int xch, u32 dom, unsigned long *parray,
   4.145 +                   unsigned long vstart)
   4.146 +{
   4.147 +    char *va;
   4.148 +    unsigned long chunksz, done, pa;
   4.149 +
   4.150 +    for ( done = 0; done < size; done += chunksz )
   4.151 +    {
   4.152 +        pa = dst + done - vstart;
   4.153 +        va = xc_map_foreign_range(
   4.154 +            xch, dom, PAGE_SIZE, PROT_WRITE, parray[pa>>PAGE_SHIFT]);
   4.155 +        chunksz = size - done;
   4.156 +        if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
   4.157 +            chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
   4.158 +        memcpy(va + (pa & (PAGE_SIZE-1)), src + done, chunksz);
   4.159 +        munmap(va, PAGE_SIZE);
   4.160 +    }
   4.161 +}
     5.1 --- a/tools/libxc/xc_private.h	Thu Jan 13 12:47:26 2005 +0000
     5.2 +++ b/tools/libxc/xc_private.h	Thu Jan 13 12:56:43 2005 +0000
     5.3 @@ -189,4 +189,17 @@ typedef struct mfn_mapper {
     5.4  
     5.5  unsigned long xc_get_m2p_start_mfn ( int xc_handle );
     5.6  
     5.7 +long xc_get_tot_pages(int xc_handle, u32 domid);
     5.8 +
     5.9 +int xc_copy_to_domain_page(int xc_handle, u32 domid,
    5.10 +                            unsigned long dst_pfn, void *src_page);
    5.11 +
    5.12 +unsigned long xc_get_filesz(int fd);
    5.13 +
    5.14 +char *xc_read_kernel_image(const char *filename, unsigned long *size);
    5.15 +
    5.16 +void xc_map_memcpy(unsigned long dst, char *src, unsigned long size,
    5.17 +                   int xch, u32 dom, unsigned long *parray,
    5.18 +                   unsigned long vstart);
    5.19 +
    5.20  #endif /* __XC_PRIVATE_H__ */
     6.1 --- a/tools/libxc/xc_vmx_build.c	Thu Jan 13 12:47:26 2005 +0000
     6.2 +++ b/tools/libxc/xc_vmx_build.c	Thu Jan 13 12:56:43 2005 +0000
     6.3 @@ -46,53 +46,6 @@ loadelfsymtab(
     6.4      char *elfbase, int xch, u32 dom, unsigned long *parray,
     6.5      struct domain_setup_info *dsi);
     6.6  
     6.7 -static long get_tot_pages(int xc_handle, u32 domid)
     6.8 -{
     6.9 -    dom0_op_t op;
    6.10 -    op.cmd = DOM0_GETDOMAININFO;
    6.11 -    op.u.getdomaininfo.domain = (domid_t)domid;
    6.12 -    op.u.getdomaininfo.ctxt = NULL;
    6.13 -    return (do_dom0_op(xc_handle, &op) < 0) ? 
    6.14 -        -1 : op.u.getdomaininfo.tot_pages;
    6.15 -}
    6.16 -
    6.17 -int xc_get_pfn_list(int xc_handle,
    6.18 -		 u32 domid, 
    6.19 -		 unsigned long *pfn_buf, 
    6.20 -		 unsigned long max_pfns)
    6.21 -{
    6.22 -    dom0_op_t op;
    6.23 -    int ret;
    6.24 -    op.cmd = DOM0_GETMEMLIST;
    6.25 -    op.u.getmemlist.domain   = (domid_t)domid;
    6.26 -    op.u.getmemlist.max_pfns = max_pfns;
    6.27 -    op.u.getmemlist.buffer   = pfn_buf;
    6.28 -
    6.29 -
    6.30 -    if ( mlock(pfn_buf, max_pfns * sizeof(unsigned long)) != 0 )
    6.31 -        return -1;
    6.32 -
    6.33 -    ret = do_dom0_op(xc_handle, &op);
    6.34 -
    6.35 -    (void)munlock(pfn_buf, max_pfns * sizeof(unsigned long));
    6.36 -
    6.37 -    return (ret < 0) ? -1 : op.u.getmemlist.num_pfns;
    6.38 -}
    6.39 -
    6.40 -static int copy_to_domain_page(int xc_handle,
    6.41 -                               u32 domid,
    6.42 -                               unsigned long dst_pfn, 
    6.43 -                               void *src_page)
    6.44 -{
    6.45 -    void *vaddr = xc_map_foreign_range(
    6.46 -        xc_handle, domid, PAGE_SIZE, PROT_WRITE, dst_pfn);
    6.47 -    if ( vaddr == NULL )
    6.48 -        return -1;
    6.49 -    memcpy(vaddr, src_page, PAGE_SIZE);
    6.50 -    munmap(vaddr, PAGE_SIZE);
    6.51 -    return 0;
    6.52 -}
    6.53 -
    6.54  static int setup_guestos(int xc_handle,
    6.55                           u32 dom,
    6.56                           char *image, unsigned long image_size,
    6.57 @@ -225,7 +178,7 @@ static int setup_guestos(int xc_handle,
    6.58                  PERROR("Error reading initrd image, could not");
    6.59                  goto error_out;
    6.60              }
    6.61 -            copy_to_domain_page(xc_handle, dom,
    6.62 +            xc_copy_to_domain_page(xc_handle, dom,
    6.63                                  page_array[i>>PAGE_SHIFT], page);
    6.64          }
    6.65      }
    6.66 @@ -402,68 +355,6 @@ static int setup_guestos(int xc_handle,
    6.67      return -1;
    6.68  }
    6.69  
    6.70 -static unsigned long get_filesz(int fd)
    6.71 -{
    6.72 -    u16 sig;
    6.73 -    u32 _sz = 0;
    6.74 -    unsigned long sz;
    6.75 -
    6.76 -    lseek(fd, 0, SEEK_SET);
    6.77 -    read(fd, &sig, sizeof(sig));
    6.78 -    sz = lseek(fd, 0, SEEK_END);
    6.79 -    if ( sig == 0x8b1f ) /* GZIP signature? */
    6.80 -    {
    6.81 -        lseek(fd, -4, SEEK_END);
    6.82 -        read(fd, &_sz, 4);
    6.83 -        sz = _sz;
    6.84 -    }
    6.85 -    lseek(fd, 0, SEEK_SET);
    6.86 -
    6.87 -    return sz;
    6.88 -}
    6.89 -
    6.90 -static char *read_kernel_image(const char *filename, unsigned long *size)
    6.91 -{
    6.92 -    int kernel_fd = -1;
    6.93 -    gzFile kernel_gfd = NULL;
    6.94 -    char *image = NULL;
    6.95 -    unsigned int bytes;
    6.96 -
    6.97 -    if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
    6.98 -    {
    6.99 -        PERROR("Could not open kernel image");
   6.100 -        goto out;
   6.101 -    }
   6.102 -
   6.103 -    *size = get_filesz(kernel_fd);
   6.104 -
   6.105 -    if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
   6.106 -    {
   6.107 -        PERROR("Could not allocate decompression state for state file");
   6.108 -        goto out;
   6.109 -    }
   6.110 -
   6.111 -    if ( (image = malloc(*size)) == NULL )
   6.112 -    {
   6.113 -        PERROR("Could not allocate memory for kernel image");
   6.114 -        goto out;
   6.115 -    }
   6.116 -
   6.117 -    if ( (bytes = gzread(kernel_gfd, image, *size)) != *size )
   6.118 -    {
   6.119 -        PERROR("Error reading kernel image, could not"
   6.120 -               " read the whole image (%d != %ld).", bytes, *size);
   6.121 -        free(image);
   6.122 -        image = NULL;
   6.123 -    }
   6.124 -
   6.125 - out:
   6.126 -    if ( kernel_gfd != NULL )
   6.127 -        gzclose(kernel_gfd);
   6.128 -    else if ( kernel_fd >= 0 )
   6.129 -        close(kernel_fd);
   6.130 -    return image;
   6.131 -}
   6.132  
   6.133  #define VMX_FEATURE_FLAG 0x20
   6.134  
   6.135 @@ -505,13 +396,13 @@ int xc_vmx_build(int xc_handle,
   6.136          goto error_out;
   6.137      }
   6.138      
   6.139 -    if ( (nr_pages = get_tot_pages(xc_handle, domid)) < 0 )
   6.140 +    if ( (nr_pages = xc_get_tot_pages(xc_handle, domid)) < 0 )
   6.141      {
   6.142          PERROR("Could not find total pages for domain");
   6.143          goto error_out;
   6.144      }
   6.145  
   6.146 -    if ( (image = read_kernel_image(image_name, &image_size)) == NULL )
   6.147 +    if ( (image = xc_read_kernel_image(image_name, &image_size)) == NULL )
   6.148          goto error_out;
   6.149  
   6.150      if ( (ramdisk_name != NULL) && (strlen(ramdisk_name) != 0) )
   6.151 @@ -522,7 +413,7 @@ int xc_vmx_build(int xc_handle,
   6.152              goto error_out;
   6.153          }
   6.154  
   6.155 -        initrd_size = get_filesz(initrd_fd);
   6.156 +        initrd_size = xc_get_filesz(initrd_fd);
   6.157  
   6.158          if ( (initrd_gfd = gzdopen(initrd_fd, "rb")) == NULL )
   6.159          {
   6.160 @@ -746,26 +637,6 @@ loadelfimage(
   6.161      return 0;
   6.162  }
   6.163  
   6.164 -static void
   6.165 -map_memcpy(
   6.166 -    unsigned long dst, char *src, unsigned long size,
   6.167 -    int xch, u32 dom, unsigned long *parray, unsigned long vstart)
   6.168 -{
   6.169 -    char *va;
   6.170 -    unsigned long chunksz, done, pa;
   6.171 -
   6.172 -    for ( done = 0; done < size; done += chunksz )
   6.173 -    {
   6.174 -        pa = dst + done - vstart;
   6.175 -        va = xc_map_foreign_range(
   6.176 -            xch, dom, PAGE_SIZE, PROT_WRITE, parray[pa>>PAGE_SHIFT]);
   6.177 -        chunksz = size - done;
   6.178 -        if ( chunksz > (PAGE_SIZE - (pa & (PAGE_SIZE-1))) )
   6.179 -            chunksz = PAGE_SIZE - (pa & (PAGE_SIZE-1));
   6.180 -        memcpy(va + (pa & (PAGE_SIZE-1)), src + done, chunksz);
   6.181 -        munmap(va, PAGE_SIZE);
   6.182 -    }
   6.183 -}
   6.184  
   6.185  #define ELFROUND (ELFSIZE / 8)
   6.186  
   6.187 @@ -817,7 +688,7 @@ loadelfsymtab(
   6.188               (shdr[h].sh_type == SHT_SYMTAB) )
   6.189          {
   6.190              if ( parray != NULL )
   6.191 -                map_memcpy(maxva, elfbase + shdr[h].sh_offset, shdr[h].sh_size,
   6.192 +                xc_map_memcpy(maxva, elfbase + shdr[h].sh_offset, shdr[h].sh_size,
   6.193                             xch, dom, parray, dsi->v_start);
   6.194  
   6.195              /* Mangled to be based on ELF header location. */
   6.196 @@ -849,7 +720,7 @@ loadelfsymtab(
   6.197          sym_ehdr->e_shstrndx = SHN_UNDEF;
   6.198  
   6.199          /* Copy total length, crafted ELF header and section header table */
   6.200 -        map_memcpy(symva, p, sizeof(int) + sizeof(Elf_Ehdr) +
   6.201 +        xc_map_memcpy(symva, p, sizeof(int) + sizeof(Elf_Ehdr) +
   6.202                     ehdr->e_shnum * sizeof(Elf_Shdr), xch, dom, parray,
   6.203                     dsi->v_start);
   6.204      }