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