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