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