target/arm: Convert SABA, SABD, UABA, UABD to decodetree
[qemu/ar7.git] / migration / savevm.c
blobc621f2359ba39e51e3537ff94561aa8f85dcd238
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2009-2015 Red Hat Inc
7 * Authors:
8 * Juan Quintela <quintela@redhat.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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.
29 #include "qemu/osdep.h"
30 #include "hw/boards.h"
31 #include "net/net.h"
32 #include "migration.h"
33 #include "migration/snapshot.h"
34 #include "migration-stats.h"
35 #include "migration/vmstate.h"
36 #include "migration/misc.h"
37 #include "migration/register.h"
38 #include "migration/global_state.h"
39 #include "migration/channel-block.h"
40 #include "ram.h"
41 #include "qemu-file.h"
42 #include "savevm.h"
43 #include "postcopy-ram.h"
44 #include "qapi/error.h"
45 #include "qapi/qapi-commands-migration.h"
46 #include "qapi/clone-visitor.h"
47 #include "qapi/qapi-builtin-visit.h"
48 #include "qemu/error-report.h"
49 #include "sysemu/cpus.h"
50 #include "exec/memory.h"
51 #include "exec/target_page.h"
52 #include "trace.h"
53 #include "qemu/iov.h"
54 #include "qemu/job.h"
55 #include "qemu/main-loop.h"
56 #include "block/snapshot.h"
57 #include "qemu/cutils.h"
58 #include "io/channel-buffer.h"
59 #include "io/channel-file.h"
60 #include "sysemu/replay.h"
61 #include "sysemu/runstate.h"
62 #include "sysemu/sysemu.h"
63 #include "sysemu/xen.h"
64 #include "migration/colo.h"
65 #include "qemu/bitmap.h"
66 #include "net/announce.h"
67 #include "qemu/yank.h"
68 #include "yank_functions.h"
69 #include "sysemu/qtest.h"
70 #include "options.h"
72 const unsigned int postcopy_ram_discard_version;
74 /* Subcommands for QEMU_VM_COMMAND */
75 enum qemu_vm_cmd {
76 MIG_CMD_INVALID = 0, /* Must be 0 */
77 MIG_CMD_OPEN_RETURN_PATH, /* Tell the dest to open the Return path */
78 MIG_CMD_PING, /* Request a PONG on the RP */
80 MIG_CMD_POSTCOPY_ADVISE, /* Prior to any page transfers, just
81 warn we might want to do PC */
82 MIG_CMD_POSTCOPY_LISTEN, /* Start listening for incoming
83 pages as it's running. */
84 MIG_CMD_POSTCOPY_RUN, /* Start execution */
86 MIG_CMD_POSTCOPY_RAM_DISCARD, /* A list of pages to discard that
87 were previously sent during
88 precopy but are dirty. */
89 MIG_CMD_PACKAGED, /* Send a wrapped stream within this stream */
90 MIG_CMD_ENABLE_COLO, /* Enable COLO */
91 MIG_CMD_POSTCOPY_RESUME, /* resume postcopy on dest */
92 MIG_CMD_RECV_BITMAP, /* Request for recved bitmap on dst */
93 MIG_CMD_MAX
96 #define MAX_VM_CMD_PACKAGED_SIZE UINT32_MAX
97 static struct mig_cmd_args {
98 ssize_t len; /* -1 = variable */
99 const char *name;
100 } mig_cmd_args[] = {
101 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
102 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
103 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
104 [MIG_CMD_POSTCOPY_ADVISE] = { .len = -1, .name = "POSTCOPY_ADVISE" },
105 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" },
106 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" },
107 [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
108 .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
109 [MIG_CMD_POSTCOPY_RESUME] = { .len = 0, .name = "POSTCOPY_RESUME" },
110 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" },
111 [MIG_CMD_RECV_BITMAP] = { .len = -1, .name = "RECV_BITMAP" },
112 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
115 /* Note for MIG_CMD_POSTCOPY_ADVISE:
116 * The format of arguments is depending on postcopy mode:
117 * - postcopy RAM only
118 * uint64_t host page size
119 * uint64_t target page size
121 * - postcopy RAM and postcopy dirty bitmaps
122 * format is the same as for postcopy RAM only
124 * - postcopy dirty bitmaps only
125 * Nothing. Command length field is 0.
127 * Be careful: adding a new postcopy entity with some other parameters should
128 * not break format self-description ability. Good way is to introduce some
129 * generic extendable format with an exception for two old entities.
132 /***********************************************************/
133 /* savevm/loadvm support */
135 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
137 if (is_writable) {
138 return qemu_file_new_output(QIO_CHANNEL(qio_channel_block_new(bs)));
139 } else {
140 return qemu_file_new_input(QIO_CHANNEL(qio_channel_block_new(bs)));
145 /* QEMUFile timer support.
146 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
149 void timer_put(QEMUFile *f, QEMUTimer *ts)
151 uint64_t expire_time;
153 expire_time = timer_expire_time_ns(ts);
154 qemu_put_be64(f, expire_time);
157 void timer_get(QEMUFile *f, QEMUTimer *ts)
159 uint64_t expire_time;
161 expire_time = qemu_get_be64(f);
162 if (expire_time != -1) {
163 timer_mod_ns(ts, expire_time);
164 } else {
165 timer_del(ts);
170 /* VMState timer support.
171 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
174 static int get_timer(QEMUFile *f, void *pv, size_t size,
175 const VMStateField *field)
177 QEMUTimer *v = pv;
178 timer_get(f, v);
179 return 0;
182 static int put_timer(QEMUFile *f, void *pv, size_t size,
183 const VMStateField *field, JSONWriter *vmdesc)
185 QEMUTimer *v = pv;
186 timer_put(f, v);
188 return 0;
191 const VMStateInfo vmstate_info_timer = {
192 .name = "timer",
193 .get = get_timer,
194 .put = put_timer,
198 typedef struct CompatEntry {
199 char idstr[256];
200 int instance_id;
201 } CompatEntry;
203 typedef struct SaveStateEntry {
204 QTAILQ_ENTRY(SaveStateEntry) entry;
205 char idstr[256];
206 uint32_t instance_id;
207 int alias_id;
208 int version_id;
209 /* version id read from the stream */
210 int load_version_id;
211 int section_id;
212 /* section id read from the stream */
213 int load_section_id;
214 const SaveVMHandlers *ops;
215 const VMStateDescription *vmsd;
216 void *opaque;
217 CompatEntry *compat;
218 int is_ram;
219 } SaveStateEntry;
221 typedef struct SaveState {
222 QTAILQ_HEAD(, SaveStateEntry) handlers;
223 SaveStateEntry *handler_pri_head[MIG_PRI_MAX + 1];
224 int global_section_id;
225 uint32_t len;
226 const char *name;
227 uint32_t target_page_bits;
228 uint32_t caps_count;
229 MigrationCapability *capabilities;
230 QemuUUID uuid;
231 } SaveState;
233 static SaveState savevm_state = {
234 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
235 .handler_pri_head = { [MIG_PRI_DEFAULT ... MIG_PRI_MAX] = NULL },
236 .global_section_id = 0,
239 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id);
241 static bool should_validate_capability(int capability)
243 assert(capability >= 0 && capability < MIGRATION_CAPABILITY__MAX);
244 /* Validate only new capabilities to keep compatibility. */
245 switch (capability) {
246 case MIGRATION_CAPABILITY_X_IGNORE_SHARED:
247 case MIGRATION_CAPABILITY_MAPPED_RAM:
248 return true;
249 default:
250 return false;
254 static uint32_t get_validatable_capabilities_count(void)
256 MigrationState *s = migrate_get_current();
257 uint32_t result = 0;
258 int i;
259 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
260 if (should_validate_capability(i) && s->capabilities[i]) {
261 result++;
264 return result;
267 static int configuration_pre_save(void *opaque)
269 SaveState *state = opaque;
270 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
271 MigrationState *s = migrate_get_current();
272 int i, j;
274 state->len = strlen(current_name);
275 state->name = current_name;
276 state->target_page_bits = qemu_target_page_bits();
278 state->caps_count = get_validatable_capabilities_count();
279 state->capabilities = g_renew(MigrationCapability, state->capabilities,
280 state->caps_count);
281 for (i = j = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
282 if (should_validate_capability(i) && s->capabilities[i]) {
283 state->capabilities[j++] = i;
286 state->uuid = qemu_uuid;
288 return 0;
291 static int configuration_post_save(void *opaque)
293 SaveState *state = opaque;
295 g_free(state->capabilities);
296 state->capabilities = NULL;
297 state->caps_count = 0;
298 return 0;
301 static int configuration_pre_load(void *opaque)
303 SaveState *state = opaque;
305 /* If there is no target-page-bits subsection it means the source
306 * predates the variable-target-page-bits support and is using the
307 * minimum possible value for this CPU.
309 state->target_page_bits = qemu_target_page_bits_min();
310 return 0;
313 static bool configuration_validate_capabilities(SaveState *state)
315 bool ret = true;
316 MigrationState *s = migrate_get_current();
317 unsigned long *source_caps_bm;
318 int i;
320 source_caps_bm = bitmap_new(MIGRATION_CAPABILITY__MAX);
321 for (i = 0; i < state->caps_count; i++) {
322 MigrationCapability capability = state->capabilities[i];
323 set_bit(capability, source_caps_bm);
326 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
327 bool source_state, target_state;
328 if (!should_validate_capability(i)) {
329 continue;
331 source_state = test_bit(i, source_caps_bm);
332 target_state = s->capabilities[i];
333 if (source_state != target_state) {
334 error_report("Capability %s is %s, but received capability is %s",
335 MigrationCapability_str(i),
336 target_state ? "on" : "off",
337 source_state ? "on" : "off");
338 ret = false;
339 /* Don't break here to report all failed capabilities */
343 g_free(source_caps_bm);
344 return ret;
347 static int configuration_post_load(void *opaque, int version_id)
349 SaveState *state = opaque;
350 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
351 int ret = 0;
353 if (strncmp(state->name, current_name, state->len) != 0) {
354 error_report("Machine type received is '%.*s' and local is '%s'",
355 (int) state->len, state->name, current_name);
356 ret = -EINVAL;
357 goto out;
360 if (state->target_page_bits != qemu_target_page_bits()) {
361 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
362 state->target_page_bits, qemu_target_page_bits());
363 ret = -EINVAL;
364 goto out;
367 if (!configuration_validate_capabilities(state)) {
368 ret = -EINVAL;
369 goto out;
372 out:
373 g_free((void *)state->name);
374 state->name = NULL;
375 state->len = 0;
376 g_free(state->capabilities);
377 state->capabilities = NULL;
378 state->caps_count = 0;
380 return ret;
383 static int get_capability(QEMUFile *f, void *pv, size_t size,
384 const VMStateField *field)
386 MigrationCapability *capability = pv;
387 char capability_str[UINT8_MAX + 1];
388 uint8_t len;
389 int i;
391 len = qemu_get_byte(f);
392 qemu_get_buffer(f, (uint8_t *)capability_str, len);
393 capability_str[len] = '\0';
394 for (i = 0; i < MIGRATION_CAPABILITY__MAX; i++) {
395 if (!strcmp(MigrationCapability_str(i), capability_str)) {
396 *capability = i;
397 return 0;
400 error_report("Received unknown capability %s", capability_str);
401 return -EINVAL;
404 static int put_capability(QEMUFile *f, void *pv, size_t size,
405 const VMStateField *field, JSONWriter *vmdesc)
407 MigrationCapability *capability = pv;
408 const char *capability_str = MigrationCapability_str(*capability);
409 size_t len = strlen(capability_str);
410 assert(len <= UINT8_MAX);
412 qemu_put_byte(f, len);
413 qemu_put_buffer(f, (uint8_t *)capability_str, len);
414 return 0;
417 static const VMStateInfo vmstate_info_capability = {
418 .name = "capability",
419 .get = get_capability,
420 .put = put_capability,
423 /* The target-page-bits subsection is present only if the
424 * target page size is not the same as the default (ie the
425 * minimum page size for a variable-page-size guest CPU).
426 * If it is present then it contains the actual target page
427 * bits for the machine, and migration will fail if the
428 * two ends don't agree about it.
430 static bool vmstate_target_page_bits_needed(void *opaque)
432 return qemu_target_page_bits()
433 > qemu_target_page_bits_min();
436 static const VMStateDescription vmstate_target_page_bits = {
437 .name = "configuration/target-page-bits",
438 .version_id = 1,
439 .minimum_version_id = 1,
440 .needed = vmstate_target_page_bits_needed,
441 .fields = (const VMStateField[]) {
442 VMSTATE_UINT32(target_page_bits, SaveState),
443 VMSTATE_END_OF_LIST()
447 static bool vmstate_capabilites_needed(void *opaque)
449 return get_validatable_capabilities_count() > 0;
452 static const VMStateDescription vmstate_capabilites = {
453 .name = "configuration/capabilities",
454 .version_id = 1,
455 .minimum_version_id = 1,
456 .needed = vmstate_capabilites_needed,
457 .fields = (const VMStateField[]) {
458 VMSTATE_UINT32_V(caps_count, SaveState, 1),
459 VMSTATE_VARRAY_UINT32_ALLOC(capabilities, SaveState, caps_count, 1,
460 vmstate_info_capability,
461 MigrationCapability),
462 VMSTATE_END_OF_LIST()
466 static bool vmstate_uuid_needed(void *opaque)
468 return qemu_uuid_set && migrate_validate_uuid();
471 static int vmstate_uuid_post_load(void *opaque, int version_id)
473 SaveState *state = opaque;
474 char uuid_src[UUID_STR_LEN];
475 char uuid_dst[UUID_STR_LEN];
477 if (!qemu_uuid_set) {
479 * It's warning because user might not know UUID in some cases,
480 * e.g. load an old snapshot
482 qemu_uuid_unparse(&state->uuid, uuid_src);
483 warn_report("UUID is received %s, but local uuid isn't set",
484 uuid_src);
485 return 0;
487 if (!qemu_uuid_is_equal(&state->uuid, &qemu_uuid)) {
488 qemu_uuid_unparse(&state->uuid, uuid_src);
489 qemu_uuid_unparse(&qemu_uuid, uuid_dst);
490 error_report("UUID received is %s and local is %s", uuid_src, uuid_dst);
491 return -EINVAL;
493 return 0;
496 static const VMStateDescription vmstate_uuid = {
497 .name = "configuration/uuid",
498 .version_id = 1,
499 .minimum_version_id = 1,
500 .needed = vmstate_uuid_needed,
501 .post_load = vmstate_uuid_post_load,
502 .fields = (const VMStateField[]) {
503 VMSTATE_UINT8_ARRAY_V(uuid.data, SaveState, sizeof(QemuUUID), 1),
504 VMSTATE_END_OF_LIST()
508 static const VMStateDescription vmstate_configuration = {
509 .name = "configuration",
510 .version_id = 1,
511 .pre_load = configuration_pre_load,
512 .post_load = configuration_post_load,
513 .pre_save = configuration_pre_save,
514 .post_save = configuration_post_save,
515 .fields = (const VMStateField[]) {
516 VMSTATE_UINT32(len, SaveState),
517 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, len),
518 VMSTATE_END_OF_LIST()
520 .subsections = (const VMStateDescription * const []) {
521 &vmstate_target_page_bits,
522 &vmstate_capabilites,
523 &vmstate_uuid,
524 NULL
528 static void dump_vmstate_vmsd(FILE *out_file,
529 const VMStateDescription *vmsd, int indent,
530 bool is_subsection);
532 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
533 int indent)
535 fprintf(out_file, "%*s{\n", indent, "");
536 indent += 2;
537 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
538 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
539 field->version_id);
540 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
541 field->field_exists ? "true" : "false");
542 if (field->flags & VMS_ARRAY) {
543 fprintf(out_file, "%*s\"num\": %d,\n", indent, "", field->num);
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);
550 fprintf(out_file, "\n%*s}", indent - 2, "");
553 static void dump_vmstate_vmss(FILE *out_file,
554 const VMStateDescription *subsection,
555 int indent)
557 if (subsection != NULL) {
558 dump_vmstate_vmsd(out_file, subsection, indent, true);
562 static void dump_vmstate_vmsd(FILE *out_file,
563 const VMStateDescription *vmsd, int indent,
564 bool is_subsection)
566 if (is_subsection) {
567 fprintf(out_file, "%*s{\n", indent, "");
568 } else {
569 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
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;
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;
589 if (!first) {
590 fprintf(out_file, ",\n");
592 dump_vmstate_vmsf(out_file, field, indent + 2);
593 field++;
594 first = false;
596 assert(field->flags == VMS_END);
597 fprintf(out_file, "\n%*s]", indent, "");
599 if (vmsd->subsections != NULL) {
600 const VMStateDescription * const *subsection = vmsd->subsections;
601 bool first;
603 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
604 first = true;
605 while (*subsection != NULL) {
606 if (!first) {
607 fprintf(out_file, ",\n");
609 dump_vmstate_vmss(out_file, *subsection, indent + 2);
610 subsection++;
611 first = false;
613 fprintf(out_file, "\n%*s]", indent, "");
615 fprintf(out_file, "\n%*s}", indent - 2, "");
618 static void dump_machine_type(FILE *out_file)
620 MachineClass *mc;
622 mc = MACHINE_GET_CLASS(current_machine);
624 fprintf(out_file, " \"vmschkmachine\": {\n");
625 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
626 fprintf(out_file, " },\n");
629 void dump_vmstate_json_to_file(FILE *out_file)
631 GSList *list, *elt;
632 bool first;
634 fprintf(out_file, "{\n");
635 dump_machine_type(out_file);
637 first = true;
638 list = object_class_get_list(TYPE_DEVICE, true);
639 for (elt = list; elt; elt = elt->next) {
640 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
641 TYPE_DEVICE);
642 const char *name;
643 int indent = 2;
645 if (!dc->vmsd) {
646 continue;
649 if (!first) {
650 fprintf(out_file, ",\n");
652 name = object_class_get_name(OBJECT_CLASS(dc));
653 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
654 indent += 2;
655 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
656 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
657 dc->vmsd->version_id);
658 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
659 dc->vmsd->minimum_version_id);
661 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
663 fprintf(out_file, "\n%*s}", indent - 2, "");
664 first = false;
666 fprintf(out_file, "\n}\n");
667 fclose(out_file);
668 g_slist_free(list);
671 static uint32_t calculate_new_instance_id(const char *idstr)
673 SaveStateEntry *se;
674 uint32_t instance_id = 0;
676 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
677 if (strcmp(idstr, se->idstr) == 0
678 && instance_id <= se->instance_id) {
679 instance_id = se->instance_id + 1;
682 /* Make sure we never loop over without being noticed */
683 assert(instance_id != VMSTATE_INSTANCE_ID_ANY);
684 return instance_id;
687 static int calculate_compat_instance_id(const char *idstr)
689 SaveStateEntry *se;
690 int instance_id = 0;
692 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
693 if (!se->compat) {
694 continue;
697 if (strcmp(idstr, se->compat->idstr) == 0
698 && instance_id <= se->compat->instance_id) {
699 instance_id = se->compat->instance_id + 1;
702 return instance_id;
705 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
707 if (se->vmsd) {
708 return se->vmsd->priority;
710 return MIG_PRI_DEFAULT;
713 static void savevm_state_handler_insert(SaveStateEntry *nse)
715 MigrationPriority priority = save_state_priority(nse);
716 SaveStateEntry *se;
717 int i;
719 assert(priority <= MIG_PRI_MAX);
722 * This should never happen otherwise migration will probably fail
723 * silently somewhere because we can be wrongly applying one
724 * object properties upon another one. Bail out ASAP.
726 if (find_se(nse->idstr, nse->instance_id)) {
727 error_report("%s: Detected duplicate SaveStateEntry: "
728 "id=%s, instance_id=0x%"PRIx32, __func__,
729 nse->idstr, nse->instance_id);
730 exit(EXIT_FAILURE);
733 for (i = priority - 1; i >= 0; i--) {
734 se = savevm_state.handler_pri_head[i];
735 if (se != NULL) {
736 assert(save_state_priority(se) < priority);
737 break;
741 if (i >= 0) {
742 QTAILQ_INSERT_BEFORE(se, nse, entry);
743 } else {
744 QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
747 if (savevm_state.handler_pri_head[priority] == NULL) {
748 savevm_state.handler_pri_head[priority] = nse;
752 static void savevm_state_handler_remove(SaveStateEntry *se)
754 SaveStateEntry *next;
755 MigrationPriority priority = save_state_priority(se);
757 if (se == savevm_state.handler_pri_head[priority]) {
758 next = QTAILQ_NEXT(se, entry);
759 if (next != NULL && save_state_priority(next) == priority) {
760 savevm_state.handler_pri_head[priority] = next;
761 } else {
762 savevm_state.handler_pri_head[priority] = NULL;
765 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
768 /* TODO: Individual devices generally have very little idea about the rest
769 of the system, so instance_id should be removed/replaced.
770 Meanwhile pass -1 as instance_id if you do not already have a clearly
771 distinguishing id for all instances of your device class. */
772 int register_savevm_live(const char *idstr,
773 uint32_t instance_id,
774 int version_id,
775 const SaveVMHandlers *ops,
776 void *opaque)
778 SaveStateEntry *se;
780 se = g_new0(SaveStateEntry, 1);
781 se->version_id = version_id;
782 se->section_id = savevm_state.global_section_id++;
783 se->ops = ops;
784 se->opaque = opaque;
785 se->vmsd = NULL;
786 /* if this is a live_savem then set is_ram */
787 if (ops->save_setup != NULL) {
788 se->is_ram = 1;
791 pstrcat(se->idstr, sizeof(se->idstr), idstr);
793 if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
794 se->instance_id = calculate_new_instance_id(se->idstr);
795 } else {
796 se->instance_id = instance_id;
798 assert(!se->compat || se->instance_id == 0);
799 savevm_state_handler_insert(se);
800 return 0;
803 void unregister_savevm(VMStateIf *obj, const char *idstr, void *opaque)
805 SaveStateEntry *se, *new_se;
806 char id[256] = "";
808 if (obj) {
809 char *oid = vmstate_if_get_id(obj);
810 if (oid) {
811 pstrcpy(id, sizeof(id), oid);
812 pstrcat(id, sizeof(id), "/");
813 g_free(oid);
816 pstrcat(id, sizeof(id), idstr);
818 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
819 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
820 savevm_state_handler_remove(se);
821 g_free(se->compat);
822 g_free(se);
828 * Perform some basic checks on vmsd's at registration
829 * time.
831 static void vmstate_check(const VMStateDescription *vmsd)
833 const VMStateField *field = vmsd->fields;
834 const VMStateDescription * const *subsection = vmsd->subsections;
836 if (field) {
837 while (field->name) {
838 if (field->flags & (VMS_STRUCT | VMS_VSTRUCT)) {
839 /* Recurse to sub structures */
840 vmstate_check(field->vmsd);
842 /* Carry on */
843 field++;
845 /* Check for the end of field list canary */
846 if (field->flags != VMS_END) {
847 error_report("VMSTATE not ending with VMS_END: %s", vmsd->name);
848 g_assert_not_reached();
852 while (subsection && *subsection) {
854 * The name of a subsection should start with the name of the
855 * current object.
857 assert(!strncmp(vmsd->name, (*subsection)->name, strlen(vmsd->name)));
858 vmstate_check(*subsection);
859 subsection++;
864 * See comment in hw/intc/xics.c:icp_realize()
866 * This function can be removed when
867 * pre_2_10_vmstate_register_dummy_icp() is removed.
869 int vmstate_replace_hack_for_ppc(VMStateIf *obj, int instance_id,
870 const VMStateDescription *vmsd,
871 void *opaque)
873 SaveStateEntry *se = find_se(vmsd->name, instance_id);
875 if (se) {
876 savevm_state_handler_remove(se);
878 return vmstate_register(obj, instance_id, vmsd, opaque);
881 int vmstate_register_with_alias_id(VMStateIf *obj, uint32_t instance_id,
882 const VMStateDescription *vmsd,
883 void *opaque, int alias_id,
884 int required_for_version,
885 Error **errp)
887 SaveStateEntry *se;
889 /* If this triggers, alias support can be dropped for the vmsd. */
890 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
892 se = g_new0(SaveStateEntry, 1);
893 se->version_id = vmsd->version_id;
894 se->section_id = savevm_state.global_section_id++;
895 se->opaque = opaque;
896 se->vmsd = vmsd;
897 se->alias_id = alias_id;
899 if (obj) {
900 char *id = vmstate_if_get_id(obj);
901 if (id) {
902 if (snprintf(se->idstr, sizeof(se->idstr), "%s/", id) >=
903 sizeof(se->idstr)) {
904 error_setg(errp, "Path too long for VMState (%s)", id);
905 g_free(id);
906 g_free(se);
908 return -1;
910 g_free(id);
912 se->compat = g_new0(CompatEntry, 1);
913 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
914 se->compat->instance_id = instance_id == VMSTATE_INSTANCE_ID_ANY ?
915 calculate_compat_instance_id(vmsd->name) : instance_id;
916 instance_id = VMSTATE_INSTANCE_ID_ANY;
919 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
921 if (instance_id == VMSTATE_INSTANCE_ID_ANY) {
922 se->instance_id = calculate_new_instance_id(se->idstr);
923 } else {
924 se->instance_id = instance_id;
927 /* Perform a recursive sanity check during the test runs */
928 if (qtest_enabled()) {
929 vmstate_check(vmsd);
931 assert(!se->compat || se->instance_id == 0);
932 savevm_state_handler_insert(se);
933 return 0;
936 void vmstate_unregister(VMStateIf *obj, const VMStateDescription *vmsd,
937 void *opaque)
939 SaveStateEntry *se, *new_se;
941 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
942 if (se->vmsd == vmsd && se->opaque == opaque) {
943 savevm_state_handler_remove(se);
944 g_free(se->compat);
945 g_free(se);
950 static int vmstate_load(QEMUFile *f, SaveStateEntry *se)
952 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
953 if (!se->vmsd) { /* Old style */
954 return se->ops->load_state(f, se->opaque, se->load_version_id);
956 return vmstate_load_state(f, se->vmsd, se->opaque, se->load_version_id);
959 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se,
960 JSONWriter *vmdesc)
962 uint64_t old_offset = qemu_file_transferred(f);
963 se->ops->save_state(f, se->opaque);
964 uint64_t size = qemu_file_transferred(f) - old_offset;
966 if (vmdesc) {
967 json_writer_int64(vmdesc, "size", size);
968 json_writer_start_array(vmdesc, "fields");
969 json_writer_start_object(vmdesc, NULL);
970 json_writer_str(vmdesc, "name", "data");
971 json_writer_int64(vmdesc, "size", size);
972 json_writer_str(vmdesc, "type", "buffer");
973 json_writer_end_object(vmdesc);
974 json_writer_end_array(vmdesc);
979 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
981 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
982 uint8_t section_type)
984 qemu_put_byte(f, section_type);
985 qemu_put_be32(f, se->section_id);
987 if (section_type == QEMU_VM_SECTION_FULL ||
988 section_type == QEMU_VM_SECTION_START) {
989 /* ID string */
990 size_t len = strlen(se->idstr);
991 qemu_put_byte(f, len);
992 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
994 qemu_put_be32(f, se->instance_id);
995 qemu_put_be32(f, se->version_id);
1000 * Write a footer onto device sections that catches cases misformatted device
1001 * sections.
1003 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
1005 if (migrate_get_current()->send_section_footer) {
1006 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
1007 qemu_put_be32(f, se->section_id);
1011 static int vmstate_save(QEMUFile *f, SaveStateEntry *se, JSONWriter *vmdesc,
1012 Error **errp)
1014 int ret;
1016 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1017 return 0;
1019 if (se->vmsd && !vmstate_section_needed(se->vmsd, se->opaque)) {
1020 trace_savevm_section_skip(se->idstr, se->section_id);
1021 return 0;
1024 trace_savevm_section_start(se->idstr, se->section_id);
1025 save_section_header(f, se, QEMU_VM_SECTION_FULL);
1026 if (vmdesc) {
1027 json_writer_start_object(vmdesc, NULL);
1028 json_writer_str(vmdesc, "name", se->idstr);
1029 json_writer_int64(vmdesc, "instance_id", se->instance_id);
1032 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
1033 if (!se->vmsd) {
1034 vmstate_save_old_style(f, se, vmdesc);
1035 } else {
1036 ret = vmstate_save_state_with_err(f, se->vmsd, se->opaque, vmdesc,
1037 errp);
1038 if (ret) {
1039 return ret;
1043 trace_savevm_section_end(se->idstr, se->section_id, 0);
1044 save_section_footer(f, se);
1045 if (vmdesc) {
1046 json_writer_end_object(vmdesc);
1048 return 0;
1051 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
1052 * command and associated data.
1054 * @f: File to send command on
1055 * @command: Command type to send
1056 * @len: Length of associated data
1057 * @data: Data associated with command.
1059 static void qemu_savevm_command_send(QEMUFile *f,
1060 enum qemu_vm_cmd command,
1061 uint16_t len,
1062 uint8_t *data)
1064 trace_savevm_command_send(command, len);
1065 qemu_put_byte(f, QEMU_VM_COMMAND);
1066 qemu_put_be16(f, (uint16_t)command);
1067 qemu_put_be16(f, len);
1068 qemu_put_buffer(f, data, len);
1069 qemu_fflush(f);
1072 void qemu_savevm_send_colo_enable(QEMUFile *f)
1074 trace_savevm_send_colo_enable();
1075 qemu_savevm_command_send(f, MIG_CMD_ENABLE_COLO, 0, NULL);
1078 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
1080 uint32_t buf;
1082 trace_savevm_send_ping(value);
1083 buf = cpu_to_be32(value);
1084 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
1087 void qemu_savevm_send_open_return_path(QEMUFile *f)
1089 trace_savevm_send_open_return_path();
1090 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
1093 /* We have a buffer of data to send; we don't want that all to be loaded
1094 * by the command itself, so the command contains just the length of the
1095 * extra buffer that we then send straight after it.
1096 * TODO: Must be a better way to organise that
1098 * Returns:
1099 * 0 on success
1100 * -ve on error
1102 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
1104 uint32_t tmp;
1105 MigrationState *ms = migrate_get_current();
1106 Error *local_err = NULL;
1108 if (len > MAX_VM_CMD_PACKAGED_SIZE) {
1109 error_setg(&local_err, "%s: Unreasonably large packaged state: %zu",
1110 __func__, len);
1111 migrate_set_error(ms, local_err);
1112 error_report_err(local_err);
1113 return -1;
1116 tmp = cpu_to_be32(len);
1118 trace_qemu_savevm_send_packaged();
1119 qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
1121 qemu_put_buffer(f, buf, len);
1123 return 0;
1126 /* Send prior to any postcopy transfer */
1127 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
1129 if (migrate_postcopy_ram()) {
1130 uint64_t tmp[2];
1131 tmp[0] = cpu_to_be64(ram_pagesize_summary());
1132 tmp[1] = cpu_to_be64(qemu_target_page_size());
1134 trace_qemu_savevm_send_postcopy_advise();
1135 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE,
1136 16, (uint8_t *)tmp);
1137 } else {
1138 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 0, NULL);
1142 /* Sent prior to starting the destination running in postcopy, discard pages
1143 * that have already been sent but redirtied on the source.
1144 * CMD_POSTCOPY_RAM_DISCARD consist of:
1145 * byte version (0)
1146 * byte Length of name field (not including 0)
1147 * n x byte RAM block name
1148 * byte 0 terminator (just for safety)
1149 * n x Byte ranges within the named RAMBlock
1150 * be64 Start of the range
1151 * be64 Length
1153 * name: RAMBlock name that these entries are part of
1154 * len: Number of page entries
1155 * start_list: 'len' addresses
1156 * length_list: 'len' addresses
1159 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
1160 uint16_t len,
1161 uint64_t *start_list,
1162 uint64_t *length_list)
1164 uint8_t *buf;
1165 uint16_t tmplen;
1166 uint16_t t;
1167 size_t name_len = strlen(name);
1169 trace_qemu_savevm_send_postcopy_ram_discard(name, len);
1170 assert(name_len < 256);
1171 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
1172 buf[0] = postcopy_ram_discard_version;
1173 buf[1] = name_len;
1174 memcpy(buf + 2, name, name_len);
1175 tmplen = 2 + name_len;
1176 buf[tmplen++] = '\0';
1178 for (t = 0; t < len; t++) {
1179 stq_be_p(buf + tmplen, start_list[t]);
1180 tmplen += 8;
1181 stq_be_p(buf + tmplen, length_list[t]);
1182 tmplen += 8;
1184 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
1185 g_free(buf);
1188 /* Get the destination into a state where it can receive postcopy data. */
1189 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
1191 trace_savevm_send_postcopy_listen();
1192 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
1195 /* Kick the destination into running */
1196 void qemu_savevm_send_postcopy_run(QEMUFile *f)
1198 trace_savevm_send_postcopy_run();
1199 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
1202 void qemu_savevm_send_postcopy_resume(QEMUFile *f)
1204 trace_savevm_send_postcopy_resume();
1205 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RESUME, 0, NULL);
1208 void qemu_savevm_send_recv_bitmap(QEMUFile *f, char *block_name)
1210 size_t len;
1211 char buf[256];
1213 trace_savevm_send_recv_bitmap(block_name);
1215 buf[0] = len = strlen(block_name);
1216 memcpy(buf + 1, block_name, len);
1218 qemu_savevm_command_send(f, MIG_CMD_RECV_BITMAP, len + 1, (uint8_t *)buf);
1221 bool qemu_savevm_state_blocked(Error **errp)
1223 SaveStateEntry *se;
1225 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1226 if (se->vmsd && se->vmsd->unmigratable) {
1227 error_setg(errp, "State blocked by non-migratable device '%s'",
1228 se->idstr);
1229 return true;
1232 return false;
1235 void qemu_savevm_non_migratable_list(strList **reasons)
1237 SaveStateEntry *se;
1239 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1240 if (se->vmsd && se->vmsd->unmigratable) {
1241 QAPI_LIST_PREPEND(*reasons,
1242 g_strdup_printf("non-migratable device: %s",
1243 se->idstr));
1248 void qemu_savevm_state_header(QEMUFile *f)
1250 MigrationState *s = migrate_get_current();
1252 s->vmdesc = json_writer_new(false);
1254 trace_savevm_state_header();
1255 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1256 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1258 if (s->send_configuration) {
1259 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
1262 * This starts the main json object and is paired with the
1263 * json_writer_end_object in
1264 * qemu_savevm_state_complete_precopy_non_iterable
1266 json_writer_start_object(s->vmdesc, NULL);
1268 json_writer_start_object(s->vmdesc, "configuration");
1269 vmstate_save_state(f, &vmstate_configuration, &savevm_state, s->vmdesc);
1270 json_writer_end_object(s->vmdesc);
1274 bool qemu_savevm_state_guest_unplug_pending(void)
1276 SaveStateEntry *se;
1278 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1279 if (se->vmsd && se->vmsd->dev_unplug_pending &&
1280 se->vmsd->dev_unplug_pending(se->opaque)) {
1281 return true;
1285 return false;
1288 int qemu_savevm_state_prepare(Error **errp)
1290 SaveStateEntry *se;
1291 int ret;
1293 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1294 if (!se->ops || !se->ops->save_prepare) {
1295 continue;
1297 if (se->ops->is_active) {
1298 if (!se->ops->is_active(se->opaque)) {
1299 continue;
1303 ret = se->ops->save_prepare(se->opaque, errp);
1304 if (ret < 0) {
1305 return ret;
1309 return 0;
1312 int qemu_savevm_state_setup(QEMUFile *f, Error **errp)
1314 ERRP_GUARD();
1315 MigrationState *ms = migrate_get_current();
1316 SaveStateEntry *se;
1317 int ret = 0;
1319 json_writer_int64(ms->vmdesc, "page_size", qemu_target_page_size());
1320 json_writer_start_array(ms->vmdesc, "devices");
1322 trace_savevm_state_setup();
1323 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1324 if (se->vmsd && se->vmsd->early_setup) {
1325 ret = vmstate_save(f, se, ms->vmdesc, errp);
1326 if (ret) {
1327 migrate_set_error(ms, *errp);
1328 qemu_file_set_error(f, ret);
1329 break;
1331 continue;
1334 if (!se->ops || !se->ops->save_setup) {
1335 continue;
1337 if (se->ops->is_active) {
1338 if (!se->ops->is_active(se->opaque)) {
1339 continue;
1342 save_section_header(f, se, QEMU_VM_SECTION_START);
1344 ret = se->ops->save_setup(f, se->opaque, errp);
1345 save_section_footer(f, se);
1346 if (ret < 0) {
1347 qemu_file_set_error(f, ret);
1348 break;
1352 if (ret) {
1353 return ret;
1356 /* TODO: Should we check that errp is set in case of failure ? */
1357 return precopy_notify(PRECOPY_NOTIFY_SETUP, errp);
1360 int qemu_savevm_state_resume_prepare(MigrationState *s)
1362 SaveStateEntry *se;
1363 int ret;
1365 trace_savevm_state_resume_prepare();
1367 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1368 if (!se->ops || !se->ops->resume_prepare) {
1369 continue;
1371 if (se->ops->is_active) {
1372 if (!se->ops->is_active(se->opaque)) {
1373 continue;
1376 ret = se->ops->resume_prepare(s, se->opaque);
1377 if (ret < 0) {
1378 return ret;
1382 return 0;
1386 * this function has three return values:
1387 * negative: there was one error, and we have -errno.
1388 * 0 : We haven't finished, caller have to go again
1389 * 1 : We have finished, we can go to complete phase
1391 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
1393 SaveStateEntry *se;
1394 bool all_finished = true;
1395 int ret;
1397 trace_savevm_state_iterate();
1398 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1399 if (!se->ops || !se->ops->save_live_iterate) {
1400 continue;
1402 if (se->ops->is_active &&
1403 !se->ops->is_active(se->opaque)) {
1404 continue;
1406 if (se->ops->is_active_iterate &&
1407 !se->ops->is_active_iterate(se->opaque)) {
1408 continue;
1411 * In the postcopy phase, any device that doesn't know how to
1412 * do postcopy should have saved it's state in the _complete
1413 * call that's already run, it might get confused if we call
1414 * iterate afterwards.
1416 if (postcopy &&
1417 !(se->ops->has_postcopy && se->ops->has_postcopy(se->opaque))) {
1418 continue;
1420 if (migration_rate_exceeded(f)) {
1421 return 0;
1423 trace_savevm_section_start(se->idstr, se->section_id);
1425 save_section_header(f, se, QEMU_VM_SECTION_PART);
1427 ret = se->ops->save_live_iterate(f, se->opaque);
1428 trace_savevm_section_end(se->idstr, se->section_id, ret);
1429 save_section_footer(f, se);
1431 if (ret < 0) {
1432 error_report("failed to save SaveStateEntry with id(name): "
1433 "%d(%s): %d",
1434 se->section_id, se->idstr, ret);
1435 qemu_file_set_error(f, ret);
1436 return ret;
1437 } else if (!ret) {
1438 all_finished = false;
1441 return all_finished;
1444 static bool should_send_vmdesc(void)
1446 MachineState *machine = MACHINE(qdev_get_machine());
1447 bool in_postcopy = migration_in_postcopy();
1448 return !machine->suppress_vmdesc && !in_postcopy;
1452 * Calls the save_live_complete_postcopy methods
1453 * causing the last few pages to be sent immediately and doing any associated
1454 * cleanup.
1455 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1456 * all the other devices, but that happens at the point we switch to postcopy.
1458 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1460 SaveStateEntry *se;
1461 int ret;
1463 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1464 if (!se->ops || !se->ops->save_live_complete_postcopy) {
1465 continue;
1467 if (se->ops->is_active) {
1468 if (!se->ops->is_active(se->opaque)) {
1469 continue;
1472 trace_savevm_section_start(se->idstr, se->section_id);
1473 /* Section type */
1474 qemu_put_byte(f, QEMU_VM_SECTION_END);
1475 qemu_put_be32(f, se->section_id);
1477 ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1478 trace_savevm_section_end(se->idstr, se->section_id, ret);
1479 save_section_footer(f, se);
1480 if (ret < 0) {
1481 qemu_file_set_error(f, ret);
1482 return;
1486 qemu_put_byte(f, QEMU_VM_EOF);
1487 qemu_fflush(f);
1490 static
1491 int qemu_savevm_state_complete_precopy_iterable(QEMUFile *f, bool in_postcopy)
1493 int64_t start_ts_each, end_ts_each;
1494 SaveStateEntry *se;
1495 int ret;
1497 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1498 if (!se->ops ||
1499 (in_postcopy && se->ops->has_postcopy &&
1500 se->ops->has_postcopy(se->opaque)) ||
1501 !se->ops->save_live_complete_precopy) {
1502 continue;
1505 if (se->ops->is_active) {
1506 if (!se->ops->is_active(se->opaque)) {
1507 continue;
1511 start_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1512 trace_savevm_section_start(se->idstr, se->section_id);
1514 save_section_header(f, se, QEMU_VM_SECTION_END);
1516 ret = se->ops->save_live_complete_precopy(f, se->opaque);
1517 trace_savevm_section_end(se->idstr, se->section_id, ret);
1518 save_section_footer(f, se);
1519 if (ret < 0) {
1520 qemu_file_set_error(f, ret);
1521 return -1;
1523 end_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1524 trace_vmstate_downtime_save("iterable", se->idstr, se->instance_id,
1525 end_ts_each - start_ts_each);
1528 trace_vmstate_downtime_checkpoint("src-iterable-saved");
1530 return 0;
1533 int qemu_savevm_state_complete_precopy_non_iterable(QEMUFile *f,
1534 bool in_postcopy,
1535 bool inactivate_disks)
1537 MigrationState *ms = migrate_get_current();
1538 int64_t start_ts_each, end_ts_each;
1539 JSONWriter *vmdesc = ms->vmdesc;
1540 int vmdesc_len;
1541 SaveStateEntry *se;
1542 Error *local_err = NULL;
1543 int ret;
1545 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1546 if (se->vmsd && se->vmsd->early_setup) {
1547 /* Already saved during qemu_savevm_state_setup(). */
1548 continue;
1551 start_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1553 ret = vmstate_save(f, se, vmdesc, &local_err);
1554 if (ret) {
1555 migrate_set_error(ms, local_err);
1556 error_report_err(local_err);
1557 qemu_file_set_error(f, ret);
1558 return ret;
1561 end_ts_each = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
1562 trace_vmstate_downtime_save("non-iterable", se->idstr, se->instance_id,
1563 end_ts_each - start_ts_each);
1566 if (inactivate_disks) {
1567 /* Inactivate before sending QEMU_VM_EOF so that the
1568 * bdrv_activate_all() on the other end won't fail. */
1569 ret = bdrv_inactivate_all();
1570 if (ret) {
1571 error_setg(&local_err, "%s: bdrv_inactivate_all() failed (%d)",
1572 __func__, ret);
1573 migrate_set_error(ms, local_err);
1574 error_report_err(local_err);
1575 qemu_file_set_error(f, ret);
1576 return ret;
1579 if (!in_postcopy) {
1580 /* Postcopy stream will still be going */
1581 qemu_put_byte(f, QEMU_VM_EOF);
1584 json_writer_end_array(vmdesc);
1585 json_writer_end_object(vmdesc);
1586 vmdesc_len = strlen(json_writer_get(vmdesc));
1588 if (should_send_vmdesc()) {
1589 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1590 qemu_put_be32(f, vmdesc_len);
1591 qemu_put_buffer(f, (uint8_t *)json_writer_get(vmdesc), vmdesc_len);
1594 /* Free it now to detect any inconsistencies. */
1595 json_writer_free(vmdesc);
1596 ms->vmdesc = NULL;
1598 trace_vmstate_downtime_checkpoint("src-non-iterable-saved");
1600 return 0;
1603 int qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only,
1604 bool inactivate_disks)
1606 int ret;
1607 Error *local_err = NULL;
1608 bool in_postcopy = migration_in_postcopy();
1610 if (precopy_notify(PRECOPY_NOTIFY_COMPLETE, &local_err)) {
1611 error_report_err(local_err);
1614 trace_savevm_state_complete_precopy();
1616 cpu_synchronize_all_states();
1618 if (!in_postcopy || iterable_only) {
1619 ret = qemu_savevm_state_complete_precopy_iterable(f, in_postcopy);
1620 if (ret) {
1621 return ret;
1625 if (iterable_only) {
1626 goto flush;
1629 ret = qemu_savevm_state_complete_precopy_non_iterable(f, in_postcopy,
1630 inactivate_disks);
1631 if (ret) {
1632 return ret;
1635 flush:
1636 return qemu_fflush(f);
1639 /* Give an estimate of the amount left to be transferred,
1640 * the result is split into the amount for units that can and
1641 * for units that can't do postcopy.
1643 void qemu_savevm_state_pending_estimate(uint64_t *must_precopy,
1644 uint64_t *can_postcopy)
1646 SaveStateEntry *se;
1648 *must_precopy = 0;
1649 *can_postcopy = 0;
1651 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1652 if (!se->ops || !se->ops->state_pending_estimate) {
1653 continue;
1655 if (se->ops->is_active) {
1656 if (!se->ops->is_active(se->opaque)) {
1657 continue;
1660 se->ops->state_pending_estimate(se->opaque, must_precopy, can_postcopy);
1664 void qemu_savevm_state_pending_exact(uint64_t *must_precopy,
1665 uint64_t *can_postcopy)
1667 SaveStateEntry *se;
1669 *must_precopy = 0;
1670 *can_postcopy = 0;
1672 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1673 if (!se->ops || !se->ops->state_pending_exact) {
1674 continue;
1676 if (se->ops->is_active) {
1677 if (!se->ops->is_active(se->opaque)) {
1678 continue;
1681 se->ops->state_pending_exact(se->opaque, must_precopy, can_postcopy);
1685 void qemu_savevm_state_cleanup(void)
1687 SaveStateEntry *se;
1688 Error *local_err = NULL;
1690 if (precopy_notify(PRECOPY_NOTIFY_CLEANUP, &local_err)) {
1691 error_report_err(local_err);
1694 trace_savevm_state_cleanup();
1695 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1696 if (se->ops && se->ops->save_cleanup) {
1697 se->ops->save_cleanup(se->opaque);
1702 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1704 int ret;
1705 MigrationState *ms = migrate_get_current();
1706 MigrationStatus status;
1708 if (migration_is_running()) {
1709 error_setg(errp, "There's a migration process in progress");
1710 return -EINVAL;
1713 ret = migrate_init(ms, errp);
1714 if (ret) {
1715 return ret;
1717 ms->to_dst_file = f;
1719 qemu_savevm_state_header(f);
1720 ret = qemu_savevm_state_setup(f, errp);
1721 if (ret) {
1722 goto cleanup;
1725 while (qemu_file_get_error(f) == 0) {
1726 if (qemu_savevm_state_iterate(f, false) > 0) {
1727 break;
1731 ret = qemu_file_get_error(f);
1732 if (ret == 0) {
1733 qemu_savevm_state_complete_precopy(f, false, false);
1734 ret = qemu_file_get_error(f);
1736 if (ret != 0) {
1737 error_setg_errno(errp, -ret, "Error while writing VM state");
1739 cleanup:
1740 qemu_savevm_state_cleanup();
1742 if (ret != 0) {
1743 status = MIGRATION_STATUS_FAILED;
1744 } else {
1745 status = MIGRATION_STATUS_COMPLETED;
1747 migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1749 /* f is outer parameter, it should not stay in global migration state after
1750 * this function finished */
1751 ms->to_dst_file = NULL;
1753 return ret;
1756 void qemu_savevm_live_state(QEMUFile *f)
1758 /* save QEMU_VM_SECTION_END section */
1759 qemu_savevm_state_complete_precopy(f, true, false);
1760 qemu_put_byte(f, QEMU_VM_EOF);
1763 int qemu_save_device_state(QEMUFile *f)
1765 MigrationState *ms = migrate_get_current();
1766 Error *local_err = NULL;
1767 SaveStateEntry *se;
1769 if (!migration_in_colo_state()) {
1770 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1771 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1773 cpu_synchronize_all_states();
1775 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1776 int ret;
1778 if (se->is_ram) {
1779 continue;
1781 ret = vmstate_save(f, se, NULL, &local_err);
1782 if (ret) {
1783 migrate_set_error(ms, local_err);
1784 error_report_err(local_err);
1785 return ret;
1789 qemu_put_byte(f, QEMU_VM_EOF);
1791 return qemu_file_get_error(f);
1794 static SaveStateEntry *find_se(const char *idstr, uint32_t instance_id)
1796 SaveStateEntry *se;
1798 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1799 if (!strcmp(se->idstr, idstr) &&
1800 (instance_id == se->instance_id ||
1801 instance_id == se->alias_id))
1802 return se;
1803 /* Migrating from an older version? */
1804 if (strstr(se->idstr, idstr) && se->compat) {
1805 if (!strcmp(se->compat->idstr, idstr) &&
1806 (instance_id == se->compat->instance_id ||
1807 instance_id == se->alias_id))
1808 return se;
1811 return NULL;
1814 enum LoadVMExitCodes {
1815 /* Allow a command to quit all layers of nested loadvm loops */
1816 LOADVM_QUIT = 1,
1819 /* ------ incoming postcopy messages ------ */
1820 /* 'advise' arrives before any transfers just to tell us that a postcopy
1821 * *might* happen - it might be skipped if precopy transferred everything
1822 * quickly.
1824 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis,
1825 uint16_t len)
1827 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1828 uint64_t remote_pagesize_summary, local_pagesize_summary, remote_tps;
1829 size_t page_size = qemu_target_page_size();
1830 Error *local_err = NULL;
1832 trace_loadvm_postcopy_handle_advise();
1833 if (ps != POSTCOPY_INCOMING_NONE) {
1834 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1835 return -1;
1838 switch (len) {
1839 case 0:
1840 if (migrate_postcopy_ram()) {
1841 error_report("RAM postcopy is enabled but have 0 byte advise");
1842 return -EINVAL;
1844 return 0;
1845 case 8 + 8:
1846 if (!migrate_postcopy_ram()) {
1847 error_report("RAM postcopy is disabled but have 16 byte advise");
1848 return -EINVAL;
1850 break;
1851 default:
1852 error_report("CMD_POSTCOPY_ADVISE invalid length (%d)", len);
1853 return -EINVAL;
1856 if (!postcopy_ram_supported_by_host(mis, &local_err)) {
1857 error_report_err(local_err);
1858 postcopy_state_set(POSTCOPY_INCOMING_NONE);
1859 return -1;
1862 remote_pagesize_summary = qemu_get_be64(mis->from_src_file);
1863 local_pagesize_summary = ram_pagesize_summary();
1865 if (remote_pagesize_summary != local_pagesize_summary) {
1867 * This detects two potential causes of mismatch:
1868 * a) A mismatch in host page sizes
1869 * Some combinations of mismatch are probably possible but it gets
1870 * a bit more complicated. In particular we need to place whole
1871 * host pages on the dest at once, and we need to ensure that we
1872 * handle dirtying to make sure we never end up sending part of
1873 * a hostpage on it's own.
1874 * b) The use of different huge page sizes on source/destination
1875 * a more fine grain test is performed during RAM block migration
1876 * but this test here causes a nice early clear failure, and
1877 * also fails when passed to an older qemu that doesn't
1878 * do huge pages.
1880 error_report("Postcopy needs matching RAM page sizes (s=%" PRIx64
1881 " d=%" PRIx64 ")",
1882 remote_pagesize_summary, local_pagesize_summary);
1883 return -1;
1886 remote_tps = qemu_get_be64(mis->from_src_file);
1887 if (remote_tps != page_size) {
1889 * Again, some differences could be dealt with, but for now keep it
1890 * simple.
1892 error_report("Postcopy needs matching target page sizes (s=%d d=%zd)",
1893 (int)remote_tps, page_size);
1894 return -1;
1897 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_ADVISE, &local_err)) {
1898 error_report_err(local_err);
1899 return -1;
1902 if (ram_postcopy_incoming_init(mis)) {
1903 return -1;
1906 return 0;
1909 /* After postcopy we will be told to throw some pages away since they're
1910 * dirty and will have to be demand fetched. Must happen before CPU is
1911 * started.
1912 * There can be 0..many of these messages, each encoding multiple pages.
1914 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1915 uint16_t len)
1917 int tmp;
1918 char ramid[256];
1919 PostcopyState ps = postcopy_state_get();
1921 trace_loadvm_postcopy_ram_handle_discard();
1923 switch (ps) {
1924 case POSTCOPY_INCOMING_ADVISE:
1925 /* 1st discard */
1926 tmp = postcopy_ram_prepare_discard(mis);
1927 if (tmp) {
1928 return tmp;
1930 break;
1932 case POSTCOPY_INCOMING_DISCARD:
1933 /* Expected state */
1934 break;
1936 default:
1937 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1938 ps);
1939 return -1;
1941 /* We're expecting a
1942 * Version (0)
1943 * a RAM ID string (length byte, name, 0 term)
1944 * then at least 1 16 byte chunk
1946 if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1947 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1948 return -1;
1951 tmp = qemu_get_byte(mis->from_src_file);
1952 if (tmp != postcopy_ram_discard_version) {
1953 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1954 return -1;
1957 if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1958 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1959 return -1;
1961 tmp = qemu_get_byte(mis->from_src_file);
1962 if (tmp != 0) {
1963 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1964 return -1;
1967 len -= 3 + strlen(ramid);
1968 if (len % 16) {
1969 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1970 return -1;
1972 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1973 while (len) {
1974 uint64_t start_addr, block_length;
1975 start_addr = qemu_get_be64(mis->from_src_file);
1976 block_length = qemu_get_be64(mis->from_src_file);
1978 len -= 16;
1979 int ret = ram_discard_range(ramid, start_addr, block_length);
1980 if (ret) {
1981 return ret;
1984 trace_loadvm_postcopy_ram_handle_discard_end();
1986 return 0;
1990 * Triggered by a postcopy_listen command; this thread takes over reading
1991 * the input stream, leaving the main thread free to carry on loading the rest
1992 * of the device state (from RAM).
1993 * (TODO:This could do with being in a postcopy file - but there again it's
1994 * just another input loop, not that postcopy specific)
1996 static void *postcopy_ram_listen_thread(void *opaque)
1998 MigrationIncomingState *mis = migration_incoming_get_current();
1999 QEMUFile *f = mis->from_src_file;
2000 int load_res;
2001 MigrationState *migr = migrate_get_current();
2003 object_ref(OBJECT(migr));
2005 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
2006 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2007 qemu_sem_post(&mis->thread_sync_sem);
2008 trace_postcopy_ram_listen_thread_start();
2010 rcu_register_thread();
2012 * Because we're a thread and not a coroutine we can't yield
2013 * in qemu_file, and thus we must be blocking now.
2015 qemu_file_set_blocking(f, true);
2016 load_res = qemu_loadvm_state_main(f, mis);
2019 * This is tricky, but, mis->from_src_file can change after it
2020 * returns, when postcopy recovery happened. In the future, we may
2021 * want a wrapper for the QEMUFile handle.
2023 f = mis->from_src_file;
2025 /* And non-blocking again so we don't block in any cleanup */
2026 qemu_file_set_blocking(f, false);
2028 trace_postcopy_ram_listen_thread_exit();
2029 if (load_res < 0) {
2030 qemu_file_set_error(f, load_res);
2031 dirty_bitmap_mig_cancel_incoming();
2032 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2033 !migrate_postcopy_ram() && migrate_dirty_bitmaps())
2035 error_report("%s: loadvm failed during postcopy: %d. All states "
2036 "are migrated except dirty bitmaps. Some dirty "
2037 "bitmaps may be lost, and present migrated dirty "
2038 "bitmaps are correctly migrated and valid.",
2039 __func__, load_res);
2040 load_res = 0; /* prevent further exit() */
2041 } else {
2042 error_report("%s: loadvm failed: %d", __func__, load_res);
2043 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2044 MIGRATION_STATUS_FAILED);
2047 if (load_res >= 0) {
2049 * This looks good, but it's possible that the device loading in the
2050 * main thread hasn't finished yet, and so we might not be in 'RUN'
2051 * state yet; wait for the end of the main thread.
2053 qemu_event_wait(&mis->main_thread_load_event);
2055 postcopy_ram_incoming_cleanup(mis);
2057 if (load_res < 0) {
2059 * If something went wrong then we have a bad state so exit;
2060 * depending how far we got it might be possible at this point
2061 * to leave the guest running and fire MCEs for pages that never
2062 * arrived as a desperate recovery step.
2064 rcu_unregister_thread();
2065 exit(EXIT_FAILURE);
2068 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
2069 MIGRATION_STATUS_COMPLETED);
2071 * If everything has worked fine, then the main thread has waited
2072 * for us to start, and we're the last use of the mis.
2073 * (If something broke then qemu will have to exit anyway since it's
2074 * got a bad migration state).
2076 migration_incoming_state_destroy();
2077 qemu_loadvm_state_cleanup();
2079 rcu_unregister_thread();
2080 mis->have_listen_thread = false;
2081 postcopy_state_set(POSTCOPY_INCOMING_END);
2083 object_unref(OBJECT(migr));
2085 return NULL;
2088 /* After this message we must be able to immediately receive postcopy data */
2089 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
2091 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
2092 Error *local_err = NULL;
2094 trace_loadvm_postcopy_handle_listen("enter");
2096 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
2097 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
2098 return -1;
2100 if (ps == POSTCOPY_INCOMING_ADVISE) {
2102 * A rare case, we entered listen without having to do any discards,
2103 * so do the setup that's normally done at the time of the 1st discard.
2105 if (migrate_postcopy_ram()) {
2106 postcopy_ram_prepare_discard(mis);
2110 trace_loadvm_postcopy_handle_listen("after discard");
2113 * Sensitise RAM - can now generate requests for blocks that don't exist
2114 * However, at this point the CPU shouldn't be running, and the IO
2115 * shouldn't be doing anything yet so don't actually expect requests
2117 if (migrate_postcopy_ram()) {
2118 if (postcopy_ram_incoming_setup(mis)) {
2119 postcopy_ram_incoming_cleanup(mis);
2120 return -1;
2124 trace_loadvm_postcopy_handle_listen("after uffd");
2126 if (postcopy_notify(POSTCOPY_NOTIFY_INBOUND_LISTEN, &local_err)) {
2127 error_report_err(local_err);
2128 return -1;
2131 mis->have_listen_thread = true;
2132 postcopy_thread_create(mis, &mis->listen_thread, "postcopy/listen",
2133 postcopy_ram_listen_thread, QEMU_THREAD_DETACHED);
2134 trace_loadvm_postcopy_handle_listen("return");
2136 return 0;
2139 static void loadvm_postcopy_handle_run_bh(void *opaque)
2141 Error *local_err = NULL;
2142 MigrationIncomingState *mis = opaque;
2144 trace_vmstate_downtime_checkpoint("dst-postcopy-bh-enter");
2146 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
2147 * in migration.c
2149 cpu_synchronize_all_post_init();
2151 trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cpu-synced");
2153 qemu_announce_self(&mis->announce_timer, migrate_announce_params());
2155 trace_vmstate_downtime_checkpoint("dst-postcopy-bh-announced");
2157 /* Make sure all file formats throw away their mutable metadata.
2158 * If we get an error here, just don't restart the VM yet. */
2159 bdrv_activate_all(&local_err);
2160 if (local_err) {
2161 error_report_err(local_err);
2162 local_err = NULL;
2163 autostart = false;
2166 trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cache-invalidated");
2168 dirty_bitmap_mig_before_vm_start();
2170 if (autostart) {
2171 /* Hold onto your hats, starting the CPU */
2172 vm_start();
2173 } else {
2174 /* leave it paused and let management decide when to start the CPU */
2175 runstate_set(RUN_STATE_PAUSED);
2178 trace_vmstate_downtime_checkpoint("dst-postcopy-bh-vm-started");
2181 /* After all discards we can start running and asking for pages */
2182 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
2184 PostcopyState ps = postcopy_state_get();
2186 trace_loadvm_postcopy_handle_run();
2187 if (ps != POSTCOPY_INCOMING_LISTENING) {
2188 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
2189 return -1;
2192 postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
2193 migration_bh_schedule(loadvm_postcopy_handle_run_bh, mis);
2195 /* We need to finish reading the stream from the package
2196 * and also stop reading anything more from the stream that loaded the
2197 * package (since it's now being read by the listener thread).
2198 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
2200 return LOADVM_QUIT;
2203 /* We must be with page_request_mutex held */
2204 static gboolean postcopy_sync_page_req(gpointer key, gpointer value,
2205 gpointer data)
2207 MigrationIncomingState *mis = data;
2208 void *host_addr = (void *) key;
2209 ram_addr_t rb_offset;
2210 RAMBlock *rb;
2211 int ret;
2213 rb = qemu_ram_block_from_host(host_addr, true, &rb_offset);
2214 if (!rb) {
2216 * This should _never_ happen. However be nice for a migrating VM to
2217 * not crash/assert. Post an error (note: intended to not use *_once
2218 * because we do want to see all the illegal addresses; and this can
2219 * never be triggered by the guest so we're safe) and move on next.
2221 error_report("%s: illegal host addr %p", __func__, host_addr);
2222 /* Try the next entry */
2223 return FALSE;
2226 ret = migrate_send_rp_message_req_pages(mis, rb, rb_offset);
2227 if (ret) {
2228 /* Please refer to above comment. */
2229 error_report("%s: send rp message failed for addr %p",
2230 __func__, host_addr);
2231 return FALSE;
2234 trace_postcopy_page_req_sync(host_addr);
2236 return FALSE;
2239 static void migrate_send_rp_req_pages_pending(MigrationIncomingState *mis)
2241 WITH_QEMU_LOCK_GUARD(&mis->page_request_mutex) {
2242 g_tree_foreach(mis->page_requested, postcopy_sync_page_req, mis);
2246 static int loadvm_postcopy_handle_resume(MigrationIncomingState *mis)
2248 if (mis->state != MIGRATION_STATUS_POSTCOPY_RECOVER) {
2249 error_report("%s: illegal resume received", __func__);
2250 /* Don't fail the load, only for this. */
2251 return 0;
2255 * Reset the last_rb before we resend any page req to source again, since
2256 * the source should have it reset already.
2258 mis->last_rb = NULL;
2261 * This means source VM is ready to resume the postcopy migration.
2263 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_RECOVER,
2264 MIGRATION_STATUS_POSTCOPY_ACTIVE);
2266 trace_loadvm_postcopy_handle_resume();
2268 /* Tell source that "we are ready" */
2269 migrate_send_rp_resume_ack(mis, MIGRATION_RESUME_ACK_VALUE);
2272 * After a postcopy recovery, the source should have lost the postcopy
2273 * queue, or potentially the requested pages could have been lost during
2274 * the network down phase. Let's re-sync with the source VM by re-sending
2275 * all the pending pages that we eagerly need, so these threads won't get
2276 * blocked too long due to the recovery.
2278 * Without this procedure, the faulted destination VM threads (waiting for
2279 * page requests right before the postcopy is interrupted) can keep hanging
2280 * until the pages are sent by the source during the background copying of
2281 * pages, or another thread faulted on the same address accidentally.
2283 migrate_send_rp_req_pages_pending(mis);
2286 * It's time to switch state and release the fault thread to continue
2287 * service page faults. Note that this should be explicitly after the
2288 * above call to migrate_send_rp_req_pages_pending(). In short:
2289 * migrate_send_rp_message_req_pages() is not thread safe, yet.
2291 qemu_sem_post(&mis->postcopy_pause_sem_fault);
2293 if (migrate_postcopy_preempt()) {
2295 * The preempt channel will be created in async manner, now let's
2296 * wait for it and make sure it's created.
2298 qemu_sem_wait(&mis->postcopy_qemufile_dst_done);
2299 assert(mis->postcopy_qemufile_dst);
2300 /* Kick the fast ram load thread too */
2301 qemu_sem_post(&mis->postcopy_pause_sem_fast_load);
2304 return 0;
2308 * Immediately following this command is a blob of data containing an embedded
2309 * chunk of migration stream; read it and load it.
2311 * @mis: Incoming state
2312 * @length: Length of packaged data to read
2314 * Returns: Negative values on error
2317 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
2319 int ret;
2320 size_t length;
2321 QIOChannelBuffer *bioc;
2323 length = qemu_get_be32(mis->from_src_file);
2324 trace_loadvm_handle_cmd_packaged(length);
2326 if (length > MAX_VM_CMD_PACKAGED_SIZE) {
2327 error_report("Unreasonably large packaged state: %zu", length);
2328 return -1;
2331 bioc = qio_channel_buffer_new(length);
2332 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
2333 ret = qemu_get_buffer(mis->from_src_file,
2334 bioc->data,
2335 length);
2336 if (ret != length) {
2337 object_unref(OBJECT(bioc));
2338 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
2339 ret, length);
2340 return (ret < 0) ? ret : -EAGAIN;
2342 bioc->usage += length;
2343 trace_loadvm_handle_cmd_packaged_received(ret);
2345 QEMUFile *packf = qemu_file_new_input(QIO_CHANNEL(bioc));
2348 * Before loading the guest states, ensure that the preempt channel has
2349 * been ready to use, as some of the states (e.g. via virtio_load) might
2350 * trigger page faults that will be handled through the preempt channel.
2351 * So yield to the main thread in the case that the channel create event
2352 * hasn't been dispatched.
2354 * TODO: if we can move migration loadvm out of main thread, then we
2355 * won't block main thread from polling the accept() fds. We can drop
2356 * this as a whole when that is done.
2358 do {
2359 if (!migrate_postcopy_preempt() || !qemu_in_coroutine() ||
2360 mis->postcopy_qemufile_dst) {
2361 break;
2364 aio_co_schedule(qemu_get_current_aio_context(), qemu_coroutine_self());
2365 qemu_coroutine_yield();
2366 } while (1);
2368 ret = qemu_loadvm_state_main(packf, mis);
2369 trace_loadvm_handle_cmd_packaged_main(ret);
2370 qemu_fclose(packf);
2371 object_unref(OBJECT(bioc));
2373 return ret;
2377 * Handle request that source requests for recved_bitmap on
2378 * destination. Payload format:
2380 * len (1 byte) + ramblock_name (<255 bytes)
2382 static int loadvm_handle_recv_bitmap(MigrationIncomingState *mis,
2383 uint16_t len)
2385 QEMUFile *file = mis->from_src_file;
2386 RAMBlock *rb;
2387 char block_name[256];
2388 size_t cnt;
2390 cnt = qemu_get_counted_string(file, block_name);
2391 if (!cnt) {
2392 error_report("%s: failed to read block name", __func__);
2393 return -EINVAL;
2396 /* Validate before using the data */
2397 if (qemu_file_get_error(file)) {
2398 return qemu_file_get_error(file);
2401 if (len != cnt + 1) {
2402 error_report("%s: invalid payload length (%d)", __func__, len);
2403 return -EINVAL;
2406 rb = qemu_ram_block_by_name(block_name);
2407 if (!rb) {
2408 error_report("%s: block '%s' not found", __func__, block_name);
2409 return -EINVAL;
2412 migrate_send_rp_recv_bitmap(mis, block_name);
2414 trace_loadvm_handle_recv_bitmap(block_name);
2416 return 0;
2419 static int loadvm_process_enable_colo(MigrationIncomingState *mis)
2421 int ret = migration_incoming_enable_colo();
2423 if (!ret) {
2424 ret = colo_init_ram_cache();
2425 if (ret) {
2426 migration_incoming_disable_colo();
2429 return ret;
2433 * Process an incoming 'QEMU_VM_COMMAND'
2434 * 0 just a normal return
2435 * LOADVM_QUIT All good, but exit the loop
2436 * <0 Error
2438 static int loadvm_process_command(QEMUFile *f)
2440 MigrationIncomingState *mis = migration_incoming_get_current();
2441 uint16_t cmd;
2442 uint16_t len;
2443 uint32_t tmp32;
2445 cmd = qemu_get_be16(f);
2446 len = qemu_get_be16(f);
2448 /* Check validity before continue processing of cmds */
2449 if (qemu_file_get_error(f)) {
2450 return qemu_file_get_error(f);
2453 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
2454 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
2455 return -EINVAL;
2458 trace_loadvm_process_command(mig_cmd_args[cmd].name, len);
2460 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
2461 error_report("%s received with bad length - expecting %zu, got %d",
2462 mig_cmd_args[cmd].name,
2463 (size_t)mig_cmd_args[cmd].len, len);
2464 return -ERANGE;
2467 switch (cmd) {
2468 case MIG_CMD_OPEN_RETURN_PATH:
2469 if (mis->to_src_file) {
2470 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
2471 /* Not really a problem, so don't give up */
2472 return 0;
2474 mis->to_src_file = qemu_file_get_return_path(f);
2475 if (!mis->to_src_file) {
2476 error_report("CMD_OPEN_RETURN_PATH failed");
2477 return -1;
2481 * Switchover ack is enabled but no device uses it, so send an ACK to
2482 * source that it's OK to switchover. Do it here, after return path has
2483 * been created.
2485 if (migrate_switchover_ack() && !mis->switchover_ack_pending_num) {
2486 int ret = migrate_send_rp_switchover_ack(mis);
2487 if (ret) {
2488 error_report(
2489 "Could not send switchover ack RP MSG, err %d (%s)", ret,
2490 strerror(-ret));
2491 return ret;
2494 break;
2496 case MIG_CMD_PING:
2497 tmp32 = qemu_get_be32(f);
2498 trace_loadvm_process_command_ping(tmp32);
2499 if (!mis->to_src_file) {
2500 error_report("CMD_PING (0x%x) received with no return path",
2501 tmp32);
2502 return -1;
2504 migrate_send_rp_pong(mis, tmp32);
2505 break;
2507 case MIG_CMD_PACKAGED:
2508 return loadvm_handle_cmd_packaged(mis);
2510 case MIG_CMD_POSTCOPY_ADVISE:
2511 return loadvm_postcopy_handle_advise(mis, len);
2513 case MIG_CMD_POSTCOPY_LISTEN:
2514 return loadvm_postcopy_handle_listen(mis);
2516 case MIG_CMD_POSTCOPY_RUN:
2517 return loadvm_postcopy_handle_run(mis);
2519 case MIG_CMD_POSTCOPY_RAM_DISCARD:
2520 return loadvm_postcopy_ram_handle_discard(mis, len);
2522 case MIG_CMD_POSTCOPY_RESUME:
2523 return loadvm_postcopy_handle_resume(mis);
2525 case MIG_CMD_RECV_BITMAP:
2526 return loadvm_handle_recv_bitmap(mis, len);
2528 case MIG_CMD_ENABLE_COLO:
2529 return loadvm_process_enable_colo(mis);
2532 return 0;
2536 * Read a footer off the wire and check that it matches the expected section
2538 * Returns: true if the footer was good
2539 * false if there is a problem (and calls error_report to say why)
2541 static bool check_section_footer(QEMUFile *f, SaveStateEntry *se)
2543 int ret;
2544 uint8_t read_mark;
2545 uint32_t read_section_id;
2547 if (!migrate_get_current()->send_section_footer) {
2548 /* No footer to check */
2549 return true;
2552 read_mark = qemu_get_byte(f);
2554 ret = qemu_file_get_error(f);
2555 if (ret) {
2556 error_report("%s: Read section footer failed: %d",
2557 __func__, ret);
2558 return false;
2561 if (read_mark != QEMU_VM_SECTION_FOOTER) {
2562 error_report("Missing section footer for %s", se->idstr);
2563 return false;
2566 read_section_id = qemu_get_be32(f);
2567 if (read_section_id != se->load_section_id) {
2568 error_report("Mismatched section id in footer for %s -"
2569 " read 0x%x expected 0x%x",
2570 se->idstr, read_section_id, se->load_section_id);
2571 return false;
2574 /* All good */
2575 return true;
2578 static int
2579 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis,
2580 uint8_t type)
2582 bool trace_downtime = (type == QEMU_VM_SECTION_FULL);
2583 uint32_t instance_id, version_id, section_id;
2584 int64_t start_ts, end_ts;
2585 SaveStateEntry *se;
2586 char idstr[256];
2587 int ret;
2589 /* Read section start */
2590 section_id = qemu_get_be32(f);
2591 if (!qemu_get_counted_string(f, idstr)) {
2592 error_report("Unable to read ID string for section %u",
2593 section_id);
2594 return -EINVAL;
2596 instance_id = qemu_get_be32(f);
2597 version_id = qemu_get_be32(f);
2599 ret = qemu_file_get_error(f);
2600 if (ret) {
2601 error_report("%s: Failed to read instance/version ID: %d",
2602 __func__, ret);
2603 return ret;
2606 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
2607 instance_id, version_id);
2608 /* Find savevm section */
2609 se = find_se(idstr, instance_id);
2610 if (se == NULL) {
2611 error_report("Unknown savevm section or instance '%s' %"PRIu32". "
2612 "Make sure that your current VM setup matches your "
2613 "saved VM setup, including any hotplugged devices",
2614 idstr, instance_id);
2615 return -EINVAL;
2618 /* Validate version */
2619 if (version_id > se->version_id) {
2620 error_report("savevm: unsupported version %d for '%s' v%d",
2621 version_id, idstr, se->version_id);
2622 return -EINVAL;
2624 se->load_version_id = version_id;
2625 se->load_section_id = section_id;
2627 /* Validate if it is a device's state */
2628 if (xen_enabled() && se->is_ram) {
2629 error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
2630 return -EINVAL;
2633 if (trace_downtime) {
2634 start_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2637 ret = vmstate_load(f, se);
2638 if (ret < 0) {
2639 error_report("error while loading state for instance 0x%"PRIx32" of"
2640 " device '%s'", instance_id, idstr);
2641 return ret;
2644 if (trace_downtime) {
2645 end_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2646 trace_vmstate_downtime_load("non-iterable", se->idstr,
2647 se->instance_id, end_ts - start_ts);
2650 if (!check_section_footer(f, se)) {
2651 return -EINVAL;
2654 return 0;
2657 static int
2658 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis,
2659 uint8_t type)
2661 bool trace_downtime = (type == QEMU_VM_SECTION_END);
2662 int64_t start_ts, end_ts;
2663 uint32_t section_id;
2664 SaveStateEntry *se;
2665 int ret;
2667 section_id = qemu_get_be32(f);
2669 ret = qemu_file_get_error(f);
2670 if (ret) {
2671 error_report("%s: Failed to read section ID: %d",
2672 __func__, ret);
2673 return ret;
2676 trace_qemu_loadvm_state_section_partend(section_id);
2677 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2678 if (se->load_section_id == section_id) {
2679 break;
2682 if (se == NULL) {
2683 error_report("Unknown savevm section %d", section_id);
2684 return -EINVAL;
2687 if (trace_downtime) {
2688 start_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2691 ret = vmstate_load(f, se);
2692 if (ret < 0) {
2693 error_report("error while loading state section id %d(%s)",
2694 section_id, se->idstr);
2695 return ret;
2698 if (trace_downtime) {
2699 end_ts = qemu_clock_get_us(QEMU_CLOCK_REALTIME);
2700 trace_vmstate_downtime_load("iterable", se->idstr,
2701 se->instance_id, end_ts - start_ts);
2704 if (!check_section_footer(f, se)) {
2705 return -EINVAL;
2708 return 0;
2711 static int qemu_loadvm_state_header(QEMUFile *f)
2713 unsigned int v;
2714 int ret;
2716 v = qemu_get_be32(f);
2717 if (v != QEMU_VM_FILE_MAGIC) {
2718 error_report("Not a migration stream");
2719 return -EINVAL;
2722 v = qemu_get_be32(f);
2723 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2724 error_report("SaveVM v2 format is obsolete and don't work anymore");
2725 return -ENOTSUP;
2727 if (v != QEMU_VM_FILE_VERSION) {
2728 error_report("Unsupported migration stream version");
2729 return -ENOTSUP;
2732 if (migrate_get_current()->send_configuration) {
2733 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
2734 error_report("Configuration section missing");
2735 qemu_loadvm_state_cleanup();
2736 return -EINVAL;
2738 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
2740 if (ret) {
2741 qemu_loadvm_state_cleanup();
2742 return ret;
2745 return 0;
2748 static void qemu_loadvm_state_switchover_ack_needed(MigrationIncomingState *mis)
2750 SaveStateEntry *se;
2752 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2753 if (!se->ops || !se->ops->switchover_ack_needed) {
2754 continue;
2757 if (se->ops->switchover_ack_needed(se->opaque)) {
2758 mis->switchover_ack_pending_num++;
2762 trace_loadvm_state_switchover_ack_needed(mis->switchover_ack_pending_num);
2765 static int qemu_loadvm_state_setup(QEMUFile *f, Error **errp)
2767 ERRP_GUARD();
2768 SaveStateEntry *se;
2769 int ret;
2771 trace_loadvm_state_setup();
2772 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2773 if (!se->ops || !se->ops->load_setup) {
2774 continue;
2776 if (se->ops->is_active) {
2777 if (!se->ops->is_active(se->opaque)) {
2778 continue;
2782 ret = se->ops->load_setup(f, se->opaque, errp);
2783 if (ret < 0) {
2784 error_prepend(errp, "Load state of device %s failed: ",
2785 se->idstr);
2786 qemu_file_set_error(f, ret);
2787 return ret;
2790 return 0;
2793 void qemu_loadvm_state_cleanup(void)
2795 SaveStateEntry *se;
2797 trace_loadvm_state_cleanup();
2798 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
2799 if (se->ops && se->ops->load_cleanup) {
2800 se->ops->load_cleanup(se->opaque);
2805 /* Return true if we should continue the migration, or false. */
2806 static bool postcopy_pause_incoming(MigrationIncomingState *mis)
2808 int i;
2810 trace_postcopy_pause_incoming();
2812 assert(migrate_postcopy_ram());
2815 * Unregister yank with either from/to src would work, since ioc behind it
2816 * is the same
2818 migration_ioc_unregister_yank_from_file(mis->from_src_file);
2820 assert(mis->from_src_file);
2821 qemu_file_shutdown(mis->from_src_file);
2822 qemu_fclose(mis->from_src_file);
2823 mis->from_src_file = NULL;
2825 assert(mis->to_src_file);
2826 qemu_file_shutdown(mis->to_src_file);
2827 qemu_mutex_lock(&mis->rp_mutex);
2828 qemu_fclose(mis->to_src_file);
2829 mis->to_src_file = NULL;
2830 qemu_mutex_unlock(&mis->rp_mutex);
2833 * NOTE: this must happen before reset the PostcopyTmpPages below,
2834 * otherwise it's racy to reset those fields when the fast load thread
2835 * can be accessing it in parallel.
2837 if (mis->postcopy_qemufile_dst) {
2838 qemu_file_shutdown(mis->postcopy_qemufile_dst);
2839 /* Take the mutex to make sure the fast ram load thread halted */
2840 qemu_mutex_lock(&mis->postcopy_prio_thread_mutex);
2841 migration_ioc_unregister_yank_from_file(mis->postcopy_qemufile_dst);
2842 qemu_fclose(mis->postcopy_qemufile_dst);
2843 mis->postcopy_qemufile_dst = NULL;
2844 qemu_mutex_unlock(&mis->postcopy_prio_thread_mutex);
2847 /* Current state can be either ACTIVE or RECOVER */
2848 migrate_set_state(&mis->state, mis->state,
2849 MIGRATION_STATUS_POSTCOPY_PAUSED);
2851 /* Notify the fault thread for the invalidated file handle */
2852 postcopy_fault_thread_notify(mis);
2855 * If network is interrupted, any temp page we received will be useless
2856 * because we didn't mark them as "received" in receivedmap. After a
2857 * proper recovery later (which will sync src dirty bitmap with receivedmap
2858 * on dest) these cached small pages will be resent again.
2860 for (i = 0; i < mis->postcopy_channels; i++) {
2861 postcopy_temp_page_reset(&mis->postcopy_tmp_pages[i]);
2864 error_report("Detected IO failure for postcopy. "
2865 "Migration paused.");
2867 while (mis->state == MIGRATION_STATUS_POSTCOPY_PAUSED) {
2868 qemu_sem_wait(&mis->postcopy_pause_sem_dst);
2871 trace_postcopy_pause_incoming_continued();
2873 return true;
2876 int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
2878 uint8_t section_type;
2879 int ret = 0;
2881 retry:
2882 while (true) {
2883 section_type = qemu_get_byte(f);
2885 ret = qemu_file_get_error_obj_any(f, mis->postcopy_qemufile_dst, NULL);
2886 if (ret) {
2887 break;
2890 trace_qemu_loadvm_state_section(section_type);
2891 switch (section_type) {
2892 case QEMU_VM_SECTION_START:
2893 case QEMU_VM_SECTION_FULL:
2894 ret = qemu_loadvm_section_start_full(f, mis, section_type);
2895 if (ret < 0) {
2896 goto out;
2898 break;
2899 case QEMU_VM_SECTION_PART:
2900 case QEMU_VM_SECTION_END:
2901 ret = qemu_loadvm_section_part_end(f, mis, section_type);
2902 if (ret < 0) {
2903 goto out;
2905 break;
2906 case QEMU_VM_COMMAND:
2907 ret = loadvm_process_command(f);
2908 trace_qemu_loadvm_state_section_command(ret);
2909 if ((ret < 0) || (ret == LOADVM_QUIT)) {
2910 goto out;
2912 break;
2913 case QEMU_VM_EOF:
2914 /* This is the end of migration */
2915 goto out;
2916 default:
2917 error_report("Unknown savevm section type %d", section_type);
2918 ret = -EINVAL;
2919 goto out;
2923 out:
2924 if (ret < 0) {
2925 qemu_file_set_error(f, ret);
2927 /* Cancel bitmaps incoming regardless of recovery */
2928 dirty_bitmap_mig_cancel_incoming();
2931 * If we are during an active postcopy, then we pause instead
2932 * of bail out to at least keep the VM's dirty data. Note
2933 * that POSTCOPY_INCOMING_LISTENING stage is still not enough,
2934 * during which we're still receiving device states and we
2935 * still haven't yet started the VM on destination.
2937 * Only RAM postcopy supports recovery. Still, if RAM postcopy is
2938 * enabled, canceled bitmaps postcopy will not affect RAM postcopy
2939 * recovering.
2941 if (postcopy_state_get() == POSTCOPY_INCOMING_RUNNING &&
2942 migrate_postcopy_ram() && postcopy_pause_incoming(mis)) {
2943 /* Reset f to point to the newly created channel */
2944 f = mis->from_src_file;
2945 goto retry;
2948 return ret;
2951 int qemu_loadvm_state(QEMUFile *f)
2953 MigrationIncomingState *mis = migration_incoming_get_current();
2954 Error *local_err = NULL;
2955 int ret;
2957 if (qemu_savevm_state_blocked(&local_err)) {
2958 error_report_err(local_err);
2959 return -EINVAL;
2962 ret = qemu_loadvm_state_header(f);
2963 if (ret) {
2964 return ret;
2967 if (qemu_loadvm_state_setup(f, &local_err) != 0) {
2968 error_report_err(local_err);
2969 return -EINVAL;
2972 if (migrate_switchover_ack()) {
2973 qemu_loadvm_state_switchover_ack_needed(mis);
2976 cpu_synchronize_all_pre_loadvm();
2978 ret = qemu_loadvm_state_main(f, mis);
2979 qemu_event_set(&mis->main_thread_load_event);
2981 trace_qemu_loadvm_state_post_main(ret);
2983 if (mis->have_listen_thread) {
2984 /* Listen thread still going, can't clean up yet */
2985 return ret;
2988 if (ret == 0) {
2989 ret = qemu_file_get_error(f);
2993 * Try to read in the VMDESC section as well, so that dumping tools that
2994 * intercept our migration stream have the chance to see it.
2997 /* We've got to be careful; if we don't read the data and just shut the fd
2998 * then the sender can error if we close while it's still sending.
2999 * We also mustn't read data that isn't there; some transports (RDMA)
3000 * will stall waiting for that data when the source has already closed.
3002 if (ret == 0 && should_send_vmdesc()) {
3003 uint8_t *buf;
3004 uint32_t size;
3005 uint8_t section_type = qemu_get_byte(f);
3007 if (section_type != QEMU_VM_VMDESCRIPTION) {
3008 error_report("Expected vmdescription section, but got %d",
3009 section_type);
3011 * It doesn't seem worth failing at this point since
3012 * we apparently have an otherwise valid VM state
3014 } else {
3015 buf = g_malloc(0x1000);
3016 size = qemu_get_be32(f);
3018 while (size > 0) {
3019 uint32_t read_chunk = MIN(size, 0x1000);
3020 qemu_get_buffer(f, buf, read_chunk);
3021 size -= read_chunk;
3023 g_free(buf);
3027 qemu_loadvm_state_cleanup();
3028 cpu_synchronize_all_post_init();
3030 return ret;
3033 int qemu_load_device_state(QEMUFile *f)
3035 MigrationIncomingState *mis = migration_incoming_get_current();
3036 int ret;
3038 /* Load QEMU_VM_SECTION_FULL section */
3039 ret = qemu_loadvm_state_main(f, mis);
3040 if (ret < 0) {
3041 error_report("Failed to load device state: %d", ret);
3042 return ret;
3045 cpu_synchronize_all_post_init();
3046 return 0;
3049 int qemu_loadvm_approve_switchover(void)
3051 MigrationIncomingState *mis = migration_incoming_get_current();
3053 if (!mis->switchover_ack_pending_num) {
3054 return -EINVAL;
3057 mis->switchover_ack_pending_num--;
3058 trace_loadvm_approve_switchover(mis->switchover_ack_pending_num);
3060 if (mis->switchover_ack_pending_num) {
3061 return 0;
3064 return migrate_send_rp_switchover_ack(mis);
3067 bool save_snapshot(const char *name, bool overwrite, const char *vmstate,
3068 bool has_devices, strList *devices, Error **errp)
3070 BlockDriverState *bs;
3071 QEMUSnapshotInfo sn1, *sn = &sn1;
3072 int ret = -1, ret2;
3073 QEMUFile *f;
3074 RunState saved_state = runstate_get();
3075 uint64_t vm_state_size;
3076 g_autoptr(GDateTime) now = g_date_time_new_now_local();
3078 GLOBAL_STATE_CODE();
3080 if (migration_is_blocked(errp)) {
3081 return false;
3084 if (!replay_can_snapshot()) {
3085 error_setg(errp, "Record/replay does not allow making snapshot "
3086 "right now. Try once more later.");
3087 return false;
3090 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3091 return false;
3094 /* Delete old snapshots of the same name */
3095 if (name) {
3096 if (overwrite) {
3097 if (bdrv_all_delete_snapshot(name, has_devices,
3098 devices, errp) < 0) {
3099 return false;
3101 } else {
3102 ret2 = bdrv_all_has_snapshot(name, has_devices, devices, errp);
3103 if (ret2 < 0) {
3104 return false;
3106 if (ret2 == 1) {
3107 error_setg(errp,
3108 "Snapshot '%s' already exists in one or more devices",
3109 name);
3110 return false;
3115 bs = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
3116 if (bs == NULL) {
3117 return false;
3120 global_state_store();
3121 vm_stop(RUN_STATE_SAVE_VM);
3123 bdrv_drain_all_begin();
3125 memset(sn, 0, sizeof(*sn));
3127 /* fill auxiliary fields */
3128 sn->date_sec = g_date_time_to_unix(now);
3129 sn->date_nsec = g_date_time_get_microsecond(now) * 1000;
3130 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
3131 if (replay_mode != REPLAY_MODE_NONE) {
3132 sn->icount = replay_get_current_icount();
3133 } else {
3134 sn->icount = -1ULL;
3137 if (name) {
3138 pstrcpy(sn->name, sizeof(sn->name), name);
3139 } else {
3140 g_autofree char *autoname = g_date_time_format(now, "vm-%Y%m%d%H%M%S");
3141 pstrcpy(sn->name, sizeof(sn->name), autoname);
3144 /* save the VM state */
3145 f = qemu_fopen_bdrv(bs, 1);
3146 if (!f) {
3147 error_setg(errp, "Could not open VM state file");
3148 goto the_end;
3150 ret = qemu_savevm_state(f, errp);
3151 vm_state_size = qemu_file_transferred(f);
3152 ret2 = qemu_fclose(f);
3153 if (ret < 0) {
3154 goto the_end;
3156 if (ret2 < 0) {
3157 ret = ret2;
3158 goto the_end;
3161 ret = bdrv_all_create_snapshot(sn, bs, vm_state_size,
3162 has_devices, devices, errp);
3163 if (ret < 0) {
3164 bdrv_all_delete_snapshot(sn->name, has_devices, devices, NULL);
3165 goto the_end;
3168 ret = 0;
3170 the_end:
3171 bdrv_drain_all_end();
3173 vm_resume(saved_state);
3174 return ret == 0;
3177 void qmp_xen_save_devices_state(const char *filename, bool has_live, bool live,
3178 Error **errp)
3180 QEMUFile *f;
3181 QIOChannelFile *ioc;
3182 int saved_vm_running;
3183 int ret;
3185 if (!has_live) {
3186 /* live default to true so old version of Xen tool stack can have a
3187 * successful live migration */
3188 live = true;
3191 saved_vm_running = runstate_is_running();
3192 vm_stop(RUN_STATE_SAVE_VM);
3193 global_state_store_running();
3195 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT | O_TRUNC,
3196 0660, errp);
3197 if (!ioc) {
3198 goto the_end;
3200 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
3201 f = qemu_file_new_output(QIO_CHANNEL(ioc));
3202 object_unref(OBJECT(ioc));
3203 ret = qemu_save_device_state(f);
3204 if (ret < 0 || qemu_fclose(f) < 0) {
3205 error_setg(errp, "saving Xen device state failed");
3206 } else {
3207 /* libxl calls the QMP command "stop" before calling
3208 * "xen-save-devices-state" and in case of migration failure, libxl
3209 * would call "cont".
3210 * So call bdrv_inactivate_all (release locks) here to let the other
3211 * side of the migration take control of the images.
3213 if (live && !saved_vm_running) {
3214 ret = bdrv_inactivate_all();
3215 if (ret) {
3216 error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
3217 __func__, ret);
3222 the_end:
3223 if (saved_vm_running) {
3224 vm_start();
3228 void qmp_xen_load_devices_state(const char *filename, Error **errp)
3230 QEMUFile *f;
3231 QIOChannelFile *ioc;
3232 int ret;
3234 /* Guest must be paused before loading the device state; the RAM state
3235 * will already have been loaded by xc
3237 if (runstate_is_running()) {
3238 error_setg(errp, "Cannot update device state while vm is running");
3239 return;
3241 vm_stop(RUN_STATE_RESTORE_VM);
3243 ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
3244 if (!ioc) {
3245 return;
3247 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
3248 f = qemu_file_new_input(QIO_CHANNEL(ioc));
3249 object_unref(OBJECT(ioc));
3251 ret = qemu_loadvm_state(f);
3252 qemu_fclose(f);
3253 if (ret < 0) {
3254 error_setg(errp, "loading Xen device state failed");
3256 migration_incoming_state_destroy();
3259 bool load_snapshot(const char *name, const char *vmstate,
3260 bool has_devices, strList *devices, Error **errp)
3262 BlockDriverState *bs_vm_state;
3263 QEMUSnapshotInfo sn;
3264 QEMUFile *f;
3265 int ret;
3266 MigrationIncomingState *mis = migration_incoming_get_current();
3268 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3269 return false;
3271 ret = bdrv_all_has_snapshot(name, has_devices, devices, errp);
3272 if (ret < 0) {
3273 return false;
3275 if (ret == 0) {
3276 error_setg(errp, "Snapshot '%s' does not exist in one or more devices",
3277 name);
3278 return false;
3281 bs_vm_state = bdrv_all_find_vmstate_bs(vmstate, has_devices, devices, errp);
3282 if (!bs_vm_state) {
3283 return false;
3286 /* Don't even try to load empty VM states */
3287 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
3288 if (ret < 0) {
3289 return false;
3290 } else if (sn.vm_state_size == 0) {
3291 error_setg(errp, "This is a disk-only snapshot. Revert to it "
3292 " offline using qemu-img");
3293 return false;
3297 * Flush the record/replay queue. Now the VM state is going
3298 * to change. Therefore we don't need to preserve its consistency
3300 replay_flush_events();
3302 /* Flush all IO requests so they don't interfere with the new state. */
3303 bdrv_drain_all_begin();
3305 ret = bdrv_all_goto_snapshot(name, has_devices, devices, errp);
3306 if (ret < 0) {
3307 goto err_drain;
3310 /* restore the VM state */
3311 f = qemu_fopen_bdrv(bs_vm_state, 0);
3312 if (!f) {
3313 error_setg(errp, "Could not open VM state file");
3314 goto err_drain;
3317 qemu_system_reset(SHUTDOWN_CAUSE_SNAPSHOT_LOAD);
3318 mis->from_src_file = f;
3320 if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
3321 ret = -EINVAL;
3322 goto err_drain;
3324 ret = qemu_loadvm_state(f);
3325 migration_incoming_state_destroy();
3327 bdrv_drain_all_end();
3329 if (ret < 0) {
3330 error_setg(errp, "Error %d while loading VM state", ret);
3331 return false;
3334 return true;
3336 err_drain:
3337 bdrv_drain_all_end();
3338 return false;
3341 void load_snapshot_resume(RunState state)
3343 vm_resume(state);
3344 if (state == RUN_STATE_RUNNING && runstate_get() == RUN_STATE_SUSPENDED) {
3345 qemu_system_wakeup_request(QEMU_WAKEUP_REASON_OTHER, &error_abort);
3349 bool delete_snapshot(const char *name, bool has_devices,
3350 strList *devices, Error **errp)
3352 if (!bdrv_all_can_snapshot(has_devices, devices, errp)) {
3353 return false;
3356 if (bdrv_all_delete_snapshot(name, has_devices, devices, errp) < 0) {
3357 return false;
3360 return true;
3363 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
3365 qemu_ram_set_idstr(mr->ram_block,
3366 memory_region_name(mr), dev);
3367 qemu_ram_set_migratable(mr->ram_block);
3370 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
3372 qemu_ram_unset_idstr(mr->ram_block);
3373 qemu_ram_unset_migratable(mr->ram_block);
3376 void vmstate_register_ram_global(MemoryRegion *mr)
3378 vmstate_register_ram(mr, NULL);
3381 bool vmstate_check_only_migratable(const VMStateDescription *vmsd)
3383 /* check needed if --only-migratable is specified */
3384 if (!only_migratable) {
3385 return true;
3388 return !(vmsd && vmsd->unmigratable);
3391 typedef struct SnapshotJob {
3392 Job common;
3393 char *tag;
3394 char *vmstate;
3395 strList *devices;
3396 Coroutine *co;
3397 Error **errp;
3398 bool ret;
3399 } SnapshotJob;
3401 static void qmp_snapshot_job_free(SnapshotJob *s)
3403 g_free(s->tag);
3404 g_free(s->vmstate);
3405 qapi_free_strList(s->devices);
3409 static void snapshot_load_job_bh(void *opaque)
3411 Job *job = opaque;
3412 SnapshotJob *s = container_of(job, SnapshotJob, common);
3413 RunState orig_state = runstate_get();
3415 job_progress_set_remaining(&s->common, 1);
3417 vm_stop(RUN_STATE_RESTORE_VM);
3419 s->ret = load_snapshot(s->tag, s->vmstate, true, s->devices, s->errp);
3420 if (s->ret) {
3421 load_snapshot_resume(orig_state);
3424 job_progress_update(&s->common, 1);
3426 qmp_snapshot_job_free(s);
3427 aio_co_wake(s->co);
3430 static void snapshot_save_job_bh(void *opaque)
3432 Job *job = opaque;
3433 SnapshotJob *s = container_of(job, SnapshotJob, common);
3435 job_progress_set_remaining(&s->common, 1);
3436 s->ret = save_snapshot(s->tag, false, s->vmstate,
3437 true, s->devices, s->errp);
3438 job_progress_update(&s->common, 1);
3440 qmp_snapshot_job_free(s);
3441 aio_co_wake(s->co);
3444 static void snapshot_delete_job_bh(void *opaque)
3446 Job *job = opaque;
3447 SnapshotJob *s = container_of(job, SnapshotJob, common);
3449 job_progress_set_remaining(&s->common, 1);
3450 s->ret = delete_snapshot(s->tag, true, s->devices, s->errp);
3451 job_progress_update(&s->common, 1);
3453 qmp_snapshot_job_free(s);
3454 aio_co_wake(s->co);
3457 static int coroutine_fn snapshot_save_job_run(Job *job, Error **errp)
3459 SnapshotJob *s = container_of(job, SnapshotJob, common);
3460 s->errp = errp;
3461 s->co = qemu_coroutine_self();
3462 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3463 snapshot_save_job_bh, job);
3464 qemu_coroutine_yield();
3465 return s->ret ? 0 : -1;
3468 static int coroutine_fn snapshot_load_job_run(Job *job, Error **errp)
3470 SnapshotJob *s = container_of(job, SnapshotJob, common);
3471 s->errp = errp;
3472 s->co = qemu_coroutine_self();
3473 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3474 snapshot_load_job_bh, job);
3475 qemu_coroutine_yield();
3476 return s->ret ? 0 : -1;
3479 static int coroutine_fn snapshot_delete_job_run(Job *job, Error **errp)
3481 SnapshotJob *s = container_of(job, SnapshotJob, common);
3482 s->errp = errp;
3483 s->co = qemu_coroutine_self();
3484 aio_bh_schedule_oneshot(qemu_get_aio_context(),
3485 snapshot_delete_job_bh, job);
3486 qemu_coroutine_yield();
3487 return s->ret ? 0 : -1;
3491 static const JobDriver snapshot_load_job_driver = {
3492 .instance_size = sizeof(SnapshotJob),
3493 .job_type = JOB_TYPE_SNAPSHOT_LOAD,
3494 .run = snapshot_load_job_run,
3497 static const JobDriver snapshot_save_job_driver = {
3498 .instance_size = sizeof(SnapshotJob),
3499 .job_type = JOB_TYPE_SNAPSHOT_SAVE,
3500 .run = snapshot_save_job_run,
3503 static const JobDriver snapshot_delete_job_driver = {
3504 .instance_size = sizeof(SnapshotJob),
3505 .job_type = JOB_TYPE_SNAPSHOT_DELETE,
3506 .run = snapshot_delete_job_run,
3510 void qmp_snapshot_save(const char *job_id,
3511 const char *tag,
3512 const char *vmstate,
3513 strList *devices,
3514 Error **errp)
3516 SnapshotJob *s;
3518 s = job_create(job_id, &snapshot_save_job_driver, NULL,
3519 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3520 NULL, NULL, errp);
3521 if (!s) {
3522 return;
3525 s->tag = g_strdup(tag);
3526 s->vmstate = g_strdup(vmstate);
3527 s->devices = QAPI_CLONE(strList, devices);
3529 job_start(&s->common);
3532 void qmp_snapshot_load(const char *job_id,
3533 const char *tag,
3534 const char *vmstate,
3535 strList *devices,
3536 Error **errp)
3538 SnapshotJob *s;
3540 s = job_create(job_id, &snapshot_load_job_driver, NULL,
3541 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3542 NULL, NULL, errp);
3543 if (!s) {
3544 return;
3547 s->tag = g_strdup(tag);
3548 s->vmstate = g_strdup(vmstate);
3549 s->devices = QAPI_CLONE(strList, devices);
3551 job_start(&s->common);
3554 void qmp_snapshot_delete(const char *job_id,
3555 const char *tag,
3556 strList *devices,
3557 Error **errp)
3559 SnapshotJob *s;
3561 s = job_create(job_id, &snapshot_delete_job_driver, NULL,
3562 qemu_get_aio_context(), JOB_MANUAL_DISMISS,
3563 NULL, NULL, errp);
3564 if (!s) {
3565 return;
3568 s->tag = g_strdup(tag);
3569 s->devices = QAPI_CLONE(strList, devices);
3571 job_start(&s->common);