pcie_aer: Convert pcie_aer_init to Error
[qemu/ar7.git] / migration / savevm.c
blobf9c06e9f96ef661e1437058858894904a6fc560a
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 "cpu.h"
31 #include "hw/boards.h"
32 #include "hw/hw.h"
33 #include "hw/qdev.h"
34 #include "hw/xen/xen.h"
35 #include "net/net.h"
36 #include "monitor/monitor.h"
37 #include "sysemu/sysemu.h"
38 #include "qemu/timer.h"
39 #include "audio/audio.h"
40 #include "migration/migration.h"
41 #include "migration/postcopy-ram.h"
42 #include "qapi/qmp/qerror.h"
43 #include "qemu/error-report.h"
44 #include "qemu/sockets.h"
45 #include "qemu/queue.h"
46 #include "sysemu/cpus.h"
47 #include "exec/memory.h"
48 #include "qmp-commands.h"
49 #include "trace.h"
50 #include "qemu/bitops.h"
51 #include "qemu/iov.h"
52 #include "block/snapshot.h"
53 #include "block/qapi.h"
54 #include "qemu/cutils.h"
55 #include "io/channel-buffer.h"
56 #include "io/channel-file.h"
58 #ifndef ETH_P_RARP
59 #define ETH_P_RARP 0x8035
60 #endif
61 #define ARP_HTYPE_ETH 0x0001
62 #define ARP_PTYPE_IP 0x0800
63 #define ARP_OP_REQUEST_REV 0x3
65 const unsigned int postcopy_ram_discard_version = 0;
67 static bool skip_section_footers;
69 static struct mig_cmd_args {
70 ssize_t len; /* -1 = variable */
71 const char *name;
72 } mig_cmd_args[] = {
73 [MIG_CMD_INVALID] = { .len = -1, .name = "INVALID" },
74 [MIG_CMD_OPEN_RETURN_PATH] = { .len = 0, .name = "OPEN_RETURN_PATH" },
75 [MIG_CMD_PING] = { .len = sizeof(uint32_t), .name = "PING" },
76 [MIG_CMD_POSTCOPY_ADVISE] = { .len = 16, .name = "POSTCOPY_ADVISE" },
77 [MIG_CMD_POSTCOPY_LISTEN] = { .len = 0, .name = "POSTCOPY_LISTEN" },
78 [MIG_CMD_POSTCOPY_RUN] = { .len = 0, .name = "POSTCOPY_RUN" },
79 [MIG_CMD_POSTCOPY_RAM_DISCARD] = {
80 .len = -1, .name = "POSTCOPY_RAM_DISCARD" },
81 [MIG_CMD_PACKAGED] = { .len = 4, .name = "PACKAGED" },
82 [MIG_CMD_MAX] = { .len = -1, .name = "MAX" },
85 static int announce_self_create(uint8_t *buf,
86 uint8_t *mac_addr)
88 /* Ethernet header. */
89 memset(buf, 0xff, 6); /* destination MAC addr */
90 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
91 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
93 /* RARP header. */
94 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
95 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
96 *(buf + 18) = 6; /* hardware addr length (ethernet) */
97 *(buf + 19) = 4; /* protocol addr length (IPv4) */
98 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
99 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
100 memset(buf + 28, 0x00, 4); /* source protocol addr */
101 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
102 memset(buf + 38, 0x00, 4); /* target protocol addr */
104 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
105 memset(buf + 42, 0x00, 18);
107 return 60; /* len (FCS will be added by hardware) */
110 static void qemu_announce_self_iter(NICState *nic, void *opaque)
112 uint8_t buf[60];
113 int len;
115 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic->conf->macaddr));
116 len = announce_self_create(buf, nic->conf->macaddr.a);
118 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
122 static void qemu_announce_self_once(void *opaque)
124 static int count = SELF_ANNOUNCE_ROUNDS;
125 QEMUTimer *timer = *(QEMUTimer **)opaque;
127 qemu_foreach_nic(qemu_announce_self_iter, NULL);
129 if (--count) {
130 /* delay 50ms, 150ms, 250ms, ... */
131 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
132 self_announce_delay(count));
133 } else {
134 timer_del(timer);
135 timer_free(timer);
139 void qemu_announce_self(void)
141 static QEMUTimer *timer;
142 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
143 qemu_announce_self_once(&timer);
146 /***********************************************************/
147 /* savevm/loadvm support */
149 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
150 int64_t pos)
152 int ret;
153 QEMUIOVector qiov;
155 qemu_iovec_init_external(&qiov, iov, iovcnt);
156 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
157 if (ret < 0) {
158 return ret;
161 return qiov.size;
164 static ssize_t block_get_buffer(void *opaque, uint8_t *buf, int64_t pos,
165 size_t size)
167 return bdrv_load_vmstate(opaque, buf, pos, size);
170 static int bdrv_fclose(void *opaque)
172 return bdrv_flush(opaque);
175 static const QEMUFileOps bdrv_read_ops = {
176 .get_buffer = block_get_buffer,
177 .close = bdrv_fclose
180 static const QEMUFileOps bdrv_write_ops = {
181 .writev_buffer = block_writev_buffer,
182 .close = bdrv_fclose
185 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
187 if (is_writable) {
188 return qemu_fopen_ops(bs, &bdrv_write_ops);
190 return qemu_fopen_ops(bs, &bdrv_read_ops);
194 /* QEMUFile timer support.
195 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
198 void timer_put(QEMUFile *f, QEMUTimer *ts)
200 uint64_t expire_time;
202 expire_time = timer_expire_time_ns(ts);
203 qemu_put_be64(f, expire_time);
206 void timer_get(QEMUFile *f, QEMUTimer *ts)
208 uint64_t expire_time;
210 expire_time = qemu_get_be64(f);
211 if (expire_time != -1) {
212 timer_mod_ns(ts, expire_time);
213 } else {
214 timer_del(ts);
219 /* VMState timer support.
220 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
223 static int get_timer(QEMUFile *f, void *pv, size_t size)
225 QEMUTimer *v = pv;
226 timer_get(f, v);
227 return 0;
230 static void put_timer(QEMUFile *f, void *pv, size_t size)
232 QEMUTimer *v = pv;
233 timer_put(f, v);
236 const VMStateInfo vmstate_info_timer = {
237 .name = "timer",
238 .get = get_timer,
239 .put = put_timer,
243 typedef struct CompatEntry {
244 char idstr[256];
245 int instance_id;
246 } CompatEntry;
248 typedef struct SaveStateEntry {
249 QTAILQ_ENTRY(SaveStateEntry) entry;
250 char idstr[256];
251 int instance_id;
252 int alias_id;
253 int version_id;
254 int section_id;
255 SaveVMHandlers *ops;
256 const VMStateDescription *vmsd;
257 void *opaque;
258 CompatEntry *compat;
259 int is_ram;
260 } SaveStateEntry;
262 typedef struct SaveState {
263 QTAILQ_HEAD(, SaveStateEntry) handlers;
264 int global_section_id;
265 bool skip_configuration;
266 uint32_t len;
267 const char *name;
268 uint32_t target_page_bits;
269 } SaveState;
271 static SaveState savevm_state = {
272 .handlers = QTAILQ_HEAD_INITIALIZER(savevm_state.handlers),
273 .global_section_id = 0,
274 .skip_configuration = false,
277 void savevm_skip_configuration(void)
279 savevm_state.skip_configuration = true;
283 static void configuration_pre_save(void *opaque)
285 SaveState *state = opaque;
286 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
288 state->len = strlen(current_name);
289 state->name = current_name;
290 state->target_page_bits = TARGET_PAGE_BITS;
293 static int configuration_pre_load(void *opaque)
295 SaveState *state = opaque;
297 /* If there is no target-page-bits subsection it means the source
298 * predates the variable-target-page-bits support and is using the
299 * minimum possible value for this CPU.
301 state->target_page_bits = TARGET_PAGE_BITS_MIN;
302 return 0;
305 static int configuration_post_load(void *opaque, int version_id)
307 SaveState *state = opaque;
308 const char *current_name = MACHINE_GET_CLASS(current_machine)->name;
310 if (strncmp(state->name, current_name, state->len) != 0) {
311 error_report("Machine type received is '%.*s' and local is '%s'",
312 (int) state->len, state->name, current_name);
313 return -EINVAL;
316 if (state->target_page_bits != TARGET_PAGE_BITS) {
317 error_report("Received TARGET_PAGE_BITS is %d but local is %d",
318 state->target_page_bits, TARGET_PAGE_BITS);
319 return -EINVAL;
322 return 0;
325 /* The target-page-bits subsection is present only if the
326 * target page size is not the same as the default (ie the
327 * minimum page size for a variable-page-size guest CPU).
328 * If it is present then it contains the actual target page
329 * bits for the machine, and migration will fail if the
330 * two ends don't agree about it.
332 static bool vmstate_target_page_bits_needed(void *opaque)
334 return TARGET_PAGE_BITS > TARGET_PAGE_BITS_MIN;
337 static const VMStateDescription vmstate_target_page_bits = {
338 .name = "configuration/target-page-bits",
339 .version_id = 1,
340 .minimum_version_id = 1,
341 .needed = vmstate_target_page_bits_needed,
342 .fields = (VMStateField[]) {
343 VMSTATE_UINT32(target_page_bits, SaveState),
344 VMSTATE_END_OF_LIST()
348 static const VMStateDescription vmstate_configuration = {
349 .name = "configuration",
350 .version_id = 1,
351 .pre_load = configuration_pre_load,
352 .post_load = configuration_post_load,
353 .pre_save = configuration_pre_save,
354 .fields = (VMStateField[]) {
355 VMSTATE_UINT32(len, SaveState),
356 VMSTATE_VBUFFER_ALLOC_UINT32(name, SaveState, 0, NULL, 0, len),
357 VMSTATE_END_OF_LIST()
359 .subsections = (const VMStateDescription*[]) {
360 &vmstate_target_page_bits,
361 NULL
365 static void dump_vmstate_vmsd(FILE *out_file,
366 const VMStateDescription *vmsd, int indent,
367 bool is_subsection);
369 static void dump_vmstate_vmsf(FILE *out_file, const VMStateField *field,
370 int indent)
372 fprintf(out_file, "%*s{\n", indent, "");
373 indent += 2;
374 fprintf(out_file, "%*s\"field\": \"%s\",\n", indent, "", field->name);
375 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
376 field->version_id);
377 fprintf(out_file, "%*s\"field_exists\": %s,\n", indent, "",
378 field->field_exists ? "true" : "false");
379 fprintf(out_file, "%*s\"size\": %zu", indent, "", field->size);
380 if (field->vmsd != NULL) {
381 fprintf(out_file, ",\n");
382 dump_vmstate_vmsd(out_file, field->vmsd, indent, false);
384 fprintf(out_file, "\n%*s}", indent - 2, "");
387 static void dump_vmstate_vmss(FILE *out_file,
388 const VMStateDescription **subsection,
389 int indent)
391 if (*subsection != NULL) {
392 dump_vmstate_vmsd(out_file, *subsection, indent, true);
396 static void dump_vmstate_vmsd(FILE *out_file,
397 const VMStateDescription *vmsd, int indent,
398 bool is_subsection)
400 if (is_subsection) {
401 fprintf(out_file, "%*s{\n", indent, "");
402 } else {
403 fprintf(out_file, "%*s\"%s\": {\n", indent, "", "Description");
405 indent += 2;
406 fprintf(out_file, "%*s\"name\": \"%s\",\n", indent, "", vmsd->name);
407 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
408 vmsd->version_id);
409 fprintf(out_file, "%*s\"minimum_version_id\": %d", indent, "",
410 vmsd->minimum_version_id);
411 if (vmsd->fields != NULL) {
412 const VMStateField *field = vmsd->fields;
413 bool first;
415 fprintf(out_file, ",\n%*s\"Fields\": [\n", indent, "");
416 first = true;
417 while (field->name != NULL) {
418 if (field->flags & VMS_MUST_EXIST) {
419 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
420 field++;
421 continue;
423 if (!first) {
424 fprintf(out_file, ",\n");
426 dump_vmstate_vmsf(out_file, field, indent + 2);
427 field++;
428 first = false;
430 fprintf(out_file, "\n%*s]", indent, "");
432 if (vmsd->subsections != NULL) {
433 const VMStateDescription **subsection = vmsd->subsections;
434 bool first;
436 fprintf(out_file, ",\n%*s\"Subsections\": [\n", indent, "");
437 first = true;
438 while (*subsection != NULL) {
439 if (!first) {
440 fprintf(out_file, ",\n");
442 dump_vmstate_vmss(out_file, subsection, indent + 2);
443 subsection++;
444 first = false;
446 fprintf(out_file, "\n%*s]", indent, "");
448 fprintf(out_file, "\n%*s}", indent - 2, "");
451 static void dump_machine_type(FILE *out_file)
453 MachineClass *mc;
455 mc = MACHINE_GET_CLASS(current_machine);
457 fprintf(out_file, " \"vmschkmachine\": {\n");
458 fprintf(out_file, " \"Name\": \"%s\"\n", mc->name);
459 fprintf(out_file, " },\n");
462 void dump_vmstate_json_to_file(FILE *out_file)
464 GSList *list, *elt;
465 bool first;
467 fprintf(out_file, "{\n");
468 dump_machine_type(out_file);
470 first = true;
471 list = object_class_get_list(TYPE_DEVICE, true);
472 for (elt = list; elt; elt = elt->next) {
473 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
474 TYPE_DEVICE);
475 const char *name;
476 int indent = 2;
478 if (!dc->vmsd) {
479 continue;
482 if (!first) {
483 fprintf(out_file, ",\n");
485 name = object_class_get_name(OBJECT_CLASS(dc));
486 fprintf(out_file, "%*s\"%s\": {\n", indent, "", name);
487 indent += 2;
488 fprintf(out_file, "%*s\"Name\": \"%s\",\n", indent, "", name);
489 fprintf(out_file, "%*s\"version_id\": %d,\n", indent, "",
490 dc->vmsd->version_id);
491 fprintf(out_file, "%*s\"minimum_version_id\": %d,\n", indent, "",
492 dc->vmsd->minimum_version_id);
494 dump_vmstate_vmsd(out_file, dc->vmsd, indent, false);
496 fprintf(out_file, "\n%*s}", indent - 2, "");
497 first = false;
499 fprintf(out_file, "\n}\n");
500 fclose(out_file);
503 static int calculate_new_instance_id(const char *idstr)
505 SaveStateEntry *se;
506 int instance_id = 0;
508 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
509 if (strcmp(idstr, se->idstr) == 0
510 && instance_id <= se->instance_id) {
511 instance_id = se->instance_id + 1;
514 return instance_id;
517 static int calculate_compat_instance_id(const char *idstr)
519 SaveStateEntry *se;
520 int instance_id = 0;
522 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
523 if (!se->compat) {
524 continue;
527 if (strcmp(idstr, se->compat->idstr) == 0
528 && instance_id <= se->compat->instance_id) {
529 instance_id = se->compat->instance_id + 1;
532 return instance_id;
535 static inline MigrationPriority save_state_priority(SaveStateEntry *se)
537 if (se->vmsd) {
538 return se->vmsd->priority;
540 return MIG_PRI_DEFAULT;
543 static void savevm_state_handler_insert(SaveStateEntry *nse)
545 MigrationPriority priority = save_state_priority(nse);
546 SaveStateEntry *se;
548 assert(priority <= MIG_PRI_MAX);
550 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
551 if (save_state_priority(se) < priority) {
552 break;
556 if (se) {
557 QTAILQ_INSERT_BEFORE(se, nse, entry);
558 } else {
559 QTAILQ_INSERT_TAIL(&savevm_state.handlers, nse, entry);
563 /* TODO: Individual devices generally have very little idea about the rest
564 of the system, so instance_id should be removed/replaced.
565 Meanwhile pass -1 as instance_id if you do not already have a clearly
566 distinguishing id for all instances of your device class. */
567 int register_savevm_live(DeviceState *dev,
568 const char *idstr,
569 int instance_id,
570 int version_id,
571 SaveVMHandlers *ops,
572 void *opaque)
574 SaveStateEntry *se;
576 se = g_new0(SaveStateEntry, 1);
577 se->version_id = version_id;
578 se->section_id = savevm_state.global_section_id++;
579 se->ops = ops;
580 se->opaque = opaque;
581 se->vmsd = NULL;
582 /* if this is a live_savem then set is_ram */
583 if (ops->save_live_setup != NULL) {
584 se->is_ram = 1;
587 if (dev) {
588 char *id = qdev_get_dev_path(dev);
589 if (id) {
590 pstrcpy(se->idstr, sizeof(se->idstr), id);
591 pstrcat(se->idstr, sizeof(se->idstr), "/");
592 g_free(id);
594 se->compat = g_new0(CompatEntry, 1);
595 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
596 se->compat->instance_id = instance_id == -1 ?
597 calculate_compat_instance_id(idstr) : instance_id;
598 instance_id = -1;
601 pstrcat(se->idstr, sizeof(se->idstr), idstr);
603 if (instance_id == -1) {
604 se->instance_id = calculate_new_instance_id(se->idstr);
605 } else {
606 se->instance_id = instance_id;
608 assert(!se->compat || se->instance_id == 0);
609 savevm_state_handler_insert(se);
610 return 0;
613 int register_savevm(DeviceState *dev,
614 const char *idstr,
615 int instance_id,
616 int version_id,
617 SaveStateHandler *save_state,
618 LoadStateHandler *load_state,
619 void *opaque)
621 SaveVMHandlers *ops = g_new0(SaveVMHandlers, 1);
622 ops->save_state = save_state;
623 ops->load_state = load_state;
624 return register_savevm_live(dev, idstr, instance_id, version_id,
625 ops, opaque);
628 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
630 SaveStateEntry *se, *new_se;
631 char id[256] = "";
633 if (dev) {
634 char *path = qdev_get_dev_path(dev);
635 if (path) {
636 pstrcpy(id, sizeof(id), path);
637 pstrcat(id, sizeof(id), "/");
638 g_free(path);
641 pstrcat(id, sizeof(id), idstr);
643 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
644 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
645 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
646 g_free(se->compat);
647 g_free(se->ops);
648 g_free(se);
653 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
654 const VMStateDescription *vmsd,
655 void *opaque, int alias_id,
656 int required_for_version)
658 SaveStateEntry *se;
660 /* If this triggers, alias support can be dropped for the vmsd. */
661 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
663 se = g_new0(SaveStateEntry, 1);
664 se->version_id = vmsd->version_id;
665 se->section_id = savevm_state.global_section_id++;
666 se->opaque = opaque;
667 se->vmsd = vmsd;
668 se->alias_id = alias_id;
670 if (dev) {
671 char *id = qdev_get_dev_path(dev);
672 if (id) {
673 pstrcpy(se->idstr, sizeof(se->idstr), id);
674 pstrcat(se->idstr, sizeof(se->idstr), "/");
675 g_free(id);
677 se->compat = g_new0(CompatEntry, 1);
678 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
679 se->compat->instance_id = instance_id == -1 ?
680 calculate_compat_instance_id(vmsd->name) : instance_id;
681 instance_id = -1;
684 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
686 if (instance_id == -1) {
687 se->instance_id = calculate_new_instance_id(se->idstr);
688 } else {
689 se->instance_id = instance_id;
691 assert(!se->compat || se->instance_id == 0);
692 savevm_state_handler_insert(se);
693 return 0;
696 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
697 void *opaque)
699 SaveStateEntry *se, *new_se;
701 QTAILQ_FOREACH_SAFE(se, &savevm_state.handlers, entry, new_se) {
702 if (se->vmsd == vmsd && se->opaque == opaque) {
703 QTAILQ_REMOVE(&savevm_state.handlers, se, entry);
704 g_free(se->compat);
705 g_free(se);
710 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
712 trace_vmstate_load(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
713 if (!se->vmsd) { /* Old style */
714 return se->ops->load_state(f, se->opaque, version_id);
716 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
719 static void vmstate_save_old_style(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
721 int64_t old_offset, size;
723 old_offset = qemu_ftell_fast(f);
724 se->ops->save_state(f, se->opaque);
725 size = qemu_ftell_fast(f) - old_offset;
727 if (vmdesc) {
728 json_prop_int(vmdesc, "size", size);
729 json_start_array(vmdesc, "fields");
730 json_start_object(vmdesc, NULL);
731 json_prop_str(vmdesc, "name", "data");
732 json_prop_int(vmdesc, "size", size);
733 json_prop_str(vmdesc, "type", "buffer");
734 json_end_object(vmdesc);
735 json_end_array(vmdesc);
739 static void vmstate_save(QEMUFile *f, SaveStateEntry *se, QJSON *vmdesc)
741 trace_vmstate_save(se->idstr, se->vmsd ? se->vmsd->name : "(old)");
742 if (!se->vmsd) {
743 vmstate_save_old_style(f, se, vmdesc);
744 return;
746 vmstate_save_state(f, se->vmsd, se->opaque, vmdesc);
749 void savevm_skip_section_footers(void)
751 skip_section_footers = true;
755 * Write the header for device section (QEMU_VM_SECTION START/END/PART/FULL)
757 static void save_section_header(QEMUFile *f, SaveStateEntry *se,
758 uint8_t section_type)
760 qemu_put_byte(f, section_type);
761 qemu_put_be32(f, se->section_id);
763 if (section_type == QEMU_VM_SECTION_FULL ||
764 section_type == QEMU_VM_SECTION_START) {
765 /* ID string */
766 size_t len = strlen(se->idstr);
767 qemu_put_byte(f, len);
768 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
770 qemu_put_be32(f, se->instance_id);
771 qemu_put_be32(f, se->version_id);
776 * Write a footer onto device sections that catches cases misformatted device
777 * sections.
779 static void save_section_footer(QEMUFile *f, SaveStateEntry *se)
781 if (!skip_section_footers) {
782 qemu_put_byte(f, QEMU_VM_SECTION_FOOTER);
783 qemu_put_be32(f, se->section_id);
788 * qemu_savevm_command_send: Send a 'QEMU_VM_COMMAND' type element with the
789 * command and associated data.
791 * @f: File to send command on
792 * @command: Command type to send
793 * @len: Length of associated data
794 * @data: Data associated with command.
796 void qemu_savevm_command_send(QEMUFile *f,
797 enum qemu_vm_cmd command,
798 uint16_t len,
799 uint8_t *data)
801 trace_savevm_command_send(command, len);
802 qemu_put_byte(f, QEMU_VM_COMMAND);
803 qemu_put_be16(f, (uint16_t)command);
804 qemu_put_be16(f, len);
805 qemu_put_buffer(f, data, len);
806 qemu_fflush(f);
809 void qemu_savevm_send_ping(QEMUFile *f, uint32_t value)
811 uint32_t buf;
813 trace_savevm_send_ping(value);
814 buf = cpu_to_be32(value);
815 qemu_savevm_command_send(f, MIG_CMD_PING, sizeof(value), (uint8_t *)&buf);
818 void qemu_savevm_send_open_return_path(QEMUFile *f)
820 trace_savevm_send_open_return_path();
821 qemu_savevm_command_send(f, MIG_CMD_OPEN_RETURN_PATH, 0, NULL);
824 /* We have a buffer of data to send; we don't want that all to be loaded
825 * by the command itself, so the command contains just the length of the
826 * extra buffer that we then send straight after it.
827 * TODO: Must be a better way to organise that
829 * Returns:
830 * 0 on success
831 * -ve on error
833 int qemu_savevm_send_packaged(QEMUFile *f, const uint8_t *buf, size_t len)
835 uint32_t tmp;
837 if (len > MAX_VM_CMD_PACKAGED_SIZE) {
838 error_report("%s: Unreasonably large packaged state: %zu",
839 __func__, len);
840 return -1;
843 tmp = cpu_to_be32(len);
845 trace_qemu_savevm_send_packaged();
846 qemu_savevm_command_send(f, MIG_CMD_PACKAGED, 4, (uint8_t *)&tmp);
848 qemu_put_buffer(f, buf, len);
850 return 0;
853 /* Send prior to any postcopy transfer */
854 void qemu_savevm_send_postcopy_advise(QEMUFile *f)
856 uint64_t tmp[2];
857 tmp[0] = cpu_to_be64(getpagesize());
858 tmp[1] = cpu_to_be64(1ul << qemu_target_page_bits());
860 trace_qemu_savevm_send_postcopy_advise();
861 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_ADVISE, 16, (uint8_t *)tmp);
864 /* Sent prior to starting the destination running in postcopy, discard pages
865 * that have already been sent but redirtied on the source.
866 * CMD_POSTCOPY_RAM_DISCARD consist of:
867 * byte version (0)
868 * byte Length of name field (not including 0)
869 * n x byte RAM block name
870 * byte 0 terminator (just for safety)
871 * n x Byte ranges within the named RAMBlock
872 * be64 Start of the range
873 * be64 Length
875 * name: RAMBlock name that these entries are part of
876 * len: Number of page entries
877 * start_list: 'len' addresses
878 * length_list: 'len' addresses
881 void qemu_savevm_send_postcopy_ram_discard(QEMUFile *f, const char *name,
882 uint16_t len,
883 uint64_t *start_list,
884 uint64_t *length_list)
886 uint8_t *buf;
887 uint16_t tmplen;
888 uint16_t t;
889 size_t name_len = strlen(name);
891 trace_qemu_savevm_send_postcopy_ram_discard(name, len);
892 assert(name_len < 256);
893 buf = g_malloc0(1 + 1 + name_len + 1 + (8 + 8) * len);
894 buf[0] = postcopy_ram_discard_version;
895 buf[1] = name_len;
896 memcpy(buf + 2, name, name_len);
897 tmplen = 2 + name_len;
898 buf[tmplen++] = '\0';
900 for (t = 0; t < len; t++) {
901 stq_be_p(buf + tmplen, start_list[t]);
902 tmplen += 8;
903 stq_be_p(buf + tmplen, length_list[t]);
904 tmplen += 8;
906 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RAM_DISCARD, tmplen, buf);
907 g_free(buf);
910 /* Get the destination into a state where it can receive postcopy data. */
911 void qemu_savevm_send_postcopy_listen(QEMUFile *f)
913 trace_savevm_send_postcopy_listen();
914 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_LISTEN, 0, NULL);
917 /* Kick the destination into running */
918 void qemu_savevm_send_postcopy_run(QEMUFile *f)
920 trace_savevm_send_postcopy_run();
921 qemu_savevm_command_send(f, MIG_CMD_POSTCOPY_RUN, 0, NULL);
924 bool qemu_savevm_state_blocked(Error **errp)
926 SaveStateEntry *se;
928 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
929 if (se->vmsd && se->vmsd->unmigratable) {
930 error_setg(errp, "State blocked by non-migratable device '%s'",
931 se->idstr);
932 return true;
935 return false;
938 static bool enforce_config_section(void)
940 MachineState *machine = MACHINE(qdev_get_machine());
941 return machine->enforce_config_section;
944 void qemu_savevm_state_header(QEMUFile *f)
946 trace_savevm_state_header();
947 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
948 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
950 if (!savevm_state.skip_configuration || enforce_config_section()) {
951 qemu_put_byte(f, QEMU_VM_CONFIGURATION);
952 vmstate_save_state(f, &vmstate_configuration, &savevm_state, 0);
957 void qemu_savevm_state_begin(QEMUFile *f,
958 const MigrationParams *params)
960 SaveStateEntry *se;
961 int ret;
963 trace_savevm_state_begin();
964 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
965 if (!se->ops || !se->ops->set_params) {
966 continue;
968 se->ops->set_params(params, se->opaque);
971 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
972 if (!se->ops || !se->ops->save_live_setup) {
973 continue;
975 if (se->ops && se->ops->is_active) {
976 if (!se->ops->is_active(se->opaque)) {
977 continue;
980 save_section_header(f, se, QEMU_VM_SECTION_START);
982 ret = se->ops->save_live_setup(f, se->opaque);
983 save_section_footer(f, se);
984 if (ret < 0) {
985 qemu_file_set_error(f, ret);
986 break;
992 * this function has three return values:
993 * negative: there was one error, and we have -errno.
994 * 0 : We haven't finished, caller have to go again
995 * 1 : We have finished, we can go to complete phase
997 int qemu_savevm_state_iterate(QEMUFile *f, bool postcopy)
999 SaveStateEntry *se;
1000 int ret = 1;
1002 trace_savevm_state_iterate();
1003 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1004 if (!se->ops || !se->ops->save_live_iterate) {
1005 continue;
1007 if (se->ops && se->ops->is_active) {
1008 if (!se->ops->is_active(se->opaque)) {
1009 continue;
1013 * In the postcopy phase, any device that doesn't know how to
1014 * do postcopy should have saved it's state in the _complete
1015 * call that's already run, it might get confused if we call
1016 * iterate afterwards.
1018 if (postcopy && !se->ops->save_live_complete_postcopy) {
1019 continue;
1021 if (qemu_file_rate_limit(f)) {
1022 return 0;
1024 trace_savevm_section_start(se->idstr, se->section_id);
1026 save_section_header(f, se, QEMU_VM_SECTION_PART);
1028 ret = se->ops->save_live_iterate(f, se->opaque);
1029 trace_savevm_section_end(se->idstr, se->section_id, ret);
1030 save_section_footer(f, se);
1032 if (ret < 0) {
1033 qemu_file_set_error(f, ret);
1035 if (ret <= 0) {
1036 /* Do not proceed to the next vmstate before this one reported
1037 completion of the current stage. This serializes the migration
1038 and reduces the probability that a faster changing state is
1039 synchronized over and over again. */
1040 break;
1043 return ret;
1046 static bool should_send_vmdesc(void)
1048 MachineState *machine = MACHINE(qdev_get_machine());
1049 bool in_postcopy = migration_in_postcopy(migrate_get_current());
1050 return !machine->suppress_vmdesc && !in_postcopy;
1054 * Calls the save_live_complete_postcopy methods
1055 * causing the last few pages to be sent immediately and doing any associated
1056 * cleanup.
1057 * Note postcopy also calls qemu_savevm_state_complete_precopy to complete
1058 * all the other devices, but that happens at the point we switch to postcopy.
1060 void qemu_savevm_state_complete_postcopy(QEMUFile *f)
1062 SaveStateEntry *se;
1063 int ret;
1065 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1066 if (!se->ops || !se->ops->save_live_complete_postcopy) {
1067 continue;
1069 if (se->ops && se->ops->is_active) {
1070 if (!se->ops->is_active(se->opaque)) {
1071 continue;
1074 trace_savevm_section_start(se->idstr, se->section_id);
1075 /* Section type */
1076 qemu_put_byte(f, QEMU_VM_SECTION_END);
1077 qemu_put_be32(f, se->section_id);
1079 ret = se->ops->save_live_complete_postcopy(f, se->opaque);
1080 trace_savevm_section_end(se->idstr, se->section_id, ret);
1081 save_section_footer(f, se);
1082 if (ret < 0) {
1083 qemu_file_set_error(f, ret);
1084 return;
1088 qemu_put_byte(f, QEMU_VM_EOF);
1089 qemu_fflush(f);
1092 void qemu_savevm_state_complete_precopy(QEMUFile *f, bool iterable_only)
1094 QJSON *vmdesc;
1095 int vmdesc_len;
1096 SaveStateEntry *se;
1097 int ret;
1098 bool in_postcopy = migration_in_postcopy(migrate_get_current());
1100 trace_savevm_state_complete_precopy();
1102 cpu_synchronize_all_states();
1104 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1105 if (!se->ops ||
1106 (in_postcopy && se->ops->save_live_complete_postcopy) ||
1107 (in_postcopy && !iterable_only) ||
1108 !se->ops->save_live_complete_precopy) {
1109 continue;
1112 if (se->ops && se->ops->is_active) {
1113 if (!se->ops->is_active(se->opaque)) {
1114 continue;
1117 trace_savevm_section_start(se->idstr, se->section_id);
1119 save_section_header(f, se, QEMU_VM_SECTION_END);
1121 ret = se->ops->save_live_complete_precopy(f, se->opaque);
1122 trace_savevm_section_end(se->idstr, se->section_id, ret);
1123 save_section_footer(f, se);
1124 if (ret < 0) {
1125 qemu_file_set_error(f, ret);
1126 return;
1130 if (iterable_only) {
1131 return;
1134 vmdesc = qjson_new();
1135 json_prop_int(vmdesc, "page_size", TARGET_PAGE_SIZE);
1136 json_start_array(vmdesc, "devices");
1137 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1139 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1140 continue;
1142 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1143 trace_savevm_section_skip(se->idstr, se->section_id);
1144 continue;
1147 trace_savevm_section_start(se->idstr, se->section_id);
1149 json_start_object(vmdesc, NULL);
1150 json_prop_str(vmdesc, "name", se->idstr);
1151 json_prop_int(vmdesc, "instance_id", se->instance_id);
1153 save_section_header(f, se, QEMU_VM_SECTION_FULL);
1154 vmstate_save(f, se, vmdesc);
1155 trace_savevm_section_end(se->idstr, se->section_id, 0);
1156 save_section_footer(f, se);
1158 json_end_object(vmdesc);
1161 if (!in_postcopy) {
1162 /* Postcopy stream will still be going */
1163 qemu_put_byte(f, QEMU_VM_EOF);
1166 json_end_array(vmdesc);
1167 qjson_finish(vmdesc);
1168 vmdesc_len = strlen(qjson_get_str(vmdesc));
1170 if (should_send_vmdesc()) {
1171 qemu_put_byte(f, QEMU_VM_VMDESCRIPTION);
1172 qemu_put_be32(f, vmdesc_len);
1173 qemu_put_buffer(f, (uint8_t *)qjson_get_str(vmdesc), vmdesc_len);
1175 qjson_destroy(vmdesc);
1177 qemu_fflush(f);
1180 /* Give an estimate of the amount left to be transferred,
1181 * the result is split into the amount for units that can and
1182 * for units that can't do postcopy.
1184 void qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size,
1185 uint64_t *res_non_postcopiable,
1186 uint64_t *res_postcopiable)
1188 SaveStateEntry *se;
1190 *res_non_postcopiable = 0;
1191 *res_postcopiable = 0;
1194 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1195 if (!se->ops || !se->ops->save_live_pending) {
1196 continue;
1198 if (se->ops && se->ops->is_active) {
1199 if (!se->ops->is_active(se->opaque)) {
1200 continue;
1203 se->ops->save_live_pending(f, se->opaque, max_size,
1204 res_non_postcopiable, res_postcopiable);
1208 void qemu_savevm_state_cleanup(void)
1210 SaveStateEntry *se;
1212 trace_savevm_state_cleanup();
1213 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1214 if (se->ops && se->ops->cleanup) {
1215 se->ops->cleanup(se->opaque);
1220 static int qemu_savevm_state(QEMUFile *f, Error **errp)
1222 int ret;
1223 MigrationParams params = {
1224 .blk = 0,
1225 .shared = 0
1227 MigrationState *ms = migrate_init(&params);
1228 MigrationStatus status;
1229 ms->to_dst_file = f;
1231 if (migration_is_blocked(errp)) {
1232 ret = -EINVAL;
1233 goto done;
1236 qemu_mutex_unlock_iothread();
1237 qemu_savevm_state_header(f);
1238 qemu_savevm_state_begin(f, &params);
1239 qemu_mutex_lock_iothread();
1241 while (qemu_file_get_error(f) == 0) {
1242 if (qemu_savevm_state_iterate(f, false) > 0) {
1243 break;
1247 ret = qemu_file_get_error(f);
1248 if (ret == 0) {
1249 qemu_savevm_state_complete_precopy(f, false);
1250 ret = qemu_file_get_error(f);
1252 qemu_savevm_state_cleanup();
1253 if (ret != 0) {
1254 error_setg_errno(errp, -ret, "Error while writing VM state");
1257 done:
1258 if (ret != 0) {
1259 status = MIGRATION_STATUS_FAILED;
1260 } else {
1261 status = MIGRATION_STATUS_COMPLETED;
1263 migrate_set_state(&ms->state, MIGRATION_STATUS_SETUP, status);
1264 return ret;
1267 static int qemu_save_device_state(QEMUFile *f)
1269 SaveStateEntry *se;
1271 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1272 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1274 cpu_synchronize_all_states();
1276 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1277 if (se->is_ram) {
1278 continue;
1280 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1281 continue;
1283 if (se->vmsd && !vmstate_save_needed(se->vmsd, se->opaque)) {
1284 continue;
1287 save_section_header(f, se, QEMU_VM_SECTION_FULL);
1289 vmstate_save(f, se, NULL);
1291 save_section_footer(f, se);
1294 qemu_put_byte(f, QEMU_VM_EOF);
1296 return qemu_file_get_error(f);
1299 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1301 SaveStateEntry *se;
1303 QTAILQ_FOREACH(se, &savevm_state.handlers, entry) {
1304 if (!strcmp(se->idstr, idstr) &&
1305 (instance_id == se->instance_id ||
1306 instance_id == se->alias_id))
1307 return se;
1308 /* Migrating from an older version? */
1309 if (strstr(se->idstr, idstr) && se->compat) {
1310 if (!strcmp(se->compat->idstr, idstr) &&
1311 (instance_id == se->compat->instance_id ||
1312 instance_id == se->alias_id))
1313 return se;
1316 return NULL;
1319 enum LoadVMExitCodes {
1320 /* Allow a command to quit all layers of nested loadvm loops */
1321 LOADVM_QUIT = 1,
1324 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis);
1326 /* ------ incoming postcopy messages ------ */
1327 /* 'advise' arrives before any transfers just to tell us that a postcopy
1328 * *might* happen - it might be skipped if precopy transferred everything
1329 * quickly.
1331 static int loadvm_postcopy_handle_advise(MigrationIncomingState *mis)
1333 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1334 uint64_t remote_hps, remote_tps;
1336 trace_loadvm_postcopy_handle_advise();
1337 if (ps != POSTCOPY_INCOMING_NONE) {
1338 error_report("CMD_POSTCOPY_ADVISE in wrong postcopy state (%d)", ps);
1339 return -1;
1342 if (!postcopy_ram_supported_by_host()) {
1343 return -1;
1346 remote_hps = qemu_get_be64(mis->from_src_file);
1347 if (remote_hps != getpagesize()) {
1349 * Some combinations of mismatch are probably possible but it gets
1350 * a bit more complicated. In particular we need to place whole
1351 * host pages on the dest at once, and we need to ensure that we
1352 * handle dirtying to make sure we never end up sending part of
1353 * a hostpage on it's own.
1355 error_report("Postcopy needs matching host page sizes (s=%d d=%d)",
1356 (int)remote_hps, getpagesize());
1357 return -1;
1360 remote_tps = qemu_get_be64(mis->from_src_file);
1361 if (remote_tps != (1ul << qemu_target_page_bits())) {
1363 * Again, some differences could be dealt with, but for now keep it
1364 * simple.
1366 error_report("Postcopy needs matching target page sizes (s=%d d=%d)",
1367 (int)remote_tps, 1 << qemu_target_page_bits());
1368 return -1;
1371 if (ram_postcopy_incoming_init(mis)) {
1372 return -1;
1375 postcopy_state_set(POSTCOPY_INCOMING_ADVISE);
1377 return 0;
1380 /* After postcopy we will be told to throw some pages away since they're
1381 * dirty and will have to be demand fetched. Must happen before CPU is
1382 * started.
1383 * There can be 0..many of these messages, each encoding multiple pages.
1385 static int loadvm_postcopy_ram_handle_discard(MigrationIncomingState *mis,
1386 uint16_t len)
1388 int tmp;
1389 char ramid[256];
1390 PostcopyState ps = postcopy_state_get();
1392 trace_loadvm_postcopy_ram_handle_discard();
1394 switch (ps) {
1395 case POSTCOPY_INCOMING_ADVISE:
1396 /* 1st discard */
1397 tmp = postcopy_ram_prepare_discard(mis);
1398 if (tmp) {
1399 return tmp;
1401 break;
1403 case POSTCOPY_INCOMING_DISCARD:
1404 /* Expected state */
1405 break;
1407 default:
1408 error_report("CMD_POSTCOPY_RAM_DISCARD in wrong postcopy state (%d)",
1409 ps);
1410 return -1;
1412 /* We're expecting a
1413 * Version (0)
1414 * a RAM ID string (length byte, name, 0 term)
1415 * then at least 1 16 byte chunk
1417 if (len < (1 + 1 + 1 + 1 + 2 * 8)) {
1418 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1419 return -1;
1422 tmp = qemu_get_byte(mis->from_src_file);
1423 if (tmp != postcopy_ram_discard_version) {
1424 error_report("CMD_POSTCOPY_RAM_DISCARD invalid version (%d)", tmp);
1425 return -1;
1428 if (!qemu_get_counted_string(mis->from_src_file, ramid)) {
1429 error_report("CMD_POSTCOPY_RAM_DISCARD Failed to read RAMBlock ID");
1430 return -1;
1432 tmp = qemu_get_byte(mis->from_src_file);
1433 if (tmp != 0) {
1434 error_report("CMD_POSTCOPY_RAM_DISCARD missing nil (%d)", tmp);
1435 return -1;
1438 len -= 3 + strlen(ramid);
1439 if (len % 16) {
1440 error_report("CMD_POSTCOPY_RAM_DISCARD invalid length (%d)", len);
1441 return -1;
1443 trace_loadvm_postcopy_ram_handle_discard_header(ramid, len);
1444 while (len) {
1445 uint64_t start_addr, block_length;
1446 start_addr = qemu_get_be64(mis->from_src_file);
1447 block_length = qemu_get_be64(mis->from_src_file);
1449 len -= 16;
1450 int ret = ram_discard_range(mis, ramid, start_addr,
1451 block_length);
1452 if (ret) {
1453 return ret;
1456 trace_loadvm_postcopy_ram_handle_discard_end();
1458 return 0;
1462 * Triggered by a postcopy_listen command; this thread takes over reading
1463 * the input stream, leaving the main thread free to carry on loading the rest
1464 * of the device state (from RAM).
1465 * (TODO:This could do with being in a postcopy file - but there again it's
1466 * just another input loop, not that postcopy specific)
1468 static void *postcopy_ram_listen_thread(void *opaque)
1470 QEMUFile *f = opaque;
1471 MigrationIncomingState *mis = migration_incoming_get_current();
1472 int load_res;
1474 migrate_set_state(&mis->state, MIGRATION_STATUS_ACTIVE,
1475 MIGRATION_STATUS_POSTCOPY_ACTIVE);
1476 qemu_sem_post(&mis->listen_thread_sem);
1477 trace_postcopy_ram_listen_thread_start();
1480 * Because we're a thread and not a coroutine we can't yield
1481 * in qemu_file, and thus we must be blocking now.
1483 qemu_file_set_blocking(f, true);
1484 load_res = qemu_loadvm_state_main(f, mis);
1485 /* And non-blocking again so we don't block in any cleanup */
1486 qemu_file_set_blocking(f, false);
1488 trace_postcopy_ram_listen_thread_exit();
1489 if (load_res < 0) {
1490 error_report("%s: loadvm failed: %d", __func__, load_res);
1491 qemu_file_set_error(f, load_res);
1492 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1493 MIGRATION_STATUS_FAILED);
1494 } else {
1496 * This looks good, but it's possible that the device loading in the
1497 * main thread hasn't finished yet, and so we might not be in 'RUN'
1498 * state yet; wait for the end of the main thread.
1500 qemu_event_wait(&mis->main_thread_load_event);
1502 postcopy_ram_incoming_cleanup(mis);
1504 if (load_res < 0) {
1506 * If something went wrong then we have a bad state so exit;
1507 * depending how far we got it might be possible at this point
1508 * to leave the guest running and fire MCEs for pages that never
1509 * arrived as a desperate recovery step.
1511 exit(EXIT_FAILURE);
1514 migrate_set_state(&mis->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
1515 MIGRATION_STATUS_COMPLETED);
1517 * If everything has worked fine, then the main thread has waited
1518 * for us to start, and we're the last use of the mis.
1519 * (If something broke then qemu will have to exit anyway since it's
1520 * got a bad migration state).
1522 migration_incoming_state_destroy();
1525 return NULL;
1528 /* After this message we must be able to immediately receive postcopy data */
1529 static int loadvm_postcopy_handle_listen(MigrationIncomingState *mis)
1531 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_LISTENING);
1532 trace_loadvm_postcopy_handle_listen();
1533 if (ps != POSTCOPY_INCOMING_ADVISE && ps != POSTCOPY_INCOMING_DISCARD) {
1534 error_report("CMD_POSTCOPY_LISTEN in wrong postcopy state (%d)", ps);
1535 return -1;
1537 if (ps == POSTCOPY_INCOMING_ADVISE) {
1539 * A rare case, we entered listen without having to do any discards,
1540 * so do the setup that's normally done at the time of the 1st discard.
1542 postcopy_ram_prepare_discard(mis);
1546 * Sensitise RAM - can now generate requests for blocks that don't exist
1547 * However, at this point the CPU shouldn't be running, and the IO
1548 * shouldn't be doing anything yet so don't actually expect requests
1550 if (postcopy_ram_enable_notify(mis)) {
1551 return -1;
1554 if (mis->have_listen_thread) {
1555 error_report("CMD_POSTCOPY_RAM_LISTEN already has a listen thread");
1556 return -1;
1559 mis->have_listen_thread = true;
1560 /* Start up the listening thread and wait for it to signal ready */
1561 qemu_sem_init(&mis->listen_thread_sem, 0);
1562 qemu_thread_create(&mis->listen_thread, "postcopy/listen",
1563 postcopy_ram_listen_thread, mis->from_src_file,
1564 QEMU_THREAD_DETACHED);
1565 qemu_sem_wait(&mis->listen_thread_sem);
1566 qemu_sem_destroy(&mis->listen_thread_sem);
1568 return 0;
1572 typedef struct {
1573 QEMUBH *bh;
1574 } HandleRunBhData;
1576 static void loadvm_postcopy_handle_run_bh(void *opaque)
1578 Error *local_err = NULL;
1579 HandleRunBhData *data = opaque;
1581 /* TODO we should move all of this lot into postcopy_ram.c or a shared code
1582 * in migration.c
1584 cpu_synchronize_all_post_init();
1586 qemu_announce_self();
1588 /* Make sure all file formats flush their mutable metadata */
1589 bdrv_invalidate_cache_all(&local_err);
1590 if (local_err) {
1591 error_report_err(local_err);
1594 trace_loadvm_postcopy_handle_run_cpu_sync();
1595 cpu_synchronize_all_post_init();
1597 trace_loadvm_postcopy_handle_run_vmstart();
1599 if (autostart) {
1600 /* Hold onto your hats, starting the CPU */
1601 vm_start();
1602 } else {
1603 /* leave it paused and let management decide when to start the CPU */
1604 runstate_set(RUN_STATE_PAUSED);
1607 qemu_bh_delete(data->bh);
1608 g_free(data);
1611 /* After all discards we can start running and asking for pages */
1612 static int loadvm_postcopy_handle_run(MigrationIncomingState *mis)
1614 PostcopyState ps = postcopy_state_set(POSTCOPY_INCOMING_RUNNING);
1615 HandleRunBhData *data;
1617 trace_loadvm_postcopy_handle_run();
1618 if (ps != POSTCOPY_INCOMING_LISTENING) {
1619 error_report("CMD_POSTCOPY_RUN in wrong postcopy state (%d)", ps);
1620 return -1;
1623 data = g_new(HandleRunBhData, 1);
1624 data->bh = qemu_bh_new(loadvm_postcopy_handle_run_bh, data);
1625 qemu_bh_schedule(data->bh);
1627 /* We need to finish reading the stream from the package
1628 * and also stop reading anything more from the stream that loaded the
1629 * package (since it's now being read by the listener thread).
1630 * LOADVM_QUIT will quit all the layers of nested loadvm loops.
1632 return LOADVM_QUIT;
1636 * Immediately following this command is a blob of data containing an embedded
1637 * chunk of migration stream; read it and load it.
1639 * @mis: Incoming state
1640 * @length: Length of packaged data to read
1642 * Returns: Negative values on error
1645 static int loadvm_handle_cmd_packaged(MigrationIncomingState *mis)
1647 int ret;
1648 size_t length;
1649 QIOChannelBuffer *bioc;
1651 length = qemu_get_be32(mis->from_src_file);
1652 trace_loadvm_handle_cmd_packaged(length);
1654 if (length > MAX_VM_CMD_PACKAGED_SIZE) {
1655 error_report("Unreasonably large packaged state: %zu", length);
1656 return -1;
1659 bioc = qio_channel_buffer_new(length);
1660 qio_channel_set_name(QIO_CHANNEL(bioc), "migration-loadvm-buffer");
1661 ret = qemu_get_buffer(mis->from_src_file,
1662 bioc->data,
1663 length);
1664 if (ret != length) {
1665 object_unref(OBJECT(bioc));
1666 error_report("CMD_PACKAGED: Buffer receive fail ret=%d length=%zu",
1667 ret, length);
1668 return (ret < 0) ? ret : -EAGAIN;
1670 bioc->usage += length;
1671 trace_loadvm_handle_cmd_packaged_received(ret);
1673 QEMUFile *packf = qemu_fopen_channel_input(QIO_CHANNEL(bioc));
1675 ret = qemu_loadvm_state_main(packf, mis);
1676 trace_loadvm_handle_cmd_packaged_main(ret);
1677 qemu_fclose(packf);
1678 object_unref(OBJECT(bioc));
1680 return ret;
1684 * Process an incoming 'QEMU_VM_COMMAND'
1685 * 0 just a normal return
1686 * LOADVM_QUIT All good, but exit the loop
1687 * <0 Error
1689 static int loadvm_process_command(QEMUFile *f)
1691 MigrationIncomingState *mis = migration_incoming_get_current();
1692 uint16_t cmd;
1693 uint16_t len;
1694 uint32_t tmp32;
1696 cmd = qemu_get_be16(f);
1697 len = qemu_get_be16(f);
1699 trace_loadvm_process_command(cmd, len);
1700 if (cmd >= MIG_CMD_MAX || cmd == MIG_CMD_INVALID) {
1701 error_report("MIG_CMD 0x%x unknown (len 0x%x)", cmd, len);
1702 return -EINVAL;
1705 if (mig_cmd_args[cmd].len != -1 && mig_cmd_args[cmd].len != len) {
1706 error_report("%s received with bad length - expecting %zu, got %d",
1707 mig_cmd_args[cmd].name,
1708 (size_t)mig_cmd_args[cmd].len, len);
1709 return -ERANGE;
1712 switch (cmd) {
1713 case MIG_CMD_OPEN_RETURN_PATH:
1714 if (mis->to_src_file) {
1715 error_report("CMD_OPEN_RETURN_PATH called when RP already open");
1716 /* Not really a problem, so don't give up */
1717 return 0;
1719 mis->to_src_file = qemu_file_get_return_path(f);
1720 if (!mis->to_src_file) {
1721 error_report("CMD_OPEN_RETURN_PATH failed");
1722 return -1;
1724 break;
1726 case MIG_CMD_PING:
1727 tmp32 = qemu_get_be32(f);
1728 trace_loadvm_process_command_ping(tmp32);
1729 if (!mis->to_src_file) {
1730 error_report("CMD_PING (0x%x) received with no return path",
1731 tmp32);
1732 return -1;
1734 migrate_send_rp_pong(mis, tmp32);
1735 break;
1737 case MIG_CMD_PACKAGED:
1738 return loadvm_handle_cmd_packaged(mis);
1740 case MIG_CMD_POSTCOPY_ADVISE:
1741 return loadvm_postcopy_handle_advise(mis);
1743 case MIG_CMD_POSTCOPY_LISTEN:
1744 return loadvm_postcopy_handle_listen(mis);
1746 case MIG_CMD_POSTCOPY_RUN:
1747 return loadvm_postcopy_handle_run(mis);
1749 case MIG_CMD_POSTCOPY_RAM_DISCARD:
1750 return loadvm_postcopy_ram_handle_discard(mis, len);
1753 return 0;
1756 struct LoadStateEntry {
1757 QLIST_ENTRY(LoadStateEntry) entry;
1758 SaveStateEntry *se;
1759 int section_id;
1760 int version_id;
1764 * Read a footer off the wire and check that it matches the expected section
1766 * Returns: true if the footer was good
1767 * false if there is a problem (and calls error_report to say why)
1769 static bool check_section_footer(QEMUFile *f, LoadStateEntry *le)
1771 uint8_t read_mark;
1772 uint32_t read_section_id;
1774 if (skip_section_footers) {
1775 /* No footer to check */
1776 return true;
1779 read_mark = qemu_get_byte(f);
1781 if (read_mark != QEMU_VM_SECTION_FOOTER) {
1782 error_report("Missing section footer for %s", le->se->idstr);
1783 return false;
1786 read_section_id = qemu_get_be32(f);
1787 if (read_section_id != le->section_id) {
1788 error_report("Mismatched section id in footer for %s -"
1789 " read 0x%x expected 0x%x",
1790 le->se->idstr, read_section_id, le->section_id);
1791 return false;
1794 /* All good */
1795 return true;
1798 void loadvm_free_handlers(MigrationIncomingState *mis)
1800 LoadStateEntry *le, *new_le;
1802 QLIST_FOREACH_SAFE(le, &mis->loadvm_handlers, entry, new_le) {
1803 QLIST_REMOVE(le, entry);
1804 g_free(le);
1808 static int
1809 qemu_loadvm_section_start_full(QEMUFile *f, MigrationIncomingState *mis)
1811 uint32_t instance_id, version_id, section_id;
1812 SaveStateEntry *se;
1813 LoadStateEntry *le;
1814 char idstr[256];
1815 int ret;
1817 /* Read section start */
1818 section_id = qemu_get_be32(f);
1819 if (!qemu_get_counted_string(f, idstr)) {
1820 error_report("Unable to read ID string for section %u",
1821 section_id);
1822 return -EINVAL;
1824 instance_id = qemu_get_be32(f);
1825 version_id = qemu_get_be32(f);
1827 trace_qemu_loadvm_state_section_startfull(section_id, idstr,
1828 instance_id, version_id);
1829 /* Find savevm section */
1830 se = find_se(idstr, instance_id);
1831 if (se == NULL) {
1832 error_report("Unknown savevm section or instance '%s' %d",
1833 idstr, instance_id);
1834 return -EINVAL;
1837 /* Validate version */
1838 if (version_id > se->version_id) {
1839 error_report("savevm: unsupported version %d for '%s' v%d",
1840 version_id, idstr, se->version_id);
1841 return -EINVAL;
1844 /* Validate if it is a device's state */
1845 if (xen_enabled() && se->is_ram) {
1846 error_report("loadvm: %s RAM loading not allowed on Xen", idstr);
1847 return -EINVAL;
1850 /* Add entry */
1851 le = g_malloc0(sizeof(*le));
1853 le->se = se;
1854 le->section_id = section_id;
1855 le->version_id = version_id;
1856 QLIST_INSERT_HEAD(&mis->loadvm_handlers, le, entry);
1858 ret = vmstate_load(f, le->se, le->version_id);
1859 if (ret < 0) {
1860 error_report("error while loading state for instance 0x%x of"
1861 " device '%s'", instance_id, idstr);
1862 return ret;
1864 if (!check_section_footer(f, le)) {
1865 return -EINVAL;
1868 return 0;
1871 static int
1872 qemu_loadvm_section_part_end(QEMUFile *f, MigrationIncomingState *mis)
1874 uint32_t section_id;
1875 LoadStateEntry *le;
1876 int ret;
1878 section_id = qemu_get_be32(f);
1880 trace_qemu_loadvm_state_section_partend(section_id);
1881 QLIST_FOREACH(le, &mis->loadvm_handlers, entry) {
1882 if (le->section_id == section_id) {
1883 break;
1886 if (le == NULL) {
1887 error_report("Unknown savevm section %d", section_id);
1888 return -EINVAL;
1891 ret = vmstate_load(f, le->se, le->version_id);
1892 if (ret < 0) {
1893 error_report("error while loading state section id %d(%s)",
1894 section_id, le->se->idstr);
1895 return ret;
1897 if (!check_section_footer(f, le)) {
1898 return -EINVAL;
1901 return 0;
1904 static int qemu_loadvm_state_main(QEMUFile *f, MigrationIncomingState *mis)
1906 uint8_t section_type;
1907 int ret = 0;
1909 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1910 ret = 0;
1911 trace_qemu_loadvm_state_section(section_type);
1912 switch (section_type) {
1913 case QEMU_VM_SECTION_START:
1914 case QEMU_VM_SECTION_FULL:
1915 ret = qemu_loadvm_section_start_full(f, mis);
1916 if (ret < 0) {
1917 goto out;
1919 break;
1920 case QEMU_VM_SECTION_PART:
1921 case QEMU_VM_SECTION_END:
1922 ret = qemu_loadvm_section_part_end(f, mis);
1923 if (ret < 0) {
1924 goto out;
1926 break;
1927 case QEMU_VM_COMMAND:
1928 ret = loadvm_process_command(f);
1929 trace_qemu_loadvm_state_section_command(ret);
1930 if ((ret < 0) || (ret & LOADVM_QUIT)) {
1931 goto out;
1933 break;
1934 default:
1935 error_report("Unknown savevm section type %d", section_type);
1936 ret = -EINVAL;
1937 goto out;
1941 out:
1942 if (ret < 0) {
1943 qemu_file_set_error(f, ret);
1945 return ret;
1948 int qemu_loadvm_state(QEMUFile *f)
1950 MigrationIncomingState *mis = migration_incoming_get_current();
1951 Error *local_err = NULL;
1952 unsigned int v;
1953 int ret;
1955 if (qemu_savevm_state_blocked(&local_err)) {
1956 error_report_err(local_err);
1957 return -EINVAL;
1960 v = qemu_get_be32(f);
1961 if (v != QEMU_VM_FILE_MAGIC) {
1962 error_report("Not a migration stream");
1963 return -EINVAL;
1966 v = qemu_get_be32(f);
1967 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1968 error_report("SaveVM v2 format is obsolete and don't work anymore");
1969 return -ENOTSUP;
1971 if (v != QEMU_VM_FILE_VERSION) {
1972 error_report("Unsupported migration stream version");
1973 return -ENOTSUP;
1976 if (!savevm_state.skip_configuration || enforce_config_section()) {
1977 if (qemu_get_byte(f) != QEMU_VM_CONFIGURATION) {
1978 error_report("Configuration section missing");
1979 return -EINVAL;
1981 ret = vmstate_load_state(f, &vmstate_configuration, &savevm_state, 0);
1983 if (ret) {
1984 return ret;
1988 ret = qemu_loadvm_state_main(f, mis);
1989 qemu_event_set(&mis->main_thread_load_event);
1991 trace_qemu_loadvm_state_post_main(ret);
1993 if (mis->have_listen_thread) {
1994 /* Listen thread still going, can't clean up yet */
1995 return ret;
1998 if (ret == 0) {
1999 ret = qemu_file_get_error(f);
2003 * Try to read in the VMDESC section as well, so that dumping tools that
2004 * intercept our migration stream have the chance to see it.
2007 /* We've got to be careful; if we don't read the data and just shut the fd
2008 * then the sender can error if we close while it's still sending.
2009 * We also mustn't read data that isn't there; some transports (RDMA)
2010 * will stall waiting for that data when the source has already closed.
2012 if (ret == 0 && should_send_vmdesc()) {
2013 uint8_t *buf;
2014 uint32_t size;
2015 uint8_t section_type = qemu_get_byte(f);
2017 if (section_type != QEMU_VM_VMDESCRIPTION) {
2018 error_report("Expected vmdescription section, but got %d",
2019 section_type);
2021 * It doesn't seem worth failing at this point since
2022 * we apparently have an otherwise valid VM state
2024 } else {
2025 buf = g_malloc(0x1000);
2026 size = qemu_get_be32(f);
2028 while (size > 0) {
2029 uint32_t read_chunk = MIN(size, 0x1000);
2030 qemu_get_buffer(f, buf, read_chunk);
2031 size -= read_chunk;
2033 g_free(buf);
2037 cpu_synchronize_all_post_init();
2039 return ret;
2042 void hmp_savevm(Monitor *mon, const QDict *qdict)
2044 BlockDriverState *bs, *bs1;
2045 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2046 int ret;
2047 QEMUFile *f;
2048 int saved_vm_running;
2049 uint64_t vm_state_size;
2050 qemu_timeval tv;
2051 struct tm tm;
2052 const char *name = qdict_get_try_str(qdict, "name");
2053 Error *local_err = NULL;
2054 AioContext *aio_context;
2056 if (!bdrv_all_can_snapshot(&bs)) {
2057 monitor_printf(mon, "Device '%s' is writable but does not "
2058 "support snapshots.\n", bdrv_get_device_name(bs));
2059 return;
2062 /* Delete old snapshots of the same name */
2063 if (name && bdrv_all_delete_snapshot(name, &bs1, &local_err) < 0) {
2064 error_reportf_err(local_err,
2065 "Error while deleting snapshot on device '%s': ",
2066 bdrv_get_device_name(bs1));
2067 return;
2070 bs = bdrv_all_find_vmstate_bs();
2071 if (bs == NULL) {
2072 monitor_printf(mon, "No block device can accept snapshots\n");
2073 return;
2075 aio_context = bdrv_get_aio_context(bs);
2077 saved_vm_running = runstate_is_running();
2079 ret = global_state_store();
2080 if (ret) {
2081 monitor_printf(mon, "Error saving global state\n");
2082 return;
2084 vm_stop(RUN_STATE_SAVE_VM);
2086 aio_context_acquire(aio_context);
2088 memset(sn, 0, sizeof(*sn));
2090 /* fill auxiliary fields */
2091 qemu_gettimeofday(&tv);
2092 sn->date_sec = tv.tv_sec;
2093 sn->date_nsec = tv.tv_usec * 1000;
2094 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2096 if (name) {
2097 ret = bdrv_snapshot_find(bs, old_sn, name);
2098 if (ret >= 0) {
2099 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2100 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2101 } else {
2102 pstrcpy(sn->name, sizeof(sn->name), name);
2104 } else {
2105 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2106 localtime_r((const time_t *)&tv.tv_sec, &tm);
2107 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2110 /* save the VM state */
2111 f = qemu_fopen_bdrv(bs, 1);
2112 if (!f) {
2113 monitor_printf(mon, "Could not open VM state file\n");
2114 goto the_end;
2116 ret = qemu_savevm_state(f, &local_err);
2117 vm_state_size = qemu_ftell(f);
2118 qemu_fclose(f);
2119 if (ret < 0) {
2120 error_report_err(local_err);
2121 goto the_end;
2124 ret = bdrv_all_create_snapshot(sn, bs, vm_state_size, &bs);
2125 if (ret < 0) {
2126 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2127 bdrv_get_device_name(bs));
2130 the_end:
2131 aio_context_release(aio_context);
2132 if (saved_vm_running) {
2133 vm_start();
2137 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2139 QEMUFile *f;
2140 QIOChannelFile *ioc;
2141 int saved_vm_running;
2142 int ret;
2144 saved_vm_running = runstate_is_running();
2145 vm_stop(RUN_STATE_SAVE_VM);
2146 global_state_store_running();
2148 ioc = qio_channel_file_new_path(filename, O_WRONLY | O_CREAT, 0660, errp);
2149 if (!ioc) {
2150 goto the_end;
2152 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-save-state");
2153 f = qemu_fopen_channel_output(QIO_CHANNEL(ioc));
2154 ret = qemu_save_device_state(f);
2155 qemu_fclose(f);
2156 if (ret < 0) {
2157 error_setg(errp, QERR_IO_ERROR);
2160 the_end:
2161 if (saved_vm_running) {
2162 vm_start();
2166 void qmp_xen_load_devices_state(const char *filename, Error **errp)
2168 QEMUFile *f;
2169 QIOChannelFile *ioc;
2170 int ret;
2172 /* Guest must be paused before loading the device state; the RAM state
2173 * will already have been loaded by xc
2175 if (runstate_is_running()) {
2176 error_setg(errp, "Cannot update device state while vm is running");
2177 return;
2179 vm_stop(RUN_STATE_RESTORE_VM);
2181 ioc = qio_channel_file_new_path(filename, O_RDONLY | O_BINARY, 0, errp);
2182 if (!ioc) {
2183 return;
2185 qio_channel_set_name(QIO_CHANNEL(ioc), "migration-xen-load-state");
2186 f = qemu_fopen_channel_input(QIO_CHANNEL(ioc));
2188 migration_incoming_state_new(f);
2189 ret = qemu_loadvm_state(f);
2190 qemu_fclose(f);
2191 if (ret < 0) {
2192 error_setg(errp, QERR_IO_ERROR);
2194 migration_incoming_state_destroy();
2197 int load_vmstate(const char *name)
2199 BlockDriverState *bs, *bs_vm_state;
2200 QEMUSnapshotInfo sn;
2201 QEMUFile *f;
2202 int ret;
2203 AioContext *aio_context;
2205 if (!bdrv_all_can_snapshot(&bs)) {
2206 error_report("Device '%s' is writable but does not support snapshots.",
2207 bdrv_get_device_name(bs));
2208 return -ENOTSUP;
2210 ret = bdrv_all_find_snapshot(name, &bs);
2211 if (ret < 0) {
2212 error_report("Device '%s' does not have the requested snapshot '%s'",
2213 bdrv_get_device_name(bs), name);
2214 return ret;
2217 bs_vm_state = bdrv_all_find_vmstate_bs();
2218 if (!bs_vm_state) {
2219 error_report("No block device supports snapshots");
2220 return -ENOTSUP;
2222 aio_context = bdrv_get_aio_context(bs_vm_state);
2224 /* Don't even try to load empty VM states */
2225 aio_context_acquire(aio_context);
2226 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2227 aio_context_release(aio_context);
2228 if (ret < 0) {
2229 return ret;
2230 } else if (sn.vm_state_size == 0) {
2231 error_report("This is a disk-only snapshot. Revert to it offline "
2232 "using qemu-img.");
2233 return -EINVAL;
2236 /* Flush all IO requests so they don't interfere with the new state. */
2237 bdrv_drain_all();
2239 ret = bdrv_all_goto_snapshot(name, &bs);
2240 if (ret < 0) {
2241 error_report("Error %d while activating snapshot '%s' on '%s'",
2242 ret, name, bdrv_get_device_name(bs));
2243 return ret;
2246 /* restore the VM state */
2247 f = qemu_fopen_bdrv(bs_vm_state, 0);
2248 if (!f) {
2249 error_report("Could not open VM state file");
2250 return -EINVAL;
2253 qemu_system_reset(VMRESET_SILENT);
2254 migration_incoming_state_new(f);
2256 aio_context_acquire(aio_context);
2257 ret = qemu_loadvm_state(f);
2258 qemu_fclose(f);
2259 aio_context_release(aio_context);
2261 migration_incoming_state_destroy();
2262 if (ret < 0) {
2263 error_report("Error %d while loading VM state", ret);
2264 return ret;
2267 return 0;
2270 void hmp_delvm(Monitor *mon, const QDict *qdict)
2272 BlockDriverState *bs;
2273 Error *err;
2274 const char *name = qdict_get_str(qdict, "name");
2276 if (bdrv_all_delete_snapshot(name, &bs, &err) < 0) {
2277 error_reportf_err(err,
2278 "Error while deleting snapshot on device '%s': ",
2279 bdrv_get_device_name(bs));
2283 void hmp_info_snapshots(Monitor *mon, const QDict *qdict)
2285 BlockDriverState *bs, *bs1;
2286 BdrvNextIterator it1;
2287 QEMUSnapshotInfo *sn_tab, *sn;
2288 bool no_snapshot = true;
2289 int nb_sns, i;
2290 int total;
2291 int *global_snapshots;
2292 AioContext *aio_context;
2294 typedef struct SnapshotEntry {
2295 QEMUSnapshotInfo sn;
2296 QTAILQ_ENTRY(SnapshotEntry) next;
2297 } SnapshotEntry;
2299 typedef struct ImageEntry {
2300 const char *imagename;
2301 QTAILQ_ENTRY(ImageEntry) next;
2302 QTAILQ_HEAD(, SnapshotEntry) snapshots;
2303 } ImageEntry;
2305 QTAILQ_HEAD(, ImageEntry) image_list =
2306 QTAILQ_HEAD_INITIALIZER(image_list);
2308 ImageEntry *image_entry, *next_ie;
2309 SnapshotEntry *snapshot_entry;
2311 bs = bdrv_all_find_vmstate_bs();
2312 if (!bs) {
2313 monitor_printf(mon, "No available block device supports snapshots\n");
2314 return;
2316 aio_context = bdrv_get_aio_context(bs);
2318 aio_context_acquire(aio_context);
2319 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2320 aio_context_release(aio_context);
2322 if (nb_sns < 0) {
2323 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2324 return;
2327 for (bs1 = bdrv_first(&it1); bs1; bs1 = bdrv_next(&it1)) {
2328 int bs1_nb_sns = 0;
2329 ImageEntry *ie;
2330 SnapshotEntry *se;
2331 AioContext *ctx = bdrv_get_aio_context(bs1);
2333 aio_context_acquire(ctx);
2334 if (bdrv_can_snapshot(bs1)) {
2335 sn = NULL;
2336 bs1_nb_sns = bdrv_snapshot_list(bs1, &sn);
2337 if (bs1_nb_sns > 0) {
2338 no_snapshot = false;
2339 ie = g_new0(ImageEntry, 1);
2340 ie->imagename = bdrv_get_device_name(bs1);
2341 QTAILQ_INIT(&ie->snapshots);
2342 QTAILQ_INSERT_TAIL(&image_list, ie, next);
2343 for (i = 0; i < bs1_nb_sns; i++) {
2344 se = g_new0(SnapshotEntry, 1);
2345 se->sn = sn[i];
2346 QTAILQ_INSERT_TAIL(&ie->snapshots, se, next);
2349 g_free(sn);
2351 aio_context_release(ctx);
2354 if (no_snapshot) {
2355 monitor_printf(mon, "There is no snapshot available.\n");
2356 return;
2359 global_snapshots = g_new0(int, nb_sns);
2360 total = 0;
2361 for (i = 0; i < nb_sns; i++) {
2362 SnapshotEntry *next_sn;
2363 if (bdrv_all_find_snapshot(sn_tab[i].name, &bs1) == 0) {
2364 global_snapshots[total] = i;
2365 total++;
2366 QTAILQ_FOREACH(image_entry, &image_list, next) {
2367 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots,
2368 next, next_sn) {
2369 if (!strcmp(sn_tab[i].name, snapshot_entry->sn.name)) {
2370 QTAILQ_REMOVE(&image_entry->snapshots, snapshot_entry,
2371 next);
2372 g_free(snapshot_entry);
2379 monitor_printf(mon, "List of snapshots present on all disks:\n");
2381 if (total > 0) {
2382 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2383 monitor_printf(mon, "\n");
2384 for (i = 0; i < total; i++) {
2385 sn = &sn_tab[global_snapshots[i]];
2386 /* The ID is not guaranteed to be the same on all images, so
2387 * overwrite it.
2389 pstrcpy(sn->id_str, sizeof(sn->id_str), "--");
2390 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2391 monitor_printf(mon, "\n");
2393 } else {
2394 monitor_printf(mon, "None\n");
2397 QTAILQ_FOREACH(image_entry, &image_list, next) {
2398 if (QTAILQ_EMPTY(&image_entry->snapshots)) {
2399 continue;
2401 monitor_printf(mon,
2402 "\nList of partial (non-loadable) snapshots on '%s':\n",
2403 image_entry->imagename);
2404 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2405 monitor_printf(mon, "\n");
2406 QTAILQ_FOREACH(snapshot_entry, &image_entry->snapshots, next) {
2407 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon,
2408 &snapshot_entry->sn);
2409 monitor_printf(mon, "\n");
2413 QTAILQ_FOREACH_SAFE(image_entry, &image_list, next, next_ie) {
2414 SnapshotEntry *next_sn;
2415 QTAILQ_FOREACH_SAFE(snapshot_entry, &image_entry->snapshots, next,
2416 next_sn) {
2417 g_free(snapshot_entry);
2419 g_free(image_entry);
2421 g_free(sn_tab);
2422 g_free(global_snapshots);
2426 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2428 qemu_ram_set_idstr(mr->ram_block,
2429 memory_region_name(mr), dev);
2432 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2434 qemu_ram_unset_idstr(mr->ram_block);
2437 void vmstate_register_ram_global(MemoryRegion *mr)
2439 vmstate_register_ram(mr, NULL);