trace: Add ftrace tracing backend
[qemu/ar7.git] / savevm.c
blob31dcce975ed40827db75df2d8ef7024f022e6f97
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"
44 #define SELF_ANNOUNCE_ROUNDS 5
46 #ifndef ETH_P_RARP
47 #define ETH_P_RARP 0x8035
48 #endif
49 #define ARP_HTYPE_ETH 0x0001
50 #define ARP_PTYPE_IP 0x0800
51 #define ARP_OP_REQUEST_REV 0x3
53 static int announce_self_create(uint8_t *buf,
54 uint8_t *mac_addr)
56 /* Ethernet header. */
57 memset(buf, 0xff, 6); /* destination MAC addr */
58 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
59 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
61 /* RARP header. */
62 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
63 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
64 *(buf + 18) = 6; /* hardware addr length (ethernet) */
65 *(buf + 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
67 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
68 memset(buf + 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
70 memset(buf + 38, 0x00, 4); /* target protocol addr */
72 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf + 42, 0x00, 18);
75 return 60; /* len (FCS will be added by hardware) */
78 static void qemu_announce_self_iter(NICState *nic, void *opaque)
80 uint8_t buf[60];
81 int len;
83 len = announce_self_create(buf, nic->conf->macaddr.a);
85 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
89 static void qemu_announce_self_once(void *opaque)
91 static int count = SELF_ANNOUNCE_ROUNDS;
92 QEMUTimer *timer = *(QEMUTimer **)opaque;
94 qemu_foreach_nic(qemu_announce_self_iter, NULL);
96 if (--count) {
97 /* delay 50ms, 150ms, 250ms, ... */
98 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
99 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
100 } else {
101 qemu_del_timer(timer);
102 qemu_free_timer(timer);
106 void qemu_announce_self(void)
108 static QEMUTimer *timer;
109 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
110 qemu_announce_self_once(&timer);
113 /***********************************************************/
114 /* savevm/loadvm support */
116 #define IO_BUF_SIZE 32768
117 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
119 struct QEMUFile {
120 const QEMUFileOps *ops;
121 void *opaque;
123 int64_t bytes_xfer;
124 int64_t xfer_limit;
126 int64_t pos; /* start of buffer when writing, end of buffer
127 when reading */
128 int buf_index;
129 int buf_size; /* 0 when writing */
130 uint8_t buf[IO_BUF_SIZE];
132 struct iovec iov[MAX_IOV_SIZE];
133 unsigned int iovcnt;
135 int last_error;
138 typedef struct QEMUFileStdio
140 FILE *stdio_file;
141 QEMUFile *file;
142 } QEMUFileStdio;
144 typedef struct QEMUFileSocket
146 int fd;
147 QEMUFile *file;
148 } QEMUFileSocket;
150 typedef struct {
151 Coroutine *co;
152 int fd;
153 } FDYieldUntilData;
155 static void fd_coroutine_enter(void *opaque)
157 FDYieldUntilData *data = opaque;
158 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
159 qemu_coroutine_enter(data->co, NULL);
163 * Yield until a file descriptor becomes readable
165 * Note that this function clobbers the handlers for the file descriptor.
167 static void coroutine_fn yield_until_fd_readable(int fd)
169 FDYieldUntilData data;
171 assert(qemu_in_coroutine());
172 data.co = qemu_coroutine_self();
173 data.fd = fd;
174 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
175 qemu_coroutine_yield();
178 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
179 int64_t pos)
181 QEMUFileSocket *s = opaque;
182 ssize_t len;
183 ssize_t size = iov_size(iov, iovcnt);
185 len = iov_send(s->fd, iov, iovcnt, 0, size);
186 if (len < size) {
187 len = -socket_error();
189 return len;
192 static int socket_get_fd(void *opaque)
194 QEMUFileSocket *s = opaque;
196 return s->fd;
199 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
201 QEMUFileSocket *s = opaque;
202 ssize_t len;
204 for (;;) {
205 len = qemu_recv(s->fd, buf, size, 0);
206 if (len != -1) {
207 break;
209 if (socket_error() == EAGAIN) {
210 yield_until_fd_readable(s->fd);
211 } else if (socket_error() != EINTR) {
212 break;
216 if (len == -1) {
217 len = -socket_error();
219 return len;
222 static int socket_close(void *opaque)
224 QEMUFileSocket *s = opaque;
225 closesocket(s->fd);
226 g_free(s);
227 return 0;
230 static int stdio_get_fd(void *opaque)
232 QEMUFileStdio *s = opaque;
234 return fileno(s->stdio_file);
237 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
239 QEMUFileStdio *s = opaque;
240 return fwrite(buf, 1, size, s->stdio_file);
243 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
245 QEMUFileStdio *s = opaque;
246 FILE *fp = s->stdio_file;
247 int bytes;
249 for (;;) {
250 clearerr(fp);
251 bytes = fread(buf, 1, size, fp);
252 if (bytes != 0 || !ferror(fp)) {
253 break;
255 if (errno == EAGAIN) {
256 yield_until_fd_readable(fileno(fp));
257 } else if (errno != EINTR) {
258 break;
261 return bytes;
264 static int stdio_pclose(void *opaque)
266 QEMUFileStdio *s = opaque;
267 int ret;
268 ret = pclose(s->stdio_file);
269 if (ret == -1) {
270 ret = -errno;
271 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
272 /* close succeeded, but non-zero exit code: */
273 ret = -EIO; /* fake errno value */
275 g_free(s);
276 return ret;
279 static int stdio_fclose(void *opaque)
281 QEMUFileStdio *s = opaque;
282 int ret = 0;
284 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
285 int fd = fileno(s->stdio_file);
286 struct stat st;
288 ret = fstat(fd, &st);
289 if (ret == 0 && S_ISREG(st.st_mode)) {
291 * If the file handle is a regular file make sure the
292 * data is flushed to disk before signaling success.
294 ret = fsync(fd);
295 if (ret != 0) {
296 ret = -errno;
297 return ret;
301 if (fclose(s->stdio_file) == EOF) {
302 ret = -errno;
304 g_free(s);
305 return ret;
308 static const QEMUFileOps stdio_pipe_read_ops = {
309 .get_fd = stdio_get_fd,
310 .get_buffer = stdio_get_buffer,
311 .close = stdio_pclose
314 static const QEMUFileOps stdio_pipe_write_ops = {
315 .get_fd = stdio_get_fd,
316 .put_buffer = stdio_put_buffer,
317 .close = stdio_pclose
320 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
322 FILE *stdio_file;
323 QEMUFileStdio *s;
325 stdio_file = popen(command, mode);
326 if (stdio_file == NULL) {
327 return NULL;
330 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
331 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
332 return NULL;
335 s = g_malloc0(sizeof(QEMUFileStdio));
337 s->stdio_file = stdio_file;
339 if(mode[0] == 'r') {
340 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
341 } else {
342 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
344 return s->file;
347 static const QEMUFileOps stdio_file_read_ops = {
348 .get_fd = stdio_get_fd,
349 .get_buffer = stdio_get_buffer,
350 .close = stdio_fclose
353 static const QEMUFileOps stdio_file_write_ops = {
354 .get_fd = stdio_get_fd,
355 .put_buffer = stdio_put_buffer,
356 .close = stdio_fclose
359 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
360 int64_t pos)
362 QEMUFileSocket *s = opaque;
363 ssize_t len, offset;
364 ssize_t size = iov_size(iov, iovcnt);
365 ssize_t total = 0;
367 assert(iovcnt > 0);
368 offset = 0;
369 while (size > 0) {
370 /* Find the next start position; skip all full-sized vector elements */
371 while (offset >= iov[0].iov_len) {
372 offset -= iov[0].iov_len;
373 iov++, iovcnt--;
376 /* skip `offset' bytes from the (now) first element, undo it on exit */
377 assert(iovcnt > 0);
378 iov[0].iov_base += offset;
379 iov[0].iov_len -= offset;
381 do {
382 len = writev(s->fd, iov, iovcnt);
383 } while (len == -1 && errno == EINTR);
384 if (len == -1) {
385 return -errno;
388 /* Undo the changes above */
389 iov[0].iov_base -= offset;
390 iov[0].iov_len += offset;
392 /* Prepare for the next iteration */
393 offset += len;
394 total += len;
395 size -= len;
398 return total;
401 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
403 QEMUFileSocket *s = opaque;
404 ssize_t len;
406 for (;;) {
407 len = read(s->fd, buf, size);
408 if (len != -1) {
409 break;
411 if (errno == EAGAIN) {
412 yield_until_fd_readable(s->fd);
413 } else if (errno != EINTR) {
414 break;
418 if (len == -1) {
419 len = -errno;
421 return len;
424 static int unix_close(void *opaque)
426 QEMUFileSocket *s = opaque;
427 close(s->fd);
428 g_free(s);
429 return 0;
432 static const QEMUFileOps unix_read_ops = {
433 .get_fd = socket_get_fd,
434 .get_buffer = unix_get_buffer,
435 .close = unix_close
438 static const QEMUFileOps unix_write_ops = {
439 .get_fd = socket_get_fd,
440 .writev_buffer = unix_writev_buffer,
441 .close = unix_close
444 QEMUFile *qemu_fdopen(int fd, const char *mode)
446 QEMUFileSocket *s;
448 if (mode == NULL ||
449 (mode[0] != 'r' && mode[0] != 'w') ||
450 mode[1] != 'b' || mode[2] != 0) {
451 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
452 return NULL;
455 s = g_malloc0(sizeof(QEMUFileSocket));
456 s->fd = fd;
458 if(mode[0] == 'r') {
459 s->file = qemu_fopen_ops(s, &unix_read_ops);
460 } else {
461 s->file = qemu_fopen_ops(s, &unix_write_ops);
463 return s->file;
466 static const QEMUFileOps socket_read_ops = {
467 .get_fd = socket_get_fd,
468 .get_buffer = socket_get_buffer,
469 .close = socket_close
472 static const QEMUFileOps socket_write_ops = {
473 .get_fd = socket_get_fd,
474 .writev_buffer = socket_writev_buffer,
475 .close = socket_close
478 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
480 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
482 if (mode == NULL ||
483 (mode[0] != 'r' && mode[0] != 'w') ||
484 mode[1] != 'b' || mode[2] != 0) {
485 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
486 return NULL;
489 s->fd = fd;
490 if (mode[0] == 'w') {
491 qemu_set_block(s->fd);
492 s->file = qemu_fopen_ops(s, &socket_write_ops);
493 } else {
494 s->file = qemu_fopen_ops(s, &socket_read_ops);
496 return s->file;
499 QEMUFile *qemu_fopen(const char *filename, const char *mode)
501 QEMUFileStdio *s;
503 if (mode == NULL ||
504 (mode[0] != 'r' && mode[0] != 'w') ||
505 mode[1] != 'b' || mode[2] != 0) {
506 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
507 return NULL;
510 s = g_malloc0(sizeof(QEMUFileStdio));
512 s->stdio_file = fopen(filename, mode);
513 if (!s->stdio_file)
514 goto fail;
516 if(mode[0] == 'w') {
517 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
518 } else {
519 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
521 return s->file;
522 fail:
523 g_free(s);
524 return NULL;
527 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
528 int64_t pos)
530 int ret;
531 QEMUIOVector qiov;
533 qemu_iovec_init_external(&qiov, iov, iovcnt);
534 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
535 if (ret < 0) {
536 return ret;
539 return qiov.size;
542 static int block_put_buffer(void *opaque, const uint8_t *buf,
543 int64_t pos, int size)
545 bdrv_save_vmstate(opaque, buf, pos, size);
546 return size;
549 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
551 return bdrv_load_vmstate(opaque, buf, pos, size);
554 static int bdrv_fclose(void *opaque)
556 return bdrv_flush(opaque);
559 static const QEMUFileOps bdrv_read_ops = {
560 .get_buffer = block_get_buffer,
561 .close = bdrv_fclose
564 static const QEMUFileOps bdrv_write_ops = {
565 .put_buffer = block_put_buffer,
566 .writev_buffer = block_writev_buffer,
567 .close = bdrv_fclose
570 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
572 if (is_writable)
573 return qemu_fopen_ops(bs, &bdrv_write_ops);
574 return qemu_fopen_ops(bs, &bdrv_read_ops);
577 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
579 QEMUFile *f;
581 f = g_malloc0(sizeof(QEMUFile));
583 f->opaque = opaque;
584 f->ops = ops;
585 return f;
588 int qemu_file_get_error(QEMUFile *f)
590 return f->last_error;
593 static void qemu_file_set_error(QEMUFile *f, int ret)
595 if (f->last_error == 0) {
596 f->last_error = ret;
600 static inline bool qemu_file_is_writable(QEMUFile *f)
602 return f->ops->writev_buffer || f->ops->put_buffer;
606 * Flushes QEMUFile buffer
608 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
609 * put_buffer ops.
611 static void qemu_fflush(QEMUFile *f)
613 ssize_t ret = 0;
615 if (!qemu_file_is_writable(f)) {
616 return;
619 if (f->ops->writev_buffer) {
620 if (f->iovcnt > 0) {
621 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
623 } else {
624 if (f->buf_index > 0) {
625 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
628 if (ret >= 0) {
629 f->pos += ret;
631 f->buf_index = 0;
632 f->iovcnt = 0;
633 if (ret < 0) {
634 qemu_file_set_error(f, ret);
638 static void qemu_fill_buffer(QEMUFile *f)
640 int len;
641 int pending;
643 assert(!qemu_file_is_writable(f));
645 pending = f->buf_size - f->buf_index;
646 if (pending > 0) {
647 memmove(f->buf, f->buf + f->buf_index, pending);
649 f->buf_index = 0;
650 f->buf_size = pending;
652 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
653 IO_BUF_SIZE - pending);
654 if (len > 0) {
655 f->buf_size += len;
656 f->pos += len;
657 } else if (len == 0) {
658 qemu_file_set_error(f, -EIO);
659 } else if (len != -EAGAIN)
660 qemu_file_set_error(f, len);
663 int qemu_get_fd(QEMUFile *f)
665 if (f->ops->get_fd) {
666 return f->ops->get_fd(f->opaque);
668 return -1;
671 /** Closes the file
673 * Returns negative error value if any error happened on previous operations or
674 * while closing the file. Returns 0 or positive number on success.
676 * The meaning of return value on success depends on the specific backend
677 * being used.
679 int qemu_fclose(QEMUFile *f)
681 int ret;
682 qemu_fflush(f);
683 ret = qemu_file_get_error(f);
685 if (f->ops->close) {
686 int ret2 = f->ops->close(f->opaque);
687 if (ret >= 0) {
688 ret = ret2;
691 /* If any error was spotted before closing, we should report it
692 * instead of the close() return value.
694 if (f->last_error) {
695 ret = f->last_error;
697 g_free(f);
698 return ret;
701 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
703 /* check for adjacent buffer and coalesce them */
704 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
705 f->iov[f->iovcnt - 1].iov_len) {
706 f->iov[f->iovcnt - 1].iov_len += size;
707 } else {
708 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
709 f->iov[f->iovcnt++].iov_len = size;
712 if (f->iovcnt >= MAX_IOV_SIZE) {
713 qemu_fflush(f);
717 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
719 if (!f->ops->writev_buffer) {
720 qemu_put_buffer(f, buf, size);
721 return;
724 if (f->last_error) {
725 return;
728 f->bytes_xfer += size;
729 add_to_iovec(f, buf, size);
732 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
734 int l;
736 if (f->last_error) {
737 return;
740 while (size > 0) {
741 l = IO_BUF_SIZE - f->buf_index;
742 if (l > size)
743 l = size;
744 memcpy(f->buf + f->buf_index, buf, l);
745 f->bytes_xfer += size;
746 if (f->ops->writev_buffer) {
747 add_to_iovec(f, f->buf + f->buf_index, l);
749 f->buf_index += l;
750 if (f->buf_index == IO_BUF_SIZE) {
751 qemu_fflush(f);
753 if (qemu_file_get_error(f)) {
754 break;
756 buf += l;
757 size -= l;
761 void qemu_put_byte(QEMUFile *f, int v)
763 if (f->last_error) {
764 return;
767 f->buf[f->buf_index] = v;
768 f->bytes_xfer++;
769 if (f->ops->writev_buffer) {
770 add_to_iovec(f, f->buf + f->buf_index, 1);
772 f->buf_index++;
773 if (f->buf_index == IO_BUF_SIZE) {
774 qemu_fflush(f);
778 static void qemu_file_skip(QEMUFile *f, int size)
780 if (f->buf_index + size <= f->buf_size) {
781 f->buf_index += size;
785 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
787 int pending;
788 int index;
790 assert(!qemu_file_is_writable(f));
792 index = f->buf_index + offset;
793 pending = f->buf_size - index;
794 if (pending < size) {
795 qemu_fill_buffer(f);
796 index = f->buf_index + offset;
797 pending = f->buf_size - index;
800 if (pending <= 0) {
801 return 0;
803 if (size > pending) {
804 size = pending;
807 memcpy(buf, f->buf + index, size);
808 return size;
811 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
813 int pending = size;
814 int done = 0;
816 while (pending > 0) {
817 int res;
819 res = qemu_peek_buffer(f, buf, pending, 0);
820 if (res == 0) {
821 return done;
823 qemu_file_skip(f, res);
824 buf += res;
825 pending -= res;
826 done += res;
828 return done;
831 static int qemu_peek_byte(QEMUFile *f, int offset)
833 int index = f->buf_index + offset;
835 assert(!qemu_file_is_writable(f));
837 if (index >= f->buf_size) {
838 qemu_fill_buffer(f);
839 index = f->buf_index + offset;
840 if (index >= f->buf_size) {
841 return 0;
844 return f->buf[index];
847 int qemu_get_byte(QEMUFile *f)
849 int result;
851 result = qemu_peek_byte(f, 0);
852 qemu_file_skip(f, 1);
853 return result;
856 int64_t qemu_ftell(QEMUFile *f)
858 qemu_fflush(f);
859 return f->pos;
862 int qemu_file_rate_limit(QEMUFile *f)
864 if (qemu_file_get_error(f)) {
865 return 1;
867 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
868 return 1;
870 return 0;
873 int64_t qemu_file_get_rate_limit(QEMUFile *f)
875 return f->xfer_limit;
878 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
880 f->xfer_limit = limit;
883 void qemu_file_reset_rate_limit(QEMUFile *f)
885 f->bytes_xfer = 0;
888 void qemu_put_be16(QEMUFile *f, unsigned int v)
890 qemu_put_byte(f, v >> 8);
891 qemu_put_byte(f, v);
894 void qemu_put_be32(QEMUFile *f, unsigned int v)
896 qemu_put_byte(f, v >> 24);
897 qemu_put_byte(f, v >> 16);
898 qemu_put_byte(f, v >> 8);
899 qemu_put_byte(f, v);
902 void qemu_put_be64(QEMUFile *f, uint64_t v)
904 qemu_put_be32(f, v >> 32);
905 qemu_put_be32(f, v);
908 unsigned int qemu_get_be16(QEMUFile *f)
910 unsigned int v;
911 v = qemu_get_byte(f) << 8;
912 v |= qemu_get_byte(f);
913 return v;
916 unsigned int qemu_get_be32(QEMUFile *f)
918 unsigned int v;
919 v = qemu_get_byte(f) << 24;
920 v |= qemu_get_byte(f) << 16;
921 v |= qemu_get_byte(f) << 8;
922 v |= qemu_get_byte(f);
923 return v;
926 uint64_t qemu_get_be64(QEMUFile *f)
928 uint64_t v;
929 v = (uint64_t)qemu_get_be32(f) << 32;
930 v |= qemu_get_be32(f);
931 return v;
935 /* timer */
937 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
939 uint64_t expire_time;
941 expire_time = qemu_timer_expire_time_ns(ts);
942 qemu_put_be64(f, expire_time);
945 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
947 uint64_t expire_time;
949 expire_time = qemu_get_be64(f);
950 if (expire_time != -1) {
951 qemu_mod_timer_ns(ts, expire_time);
952 } else {
953 qemu_del_timer(ts);
958 /* bool */
960 static int get_bool(QEMUFile *f, void *pv, size_t size)
962 bool *v = pv;
963 *v = qemu_get_byte(f);
964 return 0;
967 static void put_bool(QEMUFile *f, void *pv, size_t size)
969 bool *v = pv;
970 qemu_put_byte(f, *v);
973 const VMStateInfo vmstate_info_bool = {
974 .name = "bool",
975 .get = get_bool,
976 .put = put_bool,
979 /* 8 bit int */
981 static int get_int8(QEMUFile *f, void *pv, size_t size)
983 int8_t *v = pv;
984 qemu_get_s8s(f, v);
985 return 0;
988 static void put_int8(QEMUFile *f, void *pv, size_t size)
990 int8_t *v = pv;
991 qemu_put_s8s(f, v);
994 const VMStateInfo vmstate_info_int8 = {
995 .name = "int8",
996 .get = get_int8,
997 .put = put_int8,
1000 /* 16 bit int */
1002 static int get_int16(QEMUFile *f, void *pv, size_t size)
1004 int16_t *v = pv;
1005 qemu_get_sbe16s(f, v);
1006 return 0;
1009 static void put_int16(QEMUFile *f, void *pv, size_t size)
1011 int16_t *v = pv;
1012 qemu_put_sbe16s(f, v);
1015 const VMStateInfo vmstate_info_int16 = {
1016 .name = "int16",
1017 .get = get_int16,
1018 .put = put_int16,
1021 /* 32 bit int */
1023 static int get_int32(QEMUFile *f, void *pv, size_t size)
1025 int32_t *v = pv;
1026 qemu_get_sbe32s(f, v);
1027 return 0;
1030 static void put_int32(QEMUFile *f, void *pv, size_t size)
1032 int32_t *v = pv;
1033 qemu_put_sbe32s(f, v);
1036 const VMStateInfo vmstate_info_int32 = {
1037 .name = "int32",
1038 .get = get_int32,
1039 .put = put_int32,
1042 /* 32 bit int. See that the received value is the same than the one
1043 in the field */
1045 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1047 int32_t *v = pv;
1048 int32_t v2;
1049 qemu_get_sbe32s(f, &v2);
1051 if (*v == v2)
1052 return 0;
1053 return -EINVAL;
1056 const VMStateInfo vmstate_info_int32_equal = {
1057 .name = "int32 equal",
1058 .get = get_int32_equal,
1059 .put = put_int32,
1062 /* 32 bit int. See that the received value is the less or the same
1063 than the one in the field */
1065 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1067 int32_t *old = pv;
1068 int32_t new;
1069 qemu_get_sbe32s(f, &new);
1071 if (*old <= new)
1072 return 0;
1073 return -EINVAL;
1076 const VMStateInfo vmstate_info_int32_le = {
1077 .name = "int32 equal",
1078 .get = get_int32_le,
1079 .put = put_int32,
1082 /* 64 bit int */
1084 static int get_int64(QEMUFile *f, void *pv, size_t size)
1086 int64_t *v = pv;
1087 qemu_get_sbe64s(f, v);
1088 return 0;
1091 static void put_int64(QEMUFile *f, void *pv, size_t size)
1093 int64_t *v = pv;
1094 qemu_put_sbe64s(f, v);
1097 const VMStateInfo vmstate_info_int64 = {
1098 .name = "int64",
1099 .get = get_int64,
1100 .put = put_int64,
1103 /* 8 bit unsigned int */
1105 static int get_uint8(QEMUFile *f, void *pv, size_t size)
1107 uint8_t *v = pv;
1108 qemu_get_8s(f, v);
1109 return 0;
1112 static void put_uint8(QEMUFile *f, void *pv, size_t size)
1114 uint8_t *v = pv;
1115 qemu_put_8s(f, v);
1118 const VMStateInfo vmstate_info_uint8 = {
1119 .name = "uint8",
1120 .get = get_uint8,
1121 .put = put_uint8,
1124 /* 16 bit unsigned int */
1126 static int get_uint16(QEMUFile *f, void *pv, size_t size)
1128 uint16_t *v = pv;
1129 qemu_get_be16s(f, v);
1130 return 0;
1133 static void put_uint16(QEMUFile *f, void *pv, size_t size)
1135 uint16_t *v = pv;
1136 qemu_put_be16s(f, v);
1139 const VMStateInfo vmstate_info_uint16 = {
1140 .name = "uint16",
1141 .get = get_uint16,
1142 .put = put_uint16,
1145 /* 32 bit unsigned int */
1147 static int get_uint32(QEMUFile *f, void *pv, size_t size)
1149 uint32_t *v = pv;
1150 qemu_get_be32s(f, v);
1151 return 0;
1154 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1156 uint32_t *v = pv;
1157 qemu_put_be32s(f, v);
1160 const VMStateInfo vmstate_info_uint32 = {
1161 .name = "uint32",
1162 .get = get_uint32,
1163 .put = put_uint32,
1166 /* 32 bit uint. See that the received value is the same than the one
1167 in the field */
1169 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1171 uint32_t *v = pv;
1172 uint32_t v2;
1173 qemu_get_be32s(f, &v2);
1175 if (*v == v2) {
1176 return 0;
1178 return -EINVAL;
1181 const VMStateInfo vmstate_info_uint32_equal = {
1182 .name = "uint32 equal",
1183 .get = get_uint32_equal,
1184 .put = put_uint32,
1187 /* 64 bit unsigned int */
1189 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1191 uint64_t *v = pv;
1192 qemu_get_be64s(f, v);
1193 return 0;
1196 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1198 uint64_t *v = pv;
1199 qemu_put_be64s(f, v);
1202 const VMStateInfo vmstate_info_uint64 = {
1203 .name = "uint64",
1204 .get = get_uint64,
1205 .put = put_uint64,
1208 /* 64 bit unsigned int. See that the received value is the same than the one
1209 in the field */
1211 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1213 uint64_t *v = pv;
1214 uint64_t v2;
1215 qemu_get_be64s(f, &v2);
1217 if (*v == v2) {
1218 return 0;
1220 return -EINVAL;
1223 const VMStateInfo vmstate_info_uint64_equal = {
1224 .name = "int64 equal",
1225 .get = get_uint64_equal,
1226 .put = put_uint64,
1229 /* 8 bit int. See that the received value is the same than the one
1230 in the field */
1232 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1234 uint8_t *v = pv;
1235 uint8_t v2;
1236 qemu_get_8s(f, &v2);
1238 if (*v == v2)
1239 return 0;
1240 return -EINVAL;
1243 const VMStateInfo vmstate_info_uint8_equal = {
1244 .name = "uint8 equal",
1245 .get = get_uint8_equal,
1246 .put = put_uint8,
1249 /* 16 bit unsigned int int. See that the received value is the same than the one
1250 in the field */
1252 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1254 uint16_t *v = pv;
1255 uint16_t v2;
1256 qemu_get_be16s(f, &v2);
1258 if (*v == v2)
1259 return 0;
1260 return -EINVAL;
1263 const VMStateInfo vmstate_info_uint16_equal = {
1264 .name = "uint16 equal",
1265 .get = get_uint16_equal,
1266 .put = put_uint16,
1269 /* floating point */
1271 static int get_float64(QEMUFile *f, void *pv, size_t size)
1273 float64 *v = pv;
1275 *v = make_float64(qemu_get_be64(f));
1276 return 0;
1279 static void put_float64(QEMUFile *f, void *pv, size_t size)
1281 uint64_t *v = pv;
1283 qemu_put_be64(f, float64_val(*v));
1286 const VMStateInfo vmstate_info_float64 = {
1287 .name = "float64",
1288 .get = get_float64,
1289 .put = put_float64,
1292 /* timers */
1294 static int get_timer(QEMUFile *f, void *pv, size_t size)
1296 QEMUTimer *v = pv;
1297 qemu_get_timer(f, v);
1298 return 0;
1301 static void put_timer(QEMUFile *f, void *pv, size_t size)
1303 QEMUTimer *v = pv;
1304 qemu_put_timer(f, v);
1307 const VMStateInfo vmstate_info_timer = {
1308 .name = "timer",
1309 .get = get_timer,
1310 .put = put_timer,
1313 /* uint8_t buffers */
1315 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1317 uint8_t *v = pv;
1318 qemu_get_buffer(f, v, size);
1319 return 0;
1322 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1324 uint8_t *v = pv;
1325 qemu_put_buffer(f, v, size);
1328 const VMStateInfo vmstate_info_buffer = {
1329 .name = "buffer",
1330 .get = get_buffer,
1331 .put = put_buffer,
1334 /* unused buffers: space that was used for some fields that are
1335 not useful anymore */
1337 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1339 uint8_t buf[1024];
1340 int block_len;
1342 while (size > 0) {
1343 block_len = MIN(sizeof(buf), size);
1344 size -= block_len;
1345 qemu_get_buffer(f, buf, block_len);
1347 return 0;
1350 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1352 static const uint8_t buf[1024];
1353 int block_len;
1355 while (size > 0) {
1356 block_len = MIN(sizeof(buf), size);
1357 size -= block_len;
1358 qemu_put_buffer(f, buf, block_len);
1362 const VMStateInfo vmstate_info_unused_buffer = {
1363 .name = "unused_buffer",
1364 .get = get_unused_buffer,
1365 .put = put_unused_buffer,
1368 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1369 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1370 * bit words with the bits in big endian order. The in-memory format
1371 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1373 /* This is the number of 64 bit words sent over the wire */
1374 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1375 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1377 unsigned long *bmp = pv;
1378 int i, idx = 0;
1379 for (i = 0; i < BITS_TO_U64S(size); i++) {
1380 uint64_t w = qemu_get_be64(f);
1381 bmp[idx++] = w;
1382 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1383 bmp[idx++] = w >> 32;
1386 return 0;
1389 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1391 unsigned long *bmp = pv;
1392 int i, idx = 0;
1393 for (i = 0; i < BITS_TO_U64S(size); i++) {
1394 uint64_t w = bmp[idx++];
1395 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1396 w |= ((uint64_t)bmp[idx++]) << 32;
1398 qemu_put_be64(f, w);
1402 const VMStateInfo vmstate_info_bitmap = {
1403 .name = "bitmap",
1404 .get = get_bitmap,
1405 .put = put_bitmap,
1408 typedef struct CompatEntry {
1409 char idstr[256];
1410 int instance_id;
1411 } CompatEntry;
1413 typedef struct SaveStateEntry {
1414 QTAILQ_ENTRY(SaveStateEntry) entry;
1415 char idstr[256];
1416 int instance_id;
1417 int alias_id;
1418 int version_id;
1419 int section_id;
1420 SaveVMHandlers *ops;
1421 const VMStateDescription *vmsd;
1422 void *opaque;
1423 CompatEntry *compat;
1424 int no_migrate;
1425 int is_ram;
1426 } SaveStateEntry;
1429 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1430 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1431 static int global_section_id;
1433 static int calculate_new_instance_id(const char *idstr)
1435 SaveStateEntry *se;
1436 int instance_id = 0;
1438 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1439 if (strcmp(idstr, se->idstr) == 0
1440 && instance_id <= se->instance_id) {
1441 instance_id = se->instance_id + 1;
1444 return instance_id;
1447 static int calculate_compat_instance_id(const char *idstr)
1449 SaveStateEntry *se;
1450 int instance_id = 0;
1452 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1453 if (!se->compat)
1454 continue;
1456 if (strcmp(idstr, se->compat->idstr) == 0
1457 && instance_id <= se->compat->instance_id) {
1458 instance_id = se->compat->instance_id + 1;
1461 return instance_id;
1464 /* TODO: Individual devices generally have very little idea about the rest
1465 of the system, so instance_id should be removed/replaced.
1466 Meanwhile pass -1 as instance_id if you do not already have a clearly
1467 distinguishing id for all instances of your device class. */
1468 int register_savevm_live(DeviceState *dev,
1469 const char *idstr,
1470 int instance_id,
1471 int version_id,
1472 SaveVMHandlers *ops,
1473 void *opaque)
1475 SaveStateEntry *se;
1477 se = g_malloc0(sizeof(SaveStateEntry));
1478 se->version_id = version_id;
1479 se->section_id = global_section_id++;
1480 se->ops = ops;
1481 se->opaque = opaque;
1482 se->vmsd = NULL;
1483 se->no_migrate = 0;
1484 /* if this is a live_savem then set is_ram */
1485 if (ops->save_live_setup != NULL) {
1486 se->is_ram = 1;
1489 if (dev) {
1490 char *id = qdev_get_dev_path(dev);
1491 if (id) {
1492 pstrcpy(se->idstr, sizeof(se->idstr), id);
1493 pstrcat(se->idstr, sizeof(se->idstr), "/");
1494 g_free(id);
1496 se->compat = g_malloc0(sizeof(CompatEntry));
1497 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1498 se->compat->instance_id = instance_id == -1 ?
1499 calculate_compat_instance_id(idstr) : instance_id;
1500 instance_id = -1;
1503 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1505 if (instance_id == -1) {
1506 se->instance_id = calculate_new_instance_id(se->idstr);
1507 } else {
1508 se->instance_id = instance_id;
1510 assert(!se->compat || se->instance_id == 0);
1511 /* add at the end of list */
1512 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1513 return 0;
1516 int register_savevm(DeviceState *dev,
1517 const char *idstr,
1518 int instance_id,
1519 int version_id,
1520 SaveStateHandler *save_state,
1521 LoadStateHandler *load_state,
1522 void *opaque)
1524 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1525 ops->save_state = save_state;
1526 ops->load_state = load_state;
1527 return register_savevm_live(dev, idstr, instance_id, version_id,
1528 ops, opaque);
1531 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1533 SaveStateEntry *se, *new_se;
1534 char id[256] = "";
1536 if (dev) {
1537 char *path = qdev_get_dev_path(dev);
1538 if (path) {
1539 pstrcpy(id, sizeof(id), path);
1540 pstrcat(id, sizeof(id), "/");
1541 g_free(path);
1544 pstrcat(id, sizeof(id), idstr);
1546 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1547 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1548 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1549 if (se->compat) {
1550 g_free(se->compat);
1552 g_free(se->ops);
1553 g_free(se);
1558 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1559 const VMStateDescription *vmsd,
1560 void *opaque, int alias_id,
1561 int required_for_version)
1563 SaveStateEntry *se;
1565 /* If this triggers, alias support can be dropped for the vmsd. */
1566 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1568 se = g_malloc0(sizeof(SaveStateEntry));
1569 se->version_id = vmsd->version_id;
1570 se->section_id = global_section_id++;
1571 se->opaque = opaque;
1572 se->vmsd = vmsd;
1573 se->alias_id = alias_id;
1574 se->no_migrate = vmsd->unmigratable;
1576 if (dev) {
1577 char *id = qdev_get_dev_path(dev);
1578 if (id) {
1579 pstrcpy(se->idstr, sizeof(se->idstr), id);
1580 pstrcat(se->idstr, sizeof(se->idstr), "/");
1581 g_free(id);
1583 se->compat = g_malloc0(sizeof(CompatEntry));
1584 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1585 se->compat->instance_id = instance_id == -1 ?
1586 calculate_compat_instance_id(vmsd->name) : instance_id;
1587 instance_id = -1;
1590 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1592 if (instance_id == -1) {
1593 se->instance_id = calculate_new_instance_id(se->idstr);
1594 } else {
1595 se->instance_id = instance_id;
1597 assert(!se->compat || se->instance_id == 0);
1598 /* add at the end of list */
1599 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1600 return 0;
1603 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1604 void *opaque)
1606 SaveStateEntry *se, *new_se;
1608 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1609 if (se->vmsd == vmsd && se->opaque == opaque) {
1610 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1611 if (se->compat) {
1612 g_free(se->compat);
1614 g_free(se);
1619 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1620 void *opaque);
1621 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1622 void *opaque);
1624 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1625 void *opaque, int version_id)
1627 VMStateField *field = vmsd->fields;
1628 int ret;
1630 if (version_id > vmsd->version_id) {
1631 return -EINVAL;
1633 if (version_id < vmsd->minimum_version_id_old) {
1634 return -EINVAL;
1636 if (version_id < vmsd->minimum_version_id) {
1637 return vmsd->load_state_old(f, opaque, version_id);
1639 if (vmsd->pre_load) {
1640 int ret = vmsd->pre_load(opaque);
1641 if (ret)
1642 return ret;
1644 while(field->name) {
1645 if ((field->field_exists &&
1646 field->field_exists(opaque, version_id)) ||
1647 (!field->field_exists &&
1648 field->version_id <= version_id)) {
1649 void *base_addr = opaque + field->offset;
1650 int i, n_elems = 1;
1651 int size = field->size;
1653 if (field->flags & VMS_VBUFFER) {
1654 size = *(int32_t *)(opaque+field->size_offset);
1655 if (field->flags & VMS_MULTIPLY) {
1656 size *= field->size;
1659 if (field->flags & VMS_ARRAY) {
1660 n_elems = field->num;
1661 } else if (field->flags & VMS_VARRAY_INT32) {
1662 n_elems = *(int32_t *)(opaque+field->num_offset);
1663 } else if (field->flags & VMS_VARRAY_UINT32) {
1664 n_elems = *(uint32_t *)(opaque+field->num_offset);
1665 } else if (field->flags & VMS_VARRAY_UINT16) {
1666 n_elems = *(uint16_t *)(opaque+field->num_offset);
1667 } else if (field->flags & VMS_VARRAY_UINT8) {
1668 n_elems = *(uint8_t *)(opaque+field->num_offset);
1670 if (field->flags & VMS_POINTER) {
1671 base_addr = *(void **)base_addr + field->start;
1673 for (i = 0; i < n_elems; i++) {
1674 void *addr = base_addr + size * i;
1676 if (field->flags & VMS_ARRAY_OF_POINTER) {
1677 addr = *(void **)addr;
1679 if (field->flags & VMS_STRUCT) {
1680 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1681 } else {
1682 ret = field->info->get(f, addr, size);
1685 if (ret < 0) {
1686 return ret;
1690 field++;
1692 ret = vmstate_subsection_load(f, vmsd, opaque);
1693 if (ret != 0) {
1694 return ret;
1696 if (vmsd->post_load) {
1697 return vmsd->post_load(opaque, version_id);
1699 return 0;
1702 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1703 void *opaque)
1705 VMStateField *field = vmsd->fields;
1707 if (vmsd->pre_save) {
1708 vmsd->pre_save(opaque);
1710 while(field->name) {
1711 if (!field->field_exists ||
1712 field->field_exists(opaque, vmsd->version_id)) {
1713 void *base_addr = opaque + field->offset;
1714 int i, n_elems = 1;
1715 int size = field->size;
1717 if (field->flags & VMS_VBUFFER) {
1718 size = *(int32_t *)(opaque+field->size_offset);
1719 if (field->flags & VMS_MULTIPLY) {
1720 size *= field->size;
1723 if (field->flags & VMS_ARRAY) {
1724 n_elems = field->num;
1725 } else if (field->flags & VMS_VARRAY_INT32) {
1726 n_elems = *(int32_t *)(opaque+field->num_offset);
1727 } else if (field->flags & VMS_VARRAY_UINT32) {
1728 n_elems = *(uint32_t *)(opaque+field->num_offset);
1729 } else if (field->flags & VMS_VARRAY_UINT16) {
1730 n_elems = *(uint16_t *)(opaque+field->num_offset);
1731 } else if (field->flags & VMS_VARRAY_UINT8) {
1732 n_elems = *(uint8_t *)(opaque+field->num_offset);
1734 if (field->flags & VMS_POINTER) {
1735 base_addr = *(void **)base_addr + field->start;
1737 for (i = 0; i < n_elems; i++) {
1738 void *addr = base_addr + size * i;
1740 if (field->flags & VMS_ARRAY_OF_POINTER) {
1741 addr = *(void **)addr;
1743 if (field->flags & VMS_STRUCT) {
1744 vmstate_save_state(f, field->vmsd, addr);
1745 } else {
1746 field->info->put(f, addr, size);
1750 field++;
1752 vmstate_subsection_save(f, vmsd, opaque);
1755 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1757 if (!se->vmsd) { /* Old style */
1758 return se->ops->load_state(f, se->opaque, version_id);
1760 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1763 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1765 if (!se->vmsd) { /* Old style */
1766 se->ops->save_state(f, se->opaque);
1767 return;
1769 vmstate_save_state(f,se->vmsd, se->opaque);
1772 #define QEMU_VM_FILE_MAGIC 0x5145564d
1773 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1774 #define QEMU_VM_FILE_VERSION 0x00000003
1776 #define QEMU_VM_EOF 0x00
1777 #define QEMU_VM_SECTION_START 0x01
1778 #define QEMU_VM_SECTION_PART 0x02
1779 #define QEMU_VM_SECTION_END 0x03
1780 #define QEMU_VM_SECTION_FULL 0x04
1781 #define QEMU_VM_SUBSECTION 0x05
1783 bool qemu_savevm_state_blocked(Error **errp)
1785 SaveStateEntry *se;
1787 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1788 if (se->no_migrate) {
1789 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1790 return true;
1793 return false;
1796 void qemu_savevm_state_begin(QEMUFile *f,
1797 const MigrationParams *params)
1799 SaveStateEntry *se;
1800 int ret;
1802 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1803 if (!se->ops || !se->ops->set_params) {
1804 continue;
1806 se->ops->set_params(params, se->opaque);
1809 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1810 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1812 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1813 int len;
1815 if (!se->ops || !se->ops->save_live_setup) {
1816 continue;
1818 if (se->ops && se->ops->is_active) {
1819 if (!se->ops->is_active(se->opaque)) {
1820 continue;
1823 /* Section type */
1824 qemu_put_byte(f, QEMU_VM_SECTION_START);
1825 qemu_put_be32(f, se->section_id);
1827 /* ID string */
1828 len = strlen(se->idstr);
1829 qemu_put_byte(f, len);
1830 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1832 qemu_put_be32(f, se->instance_id);
1833 qemu_put_be32(f, se->version_id);
1835 ret = se->ops->save_live_setup(f, se->opaque);
1836 if (ret < 0) {
1837 qemu_file_set_error(f, ret);
1838 break;
1844 * this function has three return values:
1845 * negative: there was one error, and we have -errno.
1846 * 0 : We haven't finished, caller have to go again
1847 * 1 : We have finished, we can go to complete phase
1849 int qemu_savevm_state_iterate(QEMUFile *f)
1851 SaveStateEntry *se;
1852 int ret = 1;
1854 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1855 if (!se->ops || !se->ops->save_live_iterate) {
1856 continue;
1858 if (se->ops && se->ops->is_active) {
1859 if (!se->ops->is_active(se->opaque)) {
1860 continue;
1863 if (qemu_file_rate_limit(f)) {
1864 return 0;
1866 trace_savevm_section_start();
1867 /* Section type */
1868 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1869 qemu_put_be32(f, se->section_id);
1871 ret = se->ops->save_live_iterate(f, se->opaque);
1872 trace_savevm_section_end(se->section_id);
1874 if (ret < 0) {
1875 qemu_file_set_error(f, ret);
1877 if (ret <= 0) {
1878 /* Do not proceed to the next vmstate before this one reported
1879 completion of the current stage. This serializes the migration
1880 and reduces the probability that a faster changing state is
1881 synchronized over and over again. */
1882 break;
1885 return ret;
1888 void qemu_savevm_state_complete(QEMUFile *f)
1890 SaveStateEntry *se;
1891 int ret;
1893 cpu_synchronize_all_states();
1895 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1896 if (!se->ops || !se->ops->save_live_complete) {
1897 continue;
1899 if (se->ops && se->ops->is_active) {
1900 if (!se->ops->is_active(se->opaque)) {
1901 continue;
1904 trace_savevm_section_start();
1905 /* Section type */
1906 qemu_put_byte(f, QEMU_VM_SECTION_END);
1907 qemu_put_be32(f, se->section_id);
1909 ret = se->ops->save_live_complete(f, se->opaque);
1910 trace_savevm_section_end(se->section_id);
1911 if (ret < 0) {
1912 qemu_file_set_error(f, ret);
1913 return;
1917 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1918 int len;
1920 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1921 continue;
1923 trace_savevm_section_start();
1924 /* Section type */
1925 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1926 qemu_put_be32(f, se->section_id);
1928 /* ID string */
1929 len = strlen(se->idstr);
1930 qemu_put_byte(f, len);
1931 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1933 qemu_put_be32(f, se->instance_id);
1934 qemu_put_be32(f, se->version_id);
1936 vmstate_save(f, se);
1937 trace_savevm_section_end(se->section_id);
1940 qemu_put_byte(f, QEMU_VM_EOF);
1941 qemu_fflush(f);
1944 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1946 SaveStateEntry *se;
1947 uint64_t ret = 0;
1949 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1950 if (!se->ops || !se->ops->save_live_pending) {
1951 continue;
1953 if (se->ops && se->ops->is_active) {
1954 if (!se->ops->is_active(se->opaque)) {
1955 continue;
1958 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1960 return ret;
1963 void qemu_savevm_state_cancel(void)
1965 SaveStateEntry *se;
1967 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1968 if (se->ops && se->ops->cancel) {
1969 se->ops->cancel(se->opaque);
1974 static int qemu_savevm_state(QEMUFile *f)
1976 int ret;
1977 MigrationParams params = {
1978 .blk = 0,
1979 .shared = 0
1982 if (qemu_savevm_state_blocked(NULL)) {
1983 return -EINVAL;
1986 qemu_mutex_unlock_iothread();
1987 qemu_savevm_state_begin(f, &params);
1988 qemu_mutex_lock_iothread();
1990 while (qemu_file_get_error(f) == 0) {
1991 if (qemu_savevm_state_iterate(f) > 0) {
1992 break;
1996 ret = qemu_file_get_error(f);
1997 if (ret == 0) {
1998 qemu_savevm_state_complete(f);
1999 ret = qemu_file_get_error(f);
2001 if (ret != 0) {
2002 qemu_savevm_state_cancel();
2004 return ret;
2007 static int qemu_save_device_state(QEMUFile *f)
2009 SaveStateEntry *se;
2011 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2012 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2014 cpu_synchronize_all_states();
2016 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2017 int len;
2019 if (se->is_ram) {
2020 continue;
2022 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
2023 continue;
2026 /* Section type */
2027 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2028 qemu_put_be32(f, se->section_id);
2030 /* ID string */
2031 len = strlen(se->idstr);
2032 qemu_put_byte(f, len);
2033 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2035 qemu_put_be32(f, se->instance_id);
2036 qemu_put_be32(f, se->version_id);
2038 vmstate_save(f, se);
2041 qemu_put_byte(f, QEMU_VM_EOF);
2043 return qemu_file_get_error(f);
2046 static SaveStateEntry *find_se(const char *idstr, int instance_id)
2048 SaveStateEntry *se;
2050 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2051 if (!strcmp(se->idstr, idstr) &&
2052 (instance_id == se->instance_id ||
2053 instance_id == se->alias_id))
2054 return se;
2055 /* Migrating from an older version? */
2056 if (strstr(se->idstr, idstr) && se->compat) {
2057 if (!strcmp(se->compat->idstr, idstr) &&
2058 (instance_id == se->compat->instance_id ||
2059 instance_id == se->alias_id))
2060 return se;
2063 return NULL;
2066 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2068 while(sub && sub->needed) {
2069 if (strcmp(idstr, sub->vmsd->name) == 0) {
2070 return sub->vmsd;
2072 sub++;
2074 return NULL;
2077 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2078 void *opaque)
2080 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
2081 char idstr[256];
2082 int ret;
2083 uint8_t version_id, len, size;
2084 const VMStateDescription *sub_vmsd;
2086 len = qemu_peek_byte(f, 1);
2087 if (len < strlen(vmsd->name) + 1) {
2088 /* subsection name has be be "section_name/a" */
2089 return 0;
2091 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2092 if (size != len) {
2093 return 0;
2095 idstr[size] = 0;
2097 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2098 /* it don't have a valid subsection name */
2099 return 0;
2101 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
2102 if (sub_vmsd == NULL) {
2103 return -ENOENT;
2105 qemu_file_skip(f, 1); /* subsection */
2106 qemu_file_skip(f, 1); /* len */
2107 qemu_file_skip(f, len); /* idstr */
2108 version_id = qemu_get_be32(f);
2110 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2111 if (ret) {
2112 return ret;
2115 return 0;
2118 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2119 void *opaque)
2121 const VMStateSubsection *sub = vmsd->subsections;
2123 while (sub && sub->needed) {
2124 if (sub->needed(opaque)) {
2125 const VMStateDescription *vmsd = sub->vmsd;
2126 uint8_t len;
2128 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2129 len = strlen(vmsd->name);
2130 qemu_put_byte(f, len);
2131 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2132 qemu_put_be32(f, vmsd->version_id);
2133 vmstate_save_state(f, vmsd, opaque);
2135 sub++;
2139 typedef struct LoadStateEntry {
2140 QLIST_ENTRY(LoadStateEntry) entry;
2141 SaveStateEntry *se;
2142 int section_id;
2143 int version_id;
2144 } LoadStateEntry;
2146 int qemu_loadvm_state(QEMUFile *f)
2148 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2149 QLIST_HEAD_INITIALIZER(loadvm_handlers);
2150 LoadStateEntry *le, *new_le;
2151 uint8_t section_type;
2152 unsigned int v;
2153 int ret;
2155 if (qemu_savevm_state_blocked(NULL)) {
2156 return -EINVAL;
2159 v = qemu_get_be32(f);
2160 if (v != QEMU_VM_FILE_MAGIC)
2161 return -EINVAL;
2163 v = qemu_get_be32(f);
2164 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2165 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2166 return -ENOTSUP;
2168 if (v != QEMU_VM_FILE_VERSION)
2169 return -ENOTSUP;
2171 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2172 uint32_t instance_id, version_id, section_id;
2173 SaveStateEntry *se;
2174 char idstr[257];
2175 int len;
2177 switch (section_type) {
2178 case QEMU_VM_SECTION_START:
2179 case QEMU_VM_SECTION_FULL:
2180 /* Read section start */
2181 section_id = qemu_get_be32(f);
2182 len = qemu_get_byte(f);
2183 qemu_get_buffer(f, (uint8_t *)idstr, len);
2184 idstr[len] = 0;
2185 instance_id = qemu_get_be32(f);
2186 version_id = qemu_get_be32(f);
2188 /* Find savevm section */
2189 se = find_se(idstr, instance_id);
2190 if (se == NULL) {
2191 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2192 ret = -EINVAL;
2193 goto out;
2196 /* Validate version */
2197 if (version_id > se->version_id) {
2198 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2199 version_id, idstr, se->version_id);
2200 ret = -EINVAL;
2201 goto out;
2204 /* Add entry */
2205 le = g_malloc0(sizeof(*le));
2207 le->se = se;
2208 le->section_id = section_id;
2209 le->version_id = version_id;
2210 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2212 ret = vmstate_load(f, le->se, le->version_id);
2213 if (ret < 0) {
2214 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2215 instance_id, idstr);
2216 goto out;
2218 break;
2219 case QEMU_VM_SECTION_PART:
2220 case QEMU_VM_SECTION_END:
2221 section_id = qemu_get_be32(f);
2223 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2224 if (le->section_id == section_id) {
2225 break;
2228 if (le == NULL) {
2229 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2230 ret = -EINVAL;
2231 goto out;
2234 ret = vmstate_load(f, le->se, le->version_id);
2235 if (ret < 0) {
2236 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2237 section_id);
2238 goto out;
2240 break;
2241 default:
2242 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2243 ret = -EINVAL;
2244 goto out;
2248 cpu_synchronize_all_post_init();
2250 ret = 0;
2252 out:
2253 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2254 QLIST_REMOVE(le, entry);
2255 g_free(le);
2258 if (ret == 0) {
2259 ret = qemu_file_get_error(f);
2262 return ret;
2265 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2266 const char *name)
2268 QEMUSnapshotInfo *sn_tab, *sn;
2269 int nb_sns, i, ret;
2271 ret = -ENOENT;
2272 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2273 if (nb_sns < 0)
2274 return ret;
2275 for(i = 0; i < nb_sns; i++) {
2276 sn = &sn_tab[i];
2277 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2278 *sn_info = *sn;
2279 ret = 0;
2280 break;
2283 g_free(sn_tab);
2284 return ret;
2288 * Deletes snapshots of a given name in all opened images.
2290 static int del_existing_snapshots(Monitor *mon, const char *name)
2292 BlockDriverState *bs;
2293 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2294 int ret;
2296 bs = NULL;
2297 while ((bs = bdrv_next(bs))) {
2298 if (bdrv_can_snapshot(bs) &&
2299 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2301 ret = bdrv_snapshot_delete(bs, name);
2302 if (ret < 0) {
2303 monitor_printf(mon,
2304 "Error while deleting snapshot on '%s'\n",
2305 bdrv_get_device_name(bs));
2306 return -1;
2311 return 0;
2314 void do_savevm(Monitor *mon, const QDict *qdict)
2316 BlockDriverState *bs, *bs1;
2317 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2318 int ret;
2319 QEMUFile *f;
2320 int saved_vm_running;
2321 uint64_t vm_state_size;
2322 qemu_timeval tv;
2323 struct tm tm;
2324 const char *name = qdict_get_try_str(qdict, "name");
2326 /* Verify if there is a device that doesn't support snapshots and is writable */
2327 bs = NULL;
2328 while ((bs = bdrv_next(bs))) {
2330 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2331 continue;
2334 if (!bdrv_can_snapshot(bs)) {
2335 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2336 bdrv_get_device_name(bs));
2337 return;
2341 bs = bdrv_snapshots();
2342 if (!bs) {
2343 monitor_printf(mon, "No block device can accept snapshots\n");
2344 return;
2347 saved_vm_running = runstate_is_running();
2348 vm_stop(RUN_STATE_SAVE_VM);
2350 memset(sn, 0, sizeof(*sn));
2352 /* fill auxiliary fields */
2353 qemu_gettimeofday(&tv);
2354 sn->date_sec = tv.tv_sec;
2355 sn->date_nsec = tv.tv_usec * 1000;
2356 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2358 if (name) {
2359 ret = bdrv_snapshot_find(bs, old_sn, name);
2360 if (ret >= 0) {
2361 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2362 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2363 } else {
2364 pstrcpy(sn->name, sizeof(sn->name), name);
2366 } else {
2367 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2368 localtime_r((const time_t *)&tv.tv_sec, &tm);
2369 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2372 /* Delete old snapshots of the same name */
2373 if (name && del_existing_snapshots(mon, name) < 0) {
2374 goto the_end;
2377 /* save the VM state */
2378 f = qemu_fopen_bdrv(bs, 1);
2379 if (!f) {
2380 monitor_printf(mon, "Could not open VM state file\n");
2381 goto the_end;
2383 ret = qemu_savevm_state(f);
2384 vm_state_size = qemu_ftell(f);
2385 qemu_fclose(f);
2386 if (ret < 0) {
2387 monitor_printf(mon, "Error %d while writing VM\n", ret);
2388 goto the_end;
2391 /* create the snapshots */
2393 bs1 = NULL;
2394 while ((bs1 = bdrv_next(bs1))) {
2395 if (bdrv_can_snapshot(bs1)) {
2396 /* Write VM state size only to the image that contains the state */
2397 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2398 ret = bdrv_snapshot_create(bs1, sn);
2399 if (ret < 0) {
2400 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2401 bdrv_get_device_name(bs1));
2406 the_end:
2407 if (saved_vm_running)
2408 vm_start();
2411 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2413 QEMUFile *f;
2414 int saved_vm_running;
2415 int ret;
2417 saved_vm_running = runstate_is_running();
2418 vm_stop(RUN_STATE_SAVE_VM);
2420 f = qemu_fopen(filename, "wb");
2421 if (!f) {
2422 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2423 goto the_end;
2425 ret = qemu_save_device_state(f);
2426 qemu_fclose(f);
2427 if (ret < 0) {
2428 error_set(errp, QERR_IO_ERROR);
2431 the_end:
2432 if (saved_vm_running)
2433 vm_start();
2436 int load_vmstate(const char *name)
2438 BlockDriverState *bs, *bs_vm_state;
2439 QEMUSnapshotInfo sn;
2440 QEMUFile *f;
2441 int ret;
2443 bs_vm_state = bdrv_snapshots();
2444 if (!bs_vm_state) {
2445 error_report("No block device supports snapshots");
2446 return -ENOTSUP;
2449 /* Don't even try to load empty VM states */
2450 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2451 if (ret < 0) {
2452 return ret;
2453 } else if (sn.vm_state_size == 0) {
2454 error_report("This is a disk-only snapshot. Revert to it offline "
2455 "using qemu-img.");
2456 return -EINVAL;
2459 /* Verify if there is any device that doesn't support snapshots and is
2460 writable and check if the requested snapshot is available too. */
2461 bs = NULL;
2462 while ((bs = bdrv_next(bs))) {
2464 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2465 continue;
2468 if (!bdrv_can_snapshot(bs)) {
2469 error_report("Device '%s' is writable but does not support snapshots.",
2470 bdrv_get_device_name(bs));
2471 return -ENOTSUP;
2474 ret = bdrv_snapshot_find(bs, &sn, name);
2475 if (ret < 0) {
2476 error_report("Device '%s' does not have the requested snapshot '%s'",
2477 bdrv_get_device_name(bs), name);
2478 return ret;
2482 /* Flush all IO requests so they don't interfere with the new state. */
2483 bdrv_drain_all();
2485 bs = NULL;
2486 while ((bs = bdrv_next(bs))) {
2487 if (bdrv_can_snapshot(bs)) {
2488 ret = bdrv_snapshot_goto(bs, name);
2489 if (ret < 0) {
2490 error_report("Error %d while activating snapshot '%s' on '%s'",
2491 ret, name, bdrv_get_device_name(bs));
2492 return ret;
2497 /* restore the VM state */
2498 f = qemu_fopen_bdrv(bs_vm_state, 0);
2499 if (!f) {
2500 error_report("Could not open VM state file");
2501 return -EINVAL;
2504 qemu_system_reset(VMRESET_SILENT);
2505 ret = qemu_loadvm_state(f);
2507 qemu_fclose(f);
2508 if (ret < 0) {
2509 error_report("Error %d while loading VM state", ret);
2510 return ret;
2513 return 0;
2516 void do_delvm(Monitor *mon, const QDict *qdict)
2518 BlockDriverState *bs, *bs1;
2519 int ret;
2520 const char *name = qdict_get_str(qdict, "name");
2522 bs = bdrv_snapshots();
2523 if (!bs) {
2524 monitor_printf(mon, "No block device supports snapshots\n");
2525 return;
2528 bs1 = NULL;
2529 while ((bs1 = bdrv_next(bs1))) {
2530 if (bdrv_can_snapshot(bs1)) {
2531 ret = bdrv_snapshot_delete(bs1, name);
2532 if (ret < 0) {
2533 if (ret == -ENOTSUP)
2534 monitor_printf(mon,
2535 "Snapshots not supported on device '%s'\n",
2536 bdrv_get_device_name(bs1));
2537 else
2538 monitor_printf(mon, "Error %d while deleting snapshot on "
2539 "'%s'\n", ret, bdrv_get_device_name(bs1));
2545 void do_info_snapshots(Monitor *mon, const QDict *qdict)
2547 BlockDriverState *bs, *bs1;
2548 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2549 int nb_sns, i, ret, available;
2550 int total;
2551 int *available_snapshots;
2552 char buf[256];
2554 bs = bdrv_snapshots();
2555 if (!bs) {
2556 monitor_printf(mon, "No available block device supports snapshots\n");
2557 return;
2560 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2561 if (nb_sns < 0) {
2562 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2563 return;
2566 if (nb_sns == 0) {
2567 monitor_printf(mon, "There is no snapshot available.\n");
2568 return;
2571 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2572 total = 0;
2573 for (i = 0; i < nb_sns; i++) {
2574 sn = &sn_tab[i];
2575 available = 1;
2576 bs1 = NULL;
2578 while ((bs1 = bdrv_next(bs1))) {
2579 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2580 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2581 if (ret < 0) {
2582 available = 0;
2583 break;
2588 if (available) {
2589 available_snapshots[total] = i;
2590 total++;
2594 if (total > 0) {
2595 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2596 for (i = 0; i < total; i++) {
2597 sn = &sn_tab[available_snapshots[i]];
2598 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2600 } else {
2601 monitor_printf(mon, "There is no suitable snapshot available\n");
2604 g_free(sn_tab);
2605 g_free(available_snapshots);
2609 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2611 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2612 memory_region_name(mr), dev);
2615 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2617 /* Nothing do to while the implementation is in RAMBlock */
2620 void vmstate_register_ram_global(MemoryRegion *mr)
2622 vmstate_register_ram(mr, NULL);