direct-io.hg

view tools/libxc/xc_hvm_restore.c @ 14085:e21834bc78f2

hvm: Fix save/restore to handle missing pages.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kfraser@localhost.localdomain
date Fri Feb 23 16:17:00 2007 +0000 (2007-02-23)
parents d39e8c44da34
children aa1be6f5150e
line source
1 /******************************************************************************
2 * xc_hvm_restore.c
3 *
4 * Restore the state of a HVM guest.
5 *
6 * Copyright (c) 2003, K A Fraser.
7 * Copyright (c) 2006 Intel Corperation
8 * rewriten for hvm guest by Zhai Edwin <edwin.zhai@intel.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms and conditions of the GNU General Public License,
12 * version 2, as published by the Free Software Foundation.
13 *
14 * This program is distributed in the hope it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
17 * more details.
18 *
19 * You should have received a copy of the GNU General Public License along with
20 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21 * Place - Suite 330, Boston, MA 02111-1307 USA.
22 *
23 */
25 #include <stdlib.h>
26 #include <unistd.h>
28 #include "xg_private.h"
29 #include "xg_save_restore.h"
31 #include <xen/hvm/ioreq.h>
32 #include <xen/hvm/params.h>
33 #include <xen/hvm/e820.h>
35 /* max mfn of the whole machine */
36 static unsigned long max_mfn;
38 /* virtual starting address of the hypervisor */
39 static unsigned long hvirt_start;
41 /* #levels of page tables used by the currrent guest */
42 static unsigned int pt_levels;
44 /* A list of PFNs that exist, used when allocating memory to the guest */
45 static xen_pfn_t *pfns = NULL;
47 static ssize_t
48 read_exact(int fd, void *buf, size_t count)
49 {
50 int r = 0, s;
51 unsigned char *b = buf;
53 while (r < count) {
54 s = read(fd, &b[r], count - r);
55 if ((s == -1) && (errno == EINTR))
56 continue;
57 if (s <= 0) {
58 break;
59 }
60 r += s;
61 }
63 return (r == count) ? 1 : 0;
64 }
66 int xc_hvm_restore(int xc_handle, int io_fd,
67 uint32_t dom, unsigned long max_pfn,
68 unsigned int store_evtchn, unsigned long *store_mfn,
69 unsigned int pae, unsigned int apic)
70 {
71 DECLARE_DOMCTL;
73 /* The new domain's shared-info frame number. */
74 unsigned long shared_info_frame;
76 /* A copy of the CPU context of the guest. */
77 vcpu_guest_context_t ctxt;
79 char *region_base;
81 unsigned long buf[PAGE_SIZE/sizeof(unsigned long)];
83 xc_dominfo_t info;
84 unsigned int rc = 1, n, i;
85 uint32_t rec_len, nr_vcpus;
86 uint8_t *hvm_buf = NULL;
87 unsigned long long v_end, memsize;
88 unsigned long shared_page_nr;
90 unsigned long pfn;
91 unsigned int prev_pc, this_pc;
92 int verify = 0;
94 /* Types of the pfns in the current region */
95 unsigned long region_pfn_type[MAX_BATCH_SIZE];
97 struct xen_add_to_physmap xatp;
99 /* Number of pages of memory the guest has. *Not* the same as max_pfn. */
100 unsigned long nr_pages;
102 /* hvm guest mem size (Mb) */
103 memsize = (unsigned long long)*store_mfn;
104 v_end = memsize << 20;
105 nr_pages = (unsigned long) memsize << (20 - PAGE_SHIFT);
107 DPRINTF("xc_hvm_restore:dom=%d, nr_pages=0x%lx, store_evtchn=%d, "
108 "*store_mfn=%ld, pae=%u, apic=%u.\n",
109 dom, nr_pages, store_evtchn, *store_mfn, pae, apic);
111 if(!get_platform_info(xc_handle, dom,
112 &max_mfn, &hvirt_start, &pt_levels)) {
113 ERROR("Unable to get platform info.");
114 return 1;
115 }
117 DPRINTF("xc_hvm_restore start: nr_pages = %lx, max_pfn = %lx, "
118 "max_mfn = %lx, hvirt_start=%lx, pt_levels=%d\n",
119 nr_pages, max_pfn, max_mfn, hvirt_start, pt_levels);
121 if (mlock(&ctxt, sizeof(ctxt))) {
122 /* needed for build dom0 op, but might as well do early */
123 ERROR("Unable to mlock ctxt");
124 return 1;
125 }
128 pfns = malloc(max_pfn * sizeof(xen_pfn_t));
129 if (pfns == NULL) {
130 ERROR("memory alloc failed");
131 errno = ENOMEM;
132 goto out;
133 }
135 if(xc_domain_setmaxmem(xc_handle, dom, PFN_TO_KB(nr_pages)) != 0) {
136 errno = ENOMEM;
137 goto out;
138 }
140 for ( i = 0; i < max_pfn; i++ )
141 pfns[i] = i;
142 for ( i = HVM_BELOW_4G_RAM_END >> PAGE_SHIFT; i < max_pfn; i++ )
143 pfns[i] += HVM_BELOW_4G_MMIO_LENGTH >> PAGE_SHIFT;
145 /* Allocate memory for HVM guest, skipping VGA hole 0xA0000-0xC0000. */
146 rc = xc_domain_memory_populate_physmap(
147 xc_handle, dom, (nr_pages > 0xa0) ? 0xa0 : nr_pages,
148 0, 0, &pfns[0x00]);
149 if ( (rc == 0) && (nr_pages > 0xc0) )
150 rc = xc_domain_memory_populate_physmap(
151 xc_handle, dom, nr_pages - 0xc0, 0, 0, &pfns[0xc0]);
152 if ( rc != 0 )
153 {
154 PERROR("Could not allocate memory for HVM guest.\n");
155 goto out;
156 }
159 /**********XXXXXXXXXXXXXXXX******************/
160 if (xc_domain_getinfo(xc_handle, dom, 1, &info) != 1) {
161 ERROR("Could not get domain info");
162 return 1;
163 }
165 domctl.cmd = XEN_DOMCTL_getdomaininfo;
166 domctl.domain = (domid_t)dom;
167 if (xc_domctl(xc_handle, &domctl) < 0) {
168 ERROR("Could not get information on new domain");
169 goto out;
170 }
172 prev_pc = 0;
174 n = 0;
175 while (1) {
177 int j;
179 this_pc = (n * 100) / nr_pages;
180 if ( (this_pc - prev_pc) >= 5 )
181 {
182 PPRINTF("\b\b\b\b%3d%%", this_pc);
183 prev_pc = this_pc;
184 }
186 if (!read_exact(io_fd, &j, sizeof(int))) {
187 ERROR("HVM restore Error when reading batch size");
188 goto out;
189 }
191 PPRINTF("batch %d\n",j);
193 if (j == -1) {
194 verify = 1;
195 DPRINTF("Entering page verify mode\n");
196 continue;
197 }
199 if (j == 0)
200 break; /* our work here is done */
202 if (j > MAX_BATCH_SIZE) {
203 ERROR("Max batch size exceeded. Giving up.");
204 goto out;
205 }
207 if (!read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long))) {
208 ERROR("Error when reading region pfn types");
209 goto out;
210 }
212 region_base = xc_map_foreign_batch(
213 xc_handle, dom, PROT_WRITE, region_pfn_type, j);
215 for ( i = 0; i < j; i++ )
216 {
217 void *page;
219 pfn = region_pfn_type[i];
220 if ( pfn & XEN_DOMCTL_PFINFO_LTAB_MASK )
221 continue;
223 if ( pfn > max_pfn )
224 {
225 ERROR("pfn out of range");
226 goto out;
227 }
229 if ( pfn >= 0xa0 && pfn < 0xc0) {
230 ERROR("hvm restore:pfn in vga hole");
231 goto out;
232 }
235 /* In verify mode, we use a copy; otherwise we work in place */
236 page = verify ? (void *)buf : (region_base + i*PAGE_SIZE);
238 if (!read_exact(io_fd, page, PAGE_SIZE)) {
239 ERROR("Error when reading page (%x)", i);
240 goto out;
241 }
243 if (verify) {
245 int res = memcmp(buf, (region_base + i*PAGE_SIZE), PAGE_SIZE);
247 if (res) {
249 int v;
251 DPRINTF("************** pfn=%lx gotcs=%08lx "
252 "actualcs=%08lx\n", pfn,
253 csum_page(region_base + i*PAGE_SIZE),
254 csum_page(buf));
256 for (v = 0; v < 4; v++) {
258 unsigned long *p = (unsigned long *)
259 (region_base + i*PAGE_SIZE);
260 if (buf[v] != p[v])
261 DPRINTF(" %d: %08lx %08lx\n", v, buf[v], p[v]);
262 }
263 }
264 }
266 } /* end of 'batch' for loop */
267 munmap(region_base, j*PAGE_SIZE);
268 n+= j; /* crude stats */
270 }/*while 1*/
272 /* xc_set_hvm_param(xc_handle, dom, HVM_PARAM_APIC_ENABLED, apic);*/
273 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_PAE_ENABLED, pae);
274 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_STORE_EVTCHN, store_evtchn);
276 if ( v_end > HVM_BELOW_4G_RAM_END )
277 shared_page_nr = (HVM_BELOW_4G_RAM_END >> PAGE_SHIFT) - 1;
278 else
279 shared_page_nr = (v_end >> PAGE_SHIFT) - 1;
281 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_STORE_PFN, shared_page_nr-1);
282 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_BUFIOREQ_PFN, shared_page_nr-2);
283 xc_set_hvm_param(xc_handle, dom, HVM_PARAM_IOREQ_PFN, shared_page_nr);
285 /* caculate the store_mfn , wrong val cause hang when introduceDomain */
286 *store_mfn = (v_end >> PAGE_SHIFT) - 2;
287 DPRINTF("hvm restore:calculate new store_mfn=0x%lx,v_end=0x%llx..\n", *store_mfn, v_end);
289 /* restore hvm context including pic/pit/shpage */
290 if (!read_exact(io_fd, &rec_len, sizeof(uint32_t))) {
291 ERROR("error read hvm context size!\n");
292 goto out;
293 }
295 hvm_buf = malloc(rec_len);
296 if (hvm_buf == NULL) {
297 ERROR("memory alloc for hvm context buffer failed");
298 errno = ENOMEM;
299 goto out;
300 }
302 if (!read_exact(io_fd, hvm_buf, rec_len)) {
303 ERROR("error read hvm buffer!\n");
304 goto out;
305 }
307 if (( rc = xc_domain_hvm_setcontext(xc_handle, dom, hvm_buf, rec_len))) {
308 ERROR("error set hvm buffer!\n");
309 goto out;
310 }
312 if (!read_exact(io_fd, &nr_vcpus, sizeof(uint32_t))) {
313 ERROR("error read nr vcpu !\n");
314 goto out;
315 }
316 DPRINTF("hvm restore:get nr_vcpus=%d.\n", nr_vcpus);
318 for (i =0; i < nr_vcpus; i++) {
319 if (!read_exact(io_fd, &rec_len, sizeof(uint32_t))) {
320 ERROR("error read vcpu context size!\n");
321 goto out;
322 }
323 if (rec_len != sizeof(ctxt)) {
324 ERROR("vcpu context size dismatch!\n");
325 goto out;
326 }
328 if (!read_exact(io_fd, &(ctxt), sizeof(ctxt))) {
329 ERROR("error read vcpu context.\n");
330 goto out;
331 }
333 if ( (rc = xc_vcpu_setcontext(xc_handle, dom, i, &ctxt)) ) {
334 ERROR("Could not set vcpu context, rc=%d", rc);
335 goto out;
336 }
337 }
339 /* Shared-info pfn */
340 if (!read_exact(io_fd, &(shared_info_frame), sizeof(uint32_t)) ) {
341 ERROR("reading the shared-info pfn failed!\n");
342 goto out;
343 }
344 /* Map the shared-info frame where it was before */
345 xatp.domid = dom;
346 xatp.space = XENMAPSPACE_shared_info;
347 xatp.idx = 0;
348 xatp.gpfn = shared_info_frame;
349 if ( (rc = xc_memory_op(xc_handle, XENMEM_add_to_physmap, &xatp)) != 0 ) {
350 ERROR("setting the shared-info pfn failed!\n");
351 goto out;
352 }
354 rc = 0;
355 goto out;
357 out:
358 if ( (rc != 0) && (dom != 0) )
359 xc_domain_destroy(xc_handle, dom);
360 free(pfns);
361 free(hvm_buf);
363 DPRINTF("Restore exit with rc=%d\n", rc);
365 return rc;
366 }