This repository has been archived by the owner on May 8, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
xc_domain_save.c
2148 lines (1817 loc) · 68.2 KB
/
xc_domain_save.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* xc_linux_save.c
*
* Save the state of a running Linux session.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Copyright (c) 2003, K A Fraser.
*/
#include <inttypes.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include "xc_private.h"
#include "xc_bitops.h"
#include "xc_dom.h"
#include "xg_private.h"
#include "xg_save_restore.h"
#include <xen/hvm/params.h>
/*
** Default values for important tuning parameters. Can override by passing
** non-zero replacement values to xc_domain_save().
**
** XXX SMH: should consider if want to be able to override MAX_MBIT_RATE too.
**
*/
#define DEF_MAX_ITERS 29 /* limit us to 30 times round loop */
#define DEF_MAX_FACTOR 3 /* never send more than 3x p2m_size */
struct save_ctx {
unsigned long hvirt_start; /* virtual starting address of the hypervisor */
unsigned int pt_levels; /* #levels of page tables used by the current guest */
unsigned long max_mfn; /* max mfn of the whole machine */
xen_pfn_t *live_p2m; /* Live mapping of the table mapping each PFN to its current MFN. */
xen_pfn_t *live_m2p; /* Live mapping of system MFN to PFN table. */
unsigned long m2p_mfn0;
struct domain_info_context dinfo;
};
/* buffer for output */
struct outbuf {
void* buf;
size_t size;
size_t pos;
int write_count;
};
#define OUTBUF_SIZE (16384 * 1024)
/* grep fodder: machine_to_phys */
#define mfn_to_pfn(_mfn) (ctx->live_m2p[(_mfn)])
#define pfn_to_mfn(_pfn) \
((xen_pfn_t) ((dinfo->guest_width==8) \
? (((uint64_t *)ctx->live_p2m)[(_pfn)]) \
: ((((uint32_t *)ctx->live_p2m)[(_pfn)]) == 0xffffffffU \
? (-1UL) : (((uint32_t *)ctx->live_p2m)[(_pfn)]))))
/*
* Returns TRUE if the given machine frame number has a unique mapping
* in the guest's pseudophysical map.
*/
#define MFN_IS_IN_PSEUDOPHYS_MAP(_mfn) \
(((_mfn) < (ctx->max_mfn)) && \
((mfn_to_pfn(_mfn) < (dinfo->p2m_size)) && \
(pfn_to_mfn(mfn_to_pfn(_mfn)) == (_mfn))))
#define SUPERPAGE_PFN_SHIFT 9
#define SUPERPAGE_NR_PFNS (1UL << SUPERPAGE_PFN_SHIFT)
#define SUPER_PAGE_START(pfn) (((pfn) & (SUPERPAGE_NR_PFNS-1)) == 0 )
static uint64_t tv_to_us(struct timeval *new)
{
return (new->tv_sec * 1000000) + new->tv_usec;
}
static uint64_t llgettimeofday(void)
{
struct timeval now;
gettimeofday(&now, NULL);
return tv_to_us(&now);
}
static uint64_t tv_delta(struct timeval *new, struct timeval *old)
{
return (((new->tv_sec - old->tv_sec)*1000000) +
(new->tv_usec - old->tv_usec));
}
static int noncached_write(xc_interface *xch,
struct outbuf* ob,
int fd, void *buffer, int len)
{
int rc = (write_exact(fd, buffer, len) == 0) ? len : -1;
ob->write_count += len;
if ( ob->write_count >= (MAX_PAGECACHE_USAGE * PAGE_SIZE) )
{
/* Time to discard cache - dont care if this fails */
int saved_errno = errno;
discard_file_cache(xch, fd, 0 /* no flush */);
errno = saved_errno;
ob->write_count = 0;
}
return rc;
}
static int outbuf_init(xc_interface *xch, struct outbuf* ob, size_t size)
{
memset(ob, 0, sizeof(*ob));
if ( !(ob->buf = malloc(size)) ) {
DPRINTF("error allocating output buffer of size %zu\n", size);
return -1;
}
ob->size = size;
return 0;
}
static int outbuf_free(struct outbuf *ob)
{
free(ob->buf);
ob->buf = NULL;
return 0;
}
static inline int outbuf_write(xc_interface *xch,
struct outbuf* ob, void* buf, size_t len)
{
if ( len > ob->size - ob->pos ) {
DBGPRINTF("outbuf_write: %zu > %zu@%zu\n", len, ob->size - ob->pos, ob->pos);
return -1;
}
memcpy(ob->buf + ob->pos, buf, len);
ob->pos += len;
return 0;
}
/* prep for nonblocking I/O */
static int outbuf_flush(xc_interface *xch, struct outbuf* ob, int fd)
{
int rc;
int cur = 0;
if ( !ob->pos )
return 0;
rc = write(fd, ob->buf, ob->pos);
while (rc < 0 || cur + rc < ob->pos) {
if (rc < 0 && errno != EAGAIN && errno != EINTR) {
DPRINTF("error flushing output: %d\n", errno);
return -1;
}
if (rc > 0)
cur += rc;
rc = write(fd, ob->buf + cur, ob->pos - cur);
}
ob->pos = 0;
return 0;
}
/* if there's no room in the buffer, flush it and try again. */
static inline int outbuf_hardwrite(xc_interface *xch,
struct outbuf* ob, int fd, void* buf,
size_t len)
{
if ( !len )
return 0;
if ( !outbuf_write(xch, ob, buf, len) )
return 0;
if ( outbuf_flush(xch, ob, fd) < 0 )
return -1;
return outbuf_write(xch, ob, buf, len);
}
/* start buffering output once we've reached checkpoint mode. */
static inline int write_buffer(xc_interface *xch,
int dobuf, struct outbuf* ob, int fd, void* buf,
size_t len)
{
if ( dobuf )
return outbuf_hardwrite(xch, ob, fd, buf, len);
else
return write_exact(fd, buf, len);
}
/* like write_buffer for noncached, which returns number of bytes written */
static inline int write_uncached(xc_interface *xch,
int dobuf, struct outbuf* ob, int fd,
void* buf, size_t len)
{
if ( dobuf )
return outbuf_hardwrite(xch, ob, fd, buf, len) ? -1 : len;
else
return noncached_write(xch, ob, fd, buf, len);
}
static int write_compressed(xc_interface *xch, comp_ctx *compress_ctx,
int dobuf, struct outbuf* ob, int fd)
{
int rc = 0;
int header = sizeof(int) + sizeof(unsigned long);
int marker = XC_SAVE_ID_COMPRESSED_DATA;
unsigned long compbuf_len = 0;
do
{
/* check for available space (atleast 8k) */
if ((ob->pos + header + XC_PAGE_SIZE * 2) > ob->size)
{
if (outbuf_flush(xch, ob, fd) < 0)
{
ERROR("Error when flushing outbuf intermediate");
return -1;
}
}
rc = xc_compression_compress_pages(xch, compress_ctx,
ob->buf + ob->pos + header,
ob->size - ob->pos - header,
&compbuf_len);
if (!rc)
return 0;
if (outbuf_hardwrite(xch, ob, fd, &marker, sizeof(marker)) < 0)
{
PERROR("Error when writing marker (errno %d)", errno);
return -1;
}
if (outbuf_hardwrite(xch, ob, fd, &compbuf_len, sizeof(compbuf_len)) < 0)
{
PERROR("Error when writing compbuf_len (errno %d)", errno);
return -1;
}
ob->pos += (size_t) compbuf_len;
if (!dobuf && outbuf_flush(xch, ob, fd) < 0)
{
ERROR("Error when writing compressed chunk");
return -1;
}
} while (rc != 0);
return 0;
}
struct time_stats {
struct timeval wall;
long long d0_cpu, d1_cpu;
};
static int print_stats(xc_interface *xch, uint32_t domid, int pages_sent,
struct time_stats *last,
xc_shadow_op_stats_t *stats, int print)
{
struct time_stats now;
gettimeofday(&now.wall, NULL);
now.d0_cpu = xc_domain_get_cpu_usage(xch, 0, /* FIXME */ 0)/1000;
now.d1_cpu = xc_domain_get_cpu_usage(xch, domid, /* FIXME */ 0)/1000;
if ( (now.d0_cpu == -1) || (now.d1_cpu == -1) )
DPRINTF("ARRHHH!!\n");
if ( print )
{
long long wall_delta;
long long d0_cpu_delta;
long long d1_cpu_delta;
wall_delta = tv_delta(&now.wall,&last->wall)/1000;
if ( wall_delta == 0 )
wall_delta = 1;
d0_cpu_delta = (now.d0_cpu - last->d0_cpu)/1000;
d1_cpu_delta = (now.d1_cpu - last->d1_cpu)/1000;
DPRINTF("delta %lldms, dom0 %d%%, target %d%%, sent %dMb/s, "
"dirtied %dMb/s %" PRId32 " pages\n",
wall_delta,
(int)((d0_cpu_delta*100)/wall_delta),
(int)((d1_cpu_delta*100)/wall_delta),
(int)((pages_sent*PAGE_SIZE)/(wall_delta*(1000/8))),
(int)((stats->dirty_count*PAGE_SIZE)/(wall_delta*(1000/8))),
stats->dirty_count);
}
*last = now;
return 0;
}
static int analysis_phase(xc_interface *xch, uint32_t domid, struct save_ctx *ctx,
xc_hypercall_buffer_t *arr, int runs)
{
long long start, now;
xc_shadow_op_stats_t stats;
int j;
struct domain_info_context *dinfo = &ctx->dinfo;
start = llgettimeofday();
for ( j = 0; j < runs; j++ )
{
int i;
xc_shadow_control(xch, domid, XEN_DOMCTL_SHADOW_OP_CLEAN,
arr, dinfo->p2m_size, NULL, 0, NULL);
DPRINTF("#Flush\n");
for ( i = 0; i < 40; i++ )
{
usleep(50000);
now = llgettimeofday();
xc_shadow_control(xch, domid, XEN_DOMCTL_SHADOW_OP_PEEK,
NULL, 0, NULL, 0, &stats);
DPRINTF("now= %lld faults= %"PRId32" dirty= %"PRId32"\n",
((now-start)+500)/1000,
stats.fault_count, stats.dirty_count);
}
}
return -1;
}
static int suspend_and_state(int (*suspend)(void*), void* data,
xc_interface *xch, int io_fd, int dom,
xc_dominfo_t *info)
{
if ( !(*suspend)(data) )
{
ERROR("Suspend request failed");
return -1;
}
if ( (xc_domain_getinfo(xch, dom, 1, info) != 1) ||
!info->shutdown || (info->shutdown_reason != SHUTDOWN_suspend) )
{
ERROR("Domain not in suspended state");
return -1;
}
return 0;
}
/*
** Map the top-level page of MFNs from the guest. The guest might not have
** finished resuming from a previous restore operation, so we wait a while for
** it to update the MFN to a reasonable value.
*/
static void *map_frame_list_list(xc_interface *xch, uint32_t dom,
struct save_ctx *ctx,
shared_info_any_t *shinfo)
{
int count = 100;
void *p;
struct domain_info_context *dinfo = &ctx->dinfo;
uint64_t fll = GET_FIELD(shinfo, arch.pfn_to_mfn_frame_list_list);
while ( count-- && (fll == 0) )
{
usleep(10000);
fll = GET_FIELD(shinfo, arch.pfn_to_mfn_frame_list_list);
}
if ( fll == 0 )
{
ERROR("Timed out waiting for frame list updated.");
return NULL;
}
p = xc_map_foreign_range(xch, dom, PAGE_SIZE, PROT_READ, fll);
if ( p == NULL )
PERROR("Couldn't map p2m_frame_list_list (errno %d)", errno);
return p;
}
/*
** During transfer (or in the state file), all page-table pages must be
** converted into a 'canonical' form where references to actual mfns
** are replaced with references to the corresponding pfns.
**
** This function performs the appropriate conversion, taking into account
** which entries do not require canonicalization (in particular, those
** entries which map the virtual address reserved for the hypervisor).
*/
static int canonicalize_pagetable(struct save_ctx *ctx,
unsigned long type, unsigned long pfn,
const void *spage, void *dpage)
{
struct domain_info_context *dinfo = &ctx->dinfo;
int i, pte_last, xen_start, xen_end, race = 0;
uint64_t pte;
/*
** We need to determine which entries in this page table hold
** reserved hypervisor mappings. This depends on the current
** page table type as well as the number of paging levels.
*/
xen_start = xen_end = pte_last = PAGE_SIZE / 8;
if ( (ctx->pt_levels == 3) && (type == XEN_DOMCTL_PFINFO_L3TAB) )
xen_start = L3_PAGETABLE_ENTRIES_PAE;
/*
** In PAE only the L2 mapping the top 1GB contains Xen mappings.
** We can spot this by looking for the guest's mappingof the m2p.
** Guests must ensure that this check will fail for other L2s.
*/
if ( (ctx->pt_levels == 3) && (type == XEN_DOMCTL_PFINFO_L2TAB) )
{
int hstart;
uint64_t he;
hstart = (ctx->hvirt_start >> L2_PAGETABLE_SHIFT_PAE) & 0x1ff;
he = ((const uint64_t *) spage)[hstart];
if ( ((he >> PAGE_SHIFT) & MFN_MASK_X86) == ctx->m2p_mfn0 )
{
/* hvirt starts with xen stuff... */
xen_start = hstart;
}
else if ( ctx->hvirt_start != 0xf5800000 )
{
/* old L2s from before hole was shrunk... */
hstart = (0xf5800000 >> L2_PAGETABLE_SHIFT_PAE) & 0x1ff;
he = ((const uint64_t *) spage)[hstart];
if ( ((he >> PAGE_SHIFT) & MFN_MASK_X86) == ctx->m2p_mfn0 )
xen_start = hstart;
}
}
if ( (ctx->pt_levels == 4) && (type == XEN_DOMCTL_PFINFO_L4TAB) )
{
/*
** XXX SMH: should compute these from hvirt_start (which we have)
** and hvirt_end (which we don't)
*/
xen_start = 256;
xen_end = 272;
}
/* Now iterate through the page table, canonicalizing each PTE */
for (i = 0; i < pte_last; i++ )
{
unsigned long pfn, mfn;
pte = ((const uint64_t*)spage)[i];
if ( (i >= xen_start) && (i < xen_end) )
pte = 0;
if ( pte & _PAGE_PRESENT )
{
mfn = (pte >> PAGE_SHIFT) & MFN_MASK_X86;
if ( !MFN_IS_IN_PSEUDOPHYS_MAP(mfn) )
{
/* This will happen if the type info is stale which
is quite feasible under live migration */
pfn = 0; /* zap it - we'll retransmit this page later */
/* XXX: We can't spot Xen mappings in compat-mode L2es
* from 64-bit tools, but the only thing in them is the
* compat m2p, so we quietly zap them. This doesn't
* count as a race, so don't report it. */
if ( !(type == XEN_DOMCTL_PFINFO_L2TAB
&& sizeof (unsigned long) > dinfo->guest_width) )
race = 1; /* inform the caller; fatal if !live */
}
else
pfn = mfn_to_pfn(mfn);
pte &= ~MADDR_MASK_X86;
pte |= (uint64_t)pfn << PAGE_SHIFT;
/*
* PAE guest L3Es can contain these flags when running on
* a 64bit hypervisor. We zap these here to avoid any
* surprise at restore time...
*/
if ( (ctx->pt_levels == 3) &&
(type == XEN_DOMCTL_PFINFO_L3TAB) &&
(pte & (_PAGE_USER|_PAGE_RW|_PAGE_ACCESSED)) )
pte &= ~(_PAGE_USER|_PAGE_RW|_PAGE_ACCESSED);
}
((uint64_t*)dpage)[i] = pte;
}
return race;
}
xen_pfn_t *xc_map_m2p(xc_interface *xch,
unsigned long max_mfn,
int prot,
unsigned long *mfn0)
{
privcmd_mmap_entry_t *entries;
unsigned long m2p_chunks, m2p_size;
xen_pfn_t *m2p;
xen_pfn_t *extent_start;
int i;
m2p = NULL;
m2p_size = M2P_SIZE(max_mfn);
m2p_chunks = M2P_CHUNKS(max_mfn);
extent_start = calloc(m2p_chunks, sizeof(xen_pfn_t));
if ( !extent_start )
{
ERROR("failed to allocate space for m2p mfns");
goto err0;
}
if ( xc_machphys_mfn_list(xch, m2p_chunks, extent_start) )
{
PERROR("xc_get_m2p_mfns");
goto err1;
}
entries = calloc(m2p_chunks, sizeof(privcmd_mmap_entry_t));
if (entries == NULL)
{
ERROR("failed to allocate space for mmap entries");
goto err1;
}
for ( i = 0; i < m2p_chunks; i++ )
entries[i].mfn = extent_start[i];
m2p = xc_map_foreign_ranges(xch, DOMID_XEN,
m2p_size, prot, M2P_CHUNK_SIZE,
entries, m2p_chunks);
if (m2p == NULL)
{
PERROR("xc_mmap_foreign_ranges failed");
goto err2;
}
if (mfn0)
*mfn0 = entries[0].mfn;
err2:
free(entries);
err1:
free(extent_start);
err0:
return m2p;
}
static xen_pfn_t *map_and_save_p2m_table(xc_interface *xch,
int io_fd,
uint32_t dom,
struct save_ctx *ctx,
shared_info_any_t *live_shinfo)
{
vcpu_guest_context_any_t ctxt;
struct domain_info_context *dinfo = &ctx->dinfo;
/* Double and single indirect references to the live P2M table */
void *live_p2m_frame_list_list = NULL;
void *live_p2m_frame_list = NULL;
/* Copies of the above. */
xen_pfn_t *p2m_frame_list_list = NULL;
xen_pfn_t *p2m_frame_list = NULL;
/* The mapping of the live p2m table itself */
xen_pfn_t *p2m = NULL;
int i, success = 0;
live_p2m_frame_list_list = map_frame_list_list(xch, dom, ctx,
live_shinfo);
if ( !live_p2m_frame_list_list )
goto out;
/* Get a local copy of the live_P2M_frame_list_list */
if ( !(p2m_frame_list_list = malloc(PAGE_SIZE)) )
{
ERROR("Couldn't allocate p2m_frame_list_list array");
goto out;
}
memcpy(p2m_frame_list_list, live_p2m_frame_list_list, PAGE_SIZE);
/* Canonicalize guest's unsigned long vs ours */
if ( dinfo->guest_width > sizeof(unsigned long) )
for ( i = 0; i < PAGE_SIZE/sizeof(unsigned long); i++ )
if ( i < PAGE_SIZE/dinfo->guest_width )
p2m_frame_list_list[i] = ((uint64_t *)p2m_frame_list_list)[i];
else
p2m_frame_list_list[i] = 0;
else if ( dinfo->guest_width < sizeof(unsigned long) )
for ( i = PAGE_SIZE/sizeof(unsigned long) - 1; i >= 0; i-- )
p2m_frame_list_list[i] = ((uint32_t *)p2m_frame_list_list)[i];
live_p2m_frame_list =
xc_map_foreign_pages(xch, dom, PROT_READ,
p2m_frame_list_list,
P2M_FLL_ENTRIES);
if ( !live_p2m_frame_list )
{
PERROR("Couldn't map p2m_frame_list");
goto out;
}
/* Get a local copy of the live_P2M_frame_list */
if ( !(p2m_frame_list = malloc(P2M_TOOLS_FL_SIZE)) )
{
ERROR("Couldn't allocate p2m_frame_list array");
goto out;
}
memset(p2m_frame_list, 0, P2M_TOOLS_FL_SIZE);
memcpy(p2m_frame_list, live_p2m_frame_list, P2M_GUEST_FL_SIZE);
munmap(live_p2m_frame_list, P2M_FLL_ENTRIES * PAGE_SIZE);
live_p2m_frame_list = NULL;
/* Canonicalize guest's unsigned long vs ours */
if ( dinfo->guest_width > sizeof(unsigned long) )
for ( i = 0; i < P2M_FL_ENTRIES; i++ )
p2m_frame_list[i] = ((uint64_t *)p2m_frame_list)[i];
else if ( dinfo->guest_width < sizeof(unsigned long) )
for ( i = P2M_FL_ENTRIES - 1; i >= 0; i-- )
p2m_frame_list[i] = ((uint32_t *)p2m_frame_list)[i];
/* Map all the frames of the pfn->mfn table. For migrate to succeed,
the guest must not change which frames are used for this purpose.
(its not clear why it would want to change them, and we'll be OK
from a safety POV anyhow. */
p2m = xc_map_foreign_pages(xch, dom, PROT_READ,
p2m_frame_list,
P2M_FL_ENTRIES);
if ( !p2m )
{
PERROR("Couldn't map p2m table");
goto out;
}
ctx->live_p2m = p2m; /* So that translation macros will work */
/* Canonicalise the pfn-to-mfn table frame-number list. */
for ( i = 0; i < dinfo->p2m_size; i += FPP )
{
if ( !MFN_IS_IN_PSEUDOPHYS_MAP(p2m_frame_list[i/FPP]) )
{
ERROR("Frame# in pfn-to-mfn frame list is not in pseudophys");
ERROR("entry %d: p2m_frame_list[%ld] is 0x%"PRIx64", max 0x%lx",
i, i/FPP, (uint64_t)p2m_frame_list[i/FPP], ctx->max_mfn);
if ( p2m_frame_list[i/FPP] < ctx->max_mfn )
{
ERROR("m2p[0x%"PRIx64"] = 0x%"PRIx64,
(uint64_t)p2m_frame_list[i/FPP],
(uint64_t)ctx->live_m2p[p2m_frame_list[i/FPP]]);
ERROR("p2m[0x%"PRIx64"] = 0x%"PRIx64,
(uint64_t)ctx->live_m2p[p2m_frame_list[i/FPP]],
(uint64_t)p2m[ctx->live_m2p[p2m_frame_list[i/FPP]]]);
}
goto out;
}
p2m_frame_list[i/FPP] = mfn_to_pfn(p2m_frame_list[i/FPP]);
}
if ( xc_vcpu_getcontext(xch, dom, 0, &ctxt) )
{
PERROR("Could not get vcpu context");
goto out;
}
/*
* Write an extended-info structure to inform the restore code that
* a PAE guest understands extended CR3 (PDPTs above 4GB). Turns off
* slow paths in the restore code.
*/
{
unsigned long signature = ~0UL;
uint32_t chunk1_sz = ((dinfo->guest_width==8)
? sizeof(ctxt.x64)
: sizeof(ctxt.x32));
uint32_t chunk2_sz = 0;
uint32_t chunk3_sz = 4;
uint32_t xcnt_size = 0;
uint32_t tot_sz;
DECLARE_DOMCTL;
domctl.cmd = XEN_DOMCTL_getvcpuextstate;
domctl.domain = dom;
domctl.u.vcpuextstate.vcpu = 0;
domctl.u.vcpuextstate.size = 0;
domctl.u.vcpuextstate.xfeature_mask = 0;
if ( xc_domctl(xch, &domctl) < 0 )
{
PERROR("No extended context for VCPU%d", i);
goto out;
}
xcnt_size = domctl.u.vcpuextstate.size + 2 * sizeof(uint64_t);
tot_sz = (chunk1_sz + 8) + (chunk2_sz + 8);
if ( domctl.u.vcpuextstate.xfeature_mask )
tot_sz += chunk3_sz + 8;
if ( write_exact(io_fd, &signature, sizeof(signature)) ||
write_exact(io_fd, &tot_sz, sizeof(tot_sz)) ||
write_exact(io_fd, "vcpu", 4) ||
write_exact(io_fd, &chunk1_sz, sizeof(chunk1_sz)) ||
write_exact(io_fd, &ctxt, chunk1_sz) ||
write_exact(io_fd, "extv", 4) ||
write_exact(io_fd, &chunk2_sz, sizeof(chunk2_sz)) ||
(domctl.u.vcpuextstate.xfeature_mask) ?
(write_exact(io_fd, "xcnt", 4) ||
write_exact(io_fd, &chunk3_sz, sizeof(chunk3_sz)) ||
write_exact(io_fd, &xcnt_size, 4)) :
0 )
{
PERROR("write: extended info");
goto out;
}
}
if ( write_exact(io_fd, p2m_frame_list,
P2M_FL_ENTRIES * sizeof(xen_pfn_t)) )
{
PERROR("write: p2m_frame_list");
goto out;
}
success = 1;
out:
if ( !success && p2m )
munmap(p2m, P2M_FL_ENTRIES * PAGE_SIZE);
if ( live_p2m_frame_list_list )
munmap(live_p2m_frame_list_list, PAGE_SIZE);
if ( live_p2m_frame_list )
munmap(live_p2m_frame_list, P2M_FLL_ENTRIES * PAGE_SIZE);
if ( p2m_frame_list_list )
free(p2m_frame_list_list);
if ( p2m_frame_list )
free(p2m_frame_list);
return success ? p2m : NULL;
}
/* must be done AFTER suspend_and_state() */
static int save_tsc_info(xc_interface *xch, uint32_t dom, int io_fd)
{
int marker = XC_SAVE_ID_TSC_INFO;
uint32_t tsc_mode, khz, incarn;
uint64_t nsec;
if ( xc_domain_get_tsc_info(xch, dom, &tsc_mode,
&nsec, &khz, &incarn) < 0 ||
write_exact(io_fd, &marker, sizeof(marker)) ||
write_exact(io_fd, &tsc_mode, sizeof(tsc_mode)) ||
write_exact(io_fd, &nsec, sizeof(nsec)) ||
write_exact(io_fd, &khz, sizeof(khz)) ||
write_exact(io_fd, &incarn, sizeof(incarn)) )
return -1;
return 0;
}
int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom, uint32_t max_iters,
uint32_t max_factor, uint32_t flags,
struct save_callbacks* callbacks, int hvm,
unsigned long vm_generationid_addr)
{
xc_dominfo_t info;
DECLARE_DOMCTL;
int rc = 1, frc, i, j, last_iter = 0, iter = 0;
int live = (flags & XCFLAGS_LIVE);
int debug = (flags & XCFLAGS_DEBUG);
int superpages = !!hvm;
int race = 0, sent_last_iter, skip_this_iter = 0;
unsigned int sent_this_iter = 0;
int tmem_saved = 0;
/* The new domain's shared-info frame number. */
unsigned long shared_info_frame;
/* A copy of the CPU context of the guest. */
vcpu_guest_context_any_t ctxt;
/* A table containing the type of each PFN (/not/ MFN!). */
xen_pfn_t *pfn_type = NULL;
unsigned long *pfn_batch = NULL;
int *pfn_err = NULL;
/* A copy of one frame of guest memory. */
char page[PAGE_SIZE];
/* Live mapping of shared info structure */
shared_info_any_t *live_shinfo = NULL;
/* base of the region in which domain memory is mapped */
unsigned char *region_base = NULL;
/* A copy of the CPU eXtended States of the guest. */
DECLARE_HYPERCALL_BUFFER(void, buffer);
/* bitmap of pages:
- that should be sent this iteration (unless later marked as skip);
- to skip this iteration because already dirty;
- to fixup by sending at the end if not already resent; */
DECLARE_HYPERCALL_BUFFER(unsigned long, to_skip);
DECLARE_HYPERCALL_BUFFER(unsigned long, to_send);
unsigned long *to_fix = NULL;
struct time_stats time_stats;
xc_shadow_op_stats_t shadow_stats;
unsigned long needed_to_fix = 0;
unsigned long total_sent = 0;
uint64_t vcpumap[XC_SR_MAX_VCPUS/64] = { 1ULL };
/* HVM: a buffer for holding HVM context */
uint32_t hvm_buf_size = 0;
uint8_t *hvm_buf = NULL;
/* HVM: magic frames for ioreqs and xenstore comms. */
uint64_t magic_pfns[3]; /* ioreq_pfn, bufioreq_pfn, store_pfn */
unsigned long mfn;
/* Without checkpoint compression, the dirty pages, pfn arrays
* and tailbuf (vcpu ctx, shared info page, etc.) are written
* directly to outbuf. All of this is done while the domain is
* suspended.
*
* When checkpoint compression is enabled, the dirty pages are
* buffered, compressed "after" the domain is resumed and then
* written to outbuf. Since tailbuf data are collected while a
* domain is suspended, they cannot be directly written to the
* outbuf as there is no dirty page data preceeding tailbuf.
*
* So,two output buffers are maintained. Tailbuf data goes into
* ob_tailbuf. The dirty pages are compressed after resuming the
* domain and written to ob_pagebuf. ob_tailbuf is then appended
* to ob_pagebuf and finally flushed out.
*/
struct outbuf ob_pagebuf, ob_tailbuf, *ob = NULL;
struct save_ctx _ctx;
struct save_ctx *ctx = &_ctx;
struct domain_info_context *dinfo = &ctx->dinfo;
/* Compression context */
comp_ctx *compress_ctx= NULL;
/* Even if XCFLAGS_CHECKPOINT_COMPRESS is set, we enable compression only
* after sending XC_SAVE_ID_ENABLE_COMPRESSION and the tailbuf for
* first time.
*/
int compressing = 0;
int completed = 0;
DPRINTF("%s: starting save of domid %u", __func__, dom);
if ( hvm && !callbacks->switch_qemu_logdirty )
{
ERROR("No switch_qemu_logdirty callback provided.");
errno = EINVAL;
return 1;
}
outbuf_init(xch, &ob_pagebuf, OUTBUF_SIZE);
memset(ctx, 0, sizeof(*ctx));
/* If no explicit control parameters given, use defaults */
max_iters = max_iters ? : DEF_MAX_ITERS;
max_factor = max_factor ? : DEF_MAX_FACTOR;
if ( !get_platform_info(xch, dom,
&ctx->max_mfn, &ctx->hvirt_start, &ctx->pt_levels, &dinfo->guest_width) )
{
ERROR("Unable to get platform info.");
return 1;
}
if ( xc_domain_getinfo(xch, dom, 1, &info) != 1 )
{
PERROR("Could not get domain info");
return 1;
}
shared_info_frame = info.shared_info_frame;
/* Map the shared info frame */
if ( !hvm )
{
live_shinfo = xc_map_foreign_range(xch, dom, PAGE_SIZE,
PROT_READ, shared_info_frame);
if ( !live_shinfo )
{
PERROR("Couldn't map live_shinfo");
goto out;
}
}
/* Get the size of the P2M table */
dinfo->p2m_size = xc_domain_maximum_gpfn(xch, dom) + 1;
if ( dinfo->p2m_size > ~XEN_DOMCTL_PFINFO_LTAB_MASK )
{
ERROR("Cannot save this big a guest");
goto out;
}
/* Domain is still running at this point */
if ( live )
{
/* Live suspend. Enable log-dirty mode. */
if ( xc_shadow_control(xch, dom,
XEN_DOMCTL_SHADOW_OP_ENABLE_LOGDIRTY,
NULL, 0, NULL, 0, NULL) < 0 )
{
/* log-dirty already enabled? There's no test op,
so attempt to disable then reenable it */
frc = xc_shadow_control(xch, dom, XEN_DOMCTL_SHADOW_OP_OFF,
NULL, 0, NULL, 0, NULL);
if ( frc >= 0 )
{
frc = xc_shadow_control(xch, dom,
XEN_DOMCTL_SHADOW_OP_ENABLE_LOGDIRTY,
NULL, 0, NULL, 0, NULL);
}
if ( frc < 0 )
{
PERROR("Couldn't enable shadow mode (rc %d) (errno %d)", frc, errno );
goto out;
}
}
/* Enable qemu-dm logging dirty pages to xen */
if ( hvm && callbacks->switch_qemu_logdirty(dom, 1, callbacks->data) )
{
PERROR("Couldn't enable qemu log-dirty mode (errno %d)", errno);
goto out;
}
}
else
{
/* This is a non-live suspend. Suspend the domain .*/
if ( suspend_and_state(callbacks->suspend, callbacks->data, xch,
io_fd, dom, &info) )
{
ERROR("Domain appears not to have suspended");
goto out;
}
}
if ( flags & XCFLAGS_CHECKPOINT_COMPRESS )
{
if (!(compress_ctx = xc_compression_create_context(xch, dinfo->p2m_size)))
{
ERROR("Failed to create compression context");
goto out;