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"
28 #include <arpa/inet.h>
35 #include "qemu-common.h"
39 #include "monitor/monitor.h"
40 #include "sysemu/sysemu.h"
41 #include "qemu/timer.h"
42 #include "audio/audio.h"
43 #include "migration/migration.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"
50 #include "qemu/bitops.h"
52 #define SELF_ANNOUNCE_ROUNDS 5
55 #define ETH_P_RARP 0x8035
57 #define ARP_HTYPE_ETH 0x0001
58 #define ARP_PTYPE_IP 0x0800
59 #define ARP_OP_REQUEST_REV 0x3
61 static int announce_self_create(uint8_t *buf,
64 /* Ethernet header. */
65 memset(buf, 0xff, 6); /* destination MAC addr */
66 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
67 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
70 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
71 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
72 *(buf + 18) = 6; /* hardware addr length (ethernet) */
73 *(buf + 19) = 4; /* protocol addr length (IPv4) */
74 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
75 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
76 memset(buf + 28, 0x00, 4); /* source protocol addr */
77 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
78 memset(buf + 38, 0x00, 4); /* target protocol addr */
80 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
81 memset(buf + 42, 0x00, 18);
83 return 60; /* len (FCS will be added by hardware) */
86 static void qemu_announce_self_iter(NICState *nic, void *opaque)
91 len = announce_self_create(buf, nic->conf->macaddr.a);
93 qemu_send_packet_raw(&nic->nc, buf, len);
97 static void qemu_announce_self_once(void *opaque)
99 static int count = SELF_ANNOUNCE_ROUNDS;
100 QEMUTimer *timer = *(QEMUTimer **)opaque;
102 qemu_foreach_nic(qemu_announce_self_iter, NULL);
105 /* delay 50ms, 150ms, 250ms, ... */
106 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
107 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
109 qemu_del_timer(timer);
110 qemu_free_timer(timer);
114 void qemu_announce_self(void)
116 static QEMUTimer *timer;
117 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
118 qemu_announce_self_once(&timer);
121 /***********************************************************/
122 /* savevm/loadvm support */
124 #define IO_BUF_SIZE 32768
127 const QEMUFileOps *ops;
131 int64_t buf_offset; /* start of buffer when writing, end of buffer
134 int buf_size; /* 0 when writing */
135 uint8_t buf[IO_BUF_SIZE];
140 typedef struct QEMUFileStdio
146 typedef struct QEMUFileSocket
152 static int socket_get_fd(void *opaque)
154 QEMUFileSocket *s = opaque;
159 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
161 QEMUFileSocket *s = opaque;
165 len = qemu_recv(s->fd, buf, size, 0);
169 if (socket_error() == EAGAIN) {
170 assert(qemu_in_coroutine());
171 qemu_coroutine_yield();
172 } else if (socket_error() != EINTR) {
178 len = -socket_error();
183 static int socket_close(void *opaque)
185 QEMUFileSocket *s = opaque;
191 static int stdio_get_fd(void *opaque)
193 QEMUFileStdio *s = opaque;
195 return fileno(s->stdio_file);
198 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
200 QEMUFileStdio *s = opaque;
201 return fwrite(buf, 1, size, s->stdio_file);
204 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
206 QEMUFileStdio *s = opaque;
207 FILE *fp = s->stdio_file;
212 bytes = fread(buf, 1, size, fp);
213 if (bytes != 0 || !ferror(fp)) {
216 if (errno == EAGAIN) {
217 assert(qemu_in_coroutine());
218 qemu_coroutine_yield();
219 } else if (errno != EINTR) {
226 static int stdio_pclose(void *opaque)
228 QEMUFileStdio *s = opaque;
230 ret = pclose(s->stdio_file);
238 static int stdio_fclose(void *opaque)
240 QEMUFileStdio *s = opaque;
242 if (fclose(s->stdio_file) == EOF) {
249 static const QEMUFileOps stdio_pipe_read_ops = {
250 .get_fd = stdio_get_fd,
251 .get_buffer = stdio_get_buffer,
252 .close = stdio_pclose
255 static const QEMUFileOps stdio_pipe_write_ops = {
256 .get_fd = stdio_get_fd,
257 .put_buffer = stdio_put_buffer,
258 .close = stdio_pclose
261 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
265 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
266 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
270 s = g_malloc0(sizeof(QEMUFileStdio));
272 s->stdio_file = stdio_file;
275 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
277 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
282 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
286 popen_file = popen(command, mode);
287 if(popen_file == NULL) {
291 return qemu_popen(popen_file, mode);
294 static const QEMUFileOps stdio_file_read_ops = {
295 .get_fd = stdio_get_fd,
296 .get_buffer = stdio_get_buffer,
297 .close = stdio_fclose
300 static const QEMUFileOps stdio_file_write_ops = {
301 .get_fd = stdio_get_fd,
302 .put_buffer = stdio_put_buffer,
303 .close = stdio_fclose
306 QEMUFile *qemu_fdopen(int fd, const char *mode)
311 (mode[0] != 'r' && mode[0] != 'w') ||
312 mode[1] != 'b' || mode[2] != 0) {
313 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
317 s = g_malloc0(sizeof(QEMUFileStdio));
318 s->stdio_file = fdopen(fd, mode);
323 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
325 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
334 static const QEMUFileOps socket_read_ops = {
335 .get_fd = socket_get_fd,
336 .get_buffer = socket_get_buffer,
337 .close = socket_close
340 QEMUFile *qemu_fopen_socket(int fd)
342 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
345 s->file = qemu_fopen_ops(s, &socket_read_ops);
349 QEMUFile *qemu_fopen(const char *filename, const char *mode)
354 (mode[0] != 'r' && mode[0] != 'w') ||
355 mode[1] != 'b' || mode[2] != 0) {
356 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
360 s = g_malloc0(sizeof(QEMUFileStdio));
362 s->stdio_file = fopen(filename, mode);
367 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
369 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
377 static int block_put_buffer(void *opaque, const uint8_t *buf,
378 int64_t pos, int size)
380 bdrv_save_vmstate(opaque, buf, pos, size);
384 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
386 return bdrv_load_vmstate(opaque, buf, pos, size);
389 static int bdrv_fclose(void *opaque)
391 return bdrv_flush(opaque);
394 static const QEMUFileOps bdrv_read_ops = {
395 .get_buffer = block_get_buffer,
399 static const QEMUFileOps bdrv_write_ops = {
400 .put_buffer = block_put_buffer,
404 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
407 return qemu_fopen_ops(bs, &bdrv_write_ops);
408 return qemu_fopen_ops(bs, &bdrv_read_ops);
411 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
415 f = g_malloc0(sizeof(QEMUFile));
424 int qemu_file_get_error(QEMUFile *f)
426 return f->last_error;
429 static void qemu_file_set_error(QEMUFile *f, int ret)
434 /** Flushes QEMUFile buffer
437 static int qemu_fflush(QEMUFile *f)
441 if (!f->ops->put_buffer)
444 if (f->is_write && f->buf_index > 0) {
445 ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
447 f->buf_offset += f->buf_index;
454 static void qemu_fill_buffer(QEMUFile *f)
459 if (!f->ops->get_buffer)
465 pending = f->buf_size - f->buf_index;
467 memmove(f->buf, f->buf + f->buf_index, pending);
470 f->buf_size = pending;
472 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
473 IO_BUF_SIZE - pending);
476 f->buf_offset += len;
477 } else if (len == 0) {
478 qemu_file_set_error(f, -EIO);
479 } else if (len != -EAGAIN)
480 qemu_file_set_error(f, len);
483 int qemu_get_fd(QEMUFile *f)
485 if (f->ops->get_fd) {
486 return f->ops->get_fd(f->opaque);
493 * Returns negative error value if any error happened on previous operations or
494 * while closing the file. Returns 0 or positive number on success.
496 * The meaning of return value on success depends on the specific backend
499 int qemu_fclose(QEMUFile *f)
502 ret = qemu_fflush(f);
505 int ret2 = f->ops->close(f->opaque);
510 /* If any error was spotted before closing, we should report it
511 * instead of the close() return value.
520 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
528 if (f->is_write == 0 && f->buf_index > 0) {
530 "Attempted to write to buffer while read buffer is not empty\n");
535 l = IO_BUF_SIZE - f->buf_index;
538 memcpy(f->buf + f->buf_index, buf, l);
543 if (f->buf_index >= IO_BUF_SIZE) {
544 int ret = qemu_fflush(f);
546 qemu_file_set_error(f, ret);
553 void qemu_put_byte(QEMUFile *f, int v)
559 if (f->is_write == 0 && f->buf_index > 0) {
561 "Attempted to write to buffer while read buffer is not empty\n");
565 f->buf[f->buf_index++] = v;
567 if (f->buf_index >= IO_BUF_SIZE) {
568 int ret = qemu_fflush(f);
570 qemu_file_set_error(f, ret);
575 static void qemu_file_skip(QEMUFile *f, int size)
577 if (f->buf_index + size <= f->buf_size) {
578 f->buf_index += size;
582 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
591 index = f->buf_index + offset;
592 pending = f->buf_size - index;
593 if (pending < size) {
595 index = f->buf_index + offset;
596 pending = f->buf_size - index;
602 if (size > pending) {
606 memcpy(buf, f->buf + index, size);
610 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
615 while (pending > 0) {
618 res = qemu_peek_buffer(f, buf, pending, 0);
622 qemu_file_skip(f, res);
630 static int qemu_peek_byte(QEMUFile *f, int offset)
632 int index = f->buf_index + offset;
638 if (index >= f->buf_size) {
640 index = f->buf_index + offset;
641 if (index >= f->buf_size) {
645 return f->buf[index];
648 int qemu_get_byte(QEMUFile *f)
652 result = qemu_peek_byte(f, 0);
653 qemu_file_skip(f, 1);
657 static int64_t qemu_ftell(QEMUFile *f)
659 return f->buf_offset - f->buf_size + f->buf_index;
662 int qemu_file_rate_limit(QEMUFile *f)
664 if (f->ops->rate_limit)
665 return f->ops->rate_limit(f->opaque);
670 int64_t qemu_file_get_rate_limit(QEMUFile *f)
672 if (f->ops->get_rate_limit)
673 return f->ops->get_rate_limit(f->opaque);
678 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
680 /* any failed or completed migration keeps its state to allow probing of
681 * migration data, but has no associated file anymore */
682 if (f && f->ops->set_rate_limit)
683 return f->ops->set_rate_limit(f->opaque, new_rate);
688 void qemu_put_be16(QEMUFile *f, unsigned int v)
690 qemu_put_byte(f, v >> 8);
694 void qemu_put_be32(QEMUFile *f, unsigned int v)
696 qemu_put_byte(f, v >> 24);
697 qemu_put_byte(f, v >> 16);
698 qemu_put_byte(f, v >> 8);
702 void qemu_put_be64(QEMUFile *f, uint64_t v)
704 qemu_put_be32(f, v >> 32);
708 unsigned int qemu_get_be16(QEMUFile *f)
711 v = qemu_get_byte(f) << 8;
712 v |= qemu_get_byte(f);
716 unsigned int qemu_get_be32(QEMUFile *f)
719 v = qemu_get_byte(f) << 24;
720 v |= qemu_get_byte(f) << 16;
721 v |= qemu_get_byte(f) << 8;
722 v |= qemu_get_byte(f);
726 uint64_t qemu_get_be64(QEMUFile *f)
729 v = (uint64_t)qemu_get_be32(f) << 32;
730 v |= qemu_get_be32(f);
737 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
739 uint64_t expire_time;
741 expire_time = qemu_timer_expire_time_ns(ts);
742 qemu_put_be64(f, expire_time);
745 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
747 uint64_t expire_time;
749 expire_time = qemu_get_be64(f);
750 if (expire_time != -1) {
751 qemu_mod_timer_ns(ts, expire_time);
760 static int get_bool(QEMUFile *f, void *pv, size_t size)
763 *v = qemu_get_byte(f);
767 static void put_bool(QEMUFile *f, void *pv, size_t size)
770 qemu_put_byte(f, *v);
773 const VMStateInfo vmstate_info_bool = {
781 static int get_int8(QEMUFile *f, void *pv, size_t size)
788 static void put_int8(QEMUFile *f, void *pv, size_t size)
794 const VMStateInfo vmstate_info_int8 = {
802 static int get_int16(QEMUFile *f, void *pv, size_t size)
805 qemu_get_sbe16s(f, v);
809 static void put_int16(QEMUFile *f, void *pv, size_t size)
812 qemu_put_sbe16s(f, v);
815 const VMStateInfo vmstate_info_int16 = {
823 static int get_int32(QEMUFile *f, void *pv, size_t size)
826 qemu_get_sbe32s(f, v);
830 static void put_int32(QEMUFile *f, void *pv, size_t size)
833 qemu_put_sbe32s(f, v);
836 const VMStateInfo vmstate_info_int32 = {
842 /* 32 bit int. See that the received value is the same than the one
845 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
849 qemu_get_sbe32s(f, &v2);
856 const VMStateInfo vmstate_info_int32_equal = {
857 .name = "int32 equal",
858 .get = get_int32_equal,
862 /* 32 bit int. See that the received value is the less or the same
863 than the one in the field */
865 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
869 qemu_get_sbe32s(f, &new);
876 const VMStateInfo vmstate_info_int32_le = {
877 .name = "int32 equal",
884 static int get_int64(QEMUFile *f, void *pv, size_t size)
887 qemu_get_sbe64s(f, v);
891 static void put_int64(QEMUFile *f, void *pv, size_t size)
894 qemu_put_sbe64s(f, v);
897 const VMStateInfo vmstate_info_int64 = {
903 /* 8 bit unsigned int */
905 static int get_uint8(QEMUFile *f, void *pv, size_t size)
912 static void put_uint8(QEMUFile *f, void *pv, size_t size)
918 const VMStateInfo vmstate_info_uint8 = {
924 /* 16 bit unsigned int */
926 static int get_uint16(QEMUFile *f, void *pv, size_t size)
929 qemu_get_be16s(f, v);
933 static void put_uint16(QEMUFile *f, void *pv, size_t size)
936 qemu_put_be16s(f, v);
939 const VMStateInfo vmstate_info_uint16 = {
945 /* 32 bit unsigned int */
947 static int get_uint32(QEMUFile *f, void *pv, size_t size)
950 qemu_get_be32s(f, v);
954 static void put_uint32(QEMUFile *f, void *pv, size_t size)
957 qemu_put_be32s(f, v);
960 const VMStateInfo vmstate_info_uint32 = {
966 /* 32 bit uint. See that the received value is the same than the one
969 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
973 qemu_get_be32s(f, &v2);
981 const VMStateInfo vmstate_info_uint32_equal = {
982 .name = "uint32 equal",
983 .get = get_uint32_equal,
987 /* 64 bit unsigned int */
989 static int get_uint64(QEMUFile *f, void *pv, size_t size)
992 qemu_get_be64s(f, v);
996 static void put_uint64(QEMUFile *f, void *pv, size_t size)
999 qemu_put_be64s(f, v);
1002 const VMStateInfo vmstate_info_uint64 = {
1008 /* 8 bit int. See that the received value is the same than the one
1011 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1015 qemu_get_8s(f, &v2);
1022 const VMStateInfo vmstate_info_uint8_equal = {
1023 .name = "uint8 equal",
1024 .get = get_uint8_equal,
1028 /* 16 bit unsigned int int. See that the received value is the same than the one
1031 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1035 qemu_get_be16s(f, &v2);
1042 const VMStateInfo vmstate_info_uint16_equal = {
1043 .name = "uint16 equal",
1044 .get = get_uint16_equal,
1050 static int get_timer(QEMUFile *f, void *pv, size_t size)
1053 qemu_get_timer(f, v);
1057 static void put_timer(QEMUFile *f, void *pv, size_t size)
1060 qemu_put_timer(f, v);
1063 const VMStateInfo vmstate_info_timer = {
1069 /* uint8_t buffers */
1071 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1074 qemu_get_buffer(f, v, size);
1078 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1081 qemu_put_buffer(f, v, size);
1084 const VMStateInfo vmstate_info_buffer = {
1090 /* unused buffers: space that was used for some fields that are
1091 not useful anymore */
1093 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1099 block_len = MIN(sizeof(buf), size);
1101 qemu_get_buffer(f, buf, block_len);
1106 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1108 static const uint8_t buf[1024];
1112 block_len = MIN(sizeof(buf), size);
1114 qemu_put_buffer(f, buf, block_len);
1118 const VMStateInfo vmstate_info_unused_buffer = {
1119 .name = "unused_buffer",
1120 .get = get_unused_buffer,
1121 .put = put_unused_buffer,
1124 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1125 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1126 * bit words with the bits in big endian order. The in-memory format
1127 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1129 /* This is the number of 64 bit words sent over the wire */
1130 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1131 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1133 unsigned long *bmp = pv;
1135 for (i = 0; i < BITS_TO_U64S(size); i++) {
1136 uint64_t w = qemu_get_be64(f);
1138 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1139 bmp[idx++] = w >> 32;
1145 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1147 unsigned long *bmp = pv;
1149 for (i = 0; i < BITS_TO_U64S(size); i++) {
1150 uint64_t w = bmp[idx++];
1151 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1152 w |= ((uint64_t)bmp[idx++]) << 32;
1154 qemu_put_be64(f, w);
1158 const VMStateInfo vmstate_info_bitmap = {
1164 typedef struct CompatEntry {
1169 typedef struct SaveStateEntry {
1170 QTAILQ_ENTRY(SaveStateEntry) entry;
1176 SaveVMHandlers *ops;
1177 const VMStateDescription *vmsd;
1179 CompatEntry *compat;
1185 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1186 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1187 static int global_section_id;
1189 static int calculate_new_instance_id(const char *idstr)
1192 int instance_id = 0;
1194 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1195 if (strcmp(idstr, se->idstr) == 0
1196 && instance_id <= se->instance_id) {
1197 instance_id = se->instance_id + 1;
1203 static int calculate_compat_instance_id(const char *idstr)
1206 int instance_id = 0;
1208 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1212 if (strcmp(idstr, se->compat->idstr) == 0
1213 && instance_id <= se->compat->instance_id) {
1214 instance_id = se->compat->instance_id + 1;
1220 /* TODO: Individual devices generally have very little idea about the rest
1221 of the system, so instance_id should be removed/replaced.
1222 Meanwhile pass -1 as instance_id if you do not already have a clearly
1223 distinguishing id for all instances of your device class. */
1224 int register_savevm_live(DeviceState *dev,
1228 SaveVMHandlers *ops,
1233 se = g_malloc0(sizeof(SaveStateEntry));
1234 se->version_id = version_id;
1235 se->section_id = global_section_id++;
1237 se->opaque = opaque;
1240 /* if this is a live_savem then set is_ram */
1241 if (ops->save_live_setup != NULL) {
1246 char *id = qdev_get_dev_path(dev);
1248 pstrcpy(se->idstr, sizeof(se->idstr), id);
1249 pstrcat(se->idstr, sizeof(se->idstr), "/");
1252 se->compat = g_malloc0(sizeof(CompatEntry));
1253 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1254 se->compat->instance_id = instance_id == -1 ?
1255 calculate_compat_instance_id(idstr) : instance_id;
1259 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1261 if (instance_id == -1) {
1262 se->instance_id = calculate_new_instance_id(se->idstr);
1264 se->instance_id = instance_id;
1266 assert(!se->compat || se->instance_id == 0);
1267 /* add at the end of list */
1268 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1272 int register_savevm(DeviceState *dev,
1276 SaveStateHandler *save_state,
1277 LoadStateHandler *load_state,
1280 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1281 ops->save_state = save_state;
1282 ops->load_state = load_state;
1283 return register_savevm_live(dev, idstr, instance_id, version_id,
1287 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1289 SaveStateEntry *se, *new_se;
1293 char *path = qdev_get_dev_path(dev);
1295 pstrcpy(id, sizeof(id), path);
1296 pstrcat(id, sizeof(id), "/");
1300 pstrcat(id, sizeof(id), idstr);
1302 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1303 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1304 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1314 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1315 const VMStateDescription *vmsd,
1316 void *opaque, int alias_id,
1317 int required_for_version)
1321 /* If this triggers, alias support can be dropped for the vmsd. */
1322 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1324 se = g_malloc0(sizeof(SaveStateEntry));
1325 se->version_id = vmsd->version_id;
1326 se->section_id = global_section_id++;
1327 se->opaque = opaque;
1329 se->alias_id = alias_id;
1330 se->no_migrate = vmsd->unmigratable;
1333 char *id = qdev_get_dev_path(dev);
1335 pstrcpy(se->idstr, sizeof(se->idstr), id);
1336 pstrcat(se->idstr, sizeof(se->idstr), "/");
1339 se->compat = g_malloc0(sizeof(CompatEntry));
1340 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1341 se->compat->instance_id = instance_id == -1 ?
1342 calculate_compat_instance_id(vmsd->name) : instance_id;
1346 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1348 if (instance_id == -1) {
1349 se->instance_id = calculate_new_instance_id(se->idstr);
1351 se->instance_id = instance_id;
1353 assert(!se->compat || se->instance_id == 0);
1354 /* add at the end of list */
1355 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1359 int vmstate_register(DeviceState *dev, int instance_id,
1360 const VMStateDescription *vmsd, void *opaque)
1362 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1366 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1369 SaveStateEntry *se, *new_se;
1371 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1372 if (se->vmsd == vmsd && se->opaque == opaque) {
1373 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1382 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1384 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1387 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1388 void *opaque, int version_id)
1390 VMStateField *field = vmsd->fields;
1393 if (version_id > vmsd->version_id) {
1396 if (version_id < vmsd->minimum_version_id_old) {
1399 if (version_id < vmsd->minimum_version_id) {
1400 return vmsd->load_state_old(f, opaque, version_id);
1402 if (vmsd->pre_load) {
1403 int ret = vmsd->pre_load(opaque);
1407 while(field->name) {
1408 if ((field->field_exists &&
1409 field->field_exists(opaque, version_id)) ||
1410 (!field->field_exists &&
1411 field->version_id <= version_id)) {
1412 void *base_addr = opaque + field->offset;
1414 int size = field->size;
1416 if (field->flags & VMS_VBUFFER) {
1417 size = *(int32_t *)(opaque+field->size_offset);
1418 if (field->flags & VMS_MULTIPLY) {
1419 size *= field->size;
1422 if (field->flags & VMS_ARRAY) {
1423 n_elems = field->num;
1424 } else if (field->flags & VMS_VARRAY_INT32) {
1425 n_elems = *(int32_t *)(opaque+field->num_offset);
1426 } else if (field->flags & VMS_VARRAY_UINT32) {
1427 n_elems = *(uint32_t *)(opaque+field->num_offset);
1428 } else if (field->flags & VMS_VARRAY_UINT16) {
1429 n_elems = *(uint16_t *)(opaque+field->num_offset);
1430 } else if (field->flags & VMS_VARRAY_UINT8) {
1431 n_elems = *(uint8_t *)(opaque+field->num_offset);
1433 if (field->flags & VMS_POINTER) {
1434 base_addr = *(void **)base_addr + field->start;
1436 for (i = 0; i < n_elems; i++) {
1437 void *addr = base_addr + size * i;
1439 if (field->flags & VMS_ARRAY_OF_POINTER) {
1440 addr = *(void **)addr;
1442 if (field->flags & VMS_STRUCT) {
1443 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1445 ret = field->info->get(f, addr, size);
1455 ret = vmstate_subsection_load(f, vmsd, opaque);
1459 if (vmsd->post_load) {
1460 return vmsd->post_load(opaque, version_id);
1465 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1468 VMStateField *field = vmsd->fields;
1470 if (vmsd->pre_save) {
1471 vmsd->pre_save(opaque);
1473 while(field->name) {
1474 if (!field->field_exists ||
1475 field->field_exists(opaque, vmsd->version_id)) {
1476 void *base_addr = opaque + field->offset;
1478 int size = field->size;
1480 if (field->flags & VMS_VBUFFER) {
1481 size = *(int32_t *)(opaque+field->size_offset);
1482 if (field->flags & VMS_MULTIPLY) {
1483 size *= field->size;
1486 if (field->flags & VMS_ARRAY) {
1487 n_elems = field->num;
1488 } else if (field->flags & VMS_VARRAY_INT32) {
1489 n_elems = *(int32_t *)(opaque+field->num_offset);
1490 } else if (field->flags & VMS_VARRAY_UINT32) {
1491 n_elems = *(uint32_t *)(opaque+field->num_offset);
1492 } else if (field->flags & VMS_VARRAY_UINT16) {
1493 n_elems = *(uint16_t *)(opaque+field->num_offset);
1494 } else if (field->flags & VMS_VARRAY_UINT8) {
1495 n_elems = *(uint8_t *)(opaque+field->num_offset);
1497 if (field->flags & VMS_POINTER) {
1498 base_addr = *(void **)base_addr + field->start;
1500 for (i = 0; i < n_elems; i++) {
1501 void *addr = base_addr + size * i;
1503 if (field->flags & VMS_ARRAY_OF_POINTER) {
1504 addr = *(void **)addr;
1506 if (field->flags & VMS_STRUCT) {
1507 vmstate_save_state(f, field->vmsd, addr);
1509 field->info->put(f, addr, size);
1515 vmstate_subsection_save(f, vmsd, opaque);
1518 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1520 if (!se->vmsd) { /* Old style */
1521 return se->ops->load_state(f, se->opaque, version_id);
1523 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1526 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1528 if (!se->vmsd) { /* Old style */
1529 se->ops->save_state(f, se->opaque);
1532 vmstate_save_state(f,se->vmsd, se->opaque);
1535 #define QEMU_VM_FILE_MAGIC 0x5145564d
1536 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1537 #define QEMU_VM_FILE_VERSION 0x00000003
1539 #define QEMU_VM_EOF 0x00
1540 #define QEMU_VM_SECTION_START 0x01
1541 #define QEMU_VM_SECTION_PART 0x02
1542 #define QEMU_VM_SECTION_END 0x03
1543 #define QEMU_VM_SECTION_FULL 0x04
1544 #define QEMU_VM_SUBSECTION 0x05
1546 bool qemu_savevm_state_blocked(Error **errp)
1550 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1551 if (se->no_migrate) {
1552 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1559 int qemu_savevm_state_begin(QEMUFile *f,
1560 const MigrationParams *params)
1565 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1566 if (!se->ops || !se->ops->set_params) {
1569 se->ops->set_params(params, se->opaque);
1572 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1573 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1575 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1578 if (!se->ops || !se->ops->save_live_setup) {
1581 if (se->ops && se->ops->is_active) {
1582 if (!se->ops->is_active(se->opaque)) {
1587 qemu_put_byte(f, QEMU_VM_SECTION_START);
1588 qemu_put_be32(f, se->section_id);
1591 len = strlen(se->idstr);
1592 qemu_put_byte(f, len);
1593 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1595 qemu_put_be32(f, se->instance_id);
1596 qemu_put_be32(f, se->version_id);
1598 ret = se->ops->save_live_setup(f, se->opaque);
1600 qemu_savevm_state_cancel(f);
1604 ret = qemu_file_get_error(f);
1606 qemu_savevm_state_cancel(f);
1614 * this function has three return values:
1615 * negative: there was one error, and we have -errno.
1616 * 0 : We haven't finished, caller have to go again
1617 * 1 : We have finished, we can go to complete phase
1619 int qemu_savevm_state_iterate(QEMUFile *f)
1624 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1625 if (!se->ops || !se->ops->save_live_iterate) {
1628 if (se->ops && se->ops->is_active) {
1629 if (!se->ops->is_active(se->opaque)) {
1633 if (qemu_file_rate_limit(f)) {
1636 trace_savevm_section_start();
1638 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1639 qemu_put_be32(f, se->section_id);
1641 ret = se->ops->save_live_iterate(f, se->opaque);
1642 trace_savevm_section_end(se->section_id);
1645 /* Do not proceed to the next vmstate before this one reported
1646 completion of the current stage. This serializes the migration
1647 and reduces the probability that a faster changing state is
1648 synchronized over and over again. */
1655 ret = qemu_file_get_error(f);
1657 qemu_savevm_state_cancel(f);
1662 int qemu_savevm_state_complete(QEMUFile *f)
1667 cpu_synchronize_all_states();
1669 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1670 if (!se->ops || !se->ops->save_live_complete) {
1673 if (se->ops && se->ops->is_active) {
1674 if (!se->ops->is_active(se->opaque)) {
1678 trace_savevm_section_start();
1680 qemu_put_byte(f, QEMU_VM_SECTION_END);
1681 qemu_put_be32(f, se->section_id);
1683 ret = se->ops->save_live_complete(f, se->opaque);
1684 trace_savevm_section_end(se->section_id);
1690 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1693 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1696 trace_savevm_section_start();
1698 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1699 qemu_put_be32(f, se->section_id);
1702 len = strlen(se->idstr);
1703 qemu_put_byte(f, len);
1704 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1706 qemu_put_be32(f, se->instance_id);
1707 qemu_put_be32(f, se->version_id);
1709 vmstate_save(f, se);
1710 trace_savevm_section_end(se->section_id);
1713 qemu_put_byte(f, QEMU_VM_EOF);
1715 return qemu_file_get_error(f);
1718 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1723 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1724 if (!se->ops || !se->ops->save_live_pending) {
1727 if (se->ops && se->ops->is_active) {
1728 if (!se->ops->is_active(se->opaque)) {
1732 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1737 void qemu_savevm_state_cancel(QEMUFile *f)
1741 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1742 if (se->ops && se->ops->cancel) {
1743 se->ops->cancel(se->opaque);
1748 static int qemu_savevm_state(QEMUFile *f)
1751 MigrationParams params = {
1756 if (qemu_savevm_state_blocked(NULL)) {
1761 ret = qemu_savevm_state_begin(f, ¶ms);
1766 ret = qemu_savevm_state_iterate(f);
1771 ret = qemu_savevm_state_complete(f);
1775 ret = qemu_file_get_error(f);
1781 static int qemu_save_device_state(QEMUFile *f)
1785 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1786 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1788 cpu_synchronize_all_states();
1790 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1796 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1801 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1802 qemu_put_be32(f, se->section_id);
1805 len = strlen(se->idstr);
1806 qemu_put_byte(f, len);
1807 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1809 qemu_put_be32(f, se->instance_id);
1810 qemu_put_be32(f, se->version_id);
1812 vmstate_save(f, se);
1815 qemu_put_byte(f, QEMU_VM_EOF);
1817 return qemu_file_get_error(f);
1820 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1824 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1825 if (!strcmp(se->idstr, idstr) &&
1826 (instance_id == se->instance_id ||
1827 instance_id == se->alias_id))
1829 /* Migrating from an older version? */
1830 if (strstr(se->idstr, idstr) && se->compat) {
1831 if (!strcmp(se->compat->idstr, idstr) &&
1832 (instance_id == se->compat->instance_id ||
1833 instance_id == se->alias_id))
1840 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1842 while(sub && sub->needed) {
1843 if (strcmp(idstr, sub->vmsd->name) == 0) {
1851 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1854 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
1857 uint8_t version_id, len, size;
1858 const VMStateDescription *sub_vmsd;
1860 len = qemu_peek_byte(f, 1);
1861 if (len < strlen(vmsd->name) + 1) {
1862 /* subsection name has be be "section_name/a" */
1865 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1871 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1872 /* it don't have a valid subsection name */
1875 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
1876 if (sub_vmsd == NULL) {
1879 qemu_file_skip(f, 1); /* subsection */
1880 qemu_file_skip(f, 1); /* len */
1881 qemu_file_skip(f, len); /* idstr */
1882 version_id = qemu_get_be32(f);
1884 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1892 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1895 const VMStateSubsection *sub = vmsd->subsections;
1897 while (sub && sub->needed) {
1898 if (sub->needed(opaque)) {
1899 const VMStateDescription *vmsd = sub->vmsd;
1902 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1903 len = strlen(vmsd->name);
1904 qemu_put_byte(f, len);
1905 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1906 qemu_put_be32(f, vmsd->version_id);
1907 vmstate_save_state(f, vmsd, opaque);
1913 typedef struct LoadStateEntry {
1914 QLIST_ENTRY(LoadStateEntry) entry;
1920 int qemu_loadvm_state(QEMUFile *f)
1922 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1923 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1924 LoadStateEntry *le, *new_le;
1925 uint8_t section_type;
1929 if (qemu_savevm_state_blocked(NULL)) {
1933 v = qemu_get_be32(f);
1934 if (v != QEMU_VM_FILE_MAGIC)
1937 v = qemu_get_be32(f);
1938 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1939 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1942 if (v != QEMU_VM_FILE_VERSION)
1945 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1946 uint32_t instance_id, version_id, section_id;
1951 switch (section_type) {
1952 case QEMU_VM_SECTION_START:
1953 case QEMU_VM_SECTION_FULL:
1954 /* Read section start */
1955 section_id = qemu_get_be32(f);
1956 len = qemu_get_byte(f);
1957 qemu_get_buffer(f, (uint8_t *)idstr, len);
1959 instance_id = qemu_get_be32(f);
1960 version_id = qemu_get_be32(f);
1962 /* Find savevm section */
1963 se = find_se(idstr, instance_id);
1965 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1970 /* Validate version */
1971 if (version_id > se->version_id) {
1972 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1973 version_id, idstr, se->version_id);
1979 le = g_malloc0(sizeof(*le));
1982 le->section_id = section_id;
1983 le->version_id = version_id;
1984 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1986 ret = vmstate_load(f, le->se, le->version_id);
1988 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1989 instance_id, idstr);
1993 case QEMU_VM_SECTION_PART:
1994 case QEMU_VM_SECTION_END:
1995 section_id = qemu_get_be32(f);
1997 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1998 if (le->section_id == section_id) {
2003 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2008 ret = vmstate_load(f, le->se, le->version_id);
2010 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2016 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2022 cpu_synchronize_all_post_init();
2027 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2028 QLIST_REMOVE(le, entry);
2033 ret = qemu_file_get_error(f);
2039 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2042 QEMUSnapshotInfo *sn_tab, *sn;
2046 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2049 for(i = 0; i < nb_sns; i++) {
2051 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2062 * Deletes snapshots of a given name in all opened images.
2064 static int del_existing_snapshots(Monitor *mon, const char *name)
2066 BlockDriverState *bs;
2067 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2071 while ((bs = bdrv_next(bs))) {
2072 if (bdrv_can_snapshot(bs) &&
2073 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2075 ret = bdrv_snapshot_delete(bs, name);
2078 "Error while deleting snapshot on '%s'\n",
2079 bdrv_get_device_name(bs));
2088 void do_savevm(Monitor *mon, const QDict *qdict)
2090 BlockDriverState *bs, *bs1;
2091 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2094 int saved_vm_running;
2095 uint64_t vm_state_size;
2103 const char *name = qdict_get_try_str(qdict, "name");
2105 /* Verify if there is a device that doesn't support snapshots and is writable */
2107 while ((bs = bdrv_next(bs))) {
2109 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2113 if (!bdrv_can_snapshot(bs)) {
2114 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2115 bdrv_get_device_name(bs));
2120 bs = bdrv_snapshots();
2122 monitor_printf(mon, "No block device can accept snapshots\n");
2126 saved_vm_running = runstate_is_running();
2127 vm_stop(RUN_STATE_SAVE_VM);
2129 memset(sn, 0, sizeof(*sn));
2131 /* fill auxiliary fields */
2134 sn->date_sec = tb.time;
2135 sn->date_nsec = tb.millitm * 1000000;
2137 gettimeofday(&tv, NULL);
2138 sn->date_sec = tv.tv_sec;
2139 sn->date_nsec = tv.tv_usec * 1000;
2141 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2144 ret = bdrv_snapshot_find(bs, old_sn, name);
2146 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2147 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2149 pstrcpy(sn->name, sizeof(sn->name), name);
2154 ptm = localtime(&t);
2155 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2157 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2158 localtime_r((const time_t *)&tv.tv_sec, &tm);
2159 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2163 /* Delete old snapshots of the same name */
2164 if (name && del_existing_snapshots(mon, name) < 0) {
2168 /* save the VM state */
2169 f = qemu_fopen_bdrv(bs, 1);
2171 monitor_printf(mon, "Could not open VM state file\n");
2174 ret = qemu_savevm_state(f);
2175 vm_state_size = qemu_ftell(f);
2178 monitor_printf(mon, "Error %d while writing VM\n", ret);
2182 /* create the snapshots */
2185 while ((bs1 = bdrv_next(bs1))) {
2186 if (bdrv_can_snapshot(bs1)) {
2187 /* Write VM state size only to the image that contains the state */
2188 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2189 ret = bdrv_snapshot_create(bs1, sn);
2191 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2192 bdrv_get_device_name(bs1));
2198 if (saved_vm_running)
2202 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2205 int saved_vm_running;
2208 saved_vm_running = runstate_is_running();
2209 vm_stop(RUN_STATE_SAVE_VM);
2211 f = qemu_fopen(filename, "wb");
2213 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2216 ret = qemu_save_device_state(f);
2219 error_set(errp, QERR_IO_ERROR);
2223 if (saved_vm_running)
2227 int load_vmstate(const char *name)
2229 BlockDriverState *bs, *bs_vm_state;
2230 QEMUSnapshotInfo sn;
2234 bs_vm_state = bdrv_snapshots();
2236 error_report("No block device supports snapshots");
2240 /* Don't even try to load empty VM states */
2241 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2244 } else if (sn.vm_state_size == 0) {
2245 error_report("This is a disk-only snapshot. Revert to it offline "
2250 /* Verify if there is any device that doesn't support snapshots and is
2251 writable and check if the requested snapshot is available too. */
2253 while ((bs = bdrv_next(bs))) {
2255 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2259 if (!bdrv_can_snapshot(bs)) {
2260 error_report("Device '%s' is writable but does not support snapshots.",
2261 bdrv_get_device_name(bs));
2265 ret = bdrv_snapshot_find(bs, &sn, name);
2267 error_report("Device '%s' does not have the requested snapshot '%s'",
2268 bdrv_get_device_name(bs), name);
2273 /* Flush all IO requests so they don't interfere with the new state. */
2277 while ((bs = bdrv_next(bs))) {
2278 if (bdrv_can_snapshot(bs)) {
2279 ret = bdrv_snapshot_goto(bs, name);
2281 error_report("Error %d while activating snapshot '%s' on '%s'",
2282 ret, name, bdrv_get_device_name(bs));
2288 /* restore the VM state */
2289 f = qemu_fopen_bdrv(bs_vm_state, 0);
2291 error_report("Could not open VM state file");
2295 qemu_system_reset(VMRESET_SILENT);
2296 ret = qemu_loadvm_state(f);
2300 error_report("Error %d while loading VM state", ret);
2307 void do_delvm(Monitor *mon, const QDict *qdict)
2309 BlockDriverState *bs, *bs1;
2311 const char *name = qdict_get_str(qdict, "name");
2313 bs = bdrv_snapshots();
2315 monitor_printf(mon, "No block device supports snapshots\n");
2320 while ((bs1 = bdrv_next(bs1))) {
2321 if (bdrv_can_snapshot(bs1)) {
2322 ret = bdrv_snapshot_delete(bs1, name);
2324 if (ret == -ENOTSUP)
2326 "Snapshots not supported on device '%s'\n",
2327 bdrv_get_device_name(bs1));
2329 monitor_printf(mon, "Error %d while deleting snapshot on "
2330 "'%s'\n", ret, bdrv_get_device_name(bs1));
2336 void do_info_snapshots(Monitor *mon)
2338 BlockDriverState *bs, *bs1;
2339 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2340 int nb_sns, i, ret, available;
2342 int *available_snapshots;
2345 bs = bdrv_snapshots();
2347 monitor_printf(mon, "No available block device supports snapshots\n");
2351 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2353 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2358 monitor_printf(mon, "There is no snapshot available.\n");
2362 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2364 for (i = 0; i < nb_sns; i++) {
2369 while ((bs1 = bdrv_next(bs1))) {
2370 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2371 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2380 available_snapshots[total] = i;
2386 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2387 for (i = 0; i < total; i++) {
2388 sn = &sn_tab[available_snapshots[i]];
2389 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2392 monitor_printf(mon, "There is no suitable snapshot available\n");
2396 g_free(available_snapshots);
2400 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2402 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2403 memory_region_name(mr), dev);
2406 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2408 /* Nothing do to while the implementation is in RAMBlock */
2411 void vmstate_register_ram_global(MemoryRegion *mr)
2413 vmstate_register_ram(mr, NULL);
2422 nzrun = length byte...
2424 length = uleb128 encoded integer
2426 int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2427 uint8_t *dst, int dlen)
2429 uint32_t zrun_len = 0, nzrun_len = 0;
2432 uint8_t *nzrun_start = NULL;
2434 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2443 /* not aligned to sizeof(long) */
2444 res = (slen - i) % sizeof(long);
2445 while (res && old_buf[i] == new_buf[i]) {
2451 /* word at a time for speed */
2454 (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2456 zrun_len += sizeof(long);
2459 /* go over the rest */
2460 while (i < slen && old_buf[i] == new_buf[i]) {
2466 /* buffer unchanged */
2467 if (zrun_len == slen) {
2471 /* skip last zero run */
2476 d += uleb128_encode_small(dst + d, zrun_len);
2479 nzrun_start = new_buf + i;
2485 /* not aligned to sizeof(long) */
2486 res = (slen - i) % sizeof(long);
2487 while (res && old_buf[i] != new_buf[i]) {
2493 /* word at a time for speed, use of 32-bit long okay */
2495 /* truncation to 32-bit long okay */
2496 long mask = (long)0x0101010101010101ULL;
2498 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2499 if ((xor - mask) & ~xor & (mask << 7)) {
2500 /* found the end of an nzrun within the current long */
2501 while (old_buf[i] != new_buf[i]) {
2508 nzrun_len += sizeof(long);
2513 d += uleb128_encode_small(dst + d, nzrun_len);
2515 if (d + nzrun_len > dlen) {
2518 memcpy(dst + d, nzrun_start, nzrun_len);
2526 int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2535 if ((slen - i) < 2) {
2539 ret = uleb128_decode_small(src + i, &count);
2540 if (ret < 0 || (i && !count)) {
2552 if ((slen - i) < 2) {
2556 ret = uleb128_decode_small(src + i, &count);
2557 if (ret < 0 || !count) {
2563 if (d + count > dlen || i + count > slen) {
2567 memcpy(dst + d, src + i, count);