block: move snapshot code in block.c to block/snapshot.c
[qemu/ar7.git] / savevm.c
blob916080d7dc55b1395e435bff9f37b4b085109924
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"
45 #define SELF_ANNOUNCE_ROUNDS 5
47 #ifndef ETH_P_RARP
48 #define ETH_P_RARP 0x8035
49 #endif
50 #define ARP_HTYPE_ETH 0x0001
51 #define ARP_PTYPE_IP 0x0800
52 #define ARP_OP_REQUEST_REV 0x3
54 static int announce_self_create(uint8_t *buf,
55 uint8_t *mac_addr)
57 /* Ethernet header. */
58 memset(buf, 0xff, 6); /* destination MAC addr */
59 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
60 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
62 /* RARP header. */
63 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
64 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
65 *(buf + 18) = 6; /* hardware addr length (ethernet) */
66 *(buf + 19) = 4; /* protocol addr length (IPv4) */
67 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
68 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
69 memset(buf + 28, 0x00, 4); /* source protocol addr */
70 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
71 memset(buf + 38, 0x00, 4); /* target protocol addr */
73 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
74 memset(buf + 42, 0x00, 18);
76 return 60; /* len (FCS will be added by hardware) */
79 static void qemu_announce_self_iter(NICState *nic, void *opaque)
81 uint8_t buf[60];
82 int len;
84 len = announce_self_create(buf, nic->conf->macaddr.a);
86 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
90 static void qemu_announce_self_once(void *opaque)
92 static int count = SELF_ANNOUNCE_ROUNDS;
93 QEMUTimer *timer = *(QEMUTimer **)opaque;
95 qemu_foreach_nic(qemu_announce_self_iter, NULL);
97 if (--count) {
98 /* delay 50ms, 150ms, 250ms, ... */
99 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
100 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
101 } else {
102 qemu_del_timer(timer);
103 qemu_free_timer(timer);
107 void qemu_announce_self(void)
109 static QEMUTimer *timer;
110 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
111 qemu_announce_self_once(&timer);
114 /***********************************************************/
115 /* savevm/loadvm support */
117 #define IO_BUF_SIZE 32768
118 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
120 struct QEMUFile {
121 const QEMUFileOps *ops;
122 void *opaque;
124 int64_t bytes_xfer;
125 int64_t xfer_limit;
127 int64_t pos; /* start of buffer when writing, end of buffer
128 when reading */
129 int buf_index;
130 int buf_size; /* 0 when writing */
131 uint8_t buf[IO_BUF_SIZE];
133 struct iovec iov[MAX_IOV_SIZE];
134 unsigned int iovcnt;
136 int last_error;
139 typedef struct QEMUFileStdio
141 FILE *stdio_file;
142 QEMUFile *file;
143 } QEMUFileStdio;
145 typedef struct QEMUFileSocket
147 int fd;
148 QEMUFile *file;
149 } QEMUFileSocket;
151 typedef struct {
152 Coroutine *co;
153 int fd;
154 } FDYieldUntilData;
156 static void fd_coroutine_enter(void *opaque)
158 FDYieldUntilData *data = opaque;
159 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
160 qemu_coroutine_enter(data->co, NULL);
164 * Yield until a file descriptor becomes readable
166 * Note that this function clobbers the handlers for the file descriptor.
168 static void coroutine_fn yield_until_fd_readable(int fd)
170 FDYieldUntilData data;
172 assert(qemu_in_coroutine());
173 data.co = qemu_coroutine_self();
174 data.fd = fd;
175 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
176 qemu_coroutine_yield();
179 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
180 int64_t pos)
182 QEMUFileSocket *s = opaque;
183 ssize_t len;
184 ssize_t size = iov_size(iov, iovcnt);
186 len = iov_send(s->fd, iov, iovcnt, 0, size);
187 if (len < size) {
188 len = -socket_error();
190 return len;
193 static int socket_get_fd(void *opaque)
195 QEMUFileSocket *s = opaque;
197 return s->fd;
200 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
202 QEMUFileSocket *s = opaque;
203 ssize_t len;
205 for (;;) {
206 len = qemu_recv(s->fd, buf, size, 0);
207 if (len != -1) {
208 break;
210 if (socket_error() == EAGAIN) {
211 yield_until_fd_readable(s->fd);
212 } else if (socket_error() != EINTR) {
213 break;
217 if (len == -1) {
218 len = -socket_error();
220 return len;
223 static int socket_close(void *opaque)
225 QEMUFileSocket *s = opaque;
226 closesocket(s->fd);
227 g_free(s);
228 return 0;
231 static int stdio_get_fd(void *opaque)
233 QEMUFileStdio *s = opaque;
235 return fileno(s->stdio_file);
238 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
240 QEMUFileStdio *s = opaque;
241 return fwrite(buf, 1, size, s->stdio_file);
244 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
246 QEMUFileStdio *s = opaque;
247 FILE *fp = s->stdio_file;
248 int bytes;
250 for (;;) {
251 clearerr(fp);
252 bytes = fread(buf, 1, size, fp);
253 if (bytes != 0 || !ferror(fp)) {
254 break;
256 if (errno == EAGAIN) {
257 yield_until_fd_readable(fileno(fp));
258 } else if (errno != EINTR) {
259 break;
262 return bytes;
265 static int stdio_pclose(void *opaque)
267 QEMUFileStdio *s = opaque;
268 int ret;
269 ret = pclose(s->stdio_file);
270 if (ret == -1) {
271 ret = -errno;
272 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
273 /* close succeeded, but non-zero exit code: */
274 ret = -EIO; /* fake errno value */
276 g_free(s);
277 return ret;
280 static int stdio_fclose(void *opaque)
282 QEMUFileStdio *s = opaque;
283 int ret = 0;
285 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
286 int fd = fileno(s->stdio_file);
287 struct stat st;
289 ret = fstat(fd, &st);
290 if (ret == 0 && S_ISREG(st.st_mode)) {
292 * If the file handle is a regular file make sure the
293 * data is flushed to disk before signaling success.
295 ret = fsync(fd);
296 if (ret != 0) {
297 ret = -errno;
298 return ret;
302 if (fclose(s->stdio_file) == EOF) {
303 ret = -errno;
305 g_free(s);
306 return ret;
309 static const QEMUFileOps stdio_pipe_read_ops = {
310 .get_fd = stdio_get_fd,
311 .get_buffer = stdio_get_buffer,
312 .close = stdio_pclose
315 static const QEMUFileOps stdio_pipe_write_ops = {
316 .get_fd = stdio_get_fd,
317 .put_buffer = stdio_put_buffer,
318 .close = stdio_pclose
321 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
323 FILE *stdio_file;
324 QEMUFileStdio *s;
326 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
327 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
328 return NULL;
331 stdio_file = popen(command, mode);
332 if (stdio_file == NULL) {
333 return NULL;
336 s = g_malloc0(sizeof(QEMUFileStdio));
338 s->stdio_file = stdio_file;
340 if(mode[0] == 'r') {
341 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
342 } else {
343 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
345 return s->file;
348 static const QEMUFileOps stdio_file_read_ops = {
349 .get_fd = stdio_get_fd,
350 .get_buffer = stdio_get_buffer,
351 .close = stdio_fclose
354 static const QEMUFileOps stdio_file_write_ops = {
355 .get_fd = stdio_get_fd,
356 .put_buffer = stdio_put_buffer,
357 .close = stdio_fclose
360 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
361 int64_t pos)
363 QEMUFileSocket *s = opaque;
364 ssize_t len, offset;
365 ssize_t size = iov_size(iov, iovcnt);
366 ssize_t total = 0;
368 assert(iovcnt > 0);
369 offset = 0;
370 while (size > 0) {
371 /* Find the next start position; skip all full-sized vector elements */
372 while (offset >= iov[0].iov_len) {
373 offset -= iov[0].iov_len;
374 iov++, iovcnt--;
377 /* skip `offset' bytes from the (now) first element, undo it on exit */
378 assert(iovcnt > 0);
379 iov[0].iov_base += offset;
380 iov[0].iov_len -= offset;
382 do {
383 len = writev(s->fd, iov, iovcnt);
384 } while (len == -1 && errno == EINTR);
385 if (len == -1) {
386 return -errno;
389 /* Undo the changes above */
390 iov[0].iov_base -= offset;
391 iov[0].iov_len += offset;
393 /* Prepare for the next iteration */
394 offset += len;
395 total += len;
396 size -= len;
399 return total;
402 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
404 QEMUFileSocket *s = opaque;
405 ssize_t len;
407 for (;;) {
408 len = read(s->fd, buf, size);
409 if (len != -1) {
410 break;
412 if (errno == EAGAIN) {
413 yield_until_fd_readable(s->fd);
414 } else if (errno != EINTR) {
415 break;
419 if (len == -1) {
420 len = -errno;
422 return len;
425 static int unix_close(void *opaque)
427 QEMUFileSocket *s = opaque;
428 close(s->fd);
429 g_free(s);
430 return 0;
433 static const QEMUFileOps unix_read_ops = {
434 .get_fd = socket_get_fd,
435 .get_buffer = unix_get_buffer,
436 .close = unix_close
439 static const QEMUFileOps unix_write_ops = {
440 .get_fd = socket_get_fd,
441 .writev_buffer = unix_writev_buffer,
442 .close = unix_close
445 QEMUFile *qemu_fdopen(int fd, const char *mode)
447 QEMUFileSocket *s;
449 if (mode == NULL ||
450 (mode[0] != 'r' && mode[0] != 'w') ||
451 mode[1] != 'b' || mode[2] != 0) {
452 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
453 return NULL;
456 s = g_malloc0(sizeof(QEMUFileSocket));
457 s->fd = fd;
459 if(mode[0] == 'r') {
460 s->file = qemu_fopen_ops(s, &unix_read_ops);
461 } else {
462 s->file = qemu_fopen_ops(s, &unix_write_ops);
464 return s->file;
467 static const QEMUFileOps socket_read_ops = {
468 .get_fd = socket_get_fd,
469 .get_buffer = socket_get_buffer,
470 .close = socket_close
473 static const QEMUFileOps socket_write_ops = {
474 .get_fd = socket_get_fd,
475 .writev_buffer = socket_writev_buffer,
476 .close = socket_close
479 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
481 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
483 if (mode == NULL ||
484 (mode[0] != 'r' && mode[0] != 'w') ||
485 mode[1] != 'b' || mode[2] != 0) {
486 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
487 return NULL;
490 s->fd = fd;
491 if (mode[0] == 'w') {
492 qemu_set_block(s->fd);
493 s->file = qemu_fopen_ops(s, &socket_write_ops);
494 } else {
495 s->file = qemu_fopen_ops(s, &socket_read_ops);
497 return s->file;
500 QEMUFile *qemu_fopen(const char *filename, const char *mode)
502 QEMUFileStdio *s;
504 if (mode == NULL ||
505 (mode[0] != 'r' && mode[0] != 'w') ||
506 mode[1] != 'b' || mode[2] != 0) {
507 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
508 return NULL;
511 s = g_malloc0(sizeof(QEMUFileStdio));
513 s->stdio_file = fopen(filename, mode);
514 if (!s->stdio_file)
515 goto fail;
517 if(mode[0] == 'w') {
518 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
519 } else {
520 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
522 return s->file;
523 fail:
524 g_free(s);
525 return NULL;
528 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
529 int64_t pos)
531 int ret;
532 QEMUIOVector qiov;
534 qemu_iovec_init_external(&qiov, iov, iovcnt);
535 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
536 if (ret < 0) {
537 return ret;
540 return qiov.size;
543 static int block_put_buffer(void *opaque, const uint8_t *buf,
544 int64_t pos, int size)
546 bdrv_save_vmstate(opaque, buf, pos, size);
547 return size;
550 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
552 return bdrv_load_vmstate(opaque, buf, pos, size);
555 static int bdrv_fclose(void *opaque)
557 return bdrv_flush(opaque);
560 static const QEMUFileOps bdrv_read_ops = {
561 .get_buffer = block_get_buffer,
562 .close = bdrv_fclose
565 static const QEMUFileOps bdrv_write_ops = {
566 .put_buffer = block_put_buffer,
567 .writev_buffer = block_writev_buffer,
568 .close = bdrv_fclose
571 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
573 if (is_writable)
574 return qemu_fopen_ops(bs, &bdrv_write_ops);
575 return qemu_fopen_ops(bs, &bdrv_read_ops);
578 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
580 QEMUFile *f;
582 f = g_malloc0(sizeof(QEMUFile));
584 f->opaque = opaque;
585 f->ops = ops;
586 return f;
589 int qemu_file_get_error(QEMUFile *f)
591 return f->last_error;
594 static void qemu_file_set_error(QEMUFile *f, int ret)
596 if (f->last_error == 0) {
597 f->last_error = ret;
601 static inline bool qemu_file_is_writable(QEMUFile *f)
603 return f->ops->writev_buffer || f->ops->put_buffer;
607 * Flushes QEMUFile buffer
609 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
610 * put_buffer ops.
612 static void qemu_fflush(QEMUFile *f)
614 ssize_t ret = 0;
616 if (!qemu_file_is_writable(f)) {
617 return;
620 if (f->ops->writev_buffer) {
621 if (f->iovcnt > 0) {
622 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
624 } else {
625 if (f->buf_index > 0) {
626 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
629 if (ret >= 0) {
630 f->pos += ret;
632 f->buf_index = 0;
633 f->iovcnt = 0;
634 if (ret < 0) {
635 qemu_file_set_error(f, ret);
639 static void qemu_fill_buffer(QEMUFile *f)
641 int len;
642 int pending;
644 assert(!qemu_file_is_writable(f));
646 pending = f->buf_size - f->buf_index;
647 if (pending > 0) {
648 memmove(f->buf, f->buf + f->buf_index, pending);
650 f->buf_index = 0;
651 f->buf_size = pending;
653 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
654 IO_BUF_SIZE - pending);
655 if (len > 0) {
656 f->buf_size += len;
657 f->pos += len;
658 } else if (len == 0) {
659 qemu_file_set_error(f, -EIO);
660 } else if (len != -EAGAIN)
661 qemu_file_set_error(f, len);
664 int qemu_get_fd(QEMUFile *f)
666 if (f->ops->get_fd) {
667 return f->ops->get_fd(f->opaque);
669 return -1;
672 /** Closes the file
674 * Returns negative error value if any error happened on previous operations or
675 * while closing the file. Returns 0 or positive number on success.
677 * The meaning of return value on success depends on the specific backend
678 * being used.
680 int qemu_fclose(QEMUFile *f)
682 int ret;
683 qemu_fflush(f);
684 ret = qemu_file_get_error(f);
686 if (f->ops->close) {
687 int ret2 = f->ops->close(f->opaque);
688 if (ret >= 0) {
689 ret = ret2;
692 /* If any error was spotted before closing, we should report it
693 * instead of the close() return value.
695 if (f->last_error) {
696 ret = f->last_error;
698 g_free(f);
699 return ret;
702 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
704 /* check for adjacent buffer and coalesce them */
705 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
706 f->iov[f->iovcnt - 1].iov_len) {
707 f->iov[f->iovcnt - 1].iov_len += size;
708 } else {
709 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
710 f->iov[f->iovcnt++].iov_len = size;
713 if (f->iovcnt >= MAX_IOV_SIZE) {
714 qemu_fflush(f);
718 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
720 if (!f->ops->writev_buffer) {
721 qemu_put_buffer(f, buf, size);
722 return;
725 if (f->last_error) {
726 return;
729 f->bytes_xfer += size;
730 add_to_iovec(f, buf, size);
733 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
735 int l;
737 if (f->last_error) {
738 return;
741 while (size > 0) {
742 l = IO_BUF_SIZE - f->buf_index;
743 if (l > size)
744 l = size;
745 memcpy(f->buf + f->buf_index, buf, l);
746 f->bytes_xfer += size;
747 if (f->ops->writev_buffer) {
748 add_to_iovec(f, f->buf + f->buf_index, l);
750 f->buf_index += l;
751 if (f->buf_index == IO_BUF_SIZE) {
752 qemu_fflush(f);
754 if (qemu_file_get_error(f)) {
755 break;
757 buf += l;
758 size -= l;
762 void qemu_put_byte(QEMUFile *f, int v)
764 if (f->last_error) {
765 return;
768 f->buf[f->buf_index] = v;
769 f->bytes_xfer++;
770 if (f->ops->writev_buffer) {
771 add_to_iovec(f, f->buf + f->buf_index, 1);
773 f->buf_index++;
774 if (f->buf_index == IO_BUF_SIZE) {
775 qemu_fflush(f);
779 static void qemu_file_skip(QEMUFile *f, int size)
781 if (f->buf_index + size <= f->buf_size) {
782 f->buf_index += size;
786 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
788 int pending;
789 int index;
791 assert(!qemu_file_is_writable(f));
793 index = f->buf_index + offset;
794 pending = f->buf_size - index;
795 if (pending < size) {
796 qemu_fill_buffer(f);
797 index = f->buf_index + offset;
798 pending = f->buf_size - index;
801 if (pending <= 0) {
802 return 0;
804 if (size > pending) {
805 size = pending;
808 memcpy(buf, f->buf + index, size);
809 return size;
812 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
814 int pending = size;
815 int done = 0;
817 while (pending > 0) {
818 int res;
820 res = qemu_peek_buffer(f, buf, pending, 0);
821 if (res == 0) {
822 return done;
824 qemu_file_skip(f, res);
825 buf += res;
826 pending -= res;
827 done += res;
829 return done;
832 static int qemu_peek_byte(QEMUFile *f, int offset)
834 int index = f->buf_index + offset;
836 assert(!qemu_file_is_writable(f));
838 if (index >= f->buf_size) {
839 qemu_fill_buffer(f);
840 index = f->buf_index + offset;
841 if (index >= f->buf_size) {
842 return 0;
845 return f->buf[index];
848 int qemu_get_byte(QEMUFile *f)
850 int result;
852 result = qemu_peek_byte(f, 0);
853 qemu_file_skip(f, 1);
854 return result;
857 int64_t qemu_ftell(QEMUFile *f)
859 qemu_fflush(f);
860 return f->pos;
863 int qemu_file_rate_limit(QEMUFile *f)
865 if (qemu_file_get_error(f)) {
866 return 1;
868 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
869 return 1;
871 return 0;
874 int64_t qemu_file_get_rate_limit(QEMUFile *f)
876 return f->xfer_limit;
879 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
881 f->xfer_limit = limit;
884 void qemu_file_reset_rate_limit(QEMUFile *f)
886 f->bytes_xfer = 0;
889 void qemu_put_be16(QEMUFile *f, unsigned int v)
891 qemu_put_byte(f, v >> 8);
892 qemu_put_byte(f, v);
895 void qemu_put_be32(QEMUFile *f, unsigned int v)
897 qemu_put_byte(f, v >> 24);
898 qemu_put_byte(f, v >> 16);
899 qemu_put_byte(f, v >> 8);
900 qemu_put_byte(f, v);
903 void qemu_put_be64(QEMUFile *f, uint64_t v)
905 qemu_put_be32(f, v >> 32);
906 qemu_put_be32(f, v);
909 unsigned int qemu_get_be16(QEMUFile *f)
911 unsigned int v;
912 v = qemu_get_byte(f) << 8;
913 v |= qemu_get_byte(f);
914 return v;
917 unsigned int qemu_get_be32(QEMUFile *f)
919 unsigned int v;
920 v = qemu_get_byte(f) << 24;
921 v |= qemu_get_byte(f) << 16;
922 v |= qemu_get_byte(f) << 8;
923 v |= qemu_get_byte(f);
924 return v;
927 uint64_t qemu_get_be64(QEMUFile *f)
929 uint64_t v;
930 v = (uint64_t)qemu_get_be32(f) << 32;
931 v |= qemu_get_be32(f);
932 return v;
936 /* timer */
938 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
940 uint64_t expire_time;
942 expire_time = qemu_timer_expire_time_ns(ts);
943 qemu_put_be64(f, expire_time);
946 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
948 uint64_t expire_time;
950 expire_time = qemu_get_be64(f);
951 if (expire_time != -1) {
952 qemu_mod_timer_ns(ts, expire_time);
953 } else {
954 qemu_del_timer(ts);
959 /* bool */
961 static int get_bool(QEMUFile *f, void *pv, size_t size)
963 bool *v = pv;
964 *v = qemu_get_byte(f);
965 return 0;
968 static void put_bool(QEMUFile *f, void *pv, size_t size)
970 bool *v = pv;
971 qemu_put_byte(f, *v);
974 const VMStateInfo vmstate_info_bool = {
975 .name = "bool",
976 .get = get_bool,
977 .put = put_bool,
980 /* 8 bit int */
982 static int get_int8(QEMUFile *f, void *pv, size_t size)
984 int8_t *v = pv;
985 qemu_get_s8s(f, v);
986 return 0;
989 static void put_int8(QEMUFile *f, void *pv, size_t size)
991 int8_t *v = pv;
992 qemu_put_s8s(f, v);
995 const VMStateInfo vmstate_info_int8 = {
996 .name = "int8",
997 .get = get_int8,
998 .put = put_int8,
1001 /* 16 bit int */
1003 static int get_int16(QEMUFile *f, void *pv, size_t size)
1005 int16_t *v = pv;
1006 qemu_get_sbe16s(f, v);
1007 return 0;
1010 static void put_int16(QEMUFile *f, void *pv, size_t size)
1012 int16_t *v = pv;
1013 qemu_put_sbe16s(f, v);
1016 const VMStateInfo vmstate_info_int16 = {
1017 .name = "int16",
1018 .get = get_int16,
1019 .put = put_int16,
1022 /* 32 bit int */
1024 static int get_int32(QEMUFile *f, void *pv, size_t size)
1026 int32_t *v = pv;
1027 qemu_get_sbe32s(f, v);
1028 return 0;
1031 static void put_int32(QEMUFile *f, void *pv, size_t size)
1033 int32_t *v = pv;
1034 qemu_put_sbe32s(f, v);
1037 const VMStateInfo vmstate_info_int32 = {
1038 .name = "int32",
1039 .get = get_int32,
1040 .put = put_int32,
1043 /* 32 bit int. See that the received value is the same than the one
1044 in the field */
1046 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1048 int32_t *v = pv;
1049 int32_t v2;
1050 qemu_get_sbe32s(f, &v2);
1052 if (*v == v2)
1053 return 0;
1054 return -EINVAL;
1057 const VMStateInfo vmstate_info_int32_equal = {
1058 .name = "int32 equal",
1059 .get = get_int32_equal,
1060 .put = put_int32,
1063 /* 32 bit int. See that the received value is the less or the same
1064 than the one in the field */
1066 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1068 int32_t *old = pv;
1069 int32_t new;
1070 qemu_get_sbe32s(f, &new);
1072 if (*old <= new)
1073 return 0;
1074 return -EINVAL;
1077 const VMStateInfo vmstate_info_int32_le = {
1078 .name = "int32 equal",
1079 .get = get_int32_le,
1080 .put = put_int32,
1083 /* 64 bit int */
1085 static int get_int64(QEMUFile *f, void *pv, size_t size)
1087 int64_t *v = pv;
1088 qemu_get_sbe64s(f, v);
1089 return 0;
1092 static void put_int64(QEMUFile *f, void *pv, size_t size)
1094 int64_t *v = pv;
1095 qemu_put_sbe64s(f, v);
1098 const VMStateInfo vmstate_info_int64 = {
1099 .name = "int64",
1100 .get = get_int64,
1101 .put = put_int64,
1104 /* 8 bit unsigned int */
1106 static int get_uint8(QEMUFile *f, void *pv, size_t size)
1108 uint8_t *v = pv;
1109 qemu_get_8s(f, v);
1110 return 0;
1113 static void put_uint8(QEMUFile *f, void *pv, size_t size)
1115 uint8_t *v = pv;
1116 qemu_put_8s(f, v);
1119 const VMStateInfo vmstate_info_uint8 = {
1120 .name = "uint8",
1121 .get = get_uint8,
1122 .put = put_uint8,
1125 /* 16 bit unsigned int */
1127 static int get_uint16(QEMUFile *f, void *pv, size_t size)
1129 uint16_t *v = pv;
1130 qemu_get_be16s(f, v);
1131 return 0;
1134 static void put_uint16(QEMUFile *f, void *pv, size_t size)
1136 uint16_t *v = pv;
1137 qemu_put_be16s(f, v);
1140 const VMStateInfo vmstate_info_uint16 = {
1141 .name = "uint16",
1142 .get = get_uint16,
1143 .put = put_uint16,
1146 /* 32 bit unsigned int */
1148 static int get_uint32(QEMUFile *f, void *pv, size_t size)
1150 uint32_t *v = pv;
1151 qemu_get_be32s(f, v);
1152 return 0;
1155 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1157 uint32_t *v = pv;
1158 qemu_put_be32s(f, v);
1161 const VMStateInfo vmstate_info_uint32 = {
1162 .name = "uint32",
1163 .get = get_uint32,
1164 .put = put_uint32,
1167 /* 32 bit uint. See that the received value is the same than the one
1168 in the field */
1170 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1172 uint32_t *v = pv;
1173 uint32_t v2;
1174 qemu_get_be32s(f, &v2);
1176 if (*v == v2) {
1177 return 0;
1179 return -EINVAL;
1182 const VMStateInfo vmstate_info_uint32_equal = {
1183 .name = "uint32 equal",
1184 .get = get_uint32_equal,
1185 .put = put_uint32,
1188 /* 64 bit unsigned int */
1190 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1192 uint64_t *v = pv;
1193 qemu_get_be64s(f, v);
1194 return 0;
1197 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1199 uint64_t *v = pv;
1200 qemu_put_be64s(f, v);
1203 const VMStateInfo vmstate_info_uint64 = {
1204 .name = "uint64",
1205 .get = get_uint64,
1206 .put = put_uint64,
1209 /* 64 bit unsigned int. See that the received value is the same than the one
1210 in the field */
1212 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1214 uint64_t *v = pv;
1215 uint64_t v2;
1216 qemu_get_be64s(f, &v2);
1218 if (*v == v2) {
1219 return 0;
1221 return -EINVAL;
1224 const VMStateInfo vmstate_info_uint64_equal = {
1225 .name = "int64 equal",
1226 .get = get_uint64_equal,
1227 .put = put_uint64,
1230 /* 8 bit int. See that the received value is the same than the one
1231 in the field */
1233 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1235 uint8_t *v = pv;
1236 uint8_t v2;
1237 qemu_get_8s(f, &v2);
1239 if (*v == v2)
1240 return 0;
1241 return -EINVAL;
1244 const VMStateInfo vmstate_info_uint8_equal = {
1245 .name = "uint8 equal",
1246 .get = get_uint8_equal,
1247 .put = put_uint8,
1250 /* 16 bit unsigned int int. See that the received value is the same than the one
1251 in the field */
1253 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1255 uint16_t *v = pv;
1256 uint16_t v2;
1257 qemu_get_be16s(f, &v2);
1259 if (*v == v2)
1260 return 0;
1261 return -EINVAL;
1264 const VMStateInfo vmstate_info_uint16_equal = {
1265 .name = "uint16 equal",
1266 .get = get_uint16_equal,
1267 .put = put_uint16,
1270 /* floating point */
1272 static int get_float64(QEMUFile *f, void *pv, size_t size)
1274 float64 *v = pv;
1276 *v = make_float64(qemu_get_be64(f));
1277 return 0;
1280 static void put_float64(QEMUFile *f, void *pv, size_t size)
1282 uint64_t *v = pv;
1284 qemu_put_be64(f, float64_val(*v));
1287 const VMStateInfo vmstate_info_float64 = {
1288 .name = "float64",
1289 .get = get_float64,
1290 .put = put_float64,
1293 /* timers */
1295 static int get_timer(QEMUFile *f, void *pv, size_t size)
1297 QEMUTimer *v = pv;
1298 qemu_get_timer(f, v);
1299 return 0;
1302 static void put_timer(QEMUFile *f, void *pv, size_t size)
1304 QEMUTimer *v = pv;
1305 qemu_put_timer(f, v);
1308 const VMStateInfo vmstate_info_timer = {
1309 .name = "timer",
1310 .get = get_timer,
1311 .put = put_timer,
1314 /* uint8_t buffers */
1316 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1318 uint8_t *v = pv;
1319 qemu_get_buffer(f, v, size);
1320 return 0;
1323 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1325 uint8_t *v = pv;
1326 qemu_put_buffer(f, v, size);
1329 const VMStateInfo vmstate_info_buffer = {
1330 .name = "buffer",
1331 .get = get_buffer,
1332 .put = put_buffer,
1335 /* unused buffers: space that was used for some fields that are
1336 not useful anymore */
1338 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1340 uint8_t buf[1024];
1341 int block_len;
1343 while (size > 0) {
1344 block_len = MIN(sizeof(buf), size);
1345 size -= block_len;
1346 qemu_get_buffer(f, buf, block_len);
1348 return 0;
1351 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1353 static const uint8_t buf[1024];
1354 int block_len;
1356 while (size > 0) {
1357 block_len = MIN(sizeof(buf), size);
1358 size -= block_len;
1359 qemu_put_buffer(f, buf, block_len);
1363 const VMStateInfo vmstate_info_unused_buffer = {
1364 .name = "unused_buffer",
1365 .get = get_unused_buffer,
1366 .put = put_unused_buffer,
1369 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1370 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1371 * bit words with the bits in big endian order. The in-memory format
1372 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1374 /* This is the number of 64 bit words sent over the wire */
1375 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1376 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1378 unsigned long *bmp = pv;
1379 int i, idx = 0;
1380 for (i = 0; i < BITS_TO_U64S(size); i++) {
1381 uint64_t w = qemu_get_be64(f);
1382 bmp[idx++] = w;
1383 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1384 bmp[idx++] = w >> 32;
1387 return 0;
1390 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1392 unsigned long *bmp = pv;
1393 int i, idx = 0;
1394 for (i = 0; i < BITS_TO_U64S(size); i++) {
1395 uint64_t w = bmp[idx++];
1396 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1397 w |= ((uint64_t)bmp[idx++]) << 32;
1399 qemu_put_be64(f, w);
1403 const VMStateInfo vmstate_info_bitmap = {
1404 .name = "bitmap",
1405 .get = get_bitmap,
1406 .put = put_bitmap,
1409 typedef struct CompatEntry {
1410 char idstr[256];
1411 int instance_id;
1412 } CompatEntry;
1414 typedef struct SaveStateEntry {
1415 QTAILQ_ENTRY(SaveStateEntry) entry;
1416 char idstr[256];
1417 int instance_id;
1418 int alias_id;
1419 int version_id;
1420 int section_id;
1421 SaveVMHandlers *ops;
1422 const VMStateDescription *vmsd;
1423 void *opaque;
1424 CompatEntry *compat;
1425 int no_migrate;
1426 int is_ram;
1427 } SaveStateEntry;
1430 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1431 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1432 static int global_section_id;
1434 static int calculate_new_instance_id(const char *idstr)
1436 SaveStateEntry *se;
1437 int instance_id = 0;
1439 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1440 if (strcmp(idstr, se->idstr) == 0
1441 && instance_id <= se->instance_id) {
1442 instance_id = se->instance_id + 1;
1445 return instance_id;
1448 static int calculate_compat_instance_id(const char *idstr)
1450 SaveStateEntry *se;
1451 int instance_id = 0;
1453 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1454 if (!se->compat)
1455 continue;
1457 if (strcmp(idstr, se->compat->idstr) == 0
1458 && instance_id <= se->compat->instance_id) {
1459 instance_id = se->compat->instance_id + 1;
1462 return instance_id;
1465 /* TODO: Individual devices generally have very little idea about the rest
1466 of the system, so instance_id should be removed/replaced.
1467 Meanwhile pass -1 as instance_id if you do not already have a clearly
1468 distinguishing id for all instances of your device class. */
1469 int register_savevm_live(DeviceState *dev,
1470 const char *idstr,
1471 int instance_id,
1472 int version_id,
1473 SaveVMHandlers *ops,
1474 void *opaque)
1476 SaveStateEntry *se;
1478 se = g_malloc0(sizeof(SaveStateEntry));
1479 se->version_id = version_id;
1480 se->section_id = global_section_id++;
1481 se->ops = ops;
1482 se->opaque = opaque;
1483 se->vmsd = NULL;
1484 se->no_migrate = 0;
1485 /* if this is a live_savem then set is_ram */
1486 if (ops->save_live_setup != NULL) {
1487 se->is_ram = 1;
1490 if (dev) {
1491 char *id = qdev_get_dev_path(dev);
1492 if (id) {
1493 pstrcpy(se->idstr, sizeof(se->idstr), id);
1494 pstrcat(se->idstr, sizeof(se->idstr), "/");
1495 g_free(id);
1497 se->compat = g_malloc0(sizeof(CompatEntry));
1498 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1499 se->compat->instance_id = instance_id == -1 ?
1500 calculate_compat_instance_id(idstr) : instance_id;
1501 instance_id = -1;
1504 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1506 if (instance_id == -1) {
1507 se->instance_id = calculate_new_instance_id(se->idstr);
1508 } else {
1509 se->instance_id = instance_id;
1511 assert(!se->compat || se->instance_id == 0);
1512 /* add at the end of list */
1513 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1514 return 0;
1517 int register_savevm(DeviceState *dev,
1518 const char *idstr,
1519 int instance_id,
1520 int version_id,
1521 SaveStateHandler *save_state,
1522 LoadStateHandler *load_state,
1523 void *opaque)
1525 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1526 ops->save_state = save_state;
1527 ops->load_state = load_state;
1528 return register_savevm_live(dev, idstr, instance_id, version_id,
1529 ops, opaque);
1532 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1534 SaveStateEntry *se, *new_se;
1535 char id[256] = "";
1537 if (dev) {
1538 char *path = qdev_get_dev_path(dev);
1539 if (path) {
1540 pstrcpy(id, sizeof(id), path);
1541 pstrcat(id, sizeof(id), "/");
1542 g_free(path);
1545 pstrcat(id, sizeof(id), idstr);
1547 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1548 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1549 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1550 if (se->compat) {
1551 g_free(se->compat);
1553 g_free(se->ops);
1554 g_free(se);
1559 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1560 const VMStateDescription *vmsd,
1561 void *opaque, int alias_id,
1562 int required_for_version)
1564 SaveStateEntry *se;
1566 /* If this triggers, alias support can be dropped for the vmsd. */
1567 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1569 se = g_malloc0(sizeof(SaveStateEntry));
1570 se->version_id = vmsd->version_id;
1571 se->section_id = global_section_id++;
1572 se->opaque = opaque;
1573 se->vmsd = vmsd;
1574 se->alias_id = alias_id;
1575 se->no_migrate = vmsd->unmigratable;
1577 if (dev) {
1578 char *id = qdev_get_dev_path(dev);
1579 if (id) {
1580 pstrcpy(se->idstr, sizeof(se->idstr), id);
1581 pstrcat(se->idstr, sizeof(se->idstr), "/");
1582 g_free(id);
1584 se->compat = g_malloc0(sizeof(CompatEntry));
1585 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1586 se->compat->instance_id = instance_id == -1 ?
1587 calculate_compat_instance_id(vmsd->name) : instance_id;
1588 instance_id = -1;
1591 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1593 if (instance_id == -1) {
1594 se->instance_id = calculate_new_instance_id(se->idstr);
1595 } else {
1596 se->instance_id = instance_id;
1598 assert(!se->compat || se->instance_id == 0);
1599 /* add at the end of list */
1600 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1601 return 0;
1604 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1605 void *opaque)
1607 SaveStateEntry *se, *new_se;
1609 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1610 if (se->vmsd == vmsd && se->opaque == opaque) {
1611 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1612 if (se->compat) {
1613 g_free(se->compat);
1615 g_free(se);
1620 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1621 void *opaque);
1622 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1623 void *opaque);
1625 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1626 void *opaque, int version_id)
1628 VMStateField *field = vmsd->fields;
1629 int ret;
1631 if (version_id > vmsd->version_id) {
1632 return -EINVAL;
1634 if (version_id < vmsd->minimum_version_id_old) {
1635 return -EINVAL;
1637 if (version_id < vmsd->minimum_version_id) {
1638 return vmsd->load_state_old(f, opaque, version_id);
1640 if (vmsd->pre_load) {
1641 int ret = vmsd->pre_load(opaque);
1642 if (ret)
1643 return ret;
1645 while(field->name) {
1646 if ((field->field_exists &&
1647 field->field_exists(opaque, version_id)) ||
1648 (!field->field_exists &&
1649 field->version_id <= version_id)) {
1650 void *base_addr = opaque + field->offset;
1651 int i, n_elems = 1;
1652 int size = field->size;
1654 if (field->flags & VMS_VBUFFER) {
1655 size = *(int32_t *)(opaque+field->size_offset);
1656 if (field->flags & VMS_MULTIPLY) {
1657 size *= field->size;
1660 if (field->flags & VMS_ARRAY) {
1661 n_elems = field->num;
1662 } else if (field->flags & VMS_VARRAY_INT32) {
1663 n_elems = *(int32_t *)(opaque+field->num_offset);
1664 } else if (field->flags & VMS_VARRAY_UINT32) {
1665 n_elems = *(uint32_t *)(opaque+field->num_offset);
1666 } else if (field->flags & VMS_VARRAY_UINT16) {
1667 n_elems = *(uint16_t *)(opaque+field->num_offset);
1668 } else if (field->flags & VMS_VARRAY_UINT8) {
1669 n_elems = *(uint8_t *)(opaque+field->num_offset);
1671 if (field->flags & VMS_POINTER) {
1672 base_addr = *(void **)base_addr + field->start;
1674 for (i = 0; i < n_elems; i++) {
1675 void *addr = base_addr + size * i;
1677 if (field->flags & VMS_ARRAY_OF_POINTER) {
1678 addr = *(void **)addr;
1680 if (field->flags & VMS_STRUCT) {
1681 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1682 } else {
1683 ret = field->info->get(f, addr, size);
1686 if (ret < 0) {
1687 return ret;
1691 field++;
1693 ret = vmstate_subsection_load(f, vmsd, opaque);
1694 if (ret != 0) {
1695 return ret;
1697 if (vmsd->post_load) {
1698 return vmsd->post_load(opaque, version_id);
1700 return 0;
1703 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1704 void *opaque)
1706 VMStateField *field = vmsd->fields;
1708 if (vmsd->pre_save) {
1709 vmsd->pre_save(opaque);
1711 while(field->name) {
1712 if (!field->field_exists ||
1713 field->field_exists(opaque, vmsd->version_id)) {
1714 void *base_addr = opaque + field->offset;
1715 int i, n_elems = 1;
1716 int size = field->size;
1718 if (field->flags & VMS_VBUFFER) {
1719 size = *(int32_t *)(opaque+field->size_offset);
1720 if (field->flags & VMS_MULTIPLY) {
1721 size *= field->size;
1724 if (field->flags & VMS_ARRAY) {
1725 n_elems = field->num;
1726 } else if (field->flags & VMS_VARRAY_INT32) {
1727 n_elems = *(int32_t *)(opaque+field->num_offset);
1728 } else if (field->flags & VMS_VARRAY_UINT32) {
1729 n_elems = *(uint32_t *)(opaque+field->num_offset);
1730 } else if (field->flags & VMS_VARRAY_UINT16) {
1731 n_elems = *(uint16_t *)(opaque+field->num_offset);
1732 } else if (field->flags & VMS_VARRAY_UINT8) {
1733 n_elems = *(uint8_t *)(opaque+field->num_offset);
1735 if (field->flags & VMS_POINTER) {
1736 base_addr = *(void **)base_addr + field->start;
1738 for (i = 0; i < n_elems; i++) {
1739 void *addr = base_addr + size * i;
1741 if (field->flags & VMS_ARRAY_OF_POINTER) {
1742 addr = *(void **)addr;
1744 if (field->flags & VMS_STRUCT) {
1745 vmstate_save_state(f, field->vmsd, addr);
1746 } else {
1747 field->info->put(f, addr, size);
1751 field++;
1753 vmstate_subsection_save(f, vmsd, opaque);
1756 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1758 if (!se->vmsd) { /* Old style */
1759 return se->ops->load_state(f, se->opaque, version_id);
1761 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1764 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1766 if (!se->vmsd) { /* Old style */
1767 se->ops->save_state(f, se->opaque);
1768 return;
1770 vmstate_save_state(f,se->vmsd, se->opaque);
1773 #define QEMU_VM_FILE_MAGIC 0x5145564d
1774 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1775 #define QEMU_VM_FILE_VERSION 0x00000003
1777 #define QEMU_VM_EOF 0x00
1778 #define QEMU_VM_SECTION_START 0x01
1779 #define QEMU_VM_SECTION_PART 0x02
1780 #define QEMU_VM_SECTION_END 0x03
1781 #define QEMU_VM_SECTION_FULL 0x04
1782 #define QEMU_VM_SUBSECTION 0x05
1784 bool qemu_savevm_state_blocked(Error **errp)
1786 SaveStateEntry *se;
1788 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1789 if (se->no_migrate) {
1790 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1791 return true;
1794 return false;
1797 void qemu_savevm_state_begin(QEMUFile *f,
1798 const MigrationParams *params)
1800 SaveStateEntry *se;
1801 int ret;
1803 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1804 if (!se->ops || !se->ops->set_params) {
1805 continue;
1807 se->ops->set_params(params, se->opaque);
1810 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1811 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1813 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1814 int len;
1816 if (!se->ops || !se->ops->save_live_setup) {
1817 continue;
1819 if (se->ops && se->ops->is_active) {
1820 if (!se->ops->is_active(se->opaque)) {
1821 continue;
1824 /* Section type */
1825 qemu_put_byte(f, QEMU_VM_SECTION_START);
1826 qemu_put_be32(f, se->section_id);
1828 /* ID string */
1829 len = strlen(se->idstr);
1830 qemu_put_byte(f, len);
1831 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1833 qemu_put_be32(f, se->instance_id);
1834 qemu_put_be32(f, se->version_id);
1836 ret = se->ops->save_live_setup(f, se->opaque);
1837 if (ret < 0) {
1838 qemu_file_set_error(f, ret);
1839 break;
1845 * this function has three return values:
1846 * negative: there was one error, and we have -errno.
1847 * 0 : We haven't finished, caller have to go again
1848 * 1 : We have finished, we can go to complete phase
1850 int qemu_savevm_state_iterate(QEMUFile *f)
1852 SaveStateEntry *se;
1853 int ret = 1;
1855 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1856 if (!se->ops || !se->ops->save_live_iterate) {
1857 continue;
1859 if (se->ops && se->ops->is_active) {
1860 if (!se->ops->is_active(se->opaque)) {
1861 continue;
1864 if (qemu_file_rate_limit(f)) {
1865 return 0;
1867 trace_savevm_section_start();
1868 /* Section type */
1869 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1870 qemu_put_be32(f, se->section_id);
1872 ret = se->ops->save_live_iterate(f, se->opaque);
1873 trace_savevm_section_end(se->section_id);
1875 if (ret < 0) {
1876 qemu_file_set_error(f, ret);
1878 if (ret <= 0) {
1879 /* Do not proceed to the next vmstate before this one reported
1880 completion of the current stage. This serializes the migration
1881 and reduces the probability that a faster changing state is
1882 synchronized over and over again. */
1883 break;
1886 return ret;
1889 void qemu_savevm_state_complete(QEMUFile *f)
1891 SaveStateEntry *se;
1892 int ret;
1894 cpu_synchronize_all_states();
1896 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1897 if (!se->ops || !se->ops->save_live_complete) {
1898 continue;
1900 if (se->ops && se->ops->is_active) {
1901 if (!se->ops->is_active(se->opaque)) {
1902 continue;
1905 trace_savevm_section_start();
1906 /* Section type */
1907 qemu_put_byte(f, QEMU_VM_SECTION_END);
1908 qemu_put_be32(f, se->section_id);
1910 ret = se->ops->save_live_complete(f, se->opaque);
1911 trace_savevm_section_end(se->section_id);
1912 if (ret < 0) {
1913 qemu_file_set_error(f, ret);
1914 return;
1918 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1919 int len;
1921 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1922 continue;
1924 trace_savevm_section_start();
1925 /* Section type */
1926 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1927 qemu_put_be32(f, se->section_id);
1929 /* ID string */
1930 len = strlen(se->idstr);
1931 qemu_put_byte(f, len);
1932 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1934 qemu_put_be32(f, se->instance_id);
1935 qemu_put_be32(f, se->version_id);
1937 vmstate_save(f, se);
1938 trace_savevm_section_end(se->section_id);
1941 qemu_put_byte(f, QEMU_VM_EOF);
1942 qemu_fflush(f);
1945 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1947 SaveStateEntry *se;
1948 uint64_t ret = 0;
1950 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1951 if (!se->ops || !se->ops->save_live_pending) {
1952 continue;
1954 if (se->ops && se->ops->is_active) {
1955 if (!se->ops->is_active(se->opaque)) {
1956 continue;
1959 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1961 return ret;
1964 void qemu_savevm_state_cancel(void)
1966 SaveStateEntry *se;
1968 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1969 if (se->ops && se->ops->cancel) {
1970 se->ops->cancel(se->opaque);
1975 static int qemu_savevm_state(QEMUFile *f)
1977 int ret;
1978 MigrationParams params = {
1979 .blk = 0,
1980 .shared = 0
1983 if (qemu_savevm_state_blocked(NULL)) {
1984 return -EINVAL;
1987 qemu_mutex_unlock_iothread();
1988 qemu_savevm_state_begin(f, &params);
1989 qemu_mutex_lock_iothread();
1991 while (qemu_file_get_error(f) == 0) {
1992 if (qemu_savevm_state_iterate(f) > 0) {
1993 break;
1997 ret = qemu_file_get_error(f);
1998 if (ret == 0) {
1999 qemu_savevm_state_complete(f);
2000 ret = qemu_file_get_error(f);
2002 if (ret != 0) {
2003 qemu_savevm_state_cancel();
2005 return ret;
2008 static int qemu_save_device_state(QEMUFile *f)
2010 SaveStateEntry *se;
2012 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2013 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2015 cpu_synchronize_all_states();
2017 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2018 int len;
2020 if (se->is_ram) {
2021 continue;
2023 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
2024 continue;
2027 /* Section type */
2028 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2029 qemu_put_be32(f, se->section_id);
2031 /* ID string */
2032 len = strlen(se->idstr);
2033 qemu_put_byte(f, len);
2034 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2036 qemu_put_be32(f, se->instance_id);
2037 qemu_put_be32(f, se->version_id);
2039 vmstate_save(f, se);
2042 qemu_put_byte(f, QEMU_VM_EOF);
2044 return qemu_file_get_error(f);
2047 static SaveStateEntry *find_se(const char *idstr, int instance_id)
2049 SaveStateEntry *se;
2051 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2052 if (!strcmp(se->idstr, idstr) &&
2053 (instance_id == se->instance_id ||
2054 instance_id == se->alias_id))
2055 return se;
2056 /* Migrating from an older version? */
2057 if (strstr(se->idstr, idstr) && se->compat) {
2058 if (!strcmp(se->compat->idstr, idstr) &&
2059 (instance_id == se->compat->instance_id ||
2060 instance_id == se->alias_id))
2061 return se;
2064 return NULL;
2067 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2069 while(sub && sub->needed) {
2070 if (strcmp(idstr, sub->vmsd->name) == 0) {
2071 return sub->vmsd;
2073 sub++;
2075 return NULL;
2078 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2079 void *opaque)
2081 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
2082 char idstr[256];
2083 int ret;
2084 uint8_t version_id, len, size;
2085 const VMStateDescription *sub_vmsd;
2087 len = qemu_peek_byte(f, 1);
2088 if (len < strlen(vmsd->name) + 1) {
2089 /* subsection name has be be "section_name/a" */
2090 return 0;
2092 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2093 if (size != len) {
2094 return 0;
2096 idstr[size] = 0;
2098 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2099 /* it don't have a valid subsection name */
2100 return 0;
2102 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
2103 if (sub_vmsd == NULL) {
2104 return -ENOENT;
2106 qemu_file_skip(f, 1); /* subsection */
2107 qemu_file_skip(f, 1); /* len */
2108 qemu_file_skip(f, len); /* idstr */
2109 version_id = qemu_get_be32(f);
2111 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2112 if (ret) {
2113 return ret;
2116 return 0;
2119 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2120 void *opaque)
2122 const VMStateSubsection *sub = vmsd->subsections;
2124 while (sub && sub->needed) {
2125 if (sub->needed(opaque)) {
2126 const VMStateDescription *vmsd = sub->vmsd;
2127 uint8_t len;
2129 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2130 len = strlen(vmsd->name);
2131 qemu_put_byte(f, len);
2132 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2133 qemu_put_be32(f, vmsd->version_id);
2134 vmstate_save_state(f, vmsd, opaque);
2136 sub++;
2140 typedef struct LoadStateEntry {
2141 QLIST_ENTRY(LoadStateEntry) entry;
2142 SaveStateEntry *se;
2143 int section_id;
2144 int version_id;
2145 } LoadStateEntry;
2147 int qemu_loadvm_state(QEMUFile *f)
2149 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2150 QLIST_HEAD_INITIALIZER(loadvm_handlers);
2151 LoadStateEntry *le, *new_le;
2152 uint8_t section_type;
2153 unsigned int v;
2154 int ret;
2156 if (qemu_savevm_state_blocked(NULL)) {
2157 return -EINVAL;
2160 v = qemu_get_be32(f);
2161 if (v != QEMU_VM_FILE_MAGIC)
2162 return -EINVAL;
2164 v = qemu_get_be32(f);
2165 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2166 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2167 return -ENOTSUP;
2169 if (v != QEMU_VM_FILE_VERSION)
2170 return -ENOTSUP;
2172 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2173 uint32_t instance_id, version_id, section_id;
2174 SaveStateEntry *se;
2175 char idstr[257];
2176 int len;
2178 switch (section_type) {
2179 case QEMU_VM_SECTION_START:
2180 case QEMU_VM_SECTION_FULL:
2181 /* Read section start */
2182 section_id = qemu_get_be32(f);
2183 len = qemu_get_byte(f);
2184 qemu_get_buffer(f, (uint8_t *)idstr, len);
2185 idstr[len] = 0;
2186 instance_id = qemu_get_be32(f);
2187 version_id = qemu_get_be32(f);
2189 /* Find savevm section */
2190 se = find_se(idstr, instance_id);
2191 if (se == NULL) {
2192 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2193 ret = -EINVAL;
2194 goto out;
2197 /* Validate version */
2198 if (version_id > se->version_id) {
2199 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2200 version_id, idstr, se->version_id);
2201 ret = -EINVAL;
2202 goto out;
2205 /* Add entry */
2206 le = g_malloc0(sizeof(*le));
2208 le->se = se;
2209 le->section_id = section_id;
2210 le->version_id = version_id;
2211 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2213 ret = vmstate_load(f, le->se, le->version_id);
2214 if (ret < 0) {
2215 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2216 instance_id, idstr);
2217 goto out;
2219 break;
2220 case QEMU_VM_SECTION_PART:
2221 case QEMU_VM_SECTION_END:
2222 section_id = qemu_get_be32(f);
2224 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2225 if (le->section_id == section_id) {
2226 break;
2229 if (le == NULL) {
2230 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2231 ret = -EINVAL;
2232 goto out;
2235 ret = vmstate_load(f, le->se, le->version_id);
2236 if (ret < 0) {
2237 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2238 section_id);
2239 goto out;
2241 break;
2242 default:
2243 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2244 ret = -EINVAL;
2245 goto out;
2249 cpu_synchronize_all_post_init();
2251 ret = 0;
2253 out:
2254 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2255 QLIST_REMOVE(le, entry);
2256 g_free(le);
2259 if (ret == 0) {
2260 ret = qemu_file_get_error(f);
2263 return ret;
2266 static BlockDriverState *find_vmstate_bs(void)
2268 BlockDriverState *bs = NULL;
2269 while ((bs = bdrv_next(bs))) {
2270 if (bdrv_can_snapshot(bs)) {
2271 return bs;
2274 return NULL;
2278 * Deletes snapshots of a given name in all opened images.
2280 static int del_existing_snapshots(Monitor *mon, const char *name)
2282 BlockDriverState *bs;
2283 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2284 int ret;
2286 bs = NULL;
2287 while ((bs = bdrv_next(bs))) {
2288 if (bdrv_can_snapshot(bs) &&
2289 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2291 ret = bdrv_snapshot_delete(bs, name);
2292 if (ret < 0) {
2293 monitor_printf(mon,
2294 "Error while deleting snapshot on '%s'\n",
2295 bdrv_get_device_name(bs));
2296 return -1;
2301 return 0;
2304 void do_savevm(Monitor *mon, const QDict *qdict)
2306 BlockDriverState *bs, *bs1;
2307 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2308 int ret;
2309 QEMUFile *f;
2310 int saved_vm_running;
2311 uint64_t vm_state_size;
2312 qemu_timeval tv;
2313 struct tm tm;
2314 const char *name = qdict_get_try_str(qdict, "name");
2316 /* Verify if there is a device that doesn't support snapshots and is writable */
2317 bs = NULL;
2318 while ((bs = bdrv_next(bs))) {
2320 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2321 continue;
2324 if (!bdrv_can_snapshot(bs)) {
2325 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2326 bdrv_get_device_name(bs));
2327 return;
2331 bs = find_vmstate_bs();
2332 if (!bs) {
2333 monitor_printf(mon, "No block device can accept snapshots\n");
2334 return;
2337 saved_vm_running = runstate_is_running();
2338 vm_stop(RUN_STATE_SAVE_VM);
2340 memset(sn, 0, sizeof(*sn));
2342 /* fill auxiliary fields */
2343 qemu_gettimeofday(&tv);
2344 sn->date_sec = tv.tv_sec;
2345 sn->date_nsec = tv.tv_usec * 1000;
2346 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2348 if (name) {
2349 ret = bdrv_snapshot_find(bs, old_sn, name);
2350 if (ret >= 0) {
2351 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2352 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2353 } else {
2354 pstrcpy(sn->name, sizeof(sn->name), name);
2356 } else {
2357 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2358 localtime_r((const time_t *)&tv.tv_sec, &tm);
2359 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2362 /* Delete old snapshots of the same name */
2363 if (name && del_existing_snapshots(mon, name) < 0) {
2364 goto the_end;
2367 /* save the VM state */
2368 f = qemu_fopen_bdrv(bs, 1);
2369 if (!f) {
2370 monitor_printf(mon, "Could not open VM state file\n");
2371 goto the_end;
2373 ret = qemu_savevm_state(f);
2374 vm_state_size = qemu_ftell(f);
2375 qemu_fclose(f);
2376 if (ret < 0) {
2377 monitor_printf(mon, "Error %d while writing VM\n", ret);
2378 goto the_end;
2381 /* create the snapshots */
2383 bs1 = NULL;
2384 while ((bs1 = bdrv_next(bs1))) {
2385 if (bdrv_can_snapshot(bs1)) {
2386 /* Write VM state size only to the image that contains the state */
2387 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2388 ret = bdrv_snapshot_create(bs1, sn);
2389 if (ret < 0) {
2390 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2391 bdrv_get_device_name(bs1));
2396 the_end:
2397 if (saved_vm_running)
2398 vm_start();
2401 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2403 QEMUFile *f;
2404 int saved_vm_running;
2405 int ret;
2407 saved_vm_running = runstate_is_running();
2408 vm_stop(RUN_STATE_SAVE_VM);
2410 f = qemu_fopen(filename, "wb");
2411 if (!f) {
2412 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2413 goto the_end;
2415 ret = qemu_save_device_state(f);
2416 qemu_fclose(f);
2417 if (ret < 0) {
2418 error_set(errp, QERR_IO_ERROR);
2421 the_end:
2422 if (saved_vm_running)
2423 vm_start();
2426 int load_vmstate(const char *name)
2428 BlockDriverState *bs, *bs_vm_state;
2429 QEMUSnapshotInfo sn;
2430 QEMUFile *f;
2431 int ret;
2433 bs_vm_state = find_vmstate_bs();
2434 if (!bs_vm_state) {
2435 error_report("No block device supports snapshots");
2436 return -ENOTSUP;
2439 /* Don't even try to load empty VM states */
2440 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2441 if (ret < 0) {
2442 return ret;
2443 } else if (sn.vm_state_size == 0) {
2444 error_report("This is a disk-only snapshot. Revert to it offline "
2445 "using qemu-img.");
2446 return -EINVAL;
2449 /* Verify if there is any device that doesn't support snapshots and is
2450 writable and check if the requested snapshot is available too. */
2451 bs = NULL;
2452 while ((bs = bdrv_next(bs))) {
2454 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2455 continue;
2458 if (!bdrv_can_snapshot(bs)) {
2459 error_report("Device '%s' is writable but does not support snapshots.",
2460 bdrv_get_device_name(bs));
2461 return -ENOTSUP;
2464 ret = bdrv_snapshot_find(bs, &sn, name);
2465 if (ret < 0) {
2466 error_report("Device '%s' does not have the requested snapshot '%s'",
2467 bdrv_get_device_name(bs), name);
2468 return ret;
2472 /* Flush all IO requests so they don't interfere with the new state. */
2473 bdrv_drain_all();
2475 bs = NULL;
2476 while ((bs = bdrv_next(bs))) {
2477 if (bdrv_can_snapshot(bs)) {
2478 ret = bdrv_snapshot_goto(bs, name);
2479 if (ret < 0) {
2480 error_report("Error %d while activating snapshot '%s' on '%s'",
2481 ret, name, bdrv_get_device_name(bs));
2482 return ret;
2487 /* restore the VM state */
2488 f = qemu_fopen_bdrv(bs_vm_state, 0);
2489 if (!f) {
2490 error_report("Could not open VM state file");
2491 return -EINVAL;
2494 qemu_system_reset(VMRESET_SILENT);
2495 ret = qemu_loadvm_state(f);
2497 qemu_fclose(f);
2498 if (ret < 0) {
2499 error_report("Error %d while loading VM state", ret);
2500 return ret;
2503 return 0;
2506 void do_delvm(Monitor *mon, const QDict *qdict)
2508 BlockDriverState *bs, *bs1;
2509 int ret;
2510 const char *name = qdict_get_str(qdict, "name");
2512 bs = find_vmstate_bs();
2513 if (!bs) {
2514 monitor_printf(mon, "No block device supports snapshots\n");
2515 return;
2518 bs1 = NULL;
2519 while ((bs1 = bdrv_next(bs1))) {
2520 if (bdrv_can_snapshot(bs1)) {
2521 ret = bdrv_snapshot_delete(bs1, name);
2522 if (ret < 0) {
2523 if (ret == -ENOTSUP)
2524 monitor_printf(mon,
2525 "Snapshots not supported on device '%s'\n",
2526 bdrv_get_device_name(bs1));
2527 else
2528 monitor_printf(mon, "Error %d while deleting snapshot on "
2529 "'%s'\n", ret, bdrv_get_device_name(bs1));
2535 void do_info_snapshots(Monitor *mon, const QDict *qdict)
2537 BlockDriverState *bs, *bs1;
2538 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2539 int nb_sns, i, ret, available;
2540 int total;
2541 int *available_snapshots;
2542 char buf[256];
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 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2586 for (i = 0; i < total; i++) {
2587 sn = &sn_tab[available_snapshots[i]];
2588 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2590 } else {
2591 monitor_printf(mon, "There is no suitable snapshot available\n");
2594 g_free(sn_tab);
2595 g_free(available_snapshots);
2599 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2601 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2602 memory_region_name(mr), dev);
2605 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2607 /* Nothing do to while the implementation is in RAMBlock */
2610 void vmstate_register_ram_global(MemoryRegion *mr)
2612 vmstate_register_ram(mr, NULL);