debuggers.hg

view tools/libxc/xc_domain_restore.c @ 21529:779c0ef9682c

libxc: eliminate static variables, use xentoollog; API change

This patch eliminate the global variables in libxenctrl (used for
logging and error reporting).

Instead the information which was in the global variables is now in a
new xc_interface* opaque structure, which xc_interface open returns
instead of the raw file descriptor; furthermore, logging is done via
xentoollog.

There are three new parameters to xc_interface_open to control the
logging, but existing callers can just pass "0" for all three to get
the old behaviour.

All libxc callers have been adjusted accordingly.

Also update QEMU_TAG for corresponding qemu change.

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Fri May 28 09:30:19 2010 +0100 (2010-05-28)
parents a948403c8f99
children 65a7ac5e0a0a
line source
1 /******************************************************************************
2 * xc_domain_restore.c
3 *
4 * Restore the state of a guest session.
5 *
6 * Copyright (c) 2003, K A Fraser.
7 * Copyright (c) 2006, Intel Corporation
8 * Copyright (c) 2007, XenSource Inc.
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"
30 #include "xc_dom.h"
32 #include <xen/hvm/ioreq.h>
33 #include <xen/hvm/params.h>
35 struct restore_ctx {
36 unsigned long max_mfn; /* max mfn of the current host machine */
37 unsigned long hvirt_start; /* virtual starting address of the hypervisor */
38 unsigned int pt_levels; /* #levels of page tables used by the current guest */
39 unsigned long nr_pfns; /* number of 'in use' pfns in the guest (i.e. #P2M entries with a valid mfn) */
40 xen_pfn_t *live_p2m; /* Live mapping of the table mapping each PFN to its current MFN. */
41 xen_pfn_t *p2m; /* A table mapping each PFN to its new MFN. */
42 unsigned no_superpage_mem; /* If have enough continuous memory for super page allocation */
43 struct domain_info_context dinfo;
44 };
46 /*
47 **
48 **
49 */
50 #define SUPERPAGE_PFN_SHIFT 9
51 #define SUPERPAGE_NR_PFNS (1UL << SUPERPAGE_PFN_SHIFT)
53 /*
54 * Setting bit 31 force to allocate super page even not all pfns come out,
55 * bit 30 indicate that not is in a super page tracking.
56 */
57 #define FORCE_SP_SHIFT 31
58 #define FORCE_SP_MASK (1UL << FORCE_SP_SHIFT)
60 #define INVALID_SUPER_PAGE ((1UL << 30) + 1)
61 #define SUPER_PAGE_START(pfn) (((pfn) & (SUPERPAGE_NR_PFNS-1)) == 0 )
62 #define SUPER_PAGE_TRACKING(pfn) ( (pfn) != INVALID_SUPER_PAGE )
63 #define SUPER_PAGE_DONE(pfn) ( SUPER_PAGE_START(pfn) )
65 static int super_page_populated(xc_interface *xch,
66 struct restore_ctx *ctx, unsigned long pfn)
67 {
68 int i;
69 pfn &= ~(SUPERPAGE_NR_PFNS - 1);
70 for ( i = pfn; i < pfn + SUPERPAGE_NR_PFNS; i++ )
71 {
72 if ( ctx->p2m[i] != INVALID_P2M_ENTRY )
73 return 1;
74 }
75 return 0;
76 }
78 /*
79 * Break a 2M page and move contents of [extent start, next_pfn-1] to
80 * some new allocated 4K pages
81 */
82 static int break_super_page(xc_interface *xch,
83 uint32_t dom,
84 struct restore_ctx *ctx,
85 xen_pfn_t next_pfn)
86 {
87 xen_pfn_t *page_array, start_pfn, mfn;
88 uint8_t *ram_base, *save_buf;
89 unsigned long i;
90 int tot_pfns, rc = 0;
92 tot_pfns = (next_pfn & (SUPERPAGE_NR_PFNS - 1));
94 start_pfn = next_pfn & ~(SUPERPAGE_NR_PFNS - 1);
95 for ( i = start_pfn; i < start_pfn + SUPERPAGE_NR_PFNS; i++ )
96 {
97 /* check the 2M page are populated */
98 if ( ctx->p2m[i] == INVALID_P2M_ENTRY ) {
99 DPRINTF("Previous super page was populated wrongly!\n");
100 return 1;
101 }
102 }
104 page_array = (xen_pfn_t*)malloc(tot_pfns * sizeof(xen_pfn_t));
105 save_buf = (uint8_t*)malloc(tot_pfns * PAGE_SIZE);
107 if ( !page_array || !save_buf )
108 {
109 ERROR("alloc page_array failed\n");
110 errno = ENOMEM;
111 rc = 1;
112 goto out;
113 }
115 /* save previous super page contents */
116 for ( i = 0; i < tot_pfns; i++ )
117 {
118 /* only support HVM, as the mfn of the 2M page is missing */
119 page_array[i] = start_pfn + i;
120 }
122 ram_base = xc_map_foreign_pages(xch, dom, PROT_READ,
123 page_array, tot_pfns);
125 if ( ram_base == NULL )
126 {
127 ERROR("map batch failed\n");
128 rc = 1;
129 goto out;
130 }
132 memcpy(save_buf, ram_base, tot_pfns * PAGE_SIZE);
133 munmap(ram_base, tot_pfns * PAGE_SIZE);
135 /* free the super page */
136 if ( xc_domain_memory_decrease_reservation(xch, dom, 1,
137 SUPERPAGE_PFN_SHIFT, &start_pfn) != 0 )
138 {
139 ERROR("free 2M page failure @ 0x%ld.\n", next_pfn);
140 rc = 1;
141 goto out;
142 }
144 start_pfn = next_pfn & ~(SUPERPAGE_NR_PFNS - 1);
145 for ( i = start_pfn; i < start_pfn + SUPERPAGE_NR_PFNS; i++ )
146 {
147 ctx->p2m[i] = INVALID_P2M_ENTRY;
148 }
150 for ( i = start_pfn; i < start_pfn + tot_pfns; i++ )
151 {
152 mfn = i;
153 if (xc_domain_memory_populate_physmap(xch, dom, 1, 0,
154 0, &mfn) != 0)
155 {
156 ERROR("Failed to allocate physical memory.!\n");
157 errno = ENOMEM;
158 rc = 1;
159 goto out;
160 }
161 ctx->p2m[i] = mfn;
162 }
164 /* restore contents */
165 for ( i = 0; i < tot_pfns; i++ )
166 {
167 page_array[i] = start_pfn + i;
168 }
170 ram_base = xc_map_foreign_pages(xch, dom, PROT_WRITE,
171 page_array, tot_pfns);
172 if ( ram_base == NULL )
173 {
174 ERROR("map batch failed\n");
175 rc = 1;
176 goto out;
177 }
179 memcpy(ram_base, save_buf, tot_pfns * PAGE_SIZE);
180 munmap(ram_base, tot_pfns * PAGE_SIZE);
182 out:
183 free(page_array);
184 free(save_buf);
185 return rc;
186 }
189 /*
190 * According to pfn list allocate pages: one 2M page or series of 4K pages.
191 * Also optimistically allocate a 2M page even when not all pages in the 2M
192 * extent come out, and fix it up in next batch:
193 * If new pages fit the missing one in the 2M extent, do nothing; Else take
194 * place of the original 2M page by some 4K pages.
195 */
196 static int allocate_mfn_list(xc_interface *xch,
197 uint32_t dom,
198 struct restore_ctx *ctx,
199 unsigned long nr_extents,
200 xen_pfn_t *batch_buf,
201 xen_pfn_t *next_pfn,
202 int superpages)
203 {
204 unsigned int i;
205 unsigned long mfn, pfn, sp_pfn;
207 /*Check if force super page, then clear it */
208 unsigned force_super_page = !!(*next_pfn & FORCE_SP_MASK);
209 *next_pfn &= ~FORCE_SP_MASK;
211 sp_pfn = *next_pfn;
213 if ( !superpages ||
214 ctx->no_superpage_mem ||
215 !SUPER_PAGE_TRACKING(sp_pfn) )
216 goto normal_page;
218 if ( !batch_buf )
219 {
220 /* Break previous 2M page, if 512 pages split across a batch boundary */
221 if ( SUPER_PAGE_TRACKING(sp_pfn) &&
222 !SUPER_PAGE_DONE(sp_pfn))
223 {
224 /* break previously allocated super page*/
225 if ( break_super_page(xch, dom, ctx, sp_pfn) != 0 )
226 {
227 ERROR("Break previous super page fail!\n");
228 return 1;
229 }
230 }
232 /* follwing pages fit the order in 2M extent */
233 return 0;
234 }
236 /*
237 * We try to allocate a 2M page only when:
238 * user require this(superpages),
239 * AND have enough memory,
240 * AND is in the tracking,
241 * AND tracked all pages in 2M extent, OR partial 2M extent for speculation
242 * AND any page in 2M extent are not populated
243 */
244 if ( !SUPER_PAGE_DONE(sp_pfn) && !force_super_page )
245 goto normal_page;
247 pfn = batch_buf[0] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
248 if ( super_page_populated(xch, ctx, pfn) )
249 goto normal_page;
251 pfn &= ~(SUPERPAGE_NR_PFNS - 1);
252 mfn = pfn;
254 if ( xc_domain_memory_populate_physmap(xch, dom, 1,
255 SUPERPAGE_PFN_SHIFT, 0, &mfn) == 0)
256 {
257 for ( i = pfn; i < pfn + SUPERPAGE_NR_PFNS; i++, mfn++ )
258 {
259 ctx->p2m[i] = mfn;
260 }
261 return 0;
262 }
263 DPRINTF("No 2M page available for pfn 0x%lx, fall back to 4K page.\n",
264 pfn);
265 ctx->no_superpage_mem = 1;
267 normal_page:
268 if ( !batch_buf )
269 return 0;
271 /* End the tracking, if want a 2M page but end by 4K pages, */
272 *next_pfn = INVALID_SUPER_PAGE;
274 for ( i = 0; i < nr_extents; i++ )
275 {
276 unsigned long pagetype = batch_buf[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
277 if ( pagetype == XEN_DOMCTL_PFINFO_XTAB )
278 continue;
280 pfn = mfn = batch_buf[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
281 if ( ctx->p2m[pfn] == INVALID_P2M_ENTRY )
282 {
283 if (xc_domain_memory_populate_physmap(xch, dom, 1, 0,
284 0, &mfn) != 0)
285 {
286 ERROR("Failed to allocate physical memory.! pfn=0x%lx, mfn=0x%lx.\n",
287 pfn, mfn);
288 errno = ENOMEM;
289 return 1;
290 }
291 ctx->p2m[pfn] = mfn;
292 }
293 }
295 return 0;
296 }
298 static int allocate_physmem(xc_interface *xch, uint32_t dom,
299 struct restore_ctx *ctx,
300 unsigned long *region_pfn_type, int region_size,
301 unsigned int hvm, xen_pfn_t *region_mfn, int superpages)
302 {
303 int i;
304 unsigned long pfn;
305 unsigned long pagetype;
307 /* Next expected pfn in order to track a possible 2M page */
308 static unsigned long required_pfn = INVALID_SUPER_PAGE;
310 /* Buffer of pfn list for 2M page, or series of 4K pages */
311 xen_pfn_t *batch_buf;
312 unsigned int batch_buf_len;
313 struct domain_info_context *dinfo = &ctx->dinfo;
315 if ( !superpages )
316 {
317 batch_buf = &region_pfn_type[0];
318 batch_buf_len = region_size;
319 goto alloc_page;
320 }
322 batch_buf = NULL;
323 batch_buf_len = 0;
324 /* This loop tracks the possible 2M page */
325 for (i = 0; i < region_size; i++)
326 {
327 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
328 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
330 if (pagetype == XEN_DOMCTL_PFINFO_XTAB)
331 {
332 /* Do not start collecting pfns until get a valid pfn */
333 if ( batch_buf_len != 0 )
334 batch_buf_len++;
335 continue;
336 }
338 if ( SUPER_PAGE_START(pfn) )
339 {
340 /* Start of a 2M extent, populate previsous buf */
341 if ( allocate_mfn_list(xch, dom, ctx,
342 batch_buf_len, batch_buf,
343 &required_pfn, superpages) != 0 )
344 {
345 errno = ENOMEM;
346 return 1;
347 }
349 /* start new tracking for 2M page */
350 batch_buf = &region_pfn_type[i];
351 batch_buf_len = 1;
352 required_pfn = pfn + 1;
353 }
354 else if ( pfn == required_pfn )
355 {
356 /* this page fit the 2M extent in order */
357 batch_buf_len++;
358 required_pfn++;
359 }
360 else if ( SUPER_PAGE_TRACKING(required_pfn) )
361 {
362 /* break of a 2M extent, populate previous buf */
363 if ( allocate_mfn_list(xch, dom, ctx,
364 batch_buf_len, batch_buf,
365 &required_pfn, superpages) != 0 )
366 {
367 errno = ENOMEM;
368 return 1;
369 }
370 /* start new tracking for a series of 4K pages */
371 batch_buf = &region_pfn_type[i];
372 batch_buf_len = 1;
373 required_pfn = INVALID_SUPER_PAGE;
374 }
375 else
376 {
377 /* this page is 4K */
378 if ( !batch_buf )
379 batch_buf = &region_pfn_type[i];
380 batch_buf_len++;
381 }
382 }
384 /*
385 * populate rest batch_buf in the end.
386 * In a speculative way, we allocate a 2M page even when not see all the
387 * pages in order(set bit 31). If not require super page support,
388 * we can skip the tracking loop and come here directly.
389 * Speculative allocation can't be used for PV guest, as we have no mfn to
390 * map previous 2M mem range if need break it.
391 */
392 if ( SUPER_PAGE_TRACKING(required_pfn) &&
393 !SUPER_PAGE_DONE(required_pfn) )
394 {
395 if (hvm)
396 required_pfn |= FORCE_SP_MASK;
397 else
398 required_pfn = INVALID_SUPER_PAGE;
399 }
401 alloc_page:
402 if ( batch_buf )
403 {
404 if ( allocate_mfn_list(xch, dom, ctx,
405 batch_buf_len, batch_buf,
406 &required_pfn,
407 superpages) != 0 )
408 {
409 errno = ENOMEM;
410 return 1;
411 }
412 }
414 for (i = 0; i < region_size; i++)
415 {
416 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
417 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
419 if ( pfn > dinfo->p2m_size )
420 {
421 ERROR("pfn out of range");
422 return 1;
423 }
424 if (pagetype == XEN_DOMCTL_PFINFO_XTAB)
425 {
426 region_mfn[i] = ~0UL;
427 }
428 else
429 {
430 if (ctx->p2m[pfn] == INVALID_P2M_ENTRY)
431 {
432 DPRINTF("Warning: pfn 0x%lx are not allocated!\n", pfn);
433 /*XXX:allocate this page?*/
434 }
436 /* setup region_mfn[] for batch map.
437 * For HVM guests, this interface takes PFNs, not MFNs */
438 region_mfn[i] = hvm ? pfn : ctx->p2m[pfn];
439 }
440 }
441 return 0;
442 }
445 /* set when a consistent image is available */
446 static int completed = 0;
448 #define HEARTBEAT_MS 1000
450 #ifndef __MINIOS__
451 static ssize_t read_exact_timed(int fd, void* buf, size_t size)
452 {
453 size_t offset = 0;
454 ssize_t len;
455 struct timeval tv;
456 fd_set rfds;
458 while ( offset < size )
459 {
460 if ( completed ) {
461 /* expect a heartbeat every HEARBEAT_MS ms maximum */
462 tv.tv_sec = HEARTBEAT_MS / 1000;
463 tv.tv_usec = (HEARTBEAT_MS % 1000) * 1000;
465 FD_ZERO(&rfds);
466 FD_SET(fd, &rfds);
467 len = select(fd + 1, &rfds, NULL, NULL, &tv);
468 if ( !FD_ISSET(fd, &rfds) ) {
469 fprintf(stderr, "read_exact_timed failed (select returned %zd)\n", len);
470 return -1;
471 }
472 }
474 len = read(fd, buf + offset, size - offset);
475 if ( (len == -1) && ((errno == EINTR) || (errno == EAGAIN)) )
476 continue;
477 if ( len <= 0 )
478 return -1;
479 offset += len;
480 }
482 return 0;
483 }
485 #define read_exact read_exact_timed
487 #else
488 #define read_exact_timed read_exact
489 #endif
490 /*
491 ** In the state file (or during transfer), all page-table pages are
492 ** converted into a 'canonical' form where references to actual mfns
493 ** are replaced with references to the corresponding pfns.
494 ** This function inverts that operation, replacing the pfn values with
495 ** the (now known) appropriate mfn values.
496 */
497 static int uncanonicalize_pagetable(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
498 void *page, int superpages)
499 {
500 int i, pte_last;
501 unsigned long pfn;
502 uint64_t pte;
503 struct domain_info_context *dinfo = &ctx->dinfo;
505 pte_last = PAGE_SIZE / ((ctx->pt_levels == 2)? 4 : 8);
507 for ( i = 0; i < pte_last; i++ )
508 {
509 if ( ctx->pt_levels == 2 )
510 pte = ((uint32_t *)page)[i];
511 else
512 pte = ((uint64_t *)page)[i];
514 /* XXX SMH: below needs fixing for PROT_NONE etc */
515 if ( !(pte & _PAGE_PRESENT) )
516 continue;
518 pfn = (pte >> PAGE_SHIFT) & MFN_MASK_X86;
520 /* Allocate mfn if necessary */
521 if ( ctx->p2m[pfn] == INVALID_P2M_ENTRY )
522 {
523 unsigned long force_pfn = superpages ? FORCE_SP_MASK : pfn;
524 if (allocate_mfn_list(xch, dom, ctx,
525 1, &pfn, &force_pfn, superpages) != 0)
526 return 0;
527 }
528 pte &= ~MADDR_MASK_X86;
529 pte |= (uint64_t)ctx->p2m[pfn] << PAGE_SHIFT;
531 if ( ctx->pt_levels == 2 )
532 ((uint32_t *)page)[i] = (uint32_t)pte;
533 else
534 ((uint64_t *)page)[i] = (uint64_t)pte;
535 }
537 return 1;
538 }
541 /* Load the p2m frame list, plus potential extended info chunk */
542 static xen_pfn_t *load_p2m_frame_list(
543 xc_interface *xch, struct restore_ctx *ctx,
544 int io_fd, int *pae_extended_cr3, int *ext_vcpucontext)
545 {
546 xen_pfn_t *p2m_frame_list;
547 vcpu_guest_context_any_t ctxt;
548 xen_pfn_t p2m_fl_zero;
549 struct domain_info_context *dinfo = &ctx->dinfo;
551 /* Read first entry of P2M list, or extended-info signature (~0UL). */
552 if ( read_exact(io_fd, &p2m_fl_zero, sizeof(long)) )
553 {
554 ERROR("read extended-info signature failed");
555 return NULL;
556 }
558 if ( p2m_fl_zero == ~0UL )
559 {
560 uint32_t tot_bytes;
562 /* Next 4 bytes: total size of following extended info. */
563 if ( read_exact(io_fd, &tot_bytes, sizeof(tot_bytes)) )
564 {
565 ERROR("read extended-info size failed");
566 return NULL;
567 }
569 while ( tot_bytes )
570 {
571 uint32_t chunk_bytes;
572 char chunk_sig[4];
574 /* 4-character chunk signature + 4-byte remaining chunk size. */
575 if ( read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) ||
576 read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes)) ||
577 (tot_bytes < (chunk_bytes + 8)) )
578 {
579 ERROR("read extended-info chunk signature failed");
580 return NULL;
581 }
582 tot_bytes -= 8;
584 /* VCPU context structure? */
585 if ( !strncmp(chunk_sig, "vcpu", 4) )
586 {
587 /* Pick a guest word-size and PT depth from the ctxt size */
588 if ( chunk_bytes == sizeof (ctxt.x32) )
589 {
590 dinfo->guest_width = 4;
591 if ( ctx->pt_levels > 2 )
592 ctx->pt_levels = 3;
593 }
594 else if ( chunk_bytes == sizeof (ctxt.x64) )
595 {
596 dinfo->guest_width = 8;
597 ctx->pt_levels = 4;
598 }
599 else
600 {
601 ERROR("bad extended-info context size %d", chunk_bytes);
602 return NULL;
603 }
605 if ( read_exact(io_fd, &ctxt, chunk_bytes) )
606 {
607 ERROR("read extended-info vcpu context failed");
608 return NULL;
609 }
610 tot_bytes -= chunk_bytes;
611 chunk_bytes = 0;
613 if ( GET_FIELD(&ctxt, vm_assist)
614 & (1UL << VMASST_TYPE_pae_extended_cr3) )
615 *pae_extended_cr3 = 1;
616 }
617 else if ( !strncmp(chunk_sig, "extv", 4) )
618 {
619 *ext_vcpucontext = 1;
620 }
622 /* Any remaining bytes of this chunk: read and discard. */
623 while ( chunk_bytes )
624 {
625 unsigned long sz = MIN(chunk_bytes, sizeof(xen_pfn_t));
626 if ( read_exact(io_fd, &p2m_fl_zero, sz) )
627 {
628 ERROR("read-and-discard extended-info chunk bytes failed");
629 return NULL;
630 }
631 chunk_bytes -= sz;
632 tot_bytes -= sz;
633 }
634 }
636 /* Now read the real first entry of P2M list. */
637 if ( read_exact(io_fd, &p2m_fl_zero, sizeof(xen_pfn_t)) )
638 {
639 ERROR("read first entry of p2m_frame_list failed");
640 return NULL;
641 }
642 }
644 /* Now that we know the guest's word-size, can safely allocate
645 * the p2m frame list */
646 if ( (p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) == NULL )
647 {
648 ERROR("Couldn't allocate p2m_frame_list array");
649 return NULL;
650 }
652 /* First entry has already been read. */
653 p2m_frame_list[0] = p2m_fl_zero;
654 if ( read_exact(io_fd, &p2m_frame_list[1],
655 (P2M_FL_ENTRIES - 1) * sizeof(xen_pfn_t)) )
656 {
657 ERROR("read p2m_frame_list failed");
658 return NULL;
659 }
661 return p2m_frame_list;
662 }
664 typedef struct {
665 int ishvm;
666 union {
667 struct tailbuf_pv {
668 unsigned int pfncount;
669 unsigned long* pfntab;
670 unsigned int vcpucount;
671 unsigned char* vcpubuf;
672 unsigned char shared_info_page[PAGE_SIZE];
673 } pv;
674 struct tailbuf_hvm {
675 uint64_t magicpfns[3];
676 uint32_t hvmbufsize, reclen;
677 uint8_t* hvmbuf;
678 struct {
679 uint32_t magic;
680 uint32_t version;
681 uint64_t len;
682 } qemuhdr;
683 uint32_t qemubufsize;
684 uint8_t* qemubuf;
685 } hvm;
686 } u;
687 } tailbuf_t;
689 /* read stream until EOF, growing buffer as necssary */
690 static int compat_buffer_qemu(xc_interface *xch,
691 int fd, struct tailbuf_hvm *buf)
692 {
693 uint8_t *qbuf, *tmp;
694 int blen = 0, dlen = 0;
695 int rc;
697 /* currently save records tend to be about 7K */
698 blen = 8192;
699 if ( !(qbuf = malloc(blen)) ) {
700 ERROR("Error allocating QEMU buffer");
701 return -1;
702 }
704 while( (rc = read(fd, qbuf+dlen, blen-dlen)) > 0 ) {
705 DPRINTF("Read %d bytes of QEMU data\n", rc);
706 dlen += rc;
708 if (dlen == blen) {
709 DPRINTF("%d-byte QEMU buffer full, reallocating...\n", dlen);
710 blen += 4096;
711 tmp = realloc(qbuf, blen);
712 if ( !tmp ) {
713 ERROR("Error growing QEMU buffer to %d bytes", blen);
714 free(qbuf);
715 return -1;
716 }
717 qbuf = tmp;
718 }
719 }
721 if ( rc < 0 ) {
722 ERROR("Error reading QEMU data");
723 free(qbuf);
724 return -1;
725 }
727 if ( memcmp(qbuf, "QEVM", 4) ) {
728 ERROR("Invalid QEMU magic: 0x%08x", *(unsigned long*)qbuf);
729 free(qbuf);
730 return -1;
731 }
733 buf->qemubuf = qbuf;
734 buf->qemubufsize = dlen;
736 return 0;
737 }
739 static int buffer_qemu(xc_interface *xch,
740 int fd, struct tailbuf_hvm *buf)
741 {
742 uint32_t qlen;
743 uint8_t *tmp;
745 if ( read_exact(fd, &qlen, sizeof(qlen)) ) {
746 ERROR("Error reading QEMU header length");
747 return -1;
748 }
750 if ( qlen > buf->qemubufsize ) {
751 if ( buf->qemubuf) {
752 tmp = realloc(buf->qemubuf, qlen);
753 if ( tmp )
754 buf->qemubuf = tmp;
755 else {
756 ERROR("Error reallocating QEMU state buffer");
757 return -1;
758 }
759 } else {
760 buf->qemubuf = malloc(qlen);
761 if ( !buf->qemubuf ) {
762 ERROR("Error allocating QEMU state buffer");
763 return -1;
764 }
765 }
766 }
767 buf->qemubufsize = qlen;
769 if ( read_exact(fd, buf->qemubuf, buf->qemubufsize) ) {
770 ERROR("Error reading QEMU state");
771 return -1;
772 }
774 return 0;
775 }
777 static int dump_qemu(xc_interface *xch, uint32_t dom, struct tailbuf_hvm *buf)
778 {
779 int saved_errno;
780 char path[256];
781 FILE *fp;
783 sprintf(path, "/var/lib/xen/qemu-save.%u", dom);
784 fp = fopen(path, "wb");
785 if ( !fp )
786 return -1;
788 DPRINTF("Writing %d bytes of QEMU data\n", buf->qemubufsize);
789 if ( fwrite(buf->qemubuf, 1, buf->qemubufsize, fp) != buf->qemubufsize) {
790 saved_errno = errno;
791 fclose(fp);
792 errno = saved_errno;
793 return -1;
794 }
796 fclose(fp);
798 return 0;
799 }
801 static int buffer_tail_hvm(xc_interface *xch, struct restore_ctx *ctx,
802 struct tailbuf_hvm *buf, int fd,
803 unsigned int max_vcpu_id, uint64_t vcpumap,
804 int ext_vcpucontext)
805 {
806 uint8_t *tmp;
807 unsigned char qemusig[21];
809 if ( read_exact(fd, buf->magicpfns, sizeof(buf->magicpfns)) ) {
810 ERROR("Error reading magic PFNs");
811 return -1;
812 }
814 if ( read_exact(fd, &buf->reclen, sizeof(buf->reclen)) ) {
815 ERROR("Error reading HVM params size");
816 return -1;
817 }
819 if ( buf->reclen > buf->hvmbufsize ) {
820 if ( buf->hvmbuf) {
821 tmp = realloc(buf->hvmbuf, buf->reclen);
822 if ( tmp ) {
823 buf->hvmbuf = tmp;
824 buf->hvmbufsize = buf->reclen;
825 } else {
826 ERROR("Error reallocating HVM param buffer");
827 return -1;
828 }
829 } else {
830 buf->hvmbuf = malloc(buf->reclen);
831 if ( !buf->hvmbuf ) {
832 ERROR("Error allocating HVM param buffer");
833 return -1;
834 }
835 buf->hvmbufsize = buf->reclen;
836 }
837 }
839 if ( read_exact(fd, buf->hvmbuf, buf->reclen) ) {
840 ERROR("Error reading HVM params");
841 return -1;
842 }
844 if ( read_exact(fd, qemusig, sizeof(qemusig)) ) {
845 ERROR("Error reading QEMU signature");
846 return -1;
847 }
849 /* The normal live-migration QEMU record has no length information.
850 * Short of reimplementing the QEMU parser, we're forced to just read
851 * until EOF. Remus gets around this by sending a different signature
852 * which includes a length prefix */
853 if ( !memcmp(qemusig, "QemuDeviceModelRecord", sizeof(qemusig)) )
854 return compat_buffer_qemu(xch, fd, buf);
855 else if ( !memcmp(qemusig, "RemusDeviceModelState", sizeof(qemusig)) )
856 return buffer_qemu(xch, fd, buf);
858 qemusig[20] = '\0';
859 ERROR("Invalid QEMU signature: %s", qemusig);
860 return -1;
861 }
863 static int buffer_tail_pv(xc_interface *xch, struct restore_ctx *ctx,
864 struct tailbuf_pv *buf, int fd,
865 unsigned int max_vcpu_id, uint64_t vcpumap,
866 int ext_vcpucontext)
867 {
868 unsigned int i;
869 size_t pfnlen, vcpulen;
870 struct domain_info_context *dinfo = &ctx->dinfo;
872 /* TODO: handle changing pfntab and vcpu counts */
873 /* PFN tab */
874 if ( read_exact(fd, &buf->pfncount, sizeof(buf->pfncount)) ||
875 (buf->pfncount > (1U << 28)) ) /* up to 1TB of address space */
876 {
877 ERROR("Error when reading pfn count");
878 return -1;
879 }
880 pfnlen = sizeof(unsigned long) * buf->pfncount;
881 if ( !(buf->pfntab) ) {
882 if ( !(buf->pfntab = malloc(pfnlen)) ) {
883 ERROR("Error allocating PFN tail buffer");
884 return -1;
885 }
886 }
887 // DPRINTF("Reading PFN tab: %d bytes\n", pfnlen);
888 if ( read_exact(fd, buf->pfntab, pfnlen) ) {
889 ERROR("Error when reading pfntab");
890 goto free_pfntab;
891 }
893 /* VCPU contexts */
894 buf->vcpucount = 0;
895 for (i = 0; i <= max_vcpu_id; i++) {
896 // DPRINTF("vcpumap: %llx, cpu: %d, bit: %llu\n", vcpumap, i, (vcpumap % (1ULL << i)));
897 if ( (!(vcpumap & (1ULL << i))) )
898 continue;
899 buf->vcpucount++;
900 }
901 // DPRINTF("VCPU count: %d\n", buf->vcpucount);
902 vcpulen = ((dinfo->guest_width == 8) ? sizeof(vcpu_guest_context_x86_64_t)
903 : sizeof(vcpu_guest_context_x86_32_t)) * buf->vcpucount;
904 if ( ext_vcpucontext )
905 vcpulen += 128 * buf->vcpucount;
907 if ( !(buf->vcpubuf) ) {
908 if ( !(buf->vcpubuf = malloc(vcpulen)) ) {
909 ERROR("Error allocating VCPU ctxt tail buffer");
910 goto free_pfntab;
911 }
912 }
913 // DPRINTF("Reading VCPUS: %d bytes\n", vcpulen);
914 if ( read_exact(fd, buf->vcpubuf, vcpulen) ) {
915 ERROR("Error when reading ctxt");
916 goto free_vcpus;
917 }
919 /* load shared_info_page */
920 // DPRINTF("Reading shared info: %lu bytes\n", PAGE_SIZE);
921 if ( read_exact(fd, buf->shared_info_page, PAGE_SIZE) ) {
922 ERROR("Error when reading shared info page");
923 goto free_vcpus;
924 }
926 return 0;
928 free_vcpus:
929 if (buf->vcpubuf) {
930 free (buf->vcpubuf);
931 buf->vcpubuf = NULL;
932 }
933 free_pfntab:
934 if (buf->pfntab) {
935 free (buf->pfntab);
936 buf->pfntab = NULL;
937 }
939 return -1;
940 }
942 static int buffer_tail(xc_interface *xch, struct restore_ctx *ctx,
943 tailbuf_t *buf, int fd, unsigned int max_vcpu_id,
944 uint64_t vcpumap, int ext_vcpucontext)
945 {
946 if ( buf->ishvm )
947 return buffer_tail_hvm(xch, ctx, &buf->u.hvm, fd, max_vcpu_id, vcpumap,
948 ext_vcpucontext);
949 else
950 return buffer_tail_pv(xch, ctx, &buf->u.pv, fd, max_vcpu_id, vcpumap,
951 ext_vcpucontext);
952 }
954 static void tailbuf_free_hvm(struct tailbuf_hvm *buf)
955 {
956 if ( buf->hvmbuf ) {
957 free(buf->hvmbuf);
958 buf->hvmbuf = NULL;
959 }
960 if ( buf->qemubuf ) {
961 free(buf->qemubuf);
962 buf->qemubuf = NULL;
963 }
964 }
966 static void tailbuf_free_pv(struct tailbuf_pv *buf)
967 {
968 if ( buf->vcpubuf ) {
969 free(buf->vcpubuf);
970 buf->vcpubuf = NULL;
971 }
972 if ( buf->pfntab ) {
973 free(buf->pfntab);
974 buf->pfntab = NULL;
975 }
976 }
978 static void tailbuf_free(tailbuf_t *buf)
979 {
980 if ( buf->ishvm )
981 tailbuf_free_hvm(&buf->u.hvm);
982 else
983 tailbuf_free_pv(&buf->u.pv);
984 }
986 typedef struct {
987 void* pages;
988 /* pages is of length nr_physpages, pfn_types is of length nr_pages */
989 unsigned int nr_physpages, nr_pages;
991 /* Types of the pfns in the current region */
992 unsigned long* pfn_types;
994 int verify;
996 int new_ctxt_format;
997 int max_vcpu_id;
998 uint64_t vcpumap;
999 uint64_t identpt;
1000 uint64_t vm86_tss;
1001 } pagebuf_t;
1003 static int pagebuf_init(pagebuf_t* buf)
1005 memset(buf, 0, sizeof(*buf));
1006 return 0;
1009 static void pagebuf_free(pagebuf_t* buf)
1011 if (buf->pages) {
1012 free(buf->pages);
1013 buf->pages = NULL;
1015 if(buf->pfn_types) {
1016 free(buf->pfn_types);
1017 buf->pfn_types = NULL;
1021 static int pagebuf_get_one(xc_interface *xch,
1022 pagebuf_t* buf, int fd, uint32_t dom)
1024 int count, countpages, oldcount, i;
1025 void* ptmp;
1027 if ( read_exact(fd, &count, sizeof(count)) )
1029 ERROR("Error when reading batch size");
1030 return -1;
1033 // DPRINTF("reading batch of %d pages\n", count);
1035 if (!count) {
1036 // DPRINTF("Last batch read\n");
1037 return 0;
1038 } else if (count == -1) {
1039 DPRINTF("Entering page verify mode\n");
1040 buf->verify = 1;
1041 return pagebuf_get_one(xch, buf, fd, dom);
1042 } else if (count == -2) {
1043 buf->new_ctxt_format = 1;
1044 if ( read_exact(fd, &buf->max_vcpu_id, sizeof(buf->max_vcpu_id)) ||
1045 buf->max_vcpu_id >= 64 || read_exact(fd, &buf->vcpumap,
1046 sizeof(uint64_t)) ) {
1047 ERROR("Error when reading max_vcpu_id");
1048 return -1;
1050 // DPRINTF("Max VCPU ID: %d, vcpumap: %llx\n", buf->max_vcpu_id, buf->vcpumap);
1051 return pagebuf_get_one(xch, buf, fd, dom);
1052 } else if (count == -3) {
1053 /* Skip padding 4 bytes then read the EPT identity PT location. */
1054 if ( read_exact(fd, &buf->identpt, sizeof(uint32_t)) ||
1055 read_exact(fd, &buf->identpt, sizeof(uint64_t)) )
1057 ERROR("error read the address of the EPT identity map");
1058 return -1;
1060 // DPRINTF("EPT identity map address: %llx\n", buf->identpt);
1061 return pagebuf_get_one(xch, buf, fd, dom);
1062 } else if ( count == -4 ) {
1063 /* Skip padding 4 bytes then read the vm86 TSS location. */
1064 if ( read_exact(fd, &buf->vm86_tss, sizeof(uint32_t)) ||
1065 read_exact(fd, &buf->vm86_tss, sizeof(uint64_t)) )
1067 ERROR("error read the address of the vm86 TSS");
1068 return -1;
1070 // DPRINTF("VM86 TSS location: %llx\n", buf->vm86_tss);
1071 return pagebuf_get_one(xch, buf, fd, dom);
1072 } else if ( count == -5 ) {
1073 DPRINTF("xc_domain_restore start tmem\n");
1074 if ( xc_tmem_restore(xch, dom, fd) ) {
1075 ERROR("error reading/restoring tmem");
1076 return -1;
1078 return pagebuf_get_one(xch, buf, fd, dom);
1080 else if ( count == -6 ) {
1081 if ( xc_tmem_restore_extra(xch, dom, fd) ) {
1082 ERROR("error reading/restoring tmem extra");
1083 return -1;
1085 return pagebuf_get_one(xch, buf, fd, dom);
1086 } else if ( count == -7 ) {
1087 uint32_t tsc_mode, khz, incarn;
1088 uint64_t nsec;
1089 if ( read_exact(fd, &tsc_mode, sizeof(uint32_t)) ||
1090 read_exact(fd, &nsec, sizeof(uint64_t)) ||
1091 read_exact(fd, &khz, sizeof(uint32_t)) ||
1092 read_exact(fd, &incarn, sizeof(uint32_t)) ||
1093 xc_domain_set_tsc_info(xch, dom, tsc_mode, nsec, khz, incarn) ) {
1094 ERROR("error reading/restoring tsc info");
1095 return -1;
1097 return pagebuf_get_one(xch, buf, fd, dom);
1098 } else if ( (count > MAX_BATCH_SIZE) || (count < 0) ) {
1099 ERROR("Max batch size exceeded (%d). Giving up.", count);
1100 return -1;
1103 oldcount = buf->nr_pages;
1104 buf->nr_pages += count;
1105 if (!buf->pfn_types) {
1106 if (!(buf->pfn_types = malloc(buf->nr_pages * sizeof(*(buf->pfn_types))))) {
1107 ERROR("Could not allocate PFN type buffer");
1108 return -1;
1110 } else {
1111 if (!(ptmp = realloc(buf->pfn_types, buf->nr_pages * sizeof(*(buf->pfn_types))))) {
1112 ERROR("Could not reallocate PFN type buffer");
1113 return -1;
1115 buf->pfn_types = ptmp;
1117 if ( read_exact(fd, buf->pfn_types + oldcount, count * sizeof(*(buf->pfn_types)))) {
1118 ERROR("Error when reading region pfn types");
1119 return -1;
1122 countpages = count;
1123 for (i = oldcount; i < buf->nr_pages; ++i)
1124 if ((buf->pfn_types[i] & XEN_DOMCTL_PFINFO_LTAB_MASK) == XEN_DOMCTL_PFINFO_XTAB)
1125 --countpages;
1127 if (!countpages)
1128 return count;
1130 oldcount = buf->nr_physpages;
1131 buf->nr_physpages += countpages;
1132 if (!buf->pages) {
1133 if (!(buf->pages = malloc(buf->nr_physpages * PAGE_SIZE))) {
1134 ERROR("Could not allocate page buffer");
1135 return -1;
1137 } else {
1138 if (!(ptmp = realloc(buf->pages, buf->nr_physpages * PAGE_SIZE))) {
1139 ERROR("Could not reallocate page buffer");
1140 return -1;
1142 buf->pages = ptmp;
1144 if ( read_exact(fd, buf->pages + oldcount * PAGE_SIZE, countpages * PAGE_SIZE) ) {
1145 ERROR("Error when reading pages");
1146 return -1;
1149 return count;
1152 static int pagebuf_get(xc_interface *xch, pagebuf_t* buf, int fd, uint32_t dom)
1154 int rc;
1156 buf->nr_physpages = buf->nr_pages = 0;
1158 do {
1159 rc = pagebuf_get_one(xch, buf, fd, dom);
1160 } while (rc > 0);
1162 if (rc < 0)
1163 pagebuf_free(buf);
1165 return rc;
1168 static int apply_batch(xc_interface *xch, uint32_t dom, struct restore_ctx *ctx,
1169 xen_pfn_t* region_mfn, unsigned long* pfn_type, int pae_extended_cr3,
1170 unsigned int hvm, struct xc_mmu* mmu,
1171 pagebuf_t* pagebuf, int curbatch, int superpages)
1173 int i, j, curpage;
1174 /* used by debug verify code */
1175 unsigned long buf[PAGE_SIZE/sizeof(unsigned long)];
1176 /* Our mapping of the current region (batch) */
1177 char *region_base;
1178 /* A temporary mapping, and a copy, of one frame of guest memory. */
1179 unsigned long *page = NULL;
1180 int nraces = 0;
1181 struct domain_info_context *dinfo = &ctx->dinfo;
1182 int* pfn_err = NULL;
1183 int rc = -1;
1185 unsigned long mfn, pfn, pagetype;
1187 j = pagebuf->nr_pages - curbatch;
1188 if (j > MAX_BATCH_SIZE)
1189 j = MAX_BATCH_SIZE;
1191 if (allocate_physmem(xch, dom, ctx, &pagebuf->pfn_types[curbatch],
1192 j, hvm, region_mfn, superpages) != 0)
1194 ERROR("allocate_physmem() failed\n");
1195 return -1;
1198 /* Map relevant mfns */
1199 pfn_err = calloc(j, sizeof(*pfn_err));
1200 region_base = xc_map_foreign_bulk(
1201 xch, dom, PROT_WRITE, region_mfn, pfn_err, j);
1203 if ( region_base == NULL )
1205 ERROR("map batch failed");
1206 free(pfn_err);
1207 return -1;
1210 for ( i = 0, curpage = -1; i < j; i++ )
1212 pfn = pagebuf->pfn_types[i + curbatch] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
1213 pagetype = pagebuf->pfn_types[i + curbatch] & XEN_DOMCTL_PFINFO_LTAB_MASK;
1215 if ( pagetype == XEN_DOMCTL_PFINFO_XTAB )
1216 /* a bogus/unmapped page: skip it */
1217 continue;
1219 if (pfn_err[i])
1221 ERROR("unexpected PFN mapping failure");
1222 goto err_mapped;
1225 ++curpage;
1227 if ( pfn > dinfo->p2m_size )
1229 ERROR("pfn out of range");
1230 goto err_mapped;
1233 pfn_type[pfn] = pagetype;
1235 mfn = ctx->p2m[pfn];
1237 /* In verify mode, we use a copy; otherwise we work in place */
1238 page = pagebuf->verify ? (void *)buf : (region_base + i*PAGE_SIZE);
1240 memcpy(page, pagebuf->pages + (curpage + curbatch) * PAGE_SIZE, PAGE_SIZE);
1242 pagetype &= XEN_DOMCTL_PFINFO_LTABTYPE_MASK;
1244 if ( (pagetype >= XEN_DOMCTL_PFINFO_L1TAB) &&
1245 (pagetype <= XEN_DOMCTL_PFINFO_L4TAB) )
1247 /*
1248 ** A page table page - need to 'uncanonicalize' it, i.e.
1249 ** replace all the references to pfns with the corresponding
1250 ** mfns for the new domain.
1251 **
1252 ** On PAE we need to ensure that PGDs are in MFNs < 4G, and
1253 ** so we may need to update the p2m after the main loop.
1254 ** Hence we defer canonicalization of L1s until then.
1255 */
1256 if ((ctx->pt_levels != 3) ||
1257 pae_extended_cr3 ||
1258 (pagetype != XEN_DOMCTL_PFINFO_L1TAB)) {
1260 if (!uncanonicalize_pagetable(xch, dom, ctx,
1261 page, superpages)) {
1262 /*
1263 ** Failing to uncanonicalize a page table can be ok
1264 ** under live migration since the pages type may have
1265 ** changed by now (and we'll get an update later).
1266 */
1267 DPRINTF("PT L%ld race on pfn=%08lx mfn=%08lx\n",
1268 pagetype >> 28, pfn, mfn);
1269 nraces++;
1270 continue;
1274 else if ( pagetype != XEN_DOMCTL_PFINFO_NOTAB )
1276 ERROR("Bogus page type %lx page table is out of range: "
1277 "i=%d p2m_size=%lu", pagetype, i, dinfo->p2m_size);
1278 goto err_mapped;
1281 if ( pagebuf->verify )
1283 int res = memcmp(buf, (region_base + i*PAGE_SIZE), PAGE_SIZE);
1284 if ( res )
1286 int v;
1288 DPRINTF("************** pfn=%lx type=%lx gotcs=%08lx "
1289 "actualcs=%08lx\n", pfn, pagebuf->pfn_types[pfn],
1290 csum_page(region_base + (i + curbatch)*PAGE_SIZE),
1291 csum_page(buf));
1293 for ( v = 0; v < 4; v++ )
1295 unsigned long *p = (unsigned long *)
1296 (region_base + i*PAGE_SIZE);
1297 if ( buf[v] != p[v] )
1298 DPRINTF(" %d: %08lx %08lx\n", v, buf[v], p[v]);
1303 if ( !hvm &&
1304 xc_add_mmu_update(xch, mmu,
1305 (((unsigned long long)mfn) << PAGE_SHIFT)
1306 | MMU_MACHPHYS_UPDATE, pfn) )
1308 ERROR("failed machpys update mfn=%lx pfn=%lx", mfn, pfn);
1309 goto err_mapped;
1311 } /* end of 'batch' for loop */
1313 rc = nraces;
1315 err_mapped:
1316 munmap(region_base, j*PAGE_SIZE);
1317 free(pfn_err);
1319 return rc;
1322 int xc_domain_restore(xc_interface *xch, int io_fd, uint32_t dom,
1323 unsigned int store_evtchn, unsigned long *store_mfn,
1324 unsigned int console_evtchn, unsigned long *console_mfn,
1325 unsigned int hvm, unsigned int pae, int superpages)
1327 DECLARE_DOMCTL;
1328 int rc = 1, frc, i, j, n, m, pae_extended_cr3 = 0, ext_vcpucontext = 0;
1329 unsigned long mfn, pfn;
1330 unsigned int prev_pc;
1331 int nraces = 0;
1333 /* The new domain's shared-info frame number. */
1334 unsigned long shared_info_frame;
1335 unsigned char shared_info_page[PAGE_SIZE]; /* saved contents from file */
1336 shared_info_any_t *old_shared_info =
1337 (shared_info_any_t *)shared_info_page;
1338 shared_info_any_t *new_shared_info;
1340 /* A copy of the CPU context of the guest. */
1341 vcpu_guest_context_any_t ctxt;
1343 /* A table containing the type of each PFN (/not/ MFN!). */
1344 unsigned long *pfn_type = NULL;
1346 /* A table of MFNs to map in the current region */
1347 xen_pfn_t *region_mfn = NULL;
1349 /* A copy of the pfn-to-mfn table frame list. */
1350 xen_pfn_t *p2m_frame_list = NULL;
1352 /* A temporary mapping of the guest's start_info page. */
1353 start_info_any_t *start_info;
1355 /* Our mapping of the current region (batch) */
1356 char *region_base;
1358 struct xc_mmu *mmu = NULL;
1360 struct mmuext_op pin[MAX_PIN_BATCH];
1361 unsigned int nr_pins;
1363 uint64_t vcpumap = 1ULL;
1364 unsigned int max_vcpu_id = 0;
1365 int new_ctxt_format = 0;
1367 pagebuf_t pagebuf;
1368 tailbuf_t tailbuf, tmptail;
1369 void* vcpup;
1371 static struct restore_ctx _ctx = {
1372 .live_p2m = NULL,
1373 .p2m = NULL,
1374 .no_superpage_mem = 0,
1375 };
1376 static struct restore_ctx *ctx = &_ctx;
1377 struct domain_info_context *dinfo = &ctx->dinfo;
1379 pagebuf_init(&pagebuf);
1380 memset(&tailbuf, 0, sizeof(tailbuf));
1381 tailbuf.ishvm = hvm;
1383 /* For info only */
1384 ctx->nr_pfns = 0;
1386 /* Always try to allocate 2M pages for HVM */
1387 if ( hvm )
1388 superpages = 1;
1390 if ( read_exact(io_fd, &dinfo->p2m_size, sizeof(unsigned long)) )
1392 ERROR("read: p2m_size");
1393 goto out;
1395 DPRINTF("xc_domain_restore start: p2m_size = %lx\n", dinfo->p2m_size);
1397 if ( !get_platform_info(xch, dom,
1398 &ctx->max_mfn, &ctx->hvirt_start, &ctx->pt_levels, &dinfo->guest_width) )
1400 ERROR("Unable to get platform info.");
1401 return 1;
1404 /* The *current* word size of the guest isn't very interesting; for now
1405 * assume the guest will be the same as we are. We'll fix that later
1406 * if we discover otherwise. */
1407 dinfo->guest_width = sizeof(unsigned long);
1408 ctx->pt_levels = (dinfo->guest_width == 8) ? 4 : (ctx->pt_levels == 2) ? 2 : 3;
1410 if ( !hvm )
1412 /* Load the p2m frame list, plus potential extended info chunk */
1413 p2m_frame_list = load_p2m_frame_list(xch, ctx,
1414 io_fd, &pae_extended_cr3, &ext_vcpucontext);
1415 if ( !p2m_frame_list )
1416 goto out;
1418 /* Now that we know the word size, tell Xen about it */
1419 memset(&domctl, 0, sizeof(domctl));
1420 domctl.domain = dom;
1421 domctl.cmd = XEN_DOMCTL_set_address_size;
1422 domctl.u.address_size.size = dinfo->guest_width * 8;
1423 frc = do_domctl(xch, &domctl);
1424 if ( frc != 0 )
1426 ERROR("Unable to set guest address size.");
1427 goto out;
1431 /* We want zeroed memory so use calloc rather than malloc. */
1432 ctx->p2m = calloc(dinfo->p2m_size, sizeof(xen_pfn_t));
1433 pfn_type = calloc(dinfo->p2m_size, sizeof(unsigned long));
1435 region_mfn = xc_memalign(PAGE_SIZE, ROUNDUP(
1436 MAX_BATCH_SIZE * sizeof(xen_pfn_t), PAGE_SHIFT));
1438 if ( (ctx->p2m == NULL) || (pfn_type == NULL) ||
1439 (region_mfn == NULL) )
1441 ERROR("memory alloc failed");
1442 errno = ENOMEM;
1443 goto out;
1446 memset(region_mfn, 0,
1447 ROUNDUP(MAX_BATCH_SIZE * sizeof(xen_pfn_t), PAGE_SHIFT));
1449 if ( lock_pages(region_mfn, sizeof(xen_pfn_t) * MAX_BATCH_SIZE) )
1451 ERROR("Could not lock region_mfn");
1452 goto out;
1455 /* Get the domain's shared-info frame. */
1456 domctl.cmd = XEN_DOMCTL_getdomaininfo;
1457 domctl.domain = (domid_t)dom;
1458 if ( xc_domctl(xch, &domctl) < 0 )
1460 ERROR("Could not get information on new domain");
1461 goto out;
1463 shared_info_frame = domctl.u.getdomaininfo.shared_info_frame;
1465 /* Mark all PFNs as invalid; we allocate on demand */
1466 for ( pfn = 0; pfn < dinfo->p2m_size; pfn++ )
1467 ctx->p2m[pfn] = INVALID_P2M_ENTRY;
1469 mmu = xc_alloc_mmu_updates(xch, dom);
1470 if ( mmu == NULL )
1472 ERROR("Could not initialise for MMU updates");
1473 goto out;
1476 xc_report_progress_start(xch, "Reloading memory pages", dinfo->p2m_size);
1478 /*
1479 * Now simply read each saved frame into its new machine frame.
1480 * We uncanonicalise page tables as we go.
1481 */
1482 prev_pc = 0;
1484 n = m = 0;
1485 loadpages:
1486 for ( ; ; )
1488 int j, curbatch;
1490 xc_report_progress_step(xch, n, dinfo->p2m_size);
1492 if ( !completed ) {
1493 pagebuf.nr_physpages = pagebuf.nr_pages = 0;
1494 if ( pagebuf_get_one(xch, &pagebuf, io_fd, dom) < 0 ) {
1495 ERROR("Error when reading batch\n");
1496 goto out;
1499 j = pagebuf.nr_pages;
1501 DPRINTF("batch %d\n",j);
1503 if ( j == 0 ) {
1504 /* catch vcpu updates */
1505 if (pagebuf.new_ctxt_format) {
1506 vcpumap = pagebuf.vcpumap;
1507 max_vcpu_id = pagebuf.max_vcpu_id;
1509 /* should this be deferred? does it change? */
1510 if ( pagebuf.identpt )
1511 xc_set_hvm_param(xch, dom, HVM_PARAM_IDENT_PT, pagebuf.identpt);
1512 if ( pagebuf.vm86_tss )
1513 xc_set_hvm_param(xch, dom, HVM_PARAM_VM86_TSS, pagebuf.vm86_tss);
1514 break; /* our work here is done */
1517 /* break pagebuf into batches */
1518 curbatch = 0;
1519 while ( curbatch < j ) {
1520 int brc;
1522 brc = apply_batch(xch, dom, ctx, region_mfn, pfn_type,
1523 pae_extended_cr3, hvm, mmu, &pagebuf, curbatch, superpages);
1524 if ( brc < 0 )
1525 goto out;
1527 nraces += brc;
1529 curbatch += MAX_BATCH_SIZE;
1532 pagebuf.nr_physpages = pagebuf.nr_pages = 0;
1534 n += j; /* crude stats */
1536 /*
1537 * Discard cache for portion of file read so far up to last
1538 * page boundary every 16MB or so.
1539 */
1540 m += j;
1541 if ( m > MAX_PAGECACHE_USAGE )
1543 discard_file_cache(xch, io_fd, 0 /* no flush */);
1544 m = 0;
1548 /*
1549 * Ensure we flush all machphys updates before potential PAE-specific
1550 * reallocations below.
1551 */
1552 if ( !hvm && xc_flush_mmu_updates(xch, mmu) )
1554 ERROR("Error doing flush_mmu_updates()");
1555 goto out;
1558 // DPRINTF("Received all pages (%d races)\n", nraces);
1560 if ( !completed ) {
1561 int flags = 0;
1563 if ( buffer_tail(xch, ctx, &tailbuf, io_fd, max_vcpu_id, vcpumap,
1564 ext_vcpucontext) < 0 ) {
1565 ERROR ("error buffering image tail");
1566 goto out;
1568 completed = 1;
1569 /* shift into nonblocking mode for the remainder */
1570 if ( (flags = fcntl(io_fd, F_GETFL,0)) < 0 )
1571 flags = 0;
1572 fcntl(io_fd, F_SETFL, flags | O_NONBLOCK);
1575 // DPRINTF("Buffered checkpoint\n");
1577 if ( pagebuf_get(xch, &pagebuf, io_fd, dom) ) {
1578 ERROR("error when buffering batch, finishing\n");
1579 goto finish;
1581 memset(&tmptail, 0, sizeof(tmptail));
1582 tmptail.ishvm = hvm;
1583 if ( buffer_tail(xch, ctx, &tmptail, io_fd, max_vcpu_id, vcpumap,
1584 ext_vcpucontext) < 0 ) {
1585 ERROR ("error buffering image tail, finishing");
1586 goto finish;
1588 tailbuf_free(&tailbuf);
1589 memcpy(&tailbuf, &tmptail, sizeof(tailbuf));
1591 goto loadpages;
1593 finish:
1594 if ( hvm )
1595 goto finish_hvm;
1597 if ( (ctx->pt_levels == 3) && !pae_extended_cr3 )
1599 /*
1600 ** XXX SMH on PAE we need to ensure PGDs are in MFNs < 4G. This
1601 ** is a little awkward and involves (a) finding all such PGDs and
1602 ** replacing them with 'lowmem' versions; (b) upating the p2m[]
1603 ** with the new info; and (c) canonicalizing all the L1s using the
1604 ** (potentially updated) p2m[].
1605 **
1606 ** This is relatively slow (and currently involves two passes through
1607 ** the pfn_type[] array), but at least seems to be correct. May wish
1608 ** to consider more complex approaches to optimize this later.
1609 */
1611 int j, k;
1613 /* First pass: find all L3TABs current in > 4G mfns and get new mfns */
1614 for ( i = 0; i < dinfo->p2m_size; i++ )
1616 if ( ((pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) ==
1617 XEN_DOMCTL_PFINFO_L3TAB) &&
1618 (ctx->p2m[i] > 0xfffffUL) )
1620 unsigned long new_mfn;
1621 uint64_t l3ptes[4];
1622 uint64_t *l3tab;
1624 l3tab = (uint64_t *)
1625 xc_map_foreign_range(xch, dom, PAGE_SIZE,
1626 PROT_READ, ctx->p2m[i]);
1628 for ( j = 0; j < 4; j++ )
1629 l3ptes[j] = l3tab[j];
1631 munmap(l3tab, PAGE_SIZE);
1633 new_mfn = xc_make_page_below_4G(xch, dom, ctx->p2m[i]);
1634 if ( !new_mfn )
1636 ERROR("Couldn't get a page below 4GB :-(");
1637 goto out;
1640 ctx->p2m[i] = new_mfn;
1641 if ( xc_add_mmu_update(xch, mmu,
1642 (((unsigned long long)new_mfn)
1643 << PAGE_SHIFT) |
1644 MMU_MACHPHYS_UPDATE, i) )
1646 ERROR("Couldn't m2p on PAE root pgdir");
1647 goto out;
1650 l3tab = (uint64_t *)
1651 xc_map_foreign_range(xch, dom, PAGE_SIZE,
1652 PROT_READ | PROT_WRITE, ctx->p2m[i]);
1654 for ( j = 0; j < 4; j++ )
1655 l3tab[j] = l3ptes[j];
1657 munmap(l3tab, PAGE_SIZE);
1661 /* Second pass: find all L1TABs and uncanonicalize them */
1662 j = 0;
1664 for ( i = 0; i < dinfo->p2m_size; i++ )
1666 if ( ((pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) ==
1667 XEN_DOMCTL_PFINFO_L1TAB) )
1669 region_mfn[j] = ctx->p2m[i];
1670 j++;
1673 if ( (i == (dinfo->p2m_size-1)) || (j == MAX_BATCH_SIZE) )
1675 region_base = xc_map_foreign_pages(
1676 xch, dom, PROT_READ | PROT_WRITE, region_mfn, j);
1677 if ( region_base == NULL )
1679 ERROR("map batch failed");
1680 goto out;
1683 for ( k = 0; k < j; k++ )
1685 if ( !uncanonicalize_pagetable(
1686 xch, dom, ctx,
1687 region_base + k*PAGE_SIZE, superpages) )
1689 ERROR("failed uncanonicalize pt!");
1690 goto out;
1694 munmap(region_base, j*PAGE_SIZE);
1695 j = 0;
1699 if ( xc_flush_mmu_updates(xch, mmu) )
1701 ERROR("Error doing xc_flush_mmu_updates()");
1702 goto out;
1706 /*
1707 * Pin page tables. Do this after writing to them as otherwise Xen
1708 * will barf when doing the type-checking.
1709 */
1710 nr_pins = 0;
1711 for ( i = 0; i < dinfo->p2m_size; i++ )
1713 if ( (pfn_type[i] & XEN_DOMCTL_PFINFO_LPINTAB) == 0 )
1714 continue;
1716 switch ( pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK )
1718 case XEN_DOMCTL_PFINFO_L1TAB:
1719 pin[nr_pins].cmd = MMUEXT_PIN_L1_TABLE;
1720 break;
1722 case XEN_DOMCTL_PFINFO_L2TAB:
1723 pin[nr_pins].cmd = MMUEXT_PIN_L2_TABLE;
1724 break;
1726 case XEN_DOMCTL_PFINFO_L3TAB:
1727 pin[nr_pins].cmd = MMUEXT_PIN_L3_TABLE;
1728 break;
1730 case XEN_DOMCTL_PFINFO_L4TAB:
1731 pin[nr_pins].cmd = MMUEXT_PIN_L4_TABLE;
1732 break;
1734 default:
1735 continue;
1738 pin[nr_pins].arg1.mfn = ctx->p2m[i];
1739 nr_pins++;
1741 /* Batch full? Then flush. */
1742 if ( nr_pins == MAX_PIN_BATCH )
1744 if ( xc_mmuext_op(xch, pin, nr_pins, dom) < 0 )
1746 ERROR("Failed to pin batch of %d page tables", nr_pins);
1747 goto out;
1749 nr_pins = 0;
1753 /* Flush final partial batch. */
1754 if ( (nr_pins != 0) && (xc_mmuext_op(xch, pin, nr_pins, dom) < 0) )
1756 ERROR("Failed to pin batch of %d page tables", nr_pins);
1757 goto out;
1760 DPRINTF("Memory reloaded (%ld pages)\n", ctx->nr_pfns);
1762 /* Get the list of PFNs that are not in the psuedo-phys map */
1764 int nr_frees = 0;
1766 for ( i = 0; i < tailbuf.u.pv.pfncount; i++ )
1768 unsigned long pfn = tailbuf.u.pv.pfntab[i];
1770 if ( ctx->p2m[pfn] != INVALID_P2M_ENTRY )
1772 /* pfn is not in physmap now, but was at some point during
1773 the save/migration process - need to free it */
1774 tailbuf.u.pv.pfntab[nr_frees++] = ctx->p2m[pfn];
1775 ctx->p2m[pfn] = INVALID_P2M_ENTRY; /* not in pseudo-physical map */
1779 if ( nr_frees > 0 )
1781 struct xen_memory_reservation reservation = {
1782 .nr_extents = nr_frees,
1783 .extent_order = 0,
1784 .domid = dom
1785 };
1786 set_xen_guest_handle(reservation.extent_start, tailbuf.u.pv.pfntab);
1788 if ( (frc = xc_memory_op(xch, XENMEM_decrease_reservation,
1789 &reservation)) != nr_frees )
1791 ERROR("Could not decrease reservation : %d", frc);
1792 goto out;
1794 else
1795 DPRINTF("Decreased reservation by %d pages\n", tailbuf.u.pv.pfncount);
1799 if ( lock_pages(&ctxt, sizeof(ctxt)) )
1801 ERROR("Unable to lock ctxt");
1802 return 1;
1805 vcpup = tailbuf.u.pv.vcpubuf;
1806 for ( i = 0; i <= max_vcpu_id; i++ )
1808 if ( !(vcpumap & (1ULL << i)) )
1809 continue;
1811 memcpy(&ctxt, vcpup, ((dinfo->guest_width == 8) ? sizeof(ctxt.x64)
1812 : sizeof(ctxt.x32)));
1813 vcpup += (dinfo->guest_width == 8) ? sizeof(ctxt.x64) : sizeof(ctxt.x32);
1815 DPRINTF("read VCPU %d\n", i);
1817 if ( !new_ctxt_format )
1818 SET_FIELD(&ctxt, flags, GET_FIELD(&ctxt, flags) | VGCF_online);
1820 if ( i == 0 )
1822 /*
1823 * Uncanonicalise the suspend-record frame number and poke
1824 * resume record.
1825 */
1826 pfn = GET_FIELD(&ctxt, user_regs.edx);
1827 if ( (pfn >= dinfo->p2m_size) ||
1828 (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB) )
1830 ERROR("Suspend record frame number is bad");
1831 goto out;
1833 mfn = ctx->p2m[pfn];
1834 SET_FIELD(&ctxt, user_regs.edx, mfn);
1835 start_info = xc_map_foreign_range(
1836 xch, dom, PAGE_SIZE, PROT_READ | PROT_WRITE, mfn);
1837 SET_FIELD(start_info, nr_pages, dinfo->p2m_size);
1838 SET_FIELD(start_info, shared_info, shared_info_frame<<PAGE_SHIFT);
1839 SET_FIELD(start_info, flags, 0);
1840 *store_mfn = ctx->p2m[GET_FIELD(start_info, store_mfn)];
1841 SET_FIELD(start_info, store_mfn, *store_mfn);
1842 SET_FIELD(start_info, store_evtchn, store_evtchn);
1843 *console_mfn = ctx->p2m[GET_FIELD(start_info, console.domU.mfn)];
1844 SET_FIELD(start_info, console.domU.mfn, *console_mfn);
1845 SET_FIELD(start_info, console.domU.evtchn, console_evtchn);
1846 munmap(start_info, PAGE_SIZE);
1848 /* Uncanonicalise each GDT frame number. */
1849 if ( GET_FIELD(&ctxt, gdt_ents) > 8192 )
1851 ERROR("GDT entry count out of range");
1852 goto out;
1855 for ( j = 0; (512*j) < GET_FIELD(&ctxt, gdt_ents); j++ )
1857 pfn = GET_FIELD(&ctxt, gdt_frames[j]);
1858 if ( (pfn >= dinfo->p2m_size) ||
1859 (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB) )
1861 ERROR("GDT frame number %i (0x%lx) is bad",
1862 j, (unsigned long)pfn);
1863 goto out;
1865 SET_FIELD(&ctxt, gdt_frames[j], ctx->p2m[pfn]);
1867 /* Uncanonicalise the page table base pointer. */
1868 pfn = UNFOLD_CR3(GET_FIELD(&ctxt, ctrlreg[3]));
1870 if ( pfn >= dinfo->p2m_size )
1872 ERROR("PT base is bad: pfn=%lu p2m_size=%lu type=%08lx",
1873 pfn, dinfo->p2m_size, pfn_type[pfn]);
1874 goto out;
1877 if ( (pfn_type[pfn] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) !=
1878 ((unsigned long)ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT) )
1880 ERROR("PT base is bad. pfn=%lu nr=%lu type=%08lx %08lx",
1881 pfn, dinfo->p2m_size, pfn_type[pfn],
1882 (unsigned long)ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT);
1883 goto out;
1885 SET_FIELD(&ctxt, ctrlreg[3], FOLD_CR3(ctx->p2m[pfn]));
1887 /* Guest pagetable (x86/64) stored in otherwise-unused CR1. */
1888 if ( (ctx->pt_levels == 4) && (ctxt.x64.ctrlreg[1] & 1) )
1890 pfn = UNFOLD_CR3(ctxt.x64.ctrlreg[1] & ~1);
1891 if ( pfn >= dinfo->p2m_size )
1893 ERROR("User PT base is bad: pfn=%lu p2m_size=%lu",
1894 pfn, dinfo->p2m_size);
1895 goto out;
1897 if ( (pfn_type[pfn] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) !=
1898 ((unsigned long)ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT) )
1900 ERROR("User PT base is bad. pfn=%lu nr=%lu type=%08lx %08lx",
1901 pfn, dinfo->p2m_size, pfn_type[pfn],
1902 (unsigned long)ctx->pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT);
1903 goto out;
1905 ctxt.x64.ctrlreg[1] = FOLD_CR3(ctx->p2m[pfn]);
1907 domctl.cmd = XEN_DOMCTL_setvcpucontext;
1908 domctl.domain = (domid_t)dom;
1909 domctl.u.vcpucontext.vcpu = i;
1910 set_xen_guest_handle(domctl.u.vcpucontext.ctxt, &ctxt.c);
1911 frc = xc_domctl(xch, &domctl);
1912 if ( frc != 0 )
1914 ERROR("Couldn't build vcpu%d", i);
1915 goto out;
1918 if ( !ext_vcpucontext )
1919 continue;
1920 memcpy(&domctl.u.ext_vcpucontext, vcpup, 128);
1921 vcpup += 128;
1922 domctl.cmd = XEN_DOMCTL_set_ext_vcpucontext;
1923 domctl.domain = dom;
1924 frc = xc_domctl(xch, &domctl);
1925 if ( frc != 0 )
1927 ERROR("Couldn't set extended vcpu%d info\n", i);
1928 goto out;
1932 memcpy(shared_info_page, tailbuf.u.pv.shared_info_page, PAGE_SIZE);
1934 DPRINTF("Completed checkpoint load\n");
1936 /* Restore contents of shared-info page. No checking needed. */
1937 new_shared_info = xc_map_foreign_range(
1938 xch, dom, PAGE_SIZE, PROT_WRITE, shared_info_frame);
1940 /* restore saved vcpu_info and arch specific info */
1941 MEMCPY_FIELD(new_shared_info, old_shared_info, vcpu_info);
1942 MEMCPY_FIELD(new_shared_info, old_shared_info, arch);
1944 /* clear any pending events and the selector */
1945 MEMSET_ARRAY_FIELD(new_shared_info, evtchn_pending, 0);
1946 for ( i = 0; i < XEN_LEGACY_MAX_VCPUS; i++ )
1947 SET_FIELD(new_shared_info, vcpu_info[i].evtchn_pending_sel, 0);
1949 /* mask event channels */
1950 MEMSET_ARRAY_FIELD(new_shared_info, evtchn_mask, 0xff);
1952 /* leave wallclock time. set by hypervisor */
1953 munmap(new_shared_info, PAGE_SIZE);
1955 /* Uncanonicalise the pfn-to-mfn table frame-number list. */
1956 for ( i = 0; i < P2M_FL_ENTRIES; i++ )
1958 pfn = p2m_frame_list[i];
1959 if ( (pfn >= dinfo->p2m_size) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB) )
1961 ERROR("PFN-to-MFN frame number %i (%#lx) is bad", i, pfn);
1962 goto out;
1964 p2m_frame_list[i] = ctx->p2m[pfn];
1967 /* Copy the P2M we've constructed to the 'live' P2M */
1968 if ( !(ctx->live_p2m = xc_map_foreign_pages(xch, dom, PROT_WRITE,
1969 p2m_frame_list, P2M_FL_ENTRIES)) )
1971 ERROR("Couldn't map p2m table");
1972 goto out;
1975 /* If the domain we're restoring has a different word size to ours,
1976 * we need to adjust the live_p2m assignment appropriately */
1977 if ( dinfo->guest_width > sizeof (xen_pfn_t) )
1978 for ( i = dinfo->p2m_size - 1; i >= 0; i-- )
1979 ((int64_t *)ctx->live_p2m)[i] = (long)ctx->p2m[i];
1980 else if ( dinfo->guest_width < sizeof (xen_pfn_t) )
1981 for ( i = 0; i < dinfo->p2m_size; i++ )
1982 ((uint32_t *)ctx->live_p2m)[i] = ctx->p2m[i];
1983 else
1984 memcpy(ctx->live_p2m, ctx->p2m, dinfo->p2m_size * sizeof(xen_pfn_t));
1985 munmap(ctx->live_p2m, P2M_FL_ENTRIES * PAGE_SIZE);
1987 DPRINTF("Domain ready to be built.\n");
1988 rc = 0;
1989 goto out;
1991 finish_hvm:
1992 /* Dump the QEMU state to a state file for QEMU to load */
1993 if ( dump_qemu(xch, dom, &tailbuf.u.hvm) ) {
1994 ERROR("Error dumping QEMU state to file");
1995 goto out;
1998 /* These comms pages need to be zeroed at the start of day */
1999 if ( xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[0]) ||
2000 xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[1]) ||
2001 xc_clear_domain_page(xch, dom, tailbuf.u.hvm.magicpfns[2]) )
2003 ERROR("error zeroing magic pages");
2004 goto out;
2007 if ( (frc = xc_set_hvm_param(xch, dom,
2008 HVM_PARAM_IOREQ_PFN, tailbuf.u.hvm.magicpfns[0]))
2009 || (frc = xc_set_hvm_param(xch, dom,
2010 HVM_PARAM_BUFIOREQ_PFN, tailbuf.u.hvm.magicpfns[1]))
2011 || (frc = xc_set_hvm_param(xch, dom,
2012 HVM_PARAM_STORE_PFN, tailbuf.u.hvm.magicpfns[2]))
2013 || (frc = xc_set_hvm_param(xch, dom,
2014 HVM_PARAM_PAE_ENABLED, pae))
2015 || (frc = xc_set_hvm_param(xch, dom,
2016 HVM_PARAM_STORE_EVTCHN,
2017 store_evtchn)) )
2019 ERROR("error setting HVM params: %i", frc);
2020 goto out;
2022 *store_mfn = tailbuf.u.hvm.magicpfns[2];
2024 frc = xc_domain_hvm_setcontext(xch, dom, tailbuf.u.hvm.hvmbuf,
2025 tailbuf.u.hvm.reclen);
2026 if ( frc )
2028 ERROR("error setting the HVM context");
2029 goto out;
2032 /* HVM success! */
2033 rc = 0;
2035 out:
2036 if ( (rc != 0) && (dom != 0) )
2037 xc_domain_destroy(xch, dom);
2038 free(mmu);
2039 free(ctx->p2m);
2040 free(pfn_type);
2041 tailbuf_free(&tailbuf);
2043 /* discard cache for save file */
2044 discard_file_cache(xch, io_fd, 1 /*flush*/);
2046 DPRINTF("Restore exit with rc=%d\n", rc);
2048 return rc;
2050 /*
2051 * Local variables:
2052 * mode: C
2053 * c-set-style: "BSD"
2054 * c-basic-offset: 4
2055 * tab-width: 4
2056 * indent-tabs-mode: nil
2057 * End:
2058 */