ia64/xen-unstable

view tools/libxc/xg_private.c @ 6432:b54144915ae6

merge?
author cl349@firebug.cl.cam.ac.uk
date Thu Aug 25 16:26:30 2005 +0000 (2005-08-25)
parents 3428d58a85e1 fdfd511768a3
children 0610add7c3fe
line source
1 /******************************************************************************
2 * xg_private.c
3 *
4 * Helper functions for the rest of the library.
5 */
7 #include <stdlib.h>
8 #include <zlib.h>
10 #include "xg_private.h"
12 char *xc_read_kernel_image(const char *filename, unsigned long *size)
13 {
14 int kernel_fd = -1;
15 gzFile kernel_gfd = NULL;
16 char *image = NULL;
17 unsigned int bytes;
19 if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
20 {
21 PERROR("Could not open kernel image");
22 goto out;
23 }
25 if ( (*size = xc_get_filesz(kernel_fd)) == 0 )
26 {
27 PERROR("Could not read kernel image");
28 goto out;
29 }
31 if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
32 {
33 PERROR("Could not allocate decompression state for state file");
34 goto out;
35 }
37 if ( (image = malloc(*size)) == NULL )
38 {
39 PERROR("Could not allocate memory for kernel image");
40 goto out;
41 }
43 if ( (bytes = gzread(kernel_gfd, image, *size)) != *size )
44 {
45 PERROR("Error reading kernel image, could not"
46 " read the whole image (%d != %ld).", bytes, *size);
47 free(image);
48 image = NULL;
49 }
51 out:
52 if ( kernel_gfd != NULL )
53 gzclose(kernel_gfd);
54 else if ( kernel_fd >= 0 )
55 close(kernel_fd);
56 return image;
57 }
59 /*******************/
61 int pin_table(
62 int xc_handle, unsigned int type, unsigned long mfn, domid_t dom)
63 {
64 struct mmuext_op op;
66 op.cmd = type;
67 op.mfn = mfn;
69 if ( xc_mmuext_op(xc_handle, &op, 1, dom) < 0 )
70 return 1;
72 return 0;
73 }
75 /* This is shared between save and restore, and may generally be useful. */
76 unsigned long csum_page (void * page)
77 {
78 int i;
79 unsigned long *p = page;
80 unsigned long long sum=0;
82 for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
83 sum += p[i];
85 return sum ^ (sum>>32);
86 }