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
31 /* Needed early for CONFIG_BSD etc. */
32 #include "config-host.h"
35 #include <sys/times.h>
39 #include <sys/ioctl.h>
40 #include <sys/resource.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
44 #include <arpa/inet.h>
47 #include <sys/select.h>
50 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
58 #include <linux/rtc.h>
66 #include <sys/timeb.h>
68 #define getopt_long_only getopt_long
69 #define memalign(align, size) malloc(size)
72 #include "qemu-common.h"
76 #include "monitor/monitor.h"
78 #include "qemu/timer.h"
79 #include "audio/audio.h"
80 #include "migration/migration.h"
81 #include "qemu/sockets.h"
82 #include "qemu/queue.h"
83 #include "qemu/timer.h"
85 #include "exec/memory.h"
86 #include "qmp-commands.h"
88 #include "qemu/bitops.h"
90 #define SELF_ANNOUNCE_ROUNDS 5
93 #define ETH_P_RARP 0x8035
95 #define ARP_HTYPE_ETH 0x0001
96 #define ARP_PTYPE_IP 0x0800
97 #define ARP_OP_REQUEST_REV 0x3
99 static int announce_self_create(uint8_t *buf,
102 /* Ethernet header. */
103 memset(buf, 0xff, 6); /* destination MAC addr */
104 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
105 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
108 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
109 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
110 *(buf + 18) = 6; /* hardware addr length (ethernet) */
111 *(buf + 19) = 4; /* protocol addr length (IPv4) */
112 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
113 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
114 memset(buf + 28, 0x00, 4); /* source protocol addr */
115 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
116 memset(buf + 38, 0x00, 4); /* target protocol addr */
118 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
119 memset(buf + 42, 0x00, 18);
121 return 60; /* len (FCS will be added by hardware) */
124 static void qemu_announce_self_iter(NICState *nic, void *opaque)
129 len = announce_self_create(buf, nic->conf->macaddr.a);
131 qemu_send_packet_raw(&nic->nc, buf, len);
135 static void qemu_announce_self_once(void *opaque)
137 static int count = SELF_ANNOUNCE_ROUNDS;
138 QEMUTimer *timer = *(QEMUTimer **)opaque;
140 qemu_foreach_nic(qemu_announce_self_iter, NULL);
143 /* delay 50ms, 150ms, 250ms, ... */
144 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
145 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
147 qemu_del_timer(timer);
148 qemu_free_timer(timer);
152 void qemu_announce_self(void)
154 static QEMUTimer *timer;
155 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
156 qemu_announce_self_once(&timer);
159 /***********************************************************/
160 /* savevm/loadvm support */
162 #define IO_BUF_SIZE 32768
165 const QEMUFileOps *ops;
169 int64_t buf_offset; /* start of buffer when writing, end of buffer
172 int buf_size; /* 0 when writing */
173 uint8_t buf[IO_BUF_SIZE];
178 typedef struct QEMUFileStdio
184 typedef struct QEMUFileSocket
190 static int socket_get_fd(void *opaque)
192 QEMUFileSocket *s = opaque;
197 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
199 QEMUFileSocket *s = opaque;
203 len = qemu_recv(s->fd, buf, size, 0);
207 if (socket_error() == EAGAIN) {
208 assert(qemu_in_coroutine());
209 qemu_coroutine_yield();
210 } else if (socket_error() != EINTR) {
216 len = -socket_error();
221 static int socket_close(void *opaque)
223 QEMUFileSocket *s = opaque;
229 static int stdio_get_fd(void *opaque)
231 QEMUFileStdio *s = opaque;
233 return fileno(s->stdio_file);
236 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
238 QEMUFileStdio *s = opaque;
239 return fwrite(buf, 1, size, s->stdio_file);
242 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
244 QEMUFileStdio *s = opaque;
245 FILE *fp = s->stdio_file;
250 bytes = fread(buf, 1, size, fp);
251 if (bytes != 0 || !ferror(fp)) {
254 if (errno == EAGAIN) {
255 assert(qemu_in_coroutine());
256 qemu_coroutine_yield();
257 } else if (errno != EINTR) {
264 static int stdio_pclose(void *opaque)
266 QEMUFileStdio *s = opaque;
268 ret = pclose(s->stdio_file);
276 static int stdio_fclose(void *opaque)
278 QEMUFileStdio *s = opaque;
280 if (fclose(s->stdio_file) == EOF) {
287 static const QEMUFileOps stdio_pipe_read_ops = {
288 .get_fd = stdio_get_fd,
289 .get_buffer = stdio_get_buffer,
290 .close = stdio_pclose
293 static const QEMUFileOps stdio_pipe_write_ops = {
294 .get_fd = stdio_get_fd,
295 .put_buffer = stdio_put_buffer,
296 .close = stdio_pclose
299 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
303 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
304 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
308 s = g_malloc0(sizeof(QEMUFileStdio));
310 s->stdio_file = stdio_file;
313 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
315 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
320 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
324 popen_file = popen(command, mode);
325 if(popen_file == NULL) {
329 return qemu_popen(popen_file, mode);
332 static const QEMUFileOps stdio_file_read_ops = {
333 .get_fd = stdio_get_fd,
334 .get_buffer = stdio_get_buffer,
335 .close = stdio_fclose
338 static const QEMUFileOps stdio_file_write_ops = {
339 .get_fd = stdio_get_fd,
340 .put_buffer = stdio_put_buffer,
341 .close = stdio_fclose
344 QEMUFile *qemu_fdopen(int fd, const char *mode)
349 (mode[0] != 'r' && mode[0] != 'w') ||
350 mode[1] != 'b' || mode[2] != 0) {
351 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
355 s = g_malloc0(sizeof(QEMUFileStdio));
356 s->stdio_file = fdopen(fd, mode);
361 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
363 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
372 static const QEMUFileOps socket_read_ops = {
373 .get_fd = socket_get_fd,
374 .get_buffer = socket_get_buffer,
375 .close = socket_close
378 QEMUFile *qemu_fopen_socket(int fd)
380 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
383 s->file = qemu_fopen_ops(s, &socket_read_ops);
387 QEMUFile *qemu_fopen(const char *filename, const char *mode)
392 (mode[0] != 'r' && mode[0] != 'w') ||
393 mode[1] != 'b' || mode[2] != 0) {
394 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
398 s = g_malloc0(sizeof(QEMUFileStdio));
400 s->stdio_file = fopen(filename, mode);
405 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
407 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
415 static int block_put_buffer(void *opaque, const uint8_t *buf,
416 int64_t pos, int size)
418 bdrv_save_vmstate(opaque, buf, pos, size);
422 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
424 return bdrv_load_vmstate(opaque, buf, pos, size);
427 static int bdrv_fclose(void *opaque)
429 return bdrv_flush(opaque);
432 static const QEMUFileOps bdrv_read_ops = {
433 .get_buffer = block_get_buffer,
437 static const QEMUFileOps bdrv_write_ops = {
438 .put_buffer = block_put_buffer,
442 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
445 return qemu_fopen_ops(bs, &bdrv_write_ops);
446 return qemu_fopen_ops(bs, &bdrv_read_ops);
449 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
453 f = g_malloc0(sizeof(QEMUFile));
462 int qemu_file_get_error(QEMUFile *f)
464 return f->last_error;
467 static void qemu_file_set_error(QEMUFile *f, int ret)
472 /** Flushes QEMUFile buffer
475 static int qemu_fflush(QEMUFile *f)
479 if (!f->ops->put_buffer)
482 if (f->is_write && f->buf_index > 0) {
483 ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
485 f->buf_offset += f->buf_index;
492 static void qemu_fill_buffer(QEMUFile *f)
497 if (!f->ops->get_buffer)
503 pending = f->buf_size - f->buf_index;
505 memmove(f->buf, f->buf + f->buf_index, pending);
508 f->buf_size = pending;
510 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
511 IO_BUF_SIZE - pending);
514 f->buf_offset += len;
515 } else if (len == 0) {
516 qemu_file_set_error(f, -EIO);
517 } else if (len != -EAGAIN)
518 qemu_file_set_error(f, len);
521 int qemu_get_fd(QEMUFile *f)
523 if (f->ops->get_fd) {
524 return f->ops->get_fd(f->opaque);
531 * Returns negative error value if any error happened on previous operations or
532 * while closing the file. Returns 0 or positive number on success.
534 * The meaning of return value on success depends on the specific backend
537 int qemu_fclose(QEMUFile *f)
540 ret = qemu_fflush(f);
543 int ret2 = f->ops->close(f->opaque);
548 /* If any error was spotted before closing, we should report it
549 * instead of the close() return value.
558 int qemu_file_put_notify(QEMUFile *f)
560 return f->ops->put_buffer(f->opaque, NULL, 0, 0);
563 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
571 if (f->is_write == 0 && f->buf_index > 0) {
573 "Attempted to write to buffer while read buffer is not empty\n");
578 l = IO_BUF_SIZE - f->buf_index;
581 memcpy(f->buf + f->buf_index, buf, l);
586 if (f->buf_index >= IO_BUF_SIZE) {
587 int ret = qemu_fflush(f);
589 qemu_file_set_error(f, ret);
596 void qemu_put_byte(QEMUFile *f, int v)
602 if (f->is_write == 0 && f->buf_index > 0) {
604 "Attempted to write to buffer while read buffer is not empty\n");
608 f->buf[f->buf_index++] = v;
610 if (f->buf_index >= IO_BUF_SIZE) {
611 int ret = qemu_fflush(f);
613 qemu_file_set_error(f, ret);
618 static void qemu_file_skip(QEMUFile *f, int size)
620 if (f->buf_index + size <= f->buf_size) {
621 f->buf_index += size;
625 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
634 index = f->buf_index + offset;
635 pending = f->buf_size - index;
636 if (pending < size) {
638 index = f->buf_index + offset;
639 pending = f->buf_size - index;
645 if (size > pending) {
649 memcpy(buf, f->buf + index, size);
653 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
658 while (pending > 0) {
661 res = qemu_peek_buffer(f, buf, pending, 0);
665 qemu_file_skip(f, res);
673 static int qemu_peek_byte(QEMUFile *f, int offset)
675 int index = f->buf_index + offset;
681 if (index >= f->buf_size) {
683 index = f->buf_index + offset;
684 if (index >= f->buf_size) {
688 return f->buf[index];
691 int qemu_get_byte(QEMUFile *f)
695 result = qemu_peek_byte(f, 0);
696 qemu_file_skip(f, 1);
700 static int64_t qemu_ftell(QEMUFile *f)
702 return f->buf_offset - f->buf_size + f->buf_index;
705 int qemu_file_rate_limit(QEMUFile *f)
707 if (f->ops->rate_limit)
708 return f->ops->rate_limit(f->opaque);
713 int64_t qemu_file_get_rate_limit(QEMUFile *f)
715 if (f->ops->get_rate_limit)
716 return f->ops->get_rate_limit(f->opaque);
721 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
723 /* any failed or completed migration keeps its state to allow probing of
724 * migration data, but has no associated file anymore */
725 if (f && f->ops->set_rate_limit)
726 return f->ops->set_rate_limit(f->opaque, new_rate);
731 void qemu_put_be16(QEMUFile *f, unsigned int v)
733 qemu_put_byte(f, v >> 8);
737 void qemu_put_be32(QEMUFile *f, unsigned int v)
739 qemu_put_byte(f, v >> 24);
740 qemu_put_byte(f, v >> 16);
741 qemu_put_byte(f, v >> 8);
745 void qemu_put_be64(QEMUFile *f, uint64_t v)
747 qemu_put_be32(f, v >> 32);
751 unsigned int qemu_get_be16(QEMUFile *f)
754 v = qemu_get_byte(f) << 8;
755 v |= qemu_get_byte(f);
759 unsigned int qemu_get_be32(QEMUFile *f)
762 v = qemu_get_byte(f) << 24;
763 v |= qemu_get_byte(f) << 16;
764 v |= qemu_get_byte(f) << 8;
765 v |= qemu_get_byte(f);
769 uint64_t qemu_get_be64(QEMUFile *f)
772 v = (uint64_t)qemu_get_be32(f) << 32;
773 v |= qemu_get_be32(f);
780 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
782 uint64_t expire_time;
784 expire_time = qemu_timer_expire_time_ns(ts);
785 qemu_put_be64(f, expire_time);
788 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
790 uint64_t expire_time;
792 expire_time = qemu_get_be64(f);
793 if (expire_time != -1) {
794 qemu_mod_timer_ns(ts, expire_time);
803 static int get_bool(QEMUFile *f, void *pv, size_t size)
806 *v = qemu_get_byte(f);
810 static void put_bool(QEMUFile *f, void *pv, size_t size)
813 qemu_put_byte(f, *v);
816 const VMStateInfo vmstate_info_bool = {
824 static int get_int8(QEMUFile *f, void *pv, size_t size)
831 static void put_int8(QEMUFile *f, void *pv, size_t size)
837 const VMStateInfo vmstate_info_int8 = {
845 static int get_int16(QEMUFile *f, void *pv, size_t size)
848 qemu_get_sbe16s(f, v);
852 static void put_int16(QEMUFile *f, void *pv, size_t size)
855 qemu_put_sbe16s(f, v);
858 const VMStateInfo vmstate_info_int16 = {
866 static int get_int32(QEMUFile *f, void *pv, size_t size)
869 qemu_get_sbe32s(f, v);
873 static void put_int32(QEMUFile *f, void *pv, size_t size)
876 qemu_put_sbe32s(f, v);
879 const VMStateInfo vmstate_info_int32 = {
885 /* 32 bit int. See that the received value is the same than the one
888 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
892 qemu_get_sbe32s(f, &v2);
899 const VMStateInfo vmstate_info_int32_equal = {
900 .name = "int32 equal",
901 .get = get_int32_equal,
905 /* 32 bit int. See that the received value is the less or the same
906 than the one in the field */
908 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
912 qemu_get_sbe32s(f, &new);
919 const VMStateInfo vmstate_info_int32_le = {
920 .name = "int32 equal",
927 static int get_int64(QEMUFile *f, void *pv, size_t size)
930 qemu_get_sbe64s(f, v);
934 static void put_int64(QEMUFile *f, void *pv, size_t size)
937 qemu_put_sbe64s(f, v);
940 const VMStateInfo vmstate_info_int64 = {
946 /* 8 bit unsigned int */
948 static int get_uint8(QEMUFile *f, void *pv, size_t size)
955 static void put_uint8(QEMUFile *f, void *pv, size_t size)
961 const VMStateInfo vmstate_info_uint8 = {
967 /* 16 bit unsigned int */
969 static int get_uint16(QEMUFile *f, void *pv, size_t size)
972 qemu_get_be16s(f, v);
976 static void put_uint16(QEMUFile *f, void *pv, size_t size)
979 qemu_put_be16s(f, v);
982 const VMStateInfo vmstate_info_uint16 = {
988 /* 32 bit unsigned int */
990 static int get_uint32(QEMUFile *f, void *pv, size_t size)
993 qemu_get_be32s(f, v);
997 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1000 qemu_put_be32s(f, v);
1003 const VMStateInfo vmstate_info_uint32 = {
1009 /* 32 bit uint. See that the received value is the same than the one
1012 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1016 qemu_get_be32s(f, &v2);
1024 const VMStateInfo vmstate_info_uint32_equal = {
1025 .name = "uint32 equal",
1026 .get = get_uint32_equal,
1030 /* 64 bit unsigned int */
1032 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1035 qemu_get_be64s(f, v);
1039 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1042 qemu_put_be64s(f, v);
1045 const VMStateInfo vmstate_info_uint64 = {
1051 /* 8 bit int. See that the received value is the same than the one
1054 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1058 qemu_get_8s(f, &v2);
1065 const VMStateInfo vmstate_info_uint8_equal = {
1066 .name = "uint8 equal",
1067 .get = get_uint8_equal,
1071 /* 16 bit unsigned int int. See that the received value is the same than the one
1074 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1078 qemu_get_be16s(f, &v2);
1085 const VMStateInfo vmstate_info_uint16_equal = {
1086 .name = "uint16 equal",
1087 .get = get_uint16_equal,
1093 static int get_timer(QEMUFile *f, void *pv, size_t size)
1096 qemu_get_timer(f, v);
1100 static void put_timer(QEMUFile *f, void *pv, size_t size)
1103 qemu_put_timer(f, v);
1106 const VMStateInfo vmstate_info_timer = {
1112 /* uint8_t buffers */
1114 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1117 qemu_get_buffer(f, v, size);
1121 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1124 qemu_put_buffer(f, v, size);
1127 const VMStateInfo vmstate_info_buffer = {
1133 /* unused buffers: space that was used for some fields that are
1134 not useful anymore */
1136 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1142 block_len = MIN(sizeof(buf), size);
1144 qemu_get_buffer(f, buf, block_len);
1149 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1151 static const uint8_t buf[1024];
1155 block_len = MIN(sizeof(buf), size);
1157 qemu_put_buffer(f, buf, block_len);
1161 const VMStateInfo vmstate_info_unused_buffer = {
1162 .name = "unused_buffer",
1163 .get = get_unused_buffer,
1164 .put = put_unused_buffer,
1167 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1168 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1169 * bit words with the bits in big endian order. The in-memory format
1170 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1172 /* This is the number of 64 bit words sent over the wire */
1173 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1174 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1176 unsigned long *bmp = pv;
1178 for (i = 0; i < BITS_TO_U64S(size); i++) {
1179 uint64_t w = qemu_get_be64(f);
1181 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1182 bmp[idx++] = w >> 32;
1188 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1190 unsigned long *bmp = pv;
1192 for (i = 0; i < BITS_TO_U64S(size); i++) {
1193 uint64_t w = bmp[idx++];
1194 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1195 w |= ((uint64_t)bmp[idx++]) << 32;
1197 qemu_put_be64(f, w);
1201 const VMStateInfo vmstate_info_bitmap = {
1207 typedef struct CompatEntry {
1212 typedef struct SaveStateEntry {
1213 QTAILQ_ENTRY(SaveStateEntry) entry;
1219 SaveVMHandlers *ops;
1220 const VMStateDescription *vmsd;
1222 CompatEntry *compat;
1228 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1229 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1230 static int global_section_id;
1232 static int calculate_new_instance_id(const char *idstr)
1235 int instance_id = 0;
1237 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1238 if (strcmp(idstr, se->idstr) == 0
1239 && instance_id <= se->instance_id) {
1240 instance_id = se->instance_id + 1;
1246 static int calculate_compat_instance_id(const char *idstr)
1249 int instance_id = 0;
1251 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1255 if (strcmp(idstr, se->compat->idstr) == 0
1256 && instance_id <= se->compat->instance_id) {
1257 instance_id = se->compat->instance_id + 1;
1263 /* TODO: Individual devices generally have very little idea about the rest
1264 of the system, so instance_id should be removed/replaced.
1265 Meanwhile pass -1 as instance_id if you do not already have a clearly
1266 distinguishing id for all instances of your device class. */
1267 int register_savevm_live(DeviceState *dev,
1271 SaveVMHandlers *ops,
1276 se = g_malloc0(sizeof(SaveStateEntry));
1277 se->version_id = version_id;
1278 se->section_id = global_section_id++;
1280 se->opaque = opaque;
1283 /* if this is a live_savem then set is_ram */
1284 if (ops->save_live_setup != NULL) {
1289 char *id = qdev_get_dev_path(dev);
1291 pstrcpy(se->idstr, sizeof(se->idstr), id);
1292 pstrcat(se->idstr, sizeof(se->idstr), "/");
1295 se->compat = g_malloc0(sizeof(CompatEntry));
1296 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1297 se->compat->instance_id = instance_id == -1 ?
1298 calculate_compat_instance_id(idstr) : instance_id;
1302 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1304 if (instance_id == -1) {
1305 se->instance_id = calculate_new_instance_id(se->idstr);
1307 se->instance_id = instance_id;
1309 assert(!se->compat || se->instance_id == 0);
1310 /* add at the end of list */
1311 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1315 int register_savevm(DeviceState *dev,
1319 SaveStateHandler *save_state,
1320 LoadStateHandler *load_state,
1323 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1324 ops->save_state = save_state;
1325 ops->load_state = load_state;
1326 return register_savevm_live(dev, idstr, instance_id, version_id,
1330 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1332 SaveStateEntry *se, *new_se;
1336 char *path = qdev_get_dev_path(dev);
1338 pstrcpy(id, sizeof(id), path);
1339 pstrcat(id, sizeof(id), "/");
1343 pstrcat(id, sizeof(id), idstr);
1345 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1346 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1347 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1357 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1358 const VMStateDescription *vmsd,
1359 void *opaque, int alias_id,
1360 int required_for_version)
1364 /* If this triggers, alias support can be dropped for the vmsd. */
1365 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1367 se = g_malloc0(sizeof(SaveStateEntry));
1368 se->version_id = vmsd->version_id;
1369 se->section_id = global_section_id++;
1370 se->opaque = opaque;
1372 se->alias_id = alias_id;
1373 se->no_migrate = vmsd->unmigratable;
1376 char *id = qdev_get_dev_path(dev);
1378 pstrcpy(se->idstr, sizeof(se->idstr), id);
1379 pstrcat(se->idstr, sizeof(se->idstr), "/");
1382 se->compat = g_malloc0(sizeof(CompatEntry));
1383 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1384 se->compat->instance_id = instance_id == -1 ?
1385 calculate_compat_instance_id(vmsd->name) : instance_id;
1389 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1391 if (instance_id == -1) {
1392 se->instance_id = calculate_new_instance_id(se->idstr);
1394 se->instance_id = instance_id;
1396 assert(!se->compat || se->instance_id == 0);
1397 /* add at the end of list */
1398 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1402 int vmstate_register(DeviceState *dev, int instance_id,
1403 const VMStateDescription *vmsd, void *opaque)
1405 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1409 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1412 SaveStateEntry *se, *new_se;
1414 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1415 if (se->vmsd == vmsd && se->opaque == opaque) {
1416 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1425 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1427 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1430 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1431 void *opaque, int version_id)
1433 VMStateField *field = vmsd->fields;
1436 if (version_id > vmsd->version_id) {
1439 if (version_id < vmsd->minimum_version_id_old) {
1442 if (version_id < vmsd->minimum_version_id) {
1443 return vmsd->load_state_old(f, opaque, version_id);
1445 if (vmsd->pre_load) {
1446 int ret = vmsd->pre_load(opaque);
1450 while(field->name) {
1451 if ((field->field_exists &&
1452 field->field_exists(opaque, version_id)) ||
1453 (!field->field_exists &&
1454 field->version_id <= version_id)) {
1455 void *base_addr = opaque + field->offset;
1457 int size = field->size;
1459 if (field->flags & VMS_VBUFFER) {
1460 size = *(int32_t *)(opaque+field->size_offset);
1461 if (field->flags & VMS_MULTIPLY) {
1462 size *= field->size;
1465 if (field->flags & VMS_ARRAY) {
1466 n_elems = field->num;
1467 } else if (field->flags & VMS_VARRAY_INT32) {
1468 n_elems = *(int32_t *)(opaque+field->num_offset);
1469 } else if (field->flags & VMS_VARRAY_UINT32) {
1470 n_elems = *(uint32_t *)(opaque+field->num_offset);
1471 } else if (field->flags & VMS_VARRAY_UINT16) {
1472 n_elems = *(uint16_t *)(opaque+field->num_offset);
1473 } else if (field->flags & VMS_VARRAY_UINT8) {
1474 n_elems = *(uint8_t *)(opaque+field->num_offset);
1476 if (field->flags & VMS_POINTER) {
1477 base_addr = *(void **)base_addr + field->start;
1479 for (i = 0; i < n_elems; i++) {
1480 void *addr = base_addr + size * i;
1482 if (field->flags & VMS_ARRAY_OF_POINTER) {
1483 addr = *(void **)addr;
1485 if (field->flags & VMS_STRUCT) {
1486 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1488 ret = field->info->get(f, addr, size);
1498 ret = vmstate_subsection_load(f, vmsd, opaque);
1502 if (vmsd->post_load) {
1503 return vmsd->post_load(opaque, version_id);
1508 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1511 VMStateField *field = vmsd->fields;
1513 if (vmsd->pre_save) {
1514 vmsd->pre_save(opaque);
1516 while(field->name) {
1517 if (!field->field_exists ||
1518 field->field_exists(opaque, vmsd->version_id)) {
1519 void *base_addr = opaque + field->offset;
1521 int size = field->size;
1523 if (field->flags & VMS_VBUFFER) {
1524 size = *(int32_t *)(opaque+field->size_offset);
1525 if (field->flags & VMS_MULTIPLY) {
1526 size *= field->size;
1529 if (field->flags & VMS_ARRAY) {
1530 n_elems = field->num;
1531 } else if (field->flags & VMS_VARRAY_INT32) {
1532 n_elems = *(int32_t *)(opaque+field->num_offset);
1533 } else if (field->flags & VMS_VARRAY_UINT32) {
1534 n_elems = *(uint32_t *)(opaque+field->num_offset);
1535 } else if (field->flags & VMS_VARRAY_UINT16) {
1536 n_elems = *(uint16_t *)(opaque+field->num_offset);
1537 } else if (field->flags & VMS_VARRAY_UINT8) {
1538 n_elems = *(uint8_t *)(opaque+field->num_offset);
1540 if (field->flags & VMS_POINTER) {
1541 base_addr = *(void **)base_addr + field->start;
1543 for (i = 0; i < n_elems; i++) {
1544 void *addr = base_addr + size * i;
1546 if (field->flags & VMS_ARRAY_OF_POINTER) {
1547 addr = *(void **)addr;
1549 if (field->flags & VMS_STRUCT) {
1550 vmstate_save_state(f, field->vmsd, addr);
1552 field->info->put(f, addr, size);
1558 vmstate_subsection_save(f, vmsd, opaque);
1561 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1563 if (!se->vmsd) { /* Old style */
1564 return se->ops->load_state(f, se->opaque, version_id);
1566 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1569 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1571 if (!se->vmsd) { /* Old style */
1572 se->ops->save_state(f, se->opaque);
1575 vmstate_save_state(f,se->vmsd, se->opaque);
1578 #define QEMU_VM_FILE_MAGIC 0x5145564d
1579 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1580 #define QEMU_VM_FILE_VERSION 0x00000003
1582 #define QEMU_VM_EOF 0x00
1583 #define QEMU_VM_SECTION_START 0x01
1584 #define QEMU_VM_SECTION_PART 0x02
1585 #define QEMU_VM_SECTION_END 0x03
1586 #define QEMU_VM_SECTION_FULL 0x04
1587 #define QEMU_VM_SUBSECTION 0x05
1589 bool qemu_savevm_state_blocked(Error **errp)
1593 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1594 if (se->no_migrate) {
1595 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1602 int qemu_savevm_state_begin(QEMUFile *f,
1603 const MigrationParams *params)
1608 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1609 if (!se->ops || !se->ops->set_params) {
1612 se->ops->set_params(params, se->opaque);
1615 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1616 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1618 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1621 if (!se->ops || !se->ops->save_live_setup) {
1624 if (se->ops && se->ops->is_active) {
1625 if (!se->ops->is_active(se->opaque)) {
1630 qemu_put_byte(f, QEMU_VM_SECTION_START);
1631 qemu_put_be32(f, se->section_id);
1634 len = strlen(se->idstr);
1635 qemu_put_byte(f, len);
1636 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1638 qemu_put_be32(f, se->instance_id);
1639 qemu_put_be32(f, se->version_id);
1641 ret = se->ops->save_live_setup(f, se->opaque);
1643 qemu_savevm_state_cancel(f);
1647 ret = qemu_file_get_error(f);
1649 qemu_savevm_state_cancel(f);
1657 * this function has three return values:
1658 * negative: there was one error, and we have -errno.
1659 * 0 : We haven't finished, caller have to go again
1660 * 1 : We have finished, we can go to complete phase
1662 int qemu_savevm_state_iterate(QEMUFile *f)
1667 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1668 if (!se->ops || !se->ops->save_live_iterate) {
1671 if (se->ops && se->ops->is_active) {
1672 if (!se->ops->is_active(se->opaque)) {
1676 if (qemu_file_rate_limit(f)) {
1679 trace_savevm_section_start();
1681 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1682 qemu_put_be32(f, se->section_id);
1684 ret = se->ops->save_live_iterate(f, se->opaque);
1685 trace_savevm_section_end(se->section_id);
1688 /* Do not proceed to the next vmstate before this one reported
1689 completion of the current stage. This serializes the migration
1690 and reduces the probability that a faster changing state is
1691 synchronized over and over again. */
1698 ret = qemu_file_get_error(f);
1700 qemu_savevm_state_cancel(f);
1705 int qemu_savevm_state_complete(QEMUFile *f)
1710 cpu_synchronize_all_states();
1712 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1713 if (!se->ops || !se->ops->save_live_complete) {
1716 if (se->ops && se->ops->is_active) {
1717 if (!se->ops->is_active(se->opaque)) {
1721 trace_savevm_section_start();
1723 qemu_put_byte(f, QEMU_VM_SECTION_END);
1724 qemu_put_be32(f, se->section_id);
1726 ret = se->ops->save_live_complete(f, se->opaque);
1727 trace_savevm_section_end(se->section_id);
1733 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1736 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1739 trace_savevm_section_start();
1741 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1742 qemu_put_be32(f, se->section_id);
1745 len = strlen(se->idstr);
1746 qemu_put_byte(f, len);
1747 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1749 qemu_put_be32(f, se->instance_id);
1750 qemu_put_be32(f, se->version_id);
1752 vmstate_save(f, se);
1753 trace_savevm_section_end(se->section_id);
1756 qemu_put_byte(f, QEMU_VM_EOF);
1758 return qemu_file_get_error(f);
1761 void qemu_savevm_state_cancel(QEMUFile *f)
1765 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1766 if (se->ops && se->ops->cancel) {
1767 se->ops->cancel(se->opaque);
1772 static int qemu_savevm_state(QEMUFile *f)
1775 MigrationParams params = {
1780 if (qemu_savevm_state_blocked(NULL)) {
1785 ret = qemu_savevm_state_begin(f, ¶ms);
1790 ret = qemu_savevm_state_iterate(f);
1795 ret = qemu_savevm_state_complete(f);
1799 ret = qemu_file_get_error(f);
1805 static int qemu_save_device_state(QEMUFile *f)
1809 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1810 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1812 cpu_synchronize_all_states();
1814 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1820 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1825 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1826 qemu_put_be32(f, se->section_id);
1829 len = strlen(se->idstr);
1830 qemu_put_byte(f, len);
1831 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1833 qemu_put_be32(f, se->instance_id);
1834 qemu_put_be32(f, se->version_id);
1836 vmstate_save(f, se);
1839 qemu_put_byte(f, QEMU_VM_EOF);
1841 return qemu_file_get_error(f);
1844 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1848 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1849 if (!strcmp(se->idstr, idstr) &&
1850 (instance_id == se->instance_id ||
1851 instance_id == se->alias_id))
1853 /* Migrating from an older version? */
1854 if (strstr(se->idstr, idstr) && se->compat) {
1855 if (!strcmp(se->compat->idstr, idstr) &&
1856 (instance_id == se->compat->instance_id ||
1857 instance_id == se->alias_id))
1864 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1866 while(sub && sub->needed) {
1867 if (strcmp(idstr, sub->vmsd->name) == 0) {
1875 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1878 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
1881 uint8_t version_id, len, size;
1882 const VMStateDescription *sub_vmsd;
1884 len = qemu_peek_byte(f, 1);
1885 if (len < strlen(vmsd->name) + 1) {
1886 /* subsection name has be be "section_name/a" */
1889 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1895 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1896 /* it don't have a valid subsection name */
1899 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
1900 if (sub_vmsd == NULL) {
1903 qemu_file_skip(f, 1); /* subsection */
1904 qemu_file_skip(f, 1); /* len */
1905 qemu_file_skip(f, len); /* idstr */
1906 version_id = qemu_get_be32(f);
1908 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1916 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1919 const VMStateSubsection *sub = vmsd->subsections;
1921 while (sub && sub->needed) {
1922 if (sub->needed(opaque)) {
1923 const VMStateDescription *vmsd = sub->vmsd;
1926 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1927 len = strlen(vmsd->name);
1928 qemu_put_byte(f, len);
1929 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1930 qemu_put_be32(f, vmsd->version_id);
1931 vmstate_save_state(f, vmsd, opaque);
1937 typedef struct LoadStateEntry {
1938 QLIST_ENTRY(LoadStateEntry) entry;
1944 int qemu_loadvm_state(QEMUFile *f)
1946 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1947 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1948 LoadStateEntry *le, *new_le;
1949 uint8_t section_type;
1953 if (qemu_savevm_state_blocked(NULL)) {
1957 v = qemu_get_be32(f);
1958 if (v != QEMU_VM_FILE_MAGIC)
1961 v = qemu_get_be32(f);
1962 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1963 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1966 if (v != QEMU_VM_FILE_VERSION)
1969 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1970 uint32_t instance_id, version_id, section_id;
1975 switch (section_type) {
1976 case QEMU_VM_SECTION_START:
1977 case QEMU_VM_SECTION_FULL:
1978 /* Read section start */
1979 section_id = qemu_get_be32(f);
1980 len = qemu_get_byte(f);
1981 qemu_get_buffer(f, (uint8_t *)idstr, len);
1983 instance_id = qemu_get_be32(f);
1984 version_id = qemu_get_be32(f);
1986 /* Find savevm section */
1987 se = find_se(idstr, instance_id);
1989 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1994 /* Validate version */
1995 if (version_id > se->version_id) {
1996 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1997 version_id, idstr, se->version_id);
2003 le = g_malloc0(sizeof(*le));
2006 le->section_id = section_id;
2007 le->version_id = version_id;
2008 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2010 ret = vmstate_load(f, le->se, le->version_id);
2012 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2013 instance_id, idstr);
2017 case QEMU_VM_SECTION_PART:
2018 case QEMU_VM_SECTION_END:
2019 section_id = qemu_get_be32(f);
2021 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2022 if (le->section_id == section_id) {
2027 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2032 ret = vmstate_load(f, le->se, le->version_id);
2034 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2040 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2046 cpu_synchronize_all_post_init();
2051 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2052 QLIST_REMOVE(le, entry);
2057 ret = qemu_file_get_error(f);
2063 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2066 QEMUSnapshotInfo *sn_tab, *sn;
2070 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2073 for(i = 0; i < nb_sns; i++) {
2075 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2086 * Deletes snapshots of a given name in all opened images.
2088 static int del_existing_snapshots(Monitor *mon, const char *name)
2090 BlockDriverState *bs;
2091 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2095 while ((bs = bdrv_next(bs))) {
2096 if (bdrv_can_snapshot(bs) &&
2097 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2099 ret = bdrv_snapshot_delete(bs, name);
2102 "Error while deleting snapshot on '%s'\n",
2103 bdrv_get_device_name(bs));
2112 void do_savevm(Monitor *mon, const QDict *qdict)
2114 BlockDriverState *bs, *bs1;
2115 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2118 int saved_vm_running;
2119 uint64_t vm_state_size;
2127 const char *name = qdict_get_try_str(qdict, "name");
2129 /* Verify if there is a device that doesn't support snapshots and is writable */
2131 while ((bs = bdrv_next(bs))) {
2133 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2137 if (!bdrv_can_snapshot(bs)) {
2138 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2139 bdrv_get_device_name(bs));
2144 bs = bdrv_snapshots();
2146 monitor_printf(mon, "No block device can accept snapshots\n");
2150 saved_vm_running = runstate_is_running();
2151 vm_stop(RUN_STATE_SAVE_VM);
2153 memset(sn, 0, sizeof(*sn));
2155 /* fill auxiliary fields */
2158 sn->date_sec = tb.time;
2159 sn->date_nsec = tb.millitm * 1000000;
2161 gettimeofday(&tv, NULL);
2162 sn->date_sec = tv.tv_sec;
2163 sn->date_nsec = tv.tv_usec * 1000;
2165 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2168 ret = bdrv_snapshot_find(bs, old_sn, name);
2170 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2171 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2173 pstrcpy(sn->name, sizeof(sn->name), name);
2178 ptm = localtime(&t);
2179 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2181 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2182 localtime_r((const time_t *)&tv.tv_sec, &tm);
2183 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2187 /* Delete old snapshots of the same name */
2188 if (name && del_existing_snapshots(mon, name) < 0) {
2192 /* save the VM state */
2193 f = qemu_fopen_bdrv(bs, 1);
2195 monitor_printf(mon, "Could not open VM state file\n");
2198 ret = qemu_savevm_state(f);
2199 vm_state_size = qemu_ftell(f);
2202 monitor_printf(mon, "Error %d while writing VM\n", ret);
2206 /* create the snapshots */
2209 while ((bs1 = bdrv_next(bs1))) {
2210 if (bdrv_can_snapshot(bs1)) {
2211 /* Write VM state size only to the image that contains the state */
2212 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2213 ret = bdrv_snapshot_create(bs1, sn);
2215 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2216 bdrv_get_device_name(bs1));
2222 if (saved_vm_running)
2226 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2229 int saved_vm_running;
2232 saved_vm_running = runstate_is_running();
2233 vm_stop(RUN_STATE_SAVE_VM);
2235 f = qemu_fopen(filename, "wb");
2237 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2240 ret = qemu_save_device_state(f);
2243 error_set(errp, QERR_IO_ERROR);
2247 if (saved_vm_running)
2251 int load_vmstate(const char *name)
2253 BlockDriverState *bs, *bs_vm_state;
2254 QEMUSnapshotInfo sn;
2258 bs_vm_state = bdrv_snapshots();
2260 error_report("No block device supports snapshots");
2264 /* Don't even try to load empty VM states */
2265 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2268 } else if (sn.vm_state_size == 0) {
2269 error_report("This is a disk-only snapshot. Revert to it offline "
2274 /* Verify if there is any device that doesn't support snapshots and is
2275 writable and check if the requested snapshot is available too. */
2277 while ((bs = bdrv_next(bs))) {
2279 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2283 if (!bdrv_can_snapshot(bs)) {
2284 error_report("Device '%s' is writable but does not support snapshots.",
2285 bdrv_get_device_name(bs));
2289 ret = bdrv_snapshot_find(bs, &sn, name);
2291 error_report("Device '%s' does not have the requested snapshot '%s'",
2292 bdrv_get_device_name(bs), name);
2297 /* Flush all IO requests so they don't interfere with the new state. */
2301 while ((bs = bdrv_next(bs))) {
2302 if (bdrv_can_snapshot(bs)) {
2303 ret = bdrv_snapshot_goto(bs, name);
2305 error_report("Error %d while activating snapshot '%s' on '%s'",
2306 ret, name, bdrv_get_device_name(bs));
2312 /* restore the VM state */
2313 f = qemu_fopen_bdrv(bs_vm_state, 0);
2315 error_report("Could not open VM state file");
2319 qemu_system_reset(VMRESET_SILENT);
2320 ret = qemu_loadvm_state(f);
2324 error_report("Error %d while loading VM state", ret);
2331 void do_delvm(Monitor *mon, const QDict *qdict)
2333 BlockDriverState *bs, *bs1;
2335 const char *name = qdict_get_str(qdict, "name");
2337 bs = bdrv_snapshots();
2339 monitor_printf(mon, "No block device supports snapshots\n");
2344 while ((bs1 = bdrv_next(bs1))) {
2345 if (bdrv_can_snapshot(bs1)) {
2346 ret = bdrv_snapshot_delete(bs1, name);
2348 if (ret == -ENOTSUP)
2350 "Snapshots not supported on device '%s'\n",
2351 bdrv_get_device_name(bs1));
2353 monitor_printf(mon, "Error %d while deleting snapshot on "
2354 "'%s'\n", ret, bdrv_get_device_name(bs1));
2360 void do_info_snapshots(Monitor *mon)
2362 BlockDriverState *bs, *bs1;
2363 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2364 int nb_sns, i, ret, available;
2366 int *available_snapshots;
2369 bs = bdrv_snapshots();
2371 monitor_printf(mon, "No available block device supports snapshots\n");
2375 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2377 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2382 monitor_printf(mon, "There is no snapshot available.\n");
2386 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2388 for (i = 0; i < nb_sns; i++) {
2393 while ((bs1 = bdrv_next(bs1))) {
2394 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2395 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2404 available_snapshots[total] = i;
2410 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2411 for (i = 0; i < total; i++) {
2412 sn = &sn_tab[available_snapshots[i]];
2413 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2416 monitor_printf(mon, "There is no suitable snapshot available\n");
2420 g_free(available_snapshots);
2424 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2426 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2427 memory_region_name(mr), dev);
2430 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2432 /* Nothing do to while the implementation is in RAMBlock */
2435 void vmstate_register_ram_global(MemoryRegion *mr)
2437 vmstate_register_ram(mr, NULL);
2446 nzrun = length byte...
2448 length = uleb128 encoded integer
2450 int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2451 uint8_t *dst, int dlen)
2453 uint32_t zrun_len = 0, nzrun_len = 0;
2456 uint8_t *nzrun_start = NULL;
2458 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2467 /* not aligned to sizeof(long) */
2468 res = (slen - i) % sizeof(long);
2469 while (res && old_buf[i] == new_buf[i]) {
2475 /* word at a time for speed */
2478 (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2480 zrun_len += sizeof(long);
2483 /* go over the rest */
2484 while (i < slen && old_buf[i] == new_buf[i]) {
2490 /* buffer unchanged */
2491 if (zrun_len == slen) {
2495 /* skip last zero run */
2500 d += uleb128_encode_small(dst + d, zrun_len);
2503 nzrun_start = new_buf + i;
2509 /* not aligned to sizeof(long) */
2510 res = (slen - i) % sizeof(long);
2511 while (res && old_buf[i] != new_buf[i]) {
2517 /* word at a time for speed, use of 32-bit long okay */
2519 /* truncation to 32-bit long okay */
2520 long mask = (long)0x0101010101010101ULL;
2522 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2523 if ((xor - mask) & ~xor & (mask << 7)) {
2524 /* found the end of an nzrun within the current long */
2525 while (old_buf[i] != new_buf[i]) {
2532 nzrun_len += sizeof(long);
2537 d += uleb128_encode_small(dst + d, nzrun_len);
2539 if (d + nzrun_len > dlen) {
2542 memcpy(dst + d, nzrun_start, nzrun_len);
2550 int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2559 if ((slen - i) < 2) {
2563 ret = uleb128_decode_small(src + i, &count);
2564 if (ret < 0 || (i && !count)) {
2576 if ((slen - i) < 2) {
2580 ret = uleb128_decode_small(src + i, &count);
2581 if (ret < 0 || !count) {
2587 if (d + count > dlen || i + count > slen) {
2591 memcpy(dst + d, src + i, count);