hw/i386/pc_sysfw: support two flash drives
[qemu/armbru.git] / savevm.c
blob3f912ddcf9919ca9060a6fb385d9ee398139f24a
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "config-host.h"
26 #include "qemu-common.h"
27 #include "hw/hw.h"
28 #include "hw/qdev.h"
29 #include "net/net.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
40 #include "trace.h"
41 #include "qemu/bitops.h"
42 #include "qemu/iov.h"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
46 #define SELF_ANNOUNCE_ROUNDS 5
48 #ifndef ETH_P_RARP
49 #define ETH_P_RARP 0x8035
50 #endif
51 #define ARP_HTYPE_ETH 0x0001
52 #define ARP_PTYPE_IP 0x0800
53 #define ARP_OP_REQUEST_REV 0x3
55 static int announce_self_create(uint8_t *buf,
56 uint8_t *mac_addr)
58 /* Ethernet header. */
59 memset(buf, 0xff, 6); /* destination MAC addr */
60 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
61 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
63 /* RARP header. */
64 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
65 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
66 *(buf + 18) = 6; /* hardware addr length (ethernet) */
67 *(buf + 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
69 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
70 memset(buf + 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
72 memset(buf + 38, 0x00, 4); /* target protocol addr */
74 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf + 42, 0x00, 18);
77 return 60; /* len (FCS will be added by hardware) */
80 static void qemu_announce_self_iter(NICState *nic, void *opaque)
82 uint8_t buf[60];
83 int len;
85 len = announce_self_create(buf, nic->conf->macaddr.a);
87 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
91 static void qemu_announce_self_once(void *opaque)
93 static int count = SELF_ANNOUNCE_ROUNDS;
94 QEMUTimer *timer = *(QEMUTimer **)opaque;
96 qemu_foreach_nic(qemu_announce_self_iter, NULL);
98 if (--count) {
99 /* delay 50ms, 150ms, 250ms, ... */
100 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
101 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
102 } else {
103 timer_del(timer);
104 timer_free(timer);
108 void qemu_announce_self(void)
110 static QEMUTimer *timer;
111 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
112 qemu_announce_self_once(&timer);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 #define IO_BUF_SIZE 32768
119 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
121 struct QEMUFile {
122 const QEMUFileOps *ops;
123 void *opaque;
125 int64_t bytes_xfer;
126 int64_t xfer_limit;
128 int64_t pos; /* start of buffer when writing, end of buffer
129 when reading */
130 int buf_index;
131 int buf_size; /* 0 when writing */
132 uint8_t buf[IO_BUF_SIZE];
134 struct iovec iov[MAX_IOV_SIZE];
135 unsigned int iovcnt;
137 int last_error;
140 typedef struct QEMUFileStdio
142 FILE *stdio_file;
143 QEMUFile *file;
144 } QEMUFileStdio;
146 typedef struct QEMUFileSocket
148 int fd;
149 QEMUFile *file;
150 } QEMUFileSocket;
152 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
153 int64_t pos)
155 QEMUFileSocket *s = opaque;
156 ssize_t len;
157 ssize_t size = iov_size(iov, iovcnt);
159 len = iov_send(s->fd, iov, iovcnt, 0, size);
160 if (len < size) {
161 len = -socket_error();
163 return len;
166 static int socket_get_fd(void *opaque)
168 QEMUFileSocket *s = opaque;
170 return s->fd;
173 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
175 QEMUFileSocket *s = opaque;
176 ssize_t len;
178 for (;;) {
179 len = qemu_recv(s->fd, buf, size, 0);
180 if (len != -1) {
181 break;
183 if (socket_error() == EAGAIN) {
184 yield_until_fd_readable(s->fd);
185 } else if (socket_error() != EINTR) {
186 break;
190 if (len == -1) {
191 len = -socket_error();
193 return len;
196 static int socket_close(void *opaque)
198 QEMUFileSocket *s = opaque;
199 closesocket(s->fd);
200 g_free(s);
201 return 0;
204 static int stdio_get_fd(void *opaque)
206 QEMUFileStdio *s = opaque;
208 return fileno(s->stdio_file);
211 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
213 QEMUFileStdio *s = opaque;
214 return fwrite(buf, 1, size, s->stdio_file);
217 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
219 QEMUFileStdio *s = opaque;
220 FILE *fp = s->stdio_file;
221 int bytes;
223 for (;;) {
224 clearerr(fp);
225 bytes = fread(buf, 1, size, fp);
226 if (bytes != 0 || !ferror(fp)) {
227 break;
229 if (errno == EAGAIN) {
230 yield_until_fd_readable(fileno(fp));
231 } else if (errno != EINTR) {
232 break;
235 return bytes;
238 static int stdio_pclose(void *opaque)
240 QEMUFileStdio *s = opaque;
241 int ret;
242 ret = pclose(s->stdio_file);
243 if (ret == -1) {
244 ret = -errno;
245 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
246 /* close succeeded, but non-zero exit code: */
247 ret = -EIO; /* fake errno value */
249 g_free(s);
250 return ret;
253 static int stdio_fclose(void *opaque)
255 QEMUFileStdio *s = opaque;
256 int ret = 0;
258 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
259 int fd = fileno(s->stdio_file);
260 struct stat st;
262 ret = fstat(fd, &st);
263 if (ret == 0 && S_ISREG(st.st_mode)) {
265 * If the file handle is a regular file make sure the
266 * data is flushed to disk before signaling success.
268 ret = fsync(fd);
269 if (ret != 0) {
270 ret = -errno;
271 return ret;
275 if (fclose(s->stdio_file) == EOF) {
276 ret = -errno;
278 g_free(s);
279 return ret;
282 static const QEMUFileOps stdio_pipe_read_ops = {
283 .get_fd = stdio_get_fd,
284 .get_buffer = stdio_get_buffer,
285 .close = stdio_pclose
288 static const QEMUFileOps stdio_pipe_write_ops = {
289 .get_fd = stdio_get_fd,
290 .put_buffer = stdio_put_buffer,
291 .close = stdio_pclose
294 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
296 FILE *stdio_file;
297 QEMUFileStdio *s;
299 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
300 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
301 return NULL;
304 stdio_file = popen(command, mode);
305 if (stdio_file == NULL) {
306 return NULL;
309 s = g_malloc0(sizeof(QEMUFileStdio));
311 s->stdio_file = stdio_file;
313 if(mode[0] == 'r') {
314 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
315 } else {
316 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
318 return s->file;
321 static const QEMUFileOps stdio_file_read_ops = {
322 .get_fd = stdio_get_fd,
323 .get_buffer = stdio_get_buffer,
324 .close = stdio_fclose
327 static const QEMUFileOps stdio_file_write_ops = {
328 .get_fd = stdio_get_fd,
329 .put_buffer = stdio_put_buffer,
330 .close = stdio_fclose
333 static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
334 int64_t pos)
336 QEMUFileSocket *s = opaque;
337 ssize_t len, offset;
338 ssize_t size = iov_size(iov, iovcnt);
339 ssize_t total = 0;
341 assert(iovcnt > 0);
342 offset = 0;
343 while (size > 0) {
344 /* Find the next start position; skip all full-sized vector elements */
345 while (offset >= iov[0].iov_len) {
346 offset -= iov[0].iov_len;
347 iov++, iovcnt--;
350 /* skip `offset' bytes from the (now) first element, undo it on exit */
351 assert(iovcnt > 0);
352 iov[0].iov_base += offset;
353 iov[0].iov_len -= offset;
355 do {
356 len = writev(s->fd, iov, iovcnt);
357 } while (len == -1 && errno == EINTR);
358 if (len == -1) {
359 return -errno;
362 /* Undo the changes above */
363 iov[0].iov_base -= offset;
364 iov[0].iov_len += offset;
366 /* Prepare for the next iteration */
367 offset += len;
368 total += len;
369 size -= len;
372 return total;
375 static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
377 QEMUFileSocket *s = opaque;
378 ssize_t len;
380 for (;;) {
381 len = read(s->fd, buf, size);
382 if (len != -1) {
383 break;
385 if (errno == EAGAIN) {
386 yield_until_fd_readable(s->fd);
387 } else if (errno != EINTR) {
388 break;
392 if (len == -1) {
393 len = -errno;
395 return len;
398 static int unix_close(void *opaque)
400 QEMUFileSocket *s = opaque;
401 close(s->fd);
402 g_free(s);
403 return 0;
406 static const QEMUFileOps unix_read_ops = {
407 .get_fd = socket_get_fd,
408 .get_buffer = unix_get_buffer,
409 .close = unix_close
412 static const QEMUFileOps unix_write_ops = {
413 .get_fd = socket_get_fd,
414 .writev_buffer = unix_writev_buffer,
415 .close = unix_close
418 QEMUFile *qemu_fdopen(int fd, const char *mode)
420 QEMUFileSocket *s;
422 if (mode == NULL ||
423 (mode[0] != 'r' && mode[0] != 'w') ||
424 mode[1] != 'b' || mode[2] != 0) {
425 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
426 return NULL;
429 s = g_malloc0(sizeof(QEMUFileSocket));
430 s->fd = fd;
432 if(mode[0] == 'r') {
433 s->file = qemu_fopen_ops(s, &unix_read_ops);
434 } else {
435 s->file = qemu_fopen_ops(s, &unix_write_ops);
437 return s->file;
440 static const QEMUFileOps socket_read_ops = {
441 .get_fd = socket_get_fd,
442 .get_buffer = socket_get_buffer,
443 .close = socket_close
446 static const QEMUFileOps socket_write_ops = {
447 .get_fd = socket_get_fd,
448 .writev_buffer = socket_writev_buffer,
449 .close = socket_close
452 bool qemu_file_mode_is_not_valid(const char *mode)
454 if (mode == NULL ||
455 (mode[0] != 'r' && mode[0] != 'w') ||
456 mode[1] != 'b' || mode[2] != 0) {
457 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
458 return true;
461 return false;
464 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
466 QEMUFileSocket *s;
468 if (qemu_file_mode_is_not_valid(mode)) {
469 return NULL;
472 s = g_malloc0(sizeof(QEMUFileSocket));
473 s->fd = fd;
474 if (mode[0] == 'w') {
475 qemu_set_block(s->fd);
476 s->file = qemu_fopen_ops(s, &socket_write_ops);
477 } else {
478 s->file = qemu_fopen_ops(s, &socket_read_ops);
480 return s->file;
483 QEMUFile *qemu_fopen(const char *filename, const char *mode)
485 QEMUFileStdio *s;
487 if (qemu_file_mode_is_not_valid(mode)) {
488 return NULL;
491 s = g_malloc0(sizeof(QEMUFileStdio));
493 s->stdio_file = fopen(filename, mode);
494 if (!s->stdio_file)
495 goto fail;
497 if(mode[0] == 'w') {
498 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
499 } else {
500 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
502 return s->file;
503 fail:
504 g_free(s);
505 return NULL;
508 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
509 int64_t pos)
511 int ret;
512 QEMUIOVector qiov;
514 qemu_iovec_init_external(&qiov, iov, iovcnt);
515 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
516 if (ret < 0) {
517 return ret;
520 return qiov.size;
523 static int block_put_buffer(void *opaque, const uint8_t *buf,
524 int64_t pos, int size)
526 bdrv_save_vmstate(opaque, buf, pos, size);
527 return size;
530 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
532 return bdrv_load_vmstate(opaque, buf, pos, size);
535 static int bdrv_fclose(void *opaque)
537 return bdrv_flush(opaque);
540 static const QEMUFileOps bdrv_read_ops = {
541 .get_buffer = block_get_buffer,
542 .close = bdrv_fclose
545 static const QEMUFileOps bdrv_write_ops = {
546 .put_buffer = block_put_buffer,
547 .writev_buffer = block_writev_buffer,
548 .close = bdrv_fclose
551 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
553 if (is_writable)
554 return qemu_fopen_ops(bs, &bdrv_write_ops);
555 return qemu_fopen_ops(bs, &bdrv_read_ops);
558 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
560 QEMUFile *f;
562 f = g_malloc0(sizeof(QEMUFile));
564 f->opaque = opaque;
565 f->ops = ops;
566 return f;
570 * Get last error for stream f
572 * Return negative error value if there has been an error on previous
573 * operations, return 0 if no error happened.
576 int qemu_file_get_error(QEMUFile *f)
578 return f->last_error;
581 static void qemu_file_set_error(QEMUFile *f, int ret)
583 if (f->last_error == 0) {
584 f->last_error = ret;
588 static inline bool qemu_file_is_writable(QEMUFile *f)
590 return f->ops->writev_buffer || f->ops->put_buffer;
594 * Flushes QEMUFile buffer
596 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
597 * put_buffer ops.
599 void qemu_fflush(QEMUFile *f)
601 ssize_t ret = 0;
603 if (!qemu_file_is_writable(f)) {
604 return;
607 if (f->ops->writev_buffer) {
608 if (f->iovcnt > 0) {
609 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
611 } else {
612 if (f->buf_index > 0) {
613 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
616 if (ret >= 0) {
617 f->pos += ret;
619 f->buf_index = 0;
620 f->iovcnt = 0;
621 if (ret < 0) {
622 qemu_file_set_error(f, ret);
626 void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
628 int ret = 0;
630 if (f->ops->before_ram_iterate) {
631 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
632 if (ret < 0) {
633 qemu_file_set_error(f, ret);
638 void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
640 int ret = 0;
642 if (f->ops->after_ram_iterate) {
643 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
644 if (ret < 0) {
645 qemu_file_set_error(f, ret);
650 void ram_control_load_hook(QEMUFile *f, uint64_t flags)
652 int ret = -EINVAL;
654 if (f->ops->hook_ram_load) {
655 ret = f->ops->hook_ram_load(f, f->opaque, flags);
656 if (ret < 0) {
657 qemu_file_set_error(f, ret);
659 } else {
660 qemu_file_set_error(f, ret);
664 size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
665 ram_addr_t offset, size_t size, int *bytes_sent)
667 if (f->ops->save_page) {
668 int ret = f->ops->save_page(f, f->opaque, block_offset,
669 offset, size, bytes_sent);
671 if (ret != RAM_SAVE_CONTROL_DELAYED) {
672 if (bytes_sent && *bytes_sent > 0) {
673 qemu_update_position(f, *bytes_sent);
674 } else if (ret < 0) {
675 qemu_file_set_error(f, ret);
679 return ret;
682 return RAM_SAVE_CONTROL_NOT_SUPP;
685 static void qemu_fill_buffer(QEMUFile *f)
687 int len;
688 int pending;
690 assert(!qemu_file_is_writable(f));
692 pending = f->buf_size - f->buf_index;
693 if (pending > 0) {
694 memmove(f->buf, f->buf + f->buf_index, pending);
696 f->buf_index = 0;
697 f->buf_size = pending;
699 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
700 IO_BUF_SIZE - pending);
701 if (len > 0) {
702 f->buf_size += len;
703 f->pos += len;
704 } else if (len == 0) {
705 qemu_file_set_error(f, -EIO);
706 } else if (len != -EAGAIN)
707 qemu_file_set_error(f, len);
710 int qemu_get_fd(QEMUFile *f)
712 if (f->ops->get_fd) {
713 return f->ops->get_fd(f->opaque);
715 return -1;
718 void qemu_update_position(QEMUFile *f, size_t size)
720 f->pos += size;
723 /** Closes the file
725 * Returns negative error value if any error happened on previous operations or
726 * while closing the file. Returns 0 or positive number on success.
728 * The meaning of return value on success depends on the specific backend
729 * being used.
731 int qemu_fclose(QEMUFile *f)
733 int ret;
734 qemu_fflush(f);
735 ret = qemu_file_get_error(f);
737 if (f->ops->close) {
738 int ret2 = f->ops->close(f->opaque);
739 if (ret >= 0) {
740 ret = ret2;
743 /* If any error was spotted before closing, we should report it
744 * instead of the close() return value.
746 if (f->last_error) {
747 ret = f->last_error;
749 g_free(f);
750 return ret;
753 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
755 /* check for adjacent buffer and coalesce them */
756 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
757 f->iov[f->iovcnt - 1].iov_len) {
758 f->iov[f->iovcnt - 1].iov_len += size;
759 } else {
760 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
761 f->iov[f->iovcnt++].iov_len = size;
764 if (f->iovcnt >= MAX_IOV_SIZE) {
765 qemu_fflush(f);
769 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
771 if (!f->ops->writev_buffer) {
772 qemu_put_buffer(f, buf, size);
773 return;
776 if (f->last_error) {
777 return;
780 f->bytes_xfer += size;
781 add_to_iovec(f, buf, size);
784 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
786 int l;
788 if (f->last_error) {
789 return;
792 while (size > 0) {
793 l = IO_BUF_SIZE - f->buf_index;
794 if (l > size)
795 l = size;
796 memcpy(f->buf + f->buf_index, buf, l);
797 f->bytes_xfer += l;
798 if (f->ops->writev_buffer) {
799 add_to_iovec(f, f->buf + f->buf_index, l);
801 f->buf_index += l;
802 if (f->buf_index == IO_BUF_SIZE) {
803 qemu_fflush(f);
805 if (qemu_file_get_error(f)) {
806 break;
808 buf += l;
809 size -= l;
813 void qemu_put_byte(QEMUFile *f, int v)
815 if (f->last_error) {
816 return;
819 f->buf[f->buf_index] = v;
820 f->bytes_xfer++;
821 if (f->ops->writev_buffer) {
822 add_to_iovec(f, f->buf + f->buf_index, 1);
824 f->buf_index++;
825 if (f->buf_index == IO_BUF_SIZE) {
826 qemu_fflush(f);
830 static void qemu_file_skip(QEMUFile *f, int size)
832 if (f->buf_index + size <= f->buf_size) {
833 f->buf_index += size;
837 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
839 int pending;
840 int index;
842 assert(!qemu_file_is_writable(f));
844 index = f->buf_index + offset;
845 pending = f->buf_size - index;
846 if (pending < size) {
847 qemu_fill_buffer(f);
848 index = f->buf_index + offset;
849 pending = f->buf_size - index;
852 if (pending <= 0) {
853 return 0;
855 if (size > pending) {
856 size = pending;
859 memcpy(buf, f->buf + index, size);
860 return size;
863 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
865 int pending = size;
866 int done = 0;
868 while (pending > 0) {
869 int res;
871 res = qemu_peek_buffer(f, buf, pending, 0);
872 if (res == 0) {
873 return done;
875 qemu_file_skip(f, res);
876 buf += res;
877 pending -= res;
878 done += res;
880 return done;
883 static int qemu_peek_byte(QEMUFile *f, int offset)
885 int index = f->buf_index + offset;
887 assert(!qemu_file_is_writable(f));
889 if (index >= f->buf_size) {
890 qemu_fill_buffer(f);
891 index = f->buf_index + offset;
892 if (index >= f->buf_size) {
893 return 0;
896 return f->buf[index];
899 int qemu_get_byte(QEMUFile *f)
901 int result;
903 result = qemu_peek_byte(f, 0);
904 qemu_file_skip(f, 1);
905 return result;
908 int64_t qemu_ftell(QEMUFile *f)
910 qemu_fflush(f);
911 return f->pos;
914 int qemu_file_rate_limit(QEMUFile *f)
916 if (qemu_file_get_error(f)) {
917 return 1;
919 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
920 return 1;
922 return 0;
925 int64_t qemu_file_get_rate_limit(QEMUFile *f)
927 return f->xfer_limit;
930 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
932 f->xfer_limit = limit;
935 void qemu_file_reset_rate_limit(QEMUFile *f)
937 f->bytes_xfer = 0;
940 void qemu_put_be16(QEMUFile *f, unsigned int v)
942 qemu_put_byte(f, v >> 8);
943 qemu_put_byte(f, v);
946 void qemu_put_be32(QEMUFile *f, unsigned int v)
948 qemu_put_byte(f, v >> 24);
949 qemu_put_byte(f, v >> 16);
950 qemu_put_byte(f, v >> 8);
951 qemu_put_byte(f, v);
954 void qemu_put_be64(QEMUFile *f, uint64_t v)
956 qemu_put_be32(f, v >> 32);
957 qemu_put_be32(f, v);
960 unsigned int qemu_get_be16(QEMUFile *f)
962 unsigned int v;
963 v = qemu_get_byte(f) << 8;
964 v |= qemu_get_byte(f);
965 return v;
968 unsigned int qemu_get_be32(QEMUFile *f)
970 unsigned int v;
971 v = qemu_get_byte(f) << 24;
972 v |= qemu_get_byte(f) << 16;
973 v |= qemu_get_byte(f) << 8;
974 v |= qemu_get_byte(f);
975 return v;
978 uint64_t qemu_get_be64(QEMUFile *f)
980 uint64_t v;
981 v = (uint64_t)qemu_get_be32(f) << 32;
982 v |= qemu_get_be32(f);
983 return v;
987 /* timer */
989 void timer_put(QEMUFile *f, QEMUTimer *ts)
991 uint64_t expire_time;
993 expire_time = timer_expire_time_ns(ts);
994 qemu_put_be64(f, expire_time);
997 void timer_get(QEMUFile *f, QEMUTimer *ts)
999 uint64_t expire_time;
1001 expire_time = qemu_get_be64(f);
1002 if (expire_time != -1) {
1003 timer_mod_ns(ts, expire_time);
1004 } else {
1005 timer_del(ts);
1010 /* bool */
1012 static int get_bool(QEMUFile *f, void *pv, size_t size)
1014 bool *v = pv;
1015 *v = qemu_get_byte(f);
1016 return 0;
1019 static void put_bool(QEMUFile *f, void *pv, size_t size)
1021 bool *v = pv;
1022 qemu_put_byte(f, *v);
1025 const VMStateInfo vmstate_info_bool = {
1026 .name = "bool",
1027 .get = get_bool,
1028 .put = put_bool,
1031 /* 8 bit int */
1033 static int get_int8(QEMUFile *f, void *pv, size_t size)
1035 int8_t *v = pv;
1036 qemu_get_s8s(f, v);
1037 return 0;
1040 static void put_int8(QEMUFile *f, void *pv, size_t size)
1042 int8_t *v = pv;
1043 qemu_put_s8s(f, v);
1046 const VMStateInfo vmstate_info_int8 = {
1047 .name = "int8",
1048 .get = get_int8,
1049 .put = put_int8,
1052 /* 16 bit int */
1054 static int get_int16(QEMUFile *f, void *pv, size_t size)
1056 int16_t *v = pv;
1057 qemu_get_sbe16s(f, v);
1058 return 0;
1061 static void put_int16(QEMUFile *f, void *pv, size_t size)
1063 int16_t *v = pv;
1064 qemu_put_sbe16s(f, v);
1067 const VMStateInfo vmstate_info_int16 = {
1068 .name = "int16",
1069 .get = get_int16,
1070 .put = put_int16,
1073 /* 32 bit int */
1075 static int get_int32(QEMUFile *f, void *pv, size_t size)
1077 int32_t *v = pv;
1078 qemu_get_sbe32s(f, v);
1079 return 0;
1082 static void put_int32(QEMUFile *f, void *pv, size_t size)
1084 int32_t *v = pv;
1085 qemu_put_sbe32s(f, v);
1088 const VMStateInfo vmstate_info_int32 = {
1089 .name = "int32",
1090 .get = get_int32,
1091 .put = put_int32,
1094 /* 32 bit int. See that the received value is the same than the one
1095 in the field */
1097 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1099 int32_t *v = pv;
1100 int32_t v2;
1101 qemu_get_sbe32s(f, &v2);
1103 if (*v == v2)
1104 return 0;
1105 return -EINVAL;
1108 const VMStateInfo vmstate_info_int32_equal = {
1109 .name = "int32 equal",
1110 .get = get_int32_equal,
1111 .put = put_int32,
1114 /* 32 bit int. See that the received value is the less or the same
1115 than the one in the field */
1117 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1119 int32_t *old = pv;
1120 int32_t new;
1121 qemu_get_sbe32s(f, &new);
1123 if (*old <= new)
1124 return 0;
1125 return -EINVAL;
1128 const VMStateInfo vmstate_info_int32_le = {
1129 .name = "int32 equal",
1130 .get = get_int32_le,
1131 .put = put_int32,
1134 /* 64 bit int */
1136 static int get_int64(QEMUFile *f, void *pv, size_t size)
1138 int64_t *v = pv;
1139 qemu_get_sbe64s(f, v);
1140 return 0;
1143 static void put_int64(QEMUFile *f, void *pv, size_t size)
1145 int64_t *v = pv;
1146 qemu_put_sbe64s(f, v);
1149 const VMStateInfo vmstate_info_int64 = {
1150 .name = "int64",
1151 .get = get_int64,
1152 .put = put_int64,
1155 /* 8 bit unsigned int */
1157 static int get_uint8(QEMUFile *f, void *pv, size_t size)
1159 uint8_t *v = pv;
1160 qemu_get_8s(f, v);
1161 return 0;
1164 static void put_uint8(QEMUFile *f, void *pv, size_t size)
1166 uint8_t *v = pv;
1167 qemu_put_8s(f, v);
1170 const VMStateInfo vmstate_info_uint8 = {
1171 .name = "uint8",
1172 .get = get_uint8,
1173 .put = put_uint8,
1176 /* 16 bit unsigned int */
1178 static int get_uint16(QEMUFile *f, void *pv, size_t size)
1180 uint16_t *v = pv;
1181 qemu_get_be16s(f, v);
1182 return 0;
1185 static void put_uint16(QEMUFile *f, void *pv, size_t size)
1187 uint16_t *v = pv;
1188 qemu_put_be16s(f, v);
1191 const VMStateInfo vmstate_info_uint16 = {
1192 .name = "uint16",
1193 .get = get_uint16,
1194 .put = put_uint16,
1197 /* 32 bit unsigned int */
1199 static int get_uint32(QEMUFile *f, void *pv, size_t size)
1201 uint32_t *v = pv;
1202 qemu_get_be32s(f, v);
1203 return 0;
1206 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1208 uint32_t *v = pv;
1209 qemu_put_be32s(f, v);
1212 const VMStateInfo vmstate_info_uint32 = {
1213 .name = "uint32",
1214 .get = get_uint32,
1215 .put = put_uint32,
1218 /* 32 bit uint. See that the received value is the same than the one
1219 in the field */
1221 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1223 uint32_t *v = pv;
1224 uint32_t v2;
1225 qemu_get_be32s(f, &v2);
1227 if (*v == v2) {
1228 return 0;
1230 return -EINVAL;
1233 const VMStateInfo vmstate_info_uint32_equal = {
1234 .name = "uint32 equal",
1235 .get = get_uint32_equal,
1236 .put = put_uint32,
1239 /* 64 bit unsigned int */
1241 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1243 uint64_t *v = pv;
1244 qemu_get_be64s(f, v);
1245 return 0;
1248 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1250 uint64_t *v = pv;
1251 qemu_put_be64s(f, v);
1254 const VMStateInfo vmstate_info_uint64 = {
1255 .name = "uint64",
1256 .get = get_uint64,
1257 .put = put_uint64,
1260 /* 64 bit unsigned int. See that the received value is the same than the one
1261 in the field */
1263 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1265 uint64_t *v = pv;
1266 uint64_t v2;
1267 qemu_get_be64s(f, &v2);
1269 if (*v == v2) {
1270 return 0;
1272 return -EINVAL;
1275 const VMStateInfo vmstate_info_uint64_equal = {
1276 .name = "int64 equal",
1277 .get = get_uint64_equal,
1278 .put = put_uint64,
1281 /* 8 bit int. See that the received value is the same than the one
1282 in the field */
1284 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1286 uint8_t *v = pv;
1287 uint8_t v2;
1288 qemu_get_8s(f, &v2);
1290 if (*v == v2)
1291 return 0;
1292 return -EINVAL;
1295 const VMStateInfo vmstate_info_uint8_equal = {
1296 .name = "uint8 equal",
1297 .get = get_uint8_equal,
1298 .put = put_uint8,
1301 /* 16 bit unsigned int int. See that the received value is the same than the one
1302 in the field */
1304 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1306 uint16_t *v = pv;
1307 uint16_t v2;
1308 qemu_get_be16s(f, &v2);
1310 if (*v == v2)
1311 return 0;
1312 return -EINVAL;
1315 const VMStateInfo vmstate_info_uint16_equal = {
1316 .name = "uint16 equal",
1317 .get = get_uint16_equal,
1318 .put = put_uint16,
1321 /* floating point */
1323 static int get_float64(QEMUFile *f, void *pv, size_t size)
1325 float64 *v = pv;
1327 *v = make_float64(qemu_get_be64(f));
1328 return 0;
1331 static void put_float64(QEMUFile *f, void *pv, size_t size)
1333 uint64_t *v = pv;
1335 qemu_put_be64(f, float64_val(*v));
1338 const VMStateInfo vmstate_info_float64 = {
1339 .name = "float64",
1340 .get = get_float64,
1341 .put = put_float64,
1344 /* timers */
1346 static int get_timer(QEMUFile *f, void *pv, size_t size)
1348 QEMUTimer *v = pv;
1349 timer_get(f, v);
1350 return 0;
1353 static void put_timer(QEMUFile *f, void *pv, size_t size)
1355 QEMUTimer *v = pv;
1356 timer_put(f, v);
1359 const VMStateInfo vmstate_info_timer = {
1360 .name = "timer",
1361 .get = get_timer,
1362 .put = put_timer,
1365 /* uint8_t buffers */
1367 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1369 uint8_t *v = pv;
1370 qemu_get_buffer(f, v, size);
1371 return 0;
1374 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1376 uint8_t *v = pv;
1377 qemu_put_buffer(f, v, size);
1380 const VMStateInfo vmstate_info_buffer = {
1381 .name = "buffer",
1382 .get = get_buffer,
1383 .put = put_buffer,
1386 /* unused buffers: space that was used for some fields that are
1387 not useful anymore */
1389 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1391 uint8_t buf[1024];
1392 int block_len;
1394 while (size > 0) {
1395 block_len = MIN(sizeof(buf), size);
1396 size -= block_len;
1397 qemu_get_buffer(f, buf, block_len);
1399 return 0;
1402 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1404 static const uint8_t buf[1024];
1405 int block_len;
1407 while (size > 0) {
1408 block_len = MIN(sizeof(buf), size);
1409 size -= block_len;
1410 qemu_put_buffer(f, buf, block_len);
1414 const VMStateInfo vmstate_info_unused_buffer = {
1415 .name = "unused_buffer",
1416 .get = get_unused_buffer,
1417 .put = put_unused_buffer,
1420 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1421 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1422 * bit words with the bits in big endian order. The in-memory format
1423 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1425 /* This is the number of 64 bit words sent over the wire */
1426 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1427 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1429 unsigned long *bmp = pv;
1430 int i, idx = 0;
1431 for (i = 0; i < BITS_TO_U64S(size); i++) {
1432 uint64_t w = qemu_get_be64(f);
1433 bmp[idx++] = w;
1434 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1435 bmp[idx++] = w >> 32;
1438 return 0;
1441 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1443 unsigned long *bmp = pv;
1444 int i, idx = 0;
1445 for (i = 0; i < BITS_TO_U64S(size); i++) {
1446 uint64_t w = bmp[idx++];
1447 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1448 w |= ((uint64_t)bmp[idx++]) << 32;
1450 qemu_put_be64(f, w);
1454 const VMStateInfo vmstate_info_bitmap = {
1455 .name = "bitmap",
1456 .get = get_bitmap,
1457 .put = put_bitmap,
1460 typedef struct CompatEntry {
1461 char idstr[256];
1462 int instance_id;
1463 } CompatEntry;
1465 typedef struct SaveStateEntry {
1466 QTAILQ_ENTRY(SaveStateEntry) entry;
1467 char idstr[256];
1468 int instance_id;
1469 int alias_id;
1470 int version_id;
1471 int section_id;
1472 SaveVMHandlers *ops;
1473 const VMStateDescription *vmsd;
1474 void *opaque;
1475 CompatEntry *compat;
1476 int no_migrate;
1477 int is_ram;
1478 } SaveStateEntry;
1481 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1482 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1483 static int global_section_id;
1485 static int calculate_new_instance_id(const char *idstr)
1487 SaveStateEntry *se;
1488 int instance_id = 0;
1490 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1491 if (strcmp(idstr, se->idstr) == 0
1492 && instance_id <= se->instance_id) {
1493 instance_id = se->instance_id + 1;
1496 return instance_id;
1499 static int calculate_compat_instance_id(const char *idstr)
1501 SaveStateEntry *se;
1502 int instance_id = 0;
1504 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1505 if (!se->compat)
1506 continue;
1508 if (strcmp(idstr, se->compat->idstr) == 0
1509 && instance_id <= se->compat->instance_id) {
1510 instance_id = se->compat->instance_id + 1;
1513 return instance_id;
1516 /* TODO: Individual devices generally have very little idea about the rest
1517 of the system, so instance_id should be removed/replaced.
1518 Meanwhile pass -1 as instance_id if you do not already have a clearly
1519 distinguishing id for all instances of your device class. */
1520 int register_savevm_live(DeviceState *dev,
1521 const char *idstr,
1522 int instance_id,
1523 int version_id,
1524 SaveVMHandlers *ops,
1525 void *opaque)
1527 SaveStateEntry *se;
1529 se = g_malloc0(sizeof(SaveStateEntry));
1530 se->version_id = version_id;
1531 se->section_id = global_section_id++;
1532 se->ops = ops;
1533 se->opaque = opaque;
1534 se->vmsd = NULL;
1535 se->no_migrate = 0;
1536 /* if this is a live_savem then set is_ram */
1537 if (ops->save_live_setup != NULL) {
1538 se->is_ram = 1;
1541 if (dev) {
1542 char *id = qdev_get_dev_path(dev);
1543 if (id) {
1544 pstrcpy(se->idstr, sizeof(se->idstr), id);
1545 pstrcat(se->idstr, sizeof(se->idstr), "/");
1546 g_free(id);
1548 se->compat = g_malloc0(sizeof(CompatEntry));
1549 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1550 se->compat->instance_id = instance_id == -1 ?
1551 calculate_compat_instance_id(idstr) : instance_id;
1552 instance_id = -1;
1555 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1557 if (instance_id == -1) {
1558 se->instance_id = calculate_new_instance_id(se->idstr);
1559 } else {
1560 se->instance_id = instance_id;
1562 assert(!se->compat || se->instance_id == 0);
1563 /* add at the end of list */
1564 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1565 return 0;
1568 int register_savevm(DeviceState *dev,
1569 const char *idstr,
1570 int instance_id,
1571 int version_id,
1572 SaveStateHandler *save_state,
1573 LoadStateHandler *load_state,
1574 void *opaque)
1576 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1577 ops->save_state = save_state;
1578 ops->load_state = load_state;
1579 return register_savevm_live(dev, idstr, instance_id, version_id,
1580 ops, opaque);
1583 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1585 SaveStateEntry *se, *new_se;
1586 char id[256] = "";
1588 if (dev) {
1589 char *path = qdev_get_dev_path(dev);
1590 if (path) {
1591 pstrcpy(id, sizeof(id), path);
1592 pstrcat(id, sizeof(id), "/");
1593 g_free(path);
1596 pstrcat(id, sizeof(id), idstr);
1598 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1599 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1600 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1601 if (se->compat) {
1602 g_free(se->compat);
1604 g_free(se->ops);
1605 g_free(se);
1610 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1611 const VMStateDescription *vmsd,
1612 void *opaque, int alias_id,
1613 int required_for_version)
1615 SaveStateEntry *se;
1617 /* If this triggers, alias support can be dropped for the vmsd. */
1618 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1620 se = g_malloc0(sizeof(SaveStateEntry));
1621 se->version_id = vmsd->version_id;
1622 se->section_id = global_section_id++;
1623 se->opaque = opaque;
1624 se->vmsd = vmsd;
1625 se->alias_id = alias_id;
1626 se->no_migrate = vmsd->unmigratable;
1628 if (dev) {
1629 char *id = qdev_get_dev_path(dev);
1630 if (id) {
1631 pstrcpy(se->idstr, sizeof(se->idstr), id);
1632 pstrcat(se->idstr, sizeof(se->idstr), "/");
1633 g_free(id);
1635 se->compat = g_malloc0(sizeof(CompatEntry));
1636 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1637 se->compat->instance_id = instance_id == -1 ?
1638 calculate_compat_instance_id(vmsd->name) : instance_id;
1639 instance_id = -1;
1642 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1644 if (instance_id == -1) {
1645 se->instance_id = calculate_new_instance_id(se->idstr);
1646 } else {
1647 se->instance_id = instance_id;
1649 assert(!se->compat || se->instance_id == 0);
1650 /* add at the end of list */
1651 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1652 return 0;
1655 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1656 void *opaque)
1658 SaveStateEntry *se, *new_se;
1660 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1661 if (se->vmsd == vmsd && se->opaque == opaque) {
1662 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1663 if (se->compat) {
1664 g_free(se->compat);
1666 g_free(se);
1671 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1672 void *opaque);
1673 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1674 void *opaque);
1676 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1677 void *opaque, int version_id)
1679 VMStateField *field = vmsd->fields;
1680 int ret;
1682 if (version_id > vmsd->version_id) {
1683 return -EINVAL;
1685 if (version_id < vmsd->minimum_version_id_old) {
1686 return -EINVAL;
1688 if (version_id < vmsd->minimum_version_id) {
1689 return vmsd->load_state_old(f, opaque, version_id);
1691 if (vmsd->pre_load) {
1692 int ret = vmsd->pre_load(opaque);
1693 if (ret)
1694 return ret;
1696 while(field->name) {
1697 if ((field->field_exists &&
1698 field->field_exists(opaque, version_id)) ||
1699 (!field->field_exists &&
1700 field->version_id <= version_id)) {
1701 void *base_addr = opaque + field->offset;
1702 int i, n_elems = 1;
1703 int size = field->size;
1705 if (field->flags & VMS_VBUFFER) {
1706 size = *(int32_t *)(opaque+field->size_offset);
1707 if (field->flags & VMS_MULTIPLY) {
1708 size *= field->size;
1711 if (field->flags & VMS_ARRAY) {
1712 n_elems = field->num;
1713 } else if (field->flags & VMS_VARRAY_INT32) {
1714 n_elems = *(int32_t *)(opaque+field->num_offset);
1715 } else if (field->flags & VMS_VARRAY_UINT32) {
1716 n_elems = *(uint32_t *)(opaque+field->num_offset);
1717 } else if (field->flags & VMS_VARRAY_UINT16) {
1718 n_elems = *(uint16_t *)(opaque+field->num_offset);
1719 } else if (field->flags & VMS_VARRAY_UINT8) {
1720 n_elems = *(uint8_t *)(opaque+field->num_offset);
1722 if (field->flags & VMS_POINTER) {
1723 base_addr = *(void **)base_addr + field->start;
1725 for (i = 0; i < n_elems; i++) {
1726 void *addr = base_addr + size * i;
1728 if (field->flags & VMS_ARRAY_OF_POINTER) {
1729 addr = *(void **)addr;
1731 if (field->flags & VMS_STRUCT) {
1732 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1733 } else {
1734 ret = field->info->get(f, addr, size);
1737 if (ret < 0) {
1738 return ret;
1742 field++;
1744 ret = vmstate_subsection_load(f, vmsd, opaque);
1745 if (ret != 0) {
1746 return ret;
1748 if (vmsd->post_load) {
1749 return vmsd->post_load(opaque, version_id);
1751 return 0;
1754 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1755 void *opaque)
1757 VMStateField *field = vmsd->fields;
1759 if (vmsd->pre_save) {
1760 vmsd->pre_save(opaque);
1762 while(field->name) {
1763 if (!field->field_exists ||
1764 field->field_exists(opaque, vmsd->version_id)) {
1765 void *base_addr = opaque + field->offset;
1766 int i, n_elems = 1;
1767 int size = field->size;
1769 if (field->flags & VMS_VBUFFER) {
1770 size = *(int32_t *)(opaque+field->size_offset);
1771 if (field->flags & VMS_MULTIPLY) {
1772 size *= field->size;
1775 if (field->flags & VMS_ARRAY) {
1776 n_elems = field->num;
1777 } else if (field->flags & VMS_VARRAY_INT32) {
1778 n_elems = *(int32_t *)(opaque+field->num_offset);
1779 } else if (field->flags & VMS_VARRAY_UINT32) {
1780 n_elems = *(uint32_t *)(opaque+field->num_offset);
1781 } else if (field->flags & VMS_VARRAY_UINT16) {
1782 n_elems = *(uint16_t *)(opaque+field->num_offset);
1783 } else if (field->flags & VMS_VARRAY_UINT8) {
1784 n_elems = *(uint8_t *)(opaque+field->num_offset);
1786 if (field->flags & VMS_POINTER) {
1787 base_addr = *(void **)base_addr + field->start;
1789 for (i = 0; i < n_elems; i++) {
1790 void *addr = base_addr + size * i;
1792 if (field->flags & VMS_ARRAY_OF_POINTER) {
1793 addr = *(void **)addr;
1795 if (field->flags & VMS_STRUCT) {
1796 vmstate_save_state(f, field->vmsd, addr);
1797 } else {
1798 field->info->put(f, addr, size);
1802 field++;
1804 vmstate_subsection_save(f, vmsd, opaque);
1807 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1809 if (!se->vmsd) { /* Old style */
1810 return se->ops->load_state(f, se->opaque, version_id);
1812 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1815 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1817 if (!se->vmsd) { /* Old style */
1818 se->ops->save_state(f, se->opaque);
1819 return;
1821 vmstate_save_state(f,se->vmsd, se->opaque);
1824 #define QEMU_VM_FILE_MAGIC 0x5145564d
1825 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1826 #define QEMU_VM_FILE_VERSION 0x00000003
1828 #define QEMU_VM_EOF 0x00
1829 #define QEMU_VM_SECTION_START 0x01
1830 #define QEMU_VM_SECTION_PART 0x02
1831 #define QEMU_VM_SECTION_END 0x03
1832 #define QEMU_VM_SECTION_FULL 0x04
1833 #define QEMU_VM_SUBSECTION 0x05
1835 bool qemu_savevm_state_blocked(Error **errp)
1837 SaveStateEntry *se;
1839 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1840 if (se->no_migrate) {
1841 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1842 return true;
1845 return false;
1848 void qemu_savevm_state_begin(QEMUFile *f,
1849 const MigrationParams *params)
1851 SaveStateEntry *se;
1852 int ret;
1854 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1855 if (!se->ops || !se->ops->set_params) {
1856 continue;
1858 se->ops->set_params(params, se->opaque);
1861 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1862 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1864 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1865 int len;
1867 if (!se->ops || !se->ops->save_live_setup) {
1868 continue;
1870 if (se->ops && se->ops->is_active) {
1871 if (!se->ops->is_active(se->opaque)) {
1872 continue;
1875 /* Section type */
1876 qemu_put_byte(f, QEMU_VM_SECTION_START);
1877 qemu_put_be32(f, se->section_id);
1879 /* ID string */
1880 len = strlen(se->idstr);
1881 qemu_put_byte(f, len);
1882 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1884 qemu_put_be32(f, se->instance_id);
1885 qemu_put_be32(f, se->version_id);
1887 ret = se->ops->save_live_setup(f, se->opaque);
1888 if (ret < 0) {
1889 qemu_file_set_error(f, ret);
1890 break;
1896 * this function has three return values:
1897 * negative: there was one error, and we have -errno.
1898 * 0 : We haven't finished, caller have to go again
1899 * 1 : We have finished, we can go to complete phase
1901 int qemu_savevm_state_iterate(QEMUFile *f)
1903 SaveStateEntry *se;
1904 int ret = 1;
1906 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1907 if (!se->ops || !se->ops->save_live_iterate) {
1908 continue;
1910 if (se->ops && se->ops->is_active) {
1911 if (!se->ops->is_active(se->opaque)) {
1912 continue;
1915 if (qemu_file_rate_limit(f)) {
1916 return 0;
1918 trace_savevm_section_start();
1919 /* Section type */
1920 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1921 qemu_put_be32(f, se->section_id);
1923 ret = se->ops->save_live_iterate(f, se->opaque);
1924 trace_savevm_section_end(se->section_id);
1926 if (ret < 0) {
1927 qemu_file_set_error(f, ret);
1929 if (ret <= 0) {
1930 /* Do not proceed to the next vmstate before this one reported
1931 completion of the current stage. This serializes the migration
1932 and reduces the probability that a faster changing state is
1933 synchronized over and over again. */
1934 break;
1937 return ret;
1940 void qemu_savevm_state_complete(QEMUFile *f)
1942 SaveStateEntry *se;
1943 int ret;
1945 cpu_synchronize_all_states();
1947 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1948 if (!se->ops || !se->ops->save_live_complete) {
1949 continue;
1951 if (se->ops && se->ops->is_active) {
1952 if (!se->ops->is_active(se->opaque)) {
1953 continue;
1956 trace_savevm_section_start();
1957 /* Section type */
1958 qemu_put_byte(f, QEMU_VM_SECTION_END);
1959 qemu_put_be32(f, se->section_id);
1961 ret = se->ops->save_live_complete(f, se->opaque);
1962 trace_savevm_section_end(se->section_id);
1963 if (ret < 0) {
1964 qemu_file_set_error(f, ret);
1965 return;
1969 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1970 int len;
1972 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1973 continue;
1975 trace_savevm_section_start();
1976 /* Section type */
1977 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1978 qemu_put_be32(f, se->section_id);
1980 /* ID string */
1981 len = strlen(se->idstr);
1982 qemu_put_byte(f, len);
1983 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1985 qemu_put_be32(f, se->instance_id);
1986 qemu_put_be32(f, se->version_id);
1988 vmstate_save(f, se);
1989 trace_savevm_section_end(se->section_id);
1992 qemu_put_byte(f, QEMU_VM_EOF);
1993 qemu_fflush(f);
1996 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1998 SaveStateEntry *se;
1999 uint64_t ret = 0;
2001 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2002 if (!se->ops || !se->ops->save_live_pending) {
2003 continue;
2005 if (se->ops && se->ops->is_active) {
2006 if (!se->ops->is_active(se->opaque)) {
2007 continue;
2010 ret += se->ops->save_live_pending(f, se->opaque, max_size);
2012 return ret;
2015 void qemu_savevm_state_cancel(void)
2017 SaveStateEntry *se;
2019 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2020 if (se->ops && se->ops->cancel) {
2021 se->ops->cancel(se->opaque);
2026 static int qemu_savevm_state(QEMUFile *f)
2028 int ret;
2029 MigrationParams params = {
2030 .blk = 0,
2031 .shared = 0
2034 if (qemu_savevm_state_blocked(NULL)) {
2035 return -EINVAL;
2038 qemu_mutex_unlock_iothread();
2039 qemu_savevm_state_begin(f, &params);
2040 qemu_mutex_lock_iothread();
2042 while (qemu_file_get_error(f) == 0) {
2043 if (qemu_savevm_state_iterate(f) > 0) {
2044 break;
2048 ret = qemu_file_get_error(f);
2049 if (ret == 0) {
2050 qemu_savevm_state_complete(f);
2051 ret = qemu_file_get_error(f);
2053 if (ret != 0) {
2054 qemu_savevm_state_cancel();
2056 return ret;
2059 static int qemu_save_device_state(QEMUFile *f)
2061 SaveStateEntry *se;
2063 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2064 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2066 cpu_synchronize_all_states();
2068 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2069 int len;
2071 if (se->is_ram) {
2072 continue;
2074 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
2075 continue;
2078 /* Section type */
2079 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2080 qemu_put_be32(f, se->section_id);
2082 /* ID string */
2083 len = strlen(se->idstr);
2084 qemu_put_byte(f, len);
2085 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2087 qemu_put_be32(f, se->instance_id);
2088 qemu_put_be32(f, se->version_id);
2090 vmstate_save(f, se);
2093 qemu_put_byte(f, QEMU_VM_EOF);
2095 return qemu_file_get_error(f);
2098 static SaveStateEntry *find_se(const char *idstr, int instance_id)
2100 SaveStateEntry *se;
2102 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2103 if (!strcmp(se->idstr, idstr) &&
2104 (instance_id == se->instance_id ||
2105 instance_id == se->alias_id))
2106 return se;
2107 /* Migrating from an older version? */
2108 if (strstr(se->idstr, idstr) && se->compat) {
2109 if (!strcmp(se->compat->idstr, idstr) &&
2110 (instance_id == se->compat->instance_id ||
2111 instance_id == se->alias_id))
2112 return se;
2115 return NULL;
2118 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2120 while(sub && sub->needed) {
2121 if (strcmp(idstr, sub->vmsd->name) == 0) {
2122 return sub->vmsd;
2124 sub++;
2126 return NULL;
2129 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2130 void *opaque)
2132 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
2133 char idstr[256];
2134 int ret;
2135 uint8_t version_id, len, size;
2136 const VMStateDescription *sub_vmsd;
2138 len = qemu_peek_byte(f, 1);
2139 if (len < strlen(vmsd->name) + 1) {
2140 /* subsection name has be be "section_name/a" */
2141 return 0;
2143 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2144 if (size != len) {
2145 return 0;
2147 idstr[size] = 0;
2149 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2150 /* it don't have a valid subsection name */
2151 return 0;
2153 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
2154 if (sub_vmsd == NULL) {
2155 return -ENOENT;
2157 qemu_file_skip(f, 1); /* subsection */
2158 qemu_file_skip(f, 1); /* len */
2159 qemu_file_skip(f, len); /* idstr */
2160 version_id = qemu_get_be32(f);
2162 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2163 if (ret) {
2164 return ret;
2167 return 0;
2170 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2171 void *opaque)
2173 const VMStateSubsection *sub = vmsd->subsections;
2175 while (sub && sub->needed) {
2176 if (sub->needed(opaque)) {
2177 const VMStateDescription *vmsd = sub->vmsd;
2178 uint8_t len;
2180 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2181 len = strlen(vmsd->name);
2182 qemu_put_byte(f, len);
2183 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2184 qemu_put_be32(f, vmsd->version_id);
2185 vmstate_save_state(f, vmsd, opaque);
2187 sub++;
2191 typedef struct LoadStateEntry {
2192 QLIST_ENTRY(LoadStateEntry) entry;
2193 SaveStateEntry *se;
2194 int section_id;
2195 int version_id;
2196 } LoadStateEntry;
2198 int qemu_loadvm_state(QEMUFile *f)
2200 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2201 QLIST_HEAD_INITIALIZER(loadvm_handlers);
2202 LoadStateEntry *le, *new_le;
2203 uint8_t section_type;
2204 unsigned int v;
2205 int ret;
2207 if (qemu_savevm_state_blocked(NULL)) {
2208 return -EINVAL;
2211 v = qemu_get_be32(f);
2212 if (v != QEMU_VM_FILE_MAGIC)
2213 return -EINVAL;
2215 v = qemu_get_be32(f);
2216 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2217 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2218 return -ENOTSUP;
2220 if (v != QEMU_VM_FILE_VERSION)
2221 return -ENOTSUP;
2223 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2224 uint32_t instance_id, version_id, section_id;
2225 SaveStateEntry *se;
2226 char idstr[257];
2227 int len;
2229 switch (section_type) {
2230 case QEMU_VM_SECTION_START:
2231 case QEMU_VM_SECTION_FULL:
2232 /* Read section start */
2233 section_id = qemu_get_be32(f);
2234 len = qemu_get_byte(f);
2235 qemu_get_buffer(f, (uint8_t *)idstr, len);
2236 idstr[len] = 0;
2237 instance_id = qemu_get_be32(f);
2238 version_id = qemu_get_be32(f);
2240 /* Find savevm section */
2241 se = find_se(idstr, instance_id);
2242 if (se == NULL) {
2243 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2244 ret = -EINVAL;
2245 goto out;
2248 /* Validate version */
2249 if (version_id > se->version_id) {
2250 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2251 version_id, idstr, se->version_id);
2252 ret = -EINVAL;
2253 goto out;
2256 /* Add entry */
2257 le = g_malloc0(sizeof(*le));
2259 le->se = se;
2260 le->section_id = section_id;
2261 le->version_id = version_id;
2262 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2264 ret = vmstate_load(f, le->se, le->version_id);
2265 if (ret < 0) {
2266 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2267 instance_id, idstr);
2268 goto out;
2270 break;
2271 case QEMU_VM_SECTION_PART:
2272 case QEMU_VM_SECTION_END:
2273 section_id = qemu_get_be32(f);
2275 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2276 if (le->section_id == section_id) {
2277 break;
2280 if (le == NULL) {
2281 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2282 ret = -EINVAL;
2283 goto out;
2286 ret = vmstate_load(f, le->se, le->version_id);
2287 if (ret < 0) {
2288 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2289 section_id);
2290 goto out;
2292 break;
2293 default:
2294 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2295 ret = -EINVAL;
2296 goto out;
2300 cpu_synchronize_all_post_init();
2302 ret = 0;
2304 out:
2305 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2306 QLIST_REMOVE(le, entry);
2307 g_free(le);
2310 if (ret == 0) {
2311 ret = qemu_file_get_error(f);
2314 return ret;
2317 static BlockDriverState *find_vmstate_bs(void)
2319 BlockDriverState *bs = NULL;
2320 while ((bs = bdrv_next(bs))) {
2321 if (bdrv_can_snapshot(bs)) {
2322 return bs;
2325 return NULL;
2329 * Deletes snapshots of a given name in all opened images.
2331 static int del_existing_snapshots(Monitor *mon, const char *name)
2333 BlockDriverState *bs;
2334 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2335 Error *err = NULL;
2337 bs = NULL;
2338 while ((bs = bdrv_next(bs))) {
2339 if (bdrv_can_snapshot(bs) &&
2340 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2342 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
2343 if (error_is_set(&err)) {
2344 monitor_printf(mon,
2345 "Error while deleting snapshot on device '%s':"
2346 " %s\n",
2347 bdrv_get_device_name(bs),
2348 error_get_pretty(err));
2349 error_free(err);
2350 return -1;
2355 return 0;
2358 void do_savevm(Monitor *mon, const QDict *qdict)
2360 BlockDriverState *bs, *bs1;
2361 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2362 int ret;
2363 QEMUFile *f;
2364 int saved_vm_running;
2365 uint64_t vm_state_size;
2366 qemu_timeval tv;
2367 struct tm tm;
2368 const char *name = qdict_get_try_str(qdict, "name");
2370 /* Verify if there is a device that doesn't support snapshots and is writable */
2371 bs = NULL;
2372 while ((bs = bdrv_next(bs))) {
2374 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2375 continue;
2378 if (!bdrv_can_snapshot(bs)) {
2379 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2380 bdrv_get_device_name(bs));
2381 return;
2385 bs = find_vmstate_bs();
2386 if (!bs) {
2387 monitor_printf(mon, "No block device can accept snapshots\n");
2388 return;
2391 saved_vm_running = runstate_is_running();
2392 vm_stop(RUN_STATE_SAVE_VM);
2394 memset(sn, 0, sizeof(*sn));
2396 /* fill auxiliary fields */
2397 qemu_gettimeofday(&tv);
2398 sn->date_sec = tv.tv_sec;
2399 sn->date_nsec = tv.tv_usec * 1000;
2400 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
2402 if (name) {
2403 ret = bdrv_snapshot_find(bs, old_sn, name);
2404 if (ret >= 0) {
2405 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2406 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2407 } else {
2408 pstrcpy(sn->name, sizeof(sn->name), name);
2410 } else {
2411 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2412 localtime_r((const time_t *)&tv.tv_sec, &tm);
2413 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2416 /* Delete old snapshots of the same name */
2417 if (name && del_existing_snapshots(mon, name) < 0) {
2418 goto the_end;
2421 /* save the VM state */
2422 f = qemu_fopen_bdrv(bs, 1);
2423 if (!f) {
2424 monitor_printf(mon, "Could not open VM state file\n");
2425 goto the_end;
2427 ret = qemu_savevm_state(f);
2428 vm_state_size = qemu_ftell(f);
2429 qemu_fclose(f);
2430 if (ret < 0) {
2431 monitor_printf(mon, "Error %d while writing VM\n", ret);
2432 goto the_end;
2435 /* create the snapshots */
2437 bs1 = NULL;
2438 while ((bs1 = bdrv_next(bs1))) {
2439 if (bdrv_can_snapshot(bs1)) {
2440 /* Write VM state size only to the image that contains the state */
2441 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2442 ret = bdrv_snapshot_create(bs1, sn);
2443 if (ret < 0) {
2444 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2445 bdrv_get_device_name(bs1));
2450 the_end:
2451 if (saved_vm_running)
2452 vm_start();
2455 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2457 QEMUFile *f;
2458 int saved_vm_running;
2459 int ret;
2461 saved_vm_running = runstate_is_running();
2462 vm_stop(RUN_STATE_SAVE_VM);
2464 f = qemu_fopen(filename, "wb");
2465 if (!f) {
2466 error_setg_file_open(errp, errno, filename);
2467 goto the_end;
2469 ret = qemu_save_device_state(f);
2470 qemu_fclose(f);
2471 if (ret < 0) {
2472 error_set(errp, QERR_IO_ERROR);
2475 the_end:
2476 if (saved_vm_running)
2477 vm_start();
2480 int load_vmstate(const char *name)
2482 BlockDriverState *bs, *bs_vm_state;
2483 QEMUSnapshotInfo sn;
2484 QEMUFile *f;
2485 int ret;
2487 bs_vm_state = find_vmstate_bs();
2488 if (!bs_vm_state) {
2489 error_report("No block device supports snapshots");
2490 return -ENOTSUP;
2493 /* Don't even try to load empty VM states */
2494 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2495 if (ret < 0) {
2496 return ret;
2497 } else if (sn.vm_state_size == 0) {
2498 error_report("This is a disk-only snapshot. Revert to it offline "
2499 "using qemu-img.");
2500 return -EINVAL;
2503 /* Verify if there is any device that doesn't support snapshots and is
2504 writable and check if the requested snapshot is available too. */
2505 bs = NULL;
2506 while ((bs = bdrv_next(bs))) {
2508 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2509 continue;
2512 if (!bdrv_can_snapshot(bs)) {
2513 error_report("Device '%s' is writable but does not support snapshots.",
2514 bdrv_get_device_name(bs));
2515 return -ENOTSUP;
2518 ret = bdrv_snapshot_find(bs, &sn, name);
2519 if (ret < 0) {
2520 error_report("Device '%s' does not have the requested snapshot '%s'",
2521 bdrv_get_device_name(bs), name);
2522 return ret;
2526 /* Flush all IO requests so they don't interfere with the new state. */
2527 bdrv_drain_all();
2529 bs = NULL;
2530 while ((bs = bdrv_next(bs))) {
2531 if (bdrv_can_snapshot(bs)) {
2532 ret = bdrv_snapshot_goto(bs, name);
2533 if (ret < 0) {
2534 error_report("Error %d while activating snapshot '%s' on '%s'",
2535 ret, name, bdrv_get_device_name(bs));
2536 return ret;
2541 /* restore the VM state */
2542 f = qemu_fopen_bdrv(bs_vm_state, 0);
2543 if (!f) {
2544 error_report("Could not open VM state file");
2545 return -EINVAL;
2548 qemu_system_reset(VMRESET_SILENT);
2549 ret = qemu_loadvm_state(f);
2551 qemu_fclose(f);
2552 if (ret < 0) {
2553 error_report("Error %d while loading VM state", ret);
2554 return ret;
2557 return 0;
2560 void do_delvm(Monitor *mon, const QDict *qdict)
2562 BlockDriverState *bs, *bs1;
2563 Error *err = NULL;
2564 const char *name = qdict_get_str(qdict, "name");
2566 bs = find_vmstate_bs();
2567 if (!bs) {
2568 monitor_printf(mon, "No block device supports snapshots\n");
2569 return;
2572 bs1 = NULL;
2573 while ((bs1 = bdrv_next(bs1))) {
2574 if (bdrv_can_snapshot(bs1)) {
2575 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
2576 if (error_is_set(&err)) {
2577 monitor_printf(mon,
2578 "Error while deleting snapshot on device '%s':"
2579 " %s\n",
2580 bdrv_get_device_name(bs),
2581 error_get_pretty(err));
2582 error_free(err);
2588 void do_info_snapshots(Monitor *mon, const QDict *qdict)
2590 BlockDriverState *bs, *bs1;
2591 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2592 int nb_sns, i, ret, available;
2593 int total;
2594 int *available_snapshots;
2596 bs = find_vmstate_bs();
2597 if (!bs) {
2598 monitor_printf(mon, "No available block device supports snapshots\n");
2599 return;
2602 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2603 if (nb_sns < 0) {
2604 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2605 return;
2608 if (nb_sns == 0) {
2609 monitor_printf(mon, "There is no snapshot available.\n");
2610 return;
2613 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2614 total = 0;
2615 for (i = 0; i < nb_sns; i++) {
2616 sn = &sn_tab[i];
2617 available = 1;
2618 bs1 = NULL;
2620 while ((bs1 = bdrv_next(bs1))) {
2621 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2622 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2623 if (ret < 0) {
2624 available = 0;
2625 break;
2630 if (available) {
2631 available_snapshots[total] = i;
2632 total++;
2636 if (total > 0) {
2637 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2638 monitor_printf(mon, "\n");
2639 for (i = 0; i < total; i++) {
2640 sn = &sn_tab[available_snapshots[i]];
2641 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2642 monitor_printf(mon, "\n");
2644 } else {
2645 monitor_printf(mon, "There is no suitable snapshot available\n");
2648 g_free(sn_tab);
2649 g_free(available_snapshots);
2653 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2655 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2656 memory_region_name(mr), dev);
2659 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2661 /* Nothing do to while the implementation is in RAMBlock */
2664 void vmstate_register_ram_global(MemoryRegion *mr)
2666 vmstate_register_ram(mr, NULL);