4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
27 #include "hw/boards.h"
31 #include "monitor/monitor.h"
32 #include "sysemu/sysemu.h"
33 #include "qemu/timer.h"
34 #include "audio/audio.h"
35 #include "migration/migration.h"
36 #include "qemu/sockets.h"
37 #include "qemu/queue.h"
38 #include "sysemu/cpus.h"
39 #include "exec/memory.h"
40 #include "qmp-commands.h"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
48 #define ETH_P_RARP 0x8035
50 #define ARP_HTYPE_ETH 0x0001
51 #define ARP_PTYPE_IP 0x0800
52 #define ARP_OP_REQUEST_REV 0x3
54 static int announce_self_create(uint8_t *buf
,
57 /* Ethernet header. */
58 memset(buf
, 0xff, 6); /* destination MAC addr */
59 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
60 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
63 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
64 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
65 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
66 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
67 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
68 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
69 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
70 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
71 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
73 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
74 memset(buf
+ 42, 0x00, 18);
76 return 60; /* len (FCS will be added by hardware) */
79 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
84 trace_qemu_announce_self_iter(qemu_ether_ntoa(&nic
->conf
->macaddr
));
85 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
87 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
91 static void qemu_announce_self_once(void *opaque
)
93 static int count
= SELF_ANNOUNCE_ROUNDS
;
94 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
96 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
99 /* delay 50ms, 150ms, 250ms, ... */
100 timer_mod(timer
, qemu_clock_get_ms(QEMU_CLOCK_REALTIME
) +
101 self_announce_delay(count
));
108 void qemu_announce_self(void)
110 static QEMUTimer
*timer
;
111 timer
= timer_new_ms(QEMU_CLOCK_REALTIME
, qemu_announce_self_once
, &timer
);
112 qemu_announce_self_once(&timer
);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
124 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
125 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
133 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
134 int64_t pos
, int size
)
136 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
140 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
142 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
145 static int bdrv_fclose(void *opaque
)
147 return bdrv_flush(opaque
);
150 static const QEMUFileOps bdrv_read_ops
= {
151 .get_buffer
= block_get_buffer
,
155 static const QEMUFileOps bdrv_write_ops
= {
156 .put_buffer
= block_put_buffer
,
157 .writev_buffer
= block_writev_buffer
,
161 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
164 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
166 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
170 /* QEMUFile timer support.
171 * Not in qemu-file.c to not add qemu-timer.c as dependency to qemu-file.c
174 void timer_put(QEMUFile
*f
, QEMUTimer
*ts
)
176 uint64_t expire_time
;
178 expire_time
= timer_expire_time_ns(ts
);
179 qemu_put_be64(f
, expire_time
);
182 void timer_get(QEMUFile
*f
, QEMUTimer
*ts
)
184 uint64_t expire_time
;
186 expire_time
= qemu_get_be64(f
);
187 if (expire_time
!= -1) {
188 timer_mod_ns(ts
, expire_time
);
195 /* VMState timer support.
196 * Not in vmstate.c to not add qemu-timer.c as dependency to vmstate.c
199 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
206 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
212 const VMStateInfo vmstate_info_timer
= {
219 typedef struct CompatEntry
{
224 typedef struct SaveStateEntry
{
225 QTAILQ_ENTRY(SaveStateEntry
) entry
;
232 const VMStateDescription
*vmsd
;
240 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
241 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
242 static int global_section_id
;
244 static void dump_vmstate_vmsd(FILE *out_file
,
245 const VMStateDescription
*vmsd
, int indent
,
248 static void dump_vmstate_vmsf(FILE *out_file
, const VMStateField
*field
,
251 fprintf(out_file
, "%*s{\n", indent
, "");
253 fprintf(out_file
, "%*s\"field\": \"%s\",\n", indent
, "", field
->name
);
254 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
256 fprintf(out_file
, "%*s\"field_exists\": %s,\n", indent
, "",
257 field
->field_exists
? "true" : "false");
258 fprintf(out_file
, "%*s\"size\": %zu", indent
, "", field
->size
);
259 if (field
->vmsd
!= NULL
) {
260 fprintf(out_file
, ",\n");
261 dump_vmstate_vmsd(out_file
, field
->vmsd
, indent
, false);
263 fprintf(out_file
, "\n%*s}", indent
- 2, "");
266 static void dump_vmstate_vmss(FILE *out_file
,
267 const VMStateSubsection
*subsection
,
270 if (subsection
->vmsd
!= NULL
) {
271 dump_vmstate_vmsd(out_file
, subsection
->vmsd
, indent
, true);
275 static void dump_vmstate_vmsd(FILE *out_file
,
276 const VMStateDescription
*vmsd
, int indent
,
280 fprintf(out_file
, "%*s{\n", indent
, "");
282 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", "Description");
285 fprintf(out_file
, "%*s\"name\": \"%s\",\n", indent
, "", vmsd
->name
);
286 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
288 fprintf(out_file
, "%*s\"minimum_version_id\": %d", indent
, "",
289 vmsd
->minimum_version_id
);
290 if (vmsd
->fields
!= NULL
) {
291 const VMStateField
*field
= vmsd
->fields
;
294 fprintf(out_file
, ",\n%*s\"Fields\": [\n", indent
, "");
296 while (field
->name
!= NULL
) {
297 if (field
->flags
& VMS_MUST_EXIST
) {
298 /* Ignore VMSTATE_VALIDATE bits; these don't get migrated */
303 fprintf(out_file
, ",\n");
305 dump_vmstate_vmsf(out_file
, field
, indent
+ 2);
309 fprintf(out_file
, "\n%*s]", indent
, "");
311 if (vmsd
->subsections
!= NULL
) {
312 const VMStateSubsection
*subsection
= vmsd
->subsections
;
315 fprintf(out_file
, ",\n%*s\"Subsections\": [\n", indent
, "");
317 while (subsection
->vmsd
!= NULL
) {
319 fprintf(out_file
, ",\n");
321 dump_vmstate_vmss(out_file
, subsection
, indent
+ 2);
325 fprintf(out_file
, "\n%*s]", indent
, "");
327 fprintf(out_file
, "\n%*s}", indent
- 2, "");
330 static void dump_machine_type(FILE *out_file
)
334 mc
= MACHINE_GET_CLASS(current_machine
);
336 fprintf(out_file
, " \"vmschkmachine\": {\n");
337 fprintf(out_file
, " \"Name\": \"%s\"\n", mc
->name
);
338 fprintf(out_file
, " },\n");
341 void dump_vmstate_json_to_file(FILE *out_file
)
346 fprintf(out_file
, "{\n");
347 dump_machine_type(out_file
);
350 list
= object_class_get_list(TYPE_DEVICE
, true);
351 for (elt
= list
; elt
; elt
= elt
->next
) {
352 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
362 fprintf(out_file
, ",\n");
364 name
= object_class_get_name(OBJECT_CLASS(dc
));
365 fprintf(out_file
, "%*s\"%s\": {\n", indent
, "", name
);
367 fprintf(out_file
, "%*s\"Name\": \"%s\",\n", indent
, "", name
);
368 fprintf(out_file
, "%*s\"version_id\": %d,\n", indent
, "",
369 dc
->vmsd
->version_id
);
370 fprintf(out_file
, "%*s\"minimum_version_id\": %d,\n", indent
, "",
371 dc
->vmsd
->minimum_version_id
);
373 dump_vmstate_vmsd(out_file
, dc
->vmsd
, indent
, false);
375 fprintf(out_file
, "\n%*s}", indent
- 2, "");
378 fprintf(out_file
, "\n}\n");
382 static int calculate_new_instance_id(const char *idstr
)
387 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
388 if (strcmp(idstr
, se
->idstr
) == 0
389 && instance_id
<= se
->instance_id
) {
390 instance_id
= se
->instance_id
+ 1;
396 static int calculate_compat_instance_id(const char *idstr
)
401 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
406 if (strcmp(idstr
, se
->compat
->idstr
) == 0
407 && instance_id
<= se
->compat
->instance_id
) {
408 instance_id
= se
->compat
->instance_id
+ 1;
414 /* TODO: Individual devices generally have very little idea about the rest
415 of the system, so instance_id should be removed/replaced.
416 Meanwhile pass -1 as instance_id if you do not already have a clearly
417 distinguishing id for all instances of your device class. */
418 int register_savevm_live(DeviceState
*dev
,
427 se
= g_malloc0(sizeof(SaveStateEntry
));
428 se
->version_id
= version_id
;
429 se
->section_id
= global_section_id
++;
434 /* if this is a live_savem then set is_ram */
435 if (ops
->save_live_setup
!= NULL
) {
440 char *id
= qdev_get_dev_path(dev
);
442 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
443 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
446 se
->compat
= g_malloc0(sizeof(CompatEntry
));
447 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
448 se
->compat
->instance_id
= instance_id
== -1 ?
449 calculate_compat_instance_id(idstr
) : instance_id
;
453 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
455 if (instance_id
== -1) {
456 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
458 se
->instance_id
= instance_id
;
460 assert(!se
->compat
|| se
->instance_id
== 0);
461 /* add at the end of list */
462 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
466 int register_savevm(DeviceState
*dev
,
470 SaveStateHandler
*save_state
,
471 LoadStateHandler
*load_state
,
474 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
475 ops
->save_state
= save_state
;
476 ops
->load_state
= load_state
;
477 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
481 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
483 SaveStateEntry
*se
, *new_se
;
487 char *path
= qdev_get_dev_path(dev
);
489 pstrcpy(id
, sizeof(id
), path
);
490 pstrcat(id
, sizeof(id
), "/");
494 pstrcat(id
, sizeof(id
), idstr
);
496 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
497 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
498 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
508 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
509 const VMStateDescription
*vmsd
,
510 void *opaque
, int alias_id
,
511 int required_for_version
)
515 /* If this triggers, alias support can be dropped for the vmsd. */
516 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
518 se
= g_malloc0(sizeof(SaveStateEntry
));
519 se
->version_id
= vmsd
->version_id
;
520 se
->section_id
= global_section_id
++;
523 se
->alias_id
= alias_id
;
524 se
->no_migrate
= vmsd
->unmigratable
;
527 char *id
= qdev_get_dev_path(dev
);
529 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
530 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
533 se
->compat
= g_malloc0(sizeof(CompatEntry
));
534 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
535 se
->compat
->instance_id
= instance_id
== -1 ?
536 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
540 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
542 if (instance_id
== -1) {
543 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
545 se
->instance_id
= instance_id
;
547 assert(!se
->compat
|| se
->instance_id
== 0);
548 /* add at the end of list */
549 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
553 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
556 SaveStateEntry
*se
, *new_se
;
558 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
559 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
560 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
569 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
571 trace_vmstate_load(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
572 if (!se
->vmsd
) { /* Old style */
573 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
575 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
578 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
580 trace_vmstate_save(se
->idstr
, se
->vmsd
? se
->vmsd
->name
: "(old)");
581 if (!se
->vmsd
) { /* Old style */
582 se
->ops
->save_state(f
, se
->opaque
);
585 vmstate_save_state(f
, se
->vmsd
, se
->opaque
);
588 bool qemu_savevm_state_blocked(Error
**errp
)
592 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
593 if (se
->no_migrate
) {
594 error_setg(errp
, "State blocked by non-migratable device '%s'",
602 void qemu_savevm_state_begin(QEMUFile
*f
,
603 const MigrationParams
*params
)
608 trace_savevm_state_begin();
609 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
610 if (!se
->ops
|| !se
->ops
->set_params
) {
613 se
->ops
->set_params(params
, se
->opaque
);
616 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
617 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
619 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
622 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
625 if (se
->ops
&& se
->ops
->is_active
) {
626 if (!se
->ops
->is_active(se
->opaque
)) {
631 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
632 qemu_put_be32(f
, se
->section_id
);
635 len
= strlen(se
->idstr
);
636 qemu_put_byte(f
, len
);
637 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
639 qemu_put_be32(f
, se
->instance_id
);
640 qemu_put_be32(f
, se
->version_id
);
642 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
644 qemu_file_set_error(f
, ret
);
651 * this function has three return values:
652 * negative: there was one error, and we have -errno.
653 * 0 : We haven't finished, caller have to go again
654 * 1 : We have finished, we can go to complete phase
656 int qemu_savevm_state_iterate(QEMUFile
*f
)
661 trace_savevm_state_iterate();
662 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
663 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
666 if (se
->ops
&& se
->ops
->is_active
) {
667 if (!se
->ops
->is_active(se
->opaque
)) {
671 if (qemu_file_rate_limit(f
)) {
674 trace_savevm_section_start(se
->idstr
, se
->section_id
);
676 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
677 qemu_put_be32(f
, se
->section_id
);
679 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
680 trace_savevm_section_end(se
->idstr
, se
->section_id
);
683 qemu_file_set_error(f
, ret
);
686 /* Do not proceed to the next vmstate before this one reported
687 completion of the current stage. This serializes the migration
688 and reduces the probability that a faster changing state is
689 synchronized over and over again. */
696 void qemu_savevm_state_complete(QEMUFile
*f
)
701 trace_savevm_state_complete();
703 cpu_synchronize_all_states();
705 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
706 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
709 if (se
->ops
&& se
->ops
->is_active
) {
710 if (!se
->ops
->is_active(se
->opaque
)) {
714 trace_savevm_section_start(se
->idstr
, se
->section_id
);
716 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
717 qemu_put_be32(f
, se
->section_id
);
719 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
720 trace_savevm_section_end(se
->idstr
, se
->section_id
);
722 qemu_file_set_error(f
, ret
);
727 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
730 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
733 trace_savevm_section_start(se
->idstr
, se
->section_id
);
735 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
736 qemu_put_be32(f
, se
->section_id
);
739 len
= strlen(se
->idstr
);
740 qemu_put_byte(f
, len
);
741 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
743 qemu_put_be32(f
, se
->instance_id
);
744 qemu_put_be32(f
, se
->version_id
);
747 trace_savevm_section_end(se
->idstr
, se
->section_id
);
750 qemu_put_byte(f
, QEMU_VM_EOF
);
754 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
759 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
760 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
763 if (se
->ops
&& se
->ops
->is_active
) {
764 if (!se
->ops
->is_active(se
->opaque
)) {
768 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
773 void qemu_savevm_state_cancel(void)
777 trace_savevm_state_cancel();
778 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
779 if (se
->ops
&& se
->ops
->cancel
) {
780 se
->ops
->cancel(se
->opaque
);
785 static int qemu_savevm_state(QEMUFile
*f
)
788 MigrationParams params
= {
793 if (qemu_savevm_state_blocked(NULL
)) {
797 qemu_mutex_unlock_iothread();
798 qemu_savevm_state_begin(f
, ¶ms
);
799 qemu_mutex_lock_iothread();
801 while (qemu_file_get_error(f
) == 0) {
802 if (qemu_savevm_state_iterate(f
) > 0) {
807 ret
= qemu_file_get_error(f
);
809 qemu_savevm_state_complete(f
);
810 ret
= qemu_file_get_error(f
);
813 qemu_savevm_state_cancel();
818 static int qemu_save_device_state(QEMUFile
*f
)
822 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
823 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
825 cpu_synchronize_all_states();
827 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
833 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
838 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
839 qemu_put_be32(f
, se
->section_id
);
842 len
= strlen(se
->idstr
);
843 qemu_put_byte(f
, len
);
844 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
846 qemu_put_be32(f
, se
->instance_id
);
847 qemu_put_be32(f
, se
->version_id
);
852 qemu_put_byte(f
, QEMU_VM_EOF
);
854 return qemu_file_get_error(f
);
857 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
861 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
862 if (!strcmp(se
->idstr
, idstr
) &&
863 (instance_id
== se
->instance_id
||
864 instance_id
== se
->alias_id
))
866 /* Migrating from an older version? */
867 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
868 if (!strcmp(se
->compat
->idstr
, idstr
) &&
869 (instance_id
== se
->compat
->instance_id
||
870 instance_id
== se
->alias_id
))
877 typedef struct LoadStateEntry
{
878 QLIST_ENTRY(LoadStateEntry
) entry
;
884 int qemu_loadvm_state(QEMUFile
*f
)
886 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
887 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
888 LoadStateEntry
*le
, *new_le
;
889 uint8_t section_type
;
893 if (qemu_savevm_state_blocked(NULL
)) {
897 v
= qemu_get_be32(f
);
898 if (v
!= QEMU_VM_FILE_MAGIC
) {
902 v
= qemu_get_be32(f
);
903 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
904 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
907 if (v
!= QEMU_VM_FILE_VERSION
) {
911 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
912 uint32_t instance_id
, version_id
, section_id
;
917 switch (section_type
) {
918 case QEMU_VM_SECTION_START
:
919 case QEMU_VM_SECTION_FULL
:
920 /* Read section start */
921 section_id
= qemu_get_be32(f
);
922 len
= qemu_get_byte(f
);
923 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
925 instance_id
= qemu_get_be32(f
);
926 version_id
= qemu_get_be32(f
);
928 /* Find savevm section */
929 se
= find_se(idstr
, instance_id
);
931 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
936 /* Validate version */
937 if (version_id
> se
->version_id
) {
938 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
939 version_id
, idstr
, se
->version_id
);
945 le
= g_malloc0(sizeof(*le
));
948 le
->section_id
= section_id
;
949 le
->version_id
= version_id
;
950 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
952 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
954 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
959 case QEMU_VM_SECTION_PART
:
960 case QEMU_VM_SECTION_END
:
961 section_id
= qemu_get_be32(f
);
963 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
964 if (le
->section_id
== section_id
) {
969 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
974 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
976 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
982 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
988 cpu_synchronize_all_post_init();
993 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
994 QLIST_REMOVE(le
, entry
);
999 ret
= qemu_file_get_error(f
);
1005 static BlockDriverState
*find_vmstate_bs(void)
1007 BlockDriverState
*bs
= NULL
;
1008 while ((bs
= bdrv_next(bs
))) {
1009 if (bdrv_can_snapshot(bs
)) {
1017 * Deletes snapshots of a given name in all opened images.
1019 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1021 BlockDriverState
*bs
;
1022 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1026 while ((bs
= bdrv_next(bs
))) {
1027 if (bdrv_can_snapshot(bs
) &&
1028 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0) {
1029 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1032 "Error while deleting snapshot on device '%s':"
1034 bdrv_get_device_name(bs
),
1035 error_get_pretty(err
));
1045 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1047 BlockDriverState
*bs
, *bs1
;
1048 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1051 int saved_vm_running
;
1052 uint64_t vm_state_size
;
1055 const char *name
= qdict_get_try_str(qdict
, "name");
1057 /* Verify if there is a device that doesn't support snapshots and is writable */
1059 while ((bs
= bdrv_next(bs
))) {
1061 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1065 if (!bdrv_can_snapshot(bs
)) {
1066 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1067 bdrv_get_device_name(bs
));
1072 bs
= find_vmstate_bs();
1074 monitor_printf(mon
, "No block device can accept snapshots\n");
1078 saved_vm_running
= runstate_is_running();
1079 vm_stop(RUN_STATE_SAVE_VM
);
1081 memset(sn
, 0, sizeof(*sn
));
1083 /* fill auxiliary fields */
1084 qemu_gettimeofday(&tv
);
1085 sn
->date_sec
= tv
.tv_sec
;
1086 sn
->date_nsec
= tv
.tv_usec
* 1000;
1087 sn
->vm_clock_nsec
= qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
);
1090 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1092 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1093 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1095 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1098 /* cast below needed for OpenBSD where tv_sec is still 'long' */
1099 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
1100 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
1103 /* Delete old snapshots of the same name */
1104 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1108 /* save the VM state */
1109 f
= qemu_fopen_bdrv(bs
, 1);
1111 monitor_printf(mon
, "Could not open VM state file\n");
1114 ret
= qemu_savevm_state(f
);
1115 vm_state_size
= qemu_ftell(f
);
1118 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1122 /* create the snapshots */
1125 while ((bs1
= bdrv_next(bs1
))) {
1126 if (bdrv_can_snapshot(bs1
)) {
1127 /* Write VM state size only to the image that contains the state */
1128 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1129 ret
= bdrv_snapshot_create(bs1
, sn
);
1131 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1132 bdrv_get_device_name(bs1
));
1138 if (saved_vm_running
) {
1143 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
1146 int saved_vm_running
;
1149 saved_vm_running
= runstate_is_running();
1150 vm_stop(RUN_STATE_SAVE_VM
);
1152 f
= qemu_fopen(filename
, "wb");
1154 error_setg_file_open(errp
, errno
, filename
);
1157 ret
= qemu_save_device_state(f
);
1160 error_set(errp
, QERR_IO_ERROR
);
1164 if (saved_vm_running
) {
1169 int load_vmstate(const char *name
)
1171 BlockDriverState
*bs
, *bs_vm_state
;
1172 QEMUSnapshotInfo sn
;
1176 bs_vm_state
= find_vmstate_bs();
1178 error_report("No block device supports snapshots");
1182 /* Don't even try to load empty VM states */
1183 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
1186 } else if (sn
.vm_state_size
== 0) {
1187 error_report("This is a disk-only snapshot. Revert to it offline "
1192 /* Verify if there is any device that doesn't support snapshots and is
1193 writable and check if the requested snapshot is available too. */
1195 while ((bs
= bdrv_next(bs
))) {
1197 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
1201 if (!bdrv_can_snapshot(bs
)) {
1202 error_report("Device '%s' is writable but does not support snapshots.",
1203 bdrv_get_device_name(bs
));
1207 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1209 error_report("Device '%s' does not have the requested snapshot '%s'",
1210 bdrv_get_device_name(bs
), name
);
1215 /* Flush all IO requests so they don't interfere with the new state. */
1219 while ((bs
= bdrv_next(bs
))) {
1220 if (bdrv_can_snapshot(bs
)) {
1221 ret
= bdrv_snapshot_goto(bs
, name
);
1223 error_report("Error %d while activating snapshot '%s' on '%s'",
1224 ret
, name
, bdrv_get_device_name(bs
));
1230 /* restore the VM state */
1231 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
1233 error_report("Could not open VM state file");
1237 qemu_system_reset(VMRESET_SILENT
);
1238 ret
= qemu_loadvm_state(f
);
1242 error_report("Error %d while loading VM state", ret
);
1249 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1251 BlockDriverState
*bs
, *bs1
;
1253 const char *name
= qdict_get_str(qdict
, "name");
1255 bs
= find_vmstate_bs();
1257 monitor_printf(mon
, "No block device supports snapshots\n");
1262 while ((bs1
= bdrv_next(bs1
))) {
1263 if (bdrv_can_snapshot(bs1
)) {
1264 bdrv_snapshot_delete_by_id_or_name(bs
, name
, &err
);
1267 "Error while deleting snapshot on device '%s':"
1269 bdrv_get_device_name(bs
),
1270 error_get_pretty(err
));
1277 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
1279 BlockDriverState
*bs
, *bs1
;
1280 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
1281 int nb_sns
, i
, ret
, available
;
1283 int *available_snapshots
;
1285 bs
= find_vmstate_bs();
1287 monitor_printf(mon
, "No available block device supports snapshots\n");
1291 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1293 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1298 monitor_printf(mon
, "There is no snapshot available.\n");
1302 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
1304 for (i
= 0; i
< nb_sns
; i
++) {
1309 while ((bs1
= bdrv_next(bs1
))) {
1310 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
1311 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
1320 available_snapshots
[total
] = i
;
1326 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
1327 monitor_printf(mon
, "\n");
1328 for (i
= 0; i
< total
; i
++) {
1329 sn
= &sn_tab
[available_snapshots
[i
]];
1330 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
1331 monitor_printf(mon
, "\n");
1334 monitor_printf(mon
, "There is no suitable snapshot available\n");
1338 g_free(available_snapshots
);
1342 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1344 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
1345 memory_region_name(mr
), dev
);
1348 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
1350 qemu_ram_unset_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
);
1353 void vmstate_register_ram_global(MemoryRegion
*mr
)
1355 vmstate_register_ram(mr
, NULL
);