ia64/xen-unstable

view tools/libxc/xc_linux_restore.c @ 14207:33d733c3649d

[tools] Set guest address size to be the same as dom0 address size on restore.

Signed-off-by: Christian Limpach <Christian.Limpach@xensource.com>
author Christian Limpach <Christian.Limpach@xensource.com>
date Thu Mar 01 17:28:31 2007 +0000 (2007-03-01)
parents 59b8d5168cc1
children 139794d55898
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);
195 /*
196 * XXX For now, 32bit dom0's can only save/restore 32bit domUs
197 * on 64bit hypervisors.
198 */
199 memset(&domctl, 0, sizeof(domctl));
200 domctl.domain = dom;
201 domctl.cmd = XEN_DOMCTL_set_address_size;
202 domctl.u.address_size.size = sizeof(unsigned long) * 8;
203 rc = do_domctl(xc_handle, &domctl);
204 if ( rc != 0 ) {
205 ERROR("Unable to set guest address size.");
206 goto out;
207 }
209 if(!get_platform_info(xc_handle, dom,
210 &max_mfn, &hvirt_start, &pt_levels)) {
211 ERROR("Unable to get platform info.");
212 return 1;
213 }
215 if (lock_pages(&ctxt, sizeof(ctxt))) {
216 /* needed for build domctl, but might as well do early */
217 ERROR("Unable to lock ctxt");
218 return 1;
219 }
221 if (!(p2m_frame_list = malloc(P2M_FL_SIZE))) {
222 ERROR("Couldn't allocate p2m_frame_list array");
223 goto out;
224 }
226 /* Read first entry of P2M list, or extended-info signature (~0UL). */
227 if (!read_exact(io_fd, p2m_frame_list, sizeof(long))) {
228 ERROR("read extended-info signature failed");
229 goto out;
230 }
232 if (p2m_frame_list[0] == ~0UL) {
233 uint32_t tot_bytes;
235 /* Next 4 bytes: total size of following extended info. */
236 if (!read_exact(io_fd, &tot_bytes, sizeof(tot_bytes))) {
237 ERROR("read extended-info size failed");
238 goto out;
239 }
241 while (tot_bytes) {
242 uint32_t chunk_bytes;
243 char chunk_sig[4];
245 /* 4-character chunk signature + 4-byte remaining chunk size. */
246 if (!read_exact(io_fd, chunk_sig, sizeof(chunk_sig)) ||
247 !read_exact(io_fd, &chunk_bytes, sizeof(chunk_bytes))) {
248 ERROR("read extended-info chunk signature failed");
249 goto out;
250 }
251 tot_bytes -= 8;
253 /* VCPU context structure? */
254 if (!strncmp(chunk_sig, "vcpu", 4)) {
255 if (!read_exact(io_fd, &ctxt, sizeof(ctxt))) {
256 ERROR("read extended-info vcpu context failed");
257 goto out;
258 }
259 tot_bytes -= sizeof(struct vcpu_guest_context);
260 chunk_bytes -= sizeof(struct vcpu_guest_context);
262 if (ctxt.vm_assist & (1UL << VMASST_TYPE_pae_extended_cr3))
263 pae_extended_cr3 = 1;
264 }
266 /* Any remaining bytes of this chunk: read and discard. */
267 while (chunk_bytes) {
268 unsigned long sz = chunk_bytes;
269 if ( sz > P2M_FL_SIZE )
270 sz = P2M_FL_SIZE;
271 if (!read_exact(io_fd, p2m_frame_list, sz)) {
272 ERROR("read-and-discard extended-info chunk bytes failed");
273 goto out;
274 }
275 chunk_bytes -= sz;
276 tot_bytes -= sz;
277 }
278 }
280 /* Now read the real first entry of P2M list. */
281 if (!read_exact(io_fd, p2m_frame_list, sizeof(long))) {
282 ERROR("read first entry of p2m_frame_list failed");
283 goto out;
284 }
285 }
287 /* First entry is already read into the p2m array. */
288 if (!read_exact(io_fd, &p2m_frame_list[1], P2M_FL_SIZE - sizeof(long))) {
289 ERROR("read p2m_frame_list failed");
290 goto out;
291 }
293 /* We want zeroed memory so use calloc rather than malloc. */
294 p2m = calloc(max_pfn, sizeof(xen_pfn_t));
295 pfn_type = calloc(max_pfn, sizeof(unsigned long));
296 region_mfn = calloc(MAX_BATCH_SIZE, sizeof(xen_pfn_t));
297 p2m_batch = calloc(MAX_BATCH_SIZE, sizeof(xen_pfn_t));
299 if ((p2m == NULL) || (pfn_type == NULL) ||
300 (region_mfn == NULL) || (p2m_batch == NULL)) {
301 ERROR("memory alloc failed");
302 errno = ENOMEM;
303 goto out;
304 }
306 if (lock_pages(region_mfn, sizeof(xen_pfn_t) * MAX_BATCH_SIZE)) {
307 ERROR("Could not lock region_mfn");
308 goto out;
309 }
311 if (lock_pages(p2m_batch, sizeof(xen_pfn_t) * MAX_BATCH_SIZE)) {
312 ERROR("Could not lock p2m_batch");
313 goto out;
314 }
316 /* Get the domain's shared-info frame. */
317 domctl.cmd = XEN_DOMCTL_getdomaininfo;
318 domctl.domain = (domid_t)dom;
319 if (xc_domctl(xc_handle, &domctl) < 0) {
320 ERROR("Could not get information on new domain");
321 goto out;
322 }
323 shared_info_frame = domctl.u.getdomaininfo.shared_info_frame;
325 if (xc_domain_setmaxmem(xc_handle, dom, PFN_TO_KB(max_pfn)) != 0) {
326 errno = ENOMEM;
327 goto out;
328 }
330 /* Mark all PFNs as invalid; we allocate on demand */
331 for ( pfn = 0; pfn < max_pfn; pfn++ )
332 p2m[pfn] = INVALID_P2M_ENTRY;
334 if(!(mmu = xc_init_mmu_updates(xc_handle, dom))) {
335 ERROR("Could not initialise for MMU updates");
336 goto out;
337 }
339 DPRINTF("Reloading memory pages: 0%%\n");
341 /*
342 * Now simply read each saved frame into its new machine frame.
343 * We uncanonicalise page tables as we go.
344 */
345 prev_pc = 0;
347 n = m = 0;
348 while (1) {
350 int j, nr_mfns = 0;
352 this_pc = (n * 100) / max_pfn;
353 if ( (this_pc - prev_pc) >= 5 )
354 {
355 PPRINTF("\b\b\b\b%3d%%", this_pc);
356 prev_pc = this_pc;
357 }
359 if (!read_exact(io_fd, &j, sizeof(int))) {
360 ERROR("Error when reading batch size");
361 goto out;
362 }
364 PPRINTF("batch %d\n",j);
366 if (j == -1) {
367 verify = 1;
368 DPRINTF("Entering page verify mode\n");
369 continue;
370 }
372 if (j == 0)
373 break; /* our work here is done */
375 if (j > MAX_BATCH_SIZE) {
376 ERROR("Max batch size exceeded. Giving up.");
377 goto out;
378 }
380 if (!read_exact(io_fd, region_pfn_type, j*sizeof(unsigned long))) {
381 ERROR("Error when reading region pfn types");
382 goto out;
383 }
385 /* First pass for this batch: work out how much memory to alloc */
386 nr_mfns = 0;
387 for ( i = 0; i < j; i++ )
388 {
389 unsigned long pfn, pagetype;
390 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
391 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
393 if ( (pagetype != XEN_DOMCTL_PFINFO_XTAB) &&
394 (p2m[pfn] == INVALID_P2M_ENTRY) )
395 {
396 /* Have a live PFN which hasn't had an MFN allocated */
397 p2m_batch[nr_mfns++] = pfn;
398 }
399 }
402 /* Now allocate a bunch of mfns for this batch */
403 if (nr_mfns && xc_domain_memory_populate_physmap(
404 xc_handle, dom, nr_mfns, 0, 0, p2m_batch) != 0) {
405 ERROR("Failed to allocate memory for batch.!\n");
406 errno = ENOMEM;
407 goto out;
408 }
410 /* Second pass for this batch: update p2m[] and region_mfn[] */
411 nr_mfns = 0;
412 for ( i = 0; i < j; i++ )
413 {
414 unsigned long pfn, pagetype;
415 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
416 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
418 if ( pagetype == XEN_DOMCTL_PFINFO_XTAB)
419 region_mfn[i] = ~0UL; /* map will fail but we don't care */
420 else
421 {
422 if (p2m[pfn] == INVALID_P2M_ENTRY) {
423 /* We just allocated a new mfn above; update p2m */
424 p2m[pfn] = p2m_batch[nr_mfns++];
425 }
427 /* setup region_mfn[] for batch map */
428 region_mfn[i] = p2m[pfn];
429 }
430 }
432 /* Map relevant mfns */
433 region_base = xc_map_foreign_batch(
434 xc_handle, dom, PROT_WRITE, region_mfn, j);
436 if ( region_base == NULL )
437 {
438 ERROR("map batch failed");
439 goto out;
440 }
442 for ( i = 0; i < j; i++ )
443 {
444 void *page;
445 unsigned long pagetype;
447 pfn = region_pfn_type[i] & ~XEN_DOMCTL_PFINFO_LTAB_MASK;
448 pagetype = region_pfn_type[i] & XEN_DOMCTL_PFINFO_LTAB_MASK;
450 if ( pagetype == XEN_DOMCTL_PFINFO_XTAB )
451 /* a bogus/unmapped page: skip it */
452 continue;
454 if ( pfn > max_pfn )
455 {
456 ERROR("pfn out of range");
457 goto out;
458 }
460 pfn_type[pfn] = pagetype;
462 mfn = p2m[pfn];
464 /* In verify mode, we use a copy; otherwise we work in place */
465 page = verify ? (void *)buf : (region_base + i*PAGE_SIZE);
467 if (!read_exact(io_fd, page, PAGE_SIZE)) {
468 ERROR("Error when reading page (type was %lx)", pagetype);
469 goto out;
470 }
472 pagetype &= XEN_DOMCTL_PFINFO_LTABTYPE_MASK;
474 if ( (pagetype >= XEN_DOMCTL_PFINFO_L1TAB) &&
475 (pagetype <= XEN_DOMCTL_PFINFO_L4TAB) )
476 {
477 /*
478 ** A page table page - need to 'uncanonicalize' it, i.e.
479 ** replace all the references to pfns with the corresponding
480 ** mfns for the new domain.
481 **
482 ** On PAE we need to ensure that PGDs are in MFNs < 4G, and
483 ** so we may need to update the p2m after the main loop.
484 ** Hence we defer canonicalization of L1s until then.
485 */
486 if ((pt_levels != 3) ||
487 pae_extended_cr3 ||
488 (pagetype != XEN_DOMCTL_PFINFO_L1TAB)) {
490 if (!uncanonicalize_pagetable(xc_handle, dom,
491 pagetype, page)) {
492 /*
493 ** Failing to uncanonicalize a page table can be ok
494 ** under live migration since the pages type may have
495 ** changed by now (and we'll get an update later).
496 */
497 DPRINTF("PT L%ld race on pfn=%08lx mfn=%08lx\n",
498 pagetype >> 28, pfn, mfn);
499 nraces++;
500 continue;
501 }
502 }
503 }
504 else if ( pagetype != XEN_DOMCTL_PFINFO_NOTAB )
505 {
506 ERROR("Bogus page type %lx page table is out of range: "
507 "i=%d max_pfn=%lu", pagetype, i, max_pfn);
508 goto out;
510 }
513 if (verify) {
515 int res = memcmp(buf, (region_base + i*PAGE_SIZE), PAGE_SIZE);
517 if (res) {
519 int v;
521 DPRINTF("************** pfn=%lx type=%lx gotcs=%08lx "
522 "actualcs=%08lx\n", pfn, pfn_type[pfn],
523 csum_page(region_base + i*PAGE_SIZE),
524 csum_page(buf));
526 for (v = 0; v < 4; v++) {
528 unsigned long *p = (unsigned long *)
529 (region_base + i*PAGE_SIZE);
530 if (buf[v] != p[v])
531 DPRINTF(" %d: %08lx %08lx\n", v, buf[v], p[v]);
532 }
533 }
534 }
536 if (xc_add_mmu_update(xc_handle, mmu,
537 (((unsigned long long)mfn) << PAGE_SHIFT)
538 | MMU_MACHPHYS_UPDATE, pfn)) {
539 ERROR("failed machpys update mfn=%lx pfn=%lx", mfn, pfn);
540 goto out;
541 }
542 } /* end of 'batch' for loop */
544 munmap(region_base, j*PAGE_SIZE);
545 n+= j; /* crude stats */
547 /*
548 * Discard cache for portion of file read so far up to last
549 * page boundary every 16MB or so.
550 */
551 m += j;
552 if ( m > MAX_PAGECACHE_USAGE )
553 {
554 discard_file_cache(io_fd, 0 /* no flush */);
555 m = 0;
556 }
557 }
559 /*
560 * Ensure we flush all machphys updates before potential PAE-specific
561 * reallocations below.
562 */
563 if (xc_finish_mmu_updates(xc_handle, mmu)) {
564 ERROR("Error doing finish_mmu_updates()");
565 goto out;
566 }
568 DPRINTF("Received all pages (%d races)\n", nraces);
570 if ((pt_levels == 3) && !pae_extended_cr3) {
572 /*
573 ** XXX SMH on PAE we need to ensure PGDs are in MFNs < 4G. This
574 ** is a little awkward and involves (a) finding all such PGDs and
575 ** replacing them with 'lowmem' versions; (b) upating the p2m[]
576 ** with the new info; and (c) canonicalizing all the L1s using the
577 ** (potentially updated) p2m[].
578 **
579 ** This is relatively slow (and currently involves two passes through
580 ** the pfn_type[] array), but at least seems to be correct. May wish
581 ** to consider more complex approaches to optimize this later.
582 */
584 int j, k;
586 /* First pass: find all L3TABs current in > 4G mfns and get new mfns */
587 for ( i = 0; i < max_pfn; i++ )
588 {
589 if ( ((pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) ==
590 XEN_DOMCTL_PFINFO_L3TAB) &&
591 (p2m[i] > 0xfffffUL) )
592 {
593 unsigned long new_mfn;
594 uint64_t l3ptes[4];
595 uint64_t *l3tab;
597 l3tab = (uint64_t *)
598 xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
599 PROT_READ, p2m[i]);
601 for(j = 0; j < 4; j++)
602 l3ptes[j] = l3tab[j];
604 munmap(l3tab, PAGE_SIZE);
606 if (!(new_mfn=xc_make_page_below_4G(xc_handle, dom, p2m[i]))) {
607 ERROR("Couldn't get a page below 4GB :-(");
608 goto out;
609 }
611 p2m[i] = new_mfn;
612 if (xc_add_mmu_update(xc_handle, mmu,
613 (((unsigned long long)new_mfn)
614 << PAGE_SHIFT) |
615 MMU_MACHPHYS_UPDATE, i)) {
616 ERROR("Couldn't m2p on PAE root pgdir");
617 goto out;
618 }
620 l3tab = (uint64_t *)
621 xc_map_foreign_range(xc_handle, dom, PAGE_SIZE,
622 PROT_READ | PROT_WRITE, p2m[i]);
624 for(j = 0; j < 4; j++)
625 l3tab[j] = l3ptes[j];
627 munmap(l3tab, PAGE_SIZE);
629 }
630 }
632 /* Second pass: find all L1TABs and uncanonicalize them */
633 j = 0;
635 for ( i = 0; i < max_pfn; i++ )
636 {
637 if ( ((pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) ==
638 XEN_DOMCTL_PFINFO_L1TAB) )
639 {
640 region_mfn[j] = p2m[i];
641 j++;
642 }
644 if(i == (max_pfn-1) || j == MAX_BATCH_SIZE) {
646 if (!(region_base = xc_map_foreign_batch(
647 xc_handle, dom, PROT_READ | PROT_WRITE,
648 region_mfn, j))) {
649 ERROR("map batch failed");
650 goto out;
651 }
653 for(k = 0; k < j; k++) {
654 if(!uncanonicalize_pagetable(xc_handle, dom,
655 XEN_DOMCTL_PFINFO_L1TAB,
656 region_base + k*PAGE_SIZE)) {
657 ERROR("failed uncanonicalize pt!");
658 goto out;
659 }
660 }
662 munmap(region_base, j*PAGE_SIZE);
663 j = 0;
664 }
665 }
667 if (xc_finish_mmu_updates(xc_handle, mmu)) {
668 ERROR("Error doing finish_mmu_updates()");
669 goto out;
670 }
671 }
673 /*
674 * Pin page tables. Do this after writing to them as otherwise Xen
675 * will barf when doing the type-checking.
676 */
677 nr_pins = 0;
678 for ( i = 0; i < max_pfn; i++ )
679 {
680 if ( (pfn_type[i] & XEN_DOMCTL_PFINFO_LPINTAB) == 0 )
681 continue;
683 switch ( pfn_type[i] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK )
684 {
685 case XEN_DOMCTL_PFINFO_L1TAB:
686 pin[nr_pins].cmd = MMUEXT_PIN_L1_TABLE;
687 break;
689 case XEN_DOMCTL_PFINFO_L2TAB:
690 pin[nr_pins].cmd = MMUEXT_PIN_L2_TABLE;
691 break;
693 case XEN_DOMCTL_PFINFO_L3TAB:
694 pin[nr_pins].cmd = MMUEXT_PIN_L3_TABLE;
695 break;
697 case XEN_DOMCTL_PFINFO_L4TAB:
698 pin[nr_pins].cmd = MMUEXT_PIN_L4_TABLE;
699 break;
701 default:
702 continue;
703 }
705 pin[nr_pins].arg1.mfn = p2m[i];
706 nr_pins++;
708 /* Batch full? Then flush. */
709 if (nr_pins == MAX_PIN_BATCH) {
710 if (xc_mmuext_op(xc_handle, pin, nr_pins, dom) < 0) {
711 ERROR("Failed to pin batch of %d page tables", nr_pins);
712 goto out;
713 }
714 nr_pins = 0;
715 }
716 }
718 /* Flush final partial batch. */
719 if ((nr_pins != 0) && (xc_mmuext_op(xc_handle, pin, nr_pins, dom) < 0)) {
720 ERROR("Failed to pin batch of %d page tables", nr_pins);
721 goto out;
722 }
724 DPRINTF("\b\b\b\b100%%\n");
725 DPRINTF("Memory reloaded.\n");
727 /* Get the list of PFNs that are not in the psuedo-phys map */
728 {
729 unsigned int count;
730 unsigned long *pfntab;
731 int nr_frees, rc;
733 if (!read_exact(io_fd, &count, sizeof(count))) {
734 ERROR("Error when reading pfn count");
735 goto out;
736 }
738 if(!(pfntab = malloc(sizeof(unsigned long) * count))) {
739 ERROR("Out of memory");
740 goto out;
741 }
743 if (!read_exact(io_fd, pfntab, sizeof(unsigned long)*count)) {
744 ERROR("Error when reading pfntab");
745 goto out;
746 }
748 nr_frees = 0;
749 for (i = 0; i < count; i++) {
751 unsigned long pfn = pfntab[i];
753 if(p2m[pfn] != INVALID_P2M_ENTRY) {
754 /* pfn is not in physmap now, but was at some point during
755 the save/migration process - need to free it */
756 pfntab[nr_frees++] = p2m[pfn];
757 p2m[pfn] = INVALID_P2M_ENTRY; // not in pseudo-physical map
758 }
759 }
761 if (nr_frees > 0) {
763 struct xen_memory_reservation reservation = {
764 .nr_extents = nr_frees,
765 .extent_order = 0,
766 .domid = dom
767 };
768 set_xen_guest_handle(reservation.extent_start, pfntab);
770 if ((rc = xc_memory_op(xc_handle, XENMEM_decrease_reservation,
771 &reservation)) != nr_frees) {
772 ERROR("Could not decrease reservation : %d", rc);
773 goto out;
774 } else
775 DPRINTF("Decreased reservation by %d pages\n", count);
776 }
777 }
779 if (!read_exact(io_fd, &ctxt, sizeof(ctxt)) ||
780 !read_exact(io_fd, shared_info_page, PAGE_SIZE)) {
781 ERROR("Error when reading ctxt or shared info page");
782 goto out;
783 }
785 /* Uncanonicalise the suspend-record frame number and poke resume rec. */
786 pfn = ctxt.user_regs.edx;
787 if ((pfn >= max_pfn) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB)) {
788 ERROR("Suspend record frame number is bad");
789 goto out;
790 }
791 ctxt.user_regs.edx = mfn = p2m[pfn];
792 start_info = xc_map_foreign_range(
793 xc_handle, dom, PAGE_SIZE, PROT_READ | PROT_WRITE, mfn);
794 start_info->nr_pages = max_pfn;
795 start_info->shared_info = shared_info_frame << PAGE_SHIFT;
796 start_info->flags = 0;
797 *store_mfn = start_info->store_mfn = p2m[start_info->store_mfn];
798 start_info->store_evtchn = store_evtchn;
799 start_info->console.domU.mfn = p2m[start_info->console.domU.mfn];
800 start_info->console.domU.evtchn = console_evtchn;
801 *console_mfn = start_info->console.domU.mfn;
802 munmap(start_info, PAGE_SIZE);
804 /* Uncanonicalise each GDT frame number. */
805 if (ctxt.gdt_ents > 8192) {
806 ERROR("GDT entry count out of range");
807 goto out;
808 }
810 for (i = 0; (512*i) < ctxt.gdt_ents; i++) {
811 pfn = ctxt.gdt_frames[i];
812 if ((pfn >= max_pfn) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB)) {
813 ERROR("GDT frame number is bad");
814 goto out;
815 }
816 ctxt.gdt_frames[i] = p2m[pfn];
817 }
819 /* Uncanonicalise the page table base pointer. */
820 pfn = xen_cr3_to_pfn(ctxt.ctrlreg[3]);
822 if (pfn >= max_pfn) {
823 ERROR("PT base is bad: pfn=%lu max_pfn=%lu type=%08lx",
824 pfn, max_pfn, pfn_type[pfn]);
825 goto out;
826 }
828 if ( (pfn_type[pfn] & XEN_DOMCTL_PFINFO_LTABTYPE_MASK) !=
829 ((unsigned long)pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT) ) {
830 ERROR("PT base is bad. pfn=%lu nr=%lu type=%08lx %08lx",
831 pfn, max_pfn, pfn_type[pfn],
832 (unsigned long)pt_levels<<XEN_DOMCTL_PFINFO_LTAB_SHIFT);
833 goto out;
834 }
836 ctxt.ctrlreg[3] = xen_pfn_to_cr3(p2m[pfn]);
838 /* clear any pending events and the selector */
839 memset(&(shared_info->evtchn_pending[0]), 0,
840 sizeof (shared_info->evtchn_pending));
841 for ( i = 0; i < MAX_VIRT_CPUS; i++ )
842 shared_info->vcpu_info[i].evtchn_pending_sel = 0;
844 /* Copy saved contents of shared-info page. No checking needed. */
845 page = xc_map_foreign_range(
846 xc_handle, dom, PAGE_SIZE, PROT_WRITE, shared_info_frame);
847 memcpy(page, shared_info, PAGE_SIZE);
848 munmap(page, PAGE_SIZE);
850 /* Uncanonicalise the pfn-to-mfn table frame-number list. */
851 for (i = 0; i < P2M_FL_ENTRIES; i++) {
852 pfn = p2m_frame_list[i];
853 if ((pfn >= max_pfn) || (pfn_type[pfn] != XEN_DOMCTL_PFINFO_NOTAB)) {
854 ERROR("PFN-to-MFN frame number is bad");
855 goto out;
856 }
858 p2m_frame_list[i] = p2m[pfn];
859 }
861 /* Copy the P2M we've constructed to the 'live' P2M */
862 if (!(live_p2m = xc_map_foreign_batch(xc_handle, dom, PROT_WRITE,
863 p2m_frame_list, P2M_FL_ENTRIES))) {
864 ERROR("Couldn't map p2m table");
865 goto out;
866 }
868 memcpy(live_p2m, p2m, P2M_SIZE);
869 munmap(live_p2m, P2M_SIZE);
871 DPRINTF("Domain ready to be built.\n");
873 domctl.cmd = XEN_DOMCTL_setvcpucontext;
874 domctl.domain = (domid_t)dom;
875 domctl.u.vcpucontext.vcpu = 0;
876 set_xen_guest_handle(domctl.u.vcpucontext.ctxt, &ctxt);
877 rc = xc_domctl(xc_handle, &domctl);
879 if (rc != 0) {
880 ERROR("Couldn't build the domain");
881 goto out;
882 }
884 out:
885 if ( (rc != 0) && (dom != 0) )
886 xc_domain_destroy(xc_handle, dom);
887 free(mmu);
888 free(p2m);
889 free(pfn_type);
891 /* discard cache for save file */
892 discard_file_cache(io_fd, 1 /*flush*/);
894 DPRINTF("Restore exit with rc=%d\n", rc);
896 return rc;
897 }