ia64/xen-unstable

view tools/libxc/xc_linux_restore.c @ 14114:59b8d5168cc1

Reduce impact of saving/restoring/dumping large domains on Dom0 memory
usage by means of fadvise64() to tell the OS to discard the cache
pages used for the save/dump file.

Signed-off-by: Simon Graham <Simon.Graham@stratus.com>
author Keir Fraser <keir@xensource.com>
date Sat Feb 24 14:48:17 2007 +0000 (2007-02-24)
parents f61992cb82fe
children 33d733c3649d
line source
1 /******************************************************************************
2 * xc_linux_restore.c
3 *
4 * Restore the state of a Linux session.
5 *
6 * Copyright (c) 2003, K A Fraser.
7 */
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include "xg_private.h"
13 #include "xg_save_restore.h"
14 #include "xc_dom.h"
16 /* max mfn of the current host machine */
17 static unsigned long max_mfn;
19 /* virtual starting address of the hypervisor */
20 static unsigned long hvirt_start;
22 /* #levels of page tables used by the currrent guest */
23 static unsigned int pt_levels;
25 /* total number of pages used by the current guest */
26 static unsigned long max_pfn;
28 /* Live mapping of the table mapping each PFN to its current MFN. */
29 static xen_pfn_t *live_p2m = NULL;
31 /* A table mapping each PFN to its new MFN. */
32 static xen_pfn_t *p2m = NULL;
34 /* A table of P2M mappings in the current region */
35 static xen_pfn_t *p2m_batch = NULL;
38 static ssize_t
39 read_exact(int fd, void *buf, size_t count)
40 {
41 int r = 0, s;
42 unsigned char *b = buf;
44 while (r < count) {
45 s = read(fd, &b[r], count - r);
46 if ((s == -1) && (errno == EINTR))
47 continue;
48 if (s <= 0) {
49 break;
50 }
51 r += s;
52 }
54 return (r == count) ? 1 : 0;
55 }
57 /*
58 ** In the state file (or during transfer), all page-table pages are
59 ** converted into a 'canonical' form where references to actual mfns
60 ** are replaced with references to the corresponding pfns.
61 ** This function inverts that operation, replacing the pfn values with
62 ** the (now known) appropriate mfn values.
63 */
64 static int uncanonicalize_pagetable(int xc_handle, uint32_t dom,
65 unsigned long type, void *page)
66 {
67 int i, pte_last;
68 unsigned long pfn;
69 uint64_t pte;
70 int nr_mfns = 0;
72 pte_last = PAGE_SIZE / ((pt_levels == 2)? 4 : 8);
74 /* First pass: work out how many (if any) MFNs we need to alloc */
75 for(i = 0; i < pte_last; i++) {
77 if(pt_levels == 2)
78 pte = ((uint32_t *)page)[i];
79 else
80 pte = ((uint64_t *)page)[i];
82 /* XXX SMH: below needs fixing for PROT_NONE etc */
83 if(!(pte & _PAGE_PRESENT))
84 continue;
86 pfn = (pte >> PAGE_SHIFT) & MFN_MASK_X86;
88 if(pfn >= max_pfn) {
89 /* This "page table page" is probably not one; bail. */
90 ERROR("Frame number in type %lu page table is out of range: "
91 "i=%d pfn=0x%lx max_pfn=%lu",
92 type >> 28, i, pfn, max_pfn);
93 return 0;
94 }
96 if(p2m[pfn] == INVALID_P2M_ENTRY) {
97 /* Have a 'valid' PFN without a matching MFN - need to alloc */
98 p2m_batch[nr_mfns++] = pfn;
99 }
100 }
103 /* Alllocate the requistite number of mfns */
104 if (nr_mfns && xc_domain_memory_populate_physmap(
105 xc_handle, dom, nr_mfns, 0, 0, p2m_batch) != 0) {
106 ERROR("Failed to allocate memory for batch.!\n");
107 errno = ENOMEM;
108 return 0;
109 }
111 /* Second pass: uncanonicalize each present PTE */
112 nr_mfns = 0;
113 for(i = 0; i < pte_last; i++) {
115 if(pt_levels == 2)
116 pte = ((uint32_t *)page)[i];
117 else
118 pte = ((uint64_t *)page)[i];
120 /* XXX SMH: below needs fixing for PROT_NONE etc */
121 if(!(pte & _PAGE_PRESENT))
122 continue;
124 pfn = (pte >> PAGE_SHIFT) & MFN_MASK_X86;
126 if(p2m[pfn] == INVALID_P2M_ENTRY)
127 p2m[pfn] = p2m_batch[nr_mfns++];
129 pte &= ~MADDR_MASK_X86;
130 pte |= (uint64_t)p2m[pfn] << PAGE_SHIFT;
132 if(pt_levels == 2)
133 ((uint32_t *)page)[i] = (uint32_t)pte;
134 else
135 ((uint64_t *)page)[i] = (uint64_t)pte;
136 }
138 return 1;
139 }
141 int xc_linux_restore(int xc_handle, int io_fd,
142 uint32_t dom, unsigned long nr_pfns,
143 unsigned int store_evtchn, unsigned long *store_mfn,
144 unsigned int console_evtchn, unsigned long *console_mfn)
145 {
146 DECLARE_DOMCTL;
147 int rc = 1, i, n, m, pae_extended_cr3 = 0;
148 unsigned long mfn, pfn;
149 unsigned int prev_pc, this_pc;
150 int verify = 0;
151 int nraces = 0;
153 /* The new domain's shared-info frame number. */
154 unsigned long shared_info_frame;
155 unsigned char shared_info_page[PAGE_SIZE]; /* saved contents from file */
156 shared_info_t *shared_info = (shared_info_t *)shared_info_page;
158 /* A copy of the CPU context of the guest. */
159 vcpu_guest_context_t ctxt;
161 /* A table containing the type of each PFN (/not/ MFN!). */
162 unsigned long *pfn_type = NULL;
164 /* A table of MFNs to map in the current region */
165 xen_pfn_t *region_mfn = NULL;
167 /* Types of the pfns in the current region */
168 unsigned long region_pfn_type[MAX_BATCH_SIZE];
170 /* A temporary mapping, and a copy, of one frame of guest memory. */
171 unsigned long *page = NULL;
173 /* A copy of the pfn-to-mfn table frame list. */
174 xen_pfn_t *p2m_frame_list = NULL;
176 /* A temporary mapping of the guest's start_info page. */
177 start_info_t *start_info;
179 /* Our mapping of the current region (batch) */
180 char *region_base;
182 xc_mmu_t *mmu = NULL;
184 /* used by debug verify code */
185 unsigned long buf[PAGE_SIZE/sizeof(unsigned long)];
187 struct mmuext_op pin[MAX_PIN_BATCH];
188 unsigned int nr_pins;
191 max_pfn = nr_pfns;
193 DPRINTF("xc_linux_restore start: max_pfn = %lx\n", max_pfn);
196 if(!get_platform_info(xc_handle, dom,
197 &max_mfn, &hvirt_start, &pt_levels)) {
198 ERROR("Unable to get platform info.");
199 return 1;
200 }
202 if (lock_pages(&ctxt, sizeof(ctxt))) {
203 /* needed for build domctl, but might as well do early */
204 ERROR("Unable to lock ctxt");
205 return 1;
206 }
208 if (!(p2m_frame_list = malloc(P2M_FL_SIZE))) {
209 ERROR("Couldn't allocate p2m_frame_list array");
210 goto out;
211 }
213 /* Read first entry of P2M list, or extended-info signature (~0UL). */
214 if (!read_exact(io_fd, p2m_frame_list, sizeof(long))) {
215 ERROR("read extended-info signature failed");
216 goto out;
217 }
219 if (p2m_frame_list[0] == ~0UL) {
220 uint32_t tot_bytes;
222 /* Next 4 bytes: total size of following extended info. */
223 if (!read_exact(io_fd, &tot_bytes, sizeof(tot_bytes))) {
224 ERROR("read extended-info size failed");
225 goto out;
226 }
228 while (tot_bytes) {
229 uint32_t chunk_bytes;
230 char chunk_sig[4];
232 /* 4-character chunk signature + 4-byte remaining chunk size. */
233 if (!read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) ||
234 !read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes))) {
235 ERROR("read extended-info chunk signature failed");
236 goto out;
237 }
238 tot_bytes -= 8;
240 /* VCPU context structure? */
241 if (!strncmp(chunk_sig, "vcpu", 4)) {
242 if (!read_exact(io_fd, &ctxt, sizeof(ctxt))) {
243 ERROR("read extended-info vcpu context failed");
244 goto out;
245 }
246 tot_bytes -= sizeof(struct vcpu_guest_context);
247 chunk_bytes -= sizeof(struct vcpu_guest_context);
249 if (ctxt.vm_assist & (1UL << VMASST_TYPE_pae_extended_cr3))
250 pae_extended_cr3 = 1;
251 }
253 /* Any remaining bytes of this chunk: read and discard. */
254 while (chunk_bytes) {
255 unsigned long sz = chunk_bytes;
256 if ( sz > P2M_FL_SIZE )
257 sz = P2M_FL_SIZE;
258 if (!read_exact(io_fd, p2m_frame_list, sz)) {
259 ERROR("read-and-discard extended-info chunk bytes failed");
260 goto out;
261 }
262 chunk_bytes -= sz;
263 tot_bytes -= sz;
264 }
265 }
267 /* Now read the real first entry of P2M list. */
268 if (!read_exact(io_fd, p2m_frame_list, sizeof(long))) {
269 ERROR("read first entry of p2m_frame_list failed");
270 goto out;
271 }
272 }
274 /* First entry is already read into the p2m array. */
275 if (!read_exact(io_fd, &p2m_frame_list[1], P2M_FL_SIZE - sizeof(long))) {
276 ERROR("read p2m_frame_list failed");
277 goto out;
278 }
280 /* We want zeroed memory so use calloc rather than malloc. */
281 p2m = calloc(max_pfn, sizeof(xen_pfn_t));
282 pfn_type = calloc(max_pfn, sizeof(unsigned long));
283 region_mfn = calloc(MAX_BATCH_SIZE, sizeof(xen_pfn_t));
284 p2m_batch = calloc(MAX_BATCH_SIZE, sizeof(xen_pfn_t));
286 if ((p2m == NULL) || (pfn_type == NULL) ||
287 (region_mfn == NULL) || (p2m_batch == NULL)) {
288 ERROR("memory alloc failed");
289 errno = ENOMEM;
290 goto out;
291 }
293 if (lock_pages(region_mfn, sizeof(xen_pfn_t) * MAX_BATCH_SIZE)) {
294 ERROR("Could not lock region_mfn");
295 goto out;
296 }
298 if (lock_pages(p2m_batch, sizeof(xen_pfn_t) * MAX_BATCH_SIZE)) {
299 ERROR("Could not lock p2m_batch");
300 goto out;
301 }
303 /* Get the domain's shared-info frame. */
304 domctl.cmd = XEN_DOMCTL_getdomaininfo;
305 domctl.domain = (domid_t)dom;
306 if (xc_domctl(xc_handle, &domctl) < 0) {
307 ERROR("Could not get information on new domain");
308 goto out;
309 }
310 shared_info_frame = domctl.u.getdomaininfo.shared_info_frame;
312 if (xc_domain_setmaxmem(xc_handle, dom, PFN_TO_KB(max_pfn)) != 0) {
313 errno = ENOMEM;
314 goto out;
315 }
317 /* Mark all PFNs as invalid; we allocate on demand */
318 for ( pfn = 0; pfn < max_pfn; pfn++ )
319 p2m[pfn] = INVALID_P2M_ENTRY;
321 if(!(mmu = xc_init_mmu_updates(xc_handle, dom))) {
322 ERROR("Could not initialise for MMU updates");
323 goto out;
324 }
326 DPRINTF("Reloading memory pages: 0%%\n");
328 /*
329 * Now simply read each saved frame into its new machine frame.
330 * We uncanonicalise page tables as we go.
331 */
332 prev_pc = 0;
334 n = m = 0;
335 while (1) {
337 int j, nr_mfns = 0;
339 this_pc = (n * 100) / max_pfn;
340 if ( (this_pc - prev_pc) >= 5 )
341 {
342 PPRINTF("\b\b\b\b%3d%%", this_pc);
343 prev_pc = this_pc;
344 }
346 if (!read_exact(io_fd, &j, sizeof(int))) {
347 ERROR("Error when reading batch size");
348 goto out;
349 }
351 PPRINTF("batch %d\n",j);
353 if (j == -1) {
354 verify = 1;
355 DPRINTF("Entering page verify mode\n");
356 continue;
357 }
359 if (j == 0)
360 break; /* our work here is done */
362 if (j > MAX_BATCH_SIZE) {
363 ERROR("Max batch size exceeded. Giving up.");
364 goto out;
365 }
367 if (!read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long))) {
368 ERROR("Error when reading region pfn types");
369 goto out;
370 }
372 /* First pass for this batch: work out how much memory to alloc */
373 nr_mfns = 0;
374 for ( i = 0; i < j; i++ )
375 {
376 unsigned long pfn, pagetype;
377 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
378 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
380 if ( (pagetype != XEN_DOMCTL_PFINFO_XTAB) &&
381 (p2m[pfn] == INVALID_P2M_ENTRY) )
382 {
383 /* Have a live PFN which hasn't had an MFN allocated */
384 p2m_batch[nr_mfns++] = pfn;
385 }
386 }
389 /* Now allocate a bunch of mfns for this batch */
390 if (nr_mfns && xc_domain_memory_populate_physmap(
391 xc_handle, dom, nr_mfns, 0, 0, p2m_batch) != 0) {
392 ERROR("Failed to allocate memory for batch.!\n");
393 errno = ENOMEM;
394 goto out;
395 }
397 /* Second pass for this batch: update p2m[] and region_mfn[] */
398 nr_mfns = 0;
399 for ( i = 0; i < j; i++ )
400 {
401 unsigned long pfn, pagetype;
402 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
403 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
405 if ( pagetype == XEN_DOMCTL_PFINFO_XTAB)
406 region_mfn[i] = ~0UL; /* map will fail but we don't care */
407 else
408 {
409 if (p2m[pfn] == INVALID_P2M_ENTRY) {
410 /* We just allocated a new mfn above; update p2m */
411 p2m[pfn] = p2m_batch[nr_mfns++];
412 }
414 /* setup region_mfn[] for batch map */
415 region_mfn[i] = p2m[pfn];
416 }
417 }
419 /* Map relevant mfns */
420 region_base = xc_map_foreign_batch(
421 xc_handle, dom, PROT_WRITE, region_mfn, j);
423 if ( region_base == NULL )
424 {
425 ERROR("map batch failed");
426 goto out;
427 }
429 for ( i = 0; i < j; i++ )
430 {
431 void *page;
432 unsigned long pagetype;
434 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
435 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
437 if ( pagetype == XEN_DOMCTL_PFINFO_XTAB )
438 /* a bogus/unmapped page: skip it */
439 continue;
441 if ( pfn > max_pfn )
442 {
443 ERROR("pfn out of range");
444 goto out;
445 }
447 pfn_type[pfn] = pagetype;
449 mfn = p2m[pfn];
451 /* In verify mode, we use a copy; otherwise we work in place */
452 page = verify ? (void *)buf : (region_base + i*PAGE_SIZE);
454 if (!read_exact(io_fd, page, PAGE_SIZE)) {
455 ERROR("Error when reading page (type was %lx)", pagetype);
456 goto out;
457 }
459 pagetype &= XEN_DOMCTL_PFINFO_LTABTYPE_MASK;
461 if ( (pagetype >= XEN_DOMCTL_PFINFO_L1TAB) &&
462 (pagetype <= XEN_DOMCTL_PFINFO_L4TAB) )
463 {
464 /*
465 ** A page table page - need to 'uncanonicalize' it, i.e.
466 ** replace all the references to pfns with the corresponding
467 ** mfns for the new domain.
468 **
469 ** On PAE we need to ensure that PGDs are in MFNs < 4G, and
470 ** so we may need to update the p2m after the main loop.
471 ** Hence we defer canonicalization of L1s until then.
472 */
473 if ((pt_levels != 3) ||
474 pae_extended_cr3 ||
475 (pagetype != XEN_DOMCTL_PFINFO_L1TAB)) {
477 if (!uncanonicalize_pagetable(xc_handle, dom,
478 pagetype, page)) {
479 /*
480 ** Failing to uncanonicalize a page table can be ok
481 ** under live migration since the pages type may have
482 ** changed by now (and we'll get an update later).
483 */
484 DPRINTF("PT L%ld race on pfn=%08lx mfn=%08lx\n",
485 pagetype >> 28, pfn, mfn);
486 nraces++;
487 continue;
488 }
489 }
490 }
491 else if ( pagetype != XEN_DOMCTL_PFINFO_NOTAB )
492 {
493 ERROR("Bogus page type %lx page table is out of range: "
494 "i=%d max_pfn=%lu", pagetype, i, max_pfn);
495 goto out;
497 }
500 if (verify) {
502 int res = memcmp(buf, (region_base + i*PAGE_SIZE), PAGE_SIZE);
504 if (res) {
506 int v;
508 DPRINTF("************** pfn=%lx type=%lx gotcs=%08lx "
509 "actualcs=%08lx\n", pfn, pfn_type[pfn],
510 csum_page(region_base + i*PAGE_SIZE),
511 csum_page(buf));
513 for (v = 0; v < 4; v++) {
515 unsigned long *p = (unsigned long *)
516 (region_base + i*PAGE_SIZE);
517 if (buf[v] != p[v])
518 DPRINTF(" %d: %08lx %08lx\n", v, buf[v], p[v]);
519 }
520 }
521 }
523 if (xc_add_mmu_update(xc_handle, mmu,
524 (((unsigned long long)mfn) << PAGE_SHIFT)
525 | MMU_MACHPHYS_UPDATE, pfn)) {
526 ERROR("failed machpys update mfn=%lx pfn=%lx", mfn, pfn);
527 goto out;
528 }
529 } /* end of 'batch' for loop */
531 munmap(region_base, j*PAGE_SIZE);
532 n+= j; /* crude stats */
534 /*
535 * Discard cache for portion of file read so far up to last
536 * page boundary every 16MB or so.
537 */
538 m += j;
539 if ( m > MAX_PAGECACHE_USAGE )
540 {
541 discard_file_cache(io_fd, 0 /* no flush */);
542 m = 0;
543 }
544 }
546 /*
547 * Ensure we flush all machphys updates before potential PAE-specific
548 * reallocations below.
549 */
550 if (xc_finish_mmu_updates(xc_handle, mmu)) {
551 ERROR("Error doing finish_mmu_updates()");
552 goto out;
553 }
555 DPRINTF("Received all pages (%d races)\n", nraces);
557 if ((pt_levels == 3) && !pae_extended_cr3) {
559 /*
560 ** XXX SMH on PAE we need to ensure PGDs are in MFNs < 4G. This
561 ** is a little awkward and involves (a) finding all such PGDs and
562 ** replacing them with 'lowmem' versions; (b) upating the p2m[]
563 ** with the new info; and (c) canonicalizing all the L1s using the
564 ** (potentially updated) p2m[].
565 **
566 ** This is relatively slow (and currently involves two passes through
567 ** the pfn_type[] array), but at least seems to be correct. May wish
568 ** to consider more complex approaches to optimize this later.
569 */
571 int j, k;
573 /* First pass: find all L3TABs current in > 4G mfns and get new mfns */
574 for ( i = 0; i < max_pfn; i++ )
575 {
576 if ( ((pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) ==
577 XEN_DOMCTL_PFINFO_L3TAB) &&
578 (p2m[i] > 0xfffffUL) )
579 {
580 unsigned long new_mfn;
581 uint64_t l3ptes[4];
582 uint64_t *l3tab;
584 l3tab = (uint64_t *)
585 xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
586 PROT_READ, p2m[i]);
588 for(j = 0; j < 4; j++)
589 l3ptes[j] = l3tab[j];
591 munmap(l3tab, PAGE_SIZE);
593 if (!(new_mfn=xc_make_page_below_4G(xc_handle, dom, p2m[i]))) {
594 ERROR("Couldn't get a page below 4GB :-(");
595 goto out;
596 }
598 p2m[i] = new_mfn;
599 if (xc_add_mmu_update(xc_handle, mmu,
600 (((unsigned long long)new_mfn)
601 << PAGE_SHIFT) |
602 MMU_MACHPHYS_UPDATE, i)) {
603 ERROR("Couldn't m2p on PAE root pgdir");
604 goto out;
605 }
607 l3tab = (uint64_t *)
608 xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
609 PROT_READ | PROT_WRITE, p2m[i]);
611 for(j = 0; j < 4; j++)
612 l3tab[j] = l3ptes[j];
614 munmap(l3tab, PAGE_SIZE);
616 }
617 }
619 /* Second pass: find all L1TABs and uncanonicalize them */
620 j = 0;
622 for ( i = 0; i < max_pfn; i++ )
623 {
624 if ( ((pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) ==
625 XEN_DOMCTL_PFINFO_L1TAB) )
626 {
627 region_mfn[j] = p2m[i];
628 j++;
629 }
631 if(i == (max_pfn-1) || j == MAX_BATCH_SIZE) {
633 if (!(region_base = xc_map_foreign_batch(
634 xc_handle, dom, PROT_READ | PROT_WRITE,
635 region_mfn, j))) {
636 ERROR("map batch failed");
637 goto out;
638 }
640 for(k = 0; k < j; k++) {
641 if(!uncanonicalize_pagetable(xc_handle, dom,
642 XEN_DOMCTL_PFINFO_L1TAB,
643 region_base + k*PAGE_SIZE)) {
644 ERROR("failed uncanonicalize pt!");
645 goto out;
646 }
647 }
649 munmap(region_base, j*PAGE_SIZE);
650 j = 0;
651 }
652 }
654 if (xc_finish_mmu_updates(xc_handle, mmu)) {
655 ERROR("Error doing finish_mmu_updates()");
656 goto out;
657 }
658 }
660 /*
661 * Pin page tables. Do this after writing to them as otherwise Xen
662 * will barf when doing the type-checking.
663 */
664 nr_pins = 0;
665 for ( i = 0; i < max_pfn; i++ )
666 {
667 if ( (pfn_type[i] & XEN_DOMCTL_PFINFO_LPINTAB) == 0 )
668 continue;
670 switch ( pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK )
671 {
672 case XEN_DOMCTL_PFINFO_L1TAB:
673 pin[nr_pins].cmd = MMUEXT_PIN_L1_TABLE;
674 break;
676 case XEN_DOMCTL_PFINFO_L2TAB:
677 pin[nr_pins].cmd = MMUEXT_PIN_L2_TABLE;
678 break;
680 case XEN_DOMCTL_PFINFO_L3TAB:
681 pin[nr_pins].cmd = MMUEXT_PIN_L3_TABLE;
682 break;
684 case XEN_DOMCTL_PFINFO_L4TAB:
685 pin[nr_pins].cmd = MMUEXT_PIN_L4_TABLE;
686 break;
688 default:
689 continue;
690 }
692 pin[nr_pins].arg1.mfn = p2m[i];
693 nr_pins++;
695 /* Batch full? Then flush. */
696 if (nr_pins == MAX_PIN_BATCH) {
697 if (xc_mmuext_op(xc_handle, pin, nr_pins, dom) < 0) {
698 ERROR("Failed to pin batch of %d page tables", nr_pins);
699 goto out;
700 }
701 nr_pins = 0;
702 }
703 }
705 /* Flush final partial batch. */
706 if ((nr_pins != 0) && (xc_mmuext_op(xc_handle, pin, nr_pins, dom) < 0)) {
707 ERROR("Failed to pin batch of %d page tables", nr_pins);
708 goto out;
709 }
711 DPRINTF("\b\b\b\b100%%\n");
712 DPRINTF("Memory reloaded.\n");
714 /* Get the list of PFNs that are not in the psuedo-phys map */
715 {
716 unsigned int count;
717 unsigned long *pfntab;
718 int nr_frees, rc;
720 if (!read_exact(io_fd, &count, sizeof(count))) {
721 ERROR("Error when reading pfn count");
722 goto out;
723 }
725 if(!(pfntab = malloc(sizeof(unsigned long) * count))) {
726 ERROR("Out of memory");
727 goto out;
728 }
730 if (!read_exact(io_fd, pfntab, sizeof(unsigned long)*count)) {
731 ERROR("Error when reading pfntab");
732 goto out;
733 }
735 nr_frees = 0;
736 for (i = 0; i < count; i++) {
738 unsigned long pfn = pfntab[i];
740 if(p2m[pfn] != INVALID_P2M_ENTRY) {
741 /* pfn is not in physmap now, but was at some point during
742 the save/migration process - need to free it */
743 pfntab[nr_frees++] = p2m[pfn];
744 p2m[pfn] = INVALID_P2M_ENTRY; // not in pseudo-physical map
745 }
746 }
748 if (nr_frees > 0) {
750 struct xen_memory_reservation reservation = {
751 .nr_extents = nr_frees,
752 .extent_order = 0,
753 .domid = dom
754 };
755 set_xen_guest_handle(reservation.extent_start, pfntab);
757 if ((rc = xc_memory_op(xc_handle, XENMEM_decrease_reservation,
758 &reservation)) != nr_frees) {
759 ERROR("Could not decrease reservation : %d", rc);
760 goto out;
761 } else
762 DPRINTF("Decreased reservation by %d pages\n", count);
763 }
764 }
766 if (!read_exact(io_fd, &ctxt, sizeof(ctxt)) ||
767 !read_exact(io_fd, shared_info_page, PAGE_SIZE)) {
768 ERROR("Error when reading ctxt or shared info page");
769 goto out;
770 }
772 /* Uncanonicalise the suspend-record frame number and poke resume rec. */
773 pfn = ctxt.user_regs.edx;
774 if ((pfn >= max_pfn) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB)) {
775 ERROR("Suspend record frame number is bad");
776 goto out;
777 }
778 ctxt.user_regs.edx = mfn = p2m[pfn];
779 start_info = xc_map_foreign_range(
780 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE, mfn);
781 start_info->nr_pages = max_pfn;
782 start_info->shared_info = shared_info_frame << PAGE_SHIFT;
783 start_info->flags = 0;
784 *store_mfn = start_info->store_mfn = p2m[start_info->store_mfn];
785 start_info->store_evtchn = store_evtchn;
786 start_info->console.domU.mfn = p2m[start_info->console.domU.mfn];
787 start_info->console.domU.evtchn = console_evtchn;
788 *console_mfn = start_info->console.domU.mfn;
789 munmap(start_info, PAGE_SIZE);
791 /* Uncanonicalise each GDT frame number. */
792 if (ctxt.gdt_ents > 8192) {
793 ERROR("GDT entry count out of range");
794 goto out;
795 }
797 for (i = 0; (512*i) < ctxt.gdt_ents; i++) {
798 pfn = ctxt.gdt_frames[i];
799 if ((pfn >= max_pfn) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB)) {
800 ERROR("GDT frame number is bad");
801 goto out;
802 }
803 ctxt.gdt_frames[i] = p2m[pfn];
804 }
806 /* Uncanonicalise the page table base pointer. */
807 pfn = xen_cr3_to_pfn(ctxt.ctrlreg[3]);
809 if (pfn >= max_pfn) {
810 ERROR("PT base is bad: pfn=%lu max_pfn=%lu type=%08lx",
811 pfn, max_pfn, pfn_type[pfn]);
812 goto out;
813 }
815 if ( (pfn_type[pfn] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) !=
816 ((unsigned long)pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT) ) {
817 ERROR("PT base is bad. pfn=%lu nr=%lu type=%08lx %08lx",
818 pfn, max_pfn, pfn_type[pfn],
819 (unsigned long)pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT);
820 goto out;
821 }
823 ctxt.ctrlreg[3] = xen_pfn_to_cr3(p2m[pfn]);
825 /* clear any pending events and the selector */
826 memset(&(shared_info->evtchn_pending[0]), 0,
827 sizeof (shared_info->evtchn_pending));
828 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
829 shared_info->vcpu_info[i].evtchn_pending_sel = 0;
831 /* Copy saved contents of shared-info page. No checking needed. */
832 page = xc_map_foreign_range(
833 xc_handle, dom, PAGE_SIZE, PROT_WRITE, shared_info_frame);
834 memcpy(page, shared_info, PAGE_SIZE);
835 munmap(page, PAGE_SIZE);
837 /* Uncanonicalise the pfn-to-mfn table frame-number list. */
838 for (i = 0; i < P2M_FL_ENTRIES; i++) {
839 pfn = p2m_frame_list[i];
840 if ((pfn >= max_pfn) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB)) {
841 ERROR("PFN-to-MFN frame number is bad");
842 goto out;
843 }
845 p2m_frame_list[i] = p2m[pfn];
846 }
848 /* Copy the P2M we've constructed to the 'live' P2M */
849 if (!(live_p2m = xc_map_foreign_batch(xc_handle, dom, PROT_WRITE,
850 p2m_frame_list, P2M_FL_ENTRIES))) {
851 ERROR("Couldn't map p2m table");
852 goto out;
853 }
855 memcpy(live_p2m, p2m, P2M_SIZE);
856 munmap(live_p2m, P2M_SIZE);
858 DPRINTF("Domain ready to be built.\n");
860 domctl.cmd = XEN_DOMCTL_setvcpucontext;
861 domctl.domain = (domid_t)dom;
862 domctl.u.vcpucontext.vcpu = 0;
863 set_xen_guest_handle(domctl.u.vcpucontext.ctxt, &ctxt);
864 rc = xc_domctl(xc_handle, &domctl);
866 if (rc != 0) {
867 ERROR("Couldn't build the domain");
868 goto out;
869 }
871 out:
872 if ( (rc != 0) && (dom != 0) )
873 xc_domain_destroy(xc_handle, dom);
874 free(mmu);
875 free(p2m);
876 free(pfn_type);
878 /* discard cache for save file */
879 discard_file_cache(io_fd, 1 /*flush*/);
881 DPRINTF("Restore exit with rc=%d\n", rc);
883 return rc;
884 }