debuggers.hg

view tools/libxc/xg_private.c @ 21067:b4a1832a916f

Update Xen version to 4.0.0-rc6
author Keir Fraser <keir.fraser@citrix.com>
date Tue Mar 09 18:18:05 2010 +0000 (2010-03-09)
parents fbe8f32fa257
children 3ffdb094c2c0
line source
1 /******************************************************************************
2 * xg_private.c
3 *
4 * Helper functions for the rest of the library.
5 */
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <zlib.h>
10 #include <malloc.h>
12 #include "xg_private.h"
14 char *xc_read_image(const char *filename, unsigned long *size)
15 {
16 int kernel_fd = -1;
17 gzFile kernel_gfd = NULL;
18 char *image = NULL, *tmp;
19 unsigned int bytes;
21 if ( (filename == NULL) || (size == NULL) )
22 return NULL;
24 if ( (kernel_fd = open(filename, O_RDONLY)) < 0 )
25 {
26 PERROR("Could not open kernel image");
27 goto out;
28 }
30 if ( (kernel_gfd = gzdopen(kernel_fd, "rb")) == NULL )
31 {
32 PERROR("Could not allocate decompression state for state file");
33 goto out;
34 }
36 *size = 0;
38 #define CHUNK 1*1024*1024
39 while(1)
40 {
41 if ( (tmp = realloc(image, *size + CHUNK)) == NULL )
42 {
43 PERROR("Could not allocate memory for kernel image");
44 free(image);
45 image = NULL;
46 goto out;
47 }
48 image = tmp;
50 bytes = gzread(kernel_gfd, image + *size, CHUNK);
51 switch (bytes)
52 {
53 case -1:
54 PERROR("Error reading kernel image");
55 free(image);
56 image = NULL;
57 goto out;
58 case 0: /* EOF */
59 goto out;
60 default:
61 *size += bytes;
62 break;
63 }
64 }
65 #undef CHUNK
67 out:
68 if ( *size == 0 )
69 {
70 PERROR("Could not read kernel image");
71 free(image);
72 image = NULL;
73 }
74 else if ( image )
75 {
76 /* Shrink allocation to fit image. */
77 tmp = realloc(image, *size);
78 if ( tmp )
79 image = tmp;
80 }
82 if ( kernel_gfd != NULL )
83 gzclose(kernel_gfd);
84 else if ( kernel_fd >= 0 )
85 close(kernel_fd);
86 return image;
87 }
89 char *xc_inflate_buffer(const char *in_buf, unsigned long in_size,
90 unsigned long *out_size)
91 {
92 int sts;
93 z_stream zStream;
94 unsigned long out_len;
95 char *out_buf;
97 /* Not compressed? Then return the original buffer. */
98 if ( ((unsigned char)in_buf[0] != 0x1F) ||
99 ((unsigned char)in_buf[1] != 0x8B) )
100 {
101 if ( out_size != NULL )
102 *out_size = in_size;
103 return (char *)in_buf;
104 }
106 out_len = (unsigned char)in_buf[in_size-4] +
107 (256 * ((unsigned char)in_buf[in_size-3] +
108 (256 * ((unsigned char)in_buf[in_size-2] +
109 (256 * (unsigned char)in_buf[in_size-1])))));
111 memset(&zStream, 0, sizeof(zStream));
112 out_buf = malloc(out_len + 16); /* Leave a little extra space */
113 if ( out_buf == NULL )
114 {
115 ERROR("Error mallocing buffer\n");
116 return NULL;
117 }
119 zStream.next_in = (unsigned char *)in_buf;
120 zStream.avail_in = in_size;
121 zStream.next_out = (unsigned char *)out_buf;
122 zStream.avail_out = out_len+16;
123 sts = inflateInit2(&zStream, (MAX_WBITS+32)); /* +32 means "handle gzip" */
124 if ( sts != Z_OK )
125 {
126 ERROR("inflateInit failed, sts %d\n", sts);
127 free(out_buf);
128 return NULL;
129 }
131 /* Inflate in one pass/call */
132 sts = inflate(&zStream, Z_FINISH);
133 inflateEnd(&zStream);
134 if ( sts != Z_STREAM_END )
135 {
136 ERROR("inflate failed, sts %d\n", sts);
137 free(out_buf);
138 return NULL;
139 }
141 if ( out_size != NULL )
142 *out_size = out_len;
144 return out_buf;
145 }
147 /*******************/
149 int pin_table(
150 int xc_handle, unsigned int type, unsigned long mfn, domid_t dom)
151 {
152 struct mmuext_op op;
154 op.cmd = type;
155 op.arg1.mfn = mfn;
157 if ( xc_mmuext_op(xc_handle, &op, 1, dom) < 0 )
158 return 1;
160 return 0;
161 }
163 /* This is shared between save and restore, and may generally be useful. */
164 unsigned long csum_page(void *page)
165 {
166 int i;
167 unsigned long *p = page;
168 unsigned long long sum=0;
170 for ( i = 0; i < (PAGE_SIZE/sizeof(unsigned long)); i++ )
171 sum += p[i];
173 return sum ^ (sum>>32);
174 }
176 __attribute__((weak))
177 int xc_hvm_build(int xc_handle,
178 uint32_t domid,
179 int memsize,
180 const char *image_name)
181 {
182 errno = ENOSYS;
183 return -1;
184 }
186 /*
187 * Local variables:
188 * mode: C
189 * c-set-style: "BSD"
190 * c-basic-offset: 4
191 * tab-width: 4
192 * indent-tabs-mode: nil
193 * End:
194 */