xref: /openbmc/qemu/migration/savevm.c (revision c6a2225a)
1 /*
2  * QEMU System Emulator
3  *
4  * Copyright (c) 2003-2008 Fabrice Bellard
5  * Copyright (c) 2009-2015 Red Hat Inc
6  *
7  * Authors:
8  *  Juan Quintela <quintela@redhat.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a copy
11  * of this software and associated documentation files (the "Software"), to deal
12  * in the Software without restriction, including without limitation the rights
13  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14  * copies of the Software, and to permit persons to whom the Software is
15  * furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included in
18  * all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26  * THE SOFTWARE.
27  */
28 
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "hw/xen/xen.h"
32 #include "net/net.h"
33 #include "migration.h"
34 #include "migration/snapshot.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "ram.h"
40 #include "qemu-file-channel.h"
41 #include "qemu-file.h"
42 #include "savevm.h"
43 #include "postcopy-ram.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-migration.h"
46 #include "qapi/qapi-commands-misc.h"
47 #include "qapi/qmp/qerror.h"
48 #include "qemu/error-report.h"
49 #include "sysemu/cpus.h"
50 #include "exec/memory.h"
51 #include "exec/target_page.h"
52 #include "trace.h"
53 #include "qemu/iov.h"
54 #include "qemu/main-loop.h"
55 #include "block/snapshot.h"
56 #include "qemu/cutils.h"
57 #include "io/channel-buffer.h"
58 #include "io/channel-file.h"
59 #include "sysemu/replay.h"
60 #include "sysemu/runstate.h"
61 #include "sysemu/sysemu.h"
62 #include "qjson.h"
63 #include "migration/colo.h"
64 #include "qemu/bitmap.h"
65 #include "net/announce.h"
66 
67 const unsigned int postcopy_ram_discard_version = 0;
68 
69 /* Subcommands for QEMU_VM_COMMAND */
70 enum qemu_vm_cmd {
71     MIG_CMD_INVALID = 0,   /* Must be 0 */
72     MIG_CMD_OPEN_RETURN_PATH,  /* Tell the dest to open the Return path */
73     MIG_CMD_PING,              /* Request a PONG on the RP */
74 
75     MIG_CMD_POSTCOPY_ADVISE,       /* Prior to any page transfers, just
76                                       warn we might want to do PC */
77     MIG_CMD_POSTCOPY_LISTEN,       /* Start listening for incoming
78                                       pages as it's running. */
79     MIG_CMD_POSTCOPY_RUN,          /* Start execution */
80 
81     MIG_CMD_POSTCOPY_RAM_DISCARD,  /* A list of pages to discard that
82                                       were previously sent during
83                                       precopy but are dirty. */
84     MIG_CMD_PACKAGED,          /* Send a wrapped stream within this stream */
85     MIG_CMD_ENABLE_COLO,       /* Enable COLO */
86     MIG_CMD_POSTCOPY_RESUME,   /* resume postcopy on dest */
87     MIG_CMD_RECV_BITMAP,       /* Request for recved bitmap on dst */
88     MIG_CMD_MAX
89 };
90 
91 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
92 static struct mig_cmd_args {
93     ssize_t     len; /* -1 = variable */
94     const char *name;
95 } mig_cmd_args[] = {
96     [MIG_CMD_INVALID]          = { .len = -1, .name = "INVALID" },
97     [MIG_CMD_OPEN_RETURN_PATH] = { .len =  0, .name = "OPEN_RETURN_PATH" },
98     [MIG_CMD_PING]             = { .len = sizeof(uint32_t), .name = "PING" },
99     [MIG_CMD_POSTCOPY_ADVISE]  = { .len = -1, .name = "POSTCOPY_ADVISE" },
100     [MIG_CMD_POSTCOPY_LISTEN]  = { .len =  0, .name = "POSTCOPY_LISTEN" },
101     [MIG_CMD_POSTCOPY_RUN]     = { .len =  0, .name = "POSTCOPY_RUN" },
102     [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
103                                    .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
104     [MIG_CMD_POSTCOPY_RESUME]  = { .len =  0, .name = "POSTCOPY_RESUME" },
105     [MIG_CMD_PACKAGED]         = { .len =  4, .name = "PACKAGED" },
106     [MIG_CMD_RECV_BITMAP]      = { .len = -1, .name = "RECV_BITMAP" },
107     [MIG_CMD_MAX]              = { .len = -1, .name = "MAX" },
108 };
109 
110 /* Note for MIG_CMD_POSTCOPY_ADVISE:
111  * The format of arguments is depending on postcopy mode:
112  * - postcopy RAM only
113  *   uint64_t host page size
114  *   uint64_t taget page size
115  *
116  * - postcopy RAM and postcopy dirty bitmaps
117  *   format is the same as for postcopy RAM only
118  *
119  * - postcopy dirty bitmaps only
120  *   Nothing. Command length field is 0.
121  *
122  * Be careful: adding a new postcopy entity with some other parameters should
123  * not break format self-description ability. Good way is to introduce some
124  * generic extendable format with an exception for two old entities.
125  */
126 
127 /***********************************************************/
128 /* savevm/loadvm support */
129 
130 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
131                                    int64_t pos, Error **errp)
132 {
133     int ret;
134     QEMUIOVector qiov;
135 
136     qemu_iovec_init_external(&qiov, iov, iovcnt);
137     ret = bdrv_writev_vmstate(opaque, &qiov, pos);
138     if (ret < 0) {
139         return ret;
140     }
141 
142     return qiov.size;
143 }
144 
145 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
146                                 size_t size, Error **errp)
147 {
148     return bdrv_load_vmstate(opaque, buf, pos, size);
149 }
150 
151 static int bdrv_fclose(void *opaque, Error **errp)
152 {
153     return bdrv_flush(opaque);
154 }
155 
156 static const QEMUFileOps bdrv_read_ops = {
157     .get_buffer = block_get_buffer,
158     .close =      bdrv_fclose
159 };
160 
161 static const QEMUFileOps bdrv_write_ops = {
162     .writev_buffer  = block_writev_buffer,
163     .close          = bdrv_fclose
164 };
165 
166 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
167 {
168     if (is_writable) {
169         return qemu_fopen_ops(bs, &bdrv_write_ops);
170     }
171     return qemu_fopen_ops(bs, &bdrv_read_ops);
172 }
173 
174 
175 /* QEMUFile timer support.
176  * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
177  */
178 
179 void timer_put(QEMUFile *f, QEMUTimer *ts)
180 {
181     uint64_t expire_time;
182 
183     expire_time = timer_expire_time_ns(ts);
184     qemu_put_be64(f, expire_time);
185 }
186 
187 void timer_get(QEMUFile *f, QEMUTimer *ts)
188 {
189     uint64_t expire_time;
190 
191     expire_time = qemu_get_be64(f);
192     if (expire_time != -1) {
193         timer_mod_ns(ts, expire_time);
194     } else {
195         timer_del(ts);
196     }
197 }
198 
199 
200 /* VMState timer support.
201  * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
202  */
203 
204 static int get_timer(QEMUFile *f, void *pv, size_t size,
205                      const VMStateField *field)
206 {
207     QEMUTimer *v = pv;
208     timer_get(f, v);
209     return 0;
210 }
211 
212 static int put_timer(QEMUFile *f, void *pv, size_t size,
213                      const VMStateField *field, QJSON *vmdesc)
214 {
215     QEMUTimer *v = pv;
216     timer_put(f, v);
217 
218     return 0;
219 }
220 
221 const VMStateInfo vmstate_info_timer = {
222     .name = "timer",
223     .get  = get_timer,
224     .put  = put_timer,
225 };
226 
227 
228 typedef struct CompatEntry {
229     char idstr[256];
230     int instance_id;
231 } CompatEntry;
232 
233 typedef struct SaveStateEntry {
234     QTAILQ_ENTRY(SaveStateEntry) entry;
235     char idstr[256];
236     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 static
1254 int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
1255 {
1256     SaveStateEntry *se;
1257     int ret;
1258 
1259     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1260         if (!se->ops ||
1261             (in_postcopy && se->ops->has_postcopy &&
1262              se->ops->has_postcopy(se->opaque)) ||
1263             !se->ops->save_live_complete_precopy) {
1264             continue;
1265         }
1266 
1267         if (se->ops && se->ops->is_active) {
1268             if (!se->ops->is_active(se->opaque)) {
1269                 continue;
1270             }
1271         }
1272         trace_savevm_section_start(se->idstr, se->section_id);
1273 
1274         save_section_header(f, se, QEMU_VM_SECTION_END);
1275 
1276         ret = se->ops->save_live_complete_precopy(f, se->opaque);
1277         trace_savevm_section_end(se->idstr, se->section_id, ret);
1278         save_section_footer(f, se);
1279         if (ret < 0) {
1280             qemu_file_set_error(f, ret);
1281             return -1;
1282         }
1283     }
1284 
1285     return 0;
1286 }
1287 
1288 static
1289 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1290                                                     bool in_postcopy,
1291                                                     bool inactivate_disks)
1292 {
1293     QJSON *vmdesc;
1294     int vmdesc_len;
1295     SaveStateEntry *se;
1296     int ret;
1297 
1298     vmdesc = qjson_new();
1299     json_prop_int(vmdesc, "page_size", qemu_target_page_size());
1300     json_start_array(vmdesc, "devices");
1301     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1302 
1303         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1304             continue;
1305         }
1306         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1307             trace_savevm_section_skip(se->idstr, se->section_id);
1308             continue;
1309         }
1310 
1311         trace_savevm_section_start(se->idstr, se->section_id);
1312 
1313         json_start_object(vmdesc, NULL);
1314         json_prop_str(vmdesc, "name", se->idstr);
1315         json_prop_int(vmdesc, "instance_id", se->instance_id);
1316 
1317         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1318         ret = vmstate_save(f, se, vmdesc);
1319         if (ret) {
1320             qemu_file_set_error(f, ret);
1321             return ret;
1322         }
1323         trace_savevm_section_end(se->idstr, se->section_id, 0);
1324         save_section_footer(f, se);
1325 
1326         json_end_object(vmdesc);
1327     }
1328 
1329     if (inactivate_disks) {
1330         /* Inactivate before sending QEMU_VM_EOF so that the
1331          * bdrv_invalidate_cache_all() on the other end won't fail. */
1332         ret = bdrv_inactivate_all();
1333         if (ret) {
1334             error_report("%s: bdrv_inactivate_all() failed (%d)",
1335                          __func__, ret);
1336             qemu_file_set_error(f, ret);
1337             return ret;
1338         }
1339     }
1340     if (!in_postcopy) {
1341         /* Postcopy stream will still be going */
1342         qemu_put_byte(f, QEMU_VM_EOF);
1343     }
1344 
1345     json_end_array(vmdesc);
1346     qjson_finish(vmdesc);
1347     vmdesc_len = strlen(qjson_get_str(vmdesc));
1348 
1349     if (should_send_vmdesc()) {
1350         qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1351         qemu_put_be32(f, vmdesc_len);
1352         qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1353     }
1354     qjson_destroy(vmdesc);
1355 
1356     return 0;
1357 }
1358 
1359 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1360                                        bool inactivate_disks)
1361 {
1362     int ret;
1363     Error *local_err = NULL;
1364     bool in_postcopy = migration_in_postcopy();
1365 
1366     if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1367         error_report_err(local_err);
1368     }
1369 
1370     trace_savevm_state_complete_precopy();
1371 
1372     cpu_synchronize_all_states();
1373 
1374     if (!in_postcopy || iterable_only) {
1375         ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1376         if (ret) {
1377             return ret;
1378         }
1379     }
1380 
1381     if (iterable_only) {
1382         goto flush;
1383     }
1384 
1385     ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1386                                                           inactivate_disks);
1387     if (ret) {
1388         return ret;
1389     }
1390 
1391 flush:
1392     qemu_fflush(f);
1393     return 0;
1394 }
1395 
1396 /* Give an estimate of the amount left to be transferred,
1397  * the result is split into the amount for units that can and
1398  * for units that can't do postcopy.
1399  */
1400 void qemu_savevm_state_pending(QEMUFile *f, uint64_t threshold_size,
1401                                uint64_t *res_precopy_only,
1402                                uint64_t *res_compatible,
1403                                uint64_t *res_postcopy_only)
1404 {
1405     SaveStateEntry *se;
1406 
1407     *res_precopy_only = 0;
1408     *res_compatible = 0;
1409     *res_postcopy_only = 0;
1410 
1411 
1412     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1413         if (!se->ops || !se->ops->save_live_pending) {
1414             continue;
1415         }
1416         if (se->ops && se->ops->is_active) {
1417             if (!se->ops->is_active(se->opaque)) {
1418                 continue;
1419             }
1420         }
1421         se->ops->save_live_pending(f, se->opaque, threshold_size,
1422                                    res_precopy_only, res_compatible,
1423                                    res_postcopy_only);
1424     }
1425 }
1426 
1427 void qemu_savevm_state_cleanup(void)
1428 {
1429     SaveStateEntry *se;
1430     Error *local_err = NULL;
1431 
1432     if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1433         error_report_err(local_err);
1434     }
1435 
1436     trace_savevm_state_cleanup();
1437     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1438         if (se->ops && se->ops->save_cleanup) {
1439             se->ops->save_cleanup(se->opaque);
1440         }
1441     }
1442 }
1443 
1444 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1445 {
1446     int ret;
1447     MigrationState *ms = migrate_get_current();
1448     MigrationStatus status;
1449 
1450     if (migration_is_setup_or_active(ms->state) ||
1451         ms->state == MIGRATION_STATUS_CANCELLING ||
1452         ms->state == MIGRATION_STATUS_COLO) {
1453         error_setg(errp, QERR_MIGRATION_ACTIVE);
1454         return -EINVAL;
1455     }
1456 
1457     if (migrate_use_block()) {
1458         error_setg(errp, "Block migration and snapshots are incompatible");
1459         return -EINVAL;
1460     }
1461 
1462     migrate_init(ms);
1463     memset(&ram_counters, 0, sizeof(ram_counters));
1464     ms->to_dst_file = f;
1465 
1466     qemu_mutex_unlock_iothread();
1467     qemu_savevm_state_header(f);
1468     qemu_savevm_state_setup(f);
1469     qemu_mutex_lock_iothread();
1470 
1471     while (qemu_file_get_error(f) == 0) {
1472         if (qemu_savevm_state_iterate(f, false) > 0) {
1473             break;
1474         }
1475     }
1476 
1477     ret = qemu_file_get_error(f);
1478     if (ret == 0) {
1479         qemu_savevm_state_complete_precopy(f, false, false);
1480         ret = qemu_file_get_error(f);
1481     }
1482     qemu_savevm_state_cleanup();
1483     if (ret != 0) {
1484         error_setg_errno(errp, -ret, "Error while writing VM state");
1485     }
1486 
1487     if (ret != 0) {
1488         status = MIGRATION_STATUS_FAILED;
1489     } else {
1490         status = MIGRATION_STATUS_COMPLETED;
1491     }
1492     migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1493 
1494     /* f is outer parameter, it should not stay in global migration state after
1495      * this function finished */
1496     ms->to_dst_file = NULL;
1497 
1498     return ret;
1499 }
1500 
1501 void qemu_savevm_live_state(QEMUFile *f)
1502 {
1503     /* save QEMU_VM_SECTION_END section */
1504     qemu_savevm_state_complete_precopy(f, true, false);
1505     qemu_put_byte(f, QEMU_VM_EOF);
1506 }
1507 
1508 int qemu_save_device_state(QEMUFile *f)
1509 {
1510     SaveStateEntry *se;
1511 
1512     if (!migration_in_colo_state()) {
1513         qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1514         qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1515     }
1516     cpu_synchronize_all_states();
1517 
1518     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1519         int ret;
1520 
1521         if (se->is_ram) {
1522             continue;
1523         }
1524         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1525             continue;
1526         }
1527         if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1528             continue;
1529         }
1530 
1531         save_section_header(f, se, QEMU_VM_SECTION_FULL);
1532 
1533         ret = vmstate_save(f, se, NULL);
1534         if (ret) {
1535             return ret;
1536         }
1537 
1538         save_section_footer(f, se);
1539     }
1540 
1541     qemu_put_byte(f, QEMU_VM_EOF);
1542 
1543     return qemu_file_get_error(f);
1544 }
1545 
1546 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1547 {
1548     SaveStateEntry *se;
1549 
1550     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1551         if (!strcmp(se->idstr, idstr) &&
1552             (instance_id == se->instance_id ||
1553              instance_id == se->alias_id))
1554             return se;
1555         /* Migrating from an older version? */
1556         if (strstr(se->idstr, idstr) && se->compat) {
1557             if (!strcmp(se->compat->idstr, idstr) &&
1558                 (instance_id == se->compat->instance_id ||
1559                  instance_id == se->alias_id))
1560                 return se;
1561         }
1562     }
1563     return NULL;
1564 }
1565 
1566 enum LoadVMExitCodes {
1567     /* Allow a command to quit all layers of nested loadvm loops */
1568     LOADVM_QUIT     =  1,
1569 };
1570 
1571 /* ------ incoming postcopy messages ------ */
1572 /* 'advise' arrives before any transfers just to tell us that a postcopy
1573  * *might* happen - it might be skipped if precopy transferred everything
1574  * quickly.
1575  */
1576 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1577                                          uint16_t len)
1578 {
1579     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1580     uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1581     Error *local_err = NULL;
1582 
1583     trace_loadvm_postcopy_handle_advise();
1584     if (ps != POSTCOPY_INCOMING_NONE) {
1585         error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1586         return -1;
1587     }
1588 
1589     switch (len) {
1590     case 0:
1591         if (migrate_postcopy_ram()) {
1592             error_report("RAM postcopy is enabled but have 0 byte advise");
1593             return -EINVAL;
1594         }
1595         return 0;
1596     case 8 + 8:
1597         if (!migrate_postcopy_ram()) {
1598             error_report("RAM postcopy is disabled but have 16 byte advise");
1599             return -EINVAL;
1600         }
1601         break;
1602     default:
1603         error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1604         return -EINVAL;
1605     }
1606 
1607     if (!postcopy_ram_supported_by_host(mis)) {
1608         postcopy_state_set(POSTCOPY_INCOMING_NONE);
1609         return -1;
1610     }
1611 
1612     remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1613     local_pagesize_summary = ram_pagesize_summary();
1614 
1615     if (remote_pagesize_summary != local_pagesize_summary)  {
1616         /*
1617          * This detects two potential causes of mismatch:
1618          *   a) A mismatch in host page sizes
1619          *      Some combinations of mismatch are probably possible but it gets
1620          *      a bit more complicated.  In particular we need to place whole
1621          *      host pages on the dest at once, and we need to ensure that we
1622          *      handle dirtying to make sure we never end up sending part of
1623          *      a hostpage on it's own.
1624          *   b) The use of different huge page sizes on source/destination
1625          *      a more fine grain test is performed during RAM block migration
1626          *      but this test here causes a nice early clear failure, and
1627          *      also fails when passed to an older qemu that doesn't
1628          *      do huge pages.
1629          */
1630         error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1631                                                              " d=%" PRIx64 ")",
1632                      remote_pagesize_summary, local_pagesize_summary);
1633         return -1;
1634     }
1635 
1636     remote_tps = qemu_get_be64(mis->from_src_file);
1637     if (remote_tps != qemu_target_page_size()) {
1638         /*
1639          * Again, some differences could be dealt with, but for now keep it
1640          * simple.
1641          */
1642         error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1643                      (int)remote_tps, qemu_target_page_size());
1644         return -1;
1645     }
1646 
1647     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1648         error_report_err(local_err);
1649         return -1;
1650     }
1651 
1652     if (ram_postcopy_incoming_init(mis)) {
1653         return -1;
1654     }
1655 
1656     return 0;
1657 }
1658 
1659 /* After postcopy we will be told to throw some pages away since they're
1660  * dirty and will have to be demand fetched.  Must happen before CPU is
1661  * started.
1662  * There can be 0..many of these messages, each encoding multiple pages.
1663  */
1664 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1665                                               uint16_t len)
1666 {
1667     int tmp;
1668     char ramid[256];
1669     PostcopyState ps = postcopy_state_get();
1670 
1671     trace_loadvm_postcopy_ram_handle_discard();
1672 
1673     switch (ps) {
1674     case POSTCOPY_INCOMING_ADVISE:
1675         /* 1st discard */
1676         tmp = postcopy_ram_prepare_discard(mis);
1677         if (tmp) {
1678             return tmp;
1679         }
1680         break;
1681 
1682     case POSTCOPY_INCOMING_DISCARD:
1683         /* Expected state */
1684         break;
1685 
1686     default:
1687         error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1688                      ps);
1689         return -1;
1690     }
1691     /* We're expecting a
1692      *    Version (0)
1693      *    a RAM ID string (length byte, name, 0 term)
1694      *    then at least 1 16 byte chunk
1695     */
1696     if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1697         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1698         return -1;
1699     }
1700 
1701     tmp = qemu_get_byte(mis->from_src_file);
1702     if (tmp != postcopy_ram_discard_version) {
1703         error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1704         return -1;
1705     }
1706 
1707     if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1708         error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1709         return -1;
1710     }
1711     tmp = qemu_get_byte(mis->from_src_file);
1712     if (tmp != 0) {
1713         error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1714         return -1;
1715     }
1716 
1717     len -= 3 + strlen(ramid);
1718     if (len % 16) {
1719         error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1720         return -1;
1721     }
1722     trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1723     while (len) {
1724         uint64_t start_addr, block_length;
1725         start_addr = qemu_get_be64(mis->from_src_file);
1726         block_length = qemu_get_be64(mis->from_src_file);
1727 
1728         len -= 16;
1729         int ret = ram_discard_range(ramid, start_addr, block_length);
1730         if (ret) {
1731             return ret;
1732         }
1733     }
1734     trace_loadvm_postcopy_ram_handle_discard_end();
1735 
1736     return 0;
1737 }
1738 
1739 /*
1740  * Triggered by a postcopy_listen command; this thread takes over reading
1741  * the input stream, leaving the main thread free to carry on loading the rest
1742  * of the device state (from RAM).
1743  * (TODO:This could do with being in a postcopy file - but there again it's
1744  * just another input loop, not that postcopy specific)
1745  */
1746 static void *postcopy_ram_listen_thread(void *opaque)
1747 {
1748     MigrationIncomingState *mis = migration_incoming_get_current();
1749     QEMUFile *f = mis->from_src_file;
1750     int load_res;
1751 
1752     migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1753                                    MIGRATION_STATUS_POSTCOPY_ACTIVE);
1754     qemu_sem_post(&mis->listen_thread_sem);
1755     trace_postcopy_ram_listen_thread_start();
1756 
1757     rcu_register_thread();
1758     /*
1759      * Because we're a thread and not a coroutine we can't yield
1760      * in qemu_file, and thus we must be blocking now.
1761      */
1762     qemu_file_set_blocking(f, true);
1763     load_res = qemu_loadvm_state_main(f, mis);
1764 
1765     /*
1766      * This is tricky, but, mis->from_src_file can change after it
1767      * returns, when postcopy recovery happened. In the future, we may
1768      * want a wrapper for the QEMUFile handle.
1769      */
1770     f = mis->from_src_file;
1771 
1772     /* And non-blocking again so we don't block in any cleanup */
1773     qemu_file_set_blocking(f, false);
1774 
1775     trace_postcopy_ram_listen_thread_exit();
1776     if (load_res < 0) {
1777         error_report("%s: loadvm failed: %d", __func__, load_res);
1778         qemu_file_set_error(f, load_res);
1779         migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1780                                        MIGRATION_STATUS_FAILED);
1781     } else {
1782         /*
1783          * This looks good, but it's possible that the device loading in the
1784          * main thread hasn't finished yet, and so we might not be in 'RUN'
1785          * state yet; wait for the end of the main thread.
1786          */
1787         qemu_event_wait(&mis->main_thread_load_event);
1788     }
1789     postcopy_ram_incoming_cleanup(mis);
1790 
1791     if (load_res < 0) {
1792         /*
1793          * If something went wrong then we have a bad state so exit;
1794          * depending how far we got it might be possible at this point
1795          * to leave the guest running and fire MCEs for pages that never
1796          * arrived as a desperate recovery step.
1797          */
1798         rcu_unregister_thread();
1799         exit(EXIT_FAILURE);
1800     }
1801 
1802     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1803                                    MIGRATION_STATUS_COMPLETED);
1804     /*
1805      * If everything has worked fine, then the main thread has waited
1806      * for us to start, and we're the last use of the mis.
1807      * (If something broke then qemu will have to exit anyway since it's
1808      * got a bad migration state).
1809      */
1810     migration_incoming_state_destroy();
1811     qemu_loadvm_state_cleanup();
1812 
1813     rcu_unregister_thread();
1814     mis->have_listen_thread = false;
1815     return NULL;
1816 }
1817 
1818 /* After this message we must be able to immediately receive postcopy data */
1819 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1820 {
1821     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1822     trace_loadvm_postcopy_handle_listen();
1823     Error *local_err = NULL;
1824 
1825     if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1826         error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1827         return -1;
1828     }
1829     if (ps == POSTCOPY_INCOMING_ADVISE) {
1830         /*
1831          * A rare case, we entered listen without having to do any discards,
1832          * so do the setup that's normally done at the time of the 1st discard.
1833          */
1834         if (migrate_postcopy_ram()) {
1835             postcopy_ram_prepare_discard(mis);
1836         }
1837     }
1838 
1839     /*
1840      * Sensitise RAM - can now generate requests for blocks that don't exist
1841      * However, at this point the CPU shouldn't be running, and the IO
1842      * shouldn't be doing anything yet so don't actually expect requests
1843      */
1844     if (migrate_postcopy_ram()) {
1845         if (postcopy_ram_enable_notify(mis)) {
1846             postcopy_ram_incoming_cleanup(mis);
1847             return -1;
1848         }
1849     }
1850 
1851     if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
1852         error_report_err(local_err);
1853         return -1;
1854     }
1855 
1856     if (mis->have_listen_thread) {
1857         error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1858         return -1;
1859     }
1860 
1861     mis->have_listen_thread = true;
1862     /* Start up the listening thread and wait for it to signal ready */
1863     qemu_sem_init(&mis->listen_thread_sem, 0);
1864     qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1865                        postcopy_ram_listen_thread, NULL,
1866                        QEMU_THREAD_DETACHED);
1867     qemu_sem_wait(&mis->listen_thread_sem);
1868     qemu_sem_destroy(&mis->listen_thread_sem);
1869 
1870     return 0;
1871 }
1872 
1873 static void loadvm_postcopy_handle_run_bh(void *opaque)
1874 {
1875     Error *local_err = NULL;
1876     MigrationIncomingState *mis = opaque;
1877 
1878     /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1879      * in migration.c
1880      */
1881     cpu_synchronize_all_post_init();
1882 
1883     qemu_announce_self(&mis->announce_timer, migrate_announce_params());
1884 
1885     /* Make sure all file formats flush their mutable metadata.
1886      * If we get an error here, just don't restart the VM yet. */
1887     bdrv_invalidate_cache_all(&local_err);
1888     if (local_err) {
1889         error_report_err(local_err);
1890         local_err = NULL;
1891         autostart = false;
1892     }
1893 
1894     trace_loadvm_postcopy_handle_run_cpu_sync();
1895 
1896     trace_loadvm_postcopy_handle_run_vmstart();
1897 
1898     dirty_bitmap_mig_before_vm_start();
1899 
1900     if (autostart) {
1901         /* Hold onto your hats, starting the CPU */
1902         vm_start();
1903     } else {
1904         /* leave it paused and let management decide when to start the CPU */
1905         runstate_set(RUN_STATE_PAUSED);
1906     }
1907 
1908     qemu_bh_delete(mis->bh);
1909 }
1910 
1911 /* After all discards we can start running and asking for pages */
1912 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1913 {
1914     PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1915 
1916     trace_loadvm_postcopy_handle_run();
1917     if (ps != POSTCOPY_INCOMING_LISTENING) {
1918         error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1919         return -1;
1920     }
1921 
1922     mis->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, mis);
1923     qemu_bh_schedule(mis->bh);
1924 
1925     /* We need to finish reading the stream from the package
1926      * and also stop reading anything more from the stream that loaded the
1927      * package (since it's now being read by the listener thread).
1928      * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1929      */
1930     return LOADVM_QUIT;
1931 }
1932 
1933 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
1934 {
1935     if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
1936         error_report("%s: illegal resume received", __func__);
1937         /* Don't fail the load, only for this. */
1938         return 0;
1939     }
1940 
1941     /*
1942      * This means source VM is ready to resume the postcopy migration.
1943      * It's time to switch state and release the fault thread to
1944      * continue service page faults.
1945      */
1946     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
1947                       MIGRATION_STATUS_POSTCOPY_ACTIVE);
1948     qemu_sem_post(&mis->postcopy_pause_sem_fault);
1949 
1950     trace_loadvm_postcopy_handle_resume();
1951 
1952     /* Tell source that "we are ready" */
1953     migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
1954 
1955     return 0;
1956 }
1957 
1958 /**
1959  * Immediately following this command is a blob of data containing an embedded
1960  * chunk of migration stream; read it and load it.
1961  *
1962  * @mis: Incoming state
1963  * @length: Length of packaged data to read
1964  *
1965  * Returns: Negative values on error
1966  *
1967  */
1968 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1969 {
1970     int ret;
1971     size_t length;
1972     QIOChannelBuffer *bioc;
1973 
1974     length = qemu_get_be32(mis->from_src_file);
1975     trace_loadvm_handle_cmd_packaged(length);
1976 
1977     if (length > MAX_VM_CMD_PACKAGED_SIZE) {
1978         error_report("Unreasonably large packaged state: %zu", length);
1979         return -1;
1980     }
1981 
1982     bioc = qio_channel_buffer_new(length);
1983     qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
1984     ret = qemu_get_buffer(mis->from_src_file,
1985                           bioc->data,
1986                           length);
1987     if (ret != length) {
1988         object_unref(OBJECT(bioc));
1989         error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1990                      ret, length);
1991         return (ret < 0) ? ret : -EAGAIN;
1992     }
1993     bioc->usage += length;
1994     trace_loadvm_handle_cmd_packaged_received(ret);
1995 
1996     QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
1997 
1998     ret = qemu_loadvm_state_main(packf, mis);
1999     trace_loadvm_handle_cmd_packaged_main(ret);
2000     qemu_fclose(packf);
2001     object_unref(OBJECT(bioc));
2002 
2003     return ret;
2004 }
2005 
2006 /*
2007  * Handle request that source requests for recved_bitmap on
2008  * destination. Payload format:
2009  *
2010  * len (1 byte) + ramblock_name (<255 bytes)
2011  */
2012 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2013                                      uint16_t len)
2014 {
2015     QEMUFile *file = mis->from_src_file;
2016     RAMBlock *rb;
2017     char block_name[256];
2018     size_t cnt;
2019 
2020     cnt = qemu_get_counted_string(file, block_name);
2021     if (!cnt) {
2022         error_report("%s: failed to read block name", __func__);
2023         return -EINVAL;
2024     }
2025 
2026     /* Validate before using the data */
2027     if (qemu_file_get_error(file)) {
2028         return qemu_file_get_error(file);
2029     }
2030 
2031     if (len != cnt + 1) {
2032         error_report("%s: invalid payload length (%d)", __func__, len);
2033         return -EINVAL;
2034     }
2035 
2036     rb = qemu_ram_block_by_name(block_name);
2037     if (!rb) {
2038         error_report("%s: block '%s' not found", __func__, block_name);
2039         return -EINVAL;
2040     }
2041 
2042     migrate_send_rp_recv_bitmap(mis, block_name);
2043 
2044     trace_loadvm_handle_recv_bitmap(block_name);
2045 
2046     return 0;
2047 }
2048 
2049 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2050 {
2051     migration_incoming_enable_colo();
2052     return colo_init_ram_cache();
2053 }
2054 
2055 /*
2056  * Process an incoming 'QEMU_VM_COMMAND'
2057  * 0           just a normal return
2058  * LOADVM_QUIT All good, but exit the loop
2059  * <0          Error
2060  */
2061 static int loadvm_process_command(QEMUFile *f)
2062 {
2063     MigrationIncomingState *mis = migration_incoming_get_current();
2064     uint16_t cmd;
2065     uint16_t len;
2066     uint32_t tmp32;
2067 
2068     cmd = qemu_get_be16(f);
2069     len = qemu_get_be16(f);
2070 
2071     /* Check validity before continue processing of cmds */
2072     if (qemu_file_get_error(f)) {
2073         return qemu_file_get_error(f);
2074     }
2075 
2076     trace_loadvm_process_command(cmd, len);
2077     if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2078         error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2079         return -EINVAL;
2080     }
2081 
2082     if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2083         error_report("%s received with bad length - expecting %zu, got %d",
2084                      mig_cmd_args[cmd].name,
2085                      (size_t)mig_cmd_args[cmd].len, len);
2086         return -ERANGE;
2087     }
2088 
2089     switch (cmd) {
2090     case MIG_CMD_OPEN_RETURN_PATH:
2091         if (mis->to_src_file) {
2092             error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2093             /* Not really a problem, so don't give up */
2094             return 0;
2095         }
2096         mis->to_src_file = qemu_file_get_return_path(f);
2097         if (!mis->to_src_file) {
2098             error_report("CMD_OPEN_RETURN_PATH failed");
2099             return -1;
2100         }
2101         break;
2102 
2103     case MIG_CMD_PING:
2104         tmp32 = qemu_get_be32(f);
2105         trace_loadvm_process_command_ping(tmp32);
2106         if (!mis->to_src_file) {
2107             error_report("CMD_PING (0x%x) received with no return path",
2108                          tmp32);
2109             return -1;
2110         }
2111         migrate_send_rp_pong(mis, tmp32);
2112         break;
2113 
2114     case MIG_CMD_PACKAGED:
2115         return loadvm_handle_cmd_packaged(mis);
2116 
2117     case MIG_CMD_POSTCOPY_ADVISE:
2118         return loadvm_postcopy_handle_advise(mis, len);
2119 
2120     case MIG_CMD_POSTCOPY_LISTEN:
2121         return loadvm_postcopy_handle_listen(mis);
2122 
2123     case MIG_CMD_POSTCOPY_RUN:
2124         return loadvm_postcopy_handle_run(mis);
2125 
2126     case MIG_CMD_POSTCOPY_RAM_DISCARD:
2127         return loadvm_postcopy_ram_handle_discard(mis, len);
2128 
2129     case MIG_CMD_POSTCOPY_RESUME:
2130         return loadvm_postcopy_handle_resume(mis);
2131 
2132     case MIG_CMD_RECV_BITMAP:
2133         return loadvm_handle_recv_bitmap(mis, len);
2134 
2135     case MIG_CMD_ENABLE_COLO:
2136         return loadvm_process_enable_colo(mis);
2137     }
2138 
2139     return 0;
2140 }
2141 
2142 /*
2143  * Read a footer off the wire and check that it matches the expected section
2144  *
2145  * Returns: true if the footer was good
2146  *          false if there is a problem (and calls error_report to say why)
2147  */
2148 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2149 {
2150     int ret;
2151     uint8_t read_mark;
2152     uint32_t read_section_id;
2153 
2154     if (!migrate_get_current()->send_section_footer) {
2155         /* No footer to check */
2156         return true;
2157     }
2158 
2159     read_mark = qemu_get_byte(f);
2160 
2161     ret = qemu_file_get_error(f);
2162     if (ret) {
2163         error_report("%s: Read section footer failed: %d",
2164                      __func__, ret);
2165         return false;
2166     }
2167 
2168     if (read_mark != QEMU_VM_SECTION_FOOTER) {
2169         error_report("Missing section footer for %s", se->idstr);
2170         return false;
2171     }
2172 
2173     read_section_id = qemu_get_be32(f);
2174     if (read_section_id != se->load_section_id) {
2175         error_report("Mismatched section id in footer for %s -"
2176                      " read 0x%x expected 0x%x",
2177                      se->idstr, read_section_id, se->load_section_id);
2178         return false;
2179     }
2180 
2181     /* All good */
2182     return true;
2183 }
2184 
2185 static int
2186 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
2187 {
2188     uint32_t instance_id, version_id, section_id;
2189     SaveStateEntry *se;
2190     char idstr[256];
2191     int ret;
2192 
2193     /* Read section start */
2194     section_id = qemu_get_be32(f);
2195     if (!qemu_get_counted_string(f, idstr)) {
2196         error_report("Unable to read ID string for section %u",
2197                      section_id);
2198         return -EINVAL;
2199     }
2200     instance_id = qemu_get_be32(f);
2201     version_id = qemu_get_be32(f);
2202 
2203     ret = qemu_file_get_error(f);
2204     if (ret) {
2205         error_report("%s: Failed to read instance/version ID: %d",
2206                      __func__, ret);
2207         return ret;
2208     }
2209 
2210     trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2211             instance_id, version_id);
2212     /* Find savevm section */
2213     se = find_se(idstr, instance_id);
2214     if (se == NULL) {
2215         error_report("Unknown savevm section or instance '%s' %d. "
2216                      "Make sure that your current VM setup matches your "
2217                      "saved VM setup, including any hotplugged devices",
2218                      idstr, instance_id);
2219         return -EINVAL;
2220     }
2221 
2222     /* Validate version */
2223     if (version_id > se->version_id) {
2224         error_report("savevm: unsupported version %d for '%s' v%d",
2225                      version_id, idstr, se->version_id);
2226         return -EINVAL;
2227     }
2228     se->load_version_id = version_id;
2229     se->load_section_id = section_id;
2230 
2231     /* Validate if it is a device's state */
2232     if (xen_enabled() && se->is_ram) {
2233         error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2234         return -EINVAL;
2235     }
2236 
2237     ret = vmstate_load(f, se);
2238     if (ret < 0) {
2239         error_report("error while loading state for instance 0x%x of"
2240                      " device '%s'", instance_id, idstr);
2241         return ret;
2242     }
2243     if (!check_section_footer(f, se)) {
2244         return -EINVAL;
2245     }
2246 
2247     return 0;
2248 }
2249 
2250 static int
2251 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
2252 {
2253     uint32_t section_id;
2254     SaveStateEntry *se;
2255     int ret;
2256 
2257     section_id = qemu_get_be32(f);
2258 
2259     ret = qemu_file_get_error(f);
2260     if (ret) {
2261         error_report("%s: Failed to read section ID: %d",
2262                      __func__, ret);
2263         return ret;
2264     }
2265 
2266     trace_qemu_loadvm_state_section_partend(section_id);
2267     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2268         if (se->load_section_id == section_id) {
2269             break;
2270         }
2271     }
2272     if (se == NULL) {
2273         error_report("Unknown savevm section %d", section_id);
2274         return -EINVAL;
2275     }
2276 
2277     ret = vmstate_load(f, se);
2278     if (ret < 0) {
2279         error_report("error while loading state section id %d(%s)",
2280                      section_id, se->idstr);
2281         return ret;
2282     }
2283     if (!check_section_footer(f, se)) {
2284         return -EINVAL;
2285     }
2286 
2287     return 0;
2288 }
2289 
2290 static int qemu_loadvm_state_header(QEMUFile *f)
2291 {
2292     unsigned int v;
2293     int ret;
2294 
2295     v = qemu_get_be32(f);
2296     if (v != QEMU_VM_FILE_MAGIC) {
2297         error_report("Not a migration stream");
2298         return -EINVAL;
2299     }
2300 
2301     v = qemu_get_be32(f);
2302     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2303         error_report("SaveVM v2 format is obsolete and don't work anymore");
2304         return -ENOTSUP;
2305     }
2306     if (v != QEMU_VM_FILE_VERSION) {
2307         error_report("Unsupported migration stream version");
2308         return -ENOTSUP;
2309     }
2310 
2311     if (migrate_get_current()->send_configuration) {
2312         if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2313             error_report("Configuration section missing");
2314             qemu_loadvm_state_cleanup();
2315             return -EINVAL;
2316         }
2317         ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2318 
2319         if (ret) {
2320             qemu_loadvm_state_cleanup();
2321             return ret;
2322         }
2323     }
2324     return 0;
2325 }
2326 
2327 static int qemu_loadvm_state_setup(QEMUFile *f)
2328 {
2329     SaveStateEntry *se;
2330     int ret;
2331 
2332     trace_loadvm_state_setup();
2333     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2334         if (!se->ops || !se->ops->load_setup) {
2335             continue;
2336         }
2337         if (se->ops && se->ops->is_active) {
2338             if (!se->ops->is_active(se->opaque)) {
2339                 continue;
2340             }
2341         }
2342 
2343         ret = se->ops->load_setup(f, se->opaque);
2344         if (ret < 0) {
2345             qemu_file_set_error(f, ret);
2346             error_report("Load state of device %s failed", se->idstr);
2347             return ret;
2348         }
2349     }
2350     return 0;
2351 }
2352 
2353 void qemu_loadvm_state_cleanup(void)
2354 {
2355     SaveStateEntry *se;
2356 
2357     trace_loadvm_state_cleanup();
2358     QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2359         if (se->ops && se->ops->load_cleanup) {
2360             se->ops->load_cleanup(se->opaque);
2361         }
2362     }
2363 }
2364 
2365 /* Return true if we should continue the migration, or false. */
2366 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2367 {
2368     trace_postcopy_pause_incoming();
2369 
2370     /* Clear the triggered bit to allow one recovery */
2371     mis->postcopy_recover_triggered = false;
2372 
2373     assert(mis->from_src_file);
2374     qemu_file_shutdown(mis->from_src_file);
2375     qemu_fclose(mis->from_src_file);
2376     mis->from_src_file = NULL;
2377 
2378     assert(mis->to_src_file);
2379     qemu_file_shutdown(mis->to_src_file);
2380     qemu_mutex_lock(&mis->rp_mutex);
2381     qemu_fclose(mis->to_src_file);
2382     mis->to_src_file = NULL;
2383     qemu_mutex_unlock(&mis->rp_mutex);
2384 
2385     migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2386                       MIGRATION_STATUS_POSTCOPY_PAUSED);
2387 
2388     /* Notify the fault thread for the invalidated file handle */
2389     postcopy_fault_thread_notify(mis);
2390 
2391     error_report("Detected IO failure for postcopy. "
2392                  "Migration paused.");
2393 
2394     while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2395         qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2396     }
2397 
2398     trace_postcopy_pause_incoming_continued();
2399 
2400     return true;
2401 }
2402 
2403 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2404 {
2405     uint8_t section_type;
2406     int ret = 0;
2407 
2408 retry:
2409     while (true) {
2410         section_type = qemu_get_byte(f);
2411 
2412         if (qemu_file_get_error(f)) {
2413             ret = qemu_file_get_error(f);
2414             break;
2415         }
2416 
2417         trace_qemu_loadvm_state_section(section_type);
2418         switch (section_type) {
2419         case QEMU_VM_SECTION_START:
2420         case QEMU_VM_SECTION_FULL:
2421             ret = qemu_loadvm_section_start_full(f, mis);
2422             if (ret < 0) {
2423                 goto out;
2424             }
2425             break;
2426         case QEMU_VM_SECTION_PART:
2427         case QEMU_VM_SECTION_END:
2428             ret = qemu_loadvm_section_part_end(f, mis);
2429             if (ret < 0) {
2430                 goto out;
2431             }
2432             break;
2433         case QEMU_VM_COMMAND:
2434             ret = loadvm_process_command(f);
2435             trace_qemu_loadvm_state_section_command(ret);
2436             if ((ret < 0) || (ret == LOADVM_QUIT)) {
2437                 goto out;
2438             }
2439             break;
2440         case QEMU_VM_EOF:
2441             /* This is the end of migration */
2442             goto out;
2443         default:
2444             error_report("Unknown savevm section type %d", section_type);
2445             ret = -EINVAL;
2446             goto out;
2447         }
2448     }
2449 
2450 out:
2451     if (ret < 0) {
2452         qemu_file_set_error(f, ret);
2453 
2454         /*
2455          * If we are during an active postcopy, then we pause instead
2456          * of bail out to at least keep the VM's dirty data.  Note
2457          * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2458          * during which we're still receiving device states and we
2459          * still haven't yet started the VM on destination.
2460          */
2461         if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2462             postcopy_pause_incoming(mis)) {
2463             /* Reset f to point to the newly created channel */
2464             f = mis->from_src_file;
2465             goto retry;
2466         }
2467     }
2468     return ret;
2469 }
2470 
2471 int qemu_loadvm_state(QEMUFile *f)
2472 {
2473     MigrationIncomingState *mis = migration_incoming_get_current();
2474     Error *local_err = NULL;
2475     int ret;
2476 
2477     if (qemu_savevm_state_blocked(&local_err)) {
2478         error_report_err(local_err);
2479         return -EINVAL;
2480     }
2481 
2482     ret = qemu_loadvm_state_header(f);
2483     if (ret) {
2484         return ret;
2485     }
2486 
2487     if (qemu_loadvm_state_setup(f) != 0) {
2488         return -EINVAL;
2489     }
2490 
2491     cpu_synchronize_all_pre_loadvm();
2492 
2493     ret = qemu_loadvm_state_main(f, mis);
2494     qemu_event_set(&mis->main_thread_load_event);
2495 
2496     trace_qemu_loadvm_state_post_main(ret);
2497 
2498     if (mis->have_listen_thread) {
2499         /* Listen thread still going, can't clean up yet */
2500         return ret;
2501     }
2502 
2503     if (ret == 0) {
2504         ret = qemu_file_get_error(f);
2505     }
2506 
2507     /*
2508      * Try to read in the VMDESC section as well, so that dumping tools that
2509      * intercept our migration stream have the chance to see it.
2510      */
2511 
2512     /* We've got to be careful; if we don't read the data and just shut the fd
2513      * then the sender can error if we close while it's still sending.
2514      * We also mustn't read data that isn't there; some transports (RDMA)
2515      * will stall waiting for that data when the source has already closed.
2516      */
2517     if (ret == 0 && should_send_vmdesc()) {
2518         uint8_t *buf;
2519         uint32_t size;
2520         uint8_t  section_type = qemu_get_byte(f);
2521 
2522         if (section_type != QEMU_VM_VMDESCRIPTION) {
2523             error_report("Expected vmdescription section, but got %d",
2524                          section_type);
2525             /*
2526              * It doesn't seem worth failing at this point since
2527              * we apparently have an otherwise valid VM state
2528              */
2529         } else {
2530             buf = g_malloc(0x1000);
2531             size = qemu_get_be32(f);
2532 
2533             while (size > 0) {
2534                 uint32_t read_chunk = MIN(size, 0x1000);
2535                 qemu_get_buffer(f, buf, read_chunk);
2536                 size -= read_chunk;
2537             }
2538             g_free(buf);
2539         }
2540     }
2541 
2542     qemu_loadvm_state_cleanup();
2543     cpu_synchronize_all_post_init();
2544 
2545     return ret;
2546 }
2547 
2548 int qemu_load_device_state(QEMUFile *f)
2549 {
2550     MigrationIncomingState *mis = migration_incoming_get_current();
2551     int ret;
2552 
2553     /* Load QEMU_VM_SECTION_FULL section */
2554     ret = qemu_loadvm_state_main(f, mis);
2555     if (ret < 0) {
2556         error_report("Failed to load device state: %d", ret);
2557         return ret;
2558     }
2559 
2560     cpu_synchronize_all_post_init();
2561     return 0;
2562 }
2563 
2564 int save_snapshot(const char *name, Error **errp)
2565 {
2566     BlockDriverState *bs, *bs1;
2567     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2568     int ret = -1;
2569     QEMUFile *f;
2570     int saved_vm_running;
2571     uint64_t vm_state_size;
2572     qemu_timeval tv;
2573     struct tm tm;
2574     AioContext *aio_context;
2575 
2576     if (migration_is_blocked(errp)) {
2577         return ret;
2578     }
2579 
2580     if (!replay_can_snapshot()) {
2581         error_setg(errp, "Record/replay does not allow making snapshot "
2582                    "right now. Try once more later.");
2583         return ret;
2584     }
2585 
2586     if (!bdrv_all_can_snapshot(&bs)) {
2587         error_setg(errp, "Device '%s' is writable but does not support "
2588                    "snapshots", bdrv_get_device_name(bs));
2589         return ret;
2590     }
2591 
2592     /* Delete old snapshots of the same name */
2593     if (name) {
2594         ret = bdrv_all_delete_snapshot(name, &bs1, errp);
2595         if (ret < 0) {
2596             error_prepend(errp, "Error while deleting snapshot on device "
2597                           "'%s': ", bdrv_get_device_name(bs1));
2598             return ret;
2599         }
2600     }
2601 
2602     bs = bdrv_all_find_vmstate_bs();
2603     if (bs == NULL) {
2604         error_setg(errp, "No block device can accept snapshots");
2605         return ret;
2606     }
2607     aio_context = bdrv_get_aio_context(bs);
2608 
2609     saved_vm_running = runstate_is_running();
2610 
2611     ret = global_state_store();
2612     if (ret) {
2613         error_setg(errp, "Error saving global state");
2614         return ret;
2615     }
2616     vm_stop(RUN_STATE_SAVE_VM);
2617 
2618     bdrv_drain_all_begin();
2619 
2620     aio_context_acquire(aio_context);
2621 
2622     memset(sn, 0, sizeof(*sn));
2623 
2624     /* fill auxiliary fields */
2625     qemu_gettimeofday(&tv);
2626     sn->date_sec = tv.tv_sec;
2627     sn->date_nsec = tv.tv_usec * 1000;
2628     sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2629 
2630     if (name) {
2631         ret = bdrv_snapshot_find(bs, old_sn, name);
2632         if (ret >= 0) {
2633             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2634             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2635         } else {
2636             pstrcpy(sn->name, sizeof(sn->name), name);
2637         }
2638     } else {
2639         /* cast below needed for OpenBSD where tv_sec is still 'long' */
2640         localtime_r((const time_t *)&tv.tv_sec, &tm);
2641         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2642     }
2643 
2644     /* save the VM state */
2645     f = qemu_fopen_bdrv(bs, 1);
2646     if (!f) {
2647         error_setg(errp, "Could not open VM state file");
2648         goto the_end;
2649     }
2650     ret = qemu_savevm_state(f, errp);
2651     vm_state_size = qemu_ftell(f);
2652     qemu_fclose(f);
2653     if (ret < 0) {
2654         goto the_end;
2655     }
2656 
2657     /* The bdrv_all_create_snapshot() call that follows acquires the AioContext
2658      * for itself.  BDRV_POLL_WHILE() does not support nested locking because
2659      * it only releases the lock once.  Therefore synchronous I/O will deadlock
2660      * unless we release the AioContext before bdrv_all_create_snapshot().
2661      */
2662     aio_context_release(aio_context);
2663     aio_context = NULL;
2664 
2665     ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2666     if (ret < 0) {
2667         error_setg(errp, "Error while creating snapshot on '%s'",
2668                    bdrv_get_device_name(bs));
2669         goto the_end;
2670     }
2671 
2672     ret = 0;
2673 
2674  the_end:
2675     if (aio_context) {
2676         aio_context_release(aio_context);
2677     }
2678 
2679     bdrv_drain_all_end();
2680 
2681     if (saved_vm_running) {
2682         vm_start();
2683     }
2684     return ret;
2685 }
2686 
2687 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
2688                                 Error **errp)
2689 {
2690     QEMUFile *f;
2691     QIOChannelFile *ioc;
2692     int saved_vm_running;
2693     int ret;
2694 
2695     if (!has_live) {
2696         /* live default to true so old version of Xen tool stack can have a
2697          * successfull live migration */
2698         live = true;
2699     }
2700 
2701     saved_vm_running = runstate_is_running();
2702     vm_stop(RUN_STATE_SAVE_VM);
2703     global_state_store_running();
2704 
2705     ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2706     if (!ioc) {
2707         goto the_end;
2708     }
2709     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2710     f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2711     object_unref(OBJECT(ioc));
2712     ret = qemu_save_device_state(f);
2713     if (ret < 0 || qemu_fclose(f) < 0) {
2714         error_setg(errp, QERR_IO_ERROR);
2715     } else {
2716         /* libxl calls the QMP command "stop" before calling
2717          * "xen-save-devices-state" and in case of migration failure, libxl
2718          * would call "cont".
2719          * So call bdrv_inactivate_all (release locks) here to let the other
2720          * side of the migration take controle of the images.
2721          */
2722         if (live && !saved_vm_running) {
2723             ret = bdrv_inactivate_all();
2724             if (ret) {
2725                 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
2726                            __func__, ret);
2727             }
2728         }
2729     }
2730 
2731  the_end:
2732     if (saved_vm_running) {
2733         vm_start();
2734     }
2735 }
2736 
2737 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2738 {
2739     QEMUFile *f;
2740     QIOChannelFile *ioc;
2741     int ret;
2742 
2743     /* Guest must be paused before loading the device state; the RAM state
2744      * will already have been loaded by xc
2745      */
2746     if (runstate_is_running()) {
2747         error_setg(errp, "Cannot update device state while vm is running");
2748         return;
2749     }
2750     vm_stop(RUN_STATE_RESTORE_VM);
2751 
2752     ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2753     if (!ioc) {
2754         return;
2755     }
2756     qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2757     f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2758     object_unref(OBJECT(ioc));
2759 
2760     ret = qemu_loadvm_state(f);
2761     qemu_fclose(f);
2762     if (ret < 0) {
2763         error_setg(errp, QERR_IO_ERROR);
2764     }
2765     migration_incoming_state_destroy();
2766 }
2767 
2768 int load_snapshot(const char *name, Error **errp)
2769 {
2770     BlockDriverState *bs, *bs_vm_state;
2771     QEMUSnapshotInfo sn;
2772     QEMUFile *f;
2773     int ret;
2774     AioContext *aio_context;
2775     MigrationIncomingState *mis = migration_incoming_get_current();
2776 
2777     if (!replay_can_snapshot()) {
2778         error_setg(errp, "Record/replay does not allow loading snapshot "
2779                    "right now. Try once more later.");
2780         return -EINVAL;
2781     }
2782 
2783     if (!bdrv_all_can_snapshot(&bs)) {
2784         error_setg(errp,
2785                    "Device '%s' is writable but does not support snapshots",
2786                    bdrv_get_device_name(bs));
2787         return -ENOTSUP;
2788     }
2789     ret = bdrv_all_find_snapshot(name, &bs);
2790     if (ret < 0) {
2791         error_setg(errp,
2792                    "Device '%s' does not have the requested snapshot '%s'",
2793                    bdrv_get_device_name(bs), name);
2794         return ret;
2795     }
2796 
2797     bs_vm_state = bdrv_all_find_vmstate_bs();
2798     if (!bs_vm_state) {
2799         error_setg(errp, "No block device supports snapshots");
2800         return -ENOTSUP;
2801     }
2802     aio_context = bdrv_get_aio_context(bs_vm_state);
2803 
2804     /* Don't even try to load empty VM states */
2805     aio_context_acquire(aio_context);
2806     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2807     aio_context_release(aio_context);
2808     if (ret < 0) {
2809         return ret;
2810     } else if (sn.vm_state_size == 0) {
2811         error_setg(errp, "This is a disk-only snapshot. Revert to it "
2812                    " offline using qemu-img");
2813         return -EINVAL;
2814     }
2815 
2816     /* Flush all IO requests so they don't interfere with the new state.  */
2817     bdrv_drain_all_begin();
2818 
2819     ret = bdrv_all_goto_snapshot(name, &bs, errp);
2820     if (ret < 0) {
2821         error_prepend(errp, "Could not load snapshot '%s' on '%s': ",
2822                       name, bdrv_get_device_name(bs));
2823         goto err_drain;
2824     }
2825 
2826     /* restore the VM state */
2827     f = qemu_fopen_bdrv(bs_vm_state, 0);
2828     if (!f) {
2829         error_setg(errp, "Could not open VM state file");
2830         ret = -EINVAL;
2831         goto err_drain;
2832     }
2833 
2834     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
2835     mis->from_src_file = f;
2836 
2837     aio_context_acquire(aio_context);
2838     ret = qemu_loadvm_state(f);
2839     migration_incoming_state_destroy();
2840     aio_context_release(aio_context);
2841 
2842     bdrv_drain_all_end();
2843 
2844     if (ret < 0) {
2845         error_setg(errp, "Error %d while loading VM state", ret);
2846         return ret;
2847     }
2848 
2849     return 0;
2850 
2851 err_drain:
2852     bdrv_drain_all_end();
2853     return ret;
2854 }
2855 
2856 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2857 {
2858     qemu_ram_set_idstr(mr->ram_block,
2859                        memory_region_name(mr), dev);
2860     qemu_ram_set_migratable(mr->ram_block);
2861 }
2862 
2863 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2864 {
2865     qemu_ram_unset_idstr(mr->ram_block);
2866     qemu_ram_unset_migratable(mr->ram_block);
2867 }
2868 
2869 void vmstate_register_ram_global(MemoryRegion *mr)
2870 {
2871     vmstate_register_ram(mr, NULL);
2872 }
2873 
2874 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
2875 {
2876     /* check needed if --only-migratable is specified */
2877     if (!only_migratable) {
2878         return true;
2879     }
2880 
2881     return !(vmsd && vmsd->unmigratable);
2882 }
2883