gtk: use better icon
[qemu.git] / savevm.c
blob2ce439f7f4533b7ae916d1d84edeb93a0f5592df
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
25 #include "config-host.h"
26 #include "qemu-common.h"
27 #include "hw/hw.h"
28 #include "hw/qdev.h"
29 #include "net/net.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
40 #include "trace.h"
41 #include "qemu/bitops.h"
42 #include "qemu/iov.h"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
46 #define SELF_ANNOUNCE_ROUNDS 5
48 #ifndef ETH_P_RARP
49 #define ETH_P_RARP 0x8035
50 #endif
51 #define ARP_HTYPE_ETH 0x0001
52 #define ARP_PTYPE_IP 0x0800
53 #define ARP_OP_REQUEST_REV 0x3
55 static int announce_self_create(uint8_t *buf,
56 uint8_t *mac_addr)
58 /* Ethernet header. */
59 memset(buf, 0xff, 6); /* destination MAC addr */
60 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
61 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
63 /* RARP header. */
64 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
65 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
66 *(buf + 18) = 6; /* hardware addr length (ethernet) */
67 *(buf + 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
69 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
70 memset(buf + 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
72 memset(buf + 38, 0x00, 4); /* target protocol addr */
74 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf + 42, 0x00, 18);
77 return 60; /* len (FCS will be added by hardware) */
80 static void qemu_announce_self_iter(NICState *nic, void *opaque)
82 uint8_t buf[60];
83 int len;
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);
98 if (--count) {
99 /* delay 50ms, 150ms, 250ms, ... */
100 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
101 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
102 } else {
103 qemu_del_timer(timer);
104 qemu_free_timer(timer);
108 void qemu_announce_self(void)
110 static QEMUTimer *timer;
111 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
112 qemu_announce_self_once(&timer);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 #define IO_BUF_SIZE 32768
119 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
121 struct QEMUFile {
122 const QEMUFileOps *ops;
123 void *opaque;
125 int64_t bytes_xfer;
126 int64_t xfer_limit;
128 int64_t pos; /* start of buffer when writing, end of buffer
129 when reading */
130 int buf_index;
131 int buf_size; /* 0 when writing */
132 uint8_t buf[IO_BUF_SIZE];
134 struct iovec iov[MAX_IOV_SIZE];
135 unsigned int iovcnt;
137 int last_error;
140 typedef struct QEMUFileStdio
142 FILE *stdio_file;
143 QEMUFile *file;
144 } QEMUFileStdio;
146 typedef struct QEMUFileSocket
148 int fd;
149 QEMUFile *file;
150 } QEMUFileSocket;
152 typedef struct {
153 Coroutine *co;
154 int fd;
155 } FDYieldUntilData;
157 static void fd_coroutine_enter(void *opaque)
159 FDYieldUntilData *data = opaque;
160 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
161 qemu_coroutine_enter(data->co, NULL);
165 * Yield until a file descriptor becomes readable
167 * Note that this function clobbers the handlers for the file descriptor.
169 static void coroutine_fn yield_until_fd_readable(int fd)
171 FDYieldUntilData data;
173 assert(qemu_in_coroutine());
174 data.co = qemu_coroutine_self();
175 data.fd = fd;
176 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
177 qemu_coroutine_yield();
180 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
181 int64_t pos)
183 QEMUFileSocket *s = opaque;
184 ssize_t len;
185 ssize_t size = iov_size(iov, iovcnt);
187 len = iov_send(s->fd, iov, iovcnt, 0, size);
188 if (len < size) {
189 len = -socket_error();
191 return len;
194 static int socket_get_fd(void *opaque)
196 QEMUFileSocket *s = opaque;
198 return s->fd;
201 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
203 QEMUFileSocket *s = opaque;
204 ssize_t len;
206 for (;;) {
207 len = qemu_recv(s->fd, buf, size, 0);
208 if (len != -1) {
209 break;
211 if (socket_error() == EAGAIN) {
212 yield_until_fd_readable(s->fd);
213 } else if (socket_error() != EINTR) {
214 break;
218 if (len == -1) {
219 len = -socket_error();
221 return len;
224 static int socket_close(void *opaque)
226 QEMUFileSocket *s = opaque;
227 closesocket(s->fd);
228 g_free(s);
229 return 0;
232 static int stdio_get_fd(void *opaque)
234 QEMUFileStdio *s = opaque;
236 return fileno(s->stdio_file);
239 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
241 QEMUFileStdio *s = opaque;
242 return fwrite(buf, 1, size, s->stdio_file);
245 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
247 QEMUFileStdio *s = opaque;
248 FILE *fp = s->stdio_file;
249 int bytes;
251 for (;;) {
252 clearerr(fp);
253 bytes = fread(buf, 1, size, fp);
254 if (bytes != 0 || !ferror(fp)) {
255 break;
257 if (errno == EAGAIN) {
258 yield_until_fd_readable(fileno(fp));
259 } else if (errno != EINTR) {
260 break;
263 return bytes;
266 static int stdio_pclose(void *opaque)
268 QEMUFileStdio *s = opaque;
269 int ret;
270 ret = pclose(s->stdio_file);
271 if (ret == -1) {
272 ret = -errno;
273 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
274 /* close succeeded, but non-zero exit code: */
275 ret = -EIO; /* fake errno value */
277 g_free(s);
278 return ret;
281 static int stdio_fclose(void *opaque)
283 QEMUFileStdio *s = opaque;
284 int ret = 0;
286 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
287 int fd = fileno(s->stdio_file);
288 struct stat st;
290 ret = fstat(fd, &st);
291 if (ret == 0 && S_ISREG(st.st_mode)) {
293 * If the file handle is a regular file make sure the
294 * data is flushed to disk before signaling success.
296 ret = fsync(fd);
297 if (ret != 0) {
298 ret = -errno;
299 return ret;
303 if (fclose(s->stdio_file) == EOF) {
304 ret = -errno;
306 g_free(s);
307 return ret;
310 static const QEMUFileOps stdio_pipe_read_ops = {
311 .get_fd = stdio_get_fd,
312 .get_buffer = stdio_get_buffer,
313 .close = stdio_pclose
316 static const QEMUFileOps stdio_pipe_write_ops = {
317 .get_fd = stdio_get_fd,
318 .put_buffer = stdio_put_buffer,
319 .close = stdio_pclose
322 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
324 FILE *stdio_file;
325 QEMUFileStdio *s;
327 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
328 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
329 return NULL;
332 stdio_file = popen(command, mode);
333 if (stdio_file == NULL) {
334 return NULL;
337 s = g_malloc0(sizeof(QEMUFileStdio));
339 s->stdio_file = stdio_file;
341 if(mode[0] == 'r') {
342 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
343 } else {
344 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
346 return s->file;
349 static const QEMUFileOps stdio_file_read_ops = {
350 .get_fd = stdio_get_fd,
351 .get_buffer = stdio_get_buffer,
352 .close = stdio_fclose
355 static const QEMUFileOps stdio_file_write_ops = {
356 .get_fd = stdio_get_fd,
357 .put_buffer = stdio_put_buffer,
358 .close = stdio_fclose
361 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
362 int64_t pos)
364 QEMUFileSocket *s = opaque;
365 ssize_t len, offset;
366 ssize_t size = iov_size(iov, iovcnt);
367 ssize_t total = 0;
369 assert(iovcnt > 0);
370 offset = 0;
371 while (size > 0) {
372 /* Find the next start position; skip all full-sized vector elements */
373 while (offset >= iov[0].iov_len) {
374 offset -= iov[0].iov_len;
375 iov++, iovcnt--;
378 /* skip `offset' bytes from the (now) first element, undo it on exit */
379 assert(iovcnt > 0);
380 iov[0].iov_base += offset;
381 iov[0].iov_len -= offset;
383 do {
384 len = writev(s->fd, iov, iovcnt);
385 } while (len == -1 && errno == EINTR);
386 if (len == -1) {
387 return -errno;
390 /* Undo the changes above */
391 iov[0].iov_base -= offset;
392 iov[0].iov_len += offset;
394 /* Prepare for the next iteration */
395 offset += len;
396 total += len;
397 size -= len;
400 return total;
403 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
405 QEMUFileSocket *s = opaque;
406 ssize_t len;
408 for (;;) {
409 len = read(s->fd, buf, size);
410 if (len != -1) {
411 break;
413 if (errno == EAGAIN) {
414 yield_until_fd_readable(s->fd);
415 } else if (errno != EINTR) {
416 break;
420 if (len == -1) {
421 len = -errno;
423 return len;
426 static int unix_close(void *opaque)
428 QEMUFileSocket *s = opaque;
429 close(s->fd);
430 g_free(s);
431 return 0;
434 static const QEMUFileOps unix_read_ops = {
435 .get_fd = socket_get_fd,
436 .get_buffer = unix_get_buffer,
437 .close = unix_close
440 static const QEMUFileOps unix_write_ops = {
441 .get_fd = socket_get_fd,
442 .writev_buffer = unix_writev_buffer,
443 .close = unix_close
446 QEMUFile *qemu_fdopen(int fd, const char *mode)
448 QEMUFileSocket *s;
450 if (mode == NULL ||
451 (mode[0] != 'r' && mode[0] != 'w') ||
452 mode[1] != 'b' || mode[2] != 0) {
453 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
454 return NULL;
457 s = g_malloc0(sizeof(QEMUFileSocket));
458 s->fd = fd;
460 if(mode[0] == 'r') {
461 s->file = qemu_fopen_ops(s, &unix_read_ops);
462 } else {
463 s->file = qemu_fopen_ops(s, &unix_write_ops);
465 return s->file;
468 static const QEMUFileOps socket_read_ops = {
469 .get_fd = socket_get_fd,
470 .get_buffer = socket_get_buffer,
471 .close = socket_close
474 static const QEMUFileOps socket_write_ops = {
475 .get_fd = socket_get_fd,
476 .writev_buffer = socket_writev_buffer,
477 .close = socket_close
480 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
482 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
484 if (mode == NULL ||
485 (mode[0] != 'r' && mode[0] != 'w') ||
486 mode[1] != 'b' || mode[2] != 0) {
487 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
488 return NULL;
491 s->fd = fd;
492 if (mode[0] == 'w') {
493 qemu_set_block(s->fd);
494 s->file = qemu_fopen_ops(s, &socket_write_ops);
495 } else {
496 s->file = qemu_fopen_ops(s, &socket_read_ops);
498 return s->file;
501 QEMUFile *qemu_fopen(const char *filename, const char *mode)
503 QEMUFileStdio *s;
505 if (mode == NULL ||
506 (mode[0] != 'r' && mode[0] != 'w') ||
507 mode[1] != 'b' || mode[2] != 0) {
508 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
509 return NULL;
512 s = g_malloc0(sizeof(QEMUFileStdio));
514 s->stdio_file = fopen(filename, mode);
515 if (!s->stdio_file)
516 goto fail;
518 if(mode[0] == 'w') {
519 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
520 } else {
521 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
523 return s->file;
524 fail:
525 g_free(s);
526 return NULL;
529 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
530 int64_t pos)
532 int ret;
533 QEMUIOVector qiov;
535 qemu_iovec_init_external(&qiov, iov, iovcnt);
536 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
537 if (ret < 0) {
538 return ret;
541 return qiov.size;
544 static int block_put_buffer(void *opaque, const uint8_t *buf,
545 int64_t pos, int size)
547 bdrv_save_vmstate(opaque, buf, pos, size);
548 return size;
551 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
553 return bdrv_load_vmstate(opaque, buf, pos, size);
556 static int bdrv_fclose(void *opaque)
558 return bdrv_flush(opaque);
561 static const QEMUFileOps bdrv_read_ops = {
562 .get_buffer = block_get_buffer,
563 .close = bdrv_fclose
566 static const QEMUFileOps bdrv_write_ops = {
567 .put_buffer = block_put_buffer,
568 .writev_buffer = block_writev_buffer,
569 .close = bdrv_fclose
572 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
574 if (is_writable)
575 return qemu_fopen_ops(bs, &bdrv_write_ops);
576 return qemu_fopen_ops(bs, &bdrv_read_ops);
579 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
581 QEMUFile *f;
583 f = g_malloc0(sizeof(QEMUFile));
585 f->opaque = opaque;
586 f->ops = ops;
587 return f;
590 int qemu_file_get_error(QEMUFile *f)
592 return f->last_error;
595 static void qemu_file_set_error(QEMUFile *f, int ret)
597 if (f->last_error == 0) {
598 f->last_error = ret;
602 static inline bool qemu_file_is_writable(QEMUFile *f)
604 return f->ops->writev_buffer || f->ops->put_buffer;
608 * Flushes QEMUFile buffer
610 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
611 * put_buffer ops.
613 static void qemu_fflush(QEMUFile *f)
615 ssize_t ret = 0;
617 if (!qemu_file_is_writable(f)) {
618 return;
621 if (f->ops->writev_buffer) {
622 if (f->iovcnt > 0) {
623 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
625 } else {
626 if (f->buf_index > 0) {
627 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
630 if (ret >= 0) {
631 f->pos += ret;
633 f->buf_index = 0;
634 f->iovcnt = 0;
635 if (ret < 0) {
636 qemu_file_set_error(f, ret);
640 static void qemu_fill_buffer(QEMUFile *f)
642 int len;
643 int pending;
645 assert(!qemu_file_is_writable(f));
647 pending = f->buf_size - f->buf_index;
648 if (pending > 0) {
649 memmove(f->buf, f->buf + f->buf_index, pending);
651 f->buf_index = 0;
652 f->buf_size = pending;
654 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
655 IO_BUF_SIZE - pending);
656 if (len > 0) {
657 f->buf_size += len;
658 f->pos += len;
659 } else if (len == 0) {
660 qemu_file_set_error(f, -EIO);
661 } else if (len != -EAGAIN)
662 qemu_file_set_error(f, len);
665 int qemu_get_fd(QEMUFile *f)
667 if (f->ops->get_fd) {
668 return f->ops->get_fd(f->opaque);
670 return -1;
673 /** Closes the file
675 * Returns negative error value if any error happened on previous operations or
676 * while closing the file. Returns 0 or positive number on success.
678 * The meaning of return value on success depends on the specific backend
679 * being used.
681 int qemu_fclose(QEMUFile *f)
683 int ret;
684 qemu_fflush(f);
685 ret = qemu_file_get_error(f);
687 if (f->ops->close) {
688 int ret2 = f->ops->close(f->opaque);
689 if (ret >= 0) {
690 ret = ret2;
693 /* If any error was spotted before closing, we should report it
694 * instead of the close() return value.
696 if (f->last_error) {
697 ret = f->last_error;
699 g_free(f);
700 return ret;
703 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
705 /* check for adjacent buffer and coalesce them */
706 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
707 f->iov[f->iovcnt - 1].iov_len) {
708 f->iov[f->iovcnt - 1].iov_len += size;
709 } else {
710 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
711 f->iov[f->iovcnt++].iov_len = size;
714 if (f->iovcnt >= MAX_IOV_SIZE) {
715 qemu_fflush(f);
719 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
721 if (!f->ops->writev_buffer) {
722 qemu_put_buffer(f, buf, size);
723 return;
726 if (f->last_error) {
727 return;
730 f->bytes_xfer += size;
731 add_to_iovec(f, buf, size);
734 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
736 int l;
738 if (f->last_error) {
739 return;
742 while (size > 0) {
743 l = IO_BUF_SIZE - f->buf_index;
744 if (l > size)
745 l = size;
746 memcpy(f->buf + f->buf_index, buf, l);
747 f->bytes_xfer += size;
748 if (f->ops->writev_buffer) {
749 add_to_iovec(f, f->buf + f->buf_index, l);
751 f->buf_index += l;
752 if (f->buf_index == IO_BUF_SIZE) {
753 qemu_fflush(f);
755 if (qemu_file_get_error(f)) {
756 break;
758 buf += l;
759 size -= l;
763 void qemu_put_byte(QEMUFile *f, int v)
765 if (f->last_error) {
766 return;
769 f->buf[f->buf_index] = v;
770 f->bytes_xfer++;
771 if (f->ops->writev_buffer) {
772 add_to_iovec(f, f->buf + f->buf_index, 1);
774 f->buf_index++;
775 if (f->buf_index == IO_BUF_SIZE) {
776 qemu_fflush(f);
780 static void qemu_file_skip(QEMUFile *f, int size)
782 if (f->buf_index + size <= f->buf_size) {
783 f->buf_index += size;
787 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
789 int pending;
790 int index;
792 assert(!qemu_file_is_writable(f));
794 index = f->buf_index + offset;
795 pending = f->buf_size - index;
796 if (pending < size) {
797 qemu_fill_buffer(f);
798 index = f->buf_index + offset;
799 pending = f->buf_size - index;
802 if (pending <= 0) {
803 return 0;
805 if (size > pending) {
806 size = pending;
809 memcpy(buf, f->buf + index, size);
810 return size;
813 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
815 int pending = size;
816 int done = 0;
818 while (pending > 0) {
819 int res;
821 res = qemu_peek_buffer(f, buf, pending, 0);
822 if (res == 0) {
823 return done;
825 qemu_file_skip(f, res);
826 buf += res;
827 pending -= res;
828 done += res;
830 return done;
833 static int qemu_peek_byte(QEMUFile *f, int offset)
835 int index = f->buf_index + offset;
837 assert(!qemu_file_is_writable(f));
839 if (index >= f->buf_size) {
840 qemu_fill_buffer(f);
841 index = f->buf_index + offset;
842 if (index >= f->buf_size) {
843 return 0;
846 return f->buf[index];
849 int qemu_get_byte(QEMUFile *f)
851 int result;
853 result = qemu_peek_byte(f, 0);
854 qemu_file_skip(f, 1);
855 return result;
858 int64_t qemu_ftell(QEMUFile *f)
860 qemu_fflush(f);
861 return f->pos;
864 int qemu_file_rate_limit(QEMUFile *f)
866 if (qemu_file_get_error(f)) {
867 return 1;
869 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
870 return 1;
872 return 0;
875 int64_t qemu_file_get_rate_limit(QEMUFile *f)
877 return f->xfer_limit;
880 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
882 f->xfer_limit = limit;
885 void qemu_file_reset_rate_limit(QEMUFile *f)
887 f->bytes_xfer = 0;
890 void qemu_put_be16(QEMUFile *f, unsigned int v)
892 qemu_put_byte(f, v >> 8);
893 qemu_put_byte(f, v);
896 void qemu_put_be32(QEMUFile *f, unsigned int v)
898 qemu_put_byte(f, v >> 24);
899 qemu_put_byte(f, v >> 16);
900 qemu_put_byte(f, v >> 8);
901 qemu_put_byte(f, v);
904 void qemu_put_be64(QEMUFile *f, uint64_t v)
906 qemu_put_be32(f, v >> 32);
907 qemu_put_be32(f, v);
910 unsigned int qemu_get_be16(QEMUFile *f)
912 unsigned int v;
913 v = qemu_get_byte(f) << 8;
914 v |= qemu_get_byte(f);
915 return v;
918 unsigned int qemu_get_be32(QEMUFile *f)
920 unsigned int v;
921 v = qemu_get_byte(f) << 24;
922 v |= qemu_get_byte(f) << 16;
923 v |= qemu_get_byte(f) << 8;
924 v |= qemu_get_byte(f);
925 return v;
928 uint64_t qemu_get_be64(QEMUFile *f)
930 uint64_t v;
931 v = (uint64_t)qemu_get_be32(f) << 32;
932 v |= qemu_get_be32(f);
933 return v;
937 /* timer */
939 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
941 uint64_t expire_time;
943 expire_time = qemu_timer_expire_time_ns(ts);
944 qemu_put_be64(f, expire_time);
947 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
949 uint64_t expire_time;
951 expire_time = qemu_get_be64(f);
952 if (expire_time != -1) {
953 qemu_mod_timer_ns(ts, expire_time);
954 } else {
955 qemu_del_timer(ts);
960 /* bool */
962 static int get_bool(QEMUFile *f, void *pv, size_t size)
964 bool *v = pv;
965 *v = qemu_get_byte(f);
966 return 0;
969 static void put_bool(QEMUFile *f, void *pv, size_t size)
971 bool *v = pv;
972 qemu_put_byte(f, *v);
975 const VMStateInfo vmstate_info_bool = {
976 .name = "bool",
977 .get = get_bool,
978 .put = put_bool,
981 /* 8 bit int */
983 static int get_int8(QEMUFile *f, void *pv, size_t size)
985 int8_t *v = pv;
986 qemu_get_s8s(f, v);
987 return 0;
990 static void put_int8(QEMUFile *f, void *pv, size_t size)
992 int8_t *v = pv;
993 qemu_put_s8s(f, v);
996 const VMStateInfo vmstate_info_int8 = {
997 .name = "int8",
998 .get = get_int8,
999 .put = put_int8,
1002 /* 16 bit int */
1004 static int get_int16(QEMUFile *f, void *pv, size_t size)
1006 int16_t *v = pv;
1007 qemu_get_sbe16s(f, v);
1008 return 0;
1011 static void put_int16(QEMUFile *f, void *pv, size_t size)
1013 int16_t *v = pv;
1014 qemu_put_sbe16s(f, v);
1017 const VMStateInfo vmstate_info_int16 = {
1018 .name = "int16",
1019 .get = get_int16,
1020 .put = put_int16,
1023 /* 32 bit int */
1025 static int get_int32(QEMUFile *f, void *pv, size_t size)
1027 int32_t *v = pv;
1028 qemu_get_sbe32s(f, v);
1029 return 0;
1032 static void put_int32(QEMUFile *f, void *pv, size_t size)
1034 int32_t *v = pv;
1035 qemu_put_sbe32s(f, v);
1038 const VMStateInfo vmstate_info_int32 = {
1039 .name = "int32",
1040 .get = get_int32,
1041 .put = put_int32,
1044 /* 32 bit int. See that the received value is the same than the one
1045 in the field */
1047 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1049 int32_t *v = pv;
1050 int32_t v2;
1051 qemu_get_sbe32s(f, &v2);
1053 if (*v == v2)
1054 return 0;
1055 return -EINVAL;
1058 const VMStateInfo vmstate_info_int32_equal = {
1059 .name = "int32 equal",
1060 .get = get_int32_equal,
1061 .put = put_int32,
1064 /* 32 bit int. See that the received value is the less or the same
1065 than the one in the field */
1067 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1069 int32_t *old = pv;
1070 int32_t new;
1071 qemu_get_sbe32s(f, &new);
1073 if (*old <= new)
1074 return 0;
1075 return -EINVAL;
1078 const VMStateInfo vmstate_info_int32_le = {
1079 .name = "int32 equal",
1080 .get = get_int32_le,
1081 .put = put_int32,
1084 /* 64 bit int */
1086 static int get_int64(QEMUFile *f, void *pv, size_t size)
1088 int64_t *v = pv;
1089 qemu_get_sbe64s(f, v);
1090 return 0;
1093 static void put_int64(QEMUFile *f, void *pv, size_t size)
1095 int64_t *v = pv;
1096 qemu_put_sbe64s(f, v);
1099 const VMStateInfo vmstate_info_int64 = {
1100 .name = "int64",
1101 .get = get_int64,
1102 .put = put_int64,
1105 /* 8 bit unsigned int */
1107 static int get_uint8(QEMUFile *f, void *pv, size_t size)
1109 uint8_t *v = pv;
1110 qemu_get_8s(f, v);
1111 return 0;
1114 static void put_uint8(QEMUFile *f, void *pv, size_t size)
1116 uint8_t *v = pv;
1117 qemu_put_8s(f, v);
1120 const VMStateInfo vmstate_info_uint8 = {
1121 .name = "uint8",
1122 .get = get_uint8,
1123 .put = put_uint8,
1126 /* 16 bit unsigned int */
1128 static int get_uint16(QEMUFile *f, void *pv, size_t size)
1130 uint16_t *v = pv;
1131 qemu_get_be16s(f, v);
1132 return 0;
1135 static void put_uint16(QEMUFile *f, void *pv, size_t size)
1137 uint16_t *v = pv;
1138 qemu_put_be16s(f, v);
1141 const VMStateInfo vmstate_info_uint16 = {
1142 .name = "uint16",
1143 .get = get_uint16,
1144 .put = put_uint16,
1147 /* 32 bit unsigned int */
1149 static int get_uint32(QEMUFile *f, void *pv, size_t size)
1151 uint32_t *v = pv;
1152 qemu_get_be32s(f, v);
1153 return 0;
1156 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1158 uint32_t *v = pv;
1159 qemu_put_be32s(f, v);
1162 const VMStateInfo vmstate_info_uint32 = {
1163 .name = "uint32",
1164 .get = get_uint32,
1165 .put = put_uint32,
1168 /* 32 bit uint. See that the received value is the same than the one
1169 in the field */
1171 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1173 uint32_t *v = pv;
1174 uint32_t v2;
1175 qemu_get_be32s(f, &v2);
1177 if (*v == v2) {
1178 return 0;
1180 return -EINVAL;
1183 const VMStateInfo vmstate_info_uint32_equal = {
1184 .name = "uint32 equal",
1185 .get = get_uint32_equal,
1186 .put = put_uint32,
1189 /* 64 bit unsigned int */
1191 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1193 uint64_t *v = pv;
1194 qemu_get_be64s(f, v);
1195 return 0;
1198 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1200 uint64_t *v = pv;
1201 qemu_put_be64s(f, v);
1204 const VMStateInfo vmstate_info_uint64 = {
1205 .name = "uint64",
1206 .get = get_uint64,
1207 .put = put_uint64,
1210 /* 64 bit unsigned int. See that the received value is the same than the one
1211 in the field */
1213 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1215 uint64_t *v = pv;
1216 uint64_t v2;
1217 qemu_get_be64s(f, &v2);
1219 if (*v == v2) {
1220 return 0;
1222 return -EINVAL;
1225 const VMStateInfo vmstate_info_uint64_equal = {
1226 .name = "int64 equal",
1227 .get = get_uint64_equal,
1228 .put = put_uint64,
1231 /* 8 bit int. See that the received value is the same than the one
1232 in the field */
1234 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1236 uint8_t *v = pv;
1237 uint8_t v2;
1238 qemu_get_8s(f, &v2);
1240 if (*v == v2)
1241 return 0;
1242 return -EINVAL;
1245 const VMStateInfo vmstate_info_uint8_equal = {
1246 .name = "uint8 equal",
1247 .get = get_uint8_equal,
1248 .put = put_uint8,
1251 /* 16 bit unsigned int int. See that the received value is the same than the one
1252 in the field */
1254 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1256 uint16_t *v = pv;
1257 uint16_t v2;
1258 qemu_get_be16s(f, &v2);
1260 if (*v == v2)
1261 return 0;
1262 return -EINVAL;
1265 const VMStateInfo vmstate_info_uint16_equal = {
1266 .name = "uint16 equal",
1267 .get = get_uint16_equal,
1268 .put = put_uint16,
1271 /* floating point */
1273 static int get_float64(QEMUFile *f, void *pv, size_t size)
1275 float64 *v = pv;
1277 *v = make_float64(qemu_get_be64(f));
1278 return 0;
1281 static void put_float64(QEMUFile *f, void *pv, size_t size)
1283 uint64_t *v = pv;
1285 qemu_put_be64(f, float64_val(*v));
1288 const VMStateInfo vmstate_info_float64 = {
1289 .name = "float64",
1290 .get = get_float64,
1291 .put = put_float64,
1294 /* timers */
1296 static int get_timer(QEMUFile *f, void *pv, size_t size)
1298 QEMUTimer *v = pv;
1299 qemu_get_timer(f, v);
1300 return 0;
1303 static void put_timer(QEMUFile *f, void *pv, size_t size)
1305 QEMUTimer *v = pv;
1306 qemu_put_timer(f, v);
1309 const VMStateInfo vmstate_info_timer = {
1310 .name = "timer",
1311 .get = get_timer,
1312 .put = put_timer,
1315 /* uint8_t buffers */
1317 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1319 uint8_t *v = pv;
1320 qemu_get_buffer(f, v, size);
1321 return 0;
1324 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1326 uint8_t *v = pv;
1327 qemu_put_buffer(f, v, size);
1330 const VMStateInfo vmstate_info_buffer = {
1331 .name = "buffer",
1332 .get = get_buffer,
1333 .put = put_buffer,
1336 /* unused buffers: space that was used for some fields that are
1337 not useful anymore */
1339 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1341 uint8_t buf[1024];
1342 int block_len;
1344 while (size > 0) {
1345 block_len = MIN(sizeof(buf), size);
1346 size -= block_len;
1347 qemu_get_buffer(f, buf, block_len);
1349 return 0;
1352 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1354 static const uint8_t buf[1024];
1355 int block_len;
1357 while (size > 0) {
1358 block_len = MIN(sizeof(buf), size);
1359 size -= block_len;
1360 qemu_put_buffer(f, buf, block_len);
1364 const VMStateInfo vmstate_info_unused_buffer = {
1365 .name = "unused_buffer",
1366 .get = get_unused_buffer,
1367 .put = put_unused_buffer,
1370 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1371 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1372 * bit words with the bits in big endian order. The in-memory format
1373 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1375 /* This is the number of 64 bit words sent over the wire */
1376 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1377 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1379 unsigned long *bmp = pv;
1380 int i, idx = 0;
1381 for (i = 0; i < BITS_TO_U64S(size); i++) {
1382 uint64_t w = qemu_get_be64(f);
1383 bmp[idx++] = w;
1384 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1385 bmp[idx++] = w >> 32;
1388 return 0;
1391 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1393 unsigned long *bmp = pv;
1394 int i, idx = 0;
1395 for (i = 0; i < BITS_TO_U64S(size); i++) {
1396 uint64_t w = bmp[idx++];
1397 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1398 w |= ((uint64_t)bmp[idx++]) << 32;
1400 qemu_put_be64(f, w);
1404 const VMStateInfo vmstate_info_bitmap = {
1405 .name = "bitmap",
1406 .get = get_bitmap,
1407 .put = put_bitmap,
1410 typedef struct CompatEntry {
1411 char idstr[256];
1412 int instance_id;
1413 } CompatEntry;
1415 typedef struct SaveStateEntry {
1416 QTAILQ_ENTRY(SaveStateEntry) entry;
1417 char idstr[256];
1418 int instance_id;
1419 int alias_id;
1420 int version_id;
1421 int section_id;
1422 SaveVMHandlers *ops;
1423 const VMStateDescription *vmsd;
1424 void *opaque;
1425 CompatEntry *compat;
1426 int no_migrate;
1427 int is_ram;
1428 } SaveStateEntry;
1431 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1432 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1433 static int global_section_id;
1435 static int calculate_new_instance_id(const char *idstr)
1437 SaveStateEntry *se;
1438 int instance_id = 0;
1440 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1441 if (strcmp(idstr, se->idstr) == 0
1442 && instance_id <= se->instance_id) {
1443 instance_id = se->instance_id + 1;
1446 return instance_id;
1449 static int calculate_compat_instance_id(const char *idstr)
1451 SaveStateEntry *se;
1452 int instance_id = 0;
1454 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1455 if (!se->compat)
1456 continue;
1458 if (strcmp(idstr, se->compat->idstr) == 0
1459 && instance_id <= se->compat->instance_id) {
1460 instance_id = se->compat->instance_id + 1;
1463 return instance_id;
1466 /* TODO: Individual devices generally have very little idea about the rest
1467 of the system, so instance_id should be removed/replaced.
1468 Meanwhile pass -1 as instance_id if you do not already have a clearly
1469 distinguishing id for all instances of your device class. */
1470 int register_savevm_live(DeviceState *dev,
1471 const char *idstr,
1472 int instance_id,
1473 int version_id,
1474 SaveVMHandlers *ops,
1475 void *opaque)
1477 SaveStateEntry *se;
1479 se = g_malloc0(sizeof(SaveStateEntry));
1480 se->version_id = version_id;
1481 se->section_id = global_section_id++;
1482 se->ops = ops;
1483 se->opaque = opaque;
1484 se->vmsd = NULL;
1485 se->no_migrate = 0;
1486 /* if this is a live_savem then set is_ram */
1487 if (ops->save_live_setup != NULL) {
1488 se->is_ram = 1;
1491 if (dev) {
1492 char *id = qdev_get_dev_path(dev);
1493 if (id) {
1494 pstrcpy(se->idstr, sizeof(se->idstr), id);
1495 pstrcat(se->idstr, sizeof(se->idstr), "/");
1496 g_free(id);
1498 se->compat = g_malloc0(sizeof(CompatEntry));
1499 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1500 se->compat->instance_id = instance_id == -1 ?
1501 calculate_compat_instance_id(idstr) : instance_id;
1502 instance_id = -1;
1505 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1507 if (instance_id == -1) {
1508 se->instance_id = calculate_new_instance_id(se->idstr);
1509 } else {
1510 se->instance_id = instance_id;
1512 assert(!se->compat || se->instance_id == 0);
1513 /* add at the end of list */
1514 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1515 return 0;
1518 int register_savevm(DeviceState *dev,
1519 const char *idstr,
1520 int instance_id,
1521 int version_id,
1522 SaveStateHandler *save_state,
1523 LoadStateHandler *load_state,
1524 void *opaque)
1526 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1527 ops->save_state = save_state;
1528 ops->load_state = load_state;
1529 return register_savevm_live(dev, idstr, instance_id, version_id,
1530 ops, opaque);
1533 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1535 SaveStateEntry *se, *new_se;
1536 char id[256] = "";
1538 if (dev) {
1539 char *path = qdev_get_dev_path(dev);
1540 if (path) {
1541 pstrcpy(id, sizeof(id), path);
1542 pstrcat(id, sizeof(id), "/");
1543 g_free(path);
1546 pstrcat(id, sizeof(id), idstr);
1548 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1549 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1550 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1551 if (se->compat) {
1552 g_free(se->compat);
1554 g_free(se->ops);
1555 g_free(se);
1560 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1561 const VMStateDescription *vmsd,
1562 void *opaque, int alias_id,
1563 int required_for_version)
1565 SaveStateEntry *se;
1567 /* If this triggers, alias support can be dropped for the vmsd. */
1568 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1570 se = g_malloc0(sizeof(SaveStateEntry));
1571 se->version_id = vmsd->version_id;
1572 se->section_id = global_section_id++;
1573 se->opaque = opaque;
1574 se->vmsd = vmsd;
1575 se->alias_id = alias_id;
1576 se->no_migrate = vmsd->unmigratable;
1578 if (dev) {
1579 char *id = qdev_get_dev_path(dev);
1580 if (id) {
1581 pstrcpy(se->idstr, sizeof(se->idstr), id);
1582 pstrcat(se->idstr, sizeof(se->idstr), "/");
1583 g_free(id);
1585 se->compat = g_malloc0(sizeof(CompatEntry));
1586 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1587 se->compat->instance_id = instance_id == -1 ?
1588 calculate_compat_instance_id(vmsd->name) : instance_id;
1589 instance_id = -1;
1592 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1594 if (instance_id == -1) {
1595 se->instance_id = calculate_new_instance_id(se->idstr);
1596 } else {
1597 se->instance_id = instance_id;
1599 assert(!se->compat || se->instance_id == 0);
1600 /* add at the end of list */
1601 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1602 return 0;
1605 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1606 void *opaque)
1608 SaveStateEntry *se, *new_se;
1610 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1611 if (se->vmsd == vmsd && se->opaque == opaque) {
1612 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1613 if (se->compat) {
1614 g_free(se->compat);
1616 g_free(se);
1621 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1622 void *opaque);
1623 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1624 void *opaque);
1626 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1627 void *opaque, int version_id)
1629 VMStateField *field = vmsd->fields;
1630 int ret;
1632 if (version_id > vmsd->version_id) {
1633 return -EINVAL;
1635 if (version_id < vmsd->minimum_version_id_old) {
1636 return -EINVAL;
1638 if (version_id < vmsd->minimum_version_id) {
1639 return vmsd->load_state_old(f, opaque, version_id);
1641 if (vmsd->pre_load) {
1642 int ret = vmsd->pre_load(opaque);
1643 if (ret)
1644 return ret;
1646 while(field->name) {
1647 if ((field->field_exists &&
1648 field->field_exists(opaque, version_id)) ||
1649 (!field->field_exists &&
1650 field->version_id <= version_id)) {
1651 void *base_addr = opaque + field->offset;
1652 int i, n_elems = 1;
1653 int size = field->size;
1655 if (field->flags & VMS_VBUFFER) {
1656 size = *(int32_t *)(opaque+field->size_offset);
1657 if (field->flags & VMS_MULTIPLY) {
1658 size *= field->size;
1661 if (field->flags & VMS_ARRAY) {
1662 n_elems = field->num;
1663 } else if (field->flags & VMS_VARRAY_INT32) {
1664 n_elems = *(int32_t *)(opaque+field->num_offset);
1665 } else if (field->flags & VMS_VARRAY_UINT32) {
1666 n_elems = *(uint32_t *)(opaque+field->num_offset);
1667 } else if (field->flags & VMS_VARRAY_UINT16) {
1668 n_elems = *(uint16_t *)(opaque+field->num_offset);
1669 } else if (field->flags & VMS_VARRAY_UINT8) {
1670 n_elems = *(uint8_t *)(opaque+field->num_offset);
1672 if (field->flags & VMS_POINTER) {
1673 base_addr = *(void **)base_addr + field->start;
1675 for (i = 0; i < n_elems; i++) {
1676 void *addr = base_addr + size * i;
1678 if (field->flags & VMS_ARRAY_OF_POINTER) {
1679 addr = *(void **)addr;
1681 if (field->flags & VMS_STRUCT) {
1682 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1683 } else {
1684 ret = field->info->get(f, addr, size);
1687 if (ret < 0) {
1688 return ret;
1692 field++;
1694 ret = vmstate_subsection_load(f, vmsd, opaque);
1695 if (ret != 0) {
1696 return ret;
1698 if (vmsd->post_load) {
1699 return vmsd->post_load(opaque, version_id);
1701 return 0;
1704 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1705 void *opaque)
1707 VMStateField *field = vmsd->fields;
1709 if (vmsd->pre_save) {
1710 vmsd->pre_save(opaque);
1712 while(field->name) {
1713 if (!field->field_exists ||
1714 field->field_exists(opaque, vmsd->version_id)) {
1715 void *base_addr = opaque + field->offset;
1716 int i, n_elems = 1;
1717 int size = field->size;
1719 if (field->flags & VMS_VBUFFER) {
1720 size = *(int32_t *)(opaque+field->size_offset);
1721 if (field->flags & VMS_MULTIPLY) {
1722 size *= field->size;
1725 if (field->flags & VMS_ARRAY) {
1726 n_elems = field->num;
1727 } else if (field->flags & VMS_VARRAY_INT32) {
1728 n_elems = *(int32_t *)(opaque+field->num_offset);
1729 } else if (field->flags & VMS_VARRAY_UINT32) {
1730 n_elems = *(uint32_t *)(opaque+field->num_offset);
1731 } else if (field->flags & VMS_VARRAY_UINT16) {
1732 n_elems = *(uint16_t *)(opaque+field->num_offset);
1733 } else if (field->flags & VMS_VARRAY_UINT8) {
1734 n_elems = *(uint8_t *)(opaque+field->num_offset);
1736 if (field->flags & VMS_POINTER) {
1737 base_addr = *(void **)base_addr + field->start;
1739 for (i = 0; i < n_elems; i++) {
1740 void *addr = base_addr + size * i;
1742 if (field->flags & VMS_ARRAY_OF_POINTER) {
1743 addr = *(void **)addr;
1745 if (field->flags & VMS_STRUCT) {
1746 vmstate_save_state(f, field->vmsd, addr);
1747 } else {
1748 field->info->put(f, addr, size);
1752 field++;
1754 vmstate_subsection_save(f, vmsd, opaque);
1757 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1759 if (!se->vmsd) { /* Old style */
1760 return se->ops->load_state(f, se->opaque, version_id);
1762 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1765 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1767 if (!se->vmsd) { /* Old style */
1768 se->ops->save_state(f, se->opaque);
1769 return;
1771 vmstate_save_state(f,se->vmsd, se->opaque);
1774 #define QEMU_VM_FILE_MAGIC 0x5145564d
1775 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1776 #define QEMU_VM_FILE_VERSION 0x00000003
1778 #define QEMU_VM_EOF 0x00
1779 #define QEMU_VM_SECTION_START 0x01
1780 #define QEMU_VM_SECTION_PART 0x02
1781 #define QEMU_VM_SECTION_END 0x03
1782 #define QEMU_VM_SECTION_FULL 0x04
1783 #define QEMU_VM_SUBSECTION 0x05
1785 bool qemu_savevm_state_blocked(Error **errp)
1787 SaveStateEntry *se;
1789 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1790 if (se->no_migrate) {
1791 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1792 return true;
1795 return false;
1798 void qemu_savevm_state_begin(QEMUFile *f,
1799 const MigrationParams *params)
1801 SaveStateEntry *se;
1802 int ret;
1804 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1805 if (!se->ops || !se->ops->set_params) {
1806 continue;
1808 se->ops->set_params(params, se->opaque);
1811 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1812 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1814 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1815 int len;
1817 if (!se->ops || !se->ops->save_live_setup) {
1818 continue;
1820 if (se->ops && se->ops->is_active) {
1821 if (!se->ops->is_active(se->opaque)) {
1822 continue;
1825 /* Section type */
1826 qemu_put_byte(f, QEMU_VM_SECTION_START);
1827 qemu_put_be32(f, se->section_id);
1829 /* ID string */
1830 len = strlen(se->idstr);
1831 qemu_put_byte(f, len);
1832 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1834 qemu_put_be32(f, se->instance_id);
1835 qemu_put_be32(f, se->version_id);
1837 ret = se->ops->save_live_setup(f, se->opaque);
1838 if (ret < 0) {
1839 qemu_file_set_error(f, ret);
1840 break;
1846 * this function has three return values:
1847 * negative: there was one error, and we have -errno.
1848 * 0 : We haven't finished, caller have to go again
1849 * 1 : We have finished, we can go to complete phase
1851 int qemu_savevm_state_iterate(QEMUFile *f)
1853 SaveStateEntry *se;
1854 int ret = 1;
1856 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1857 if (!se->ops || !se->ops->save_live_iterate) {
1858 continue;
1860 if (se->ops && se->ops->is_active) {
1861 if (!se->ops->is_active(se->opaque)) {
1862 continue;
1865 if (qemu_file_rate_limit(f)) {
1866 return 0;
1868 trace_savevm_section_start();
1869 /* Section type */
1870 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1871 qemu_put_be32(f, se->section_id);
1873 ret = se->ops->save_live_iterate(f, se->opaque);
1874 trace_savevm_section_end(se->section_id);
1876 if (ret < 0) {
1877 qemu_file_set_error(f, ret);
1879 if (ret <= 0) {
1880 /* Do not proceed to the next vmstate before this one reported
1881 completion of the current stage. This serializes the migration
1882 and reduces the probability that a faster changing state is
1883 synchronized over and over again. */
1884 break;
1887 return ret;
1890 void qemu_savevm_state_complete(QEMUFile *f)
1892 SaveStateEntry *se;
1893 int ret;
1895 cpu_synchronize_all_states();
1897 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1898 if (!se->ops || !se->ops->save_live_complete) {
1899 continue;
1901 if (se->ops && se->ops->is_active) {
1902 if (!se->ops->is_active(se->opaque)) {
1903 continue;
1906 trace_savevm_section_start();
1907 /* Section type */
1908 qemu_put_byte(f, QEMU_VM_SECTION_END);
1909 qemu_put_be32(f, se->section_id);
1911 ret = se->ops->save_live_complete(f, se->opaque);
1912 trace_savevm_section_end(se->section_id);
1913 if (ret < 0) {
1914 qemu_file_set_error(f, ret);
1915 return;
1919 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1920 int len;
1922 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1923 continue;
1925 trace_savevm_section_start();
1926 /* Section type */
1927 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1928 qemu_put_be32(f, se->section_id);
1930 /* ID string */
1931 len = strlen(se->idstr);
1932 qemu_put_byte(f, len);
1933 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1935 qemu_put_be32(f, se->instance_id);
1936 qemu_put_be32(f, se->version_id);
1938 vmstate_save(f, se);
1939 trace_savevm_section_end(se->section_id);
1942 qemu_put_byte(f, QEMU_VM_EOF);
1943 qemu_fflush(f);
1946 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1948 SaveStateEntry *se;
1949 uint64_t ret = 0;
1951 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1952 if (!se->ops || !se->ops->save_live_pending) {
1953 continue;
1955 if (se->ops && se->ops->is_active) {
1956 if (!se->ops->is_active(se->opaque)) {
1957 continue;
1960 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1962 return ret;
1965 void qemu_savevm_state_cancel(void)
1967 SaveStateEntry *se;
1969 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1970 if (se->ops && se->ops->cancel) {
1971 se->ops->cancel(se->opaque);
1976 static int qemu_savevm_state(QEMUFile *f)
1978 int ret;
1979 MigrationParams params = {
1980 .blk = 0,
1981 .shared = 0
1984 if (qemu_savevm_state_blocked(NULL)) {
1985 return -EINVAL;
1988 qemu_mutex_unlock_iothread();
1989 qemu_savevm_state_begin(f, &params);
1990 qemu_mutex_lock_iothread();
1992 while (qemu_file_get_error(f) == 0) {
1993 if (qemu_savevm_state_iterate(f) > 0) {
1994 break;
1998 ret = qemu_file_get_error(f);
1999 if (ret == 0) {
2000 qemu_savevm_state_complete(f);
2001 ret = qemu_file_get_error(f);
2003 if (ret != 0) {
2004 qemu_savevm_state_cancel();
2006 return ret;
2009 static int qemu_save_device_state(QEMUFile *f)
2011 SaveStateEntry *se;
2013 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2014 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2016 cpu_synchronize_all_states();
2018 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2019 int len;
2021 if (se->is_ram) {
2022 continue;
2024 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
2025 continue;
2028 /* Section type */
2029 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2030 qemu_put_be32(f, se->section_id);
2032 /* ID string */
2033 len = strlen(se->idstr);
2034 qemu_put_byte(f, len);
2035 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2037 qemu_put_be32(f, se->instance_id);
2038 qemu_put_be32(f, se->version_id);
2040 vmstate_save(f, se);
2043 qemu_put_byte(f, QEMU_VM_EOF);
2045 return qemu_file_get_error(f);
2048 static SaveStateEntry *find_se(const char *idstr, int instance_id)
2050 SaveStateEntry *se;
2052 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2053 if (!strcmp(se->idstr, idstr) &&
2054 (instance_id == se->instance_id ||
2055 instance_id == se->alias_id))
2056 return se;
2057 /* Migrating from an older version? */
2058 if (strstr(se->idstr, idstr) && se->compat) {
2059 if (!strcmp(se->compat->idstr, idstr) &&
2060 (instance_id == se->compat->instance_id ||
2061 instance_id == se->alias_id))
2062 return se;
2065 return NULL;
2068 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2070 while(sub && sub->needed) {
2071 if (strcmp(idstr, sub->vmsd->name) == 0) {
2072 return sub->vmsd;
2074 sub++;
2076 return NULL;
2079 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2080 void *opaque)
2082 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
2083 char idstr[256];
2084 int ret;
2085 uint8_t version_id, len, size;
2086 const VMStateDescription *sub_vmsd;
2088 len = qemu_peek_byte(f, 1);
2089 if (len < strlen(vmsd->name) + 1) {
2090 /* subsection name has be be "section_name/a" */
2091 return 0;
2093 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2094 if (size != len) {
2095 return 0;
2097 idstr[size] = 0;
2099 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2100 /* it don't have a valid subsection name */
2101 return 0;
2103 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
2104 if (sub_vmsd == NULL) {
2105 return -ENOENT;
2107 qemu_file_skip(f, 1); /* subsection */
2108 qemu_file_skip(f, 1); /* len */
2109 qemu_file_skip(f, len); /* idstr */
2110 version_id = qemu_get_be32(f);
2112 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2113 if (ret) {
2114 return ret;
2117 return 0;
2120 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2121 void *opaque)
2123 const VMStateSubsection *sub = vmsd->subsections;
2125 while (sub && sub->needed) {
2126 if (sub->needed(opaque)) {
2127 const VMStateDescription *vmsd = sub->vmsd;
2128 uint8_t len;
2130 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2131 len = strlen(vmsd->name);
2132 qemu_put_byte(f, len);
2133 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2134 qemu_put_be32(f, vmsd->version_id);
2135 vmstate_save_state(f, vmsd, opaque);
2137 sub++;
2141 typedef struct LoadStateEntry {
2142 QLIST_ENTRY(LoadStateEntry) entry;
2143 SaveStateEntry *se;
2144 int section_id;
2145 int version_id;
2146 } LoadStateEntry;
2148 int qemu_loadvm_state(QEMUFile *f)
2150 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2151 QLIST_HEAD_INITIALIZER(loadvm_handlers);
2152 LoadStateEntry *le, *new_le;
2153 uint8_t section_type;
2154 unsigned int v;
2155 int ret;
2157 if (qemu_savevm_state_blocked(NULL)) {
2158 return -EINVAL;
2161 v = qemu_get_be32(f);
2162 if (v != QEMU_VM_FILE_MAGIC)
2163 return -EINVAL;
2165 v = qemu_get_be32(f);
2166 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2167 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2168 return -ENOTSUP;
2170 if (v != QEMU_VM_FILE_VERSION)
2171 return -ENOTSUP;
2173 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2174 uint32_t instance_id, version_id, section_id;
2175 SaveStateEntry *se;
2176 char idstr[257];
2177 int len;
2179 switch (section_type) {
2180 case QEMU_VM_SECTION_START:
2181 case QEMU_VM_SECTION_FULL:
2182 /* Read section start */
2183 section_id = qemu_get_be32(f);
2184 len = qemu_get_byte(f);
2185 qemu_get_buffer(f, (uint8_t *)idstr, len);
2186 idstr[len] = 0;
2187 instance_id = qemu_get_be32(f);
2188 version_id = qemu_get_be32(f);
2190 /* Find savevm section */
2191 se = find_se(idstr, instance_id);
2192 if (se == NULL) {
2193 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2194 ret = -EINVAL;
2195 goto out;
2198 /* Validate version */
2199 if (version_id > se->version_id) {
2200 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2201 version_id, idstr, se->version_id);
2202 ret = -EINVAL;
2203 goto out;
2206 /* Add entry */
2207 le = g_malloc0(sizeof(*le));
2209 le->se = se;
2210 le->section_id = section_id;
2211 le->version_id = version_id;
2212 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2214 ret = vmstate_load(f, le->se, le->version_id);
2215 if (ret < 0) {
2216 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2217 instance_id, idstr);
2218 goto out;
2220 break;
2221 case QEMU_VM_SECTION_PART:
2222 case QEMU_VM_SECTION_END:
2223 section_id = qemu_get_be32(f);
2225 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2226 if (le->section_id == section_id) {
2227 break;
2230 if (le == NULL) {
2231 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2232 ret = -EINVAL;
2233 goto out;
2236 ret = vmstate_load(f, le->se, le->version_id);
2237 if (ret < 0) {
2238 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2239 section_id);
2240 goto out;
2242 break;
2243 default:
2244 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2245 ret = -EINVAL;
2246 goto out;
2250 cpu_synchronize_all_post_init();
2252 ret = 0;
2254 out:
2255 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2256 QLIST_REMOVE(le, entry);
2257 g_free(le);
2260 if (ret == 0) {
2261 ret = qemu_file_get_error(f);
2264 return ret;
2267 static BlockDriverState *find_vmstate_bs(void)
2269 BlockDriverState *bs = NULL;
2270 while ((bs = bdrv_next(bs))) {
2271 if (bdrv_can_snapshot(bs)) {
2272 return bs;
2275 return NULL;
2279 * Deletes snapshots of a given name in all opened images.
2281 static int del_existing_snapshots(Monitor *mon, const char *name)
2283 BlockDriverState *bs;
2284 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2285 int ret;
2287 bs = NULL;
2288 while ((bs = bdrv_next(bs))) {
2289 if (bdrv_can_snapshot(bs) &&
2290 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2292 ret = bdrv_snapshot_delete(bs, name);
2293 if (ret < 0) {
2294 monitor_printf(mon,
2295 "Error while deleting snapshot on '%s'\n",
2296 bdrv_get_device_name(bs));
2297 return -1;
2302 return 0;
2305 void do_savevm(Monitor *mon, const QDict *qdict)
2307 BlockDriverState *bs, *bs1;
2308 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2309 int ret;
2310 QEMUFile *f;
2311 int saved_vm_running;
2312 uint64_t vm_state_size;
2313 qemu_timeval tv;
2314 struct tm tm;
2315 const char *name = qdict_get_try_str(qdict, "name");
2317 /* Verify if there is a device that doesn't support snapshots and is writable */
2318 bs = NULL;
2319 while ((bs = bdrv_next(bs))) {
2321 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2322 continue;
2325 if (!bdrv_can_snapshot(bs)) {
2326 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2327 bdrv_get_device_name(bs));
2328 return;
2332 bs = find_vmstate_bs();
2333 if (!bs) {
2334 monitor_printf(mon, "No block device can accept snapshots\n");
2335 return;
2338 saved_vm_running = runstate_is_running();
2339 vm_stop(RUN_STATE_SAVE_VM);
2341 memset(sn, 0, sizeof(*sn));
2343 /* fill auxiliary fields */
2344 qemu_gettimeofday(&tv);
2345 sn->date_sec = tv.tv_sec;
2346 sn->date_nsec = tv.tv_usec * 1000;
2347 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2349 if (name) {
2350 ret = bdrv_snapshot_find(bs, old_sn, name);
2351 if (ret >= 0) {
2352 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2353 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2354 } else {
2355 pstrcpy(sn->name, sizeof(sn->name), name);
2357 } else {
2358 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2359 localtime_r((const time_t *)&tv.tv_sec, &tm);
2360 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2363 /* Delete old snapshots of the same name */
2364 if (name && del_existing_snapshots(mon, name) < 0) {
2365 goto the_end;
2368 /* save the VM state */
2369 f = qemu_fopen_bdrv(bs, 1);
2370 if (!f) {
2371 monitor_printf(mon, "Could not open VM state file\n");
2372 goto the_end;
2374 ret = qemu_savevm_state(f);
2375 vm_state_size = qemu_ftell(f);
2376 qemu_fclose(f);
2377 if (ret < 0) {
2378 monitor_printf(mon, "Error %d while writing VM\n", ret);
2379 goto the_end;
2382 /* create the snapshots */
2384 bs1 = NULL;
2385 while ((bs1 = bdrv_next(bs1))) {
2386 if (bdrv_can_snapshot(bs1)) {
2387 /* Write VM state size only to the image that contains the state */
2388 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2389 ret = bdrv_snapshot_create(bs1, sn);
2390 if (ret < 0) {
2391 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2392 bdrv_get_device_name(bs1));
2397 the_end:
2398 if (saved_vm_running)
2399 vm_start();
2402 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2404 QEMUFile *f;
2405 int saved_vm_running;
2406 int ret;
2408 saved_vm_running = runstate_is_running();
2409 vm_stop(RUN_STATE_SAVE_VM);
2411 f = qemu_fopen(filename, "wb");
2412 if (!f) {
2413 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2414 goto the_end;
2416 ret = qemu_save_device_state(f);
2417 qemu_fclose(f);
2418 if (ret < 0) {
2419 error_set(errp, QERR_IO_ERROR);
2422 the_end:
2423 if (saved_vm_running)
2424 vm_start();
2427 int load_vmstate(const char *name)
2429 BlockDriverState *bs, *bs_vm_state;
2430 QEMUSnapshotInfo sn;
2431 QEMUFile *f;
2432 int ret;
2434 bs_vm_state = find_vmstate_bs();
2435 if (!bs_vm_state) {
2436 error_report("No block device supports snapshots");
2437 return -ENOTSUP;
2440 /* Don't even try to load empty VM states */
2441 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2442 if (ret < 0) {
2443 return ret;
2444 } else if (sn.vm_state_size == 0) {
2445 error_report("This is a disk-only snapshot. Revert to it offline "
2446 "using qemu-img.");
2447 return -EINVAL;
2450 /* Verify if there is any device that doesn't support snapshots and is
2451 writable and check if the requested snapshot is available too. */
2452 bs = NULL;
2453 while ((bs = bdrv_next(bs))) {
2455 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2456 continue;
2459 if (!bdrv_can_snapshot(bs)) {
2460 error_report("Device '%s' is writable but does not support snapshots.",
2461 bdrv_get_device_name(bs));
2462 return -ENOTSUP;
2465 ret = bdrv_snapshot_find(bs, &sn, name);
2466 if (ret < 0) {
2467 error_report("Device '%s' does not have the requested snapshot '%s'",
2468 bdrv_get_device_name(bs), name);
2469 return ret;
2473 /* Flush all IO requests so they don't interfere with the new state. */
2474 bdrv_drain_all();
2476 bs = NULL;
2477 while ((bs = bdrv_next(bs))) {
2478 if (bdrv_can_snapshot(bs)) {
2479 ret = bdrv_snapshot_goto(bs, name);
2480 if (ret < 0) {
2481 error_report("Error %d while activating snapshot '%s' on '%s'",
2482 ret, name, bdrv_get_device_name(bs));
2483 return ret;
2488 /* restore the VM state */
2489 f = qemu_fopen_bdrv(bs_vm_state, 0);
2490 if (!f) {
2491 error_report("Could not open VM state file");
2492 return -EINVAL;
2495 qemu_system_reset(VMRESET_SILENT);
2496 ret = qemu_loadvm_state(f);
2498 qemu_fclose(f);
2499 if (ret < 0) {
2500 error_report("Error %d while loading VM state", ret);
2501 return ret;
2504 return 0;
2507 void do_delvm(Monitor *mon, const QDict *qdict)
2509 BlockDriverState *bs, *bs1;
2510 int ret;
2511 const char *name = qdict_get_str(qdict, "name");
2513 bs = find_vmstate_bs();
2514 if (!bs) {
2515 monitor_printf(mon, "No block device supports snapshots\n");
2516 return;
2519 bs1 = NULL;
2520 while ((bs1 = bdrv_next(bs1))) {
2521 if (bdrv_can_snapshot(bs1)) {
2522 ret = bdrv_snapshot_delete(bs1, name);
2523 if (ret < 0) {
2524 if (ret == -ENOTSUP)
2525 monitor_printf(mon,
2526 "Snapshots not supported on device '%s'\n",
2527 bdrv_get_device_name(bs1));
2528 else
2529 monitor_printf(mon, "Error %d while deleting snapshot on "
2530 "'%s'\n", ret, bdrv_get_device_name(bs1));
2536 void do_info_snapshots(Monitor *mon, const QDict *qdict)
2538 BlockDriverState *bs, *bs1;
2539 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2540 int nb_sns, i, ret, available;
2541 int total;
2542 int *available_snapshots;
2544 bs = find_vmstate_bs();
2545 if (!bs) {
2546 monitor_printf(mon, "No available block device supports snapshots\n");
2547 return;
2550 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2551 if (nb_sns < 0) {
2552 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2553 return;
2556 if (nb_sns == 0) {
2557 monitor_printf(mon, "There is no snapshot available.\n");
2558 return;
2561 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2562 total = 0;
2563 for (i = 0; i < nb_sns; i++) {
2564 sn = &sn_tab[i];
2565 available = 1;
2566 bs1 = NULL;
2568 while ((bs1 = bdrv_next(bs1))) {
2569 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2570 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2571 if (ret < 0) {
2572 available = 0;
2573 break;
2578 if (available) {
2579 available_snapshots[total] = i;
2580 total++;
2584 if (total > 0) {
2585 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2586 monitor_printf(mon, "\n");
2587 for (i = 0; i < total; i++) {
2588 sn = &sn_tab[available_snapshots[i]];
2589 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2590 monitor_printf(mon, "\n");
2592 } else {
2593 monitor_printf(mon, "There is no suitable snapshot available\n");
2596 g_free(sn_tab);
2597 g_free(available_snapshots);
2601 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2603 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2604 memory_region_name(mr), dev);
2607 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2609 /* Nothing do to while the implementation is in RAMBlock */
2612 void vmstate_register_ram_global(MemoryRegion *mr)
2614 vmstate_register_ram(mr, NULL);