xref: /qemu/migration/savevm.c (revision 164c374b)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2009-2015 Red Hat Inc
6  *
7  * Authors:
8  *  Juan Quintela <quintela@redhat.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28 
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
32 #include "net/net.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "ram.h"
40 #include "qemu-file-channel.h"
41 #include "qemu-file.h"
42 #include "savevm.h"
43 #include "postcopy-ram.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-migration.h"
46 #include "qapi/qapi-commands-misc.h"
47 #include "qapi/qmp/qerror.h"
48 #include "qemu/error-report.h"
49 #include "sysemu/cpus.h"
50 #include "exec/memory.h"
51 #include "exec/target_page.h"
52 #include "trace.h"
53 #include "qemu/iov.h"
54 #include "qemu/main-loop.h"
55 #include "block/snapshot.h"
56 #include "qemu/cutils.h"
57 #include "io/channel-buffer.h"
58 #include "io/channel-file.h"
59 #include "sysemu/replay.h"
60 #include "sysemu/runstate.h"
61 #include "sysemu/sysemu.h"
62 #include "qjson.h"
63 #include "migration/colo.h"
64 #include "qemu/bitmap.h"
65 #include "net/announce.h"
66 
67 const unsigned int postcopy_ram_discard_version = 0;
68 
69 /* Subcommands for QEMU_VM_COMMAND */
70 enum qemu_vm_cmd {
71     MIG_CMD_INVALID = 0,   /* Must be 0 */
72     MIG_CMD_OPEN_RETURN_PATH,  /* Tell the dest to open the Return path */
73     MIG_CMD_PING,              /* Request a PONG on the RP */
74 
75     MIG_CMD_POSTCOPY_ADVISE,       /* Prior to any page transfers, just
76                                       warn we might want to do PC */
77     MIG_CMD_POSTCOPY_LISTEN,       /* Start listening for incoming
78                                       pages as it's running. */
79     MIG_CMD_POSTCOPY_RUN,          /* Start execution */
80 
81     MIG_CMD_POSTCOPY_RAM_DISCARD,  /* A list of pages to discard that
82                                       were previously sent during
83                                       precopy but are dirty. */
84     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
85     MIG_CMD_ENABLE_COLO,       /* Enable COLO */
86     MIG_CMD_POSTCOPY_RESUME,   /* resume postcopy on dest */
87     MIG_CMD_RECV_BITMAP,       /* Request for recved bitmap on dst */
88     MIG_CMD_MAX
89 };
90 
91 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
92 static struct mig_cmd_args {
93     ssize_t     len; /* -1 = variable */
94     const char *name;
95 } mig_cmd_args[] = {
96     [MIG_CMD_INVALID]          = { .len = -1, .name = "INVALID" },
97     [MIG_CMD_OPEN_RETURN_PATH] = { .len =  0, .name = "OPEN_RETURN_PATH" },
98     [MIG_CMD_PING]             = { .len = sizeof(uint32_t), .name = "PING" },
99     [MIG_CMD_POSTCOPY_ADVISE]  = { .len = -1, .name = "POSTCOPY_ADVISE" },
100     [MIG_CMD_POSTCOPY_LISTEN]  = { .len =  0, .name = "POSTCOPY_LISTEN" },
101     [MIG_CMD_POSTCOPY_RUN]     = { .len =  0, .name = "POSTCOPY_RUN" },
102     [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
103                                    .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
104     [MIG_CMD_POSTCOPY_RESUME]  = { .len =  0, .name = "POSTCOPY_RESUME" },
105     [MIG_CMD_PACKAGED]         = { .len =  4, .name = "PACKAGED" },
106     [MIG_CMD_RECV_BITMAP]      = { .len = -1, .name = "RECV_BITMAP" },
107     [MIG_CMD_MAX]              = { .len = -1, .name = "MAX" },
108 };
109 
110 /* Note for MIG_CMD_POSTCOPY_ADVISE:
111  * The format of arguments is depending on postcopy mode:
112  * - postcopy RAM only
113  *   uint64_t host page size
114  *   uint64_t taget page size
115  *
116  * - postcopy RAM and postcopy dirty bitmaps
117  *   format is the same as for postcopy RAM only
118  *
119  * - postcopy dirty bitmaps only
120  *   Nothing. Command length field is 0.
121  *
122  * Be careful: adding a new postcopy entity with some other parameters should
123  * not break format self-description ability. Good way is to introduce some
124  * generic extendable format with an exception for two old entities.
125  */
126 
127 /***********************************************************/
128 /* savevm/loadvm support */
129 
130 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
131                                    int64_t pos, Error **errp)
132 {
133     int ret;
134     QEMUIOVector qiov;
135 
136     qemu_iovec_init_external(&qiov, iov, iovcnt);
137     ret = bdrv_writev_vmstate(opaque, &qiov, pos);
138     if (ret < 0) {
139         return ret;
140     }
141 
142     return qiov.size;
143 }
144 
145 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
146                                 size_t size, Error **errp)
147 {
148     return bdrv_load_vmstate(opaque, buf, pos, size);
149 }
150 
151 static int bdrv_fclose(void *opaque, Error **errp)
152 {
153     return bdrv_flush(opaque);
154 }
155 
156 static const QEMUFileOps bdrv_read_ops = {
157     .get_buffer = block_get_buffer,
158     .close =      bdrv_fclose
159 };
160 
161 static const QEMUFileOps bdrv_write_ops = {
162     .writev_buffer  = block_writev_buffer,
163     .close          = bdrv_fclose
164 };
165 
166 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
167 {
168     if (is_writable) {
169         return qemu_fopen_ops(bs, &bdrv_write_ops);
170     }
171     return qemu_fopen_ops(bs, &bdrv_read_ops);
172 }
173 
174 
175 /* QEMUFile timer support.
176  * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
177  */
178 
179 void timer_put(QEMUFile *f, QEMUTimer *ts)
180 {
181     uint64_t expire_time;
182 
183     expire_time = timer_expire_time_ns(ts);
184     qemu_put_be64(f, expire_time);
185 }
186 
187 void timer_get(QEMUFile *f, QEMUTimer *ts)
188 {
189     uint64_t expire_time;
190 
191     expire_time = qemu_get_be64(f);
192     if (expire_time != -1) {
193         timer_mod_ns(ts, expire_time);
194     } else {
195         timer_del(ts);
196     }
197 }
198 
199 
200 /* VMState timer support.
201  * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
202  */
203 
204 static int get_timer(QEMUFile *f, void *pv, size_t size,
205                      const VMStateField *field)
206 {
207     QEMUTimer *v = pv;
208     timer_get(f, v);
209     return 0;
210 }
211 
212 static int put_timer(QEMUFile *f, void *pv, size_t size,
213                      const VMStateField *field, QJSON *vmdesc)
214 {
215     QEMUTimer *v = pv;
216     timer_put(f, v);
217 
218     return 0;
219 }
220 
221 const VMStateInfo vmstate_info_timer = {
222     .name = "timer",
223     .get  = get_timer,
224     .put  = put_timer,
225 };
226 
227 
228 typedef struct CompatEntry {
229     char idstr[256];
230     int instance_id;
231 } CompatEntry;
232 
233 typedef struct SaveStateEntry {
234     QTAILQ_ENTRY(SaveStateEntry) entry;
235     char idstr[256];
236     uint32_t instance_id;
237     int alias_id;
238     int version_id;
239     /* version id read from the stream */
240     int load_version_id;
241     int section_id;
242     /* section id read from the stream */
243     int load_section_id;
244     const SaveVMHandlers *ops;
245     const VMStateDescription *vmsd;
246     void *opaque;
247     CompatEntry *compat;
248     int is_ram;
249 } SaveStateEntry;
250 
251 typedef struct SaveState {
252     QTAILQ_HEAD(, SaveStateEntry) handlers;
253     SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1];
254     int global_section_id;
255     uint32_t len;
256     const char *name;
257     uint32_t target_page_bits;
258     uint32_t caps_count;
259     MigrationCapability *capabilities;
260     QemuUUID uuid;
261 } SaveState;
262 
263 static SaveState savevm_state = {
264     .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
265     .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL },
266     .global_section_id = 0,
267 };
268 
269 static bool should_validate_capability(int capability)
270 {
271     assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
272     /* Validate only new capabilities to keep compatibility. */
273     switch (capability) {
274     case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
275         return true;
276     default:
277         return false;
278     }
279 }
280 
281 static uint32_t get_validatable_capabilities_count(void)
282 {
283     MigrationState *s = migrate_get_current();
284     uint32_t result = 0;
285     int i;
286     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
287         if (should_validate_capability(i) && s->enabled_capabilities[i]) {
288             result++;
289         }
290     }
291     return result;
292 }
293 
294 static int configuration_pre_save(void *opaque)
295 {
296     SaveState *state = opaque;
297     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
298     MigrationState *s = migrate_get_current();
299     int i, j;
300 
301     state->len = strlen(current_name);
302     state->name = current_name;
303     state->target_page_bits = qemu_target_page_bits();
304 
305     state->caps_count = get_validatable_capabilities_count();
306     state->capabilities = g_renew(MigrationCapability, state->capabilities,
307                                   state->caps_count);
308     for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
309         if (should_validate_capability(i) && s->enabled_capabilities[i]) {
310             state->capabilities[j++] = i;
311         }
312     }
313     state->uuid = qemu_uuid;
314 
315     return 0;
316 }
317 
318 static int configuration_pre_load(void *opaque)
319 {
320     SaveState *state = opaque;
321 
322     /* If there is no target-page-bits subsection it means the source
323      * predates the variable-target-page-bits support and is using the
324      * minimum possible value for this CPU.
325      */
326     state->target_page_bits = qemu_target_page_bits_min();
327     return 0;
328 }
329 
330 static bool configuration_validate_capabilities(SaveState *state)
331 {
332     bool ret = true;
333     MigrationState *s = migrate_get_current();
334     unsigned long *source_caps_bm;
335     int i;
336 
337     source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
338     for (i = 0; i < state->caps_count; i++) {
339         MigrationCapability capability = state->capabilities[i];
340         set_bit(capability, source_caps_bm);
341     }
342 
343     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
344         bool source_state, target_state;
345         if (!should_validate_capability(i)) {
346             continue;
347         }
348         source_state = test_bit(i, source_caps_bm);
349         target_state = s->enabled_capabilities[i];
350         if (source_state != target_state) {
351             error_report("Capability %s is %s, but received capability is %s",
352                          MigrationCapability_str(i),
353                          target_state ? "on" : "off",
354                          source_state ? "on" : "off");
355             ret = false;
356             /* Don't break here to report all failed capabilities */
357         }
358     }
359 
360     g_free(source_caps_bm);
361     return ret;
362 }
363 
364 static int configuration_post_load(void *opaque, int version_id)
365 {
366     SaveState *state = opaque;
367     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
368 
369     if (strncmp(state->name, current_name, state->len) != 0) {
370         error_report("Machine type received is '%.*s' and local is '%s'",
371                      (int) state->len, state->name, current_name);
372         return -EINVAL;
373     }
374 
375     if (state->target_page_bits != qemu_target_page_bits()) {
376         error_report("Received TARGET_PAGE_BITS is %d but local is %d",
377                      state->target_page_bits, qemu_target_page_bits());
378         return -EINVAL;
379     }
380 
381     if (!configuration_validate_capabilities(state)) {
382         return -EINVAL;
383     }
384 
385     return 0;
386 }
387 
388 static int get_capability(QEMUFile *f, void *pv, size_t size,
389                           const VMStateField *field)
390 {
391     MigrationCapability *capability = pv;
392     char capability_str[UINT8_MAX + 1];
393     uint8_t len;
394     int i;
395 
396     len = qemu_get_byte(f);
397     qemu_get_buffer(f, (uint8_t *)capability_str, len);
398     capability_str[len] = '\0';
399     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
400         if (!strcmp(MigrationCapability_str(i), capability_str)) {
401             *capability = i;
402             return 0;
403         }
404     }
405     error_report("Received unknown capability %s", capability_str);
406     return -EINVAL;
407 }
408 
409 static int put_capability(QEMUFile *f, void *pv, size_t size,
410                           const VMStateField *field, QJSON *vmdesc)
411 {
412     MigrationCapability *capability = pv;
413     const char *capability_str = MigrationCapability_str(*capability);
414     size_t len = strlen(capability_str);
415     assert(len <= UINT8_MAX);
416 
417     qemu_put_byte(f, len);
418     qemu_put_buffer(f, (uint8_t *)capability_str, len);
419     return 0;
420 }
421 
422 static const VMStateInfo vmstate_info_capability = {
423     .name = "capability",
424     .get  = get_capability,
425     .put  = put_capability,
426 };
427 
428 /* The target-page-bits subsection is present only if the
429  * target page size is not the same as the default (ie the
430  * minimum page size for a variable-page-size guest CPU).
431  * If it is present then it contains the actual target page
432  * bits for the machine, and migration will fail if the
433  * two ends don't agree about it.
434  */
435 static bool vmstate_target_page_bits_needed(void *opaque)
436 {
437     return qemu_target_page_bits()
438         > qemu_target_page_bits_min();
439 }
440 
441 static const VMStateDescription vmstate_target_page_bits = {
442     .name = "configuration/target-page-bits",
443     .version_id = 1,
444     .minimum_version_id = 1,
445     .needed = vmstate_target_page_bits_needed,
446     .fields = (VMStateField[]) {
447         VMSTATE_UINT32(target_page_bits, SaveState),
448         VMSTATE_END_OF_LIST()
449     }
450 };
451 
452 static bool vmstate_capabilites_needed(void *opaque)
453 {
454     return get_validatable_capabilities_count() > 0;
455 }
456 
457 static const VMStateDescription vmstate_capabilites = {
458     .name = "configuration/capabilities",
459     .version_id = 1,
460     .minimum_version_id = 1,
461     .needed = vmstate_capabilites_needed,
462     .fields = (VMStateField[]) {
463         VMSTATE_UINT32_V(caps_count, SaveState, 1),
464         VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
465                                     vmstate_info_capability,
466                                     MigrationCapability),
467         VMSTATE_END_OF_LIST()
468     }
469 };
470 
471 static bool vmstate_uuid_needed(void *opaque)
472 {
473     return qemu_uuid_set && migrate_validate_uuid();
474 }
475 
476 static int vmstate_uuid_post_load(void *opaque, int version_id)
477 {
478     SaveState *state = opaque;
479     char uuid_src[UUID_FMT_LEN + 1];
480     char uuid_dst[UUID_FMT_LEN + 1];
481 
482     if (!qemu_uuid_set) {
483         /*
484          * It's warning because user might not know UUID in some cases,
485          * e.g. load an old snapshot
486          */
487         qemu_uuid_unparse(&state->uuid, uuid_src);
488         warn_report("UUID is received %s, but local uuid isn't set",
489                      uuid_src);
490         return 0;
491     }
492     if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) {
493         qemu_uuid_unparse(&state->uuid, uuid_src);
494         qemu_uuid_unparse(&qemu_uuid, uuid_dst);
495         error_report("UUID received is %s and local is %s", uuid_src, uuid_dst);
496         return -EINVAL;
497     }
498     return 0;
499 }
500 
501 static const VMStateDescription vmstate_uuid = {
502     .name = "configuration/uuid",
503     .version_id = 1,
504     .minimum_version_id = 1,
505     .needed = vmstate_uuid_needed,
506     .post_load = vmstate_uuid_post_load,
507     .fields = (VMStateField[]) {
508         VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1),
509         VMSTATE_END_OF_LIST()
510     }
511 };
512 
513 static const VMStateDescription vmstate_configuration = {
514     .name = "configuration",
515     .version_id = 1,
516     .pre_load = configuration_pre_load,
517     .post_load = configuration_post_load,
518     .pre_save = configuration_pre_save,
519     .fields = (VMStateField[]) {
520         VMSTATE_UINT32(len, SaveState),
521         VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
522         VMSTATE_END_OF_LIST()
523     },
524     .subsections = (const VMStateDescription*[]) {
525         &vmstate_target_page_bits,
526         &vmstate_capabilites,
527         &vmstate_uuid,
528         NULL
529     }
530 };
531 
532 static void dump_vmstate_vmsd(FILE *out_file,
533                               const VMStateDescription *vmsd, int indent,
534                               bool is_subsection);
535 
536 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
537                               int indent)
538 {
539     fprintf(out_file, "%*s{\n", indent, "");
540     indent += 2;
541     fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
542     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
543             field->version_id);
544     fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
545             field->field_exists ? "true" : "false");
546     fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
547     if (field->vmsd != NULL) {
548         fprintf(out_file, ",\n");
549         dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
550     }
551     fprintf(out_file, "\n%*s}", indent - 2, "");
552 }
553 
554 static void dump_vmstate_vmss(FILE *out_file,
555                               const VMStateDescription **subsection,
556                               int indent)
557 {
558     if (*subsection != NULL) {
559         dump_vmstate_vmsd(out_file, *subsection, indent, true);
560     }
561 }
562 
563 static void dump_vmstate_vmsd(FILE *out_file,
564                               const VMStateDescription *vmsd, int indent,
565                               bool is_subsection)
566 {
567     if (is_subsection) {
568         fprintf(out_file, "%*s{\n", indent, "");
569     } else {
570         fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
571     }
572     indent += 2;
573     fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
574     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
575             vmsd->version_id);
576     fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
577             vmsd->minimum_version_id);
578     if (vmsd->fields != NULL) {
579         const VMStateField *field = vmsd->fields;
580         bool first;
581 
582         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
583         first = true;
584         while (field->name != NULL) {
585             if (field->flags & VMS_MUST_EXIST) {
586                 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
587                 field++;
588                 continue;
589             }
590             if (!first) {
591                 fprintf(out_file, ",\n");
592             }
593             dump_vmstate_vmsf(out_file, field, indent + 2);
594             field++;
595             first = false;
596         }
597         fprintf(out_file, "\n%*s]", indent, "");
598     }
599     if (vmsd->subsections != NULL) {
600         const VMStateDescription **subsection = vmsd->subsections;
601         bool first;
602 
603         fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
604         first = true;
605         while (*subsection != NULL) {
606             if (!first) {
607                 fprintf(out_file, ",\n");
608             }
609             dump_vmstate_vmss(out_file, subsection, indent + 2);
610             subsection++;
611             first = false;
612         }
613         fprintf(out_file, "\n%*s]", indent, "");
614     }
615     fprintf(out_file, "\n%*s}", indent - 2, "");
616 }
617 
618 static void dump_machine_type(FILE *out_file)
619 {
620     MachineClass *mc;
621 
622     mc = MACHINE_GET_CLASS(current_machine);
623 
624     fprintf(out_file, "  \"vmschkmachine\": {\n");
625     fprintf(out_file, "    \"Name\": \"%s\"\n", mc->name);
626     fprintf(out_file, "  },\n");
627 }
628 
629 void dump_vmstate_json_to_file(FILE *out_file)
630 {
631     GSList *list, *elt;
632     bool first;
633 
634     fprintf(out_file, "{\n");
635     dump_machine_type(out_file);
636 
637     first = true;
638     list = object_class_get_list(TYPE_DEVICE, true);
639     for (elt = list; elt; elt = elt->next) {
640         DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
641                                              TYPE_DEVICE);
642         const char *name;
643         int indent = 2;
644 
645         if (!dc->vmsd) {
646             continue;
647         }
648 
649         if (!first) {
650             fprintf(out_file, ",\n");
651         }
652         name = object_class_get_name(OBJECT_CLASS(dc));
653         fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
654         indent += 2;
655         fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
656         fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
657                 dc->vmsd->version_id);
658         fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
659                 dc->vmsd->minimum_version_id);
660 
661         dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
662 
663         fprintf(out_file, "\n%*s}", indent - 2, "");
664         first = false;
665     }
666     fprintf(out_file, "\n}\n");
667     fclose(out_file);
668 }
669 
670 static uint32_t calculate_new_instance_id(const char *idstr)
671 {
672     SaveStateEntry *se;
673     uint32_t instance_id = 0;
674 
675     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
676         if (strcmp(idstr, se->idstr) == 0
677             && instance_id <= se->instance_id) {
678             instance_id = se->instance_id + 1;
679         }
680     }
681     /* Make sure we never loop over without being noticed */
682     assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
683     return instance_id;
684 }
685 
686 static int calculate_compat_instance_id(const char *idstr)
687 {
688     SaveStateEntry *se;
689     int instance_id = 0;
690 
691     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
692         if (!se->compat) {
693             continue;
694         }
695 
696         if (strcmp(idstr, se->compat->idstr) == 0
697             && instance_id <= se->compat->instance_id) {
698             instance_id = se->compat->instance_id + 1;
699         }
700     }
701     return instance_id;
702 }
703 
704 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
705 {
706     if (se->vmsd) {
707         return se->vmsd->priority;
708     }
709     return MIG_PRI_DEFAULT;
710 }
711 
712 static void savevm_state_handler_insert(SaveStateEntry *nse)
713 {
714     MigrationPriority priority = save_state_priority(nse);
715     SaveStateEntry *se;
716     int i;
717 
718     assert(priority <= MIG_PRI_MAX);
719 
720     for (i = priority - 1; i >= 0; i--) {
721         se = savevm_state.handler_pri_head[i];
722         if (se != NULL) {
723             assert(save_state_priority(se) < priority);
724             break;
725         }
726     }
727 
728     if (i >= 0) {
729         QTAILQ_INSERT_BEFORE(se, nse, entry);
730     } else {
731         QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
732     }
733 
734     if (savevm_state.handler_pri_head[priority] == NULL) {
735         savevm_state.handler_pri_head[priority] = nse;
736     }
737 }
738 
739 static void savevm_state_handler_remove(SaveStateEntry *se)
740 {
741     SaveStateEntry *next;
742     MigrationPriority priority = save_state_priority(se);
743 
744     if (se == savevm_state.handler_pri_head[priority]) {
745         next = QTAILQ_NEXT(se, entry);
746         if (next != NULL && save_state_priority(next) == priority) {
747             savevm_state.handler_pri_head[priority] = next;
748         } else {
749             savevm_state.handler_pri_head[priority] = NULL;
750         }
751     }
752     QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
753 }
754 
755 /* TODO: Individual devices generally have very little idea about the rest
756    of the system, so instance_id should be removed/replaced.
757    Meanwhile pass -1 as instance_id if you do not already have a clearly
758    distinguishing id for all instances of your device class. */
759 int register_savevm_live(const char *idstr,
760                          uint32_t instance_id,
761                          int version_id,
762                          const SaveVMHandlers *ops,
763                          void *opaque)
764 {
765     SaveStateEntry *se;
766 
767     se = g_new0(SaveStateEntry, 1);
768     se->version_id = version_id;
769     se->section_id = savevm_state.global_section_id++;
770     se->ops = ops;
771     se->opaque = opaque;
772     se->vmsd = NULL;
773     /* if this is a live_savem then set is_ram */
774     if (ops->save_setup != NULL) {
775         se->is_ram = 1;
776     }
777 
778     pstrcat(se->idstr, sizeof(se->idstr), idstr);
779 
780     if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
781         se->instance_id = calculate_new_instance_id(se->idstr);
782     } else {
783         se->instance_id = instance_id;
784     }
785     assert(!se->compat || se->instance_id == 0);
786     savevm_state_handler_insert(se);
787     return 0;
788 }
789 
790 void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque)
791 {
792     SaveStateEntry *se, *new_se;
793     char id[256] = "";
794 
795     if (obj) {
796         char *oid = vmstate_if_get_id(obj);
797         if (oid) {
798             pstrcpy(id, sizeof(id), oid);
799             pstrcat(id, sizeof(id), "/");
800             g_free(oid);
801         }
802     }
803     pstrcat(id, sizeof(id), idstr);
804 
805     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
806         if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
807             savevm_state_handler_remove(se);
808             g_free(se->compat);
809             g_free(se);
810         }
811     }
812 }
813 
814 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
815                                    const VMStateDescription *vmsd,
816                                    void *opaque, int alias_id,
817                                    int required_for_version,
818                                    Error **errp)
819 {
820     SaveStateEntry *se;
821 
822     /* If this triggers, alias support can be dropped for the vmsd. */
823     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
824 
825     se = g_new0(SaveStateEntry, 1);
826     se->version_id = vmsd->version_id;
827     se->section_id = savevm_state.global_section_id++;
828     se->opaque = opaque;
829     se->vmsd = vmsd;
830     se->alias_id = alias_id;
831 
832     if (obj) {
833         char *id = vmstate_if_get_id(obj);
834         if (id) {
835             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
836                 sizeof(se->idstr)) {
837                 error_setg(errp, "Path too long for VMState (%s)", id);
838                 g_free(id);
839                 g_free(se);
840 
841                 return -1;
842             }
843             g_free(id);
844 
845             se->compat = g_new0(CompatEntry, 1);
846             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
847             se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ?
848                          calculate_compat_instance_id(vmsd->name) : instance_id;
849             instance_id = VMSTATE_INSTANCE_ID_ANY;
850         }
851     }
852     pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
853 
854     if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
855         se->instance_id = calculate_new_instance_id(se->idstr);
856     } else {
857         se->instance_id = instance_id;
858     }
859     assert(!se->compat || se->instance_id == 0);
860     savevm_state_handler_insert(se);
861     return 0;
862 }
863 
864 void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
865                         void *opaque)
866 {
867     SaveStateEntry *se, *new_se;
868 
869     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
870         if (se->vmsd == vmsd && se->opaque == opaque) {
871             savevm_state_handler_remove(se);
872             g_free(se->compat);
873             g_free(se);
874         }
875     }
876 }
877 
878 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
879 {
880     trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
881     if (!se->vmsd) {         /* Old style */
882         return se->ops->load_state(f, se->opaque, se->load_version_id);
883     }
884     return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
885 }
886 
887 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
888 {
889     int64_t old_offset, size;
890 
891     old_offset = qemu_ftell_fast(f);
892     se->ops->save_state(f, se->opaque);
893     size = qemu_ftell_fast(f) - old_offset;
894 
895     if (vmdesc) {
896         json_prop_int(vmdesc, "size", size);
897         json_start_array(vmdesc, "fields");
898         json_start_object(vmdesc, NULL);
899         json_prop_str(vmdesc, "name", "data");
900         json_prop_int(vmdesc, "size", size);
901         json_prop_str(vmdesc, "type", "buffer");
902         json_end_object(vmdesc);
903         json_end_array(vmdesc);
904     }
905 }
906 
907 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
908 {
909     trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
910     if (!se->vmsd) {
911         vmstate_save_old_style(f, se, vmdesc);
912         return 0;
913     }
914     return vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
915 }
916 
917 /*
918  * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
919  */
920 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
921                                 uint8_t section_type)
922 {
923     qemu_put_byte(f, section_type);
924     qemu_put_be32(f, se->section_id);
925 
926     if (section_type == QEMU_VM_SECTION_FULL ||
927         section_type == QEMU_VM_SECTION_START) {
928         /* ID string */
929         size_t len = strlen(se->idstr);
930         qemu_put_byte(f, len);
931         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
932 
933         qemu_put_be32(f, se->instance_id);
934         qemu_put_be32(f, se->version_id);
935     }
936 }
937 
938 /*
939  * Write a footer onto device sections that catches cases misformatted device
940  * sections.
941  */
942 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
943 {
944     if (migrate_get_current()->send_section_footer) {
945         qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
946         qemu_put_be32(f, se->section_id);
947     }
948 }
949 
950 /**
951  * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
952  *                           command and associated data.
953  *
954  * @f: File to send command on
955  * @command: Command type to send
956  * @len: Length of associated data
957  * @data: Data associated with command.
958  */
959 static void qemu_savevm_command_send(QEMUFile *f,
960                                      enum qemu_vm_cmd command,
961                                      uint16_t len,
962                                      uint8_t *data)
963 {
964     trace_savevm_command_send(command, len);
965     qemu_put_byte(f, QEMU_VM_COMMAND);
966     qemu_put_be16(f, (uint16_t)command);
967     qemu_put_be16(f, len);
968     qemu_put_buffer(f, data, len);
969     qemu_fflush(f);
970 }
971 
972 void qemu_savevm_send_colo_enable(QEMUFile *f)
973 {
974     trace_savevm_send_colo_enable();
975     qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
976 }
977 
978 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
979 {
980     uint32_t buf;
981 
982     trace_savevm_send_ping(value);
983     buf = cpu_to_be32(value);
984     qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
985 }
986 
987 void qemu_savevm_send_open_return_path(QEMUFile *f)
988 {
989     trace_savevm_send_open_return_path();
990     qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
991 }
992 
993 /* We have a buffer of data to send; we don't want that all to be loaded
994  * by the command itself, so the command contains just the length of the
995  * extra buffer that we then send straight after it.
996  * TODO: Must be a better way to organise that
997  *
998  * Returns:
999  *    0 on success
1000  *    -ve on error
1001  */
1002 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
1003 {
1004     uint32_t tmp;
1005 
1006     if (len > MAX_VM_CMD_PACKAGED_SIZE) {
1007         error_report("%s: Unreasonably large packaged state: %zu",
1008                      __func__, len);
1009         return -1;
1010     }
1011 
1012     tmp = cpu_to_be32(len);
1013 
1014     trace_qemu_savevm_send_packaged();
1015     qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
1016 
1017     qemu_put_buffer(f, buf, len);
1018 
1019     return 0;
1020 }
1021 
1022 /* Send prior to any postcopy transfer */
1023 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
1024 {
1025     if (migrate_postcopy_ram()) {
1026         uint64_t tmp[2];
1027         tmp[0] = cpu_to_be64(ram_pagesize_summary());
1028         tmp[1] = cpu_to_be64(qemu_target_page_size());
1029 
1030         trace_qemu_savevm_send_postcopy_advise();
1031         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
1032                                  16, (uint8_t *)tmp);
1033     } else {
1034         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
1035     }
1036 }
1037 
1038 /* Sent prior to starting the destination running in postcopy, discard pages
1039  * that have already been sent but redirtied on the source.
1040  * CMD_POSTCOPY_RAM_DISCARD consist of:
1041  *      byte   version (0)
1042  *      byte   Length of name field (not including 0)
1043  *  n x byte   RAM block name
1044  *      byte   0 terminator (just for safety)
1045  *  n x        Byte ranges within the named RAMBlock
1046  *      be64   Start of the range
1047  *      be64   Length
1048  *
1049  *  name:  RAMBlock name that these entries are part of
1050  *  len: Number of page entries
1051  *  start_list: 'len' addresses
1052  *  length_list: 'len' addresses
1053  *
1054  */
1055 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1056                                            uint16_t len,
1057                                            uint64_t *start_list,
1058                                            uint64_t *length_list)
1059 {
1060     uint8_t *buf;
1061     uint16_t tmplen;
1062     uint16_t t;
1063     size_t name_len = strlen(name);
1064 
1065     trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1066     assert(name_len < 256);
1067     buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1068     buf[0] = postcopy_ram_discard_version;
1069     buf[1] = name_len;
1070     memcpy(buf + 2, name, name_len);
1071     tmplen = 2 + name_len;
1072     buf[tmplen++] = '\0';
1073 
1074     for (t = 0; t < len; t++) {
1075         stq_be_p(buf + tmplen, start_list[t]);
1076         tmplen += 8;
1077         stq_be_p(buf + tmplen, length_list[t]);
1078         tmplen += 8;
1079     }
1080     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1081     g_free(buf);
1082 }
1083 
1084 /* Get the destination into a state where it can receive postcopy data. */
1085 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1086 {
1087     trace_savevm_send_postcopy_listen();
1088     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1089 }
1090 
1091 /* Kick the destination into running */
1092 void qemu_savevm_send_postcopy_run(QEMUFile *f)
1093 {
1094     trace_savevm_send_postcopy_run();
1095     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1096 }
1097 
1098 void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1099 {
1100     trace_savevm_send_postcopy_resume();
1101     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1102 }
1103 
1104 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1105 {
1106     size_t len;
1107     char buf[256];
1108 
1109     trace_savevm_send_recv_bitmap(block_name);
1110 
1111     buf[0] = len = strlen(block_name);
1112     memcpy(buf + 1, block_name, len);
1113 
1114     qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1115 }
1116 
1117 bool qemu_savevm_state_blocked(Error **errp)
1118 {
1119     SaveStateEntry *se;
1120 
1121     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1122         if (se->vmsd && se->vmsd->unmigratable) {
1123             error_setg(errp, "State blocked by non-migratable device '%s'",
1124                        se->idstr);
1125             return true;
1126         }
1127     }
1128     return false;
1129 }
1130 
1131 void qemu_savevm_state_header(QEMUFile *f)
1132 {
1133     trace_savevm_state_header();
1134     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1135     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1136 
1137     if (migrate_get_current()->send_configuration) {
1138         qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1139         vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
1140     }
1141 }
1142 
1143 int qemu_savevm_nr_failover_devices(void)
1144 {
1145     SaveStateEntry *se;
1146     int n = 0;
1147 
1148     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1149         if (se->vmsd && se->vmsd->dev_unplug_pending &&
1150             se->vmsd->dev_unplug_pending(se->opaque)) {
1151             n++;
1152         }
1153     }
1154 
1155     return n;
1156 }
1157 
1158 bool qemu_savevm_state_guest_unplug_pending(void)
1159 {
1160     SaveStateEntry *se;
1161     int n = 0;
1162 
1163     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1164         if (!se->vmsd || !se->vmsd->dev_unplug_pending) {
1165             continue;
1166         }
1167         if (se->vmsd->dev_unplug_pending(se->opaque)) {
1168             n++;
1169         }
1170     }
1171 
1172     return n > 0;
1173 }
1174 
1175 void qemu_savevm_state_setup(QEMUFile *f)
1176 {
1177     SaveStateEntry *se;
1178     Error *local_err = NULL;
1179     int ret;
1180 
1181     trace_savevm_state_setup();
1182     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1183         if (!se->ops || !se->ops->save_setup) {
1184             continue;
1185         }
1186         if (se->ops->is_active) {
1187             if (!se->ops->is_active(se->opaque)) {
1188                 continue;
1189             }
1190         }
1191         save_section_header(f, se, QEMU_VM_SECTION_START);
1192 
1193         ret = se->ops->save_setup(f, se->opaque);
1194         save_section_footer(f, se);
1195         if (ret < 0) {
1196             qemu_file_set_error(f, ret);
1197             break;
1198         }
1199     }
1200 
1201     if (precopy_notify(PRECOPY_NOTIFY_SETUP, &local_err)) {
1202         error_report_err(local_err);
1203     }
1204 }
1205 
1206 int qemu_savevm_state_resume_prepare(MigrationState *s)
1207 {
1208     SaveStateEntry *se;
1209     int ret;
1210 
1211     trace_savevm_state_resume_prepare();
1212 
1213     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1214         if (!se->ops || !se->ops->resume_prepare) {
1215             continue;
1216         }
1217         if (se->ops->is_active) {
1218             if (!se->ops->is_active(se->opaque)) {
1219                 continue;
1220             }
1221         }
1222         ret = se->ops->resume_prepare(s, se->opaque);
1223         if (ret < 0) {
1224             return ret;
1225         }
1226     }
1227 
1228     return 0;
1229 }
1230 
1231 /*
1232  * this function has three return values:
1233  *   negative: there was one error, and we have -errno.
1234  *   0 : We haven't finished, caller have to go again
1235  *   1 : We have finished, we can go to complete phase
1236  */
1237 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1238 {
1239     SaveStateEntry *se;
1240     int ret = 1;
1241 
1242     trace_savevm_state_iterate();
1243     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1244         if (!se->ops || !se->ops->save_live_iterate) {
1245             continue;
1246         }
1247         if (se->ops->is_active &&
1248             !se->ops->is_active(se->opaque)) {
1249             continue;
1250         }
1251         if (se->ops->is_active_iterate &&
1252             !se->ops->is_active_iterate(se->opaque)) {
1253             continue;
1254         }
1255         /*
1256          * In the postcopy phase, any device that doesn't know how to
1257          * do postcopy should have saved it's state in the _complete
1258          * call that's already run, it might get confused if we call
1259          * iterate afterwards.
1260          */
1261         if (postcopy &&
1262             !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
1263             continue;
1264         }
1265         if (qemu_file_rate_limit(f)) {
1266             return 0;
1267         }
1268         trace_savevm_section_start(se->idstr, se->section_id);
1269 
1270         save_section_header(f, se, QEMU_VM_SECTION_PART);
1271 
1272         ret = se->ops->save_live_iterate(f, se->opaque);
1273         trace_savevm_section_end(se->idstr, se->section_id, ret);
1274         save_section_footer(f, se);
1275 
1276         if (ret < 0) {
1277             error_report("failed to save SaveStateEntry with id(name): %d(%s)",
1278                          se->section_id, se->idstr);
1279             qemu_file_set_error(f, ret);
1280         }
1281         if (ret <= 0) {
1282             /* Do not proceed to the next vmstate before this one reported
1283                completion of the current stage. This serializes the migration
1284                and reduces the probability that a faster changing state is
1285                synchronized over and over again. */
1286             break;
1287         }
1288     }
1289     return ret;
1290 }
1291 
1292 static bool should_send_vmdesc(void)
1293 {
1294     MachineState *machine = MACHINE(qdev_get_machine());
1295     bool in_postcopy = migration_in_postcopy();
1296     return !machine->suppress_vmdesc && !in_postcopy;
1297 }
1298 
1299 /*
1300  * Calls the save_live_complete_postcopy methods
1301  * causing the last few pages to be sent immediately and doing any associated
1302  * cleanup.
1303  * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1304  * all the other devices, but that happens at the point we switch to postcopy.
1305  */
1306 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1307 {
1308     SaveStateEntry *se;
1309     int ret;
1310 
1311     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1312         if (!se->ops || !se->ops->save_live_complete_postcopy) {
1313             continue;
1314         }
1315         if (se->ops->is_active) {
1316             if (!se->ops->is_active(se->opaque)) {
1317                 continue;
1318             }
1319         }
1320         trace_savevm_section_start(se->idstr, se->section_id);
1321         /* Section type */
1322         qemu_put_byte(f, QEMU_VM_SECTION_END);
1323         qemu_put_be32(f, se->section_id);
1324 
1325         ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1326         trace_savevm_section_end(se->idstr, se->section_id, ret);
1327         save_section_footer(f, se);
1328         if (ret < 0) {
1329             qemu_file_set_error(f, ret);
1330             return;
1331         }
1332     }
1333 
1334     qemu_put_byte(f, QEMU_VM_EOF);
1335     qemu_fflush(f);
1336 }
1337 
1338 static
1339 int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
1340 {
1341     SaveStateEntry *se;
1342     int ret;
1343 
1344     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1345         if (!se->ops ||
1346             (in_postcopy && se->ops->has_postcopy &&
1347              se->ops->has_postcopy(se->opaque)) ||
1348             !se->ops->save_live_complete_precopy) {
1349             continue;
1350         }
1351 
1352         if (se->ops->is_active) {
1353             if (!se->ops->is_active(se->opaque)) {
1354                 continue;
1355             }
1356         }
1357         trace_savevm_section_start(se->idstr, se->section_id);
1358 
1359         save_section_header(f, se, QEMU_VM_SECTION_END);
1360 
1361         ret = se->ops->save_live_complete_precopy(f, se->opaque);
1362         trace_savevm_section_end(se->idstr, se->section_id, ret);
1363         save_section_footer(f, se);
1364         if (ret < 0) {
1365             qemu_file_set_error(f, ret);
1366             return -1;
1367         }
1368     }
1369 
1370     return 0;
1371 }
1372 
1373 static
1374 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1375                                                     bool in_postcopy,
1376                                                     bool inactivate_disks)
1377 {
1378     g_autoptr(QJSON) vmdesc = NULL;
1379     int vmdesc_len;
1380     SaveStateEntry *se;
1381     int ret;
1382 
1383     vmdesc = qjson_new();
1384     json_prop_int(vmdesc, "page_size", qemu_target_page_size());
1385     json_start_array(vmdesc, "devices");
1386     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1387 
1388         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1389             continue;
1390         }
1391         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1392             trace_savevm_section_skip(se->idstr, se->section_id);
1393             continue;
1394         }
1395 
1396         trace_savevm_section_start(se->idstr, se->section_id);
1397 
1398         json_start_object(vmdesc, NULL);
1399         json_prop_str(vmdesc, "name", se->idstr);
1400         json_prop_int(vmdesc, "instance_id", se->instance_id);
1401 
1402         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1403         ret = vmstate_save(f, se, vmdesc);
1404         if (ret) {
1405             qemu_file_set_error(f, ret);
1406             return ret;
1407         }
1408         trace_savevm_section_end(se->idstr, se->section_id, 0);
1409         save_section_footer(f, se);
1410 
1411         json_end_object(vmdesc);
1412     }
1413 
1414     if (inactivate_disks) {
1415         /* Inactivate before sending QEMU_VM_EOF so that the
1416          * bdrv_invalidate_cache_all() on the other end won't fail. */
1417         ret = bdrv_inactivate_all();
1418         if (ret) {
1419             error_report("%s: bdrv_inactivate_all() failed (%d)",
1420                          __func__, ret);
1421             qemu_file_set_error(f, ret);
1422             return ret;
1423         }
1424     }
1425     if (!in_postcopy) {
1426         /* Postcopy stream will still be going */
1427         qemu_put_byte(f, QEMU_VM_EOF);
1428     }
1429 
1430     json_end_array(vmdesc);
1431     qjson_finish(vmdesc);
1432     vmdesc_len = strlen(qjson_get_str(vmdesc));
1433 
1434     if (should_send_vmdesc()) {
1435         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1436         qemu_put_be32(f, vmdesc_len);
1437         qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1438     }
1439 
1440     return 0;
1441 }
1442 
1443 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1444                                        bool inactivate_disks)
1445 {
1446     int ret;
1447     Error *local_err = NULL;
1448     bool in_postcopy = migration_in_postcopy();
1449 
1450     if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1451         error_report_err(local_err);
1452     }
1453 
1454     trace_savevm_state_complete_precopy();
1455 
1456     cpu_synchronize_all_states();
1457 
1458     if (!in_postcopy || iterable_only) {
1459         ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1460         if (ret) {
1461             return ret;
1462         }
1463     }
1464 
1465     if (iterable_only) {
1466         goto flush;
1467     }
1468 
1469     ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1470                                                           inactivate_disks);
1471     if (ret) {
1472         return ret;
1473     }
1474 
1475 flush:
1476     qemu_fflush(f);
1477     return 0;
1478 }
1479 
1480 /* Give an estimate of the amount left to be transferred,
1481  * the result is split into the amount for units that can and
1482  * for units that can't do postcopy.
1483  */
1484 void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
1485                                uint64_t *res_precopy_only,
1486                                uint64_t *res_compatible,
1487                                uint64_t *res_postcopy_only)
1488 {
1489     SaveStateEntry *se;
1490 
1491     *res_precopy_only = 0;
1492     *res_compatible = 0;
1493     *res_postcopy_only = 0;
1494 
1495 
1496     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1497         if (!se->ops || !se->ops->save_live_pending) {
1498             continue;
1499         }
1500         if (se->ops->is_active) {
1501             if (!se->ops->is_active(se->opaque)) {
1502                 continue;
1503             }
1504         }
1505         se->ops->save_live_pending(f, se->opaque, threshold_size,
1506                                    res_precopy_only, res_compatible,
1507                                    res_postcopy_only);
1508     }
1509 }
1510 
1511 void qemu_savevm_state_cleanup(void)
1512 {
1513     SaveStateEntry *se;
1514     Error *local_err = NULL;
1515 
1516     if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1517         error_report_err(local_err);
1518     }
1519 
1520     trace_savevm_state_cleanup();
1521     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1522         if (se->ops && se->ops->save_cleanup) {
1523             se->ops->save_cleanup(se->opaque);
1524         }
1525     }
1526 }
1527 
1528 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1529 {
1530     int ret;
1531     MigrationState *ms = migrate_get_current();
1532     MigrationStatus status;
1533 
1534     if (migration_is_setup_or_active(ms->state) ||
1535         ms->state == MIGRATION_STATUS_CANCELLING ||
1536         ms->state == MIGRATION_STATUS_COLO) {
1537         error_setg(errp, QERR_MIGRATION_ACTIVE);
1538         return -EINVAL;
1539     }
1540 
1541     if (migrate_use_block()) {
1542         error_setg(errp, "Block migration and snapshots are incompatible");
1543         return -EINVAL;
1544     }
1545 
1546     migrate_init(ms);
1547     memset(&ram_counters, 0, sizeof(ram_counters));
1548     ms->to_dst_file = f;
1549 
1550     qemu_mutex_unlock_iothread();
1551     qemu_savevm_state_header(f);
1552     qemu_savevm_state_setup(f);
1553     qemu_mutex_lock_iothread();
1554 
1555     while (qemu_file_get_error(f) == 0) {
1556         if (qemu_savevm_state_iterate(f, false) > 0) {
1557             break;
1558         }
1559     }
1560 
1561     ret = qemu_file_get_error(f);
1562     if (ret == 0) {
1563         qemu_savevm_state_complete_precopy(f, false, false);
1564         ret = qemu_file_get_error(f);
1565     }
1566     qemu_savevm_state_cleanup();
1567     if (ret != 0) {
1568         error_setg_errno(errp, -ret, "Error while writing VM state");
1569     }
1570 
1571     if (ret != 0) {
1572         status = MIGRATION_STATUS_FAILED;
1573     } else {
1574         status = MIGRATION_STATUS_COMPLETED;
1575     }
1576     migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1577 
1578     /* f is outer parameter, it should not stay in global migration state after
1579      * this function finished */
1580     ms->to_dst_file = NULL;
1581 
1582     return ret;
1583 }
1584 
1585 void qemu_savevm_live_state(QEMUFile *f)
1586 {
1587     /* save QEMU_VM_SECTION_END section */
1588     qemu_savevm_state_complete_precopy(f, true, false);
1589     qemu_put_byte(f, QEMU_VM_EOF);
1590 }
1591 
1592 int qemu_save_device_state(QEMUFile *f)
1593 {
1594     SaveStateEntry *se;
1595 
1596     if (!migration_in_colo_state()) {
1597         qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1598         qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1599     }
1600     cpu_synchronize_all_states();
1601 
1602     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1603         int ret;
1604 
1605         if (se->is_ram) {
1606             continue;
1607         }
1608         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1609             continue;
1610         }
1611         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1612             continue;
1613         }
1614 
1615         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1616 
1617         ret = vmstate_save(f, se, NULL);
1618         if (ret) {
1619             return ret;
1620         }
1621 
1622         save_section_footer(f, se);
1623     }
1624 
1625     qemu_put_byte(f, QEMU_VM_EOF);
1626 
1627     return qemu_file_get_error(f);
1628 }
1629 
1630 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id)
1631 {
1632     SaveStateEntry *se;
1633 
1634     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1635         if (!strcmp(se->idstr, idstr) &&
1636             (instance_id == se->instance_id ||
1637              instance_id == se->alias_id))
1638             return se;
1639         /* Migrating from an older version? */
1640         if (strstr(se->idstr, idstr) && se->compat) {
1641             if (!strcmp(se->compat->idstr, idstr) &&
1642                 (instance_id == se->compat->instance_id ||
1643                  instance_id == se->alias_id))
1644                 return se;
1645         }
1646     }
1647     return NULL;
1648 }
1649 
1650 enum LoadVMExitCodes {
1651     /* Allow a command to quit all layers of nested loadvm loops */
1652     LOADVM_QUIT     =  1,
1653 };
1654 
1655 /* ------ incoming postcopy messages ------ */
1656 /* 'advise' arrives before any transfers just to tell us that a postcopy
1657  * *might* happen - it might be skipped if precopy transferred everything
1658  * quickly.
1659  */
1660 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1661                                          uint16_t len)
1662 {
1663     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1664     uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1665     Error *local_err = NULL;
1666 
1667     trace_loadvm_postcopy_handle_advise();
1668     if (ps != POSTCOPY_INCOMING_NONE) {
1669         error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1670         return -1;
1671     }
1672 
1673     switch (len) {
1674     case 0:
1675         if (migrate_postcopy_ram()) {
1676             error_report("RAM postcopy is enabled but have 0 byte advise");
1677             return -EINVAL;
1678         }
1679         return 0;
1680     case 8 + 8:
1681         if (!migrate_postcopy_ram()) {
1682             error_report("RAM postcopy is disabled but have 16 byte advise");
1683             return -EINVAL;
1684         }
1685         break;
1686     default:
1687         error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1688         return -EINVAL;
1689     }
1690 
1691     if (!postcopy_ram_supported_by_host(mis)) {
1692         postcopy_state_set(POSTCOPY_INCOMING_NONE);
1693         return -1;
1694     }
1695 
1696     remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1697     local_pagesize_summary = ram_pagesize_summary();
1698 
1699     if (remote_pagesize_summary != local_pagesize_summary)  {
1700         /*
1701          * This detects two potential causes of mismatch:
1702          *   a) A mismatch in host page sizes
1703          *      Some combinations of mismatch are probably possible but it gets
1704          *      a bit more complicated.  In particular we need to place whole
1705          *      host pages on the dest at once, and we need to ensure that we
1706          *      handle dirtying to make sure we never end up sending part of
1707          *      a hostpage on it's own.
1708          *   b) The use of different huge page sizes on source/destination
1709          *      a more fine grain test is performed during RAM block migration
1710          *      but this test here causes a nice early clear failure, and
1711          *      also fails when passed to an older qemu that doesn't
1712          *      do huge pages.
1713          */
1714         error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1715                                                              " d=%" PRIx64 ")",
1716                      remote_pagesize_summary, local_pagesize_summary);
1717         return -1;
1718     }
1719 
1720     remote_tps = qemu_get_be64(mis->from_src_file);
1721     if (remote_tps != qemu_target_page_size()) {
1722         /*
1723          * Again, some differences could be dealt with, but for now keep it
1724          * simple.
1725          */
1726         error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1727                      (int)remote_tps, qemu_target_page_size());
1728         return -1;
1729     }
1730 
1731     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1732         error_report_err(local_err);
1733         return -1;
1734     }
1735 
1736     if (ram_postcopy_incoming_init(mis)) {
1737         return -1;
1738     }
1739 
1740     return 0;
1741 }
1742 
1743 /* After postcopy we will be told to throw some pages away since they're
1744  * dirty and will have to be demand fetched.  Must happen before CPU is
1745  * started.
1746  * There can be 0..many of these messages, each encoding multiple pages.
1747  */
1748 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1749                                               uint16_t len)
1750 {
1751     int tmp;
1752     char ramid[256];
1753     PostcopyState ps = postcopy_state_get();
1754 
1755     trace_loadvm_postcopy_ram_handle_discard();
1756 
1757     switch (ps) {
1758     case POSTCOPY_INCOMING_ADVISE:
1759         /* 1st discard */
1760         tmp = postcopy_ram_prepare_discard(mis);
1761         if (tmp) {
1762             return tmp;
1763         }
1764         break;
1765 
1766     case POSTCOPY_INCOMING_DISCARD:
1767         /* Expected state */
1768         break;
1769 
1770     default:
1771         error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1772                      ps);
1773         return -1;
1774     }
1775     /* We're expecting a
1776      *    Version (0)
1777      *    a RAM ID string (length byte, name, 0 term)
1778      *    then at least 1 16 byte chunk
1779     */
1780     if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1781         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1782         return -1;
1783     }
1784 
1785     tmp = qemu_get_byte(mis->from_src_file);
1786     if (tmp != postcopy_ram_discard_version) {
1787         error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1788         return -1;
1789     }
1790 
1791     if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1792         error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1793         return -1;
1794     }
1795     tmp = qemu_get_byte(mis->from_src_file);
1796     if (tmp != 0) {
1797         error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1798         return -1;
1799     }
1800 
1801     len -= 3 + strlen(ramid);
1802     if (len % 16) {
1803         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1804         return -1;
1805     }
1806     trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1807     while (len) {
1808         uint64_t start_addr, block_length;
1809         start_addr = qemu_get_be64(mis->from_src_file);
1810         block_length = qemu_get_be64(mis->from_src_file);
1811 
1812         len -= 16;
1813         int ret = ram_discard_range(ramid, start_addr, block_length);
1814         if (ret) {
1815             return ret;
1816         }
1817     }
1818     trace_loadvm_postcopy_ram_handle_discard_end();
1819 
1820     return 0;
1821 }
1822 
1823 /*
1824  * Triggered by a postcopy_listen command; this thread takes over reading
1825  * the input stream, leaving the main thread free to carry on loading the rest
1826  * of the device state (from RAM).
1827  * (TODO:This could do with being in a postcopy file - but there again it's
1828  * just another input loop, not that postcopy specific)
1829  */
1830 static void *postcopy_ram_listen_thread(void *opaque)
1831 {
1832     MigrationIncomingState *mis = migration_incoming_get_current();
1833     QEMUFile *f = mis->from_src_file;
1834     int load_res;
1835 
1836     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1837                                    MIGRATION_STATUS_POSTCOPY_ACTIVE);
1838     qemu_sem_post(&mis->listen_thread_sem);
1839     trace_postcopy_ram_listen_thread_start();
1840 
1841     rcu_register_thread();
1842     /*
1843      * Because we're a thread and not a coroutine we can't yield
1844      * in qemu_file, and thus we must be blocking now.
1845      */
1846     qemu_file_set_blocking(f, true);
1847     load_res = qemu_loadvm_state_main(f, mis);
1848 
1849     /*
1850      * This is tricky, but, mis->from_src_file can change after it
1851      * returns, when postcopy recovery happened. In the future, we may
1852      * want a wrapper for the QEMUFile handle.
1853      */
1854     f = mis->from_src_file;
1855 
1856     /* And non-blocking again so we don't block in any cleanup */
1857     qemu_file_set_blocking(f, false);
1858 
1859     trace_postcopy_ram_listen_thread_exit();
1860     if (load_res < 0) {
1861         error_report("%s: loadvm failed: %d", __func__, load_res);
1862         qemu_file_set_error(f, load_res);
1863         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1864                                        MIGRATION_STATUS_FAILED);
1865     } else {
1866         /*
1867          * This looks good, but it's possible that the device loading in the
1868          * main thread hasn't finished yet, and so we might not be in 'RUN'
1869          * state yet; wait for the end of the main thread.
1870          */
1871         qemu_event_wait(&mis->main_thread_load_event);
1872     }
1873     postcopy_ram_incoming_cleanup(mis);
1874 
1875     if (load_res < 0) {
1876         /*
1877          * If something went wrong then we have a bad state so exit;
1878          * depending how far we got it might be possible at this point
1879          * to leave the guest running and fire MCEs for pages that never
1880          * arrived as a desperate recovery step.
1881          */
1882         rcu_unregister_thread();
1883         exit(EXIT_FAILURE);
1884     }
1885 
1886     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1887                                    MIGRATION_STATUS_COMPLETED);
1888     /*
1889      * If everything has worked fine, then the main thread has waited
1890      * for us to start, and we're the last use of the mis.
1891      * (If something broke then qemu will have to exit anyway since it's
1892      * got a bad migration state).
1893      */
1894     migration_incoming_state_destroy();
1895     qemu_loadvm_state_cleanup();
1896 
1897     rcu_unregister_thread();
1898     mis->have_listen_thread = false;
1899     postcopy_state_set(POSTCOPY_INCOMING_END);
1900 
1901     return NULL;
1902 }
1903 
1904 /* After this message we must be able to immediately receive postcopy data */
1905 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1906 {
1907     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1908     trace_loadvm_postcopy_handle_listen();
1909     Error *local_err = NULL;
1910 
1911     if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1912         error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1913         return -1;
1914     }
1915     if (ps == POSTCOPY_INCOMING_ADVISE) {
1916         /*
1917          * A rare case, we entered listen without having to do any discards,
1918          * so do the setup that's normally done at the time of the 1st discard.
1919          */
1920         if (migrate_postcopy_ram()) {
1921             postcopy_ram_prepare_discard(mis);
1922         }
1923     }
1924 
1925     /*
1926      * Sensitise RAM - can now generate requests for blocks that don't exist
1927      * However, at this point the CPU shouldn't be running, and the IO
1928      * shouldn't be doing anything yet so don't actually expect requests
1929      */
1930     if (migrate_postcopy_ram()) {
1931         if (postcopy_ram_incoming_setup(mis)) {
1932             postcopy_ram_incoming_cleanup(mis);
1933             return -1;
1934         }
1935     }
1936 
1937     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
1938         error_report_err(local_err);
1939         return -1;
1940     }
1941 
1942     mis->have_listen_thread = true;
1943     /* Start up the listening thread and wait for it to signal ready */
1944     qemu_sem_init(&mis->listen_thread_sem, 0);
1945     qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1946                        postcopy_ram_listen_thread, NULL,
1947                        QEMU_THREAD_DETACHED);
1948     qemu_sem_wait(&mis->listen_thread_sem);
1949     qemu_sem_destroy(&mis->listen_thread_sem);
1950 
1951     return 0;
1952 }
1953 
1954 static void loadvm_postcopy_handle_run_bh(void *opaque)
1955 {
1956     Error *local_err = NULL;
1957     MigrationIncomingState *mis = opaque;
1958 
1959     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1960      * in migration.c
1961      */
1962     cpu_synchronize_all_post_init();
1963 
1964     qemu_announce_self(&mis->announce_timer, migrate_announce_params());
1965 
1966     /* Make sure all file formats flush their mutable metadata.
1967      * If we get an error here, just don't restart the VM yet. */
1968     bdrv_invalidate_cache_all(&local_err);
1969     if (local_err) {
1970         error_report_err(local_err);
1971         local_err = NULL;
1972         autostart = false;
1973     }
1974 
1975     trace_loadvm_postcopy_handle_run_cpu_sync();
1976 
1977     trace_loadvm_postcopy_handle_run_vmstart();
1978 
1979     dirty_bitmap_mig_before_vm_start();
1980 
1981     if (autostart) {
1982         /* Hold onto your hats, starting the CPU */
1983         vm_start();
1984     } else {
1985         /* leave it paused and let management decide when to start the CPU */
1986         runstate_set(RUN_STATE_PAUSED);
1987     }
1988 
1989     qemu_bh_delete(mis->bh);
1990 }
1991 
1992 /* After all discards we can start running and asking for pages */
1993 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1994 {
1995     PostcopyState ps = postcopy_state_get();
1996 
1997     trace_loadvm_postcopy_handle_run();
1998     if (ps != POSTCOPY_INCOMING_LISTENING) {
1999         error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
2000         return -1;
2001     }
2002 
2003     postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
2004     mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis);
2005     qemu_bh_schedule(mis->bh);
2006 
2007     /* We need to finish reading the stream from the package
2008      * and also stop reading anything more from the stream that loaded the
2009      * package (since it's now being read by the listener thread).
2010      * LOADVM_QUIT will quit all the layers of nested loadvm loops.
2011      */
2012     return LOADVM_QUIT;
2013 }
2014 
2015 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
2016 {
2017     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
2018         error_report("%s: illegal resume received", __func__);
2019         /* Don't fail the load, only for this. */
2020         return 0;
2021     }
2022 
2023     /*
2024      * This means source VM is ready to resume the postcopy migration.
2025      * It's time to switch state and release the fault thread to
2026      * continue service page faults.
2027      */
2028     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2029                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
2030     qemu_sem_post(&mis->postcopy_pause_sem_fault);
2031 
2032     trace_loadvm_postcopy_handle_resume();
2033 
2034     /* Tell source that "we are ready" */
2035     migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
2036 
2037     return 0;
2038 }
2039 
2040 /**
2041  * Immediately following this command is a blob of data containing an embedded
2042  * chunk of migration stream; read it and load it.
2043  *
2044  * @mis: Incoming state
2045  * @length: Length of packaged data to read
2046  *
2047  * Returns: Negative values on error
2048  *
2049  */
2050 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
2051 {
2052     int ret;
2053     size_t length;
2054     QIOChannelBuffer *bioc;
2055 
2056     length = qemu_get_be32(mis->from_src_file);
2057     trace_loadvm_handle_cmd_packaged(length);
2058 
2059     if (length > MAX_VM_CMD_PACKAGED_SIZE) {
2060         error_report("Unreasonably large packaged state: %zu", length);
2061         return -1;
2062     }
2063 
2064     bioc = qio_channel_buffer_new(length);
2065     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
2066     ret = qemu_get_buffer(mis->from_src_file,
2067                           bioc->data,
2068                           length);
2069     if (ret != length) {
2070         object_unref(OBJECT(bioc));
2071         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
2072                      ret, length);
2073         return (ret < 0) ? ret : -EAGAIN;
2074     }
2075     bioc->usage += length;
2076     trace_loadvm_handle_cmd_packaged_received(ret);
2077 
2078     QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
2079 
2080     ret = qemu_loadvm_state_main(packf, mis);
2081     trace_loadvm_handle_cmd_packaged_main(ret);
2082     qemu_fclose(packf);
2083     object_unref(OBJECT(bioc));
2084 
2085     return ret;
2086 }
2087 
2088 /*
2089  * Handle request that source requests for recved_bitmap on
2090  * destination. Payload format:
2091  *
2092  * len (1 byte) + ramblock_name (<255 bytes)
2093  */
2094 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2095                                      uint16_t len)
2096 {
2097     QEMUFile *file = mis->from_src_file;
2098     RAMBlock *rb;
2099     char block_name[256];
2100     size_t cnt;
2101 
2102     cnt = qemu_get_counted_string(file, block_name);
2103     if (!cnt) {
2104         error_report("%s: failed to read block name", __func__);
2105         return -EINVAL;
2106     }
2107 
2108     /* Validate before using the data */
2109     if (qemu_file_get_error(file)) {
2110         return qemu_file_get_error(file);
2111     }
2112 
2113     if (len != cnt + 1) {
2114         error_report("%s: invalid payload length (%d)", __func__, len);
2115         return -EINVAL;
2116     }
2117 
2118     rb = qemu_ram_block_by_name(block_name);
2119     if (!rb) {
2120         error_report("%s: block '%s' not found", __func__, block_name);
2121         return -EINVAL;
2122     }
2123 
2124     migrate_send_rp_recv_bitmap(mis, block_name);
2125 
2126     trace_loadvm_handle_recv_bitmap(block_name);
2127 
2128     return 0;
2129 }
2130 
2131 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2132 {
2133     migration_incoming_enable_colo();
2134     return colo_init_ram_cache();
2135 }
2136 
2137 /*
2138  * Process an incoming 'QEMU_VM_COMMAND'
2139  * 0           just a normal return
2140  * LOADVM_QUIT All good, but exit the loop
2141  * <0          Error
2142  */
2143 static int loadvm_process_command(QEMUFile *f)
2144 {
2145     MigrationIncomingState *mis = migration_incoming_get_current();
2146     uint16_t cmd;
2147     uint16_t len;
2148     uint32_t tmp32;
2149 
2150     cmd = qemu_get_be16(f);
2151     len = qemu_get_be16(f);
2152 
2153     /* Check validity before continue processing of cmds */
2154     if (qemu_file_get_error(f)) {
2155         return qemu_file_get_error(f);
2156     }
2157 
2158     trace_loadvm_process_command(cmd, len);
2159     if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2160         error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2161         return -EINVAL;
2162     }
2163 
2164     if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2165         error_report("%s received with bad length - expecting %zu, got %d",
2166                      mig_cmd_args[cmd].name,
2167                      (size_t)mig_cmd_args[cmd].len, len);
2168         return -ERANGE;
2169     }
2170 
2171     switch (cmd) {
2172     case MIG_CMD_OPEN_RETURN_PATH:
2173         if (mis->to_src_file) {
2174             error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2175             /* Not really a problem, so don't give up */
2176             return 0;
2177         }
2178         mis->to_src_file = qemu_file_get_return_path(f);
2179         if (!mis->to_src_file) {
2180             error_report("CMD_OPEN_RETURN_PATH failed");
2181             return -1;
2182         }
2183         break;
2184 
2185     case MIG_CMD_PING:
2186         tmp32 = qemu_get_be32(f);
2187         trace_loadvm_process_command_ping(tmp32);
2188         if (!mis->to_src_file) {
2189             error_report("CMD_PING (0x%x) received with no return path",
2190                          tmp32);
2191             return -1;
2192         }
2193         migrate_send_rp_pong(mis, tmp32);
2194         break;
2195 
2196     case MIG_CMD_PACKAGED:
2197         return loadvm_handle_cmd_packaged(mis);
2198 
2199     case MIG_CMD_POSTCOPY_ADVISE:
2200         return loadvm_postcopy_handle_advise(mis, len);
2201 
2202     case MIG_CMD_POSTCOPY_LISTEN:
2203         return loadvm_postcopy_handle_listen(mis);
2204 
2205     case MIG_CMD_POSTCOPY_RUN:
2206         return loadvm_postcopy_handle_run(mis);
2207 
2208     case MIG_CMD_POSTCOPY_RAM_DISCARD:
2209         return loadvm_postcopy_ram_handle_discard(mis, len);
2210 
2211     case MIG_CMD_POSTCOPY_RESUME:
2212         return loadvm_postcopy_handle_resume(mis);
2213 
2214     case MIG_CMD_RECV_BITMAP:
2215         return loadvm_handle_recv_bitmap(mis, len);
2216 
2217     case MIG_CMD_ENABLE_COLO:
2218         return loadvm_process_enable_colo(mis);
2219     }
2220 
2221     return 0;
2222 }
2223 
2224 /*
2225  * Read a footer off the wire and check that it matches the expected section
2226  *
2227  * Returns: true if the footer was good
2228  *          false if there is a problem (and calls error_report to say why)
2229  */
2230 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2231 {
2232     int ret;
2233     uint8_t read_mark;
2234     uint32_t read_section_id;
2235 
2236     if (!migrate_get_current()->send_section_footer) {
2237         /* No footer to check */
2238         return true;
2239     }
2240 
2241     read_mark = qemu_get_byte(f);
2242 
2243     ret = qemu_file_get_error(f);
2244     if (ret) {
2245         error_report("%s: Read section footer failed: %d",
2246                      __func__, ret);
2247         return false;
2248     }
2249 
2250     if (read_mark != QEMU_VM_SECTION_FOOTER) {
2251         error_report("Missing section footer for %s", se->idstr);
2252         return false;
2253     }
2254 
2255     read_section_id = qemu_get_be32(f);
2256     if (read_section_id != se->load_section_id) {
2257         error_report("Mismatched section id in footer for %s -"
2258                      " read 0x%x expected 0x%x",
2259                      se->idstr, read_section_id, se->load_section_id);
2260         return false;
2261     }
2262 
2263     /* All good */
2264     return true;
2265 }
2266 
2267 static int
2268 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2269 {
2270     uint32_t instance_id, version_id, section_id;
2271     SaveStateEntry *se;
2272     char idstr[256];
2273     int ret;
2274 
2275     /* Read section start */
2276     section_id = qemu_get_be32(f);
2277     if (!qemu_get_counted_string(f, idstr)) {
2278         error_report("Unable to read ID string for section %u",
2279                      section_id);
2280         return -EINVAL;
2281     }
2282     instance_id = qemu_get_be32(f);
2283     version_id = qemu_get_be32(f);
2284 
2285     ret = qemu_file_get_error(f);
2286     if (ret) {
2287         error_report("%s: Failed to read instance/version ID: %d",
2288                      __func__, ret);
2289         return ret;
2290     }
2291 
2292     trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2293             instance_id, version_id);
2294     /* Find savevm section */
2295     se = find_se(idstr, instance_id);
2296     if (se == NULL) {
2297         error_report("Unknown savevm section or instance '%s' %"PRIu32". "
2298                      "Make sure that your current VM setup matches your "
2299                      "saved VM setup, including any hotplugged devices",
2300                      idstr, instance_id);
2301         return -EINVAL;
2302     }
2303 
2304     /* Validate version */
2305     if (version_id > se->version_id) {
2306         error_report("savevm: unsupported version %d for '%s' v%d",
2307                      version_id, idstr, se->version_id);
2308         return -EINVAL;
2309     }
2310     se->load_version_id = version_id;
2311     se->load_section_id = section_id;
2312 
2313     /* Validate if it is a device's state */
2314     if (xen_enabled() && se->is_ram) {
2315         error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2316         return -EINVAL;
2317     }
2318 
2319     ret = vmstate_load(f, se);
2320     if (ret < 0) {
2321         error_report("error while loading state for instance 0x%"PRIx32" of"
2322                      " device '%s'", instance_id, idstr);
2323         return ret;
2324     }
2325     if (!check_section_footer(f, se)) {
2326         return -EINVAL;
2327     }
2328 
2329     return 0;
2330 }
2331 
2332 static int
2333 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2334 {
2335     uint32_t section_id;
2336     SaveStateEntry *se;
2337     int ret;
2338 
2339     section_id = qemu_get_be32(f);
2340 
2341     ret = qemu_file_get_error(f);
2342     if (ret) {
2343         error_report("%s: Failed to read section ID: %d",
2344                      __func__, ret);
2345         return ret;
2346     }
2347 
2348     trace_qemu_loadvm_state_section_partend(section_id);
2349     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2350         if (se->load_section_id == section_id) {
2351             break;
2352         }
2353     }
2354     if (se == NULL) {
2355         error_report("Unknown savevm section %d", section_id);
2356         return -EINVAL;
2357     }
2358 
2359     ret = vmstate_load(f, se);
2360     if (ret < 0) {
2361         error_report("error while loading state section id %d(%s)",
2362                      section_id, se->idstr);
2363         return ret;
2364     }
2365     if (!check_section_footer(f, se)) {
2366         return -EINVAL;
2367     }
2368 
2369     return 0;
2370 }
2371 
2372 static int qemu_loadvm_state_header(QEMUFile *f)
2373 {
2374     unsigned int v;
2375     int ret;
2376 
2377     v = qemu_get_be32(f);
2378     if (v != QEMU_VM_FILE_MAGIC) {
2379         error_report("Not a migration stream");
2380         return -EINVAL;
2381     }
2382 
2383     v = qemu_get_be32(f);
2384     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2385         error_report("SaveVM v2 format is obsolete and don't work anymore");
2386         return -ENOTSUP;
2387     }
2388     if (v != QEMU_VM_FILE_VERSION) {
2389         error_report("Unsupported migration stream version");
2390         return -ENOTSUP;
2391     }
2392 
2393     if (migrate_get_current()->send_configuration) {
2394         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2395             error_report("Configuration section missing");
2396             qemu_loadvm_state_cleanup();
2397             return -EINVAL;
2398         }
2399         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2400 
2401         if (ret) {
2402             qemu_loadvm_state_cleanup();
2403             return ret;
2404         }
2405     }
2406     return 0;
2407 }
2408 
2409 static int qemu_loadvm_state_setup(QEMUFile *f)
2410 {
2411     SaveStateEntry *se;
2412     int ret;
2413 
2414     trace_loadvm_state_setup();
2415     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2416         if (!se->ops || !se->ops->load_setup) {
2417             continue;
2418         }
2419         if (se->ops->is_active) {
2420             if (!se->ops->is_active(se->opaque)) {
2421                 continue;
2422             }
2423         }
2424 
2425         ret = se->ops->load_setup(f, se->opaque);
2426         if (ret < 0) {
2427             qemu_file_set_error(f, ret);
2428             error_report("Load state of device %s failed", se->idstr);
2429             return ret;
2430         }
2431     }
2432     return 0;
2433 }
2434 
2435 void qemu_loadvm_state_cleanup(void)
2436 {
2437     SaveStateEntry *se;
2438 
2439     trace_loadvm_state_cleanup();
2440     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2441         if (se->ops && se->ops->load_cleanup) {
2442             se->ops->load_cleanup(se->opaque);
2443         }
2444     }
2445 }
2446 
2447 /* Return true if we should continue the migration, or false. */
2448 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2449 {
2450     trace_postcopy_pause_incoming();
2451 
2452     /* Clear the triggered bit to allow one recovery */
2453     mis->postcopy_recover_triggered = false;
2454 
2455     assert(mis->from_src_file);
2456     qemu_file_shutdown(mis->from_src_file);
2457     qemu_fclose(mis->from_src_file);
2458     mis->from_src_file = NULL;
2459 
2460     assert(mis->to_src_file);
2461     qemu_file_shutdown(mis->to_src_file);
2462     qemu_mutex_lock(&mis->rp_mutex);
2463     qemu_fclose(mis->to_src_file);
2464     mis->to_src_file = NULL;
2465     qemu_mutex_unlock(&mis->rp_mutex);
2466 
2467     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2468                       MIGRATION_STATUS_POSTCOPY_PAUSED);
2469 
2470     /* Notify the fault thread for the invalidated file handle */
2471     postcopy_fault_thread_notify(mis);
2472 
2473     error_report("Detected IO failure for postcopy. "
2474                  "Migration paused.");
2475 
2476     while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2477         qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2478     }
2479 
2480     trace_postcopy_pause_incoming_continued();
2481 
2482     return true;
2483 }
2484 
2485 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2486 {
2487     uint8_t section_type;
2488     int ret = 0;
2489 
2490 retry:
2491     while (true) {
2492         section_type = qemu_get_byte(f);
2493 
2494         if (qemu_file_get_error(f)) {
2495             ret = qemu_file_get_error(f);
2496             break;
2497         }
2498 
2499         trace_qemu_loadvm_state_section(section_type);
2500         switch (section_type) {
2501         case QEMU_VM_SECTION_START:
2502         case QEMU_VM_SECTION_FULL:
2503             ret = qemu_loadvm_section_start_full(f, mis);
2504             if (ret < 0) {
2505                 goto out;
2506             }
2507             break;
2508         case QEMU_VM_SECTION_PART:
2509         case QEMU_VM_SECTION_END:
2510             ret = qemu_loadvm_section_part_end(f, mis);
2511             if (ret < 0) {
2512                 goto out;
2513             }
2514             break;
2515         case QEMU_VM_COMMAND:
2516             ret = loadvm_process_command(f);
2517             trace_qemu_loadvm_state_section_command(ret);
2518             if ((ret < 0) || (ret == LOADVM_QUIT)) {
2519                 goto out;
2520             }
2521             break;
2522         case QEMU_VM_EOF:
2523             /* This is the end of migration */
2524             goto out;
2525         default:
2526             error_report("Unknown savevm section type %d", section_type);
2527             ret = -EINVAL;
2528             goto out;
2529         }
2530     }
2531 
2532 out:
2533     if (ret < 0) {
2534         qemu_file_set_error(f, ret);
2535 
2536         /*
2537          * If we are during an active postcopy, then we pause instead
2538          * of bail out to at least keep the VM's dirty data.  Note
2539          * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2540          * during which we're still receiving device states and we
2541          * still haven't yet started the VM on destination.
2542          */
2543         if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2544             postcopy_pause_incoming(mis)) {
2545             /* Reset f to point to the newly created channel */
2546             f = mis->from_src_file;
2547             goto retry;
2548         }
2549     }
2550     return ret;
2551 }
2552 
2553 int qemu_loadvm_state(QEMUFile *f)
2554 {
2555     MigrationIncomingState *mis = migration_incoming_get_current();
2556     Error *local_err = NULL;
2557     int ret;
2558 
2559     if (qemu_savevm_state_blocked(&local_err)) {
2560         error_report_err(local_err);
2561         return -EINVAL;
2562     }
2563 
2564     ret = qemu_loadvm_state_header(f);
2565     if (ret) {
2566         return ret;
2567     }
2568 
2569     if (qemu_loadvm_state_setup(f) != 0) {
2570         return -EINVAL;
2571     }
2572 
2573     cpu_synchronize_all_pre_loadvm();
2574 
2575     ret = qemu_loadvm_state_main(f, mis);
2576     qemu_event_set(&mis->main_thread_load_event);
2577 
2578     trace_qemu_loadvm_state_post_main(ret);
2579 
2580     if (mis->have_listen_thread) {
2581         /* Listen thread still going, can't clean up yet */
2582         return ret;
2583     }
2584 
2585     if (ret == 0) {
2586         ret = qemu_file_get_error(f);
2587     }
2588 
2589     /*
2590      * Try to read in the VMDESC section as well, so that dumping tools that
2591      * intercept our migration stream have the chance to see it.
2592      */
2593 
2594     /* We've got to be careful; if we don't read the data and just shut the fd
2595      * then the sender can error if we close while it's still sending.
2596      * We also mustn't read data that isn't there; some transports (RDMA)
2597      * will stall waiting for that data when the source has already closed.
2598      */
2599     if (ret == 0 && should_send_vmdesc()) {
2600         uint8_t *buf;
2601         uint32_t size;
2602         uint8_t  section_type = qemu_get_byte(f);
2603 
2604         if (section_type != QEMU_VM_VMDESCRIPTION) {
2605             error_report("Expected vmdescription section, but got %d",
2606                          section_type);
2607             /*
2608              * It doesn't seem worth failing at this point since
2609              * we apparently have an otherwise valid VM state
2610              */
2611         } else {
2612             buf = g_malloc(0x1000);
2613             size = qemu_get_be32(f);
2614 
2615             while (size > 0) {
2616                 uint32_t read_chunk = MIN(size, 0x1000);
2617                 qemu_get_buffer(f, buf, read_chunk);
2618                 size -= read_chunk;
2619             }
2620             g_free(buf);
2621         }
2622     }
2623 
2624     qemu_loadvm_state_cleanup();
2625     cpu_synchronize_all_post_init();
2626 
2627     return ret;
2628 }
2629 
2630 int qemu_load_device_state(QEMUFile *f)
2631 {
2632     MigrationIncomingState *mis = migration_incoming_get_current();
2633     int ret;
2634 
2635     /* Load QEMU_VM_SECTION_FULL section */
2636     ret = qemu_loadvm_state_main(f, mis);
2637     if (ret < 0) {
2638         error_report("Failed to load device state: %d", ret);
2639         return ret;
2640     }
2641 
2642     cpu_synchronize_all_post_init();
2643     return 0;
2644 }
2645 
2646 int save_snapshot(const char *name, Error **errp)
2647 {
2648     BlockDriverState *bs, *bs1;
2649     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2650     int ret = -1;
2651     QEMUFile *f;
2652     int saved_vm_running;
2653     uint64_t vm_state_size;
2654     qemu_timeval tv;
2655     struct tm tm;
2656     AioContext *aio_context;
2657 
2658     if (migration_is_blocked(errp)) {
2659         return ret;
2660     }
2661 
2662     if (!replay_can_snapshot()) {
2663         error_setg(errp, "Record/replay does not allow making snapshot "
2664                    "right now. Try once more later.");
2665         return ret;
2666     }
2667 
2668     if (!bdrv_all_can_snapshot(&bs)) {
2669         error_setg(errp, "Device '%s' is writable but does not support "
2670                    "snapshots", bdrv_get_device_name(bs));
2671         return ret;
2672     }
2673 
2674     /* Delete old snapshots of the same name */
2675     if (name) {
2676         ret = bdrv_all_delete_snapshot(name, &bs1, errp);
2677         if (ret < 0) {
2678             error_prepend(errp, "Error while deleting snapshot on device "
2679                           "'%s': ", bdrv_get_device_name(bs1));
2680             return ret;
2681         }
2682     }
2683 
2684     bs = bdrv_all_find_vmstate_bs();
2685     if (bs == NULL) {
2686         error_setg(errp, "No block device can accept snapshots");
2687         return ret;
2688     }
2689     aio_context = bdrv_get_aio_context(bs);
2690 
2691     saved_vm_running = runstate_is_running();
2692 
2693     ret = global_state_store();
2694     if (ret) {
2695         error_setg(errp, "Error saving global state");
2696         return ret;
2697     }
2698     vm_stop(RUN_STATE_SAVE_VM);
2699 
2700     bdrv_drain_all_begin();
2701 
2702     aio_context_acquire(aio_context);
2703 
2704     memset(sn, 0, sizeof(*sn));
2705 
2706     /* fill auxiliary fields */
2707     qemu_gettimeofday(&tv);
2708     sn->date_sec = tv.tv_sec;
2709     sn->date_nsec = tv.tv_usec * 1000;
2710     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2711 
2712     if (name) {
2713         ret = bdrv_snapshot_find(bs, old_sn, name);
2714         if (ret >= 0) {
2715             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2716             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2717         } else {
2718             pstrcpy(sn->name, sizeof(sn->name), name);
2719         }
2720     } else {
2721         /* cast below needed for OpenBSD where tv_sec is still 'long' */
2722         localtime_r((const time_t *)&tv.tv_sec, &tm);
2723         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2724     }
2725 
2726     /* save the VM state */
2727     f = qemu_fopen_bdrv(bs, 1);
2728     if (!f) {
2729         error_setg(errp, "Could not open VM state file");
2730         goto the_end;
2731     }
2732     ret = qemu_savevm_state(f, errp);
2733     vm_state_size = qemu_ftell(f);
2734     qemu_fclose(f);
2735     if (ret < 0) {
2736         goto the_end;
2737     }
2738 
2739     /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2740      * for itself.  BDRV_POLL_WHILE() does not support nested locking because
2741      * it only releases the lock once.  Therefore synchronous I/O will deadlock
2742      * unless we release the AioContext before bdrv_all_create_snapshot().
2743      */
2744     aio_context_release(aio_context);
2745     aio_context = NULL;
2746 
2747     ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2748     if (ret < 0) {
2749         error_setg(errp, "Error while creating snapshot on '%s'",
2750                    bdrv_get_device_name(bs));
2751         goto the_end;
2752     }
2753 
2754     ret = 0;
2755 
2756  the_end:
2757     if (aio_context) {
2758         aio_context_release(aio_context);
2759     }
2760 
2761     bdrv_drain_all_end();
2762 
2763     if (saved_vm_running) {
2764         vm_start();
2765     }
2766     return ret;
2767 }
2768 
2769 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
2770                                 Error **errp)
2771 {
2772     QEMUFile *f;
2773     QIOChannelFile *ioc;
2774     int saved_vm_running;
2775     int ret;
2776 
2777     if (!has_live) {
2778         /* live default to true so old version of Xen tool stack can have a
2779          * successfull live migration */
2780         live = true;
2781     }
2782 
2783     saved_vm_running = runstate_is_running();
2784     vm_stop(RUN_STATE_SAVE_VM);
2785     global_state_store_running();
2786 
2787     ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2788     if (!ioc) {
2789         goto the_end;
2790     }
2791     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2792     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2793     object_unref(OBJECT(ioc));
2794     ret = qemu_save_device_state(f);
2795     if (ret < 0 || qemu_fclose(f) < 0) {
2796         error_setg(errp, QERR_IO_ERROR);
2797     } else {
2798         /* libxl calls the QMP command "stop" before calling
2799          * "xen-save-devices-state" and in case of migration failure, libxl
2800          * would call "cont".
2801          * So call bdrv_inactivate_all (release locks) here to let the other
2802          * side of the migration take controle of the images.
2803          */
2804         if (live && !saved_vm_running) {
2805             ret = bdrv_inactivate_all();
2806             if (ret) {
2807                 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
2808                            __func__, ret);
2809             }
2810         }
2811     }
2812 
2813  the_end:
2814     if (saved_vm_running) {
2815         vm_start();
2816     }
2817 }
2818 
2819 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2820 {
2821     QEMUFile *f;
2822     QIOChannelFile *ioc;
2823     int ret;
2824 
2825     /* Guest must be paused before loading the device state; the RAM state
2826      * will already have been loaded by xc
2827      */
2828     if (runstate_is_running()) {
2829         error_setg(errp, "Cannot update device state while vm is running");
2830         return;
2831     }
2832     vm_stop(RUN_STATE_RESTORE_VM);
2833 
2834     ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2835     if (!ioc) {
2836         return;
2837     }
2838     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2839     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2840     object_unref(OBJECT(ioc));
2841 
2842     ret = qemu_loadvm_state(f);
2843     qemu_fclose(f);
2844     if (ret < 0) {
2845         error_setg(errp, QERR_IO_ERROR);
2846     }
2847     migration_incoming_state_destroy();
2848 }
2849 
2850 int load_snapshot(const char *name, Error **errp)
2851 {
2852     BlockDriverState *bs, *bs_vm_state;
2853     QEMUSnapshotInfo sn;
2854     QEMUFile *f;
2855     int ret;
2856     AioContext *aio_context;
2857     MigrationIncomingState *mis = migration_incoming_get_current();
2858 
2859     if (!replay_can_snapshot()) {
2860         error_setg(errp, "Record/replay does not allow loading snapshot "
2861                    "right now. Try once more later.");
2862         return -EINVAL;
2863     }
2864 
2865     if (!bdrv_all_can_snapshot(&bs)) {
2866         error_setg(errp,
2867                    "Device '%s' is writable but does not support snapshots",
2868                    bdrv_get_device_name(bs));
2869         return -ENOTSUP;
2870     }
2871     ret = bdrv_all_find_snapshot(name, &bs);
2872     if (ret < 0) {
2873         error_setg(errp,
2874                    "Device '%s' does not have the requested snapshot '%s'",
2875                    bdrv_get_device_name(bs), name);
2876         return ret;
2877     }
2878 
2879     bs_vm_state = bdrv_all_find_vmstate_bs();
2880     if (!bs_vm_state) {
2881         error_setg(errp, "No block device supports snapshots");
2882         return -ENOTSUP;
2883     }
2884     aio_context = bdrv_get_aio_context(bs_vm_state);
2885 
2886     /* Don't even try to load empty VM states */
2887     aio_context_acquire(aio_context);
2888     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2889     aio_context_release(aio_context);
2890     if (ret < 0) {
2891         return ret;
2892     } else if (sn.vm_state_size == 0) {
2893         error_setg(errp, "This is a disk-only snapshot. Revert to it "
2894                    " offline using qemu-img");
2895         return -EINVAL;
2896     }
2897 
2898     /* Flush all IO requests so they don't interfere with the new state.  */
2899     bdrv_drain_all_begin();
2900 
2901     ret = bdrv_all_goto_snapshot(name, &bs, errp);
2902     if (ret < 0) {
2903         error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
2904                       name, bdrv_get_device_name(bs));
2905         goto err_drain;
2906     }
2907 
2908     /* restore the VM state */
2909     f = qemu_fopen_bdrv(bs_vm_state, 0);
2910     if (!f) {
2911         error_setg(errp, "Could not open VM state file");
2912         ret = -EINVAL;
2913         goto err_drain;
2914     }
2915 
2916     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
2917     mis->from_src_file = f;
2918 
2919     aio_context_acquire(aio_context);
2920     ret = qemu_loadvm_state(f);
2921     migration_incoming_state_destroy();
2922     aio_context_release(aio_context);
2923 
2924     bdrv_drain_all_end();
2925 
2926     if (ret < 0) {
2927         error_setg(errp, "Error %d while loading VM state", ret);
2928         return ret;
2929     }
2930 
2931     return 0;
2932 
2933 err_drain:
2934     bdrv_drain_all_end();
2935     return ret;
2936 }
2937 
2938 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2939 {
2940     qemu_ram_set_idstr(mr->ram_block,
2941                        memory_region_name(mr), dev);
2942     qemu_ram_set_migratable(mr->ram_block);
2943 }
2944 
2945 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2946 {
2947     qemu_ram_unset_idstr(mr->ram_block);
2948     qemu_ram_unset_migratable(mr->ram_block);
2949 }
2950 
2951 void vmstate_register_ram_global(MemoryRegion *mr)
2952 {
2953     vmstate_register_ram(mr, NULL);
2954 }
2955 
2956 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
2957 {
2958     /* check needed if --only-migratable is specified */
2959     if (!only_migratable) {
2960         return true;
2961     }
2962 
2963     return !(vmsd && vmsd->unmigratable);
2964 }
2965