xref: /openbmc/qemu/migration/savevm.c (revision d481cec7)
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 "net/net.h"
32 #include "migration.h"
33 #include "migration/snapshot.h"
34 #include "migration-stats.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "migration/channel-block.h"
40 #include "ram.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/clone-visitor.h"
47 #include "qapi/qapi-builtin-visit.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/job.h"
55 #include "qemu/main-loop.h"
56 #include "block/snapshot.h"
57 #include "qemu/cutils.h"
58 #include "io/channel-buffer.h"
59 #include "io/channel-file.h"
60 #include "sysemu/replay.h"
61 #include "sysemu/runstate.h"
62 #include "sysemu/sysemu.h"
63 #include "sysemu/xen.h"
64 #include "migration/colo.h"
65 #include "qemu/bitmap.h"
66 #include "net/announce.h"
67 #include "qemu/yank.h"
68 #include "yank_functions.h"
69 #include "sysemu/qtest.h"
70 #include "options.h"
71 
72 const unsigned int postcopy_ram_discard_version;
73 
74 /* Subcommands for QEMU_VM_COMMAND */
75 enum qemu_vm_cmd {
76     MIG_CMD_INVALID = 0,   /* Must be 0 */
77     MIG_CMD_OPEN_RETURN_PATH,  /* Tell the dest to open the Return path */
78     MIG_CMD_PING,              /* Request a PONG on the RP */
79 
80     MIG_CMD_POSTCOPY_ADVISE,       /* Prior to any page transfers, just
81                                       warn we might want to do PC */
82     MIG_CMD_POSTCOPY_LISTEN,       /* Start listening for incoming
83                                       pages as it's running. */
84     MIG_CMD_POSTCOPY_RUN,          /* Start execution */
85 
86     MIG_CMD_POSTCOPY_RAM_DISCARD,  /* A list of pages to discard that
87                                       were previously sent during
88                                       precopy but are dirty. */
89     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
90     MIG_CMD_ENABLE_COLO,       /* Enable COLO */
91     MIG_CMD_POSTCOPY_RESUME,   /* resume postcopy on dest */
92     MIG_CMD_RECV_BITMAP,       /* Request for recved bitmap on dst */
93     MIG_CMD_MAX
94 };
95 
96 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
97 static struct mig_cmd_args {
98     ssize_t     len; /* -1 = variable */
99     const char *name;
100 } mig_cmd_args[] = {
101     [MIG_CMD_INVALID]          = { .len = -1, .name = "INVALID" },
102     [MIG_CMD_OPEN_RETURN_PATH] = { .len =  0, .name = "OPEN_RETURN_PATH" },
103     [MIG_CMD_PING]             = { .len = sizeof(uint32_t), .name = "PING" },
104     [MIG_CMD_POSTCOPY_ADVISE]  = { .len = -1, .name = "POSTCOPY_ADVISE" },
105     [MIG_CMD_POSTCOPY_LISTEN]  = { .len =  0, .name = "POSTCOPY_LISTEN" },
106     [MIG_CMD_POSTCOPY_RUN]     = { .len =  0, .name = "POSTCOPY_RUN" },
107     [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
108                                    .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
109     [MIG_CMD_POSTCOPY_RESUME]  = { .len =  0, .name = "POSTCOPY_RESUME" },
110     [MIG_CMD_PACKAGED]         = { .len =  4, .name = "PACKAGED" },
111     [MIG_CMD_RECV_BITMAP]      = { .len = -1, .name = "RECV_BITMAP" },
112     [MIG_CMD_MAX]              = { .len = -1, .name = "MAX" },
113 };
114 
115 /* Note for MIG_CMD_POSTCOPY_ADVISE:
116  * The format of arguments is depending on postcopy mode:
117  * - postcopy RAM only
118  *   uint64_t host page size
119  *   uint64_t target page size
120  *
121  * - postcopy RAM and postcopy dirty bitmaps
122  *   format is the same as for postcopy RAM only
123  *
124  * - postcopy dirty bitmaps only
125  *   Nothing. Command length field is 0.
126  *
127  * Be careful: adding a new postcopy entity with some other parameters should
128  * not break format self-description ability. Good way is to introduce some
129  * generic extendable format with an exception for two old entities.
130  */
131 
132 /***********************************************************/
133 /* savevm/loadvm support */
134 
135 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
136 {
137     if (is_writable) {
138         return qemu_file_new_output(QIO_CHANNEL(qio_channel_block_new(bs)));
139     } else {
140         return qemu_file_new_input(QIO_CHANNEL(qio_channel_block_new(bs)));
141     }
142 }
143 
144 
145 /* QEMUFile timer support.
146  * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
147  */
148 
149 void timer_put(QEMUFile *f, QEMUTimer *ts)
150 {
151     uint64_t expire_time;
152 
153     expire_time = timer_expire_time_ns(ts);
154     qemu_put_be64(f, expire_time);
155 }
156 
157 void timer_get(QEMUFile *f, QEMUTimer *ts)
158 {
159     uint64_t expire_time;
160 
161     expire_time = qemu_get_be64(f);
162     if (expire_time != -1) {
163         timer_mod_ns(ts, expire_time);
164     } else {
165         timer_del(ts);
166     }
167 }
168 
169 
170 /* VMState timer support.
171  * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
172  */
173 
174 static int get_timer(QEMUFile *f, void *pv, size_t size,
175                      const VMStateField *field)
176 {
177     QEMUTimer *v = pv;
178     timer_get(f, v);
179     return 0;
180 }
181 
182 static int put_timer(QEMUFile *f, void *pv, size_t size,
183                      const VMStateField *field, JSONWriter *vmdesc)
184 {
185     QEMUTimer *v = pv;
186     timer_put(f, v);
187 
188     return 0;
189 }
190 
191 const VMStateInfo vmstate_info_timer = {
192     .name = "timer",
193     .get  = get_timer,
194     .put  = put_timer,
195 };
196 
197 
198 typedef struct CompatEntry {
199     char idstr[256];
200     int instance_id;
201 } CompatEntry;
202 
203 typedef struct SaveStateEntry {
204     QTAILQ_ENTRY(SaveStateEntry) entry;
205     char idstr[256];
206     uint32_t instance_id;
207     int alias_id;
208     int version_id;
209     /* version id read from the stream */
210     int load_version_id;
211     int section_id;
212     /* section id read from the stream */
213     int load_section_id;
214     const SaveVMHandlers *ops;
215     const VMStateDescription *vmsd;
216     void *opaque;
217     CompatEntry *compat;
218     int is_ram;
219 } SaveStateEntry;
220 
221 typedef struct SaveState {
222     QTAILQ_HEAD(, SaveStateEntry) handlers;
223     SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1];
224     int global_section_id;
225     uint32_t len;
226     const char *name;
227     uint32_t target_page_bits;
228     uint32_t caps_count;
229     MigrationCapability *capabilities;
230     QemuUUID uuid;
231 } SaveState;
232 
233 static SaveState savevm_state = {
234     .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
235     .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL },
236     .global_section_id = 0,
237 };
238 
239 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id);
240 
241 static bool should_validate_capability(int capability)
242 {
243     assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
244     /* Validate only new capabilities to keep compatibility. */
245     switch (capability) {
246     case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
247     case MIGRATION_CAPABILITY_MAPPED_RAM:
248         return true;
249     default:
250         return false;
251     }
252 }
253 
254 static uint32_t get_validatable_capabilities_count(void)
255 {
256     MigrationState *s = migrate_get_current();
257     uint32_t result = 0;
258     int i;
259     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
260         if (should_validate_capability(i) && s->capabilities[i]) {
261             result++;
262         }
263     }
264     return result;
265 }
266 
267 static int configuration_pre_save(void *opaque)
268 {
269     SaveState *state = opaque;
270     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
271     MigrationState *s = migrate_get_current();
272     int i, j;
273 
274     state->len = strlen(current_name);
275     state->name = current_name;
276     state->target_page_bits = qemu_target_page_bits();
277 
278     state->caps_count = get_validatable_capabilities_count();
279     state->capabilities = g_renew(MigrationCapability, state->capabilities,
280                                   state->caps_count);
281     for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
282         if (should_validate_capability(i) && s->capabilities[i]) {
283             state->capabilities[j++] = i;
284         }
285     }
286     state->uuid = qemu_uuid;
287 
288     return 0;
289 }
290 
291 static int configuration_post_save(void *opaque)
292 {
293     SaveState *state = opaque;
294 
295     g_free(state->capabilities);
296     state->capabilities = NULL;
297     state->caps_count = 0;
298     return 0;
299 }
300 
301 static int configuration_pre_load(void *opaque)
302 {
303     SaveState *state = opaque;
304 
305     /* If there is no target-page-bits subsection it means the source
306      * predates the variable-target-page-bits support and is using the
307      * minimum possible value for this CPU.
308      */
309     state->target_page_bits = qemu_target_page_bits_min();
310     return 0;
311 }
312 
313 static bool configuration_validate_capabilities(SaveState *state)
314 {
315     bool ret = true;
316     MigrationState *s = migrate_get_current();
317     unsigned long *source_caps_bm;
318     int i;
319 
320     source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
321     for (i = 0; i < state->caps_count; i++) {
322         MigrationCapability capability = state->capabilities[i];
323         set_bit(capability, source_caps_bm);
324     }
325 
326     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
327         bool source_state, target_state;
328         if (!should_validate_capability(i)) {
329             continue;
330         }
331         source_state = test_bit(i, source_caps_bm);
332         target_state = s->capabilities[i];
333         if (source_state != target_state) {
334             error_report("Capability %s is %s, but received capability is %s",
335                          MigrationCapability_str(i),
336                          target_state ? "on" : "off",
337                          source_state ? "on" : "off");
338             ret = false;
339             /* Don't break here to report all failed capabilities */
340         }
341     }
342 
343     g_free(source_caps_bm);
344     return ret;
345 }
346 
347 static int configuration_post_load(void *opaque, int version_id)
348 {
349     SaveState *state = opaque;
350     const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
351     int ret = 0;
352 
353     if (strncmp(state->name, current_name, state->len) != 0) {
354         error_report("Machine type received is '%.*s' and local is '%s'",
355                      (int) state->len, state->name, current_name);
356         ret = -EINVAL;
357         goto out;
358     }
359 
360     if (state->target_page_bits != qemu_target_page_bits()) {
361         error_report("Received TARGET_PAGE_BITS is %d but local is %d",
362                      state->target_page_bits, qemu_target_page_bits());
363         ret = -EINVAL;
364         goto out;
365     }
366 
367     if (!configuration_validate_capabilities(state)) {
368         ret = -EINVAL;
369         goto out;
370     }
371 
372 out:
373     g_free((void *)state->name);
374     state->name = NULL;
375     state->len = 0;
376     g_free(state->capabilities);
377     state->capabilities = NULL;
378     state->caps_count = 0;
379 
380     return ret;
381 }
382 
383 static int get_capability(QEMUFile *f, void *pv, size_t size,
384                           const VMStateField *field)
385 {
386     MigrationCapability *capability = pv;
387     char capability_str[UINT8_MAX + 1];
388     uint8_t len;
389     int i;
390 
391     len = qemu_get_byte(f);
392     qemu_get_buffer(f, (uint8_t *)capability_str, len);
393     capability_str[len] = '\0';
394     for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
395         if (!strcmp(MigrationCapability_str(i), capability_str)) {
396             *capability = i;
397             return 0;
398         }
399     }
400     error_report("Received unknown capability %s", capability_str);
401     return -EINVAL;
402 }
403 
404 static int put_capability(QEMUFile *f, void *pv, size_t size,
405                           const VMStateField *field, JSONWriter *vmdesc)
406 {
407     MigrationCapability *capability = pv;
408     const char *capability_str = MigrationCapability_str(*capability);
409     size_t len = strlen(capability_str);
410     assert(len <= UINT8_MAX);
411 
412     qemu_put_byte(f, len);
413     qemu_put_buffer(f, (uint8_t *)capability_str, len);
414     return 0;
415 }
416 
417 static const VMStateInfo vmstate_info_capability = {
418     .name = "capability",
419     .get  = get_capability,
420     .put  = put_capability,
421 };
422 
423 /* The target-page-bits subsection is present only if the
424  * target page size is not the same as the default (ie the
425  * minimum page size for a variable-page-size guest CPU).
426  * If it is present then it contains the actual target page
427  * bits for the machine, and migration will fail if the
428  * two ends don't agree about it.
429  */
430 static bool vmstate_target_page_bits_needed(void *opaque)
431 {
432     return qemu_target_page_bits()
433         > qemu_target_page_bits_min();
434 }
435 
436 static const VMStateDescription vmstate_target_page_bits = {
437     .name = "configuration/target-page-bits",
438     .version_id = 1,
439     .minimum_version_id = 1,
440     .needed = vmstate_target_page_bits_needed,
441     .fields = (const VMStateField[]) {
442         VMSTATE_UINT32(target_page_bits, SaveState),
443         VMSTATE_END_OF_LIST()
444     }
445 };
446 
447 static bool vmstate_capabilites_needed(void *opaque)
448 {
449     return get_validatable_capabilities_count() > 0;
450 }
451 
452 static const VMStateDescription vmstate_capabilites = {
453     .name = "configuration/capabilities",
454     .version_id = 1,
455     .minimum_version_id = 1,
456     .needed = vmstate_capabilites_needed,
457     .fields = (const VMStateField[]) {
458         VMSTATE_UINT32_V(caps_count, SaveState, 1),
459         VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
460                                     vmstate_info_capability,
461                                     MigrationCapability),
462         VMSTATE_END_OF_LIST()
463     }
464 };
465 
466 static bool vmstate_uuid_needed(void *opaque)
467 {
468     return qemu_uuid_set && migrate_validate_uuid();
469 }
470 
471 static int vmstate_uuid_post_load(void *opaque, int version_id)
472 {
473     SaveState *state = opaque;
474     char uuid_src[UUID_STR_LEN];
475     char uuid_dst[UUID_STR_LEN];
476 
477     if (!qemu_uuid_set) {
478         /*
479          * It's warning because user might not know UUID in some cases,
480          * e.g. load an old snapshot
481          */
482         qemu_uuid_unparse(&state->uuid, uuid_src);
483         warn_report("UUID is received %s, but local uuid isn't set",
484                      uuid_src);
485         return 0;
486     }
487     if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) {
488         qemu_uuid_unparse(&state->uuid, uuid_src);
489         qemu_uuid_unparse(&qemu_uuid, uuid_dst);
490         error_report("UUID received is %s and local is %s", uuid_src, uuid_dst);
491         return -EINVAL;
492     }
493     return 0;
494 }
495 
496 static const VMStateDescription vmstate_uuid = {
497     .name = "configuration/uuid",
498     .version_id = 1,
499     .minimum_version_id = 1,
500     .needed = vmstate_uuid_needed,
501     .post_load = vmstate_uuid_post_load,
502     .fields = (const VMStateField[]) {
503         VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1),
504         VMSTATE_END_OF_LIST()
505     }
506 };
507 
508 static const VMStateDescription vmstate_configuration = {
509     .name = "configuration",
510     .version_id = 1,
511     .pre_load = configuration_pre_load,
512     .post_load = configuration_post_load,
513     .pre_save = configuration_pre_save,
514     .post_save = configuration_post_save,
515     .fields = (const VMStateField[]) {
516         VMSTATE_UINT32(len, SaveState),
517         VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
518         VMSTATE_END_OF_LIST()
519     },
520     .subsections = (const VMStateDescription * const []) {
521         &vmstate_target_page_bits,
522         &vmstate_capabilites,
523         &vmstate_uuid,
524         NULL
525     }
526 };
527 
528 static void dump_vmstate_vmsd(FILE *out_file,
529                               const VMStateDescription *vmsd, int indent,
530                               bool is_subsection);
531 
532 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
533                               int indent)
534 {
535     fprintf(out_file, "%*s{\n", indent, "");
536     indent += 2;
537     fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
538     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
539             field->version_id);
540     fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
541             field->field_exists ? "true" : "false");
542     if (field->flags & VMS_ARRAY) {
543         fprintf(out_file, "%*s\"num\": %d,\n", indent, "", field->num);
544     }
545     fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
546     if (field->vmsd != NULL) {
547         fprintf(out_file, ",\n");
548         dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
549     }
550     fprintf(out_file, "\n%*s}", indent - 2, "");
551 }
552 
553 static void dump_vmstate_vmss(FILE *out_file,
554                               const VMStateDescription *subsection,
555                               int indent)
556 {
557     if (subsection != NULL) {
558         dump_vmstate_vmsd(out_file, subsection, indent, true);
559     }
560 }
561 
562 static void dump_vmstate_vmsd(FILE *out_file,
563                               const VMStateDescription *vmsd, int indent,
564                               bool is_subsection)
565 {
566     if (is_subsection) {
567         fprintf(out_file, "%*s{\n", indent, "");
568     } else {
569         fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
570     }
571     indent += 2;
572     fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
573     fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
574             vmsd->version_id);
575     fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
576             vmsd->minimum_version_id);
577     if (vmsd->fields != NULL) {
578         const VMStateField *field = vmsd->fields;
579         bool first;
580 
581         fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
582         first = true;
583         while (field->name != NULL) {
584             if (field->flags & VMS_MUST_EXIST) {
585                 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
586                 field++;
587                 continue;
588             }
589             if (!first) {
590                 fprintf(out_file, ",\n");
591             }
592             dump_vmstate_vmsf(out_file, field, indent + 2);
593             field++;
594             first = false;
595         }
596         assert(field->flags == VMS_END);
597         fprintf(out_file, "\n%*s]", indent, "");
598     }
599     if (vmsd->subsections != NULL) {
600         const VMStateDescription * const *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     g_slist_free(list);
669 }
670 
671 static uint32_t calculate_new_instance_id(const char *idstr)
672 {
673     SaveStateEntry *se;
674     uint32_t instance_id = 0;
675 
676     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
677         if (strcmp(idstr, se->idstr) == 0
678             && instance_id <= se->instance_id) {
679             instance_id = se->instance_id + 1;
680         }
681     }
682     /* Make sure we never loop over without being noticed */
683     assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
684     return instance_id;
685 }
686 
687 static int calculate_compat_instance_id(const char *idstr)
688 {
689     SaveStateEntry *se;
690     int instance_id = 0;
691 
692     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
693         if (!se->compat) {
694             continue;
695         }
696 
697         if (strcmp(idstr, se->compat->idstr) == 0
698             && instance_id <= se->compat->instance_id) {
699             instance_id = se->compat->instance_id + 1;
700         }
701     }
702     return instance_id;
703 }
704 
705 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
706 {
707     if (se->vmsd) {
708         return se->vmsd->priority;
709     }
710     return MIG_PRI_DEFAULT;
711 }
712 
713 static void savevm_state_handler_insert(SaveStateEntry *nse)
714 {
715     MigrationPriority priority = save_state_priority(nse);
716     SaveStateEntry *se;
717     int i;
718 
719     assert(priority <= MIG_PRI_MAX);
720 
721     /*
722      * This should never happen otherwise migration will probably fail
723      * silently somewhere because we can be wrongly applying one
724      * object properties upon another one.  Bail out ASAP.
725      */
726     if (find_se(nse->idstr, nse->instance_id)) {
727         error_report("%s: Detected duplicate SaveStateEntry: "
728                      "id=%s, instance_id=0x%"PRIx32, __func__,
729                      nse->idstr, nse->instance_id);
730         exit(EXIT_FAILURE);
731     }
732 
733     for (i = priority - 1; i >= 0; i--) {
734         se = savevm_state.handler_pri_head[i];
735         if (se != NULL) {
736             assert(save_state_priority(se) < priority);
737             break;
738         }
739     }
740 
741     if (i >= 0) {
742         QTAILQ_INSERT_BEFORE(se, nse, entry);
743     } else {
744         QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
745     }
746 
747     if (savevm_state.handler_pri_head[priority] == NULL) {
748         savevm_state.handler_pri_head[priority] = nse;
749     }
750 }
751 
752 static void savevm_state_handler_remove(SaveStateEntry *se)
753 {
754     SaveStateEntry *next;
755     MigrationPriority priority = save_state_priority(se);
756 
757     if (se == savevm_state.handler_pri_head[priority]) {
758         next = QTAILQ_NEXT(se, entry);
759         if (next != NULL && save_state_priority(next) == priority) {
760             savevm_state.handler_pri_head[priority] = next;
761         } else {
762             savevm_state.handler_pri_head[priority] = NULL;
763         }
764     }
765     QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
766 }
767 
768 /* TODO: Individual devices generally have very little idea about the rest
769    of the system, so instance_id should be removed/replaced.
770    Meanwhile pass -1 as instance_id if you do not already have a clearly
771    distinguishing id for all instances of your device class. */
772 int register_savevm_live(const char *idstr,
773                          uint32_t instance_id,
774                          int version_id,
775                          const SaveVMHandlers *ops,
776                          void *opaque)
777 {
778     SaveStateEntry *se;
779 
780     se = g_new0(SaveStateEntry, 1);
781     se->version_id = version_id;
782     se->section_id = savevm_state.global_section_id++;
783     se->ops = ops;
784     se->opaque = opaque;
785     se->vmsd = NULL;
786     /* if this is a live_savem then set is_ram */
787     if (ops->save_setup != NULL) {
788         se->is_ram = 1;
789     }
790 
791     pstrcat(se->idstr, sizeof(se->idstr), idstr);
792 
793     if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
794         se->instance_id = calculate_new_instance_id(se->idstr);
795     } else {
796         se->instance_id = instance_id;
797     }
798     assert(!se->compat || se->instance_id == 0);
799     savevm_state_handler_insert(se);
800     return 0;
801 }
802 
803 void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque)
804 {
805     SaveStateEntry *se, *new_se;
806     char id[256] = "";
807 
808     if (obj) {
809         char *oid = vmstate_if_get_id(obj);
810         if (oid) {
811             pstrcpy(id, sizeof(id), oid);
812             pstrcat(id, sizeof(id), "/");
813             g_free(oid);
814         }
815     }
816     pstrcat(id, sizeof(id), idstr);
817 
818     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
819         if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
820             savevm_state_handler_remove(se);
821             g_free(se->compat);
822             g_free(se);
823         }
824     }
825 }
826 
827 /*
828  * Perform some basic checks on vmsd's at registration
829  * time.
830  */
831 static void vmstate_check(const VMStateDescription *vmsd)
832 {
833     const VMStateField *field = vmsd->fields;
834     const VMStateDescription * const *subsection = vmsd->subsections;
835 
836     if (field) {
837         while (field->name) {
838             if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) {
839                 /* Recurse to sub structures */
840                 vmstate_check(field->vmsd);
841             }
842             /* Carry on */
843             field++;
844         }
845         /* Check for the end of field list canary */
846         if (field->flags != VMS_END) {
847             error_report("VMSTATE not ending with VMS_END: %s", vmsd->name);
848             g_assert_not_reached();
849         }
850     }
851 
852     while (subsection && *subsection) {
853         /*
854          * The name of a subsection should start with the name of the
855          * current object.
856          */
857         assert(!strncmp(vmsd->name, (*subsection)->name, strlen(vmsd->name)));
858         vmstate_check(*subsection);
859         subsection++;
860     }
861 }
862 
863 /*
864  * See comment in hw/intc/xics.c:icp_realize()
865  *
866  * This function can be removed when
867  * pre_2_10_vmstate_register_dummy_icp() is removed.
868  */
869 int vmstate_replace_hack_for_ppc(VMStateIf *obj, int instance_id,
870                                  const VMStateDescription *vmsd,
871                                  void *opaque)
872 {
873     SaveStateEntry *se = find_se(vmsd->name, instance_id);
874 
875     if (se) {
876         savevm_state_handler_remove(se);
877         g_free(se->compat);
878         g_free(se);
879     }
880     return vmstate_register(obj, instance_id, vmsd, opaque);
881 }
882 
883 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
884                                    const VMStateDescription *vmsd,
885                                    void *opaque, int alias_id,
886                                    int required_for_version,
887                                    Error **errp)
888 {
889     SaveStateEntry *se;
890 
891     /* If this triggers, alias support can be dropped for the vmsd. */
892     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
893 
894     se = g_new0(SaveStateEntry, 1);
895     se->version_id = vmsd->version_id;
896     se->section_id = savevm_state.global_section_id++;
897     se->opaque = opaque;
898     se->vmsd = vmsd;
899     se->alias_id = alias_id;
900 
901     if (obj) {
902         char *id = vmstate_if_get_id(obj);
903         if (id) {
904             if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
905                 sizeof(se->idstr)) {
906                 error_setg(errp, "Path too long for VMState (%s)", id);
907                 g_free(id);
908                 g_free(se);
909 
910                 return -1;
911             }
912             g_free(id);
913 
914             se->compat = g_new0(CompatEntry, 1);
915             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
916             se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ?
917                          calculate_compat_instance_id(vmsd->name) : instance_id;
918             instance_id = VMSTATE_INSTANCE_ID_ANY;
919         }
920     }
921     pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
922 
923     if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
924         se->instance_id = calculate_new_instance_id(se->idstr);
925     } else {
926         se->instance_id = instance_id;
927     }
928 
929     /* Perform a recursive sanity check during the test runs */
930     if (qtest_enabled()) {
931         vmstate_check(vmsd);
932     }
933     assert(!se->compat || se->instance_id == 0);
934     savevm_state_handler_insert(se);
935     return 0;
936 }
937 
938 void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
939                         void *opaque)
940 {
941     SaveStateEntry *se, *new_se;
942 
943     QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
944         if (se->vmsd == vmsd && se->opaque == opaque) {
945             savevm_state_handler_remove(se);
946             g_free(se->compat);
947             g_free(se);
948         }
949     }
950 }
951 
952 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
953 {
954     trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
955     if (!se->vmsd) {         /* Old style */
956         return se->ops->load_state(f, se->opaque, se->load_version_id);
957     }
958     return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
959 }
960 
961 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se,
962                                    JSONWriter *vmdesc)
963 {
964     uint64_t old_offset = qemu_file_transferred(f);
965     se->ops->save_state(f, se->opaque);
966     uint64_t size = qemu_file_transferred(f) - old_offset;
967 
968     if (vmdesc) {
969         json_writer_int64(vmdesc, "size", size);
970         json_writer_start_array(vmdesc, "fields");
971         json_writer_start_object(vmdesc, NULL);
972         json_writer_str(vmdesc, "name", "data");
973         json_writer_int64(vmdesc, "size", size);
974         json_writer_str(vmdesc, "type", "buffer");
975         json_writer_end_object(vmdesc);
976         json_writer_end_array(vmdesc);
977     }
978 }
979 
980 /*
981  * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
982  */
983 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
984                                 uint8_t section_type)
985 {
986     qemu_put_byte(f, section_type);
987     qemu_put_be32(f, se->section_id);
988 
989     if (section_type == QEMU_VM_SECTION_FULL ||
990         section_type == QEMU_VM_SECTION_START) {
991         /* ID string */
992         size_t len = strlen(se->idstr);
993         qemu_put_byte(f, len);
994         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
995 
996         qemu_put_be32(f, se->instance_id);
997         qemu_put_be32(f, se->version_id);
998     }
999 }
1000 
1001 /*
1002  * Write a footer onto device sections that catches cases misformatted device
1003  * sections.
1004  */
1005 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
1006 {
1007     if (migrate_get_current()->send_section_footer) {
1008         qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
1009         qemu_put_be32(f, se->section_id);
1010     }
1011 }
1012 
1013 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, JSONWriter *vmdesc,
1014                         Error **errp)
1015 {
1016     int ret;
1017 
1018     if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1019         return 0;
1020     }
1021     if (se->vmsd && !vmstate_section_needed(se->vmsd, se->opaque)) {
1022         trace_savevm_section_skip(se->idstr, se->section_id);
1023         return 0;
1024     }
1025 
1026     trace_savevm_section_start(se->idstr, se->section_id);
1027     save_section_header(f, se, QEMU_VM_SECTION_FULL);
1028     if (vmdesc) {
1029         json_writer_start_object(vmdesc, NULL);
1030         json_writer_str(vmdesc, "name", se->idstr);
1031         json_writer_int64(vmdesc, "instance_id", se->instance_id);
1032     }
1033 
1034     trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
1035     if (!se->vmsd) {
1036         vmstate_save_old_style(f, se, vmdesc);
1037     } else {
1038         ret = vmstate_save_state_with_err(f, se->vmsd, se->opaque, vmdesc,
1039                                           errp);
1040         if (ret) {
1041             return ret;
1042         }
1043     }
1044 
1045     trace_savevm_section_end(se->idstr, se->section_id, 0);
1046     save_section_footer(f, se);
1047     if (vmdesc) {
1048         json_writer_end_object(vmdesc);
1049     }
1050     return 0;
1051 }
1052 /**
1053  * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
1054  *                           command and associated data.
1055  *
1056  * @f: File to send command on
1057  * @command: Command type to send
1058  * @len: Length of associated data
1059  * @data: Data associated with command.
1060  */
1061 static void qemu_savevm_command_send(QEMUFile *f,
1062                                      enum qemu_vm_cmd command,
1063                                      uint16_t len,
1064                                      uint8_t *data)
1065 {
1066     trace_savevm_command_send(command, len);
1067     qemu_put_byte(f, QEMU_VM_COMMAND);
1068     qemu_put_be16(f, (uint16_t)command);
1069     qemu_put_be16(f, len);
1070     qemu_put_buffer(f, data, len);
1071     qemu_fflush(f);
1072 }
1073 
1074 void qemu_savevm_send_colo_enable(QEMUFile *f)
1075 {
1076     trace_savevm_send_colo_enable();
1077     qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
1078 }
1079 
1080 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
1081 {
1082     uint32_t buf;
1083 
1084     trace_savevm_send_ping(value);
1085     buf = cpu_to_be32(value);
1086     qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
1087 }
1088 
1089 void qemu_savevm_send_open_return_path(QEMUFile *f)
1090 {
1091     trace_savevm_send_open_return_path();
1092     qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
1093 }
1094 
1095 /* We have a buffer of data to send; we don't want that all to be loaded
1096  * by the command itself, so the command contains just the length of the
1097  * extra buffer that we then send straight after it.
1098  * TODO: Must be a better way to organise that
1099  *
1100  * Returns:
1101  *    0 on success
1102  *    -ve on error
1103  */
1104 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
1105 {
1106     uint32_t tmp;
1107     MigrationState *ms = migrate_get_current();
1108     Error *local_err = NULL;
1109 
1110     if (len > MAX_VM_CMD_PACKAGED_SIZE) {
1111         error_setg(&local_err, "%s: Unreasonably large packaged state: %zu",
1112                      __func__, len);
1113         migrate_set_error(ms, local_err);
1114         error_report_err(local_err);
1115         return -1;
1116     }
1117 
1118     tmp = cpu_to_be32(len);
1119 
1120     trace_qemu_savevm_send_packaged();
1121     qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
1122 
1123     qemu_put_buffer(f, buf, len);
1124 
1125     return 0;
1126 }
1127 
1128 /* Send prior to any postcopy transfer */
1129 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
1130 {
1131     if (migrate_postcopy_ram()) {
1132         uint64_t tmp[2];
1133         tmp[0] = cpu_to_be64(ram_pagesize_summary());
1134         tmp[1] = cpu_to_be64(qemu_target_page_size());
1135 
1136         trace_qemu_savevm_send_postcopy_advise();
1137         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
1138                                  16, (uint8_t *)tmp);
1139     } else {
1140         qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
1141     }
1142 }
1143 
1144 /* Sent prior to starting the destination running in postcopy, discard pages
1145  * that have already been sent but redirtied on the source.
1146  * CMD_POSTCOPY_RAM_DISCARD consist of:
1147  *      byte   version (0)
1148  *      byte   Length of name field (not including 0)
1149  *  n x byte   RAM block name
1150  *      byte   0 terminator (just for safety)
1151  *  n x        Byte ranges within the named RAMBlock
1152  *      be64   Start of the range
1153  *      be64   Length
1154  *
1155  *  name:  RAMBlock name that these entries are part of
1156  *  len: Number of page entries
1157  *  start_list: 'len' addresses
1158  *  length_list: 'len' addresses
1159  *
1160  */
1161 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1162                                            uint16_t len,
1163                                            uint64_t *start_list,
1164                                            uint64_t *length_list)
1165 {
1166     uint8_t *buf;
1167     uint16_t tmplen;
1168     uint16_t t;
1169     size_t name_len = strlen(name);
1170 
1171     trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1172     assert(name_len < 256);
1173     buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1174     buf[0] = postcopy_ram_discard_version;
1175     buf[1] = name_len;
1176     memcpy(buf + 2, name, name_len);
1177     tmplen = 2 + name_len;
1178     buf[tmplen++] = '\0';
1179 
1180     for (t = 0; t < len; t++) {
1181         stq_be_p(buf + tmplen, start_list[t]);
1182         tmplen += 8;
1183         stq_be_p(buf + tmplen, length_list[t]);
1184         tmplen += 8;
1185     }
1186     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1187     g_free(buf);
1188 }
1189 
1190 /* Get the destination into a state where it can receive postcopy data. */
1191 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1192 {
1193     trace_savevm_send_postcopy_listen();
1194     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1195 }
1196 
1197 /* Kick the destination into running */
1198 void qemu_savevm_send_postcopy_run(QEMUFile *f)
1199 {
1200     trace_savevm_send_postcopy_run();
1201     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1202 }
1203 
1204 void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1205 {
1206     trace_savevm_send_postcopy_resume();
1207     qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1208 }
1209 
1210 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1211 {
1212     size_t len;
1213     char buf[256];
1214 
1215     trace_savevm_send_recv_bitmap(block_name);
1216 
1217     buf[0] = len = strlen(block_name);
1218     memcpy(buf + 1, block_name, len);
1219 
1220     qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1221 }
1222 
1223 bool qemu_savevm_state_blocked(Error **errp)
1224 {
1225     SaveStateEntry *se;
1226 
1227     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1228         if (se->vmsd && se->vmsd->unmigratable) {
1229             error_setg(errp, "State blocked by non-migratable device '%s'",
1230                        se->idstr);
1231             return true;
1232         }
1233     }
1234     return false;
1235 }
1236 
1237 void qemu_savevm_non_migratable_list(strList **reasons)
1238 {
1239     SaveStateEntry *se;
1240 
1241     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1242         if (se->vmsd && se->vmsd->unmigratable) {
1243             QAPI_LIST_PREPEND(*reasons,
1244                               g_strdup_printf("non-migratable device: %s",
1245                                               se->idstr));
1246         }
1247     }
1248 }
1249 
1250 void qemu_savevm_state_header(QEMUFile *f)
1251 {
1252     MigrationState *s = migrate_get_current();
1253 
1254     s->vmdesc = json_writer_new(false);
1255 
1256     trace_savevm_state_header();
1257     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1258     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1259 
1260     if (s->send_configuration) {
1261         qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1262 
1263         /*
1264          * This starts the main json object and is paired with the
1265          * json_writer_end_object in
1266          * qemu_savevm_state_complete_precopy_non_iterable
1267          */
1268         json_writer_start_object(s->vmdesc, NULL);
1269 
1270         json_writer_start_object(s->vmdesc, "configuration");
1271         vmstate_save_state(f, &vmstate_configuration, &savevm_state, s->vmdesc);
1272         json_writer_end_object(s->vmdesc);
1273     }
1274 }
1275 
1276 bool qemu_savevm_state_guest_unplug_pending(void)
1277 {
1278     SaveStateEntry *se;
1279 
1280     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1281         if (se->vmsd && se->vmsd->dev_unplug_pending &&
1282             se->vmsd->dev_unplug_pending(se->opaque)) {
1283             return true;
1284         }
1285     }
1286 
1287     return false;
1288 }
1289 
1290 int qemu_savevm_state_prepare(Error **errp)
1291 {
1292     SaveStateEntry *se;
1293     int ret;
1294 
1295     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1296         if (!se->ops || !se->ops->save_prepare) {
1297             continue;
1298         }
1299         if (se->ops->is_active) {
1300             if (!se->ops->is_active(se->opaque)) {
1301                 continue;
1302             }
1303         }
1304 
1305         ret = se->ops->save_prepare(se->opaque, errp);
1306         if (ret < 0) {
1307             return ret;
1308         }
1309     }
1310 
1311     return 0;
1312 }
1313 
1314 int qemu_savevm_state_setup(QEMUFile *f, Error **errp)
1315 {
1316     ERRP_GUARD();
1317     MigrationState *ms = migrate_get_current();
1318     SaveStateEntry *se;
1319     int ret = 0;
1320 
1321     json_writer_int64(ms->vmdesc, "page_size", qemu_target_page_size());
1322     json_writer_start_array(ms->vmdesc, "devices");
1323 
1324     trace_savevm_state_setup();
1325     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1326         if (se->vmsd && se->vmsd->early_setup) {
1327             ret = vmstate_save(f, se, ms->vmdesc, errp);
1328             if (ret) {
1329                 migrate_set_error(ms, *errp);
1330                 qemu_file_set_error(f, ret);
1331                 break;
1332             }
1333             continue;
1334         }
1335 
1336         if (!se->ops || !se->ops->save_setup) {
1337             continue;
1338         }
1339         if (se->ops->is_active) {
1340             if (!se->ops->is_active(se->opaque)) {
1341                 continue;
1342             }
1343         }
1344         save_section_header(f, se, QEMU_VM_SECTION_START);
1345 
1346         ret = se->ops->save_setup(f, se->opaque, errp);
1347         save_section_footer(f, se);
1348         if (ret < 0) {
1349             qemu_file_set_error(f, ret);
1350             break;
1351         }
1352     }
1353 
1354     if (ret) {
1355         return ret;
1356     }
1357 
1358     /* TODO: Should we check that errp is set in case of failure ? */
1359     return precopy_notify(PRECOPY_NOTIFY_SETUP, errp);
1360 }
1361 
1362 int qemu_savevm_state_resume_prepare(MigrationState *s)
1363 {
1364     SaveStateEntry *se;
1365     int ret;
1366 
1367     trace_savevm_state_resume_prepare();
1368 
1369     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1370         if (!se->ops || !se->ops->resume_prepare) {
1371             continue;
1372         }
1373         if (se->ops->is_active) {
1374             if (!se->ops->is_active(se->opaque)) {
1375                 continue;
1376             }
1377         }
1378         ret = se->ops->resume_prepare(s, se->opaque);
1379         if (ret < 0) {
1380             return ret;
1381         }
1382     }
1383 
1384     return 0;
1385 }
1386 
1387 /*
1388  * this function has three return values:
1389  *   negative: there was one error, and we have -errno.
1390  *   0 : We haven't finished, caller have to go again
1391  *   1 : We have finished, we can go to complete phase
1392  */
1393 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1394 {
1395     SaveStateEntry *se;
1396     bool all_finished = true;
1397     int ret;
1398 
1399     trace_savevm_state_iterate();
1400     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1401         if (!se->ops || !se->ops->save_live_iterate) {
1402             continue;
1403         }
1404         if (se->ops->is_active &&
1405             !se->ops->is_active(se->opaque)) {
1406             continue;
1407         }
1408         if (se->ops->is_active_iterate &&
1409             !se->ops->is_active_iterate(se->opaque)) {
1410             continue;
1411         }
1412         /*
1413          * In the postcopy phase, any device that doesn't know how to
1414          * do postcopy should have saved it's state in the _complete
1415          * call that's already run, it might get confused if we call
1416          * iterate afterwards.
1417          */
1418         if (postcopy &&
1419             !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
1420             continue;
1421         }
1422         if (migration_rate_exceeded(f)) {
1423             return 0;
1424         }
1425         trace_savevm_section_start(se->idstr, se->section_id);
1426 
1427         save_section_header(f, se, QEMU_VM_SECTION_PART);
1428 
1429         ret = se->ops->save_live_iterate(f, se->opaque);
1430         trace_savevm_section_end(se->idstr, se->section_id, ret);
1431         save_section_footer(f, se);
1432 
1433         if (ret < 0) {
1434             error_report("failed to save SaveStateEntry with id(name): "
1435                          "%d(%s): %d",
1436                          se->section_id, se->idstr, ret);
1437             qemu_file_set_error(f, ret);
1438             return ret;
1439         } else if (!ret) {
1440             all_finished = false;
1441         }
1442     }
1443     return all_finished;
1444 }
1445 
1446 static bool should_send_vmdesc(void)
1447 {
1448     MachineState *machine = MACHINE(qdev_get_machine());
1449     bool in_postcopy = migration_in_postcopy();
1450     return !machine->suppress_vmdesc && !in_postcopy;
1451 }
1452 
1453 /*
1454  * Calls the save_live_complete_postcopy methods
1455  * causing the last few pages to be sent immediately and doing any associated
1456  * cleanup.
1457  * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1458  * all the other devices, but that happens at the point we switch to postcopy.
1459  */
1460 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1461 {
1462     SaveStateEntry *se;
1463     int ret;
1464 
1465     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1466         if (!se->ops || !se->ops->save_live_complete_postcopy) {
1467             continue;
1468         }
1469         if (se->ops->is_active) {
1470             if (!se->ops->is_active(se->opaque)) {
1471                 continue;
1472             }
1473         }
1474         trace_savevm_section_start(se->idstr, se->section_id);
1475         /* Section type */
1476         qemu_put_byte(f, QEMU_VM_SECTION_END);
1477         qemu_put_be32(f, se->section_id);
1478 
1479         ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1480         trace_savevm_section_end(se->idstr, se->section_id, ret);
1481         save_section_footer(f, se);
1482         if (ret < 0) {
1483             qemu_file_set_error(f, ret);
1484             return;
1485         }
1486     }
1487 
1488     qemu_put_byte(f, QEMU_VM_EOF);
1489     qemu_fflush(f);
1490 }
1491 
1492 static
1493 int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
1494 {
1495     int64_t start_ts_each, end_ts_each;
1496     SaveStateEntry *se;
1497     int ret;
1498 
1499     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1500         if (!se->ops ||
1501             (in_postcopy && se->ops->has_postcopy &&
1502              se->ops->has_postcopy(se->opaque)) ||
1503             !se->ops->save_live_complete_precopy) {
1504             continue;
1505         }
1506 
1507         if (se->ops->is_active) {
1508             if (!se->ops->is_active(se->opaque)) {
1509                 continue;
1510             }
1511         }
1512 
1513         start_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1514         trace_savevm_section_start(se->idstr, se->section_id);
1515 
1516         save_section_header(f, se, QEMU_VM_SECTION_END);
1517 
1518         ret = se->ops->save_live_complete_precopy(f, se->opaque);
1519         trace_savevm_section_end(se->idstr, se->section_id, ret);
1520         save_section_footer(f, se);
1521         if (ret < 0) {
1522             qemu_file_set_error(f, ret);
1523             return -1;
1524         }
1525         end_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1526         trace_vmstate_downtime_save("iterable", se->idstr, se->instance_id,
1527                                     end_ts_each - start_ts_each);
1528     }
1529 
1530     trace_vmstate_downtime_checkpoint("src-iterable-saved");
1531 
1532     return 0;
1533 }
1534 
1535 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1536                                                     bool in_postcopy,
1537                                                     bool inactivate_disks)
1538 {
1539     MigrationState *ms = migrate_get_current();
1540     int64_t start_ts_each, end_ts_each;
1541     JSONWriter *vmdesc = ms->vmdesc;
1542     int vmdesc_len;
1543     SaveStateEntry *se;
1544     Error *local_err = NULL;
1545     int ret;
1546 
1547     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1548         if (se->vmsd && se->vmsd->early_setup) {
1549             /* Already saved during qemu_savevm_state_setup(). */
1550             continue;
1551         }
1552 
1553         start_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1554 
1555         ret = vmstate_save(f, se, vmdesc, &local_err);
1556         if (ret) {
1557             migrate_set_error(ms, local_err);
1558             error_report_err(local_err);
1559             qemu_file_set_error(f, ret);
1560             return ret;
1561         }
1562 
1563         end_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1564         trace_vmstate_downtime_save("non-iterable", se->idstr, se->instance_id,
1565                                     end_ts_each - start_ts_each);
1566     }
1567 
1568     if (inactivate_disks) {
1569         /* Inactivate before sending QEMU_VM_EOF so that the
1570          * bdrv_activate_all() on the other end won't fail. */
1571         ret = bdrv_inactivate_all();
1572         if (ret) {
1573             error_setg(&local_err, "%s: bdrv_inactivate_all() failed (%d)",
1574                        __func__, ret);
1575             migrate_set_error(ms, local_err);
1576             error_report_err(local_err);
1577             qemu_file_set_error(f, ret);
1578             return ret;
1579         }
1580     }
1581     if (!in_postcopy) {
1582         /* Postcopy stream will still be going */
1583         qemu_put_byte(f, QEMU_VM_EOF);
1584     }
1585 
1586     json_writer_end_array(vmdesc);
1587     json_writer_end_object(vmdesc);
1588     vmdesc_len = strlen(json_writer_get(vmdesc));
1589 
1590     if (should_send_vmdesc()) {
1591         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1592         qemu_put_be32(f, vmdesc_len);
1593         qemu_put_buffer(f, (uint8_t *)json_writer_get(vmdesc), vmdesc_len);
1594     }
1595 
1596     /* Free it now to detect any inconsistencies. */
1597     json_writer_free(vmdesc);
1598     ms->vmdesc = NULL;
1599 
1600     trace_vmstate_downtime_checkpoint("src-non-iterable-saved");
1601 
1602     return 0;
1603 }
1604 
1605 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1606                                        bool inactivate_disks)
1607 {
1608     int ret;
1609     Error *local_err = NULL;
1610     bool in_postcopy = migration_in_postcopy();
1611 
1612     if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1613         error_report_err(local_err);
1614     }
1615 
1616     trace_savevm_state_complete_precopy();
1617 
1618     cpu_synchronize_all_states();
1619 
1620     if (!in_postcopy || iterable_only) {
1621         ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1622         if (ret) {
1623             return ret;
1624         }
1625     }
1626 
1627     if (iterable_only) {
1628         goto flush;
1629     }
1630 
1631     ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1632                                                           inactivate_disks);
1633     if (ret) {
1634         return ret;
1635     }
1636 
1637 flush:
1638     return qemu_fflush(f);
1639 }
1640 
1641 /* Give an estimate of the amount left to be transferred,
1642  * the result is split into the amount for units that can and
1643  * for units that can't do postcopy.
1644  */
1645 void qemu_savevm_state_pending_estimate(uint64_t *must_precopy,
1646                                         uint64_t *can_postcopy)
1647 {
1648     SaveStateEntry *se;
1649 
1650     *must_precopy = 0;
1651     *can_postcopy = 0;
1652 
1653     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1654         if (!se->ops || !se->ops->state_pending_estimate) {
1655             continue;
1656         }
1657         if (se->ops->is_active) {
1658             if (!se->ops->is_active(se->opaque)) {
1659                 continue;
1660             }
1661         }
1662         se->ops->state_pending_estimate(se->opaque, must_precopy, can_postcopy);
1663     }
1664 }
1665 
1666 void qemu_savevm_state_pending_exact(uint64_t *must_precopy,
1667                                      uint64_t *can_postcopy)
1668 {
1669     SaveStateEntry *se;
1670 
1671     *must_precopy = 0;
1672     *can_postcopy = 0;
1673 
1674     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1675         if (!se->ops || !se->ops->state_pending_exact) {
1676             continue;
1677         }
1678         if (se->ops->is_active) {
1679             if (!se->ops->is_active(se->opaque)) {
1680                 continue;
1681             }
1682         }
1683         se->ops->state_pending_exact(se->opaque, must_precopy, can_postcopy);
1684     }
1685 }
1686 
1687 void qemu_savevm_state_cleanup(void)
1688 {
1689     SaveStateEntry *se;
1690     Error *local_err = NULL;
1691 
1692     if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1693         error_report_err(local_err);
1694     }
1695 
1696     trace_savevm_state_cleanup();
1697     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1698         if (se->ops && se->ops->save_cleanup) {
1699             se->ops->save_cleanup(se->opaque);
1700         }
1701     }
1702 }
1703 
1704 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1705 {
1706     int ret;
1707     MigrationState *ms = migrate_get_current();
1708     MigrationStatus status;
1709 
1710     if (migration_is_running()) {
1711         error_setg(errp, "There's a migration process in progress");
1712         return -EINVAL;
1713     }
1714 
1715     ret = migrate_init(ms, errp);
1716     if (ret) {
1717         return ret;
1718     }
1719     ms->to_dst_file = f;
1720 
1721     qemu_savevm_state_header(f);
1722     ret = qemu_savevm_state_setup(f, errp);
1723     if (ret) {
1724         goto cleanup;
1725     }
1726 
1727     while (qemu_file_get_error(f) == 0) {
1728         if (qemu_savevm_state_iterate(f, false) > 0) {
1729             break;
1730         }
1731     }
1732 
1733     ret = qemu_file_get_error(f);
1734     if (ret == 0) {
1735         qemu_savevm_state_complete_precopy(f, false, false);
1736         ret = qemu_file_get_error(f);
1737     }
1738     if (ret != 0) {
1739         error_setg_errno(errp, -ret, "Error while writing VM state");
1740     }
1741 cleanup:
1742     qemu_savevm_state_cleanup();
1743 
1744     if (ret != 0) {
1745         status = MIGRATION_STATUS_FAILED;
1746     } else {
1747         status = MIGRATION_STATUS_COMPLETED;
1748     }
1749     migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1750 
1751     /* f is outer parameter, it should not stay in global migration state after
1752      * this function finished */
1753     ms->to_dst_file = NULL;
1754 
1755     return ret;
1756 }
1757 
1758 void qemu_savevm_live_state(QEMUFile *f)
1759 {
1760     /* save QEMU_VM_SECTION_END section */
1761     qemu_savevm_state_complete_precopy(f, true, false);
1762     qemu_put_byte(f, QEMU_VM_EOF);
1763 }
1764 
1765 int qemu_save_device_state(QEMUFile *f)
1766 {
1767     MigrationState *ms = migrate_get_current();
1768     Error *local_err = NULL;
1769     SaveStateEntry *se;
1770 
1771     if (!migration_in_colo_state()) {
1772         qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1773         qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1774     }
1775     cpu_synchronize_all_states();
1776 
1777     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1778         int ret;
1779 
1780         if (se->is_ram) {
1781             continue;
1782         }
1783         ret = vmstate_save(f, se, NULL, &local_err);
1784         if (ret) {
1785             migrate_set_error(ms, local_err);
1786             error_report_err(local_err);
1787             return ret;
1788         }
1789     }
1790 
1791     qemu_put_byte(f, QEMU_VM_EOF);
1792 
1793     return qemu_file_get_error(f);
1794 }
1795 
1796 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id)
1797 {
1798     SaveStateEntry *se;
1799 
1800     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1801         if (!strcmp(se->idstr, idstr) &&
1802             (instance_id == se->instance_id ||
1803              instance_id == se->alias_id))
1804             return se;
1805         /* Migrating from an older version? */
1806         if (strstr(se->idstr, idstr) && se->compat) {
1807             if (!strcmp(se->compat->idstr, idstr) &&
1808                 (instance_id == se->compat->instance_id ||
1809                  instance_id == se->alias_id))
1810                 return se;
1811         }
1812     }
1813     return NULL;
1814 }
1815 
1816 enum LoadVMExitCodes {
1817     /* Allow a command to quit all layers of nested loadvm loops */
1818     LOADVM_QUIT     =  1,
1819 };
1820 
1821 /* ------ incoming postcopy messages ------ */
1822 /* 'advise' arrives before any transfers just to tell us that a postcopy
1823  * *might* happen - it might be skipped if precopy transferred everything
1824  * quickly.
1825  */
1826 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1827                                          uint16_t len)
1828 {
1829     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1830     uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1831     size_t page_size = qemu_target_page_size();
1832     Error *local_err = NULL;
1833 
1834     trace_loadvm_postcopy_handle_advise();
1835     if (ps != POSTCOPY_INCOMING_NONE) {
1836         error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1837         return -1;
1838     }
1839 
1840     switch (len) {
1841     case 0:
1842         if (migrate_postcopy_ram()) {
1843             error_report("RAM postcopy is enabled but have 0 byte advise");
1844             return -EINVAL;
1845         }
1846         return 0;
1847     case 8 + 8:
1848         if (!migrate_postcopy_ram()) {
1849             error_report("RAM postcopy is disabled but have 16 byte advise");
1850             return -EINVAL;
1851         }
1852         break;
1853     default:
1854         error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1855         return -EINVAL;
1856     }
1857 
1858     if (!postcopy_ram_supported_by_host(mis, &local_err)) {
1859         error_report_err(local_err);
1860         postcopy_state_set(POSTCOPY_INCOMING_NONE);
1861         return -1;
1862     }
1863 
1864     remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1865     local_pagesize_summary = ram_pagesize_summary();
1866 
1867     if (remote_pagesize_summary != local_pagesize_summary)  {
1868         /*
1869          * This detects two potential causes of mismatch:
1870          *   a) A mismatch in host page sizes
1871          *      Some combinations of mismatch are probably possible but it gets
1872          *      a bit more complicated.  In particular we need to place whole
1873          *      host pages on the dest at once, and we need to ensure that we
1874          *      handle dirtying to make sure we never end up sending part of
1875          *      a hostpage on it's own.
1876          *   b) The use of different huge page sizes on source/destination
1877          *      a more fine grain test is performed during RAM block migration
1878          *      but this test here causes a nice early clear failure, and
1879          *      also fails when passed to an older qemu that doesn't
1880          *      do huge pages.
1881          */
1882         error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1883                                                              " d=%" PRIx64 ")",
1884                      remote_pagesize_summary, local_pagesize_summary);
1885         return -1;
1886     }
1887 
1888     remote_tps = qemu_get_be64(mis->from_src_file);
1889     if (remote_tps != page_size) {
1890         /*
1891          * Again, some differences could be dealt with, but for now keep it
1892          * simple.
1893          */
1894         error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1895                      (int)remote_tps, page_size);
1896         return -1;
1897     }
1898 
1899     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1900         error_report_err(local_err);
1901         return -1;
1902     }
1903 
1904     if (ram_postcopy_incoming_init(mis)) {
1905         return -1;
1906     }
1907 
1908     return 0;
1909 }
1910 
1911 /* After postcopy we will be told to throw some pages away since they're
1912  * dirty and will have to be demand fetched.  Must happen before CPU is
1913  * started.
1914  * There can be 0..many of these messages, each encoding multiple pages.
1915  */
1916 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1917                                               uint16_t len)
1918 {
1919     int tmp;
1920     char ramid[256];
1921     PostcopyState ps = postcopy_state_get();
1922 
1923     trace_loadvm_postcopy_ram_handle_discard();
1924 
1925     switch (ps) {
1926     case POSTCOPY_INCOMING_ADVISE:
1927         /* 1st discard */
1928         tmp = postcopy_ram_prepare_discard(mis);
1929         if (tmp) {
1930             return tmp;
1931         }
1932         break;
1933 
1934     case POSTCOPY_INCOMING_DISCARD:
1935         /* Expected state */
1936         break;
1937 
1938     default:
1939         error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1940                      ps);
1941         return -1;
1942     }
1943     /* We're expecting a
1944      *    Version (0)
1945      *    a RAM ID string (length byte, name, 0 term)
1946      *    then at least 1 16 byte chunk
1947     */
1948     if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1949         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1950         return -1;
1951     }
1952 
1953     tmp = qemu_get_byte(mis->from_src_file);
1954     if (tmp != postcopy_ram_discard_version) {
1955         error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1956         return -1;
1957     }
1958 
1959     if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1960         error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1961         return -1;
1962     }
1963     tmp = qemu_get_byte(mis->from_src_file);
1964     if (tmp != 0) {
1965         error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1966         return -1;
1967     }
1968 
1969     len -= 3 + strlen(ramid);
1970     if (len % 16) {
1971         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1972         return -1;
1973     }
1974     trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1975     while (len) {
1976         uint64_t start_addr, block_length;
1977         start_addr = qemu_get_be64(mis->from_src_file);
1978         block_length = qemu_get_be64(mis->from_src_file);
1979 
1980         len -= 16;
1981         int ret = ram_discard_range(ramid, start_addr, block_length);
1982         if (ret) {
1983             return ret;
1984         }
1985     }
1986     trace_loadvm_postcopy_ram_handle_discard_end();
1987 
1988     return 0;
1989 }
1990 
1991 /*
1992  * Triggered by a postcopy_listen command; this thread takes over reading
1993  * the input stream, leaving the main thread free to carry on loading the rest
1994  * of the device state (from RAM).
1995  * (TODO:This could do with being in a postcopy file - but there again it's
1996  * just another input loop, not that postcopy specific)
1997  */
1998 static void *postcopy_ram_listen_thread(void *opaque)
1999 {
2000     MigrationIncomingState *mis = migration_incoming_get_current();
2001     QEMUFile *f = mis->from_src_file;
2002     int load_res;
2003     MigrationState *migr = migrate_get_current();
2004 
2005     object_ref(OBJECT(migr));
2006 
2007     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
2008                                    MIGRATION_STATUS_POSTCOPY_ACTIVE);
2009     qemu_sem_post(&mis->thread_sync_sem);
2010     trace_postcopy_ram_listen_thread_start();
2011 
2012     rcu_register_thread();
2013     /*
2014      * Because we're a thread and not a coroutine we can't yield
2015      * in qemu_file, and thus we must be blocking now.
2016      */
2017     qemu_file_set_blocking(f, true);
2018     load_res = qemu_loadvm_state_main(f, mis);
2019 
2020     /*
2021      * This is tricky, but, mis->from_src_file can change after it
2022      * returns, when postcopy recovery happened. In the future, we may
2023      * want a wrapper for the QEMUFile handle.
2024      */
2025     f = mis->from_src_file;
2026 
2027     /* And non-blocking again so we don't block in any cleanup */
2028     qemu_file_set_blocking(f, false);
2029 
2030     trace_postcopy_ram_listen_thread_exit();
2031     if (load_res < 0) {
2032         qemu_file_set_error(f, load_res);
2033         dirty_bitmap_mig_cancel_incoming();
2034         if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2035             !migrate_postcopy_ram() && migrate_dirty_bitmaps())
2036         {
2037             error_report("%s: loadvm failed during postcopy: %d. All states "
2038                          "are migrated except dirty bitmaps. Some dirty "
2039                          "bitmaps may be lost, and present migrated dirty "
2040                          "bitmaps are correctly migrated and valid.",
2041                          __func__, load_res);
2042             load_res = 0; /* prevent further exit() */
2043         } else {
2044             error_report("%s: loadvm failed: %d", __func__, load_res);
2045             migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2046                                            MIGRATION_STATUS_FAILED);
2047         }
2048     }
2049     if (load_res >= 0) {
2050         /*
2051          * This looks good, but it's possible that the device loading in the
2052          * main thread hasn't finished yet, and so we might not be in 'RUN'
2053          * state yet; wait for the end of the main thread.
2054          */
2055         qemu_event_wait(&mis->main_thread_load_event);
2056     }
2057     postcopy_ram_incoming_cleanup(mis);
2058 
2059     if (load_res < 0) {
2060         /*
2061          * If something went wrong then we have a bad state so exit;
2062          * depending how far we got it might be possible at this point
2063          * to leave the guest running and fire MCEs for pages that never
2064          * arrived as a desperate recovery step.
2065          */
2066         rcu_unregister_thread();
2067         exit(EXIT_FAILURE);
2068     }
2069 
2070     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2071                                    MIGRATION_STATUS_COMPLETED);
2072     /*
2073      * If everything has worked fine, then the main thread has waited
2074      * for us to start, and we're the last use of the mis.
2075      * (If something broke then qemu will have to exit anyway since it's
2076      * got a bad migration state).
2077      */
2078     migration_incoming_state_destroy();
2079     qemu_loadvm_state_cleanup();
2080 
2081     rcu_unregister_thread();
2082     mis->have_listen_thread = false;
2083     postcopy_state_set(POSTCOPY_INCOMING_END);
2084 
2085     object_unref(OBJECT(migr));
2086 
2087     return NULL;
2088 }
2089 
2090 /* After this message we must be able to immediately receive postcopy data */
2091 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
2092 {
2093     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
2094     Error *local_err = NULL;
2095 
2096     trace_loadvm_postcopy_handle_listen("enter");
2097 
2098     if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
2099         error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
2100         return -1;
2101     }
2102     if (ps == POSTCOPY_INCOMING_ADVISE) {
2103         /*
2104          * A rare case, we entered listen without having to do any discards,
2105          * so do the setup that's normally done at the time of the 1st discard.
2106          */
2107         if (migrate_postcopy_ram()) {
2108             postcopy_ram_prepare_discard(mis);
2109         }
2110     }
2111 
2112     trace_loadvm_postcopy_handle_listen("after discard");
2113 
2114     /*
2115      * Sensitise RAM - can now generate requests for blocks that don't exist
2116      * However, at this point the CPU shouldn't be running, and the IO
2117      * shouldn't be doing anything yet so don't actually expect requests
2118      */
2119     if (migrate_postcopy_ram()) {
2120         if (postcopy_ram_incoming_setup(mis)) {
2121             postcopy_ram_incoming_cleanup(mis);
2122             return -1;
2123         }
2124     }
2125 
2126     trace_loadvm_postcopy_handle_listen("after uffd");
2127 
2128     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
2129         error_report_err(local_err);
2130         return -1;
2131     }
2132 
2133     mis->have_listen_thread = true;
2134     postcopy_thread_create(mis, &mis->listen_thread,
2135                            MIGRATION_THREAD_DST_LISTEN,
2136                            postcopy_ram_listen_thread, QEMU_THREAD_DETACHED);
2137     trace_loadvm_postcopy_handle_listen("return");
2138 
2139     return 0;
2140 }
2141 
2142 static void loadvm_postcopy_handle_run_bh(void *opaque)
2143 {
2144     Error *local_err = NULL;
2145     MigrationIncomingState *mis = opaque;
2146 
2147     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-enter");
2148 
2149     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
2150      * in migration.c
2151      */
2152     cpu_synchronize_all_post_init();
2153 
2154     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cpu-synced");
2155 
2156     qemu_announce_self(&mis->announce_timer, migrate_announce_params());
2157 
2158     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-announced");
2159 
2160     /* Make sure all file formats throw away their mutable metadata.
2161      * If we get an error here, just don't restart the VM yet. */
2162     bdrv_activate_all(&local_err);
2163     if (local_err) {
2164         error_report_err(local_err);
2165         local_err = NULL;
2166         autostart = false;
2167     }
2168 
2169     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cache-invalidated");
2170 
2171     dirty_bitmap_mig_before_vm_start();
2172 
2173     if (autostart) {
2174         /* Hold onto your hats, starting the CPU */
2175         vm_start();
2176     } else {
2177         /* leave it paused and let management decide when to start the CPU */
2178         runstate_set(RUN_STATE_PAUSED);
2179     }
2180 
2181     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-vm-started");
2182 }
2183 
2184 /* After all discards we can start running and asking for pages */
2185 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
2186 {
2187     PostcopyState ps = postcopy_state_get();
2188 
2189     trace_loadvm_postcopy_handle_run();
2190     if (ps != POSTCOPY_INCOMING_LISTENING) {
2191         error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
2192         return -1;
2193     }
2194 
2195     postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
2196     migration_bh_schedule(loadvm_postcopy_handle_run_bh, mis);
2197 
2198     /* We need to finish reading the stream from the package
2199      * and also stop reading anything more from the stream that loaded the
2200      * package (since it's now being read by the listener thread).
2201      * LOADVM_QUIT will quit all the layers of nested loadvm loops.
2202      */
2203     return LOADVM_QUIT;
2204 }
2205 
2206 /* We must be with page_request_mutex held */
2207 static gboolean postcopy_sync_page_req(gpointer key, gpointer value,
2208                                        gpointer data)
2209 {
2210     MigrationIncomingState *mis = data;
2211     void *host_addr = (void *) key;
2212     ram_addr_t rb_offset;
2213     RAMBlock *rb;
2214     int ret;
2215 
2216     rb = qemu_ram_block_from_host(host_addr, true, &rb_offset);
2217     if (!rb) {
2218         /*
2219          * This should _never_ happen.  However be nice for a migrating VM to
2220          * not crash/assert.  Post an error (note: intended to not use *_once
2221          * because we do want to see all the illegal addresses; and this can
2222          * never be triggered by the guest so we're safe) and move on next.
2223          */
2224         error_report("%s: illegal host addr %p", __func__, host_addr);
2225         /* Try the next entry */
2226         return FALSE;
2227     }
2228 
2229     ret = migrate_send_rp_message_req_pages(mis, rb, rb_offset);
2230     if (ret) {
2231         /* Please refer to above comment. */
2232         error_report("%s: send rp message failed for addr %p",
2233                      __func__, host_addr);
2234         return FALSE;
2235     }
2236 
2237     trace_postcopy_page_req_sync(host_addr);
2238 
2239     return FALSE;
2240 }
2241 
2242 static void migrate_send_rp_req_pages_pending(MigrationIncomingState *mis)
2243 {
2244     WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
2245         g_tree_foreach(mis->page_requested, postcopy_sync_page_req, mis);
2246     }
2247 }
2248 
2249 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
2250 {
2251     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
2252         error_report("%s: illegal resume received", __func__);
2253         /* Don't fail the load, only for this. */
2254         return 0;
2255     }
2256 
2257     /*
2258      * Reset the last_rb before we resend any page req to source again, since
2259      * the source should have it reset already.
2260      */
2261     mis->last_rb = NULL;
2262 
2263     /*
2264      * This means source VM is ready to resume the postcopy migration.
2265      */
2266     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2267                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
2268 
2269     trace_loadvm_postcopy_handle_resume();
2270 
2271     /* Tell source that "we are ready" */
2272     migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
2273 
2274     /*
2275      * After a postcopy recovery, the source should have lost the postcopy
2276      * queue, or potentially the requested pages could have been lost during
2277      * the network down phase.  Let's re-sync with the source VM by re-sending
2278      * all the pending pages that we eagerly need, so these threads won't get
2279      * blocked too long due to the recovery.
2280      *
2281      * Without this procedure, the faulted destination VM threads (waiting for
2282      * page requests right before the postcopy is interrupted) can keep hanging
2283      * until the pages are sent by the source during the background copying of
2284      * pages, or another thread faulted on the same address accidentally.
2285      */
2286     migrate_send_rp_req_pages_pending(mis);
2287 
2288     /*
2289      * It's time to switch state and release the fault thread to continue
2290      * service page faults.  Note that this should be explicitly after the
2291      * above call to migrate_send_rp_req_pages_pending().  In short:
2292      * migrate_send_rp_message_req_pages() is not thread safe, yet.
2293      */
2294     qemu_sem_post(&mis->postcopy_pause_sem_fault);
2295 
2296     if (migrate_postcopy_preempt()) {
2297         /*
2298          * The preempt channel will be created in async manner, now let's
2299          * wait for it and make sure it's created.
2300          */
2301         qemu_sem_wait(&mis->postcopy_qemufile_dst_done);
2302         assert(mis->postcopy_qemufile_dst);
2303         /* Kick the fast ram load thread too */
2304         qemu_sem_post(&mis->postcopy_pause_sem_fast_load);
2305     }
2306 
2307     return 0;
2308 }
2309 
2310 /**
2311  * Immediately following this command is a blob of data containing an embedded
2312  * chunk of migration stream; read it and load it.
2313  *
2314  * @mis: Incoming state
2315  * @length: Length of packaged data to read
2316  *
2317  * Returns: Negative values on error
2318  *
2319  */
2320 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
2321 {
2322     int ret;
2323     size_t length;
2324     QIOChannelBuffer *bioc;
2325 
2326     length = qemu_get_be32(mis->from_src_file);
2327     trace_loadvm_handle_cmd_packaged(length);
2328 
2329     if (length > MAX_VM_CMD_PACKAGED_SIZE) {
2330         error_report("Unreasonably large packaged state: %zu", length);
2331         return -1;
2332     }
2333 
2334     bioc = qio_channel_buffer_new(length);
2335     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
2336     ret = qemu_get_buffer(mis->from_src_file,
2337                           bioc->data,
2338                           length);
2339     if (ret != length) {
2340         object_unref(OBJECT(bioc));
2341         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
2342                      ret, length);
2343         return (ret < 0) ? ret : -EAGAIN;
2344     }
2345     bioc->usage += length;
2346     trace_loadvm_handle_cmd_packaged_received(ret);
2347 
2348     QEMUFile *packf = qemu_file_new_input(QIO_CHANNEL(bioc));
2349 
2350     /*
2351      * Before loading the guest states, ensure that the preempt channel has
2352      * been ready to use, as some of the states (e.g. via virtio_load) might
2353      * trigger page faults that will be handled through the preempt channel.
2354      * So yield to the main thread in the case that the channel create event
2355      * hasn't been dispatched.
2356      *
2357      * TODO: if we can move migration loadvm out of main thread, then we
2358      * won't block main thread from polling the accept() fds.  We can drop
2359      * this as a whole when that is done.
2360      */
2361     do {
2362         if (!migrate_postcopy_preempt() || !qemu_in_coroutine() ||
2363             mis->postcopy_qemufile_dst) {
2364             break;
2365         }
2366 
2367         aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
2368         qemu_coroutine_yield();
2369     } while (1);
2370 
2371     ret = qemu_loadvm_state_main(packf, mis);
2372     trace_loadvm_handle_cmd_packaged_main(ret);
2373     qemu_fclose(packf);
2374     object_unref(OBJECT(bioc));
2375 
2376     return ret;
2377 }
2378 
2379 /*
2380  * Handle request that source requests for recved_bitmap on
2381  * destination. Payload format:
2382  *
2383  * len (1 byte) + ramblock_name (<255 bytes)
2384  */
2385 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2386                                      uint16_t len)
2387 {
2388     QEMUFile *file = mis->from_src_file;
2389     RAMBlock *rb;
2390     char block_name[256];
2391     size_t cnt;
2392 
2393     cnt = qemu_get_counted_string(file, block_name);
2394     if (!cnt) {
2395         error_report("%s: failed to read block name", __func__);
2396         return -EINVAL;
2397     }
2398 
2399     /* Validate before using the data */
2400     if (qemu_file_get_error(file)) {
2401         return qemu_file_get_error(file);
2402     }
2403 
2404     if (len != cnt + 1) {
2405         error_report("%s: invalid payload length (%d)", __func__, len);
2406         return -EINVAL;
2407     }
2408 
2409     rb = qemu_ram_block_by_name(block_name);
2410     if (!rb) {
2411         error_report("%s: block '%s' not found", __func__, block_name);
2412         return -EINVAL;
2413     }
2414 
2415     migrate_send_rp_recv_bitmap(mis, block_name);
2416 
2417     trace_loadvm_handle_recv_bitmap(block_name);
2418 
2419     return 0;
2420 }
2421 
2422 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2423 {
2424     int ret = migration_incoming_enable_colo();
2425 
2426     if (!ret) {
2427         ret = colo_init_ram_cache();
2428         if (ret) {
2429             migration_incoming_disable_colo();
2430         }
2431     }
2432     return ret;
2433 }
2434 
2435 /*
2436  * Process an incoming 'QEMU_VM_COMMAND'
2437  * 0           just a normal return
2438  * LOADVM_QUIT All good, but exit the loop
2439  * <0          Error
2440  */
2441 static int loadvm_process_command(QEMUFile *f)
2442 {
2443     MigrationIncomingState *mis = migration_incoming_get_current();
2444     uint16_t cmd;
2445     uint16_t len;
2446     uint32_t tmp32;
2447 
2448     cmd = qemu_get_be16(f);
2449     len = qemu_get_be16(f);
2450 
2451     /* Check validity before continue processing of cmds */
2452     if (qemu_file_get_error(f)) {
2453         return qemu_file_get_error(f);
2454     }
2455 
2456     if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2457         error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2458         return -EINVAL;
2459     }
2460 
2461     trace_loadvm_process_command(mig_cmd_args[cmd].name, len);
2462 
2463     if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2464         error_report("%s received with bad length - expecting %zu, got %d",
2465                      mig_cmd_args[cmd].name,
2466                      (size_t)mig_cmd_args[cmd].len, len);
2467         return -ERANGE;
2468     }
2469 
2470     switch (cmd) {
2471     case MIG_CMD_OPEN_RETURN_PATH:
2472         if (mis->to_src_file) {
2473             error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2474             /* Not really a problem, so don't give up */
2475             return 0;
2476         }
2477         mis->to_src_file = qemu_file_get_return_path(f);
2478         if (!mis->to_src_file) {
2479             error_report("CMD_OPEN_RETURN_PATH failed");
2480             return -1;
2481         }
2482 
2483         /*
2484          * Switchover ack is enabled but no device uses it, so send an ACK to
2485          * source that it's OK to switchover. Do it here, after return path has
2486          * been created.
2487          */
2488         if (migrate_switchover_ack() && !mis->switchover_ack_pending_num) {
2489             int ret = migrate_send_rp_switchover_ack(mis);
2490             if (ret) {
2491                 error_report(
2492                     "Could not send switchover ack RP MSG, err %d (%s)", ret,
2493                     strerror(-ret));
2494                 return ret;
2495             }
2496         }
2497         break;
2498 
2499     case MIG_CMD_PING:
2500         tmp32 = qemu_get_be32(f);
2501         trace_loadvm_process_command_ping(tmp32);
2502         if (!mis->to_src_file) {
2503             error_report("CMD_PING (0x%x) received with no return path",
2504                          tmp32);
2505             return -1;
2506         }
2507         migrate_send_rp_pong(mis, tmp32);
2508         break;
2509 
2510     case MIG_CMD_PACKAGED:
2511         return loadvm_handle_cmd_packaged(mis);
2512 
2513     case MIG_CMD_POSTCOPY_ADVISE:
2514         return loadvm_postcopy_handle_advise(mis, len);
2515 
2516     case MIG_CMD_POSTCOPY_LISTEN:
2517         return loadvm_postcopy_handle_listen(mis);
2518 
2519     case MIG_CMD_POSTCOPY_RUN:
2520         return loadvm_postcopy_handle_run(mis);
2521 
2522     case MIG_CMD_POSTCOPY_RAM_DISCARD:
2523         return loadvm_postcopy_ram_handle_discard(mis, len);
2524 
2525     case MIG_CMD_POSTCOPY_RESUME:
2526         return loadvm_postcopy_handle_resume(mis);
2527 
2528     case MIG_CMD_RECV_BITMAP:
2529         return loadvm_handle_recv_bitmap(mis, len);
2530 
2531     case MIG_CMD_ENABLE_COLO:
2532         return loadvm_process_enable_colo(mis);
2533     }
2534 
2535     return 0;
2536 }
2537 
2538 /*
2539  * Read a footer off the wire and check that it matches the expected section
2540  *
2541  * Returns: true if the footer was good
2542  *          false if there is a problem (and calls error_report to say why)
2543  */
2544 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2545 {
2546     int ret;
2547     uint8_t read_mark;
2548     uint32_t read_section_id;
2549 
2550     if (!migrate_get_current()->send_section_footer) {
2551         /* No footer to check */
2552         return true;
2553     }
2554 
2555     read_mark = qemu_get_byte(f);
2556 
2557     ret = qemu_file_get_error(f);
2558     if (ret) {
2559         error_report("%s: Read section footer failed: %d",
2560                      __func__, ret);
2561         return false;
2562     }
2563 
2564     if (read_mark != QEMU_VM_SECTION_FOOTER) {
2565         error_report("Missing section footer for %s", se->idstr);
2566         return false;
2567     }
2568 
2569     read_section_id = qemu_get_be32(f);
2570     if (read_section_id != se->load_section_id) {
2571         error_report("Mismatched section id in footer for %s -"
2572                      " read 0x%x expected 0x%x",
2573                      se->idstr, read_section_id, se->load_section_id);
2574         return false;
2575     }
2576 
2577     /* All good */
2578     return true;
2579 }
2580 
2581 static int
2582 qemu_loadvm_section_start_full(QEMUFile *f, uint8_t type)
2583 {
2584     bool trace_downtime = (type == QEMU_VM_SECTION_FULL);
2585     uint32_t instance_id, version_id, section_id;
2586     int64_t start_ts, end_ts;
2587     SaveStateEntry *se;
2588     char idstr[256];
2589     int ret;
2590 
2591     /* Read section start */
2592     section_id = qemu_get_be32(f);
2593     if (!qemu_get_counted_string(f, idstr)) {
2594         error_report("Unable to read ID string for section %u",
2595                      section_id);
2596         return -EINVAL;
2597     }
2598     instance_id = qemu_get_be32(f);
2599     version_id = qemu_get_be32(f);
2600 
2601     ret = qemu_file_get_error(f);
2602     if (ret) {
2603         error_report("%s: Failed to read instance/version ID: %d",
2604                      __func__, ret);
2605         return ret;
2606     }
2607 
2608     trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2609             instance_id, version_id);
2610     /* Find savevm section */
2611     se = find_se(idstr, instance_id);
2612     if (se == NULL) {
2613         error_report("Unknown savevm section or instance '%s' %"PRIu32". "
2614                      "Make sure that your current VM setup matches your "
2615                      "saved VM setup, including any hotplugged devices",
2616                      idstr, instance_id);
2617         return -EINVAL;
2618     }
2619 
2620     /* Validate version */
2621     if (version_id > se->version_id) {
2622         error_report("savevm: unsupported version %d for '%s' v%d",
2623                      version_id, idstr, se->version_id);
2624         return -EINVAL;
2625     }
2626     se->load_version_id = version_id;
2627     se->load_section_id = section_id;
2628 
2629     /* Validate if it is a device's state */
2630     if (xen_enabled() && se->is_ram) {
2631         error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2632         return -EINVAL;
2633     }
2634 
2635     if (trace_downtime) {
2636         start_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2637     }
2638 
2639     ret = vmstate_load(f, se);
2640     if (ret < 0) {
2641         error_report("error while loading state for instance 0x%"PRIx32" of"
2642                      " device '%s'", instance_id, idstr);
2643         return ret;
2644     }
2645 
2646     if (trace_downtime) {
2647         end_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2648         trace_vmstate_downtime_load("non-iterable", se->idstr,
2649                                     se->instance_id, end_ts - start_ts);
2650     }
2651 
2652     if (!check_section_footer(f, se)) {
2653         return -EINVAL;
2654     }
2655 
2656     return 0;
2657 }
2658 
2659 static int
2660 qemu_loadvm_section_part_end(QEMUFile *f, uint8_t type)
2661 {
2662     bool trace_downtime = (type == QEMU_VM_SECTION_END);
2663     int64_t start_ts, end_ts;
2664     uint32_t section_id;
2665     SaveStateEntry *se;
2666     int ret;
2667 
2668     section_id = qemu_get_be32(f);
2669 
2670     ret = qemu_file_get_error(f);
2671     if (ret) {
2672         error_report("%s: Failed to read section ID: %d",
2673                      __func__, ret);
2674         return ret;
2675     }
2676 
2677     trace_qemu_loadvm_state_section_partend(section_id);
2678     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2679         if (se->load_section_id == section_id) {
2680             break;
2681         }
2682     }
2683     if (se == NULL) {
2684         error_report("Unknown savevm section %d", section_id);
2685         return -EINVAL;
2686     }
2687 
2688     if (trace_downtime) {
2689         start_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2690     }
2691 
2692     ret = vmstate_load(f, se);
2693     if (ret < 0) {
2694         error_report("error while loading state section id %d(%s)",
2695                      section_id, se->idstr);
2696         return ret;
2697     }
2698 
2699     if (trace_downtime) {
2700         end_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2701         trace_vmstate_downtime_load("iterable", se->idstr,
2702                                     se->instance_id, end_ts - start_ts);
2703     }
2704 
2705     if (!check_section_footer(f, se)) {
2706         return -EINVAL;
2707     }
2708 
2709     return 0;
2710 }
2711 
2712 static int qemu_loadvm_state_header(QEMUFile *f)
2713 {
2714     unsigned int v;
2715     int ret;
2716 
2717     v = qemu_get_be32(f);
2718     if (v != QEMU_VM_FILE_MAGIC) {
2719         error_report("Not a migration stream");
2720         return -EINVAL;
2721     }
2722 
2723     v = qemu_get_be32(f);
2724     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2725         error_report("SaveVM v2 format is obsolete and don't work anymore");
2726         return -ENOTSUP;
2727     }
2728     if (v != QEMU_VM_FILE_VERSION) {
2729         error_report("Unsupported migration stream version");
2730         return -ENOTSUP;
2731     }
2732 
2733     if (migrate_get_current()->send_configuration) {
2734         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2735             error_report("Configuration section missing");
2736             return -EINVAL;
2737         }
2738         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2739 
2740         if (ret) {
2741             return ret;
2742         }
2743     }
2744     return 0;
2745 }
2746 
2747 static void qemu_loadvm_state_switchover_ack_needed(MigrationIncomingState *mis)
2748 {
2749     SaveStateEntry *se;
2750 
2751     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2752         if (!se->ops || !se->ops->switchover_ack_needed) {
2753             continue;
2754         }
2755 
2756         if (se->ops->switchover_ack_needed(se->opaque)) {
2757             mis->switchover_ack_pending_num++;
2758         }
2759     }
2760 
2761     trace_loadvm_state_switchover_ack_needed(mis->switchover_ack_pending_num);
2762 }
2763 
2764 static int qemu_loadvm_state_setup(QEMUFile *f, Error **errp)
2765 {
2766     ERRP_GUARD();
2767     SaveStateEntry *se;
2768     int ret;
2769 
2770     trace_loadvm_state_setup();
2771     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2772         if (!se->ops || !se->ops->load_setup) {
2773             continue;
2774         }
2775         if (se->ops->is_active) {
2776             if (!se->ops->is_active(se->opaque)) {
2777                 continue;
2778             }
2779         }
2780 
2781         ret = se->ops->load_setup(f, se->opaque, errp);
2782         if (ret < 0) {
2783             error_prepend(errp, "Load state of device %s failed: ",
2784                           se->idstr);
2785             qemu_file_set_error(f, ret);
2786             return ret;
2787         }
2788     }
2789     return 0;
2790 }
2791 
2792 void qemu_loadvm_state_cleanup(void)
2793 {
2794     SaveStateEntry *se;
2795 
2796     trace_loadvm_state_cleanup();
2797     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2798         if (se->ops && se->ops->load_cleanup) {
2799             se->ops->load_cleanup(se->opaque);
2800         }
2801     }
2802 }
2803 
2804 /* Return true if we should continue the migration, or false. */
2805 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2806 {
2807     int i;
2808 
2809     trace_postcopy_pause_incoming();
2810 
2811     assert(migrate_postcopy_ram());
2812 
2813     /*
2814      * Unregister yank with either from/to src would work, since ioc behind it
2815      * is the same
2816      */
2817     migration_ioc_unregister_yank_from_file(mis->from_src_file);
2818 
2819     assert(mis->from_src_file);
2820     qemu_file_shutdown(mis->from_src_file);
2821     qemu_fclose(mis->from_src_file);
2822     mis->from_src_file = NULL;
2823 
2824     assert(mis->to_src_file);
2825     qemu_file_shutdown(mis->to_src_file);
2826     qemu_mutex_lock(&mis->rp_mutex);
2827     qemu_fclose(mis->to_src_file);
2828     mis->to_src_file = NULL;
2829     qemu_mutex_unlock(&mis->rp_mutex);
2830 
2831     /*
2832      * NOTE: this must happen before reset the PostcopyTmpPages below,
2833      * otherwise it's racy to reset those fields when the fast load thread
2834      * can be accessing it in parallel.
2835      */
2836     if (mis->postcopy_qemufile_dst) {
2837         qemu_file_shutdown(mis->postcopy_qemufile_dst);
2838         /* Take the mutex to make sure the fast ram load thread halted */
2839         qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
2840         migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
2841         qemu_fclose(mis->postcopy_qemufile_dst);
2842         mis->postcopy_qemufile_dst = NULL;
2843         qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
2844     }
2845 
2846     /* Current state can be either ACTIVE or RECOVER */
2847     migrate_set_state(&mis->state, mis->state,
2848                       MIGRATION_STATUS_POSTCOPY_PAUSED);
2849 
2850     /* Notify the fault thread for the invalidated file handle */
2851     postcopy_fault_thread_notify(mis);
2852 
2853     /*
2854      * If network is interrupted, any temp page we received will be useless
2855      * because we didn't mark them as "received" in receivedmap.  After a
2856      * proper recovery later (which will sync src dirty bitmap with receivedmap
2857      * on dest) these cached small pages will be resent again.
2858      */
2859     for (i = 0; i < mis->postcopy_channels; i++) {
2860         postcopy_temp_page_reset(&mis->postcopy_tmp_pages[i]);
2861     }
2862 
2863     error_report("Detected IO failure for postcopy. "
2864                  "Migration paused.");
2865 
2866     do {
2867         qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2868     } while (postcopy_is_paused(mis->state));
2869 
2870     trace_postcopy_pause_incoming_continued();
2871 
2872     return true;
2873 }
2874 
2875 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2876 {
2877     uint8_t section_type;
2878     int ret = 0;
2879 
2880 retry:
2881     while (true) {
2882         section_type = qemu_get_byte(f);
2883 
2884         ret = qemu_file_get_error_obj_any(f, mis->postcopy_qemufile_dst, NULL);
2885         if (ret) {
2886             break;
2887         }
2888 
2889         trace_qemu_loadvm_state_section(section_type);
2890         switch (section_type) {
2891         case QEMU_VM_SECTION_START:
2892         case QEMU_VM_SECTION_FULL:
2893             ret = qemu_loadvm_section_start_full(f, section_type);
2894             if (ret < 0) {
2895                 goto out;
2896             }
2897             break;
2898         case QEMU_VM_SECTION_PART:
2899         case QEMU_VM_SECTION_END:
2900             ret = qemu_loadvm_section_part_end(f, section_type);
2901             if (ret < 0) {
2902                 goto out;
2903             }
2904             break;
2905         case QEMU_VM_COMMAND:
2906             ret = loadvm_process_command(f);
2907             trace_qemu_loadvm_state_section_command(ret);
2908             if ((ret < 0) || (ret == LOADVM_QUIT)) {
2909                 goto out;
2910             }
2911             break;
2912         case QEMU_VM_EOF:
2913             /* This is the end of migration */
2914             goto out;
2915         default:
2916             error_report("Unknown savevm section type %d", section_type);
2917             ret = -EINVAL;
2918             goto out;
2919         }
2920     }
2921 
2922 out:
2923     if (ret < 0) {
2924         qemu_file_set_error(f, ret);
2925 
2926         /* Cancel bitmaps incoming regardless of recovery */
2927         dirty_bitmap_mig_cancel_incoming();
2928 
2929         /*
2930          * If we are during an active postcopy, then we pause instead
2931          * of bail out to at least keep the VM's dirty data.  Note
2932          * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2933          * during which we're still receiving device states and we
2934          * still haven't yet started the VM on destination.
2935          *
2936          * Only RAM postcopy supports recovery. Still, if RAM postcopy is
2937          * enabled, canceled bitmaps postcopy will not affect RAM postcopy
2938          * recovering.
2939          */
2940         if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2941             migrate_postcopy_ram() && postcopy_pause_incoming(mis)) {
2942             /* Reset f to point to the newly created channel */
2943             f = mis->from_src_file;
2944             goto retry;
2945         }
2946     }
2947     return ret;
2948 }
2949 
2950 int qemu_loadvm_state(QEMUFile *f)
2951 {
2952     MigrationIncomingState *mis = migration_incoming_get_current();
2953     Error *local_err = NULL;
2954     int ret;
2955 
2956     if (qemu_savevm_state_blocked(&local_err)) {
2957         error_report_err(local_err);
2958         return -EINVAL;
2959     }
2960 
2961     ret = qemu_loadvm_state_header(f);
2962     if (ret) {
2963         return ret;
2964     }
2965 
2966     if (qemu_loadvm_state_setup(f, &local_err) != 0) {
2967         error_report_err(local_err);
2968         return -EINVAL;
2969     }
2970 
2971     if (migrate_switchover_ack()) {
2972         qemu_loadvm_state_switchover_ack_needed(mis);
2973     }
2974 
2975     cpu_synchronize_all_pre_loadvm();
2976 
2977     ret = qemu_loadvm_state_main(f, mis);
2978     qemu_event_set(&mis->main_thread_load_event);
2979 
2980     trace_qemu_loadvm_state_post_main(ret);
2981 
2982     if (mis->have_listen_thread) {
2983         /*
2984          * Postcopy listen thread still going, don't synchronize the
2985          * cpus yet.
2986          */
2987         return ret;
2988     }
2989 
2990     if (ret == 0) {
2991         ret = qemu_file_get_error(f);
2992     }
2993 
2994     /*
2995      * Try to read in the VMDESC section as well, so that dumping tools that
2996      * intercept our migration stream have the chance to see it.
2997      */
2998 
2999     /* We've got to be careful; if we don't read the data and just shut the fd
3000      * then the sender can error if we close while it's still sending.
3001      * We also mustn't read data that isn't there; some transports (RDMA)
3002      * will stall waiting for that data when the source has already closed.
3003      */
3004     if (ret == 0 && should_send_vmdesc()) {
3005         uint8_t *buf;
3006         uint32_t size;
3007         uint8_t  section_type = qemu_get_byte(f);
3008 
3009         if (section_type != QEMU_VM_VMDESCRIPTION) {
3010             error_report("Expected vmdescription section, but got %d",
3011                          section_type);
3012             /*
3013              * It doesn't seem worth failing at this point since
3014              * we apparently have an otherwise valid VM state
3015              */
3016         } else {
3017             buf = g_malloc(0x1000);
3018             size = qemu_get_be32(f);
3019 
3020             while (size > 0) {
3021                 uint32_t read_chunk = MIN(size, 0x1000);
3022                 qemu_get_buffer(f, buf, read_chunk);
3023                 size -= read_chunk;
3024             }
3025             g_free(buf);
3026         }
3027     }
3028 
3029     cpu_synchronize_all_post_init();
3030 
3031     return ret;
3032 }
3033 
3034 int qemu_load_device_state(QEMUFile *f)
3035 {
3036     MigrationIncomingState *mis = migration_incoming_get_current();
3037     int ret;
3038 
3039     /* Load QEMU_VM_SECTION_FULL section */
3040     ret = qemu_loadvm_state_main(f, mis);
3041     if (ret < 0) {
3042         error_report("Failed to load device state: %d", ret);
3043         return ret;
3044     }
3045 
3046     cpu_synchronize_all_post_init();
3047     return 0;
3048 }
3049 
3050 int qemu_loadvm_approve_switchover(void)
3051 {
3052     MigrationIncomingState *mis = migration_incoming_get_current();
3053 
3054     if (!mis->switchover_ack_pending_num) {
3055         return -EINVAL;
3056     }
3057 
3058     mis->switchover_ack_pending_num--;
3059     trace_loadvm_approve_switchover(mis->switchover_ack_pending_num);
3060 
3061     if (mis->switchover_ack_pending_num) {
3062         return 0;
3063     }
3064 
3065     return migrate_send_rp_switchover_ack(mis);
3066 }
3067 
3068 bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
3069                   bool has_devices, strList *devices, Error **errp)
3070 {
3071     BlockDriverState *bs;
3072     QEMUSnapshotInfo sn1, *sn = &sn1;
3073     int ret = -1, ret2;
3074     QEMUFile *f;
3075     RunState saved_state = runstate_get();
3076     uint64_t vm_state_size;
3077     g_autoptr(GDateTime) now = g_date_time_new_now_local();
3078 
3079     GLOBAL_STATE_CODE();
3080 
3081     if (migration_is_blocked(errp)) {
3082         return false;
3083     }
3084 
3085     if (!replay_can_snapshot()) {
3086         error_setg(errp, "Record/replay does not allow making snapshot "
3087                    "right now. Try once more later.");
3088         return false;
3089     }
3090 
3091     if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3092         return false;
3093     }
3094 
3095     /* Delete old snapshots of the same name */
3096     if (name) {
3097         if (overwrite) {
3098             if (bdrv_all_delete_snapshot(name, has_devices,
3099                                          devices, errp) < 0) {
3100                 return false;
3101             }
3102         } else {
3103             ret2 = bdrv_all_has_snapshot(name, has_devices, devices, errp);
3104             if (ret2 < 0) {
3105                 return false;
3106             }
3107             if (ret2 == 1) {
3108                 error_setg(errp,
3109                            "Snapshot '%s' already exists in one or more devices",
3110                            name);
3111                 return false;
3112             }
3113         }
3114     }
3115 
3116     bs = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
3117     if (bs == NULL) {
3118         return false;
3119     }
3120 
3121     global_state_store();
3122     vm_stop(RUN_STATE_SAVE_VM);
3123 
3124     bdrv_drain_all_begin();
3125 
3126     memset(sn, 0, sizeof(*sn));
3127 
3128     /* fill auxiliary fields */
3129     sn->date_sec = g_date_time_to_unix(now);
3130     sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
3131     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
3132     if (replay_mode != REPLAY_MODE_NONE) {
3133         sn->icount = replay_get_current_icount();
3134     } else {
3135         sn->icount = -1ULL;
3136     }
3137 
3138     if (name) {
3139         pstrcpy(sn->name, sizeof(sn->name), name);
3140     } else {
3141         g_autofree char *autoname = g_date_time_format(now,  "vm-%Y%m%d%H%M%S");
3142         pstrcpy(sn->name, sizeof(sn->name), autoname);
3143     }
3144 
3145     /* save the VM state */
3146     f = qemu_fopen_bdrv(bs, 1);
3147     if (!f) {
3148         error_setg(errp, "Could not open VM state file");
3149         goto the_end;
3150     }
3151     ret = qemu_savevm_state(f, errp);
3152     vm_state_size = qemu_file_transferred(f);
3153     ret2 = qemu_fclose(f);
3154     if (ret < 0) {
3155         goto the_end;
3156     }
3157     if (ret2 < 0) {
3158         ret = ret2;
3159         goto the_end;
3160     }
3161 
3162     ret = bdrv_all_create_snapshot(sn, bs, vm_state_size,
3163                                    has_devices, devices, errp);
3164     if (ret < 0) {
3165         bdrv_all_delete_snapshot(sn->name, has_devices, devices, NULL);
3166         goto the_end;
3167     }
3168 
3169     ret = 0;
3170 
3171  the_end:
3172     bdrv_drain_all_end();
3173 
3174     vm_resume(saved_state);
3175     return ret == 0;
3176 }
3177 
3178 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
3179                                 Error **errp)
3180 {
3181     QEMUFile *f;
3182     QIOChannelFile *ioc;
3183     int saved_vm_running;
3184     int ret;
3185 
3186     if (!has_live) {
3187         /* live default to true so old version of Xen tool stack can have a
3188          * successful live migration */
3189         live = true;
3190     }
3191 
3192     saved_vm_running = runstate_is_running();
3193     vm_stop(RUN_STATE_SAVE_VM);
3194     global_state_store_running();
3195 
3196     ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT | O_TRUNC,
3197                                     0660, errp);
3198     if (!ioc) {
3199         goto the_end;
3200     }
3201     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
3202     f = qemu_file_new_output(QIO_CHANNEL(ioc));
3203     object_unref(OBJECT(ioc));
3204     ret = qemu_save_device_state(f);
3205     if (ret < 0 || qemu_fclose(f) < 0) {
3206         error_setg(errp, "saving Xen device state failed");
3207     } else {
3208         /* libxl calls the QMP command "stop" before calling
3209          * "xen-save-devices-state" and in case of migration failure, libxl
3210          * would call "cont".
3211          * So call bdrv_inactivate_all (release locks) here to let the other
3212          * side of the migration take control of the images.
3213          */
3214         if (live && !saved_vm_running) {
3215             ret = bdrv_inactivate_all();
3216             if (ret) {
3217                 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
3218                            __func__, ret);
3219             }
3220         }
3221     }
3222 
3223  the_end:
3224     if (saved_vm_running) {
3225         vm_start();
3226     }
3227 }
3228 
3229 void qmp_xen_load_devices_state(const char *filename, Error **errp)
3230 {
3231     QEMUFile *f;
3232     QIOChannelFile *ioc;
3233     int ret;
3234 
3235     /* Guest must be paused before loading the device state; the RAM state
3236      * will already have been loaded by xc
3237      */
3238     if (runstate_is_running()) {
3239         error_setg(errp, "Cannot update device state while vm is running");
3240         return;
3241     }
3242     vm_stop(RUN_STATE_RESTORE_VM);
3243 
3244     ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
3245     if (!ioc) {
3246         return;
3247     }
3248     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
3249     f = qemu_file_new_input(QIO_CHANNEL(ioc));
3250     object_unref(OBJECT(ioc));
3251 
3252     ret = qemu_loadvm_state(f);
3253     qemu_fclose(f);
3254     if (ret < 0) {
3255         error_setg(errp, "loading Xen device state failed");
3256     }
3257     migration_incoming_state_destroy();
3258 }
3259 
3260 bool load_snapshot(const char *name, const char *vmstate,
3261                    bool has_devices, strList *devices, Error **errp)
3262 {
3263     BlockDriverState *bs_vm_state;
3264     QEMUSnapshotInfo sn;
3265     QEMUFile *f;
3266     int ret;
3267     MigrationIncomingState *mis = migration_incoming_get_current();
3268 
3269     if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3270         return false;
3271     }
3272     ret = bdrv_all_has_snapshot(name, has_devices, devices, errp);
3273     if (ret < 0) {
3274         return false;
3275     }
3276     if (ret == 0) {
3277         error_setg(errp, "Snapshot '%s' does not exist in one or more devices",
3278                    name);
3279         return false;
3280     }
3281 
3282     bs_vm_state = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
3283     if (!bs_vm_state) {
3284         return false;
3285     }
3286 
3287     /* Don't even try to load empty VM states */
3288     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
3289     if (ret < 0) {
3290         error_setg(errp, "Snapshot can not be found");
3291         return false;
3292     } else if (sn.vm_state_size == 0) {
3293         error_setg(errp, "This is a disk-only snapshot. Revert to it "
3294                    " offline using qemu-img");
3295         return false;
3296     }
3297 
3298     /*
3299      * Flush the record/replay queue. Now the VM state is going
3300      * to change. Therefore we don't need to preserve its consistency
3301      */
3302     replay_flush_events();
3303 
3304     /* Flush all IO requests so they don't interfere with the new state.  */
3305     bdrv_drain_all_begin();
3306 
3307     ret = bdrv_all_goto_snapshot(name, has_devices, devices, errp);
3308     if (ret < 0) {
3309         goto err_drain;
3310     }
3311 
3312     /* restore the VM state */
3313     f = qemu_fopen_bdrv(bs_vm_state, 0);
3314     if (!f) {
3315         error_setg(errp, "Could not open VM state file");
3316         goto err_drain;
3317     }
3318 
3319     qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD);
3320     mis->from_src_file = f;
3321 
3322     if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
3323         ret = -EINVAL;
3324         goto err_drain;
3325     }
3326     ret = qemu_loadvm_state(f);
3327     migration_incoming_state_destroy();
3328 
3329     bdrv_drain_all_end();
3330 
3331     if (ret < 0) {
3332         error_setg(errp, "Error %d while loading VM state", ret);
3333         return false;
3334     }
3335 
3336     return true;
3337 
3338 err_drain:
3339     bdrv_drain_all_end();
3340     return false;
3341 }
3342 
3343 void load_snapshot_resume(RunState state)
3344 {
3345     vm_resume(state);
3346     if (state == RUN_STATE_RUNNING && runstate_get() == RUN_STATE_SUSPENDED) {
3347         qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, &error_abort);
3348     }
3349 }
3350 
3351 bool delete_snapshot(const char *name, bool has_devices,
3352                      strList *devices, Error **errp)
3353 {
3354     if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3355         return false;
3356     }
3357 
3358     if (bdrv_all_delete_snapshot(name, has_devices, devices, errp) < 0) {
3359         return false;
3360     }
3361 
3362     return true;
3363 }
3364 
3365 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
3366 {
3367     qemu_ram_set_idstr(mr->ram_block,
3368                        memory_region_name(mr), dev);
3369     qemu_ram_set_migratable(mr->ram_block);
3370 }
3371 
3372 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
3373 {
3374     qemu_ram_unset_idstr(mr->ram_block);
3375     qemu_ram_unset_migratable(mr->ram_block);
3376 }
3377 
3378 void vmstate_register_ram_global(MemoryRegion *mr)
3379 {
3380     vmstate_register_ram(mr, NULL);
3381 }
3382 
3383 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
3384 {
3385     /* check needed if --only-migratable is specified */
3386     if (!only_migratable) {
3387         return true;
3388     }
3389 
3390     return !(vmsd && vmsd->unmigratable);
3391 }
3392 
3393 typedef struct SnapshotJob {
3394     Job common;
3395     char *tag;
3396     char *vmstate;
3397     strList *devices;
3398     Coroutine *co;
3399     Error **errp;
3400     bool ret;
3401 } SnapshotJob;
3402 
3403 static void qmp_snapshot_job_free(SnapshotJob *s)
3404 {
3405     g_free(s->tag);
3406     g_free(s->vmstate);
3407     qapi_free_strList(s->devices);
3408 }
3409 
3410 
3411 static void snapshot_load_job_bh(void *opaque)
3412 {
3413     Job *job = opaque;
3414     SnapshotJob *s = container_of(job, SnapshotJob, common);
3415     RunState orig_state = runstate_get();
3416 
3417     job_progress_set_remaining(&s->common, 1);
3418 
3419     vm_stop(RUN_STATE_RESTORE_VM);
3420 
3421     s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp);
3422     if (s->ret) {
3423         load_snapshot_resume(orig_state);
3424     }
3425 
3426     job_progress_update(&s->common, 1);
3427 
3428     qmp_snapshot_job_free(s);
3429     aio_co_wake(s->co);
3430 }
3431 
3432 static void snapshot_save_job_bh(void *opaque)
3433 {
3434     Job *job = opaque;
3435     SnapshotJob *s = container_of(job, SnapshotJob, common);
3436 
3437     job_progress_set_remaining(&s->common, 1);
3438     s->ret = save_snapshot(s->tag, false, s->vmstate,
3439                            true, s->devices, s->errp);
3440     job_progress_update(&s->common, 1);
3441 
3442     qmp_snapshot_job_free(s);
3443     aio_co_wake(s->co);
3444 }
3445 
3446 static void snapshot_delete_job_bh(void *opaque)
3447 {
3448     Job *job = opaque;
3449     SnapshotJob *s = container_of(job, SnapshotJob, common);
3450 
3451     job_progress_set_remaining(&s->common, 1);
3452     s->ret = delete_snapshot(s->tag, true, s->devices, s->errp);
3453     job_progress_update(&s->common, 1);
3454 
3455     qmp_snapshot_job_free(s);
3456     aio_co_wake(s->co);
3457 }
3458 
3459 static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp)
3460 {
3461     SnapshotJob *s = container_of(job, SnapshotJob, common);
3462     s->errp = errp;
3463     s->co = qemu_coroutine_self();
3464     aio_bh_schedule_oneshot(qemu_get_aio_context(),
3465                             snapshot_save_job_bh, job);
3466     qemu_coroutine_yield();
3467     return s->ret ? 0 : -1;
3468 }
3469 
3470 static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp)
3471 {
3472     SnapshotJob *s = container_of(job, SnapshotJob, common);
3473     s->errp = errp;
3474     s->co = qemu_coroutine_self();
3475     aio_bh_schedule_oneshot(qemu_get_aio_context(),
3476                             snapshot_load_job_bh, job);
3477     qemu_coroutine_yield();
3478     return s->ret ? 0 : -1;
3479 }
3480 
3481 static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp)
3482 {
3483     SnapshotJob *s = container_of(job, SnapshotJob, common);
3484     s->errp = errp;
3485     s->co = qemu_coroutine_self();
3486     aio_bh_schedule_oneshot(qemu_get_aio_context(),
3487                             snapshot_delete_job_bh, job);
3488     qemu_coroutine_yield();
3489     return s->ret ? 0 : -1;
3490 }
3491 
3492 
3493 static const JobDriver snapshot_load_job_driver = {
3494     .instance_size = sizeof(SnapshotJob),
3495     .job_type      = JOB_TYPE_SNAPSHOT_LOAD,
3496     .run           = snapshot_load_job_run,
3497 };
3498 
3499 static const JobDriver snapshot_save_job_driver = {
3500     .instance_size = sizeof(SnapshotJob),
3501     .job_type      = JOB_TYPE_SNAPSHOT_SAVE,
3502     .run           = snapshot_save_job_run,
3503 };
3504 
3505 static const JobDriver snapshot_delete_job_driver = {
3506     .instance_size = sizeof(SnapshotJob),
3507     .job_type      = JOB_TYPE_SNAPSHOT_DELETE,
3508     .run           = snapshot_delete_job_run,
3509 };
3510 
3511 
3512 void qmp_snapshot_save(const char *job_id,
3513                        const char *tag,
3514                        const char *vmstate,
3515                        strList *devices,
3516                        Error **errp)
3517 {
3518     SnapshotJob *s;
3519 
3520     s = job_create(job_id, &snapshot_save_job_driver, NULL,
3521                    qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3522                    NULL, NULL, errp);
3523     if (!s) {
3524         return;
3525     }
3526 
3527     s->tag = g_strdup(tag);
3528     s->vmstate = g_strdup(vmstate);
3529     s->devices = QAPI_CLONE(strList, devices);
3530 
3531     job_start(&s->common);
3532 }
3533 
3534 void qmp_snapshot_load(const char *job_id,
3535                        const char *tag,
3536                        const char *vmstate,
3537                        strList *devices,
3538                        Error **errp)
3539 {
3540     SnapshotJob *s;
3541 
3542     s = job_create(job_id, &snapshot_load_job_driver, NULL,
3543                    qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3544                    NULL, NULL, errp);
3545     if (!s) {
3546         return;
3547     }
3548 
3549     s->tag = g_strdup(tag);
3550     s->vmstate = g_strdup(vmstate);
3551     s->devices = QAPI_CLONE(strList, devices);
3552 
3553     job_start(&s->common);
3554 }
3555 
3556 void qmp_snapshot_delete(const char *job_id,
3557                          const char *tag,
3558                          strList *devices,
3559                          Error **errp)
3560 {
3561     SnapshotJob *s;
3562 
3563     s = job_create(job_id, &snapshot_delete_job_driver, NULL,
3564                    qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3565                    NULL, NULL, errp);
3566     if (!s) {
3567         return;
3568     }
3569 
3570     s->tag = g_strdup(tag);
3571     s->devices = QAPI_CLONE(strList, devices);
3572 
3573     job_start(&s->common);
3574 }
3575