console: allow pinning displaychangelisteners to consoles
[qemu/ar7.git] / savevm.c
blob53515cb794c80d6cc9c6e149e72d77d06ec55cf9
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_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
224 QEMUFileSocket *s = opaque;
225 ssize_t len;
227 len = qemu_send_full(s->fd, buf, size, 0);
228 if (len < size) {
229 len = -socket_error();
231 return len;
234 static int socket_close(void *opaque)
236 QEMUFileSocket *s = opaque;
237 closesocket(s->fd);
238 g_free(s);
239 return 0;
242 static int stdio_get_fd(void *opaque)
244 QEMUFileStdio *s = opaque;
246 return fileno(s->stdio_file);
249 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
251 QEMUFileStdio *s = opaque;
252 return fwrite(buf, 1, size, s->stdio_file);
255 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
257 QEMUFileStdio *s = opaque;
258 FILE *fp = s->stdio_file;
259 int bytes;
261 for (;;) {
262 clearerr(fp);
263 bytes = fread(buf, 1, size, fp);
264 if (bytes != 0 || !ferror(fp)) {
265 break;
267 if (errno == EAGAIN) {
268 yield_until_fd_readable(fileno(fp));
269 } else if (errno != EINTR) {
270 break;
273 return bytes;
276 static int stdio_pclose(void *opaque)
278 QEMUFileStdio *s = opaque;
279 int ret;
280 ret = pclose(s->stdio_file);
281 if (ret == -1) {
282 ret = -errno;
283 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
284 /* close succeeded, but non-zero exit code: */
285 ret = -EIO; /* fake errno value */
287 g_free(s);
288 return ret;
291 static int stdio_fclose(void *opaque)
293 QEMUFileStdio *s = opaque;
294 int ret = 0;
296 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
297 int fd = fileno(s->stdio_file);
298 struct stat st;
300 ret = fstat(fd, &st);
301 if (ret == 0 && S_ISREG(st.st_mode)) {
303 * If the file handle is a regular file make sure the
304 * data is flushed to disk before signaling success.
306 ret = fsync(fd);
307 if (ret != 0) {
308 ret = -errno;
309 return ret;
313 if (fclose(s->stdio_file) == EOF) {
314 ret = -errno;
316 g_free(s);
317 return ret;
320 static const QEMUFileOps stdio_pipe_read_ops = {
321 .get_fd = stdio_get_fd,
322 .get_buffer = stdio_get_buffer,
323 .close = stdio_pclose
326 static const QEMUFileOps stdio_pipe_write_ops = {
327 .get_fd = stdio_get_fd,
328 .put_buffer = stdio_put_buffer,
329 .close = stdio_pclose
332 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
334 FILE *stdio_file;
335 QEMUFileStdio *s;
337 stdio_file = popen(command, mode);
338 if (stdio_file == NULL) {
339 return NULL;
342 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
343 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
344 return NULL;
347 s = g_malloc0(sizeof(QEMUFileStdio));
349 s->stdio_file = stdio_file;
351 if(mode[0] == 'r') {
352 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
353 } else {
354 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
356 return s->file;
359 static const QEMUFileOps stdio_file_read_ops = {
360 .get_fd = stdio_get_fd,
361 .get_buffer = stdio_get_buffer,
362 .close = stdio_fclose
365 static const QEMUFileOps stdio_file_write_ops = {
366 .get_fd = stdio_get_fd,
367 .put_buffer = stdio_put_buffer,
368 .close = stdio_fclose
371 QEMUFile *qemu_fdopen(int fd, const char *mode)
373 QEMUFileStdio *s;
375 if (mode == NULL ||
376 (mode[0] != 'r' && mode[0] != 'w') ||
377 mode[1] != 'b' || mode[2] != 0) {
378 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
379 return NULL;
382 s = g_malloc0(sizeof(QEMUFileStdio));
383 s->stdio_file = fdopen(fd, mode);
384 if (!s->stdio_file)
385 goto fail;
387 if(mode[0] == 'r') {
388 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
389 } else {
390 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
392 return s->file;
394 fail:
395 g_free(s);
396 return NULL;
399 static const QEMUFileOps socket_read_ops = {
400 .get_fd = socket_get_fd,
401 .get_buffer = socket_get_buffer,
402 .close = socket_close
405 static const QEMUFileOps socket_write_ops = {
406 .get_fd = socket_get_fd,
407 .put_buffer = socket_put_buffer,
408 .writev_buffer = socket_writev_buffer,
409 .close = socket_close
412 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
414 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
416 if (mode == NULL ||
417 (mode[0] != 'r' && mode[0] != 'w') ||
418 mode[1] != 'b' || mode[2] != 0) {
419 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
420 return NULL;
423 s->fd = fd;
424 if (mode[0] == 'w') {
425 qemu_set_block(s->fd);
426 s->file = qemu_fopen_ops(s, &socket_write_ops);
427 } else {
428 s->file = qemu_fopen_ops(s, &socket_read_ops);
430 return s->file;
433 QEMUFile *qemu_fopen(const char *filename, const char *mode)
435 QEMUFileStdio *s;
437 if (mode == NULL ||
438 (mode[0] != 'r' && mode[0] != 'w') ||
439 mode[1] != 'b' || mode[2] != 0) {
440 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
441 return NULL;
444 s = g_malloc0(sizeof(QEMUFileStdio));
446 s->stdio_file = fopen(filename, mode);
447 if (!s->stdio_file)
448 goto fail;
450 if(mode[0] == 'w') {
451 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
452 } else {
453 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
455 return s->file;
456 fail:
457 g_free(s);
458 return NULL;
461 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
462 int64_t pos)
464 int ret;
465 QEMUIOVector qiov;
467 qemu_iovec_init_external(&qiov, iov, iovcnt);
468 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
469 if (ret < 0) {
470 return ret;
473 return qiov.size;
476 static int block_put_buffer(void *opaque, const uint8_t *buf,
477 int64_t pos, int size)
479 bdrv_save_vmstate(opaque, buf, pos, size);
480 return size;
483 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
485 return bdrv_load_vmstate(opaque, buf, pos, size);
488 static int bdrv_fclose(void *opaque)
490 return bdrv_flush(opaque);
493 static const QEMUFileOps bdrv_read_ops = {
494 .get_buffer = block_get_buffer,
495 .close = bdrv_fclose
498 static const QEMUFileOps bdrv_write_ops = {
499 .put_buffer = block_put_buffer,
500 .writev_buffer = block_writev_buffer,
501 .close = bdrv_fclose
504 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
506 if (is_writable)
507 return qemu_fopen_ops(bs, &bdrv_write_ops);
508 return qemu_fopen_ops(bs, &bdrv_read_ops);
511 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
513 QEMUFile *f;
515 f = g_malloc0(sizeof(QEMUFile));
517 f->opaque = opaque;
518 f->ops = ops;
519 return f;
522 int qemu_file_get_error(QEMUFile *f)
524 return f->last_error;
527 static void qemu_file_set_error(QEMUFile *f, int ret)
529 if (f->last_error == 0) {
530 f->last_error = ret;
534 static inline bool qemu_file_is_writable(QEMUFile *f)
536 return f->ops->writev_buffer || f->ops->put_buffer;
540 * Flushes QEMUFile buffer
542 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
543 * put_buffer ops.
545 static void qemu_fflush(QEMUFile *f)
547 ssize_t ret = 0;
549 if (!qemu_file_is_writable(f)) {
550 return;
553 if (f->ops->writev_buffer) {
554 if (f->iovcnt > 0) {
555 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
557 } else {
558 if (f->buf_index > 0) {
559 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
562 if (ret >= 0) {
563 f->pos += ret;
565 f->buf_index = 0;
566 f->iovcnt = 0;
567 if (ret < 0) {
568 qemu_file_set_error(f, ret);
572 static void qemu_fill_buffer(QEMUFile *f)
574 int len;
575 int pending;
577 assert(!qemu_file_is_writable(f));
579 pending = f->buf_size - f->buf_index;
580 if (pending > 0) {
581 memmove(f->buf, f->buf + f->buf_index, pending);
583 f->buf_index = 0;
584 f->buf_size = pending;
586 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
587 IO_BUF_SIZE - pending);
588 if (len > 0) {
589 f->buf_size += len;
590 f->pos += len;
591 } else if (len == 0) {
592 qemu_file_set_error(f, -EIO);
593 } else if (len != -EAGAIN)
594 qemu_file_set_error(f, len);
597 int qemu_get_fd(QEMUFile *f)
599 if (f->ops->get_fd) {
600 return f->ops->get_fd(f->opaque);
602 return -1;
605 /** Closes the file
607 * Returns negative error value if any error happened on previous operations or
608 * while closing the file. Returns 0 or positive number on success.
610 * The meaning of return value on success depends on the specific backend
611 * being used.
613 int qemu_fclose(QEMUFile *f)
615 int ret;
616 qemu_fflush(f);
617 ret = qemu_file_get_error(f);
619 if (f->ops->close) {
620 int ret2 = f->ops->close(f->opaque);
621 if (ret >= 0) {
622 ret = ret2;
625 /* If any error was spotted before closing, we should report it
626 * instead of the close() return value.
628 if (f->last_error) {
629 ret = f->last_error;
631 g_free(f);
632 return ret;
635 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
637 /* check for adjacent buffer and coalesce them */
638 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
639 f->iov[f->iovcnt - 1].iov_len) {
640 f->iov[f->iovcnt - 1].iov_len += size;
641 } else {
642 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
643 f->iov[f->iovcnt++].iov_len = size;
646 if (f->iovcnt >= MAX_IOV_SIZE) {
647 qemu_fflush(f);
651 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
653 if (!f->ops->writev_buffer) {
654 qemu_put_buffer(f, buf, size);
655 return;
658 if (f->last_error) {
659 return;
662 f->bytes_xfer += size;
663 add_to_iovec(f, buf, size);
666 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
668 int l;
670 if (f->last_error) {
671 return;
674 while (size > 0) {
675 l = IO_BUF_SIZE - f->buf_index;
676 if (l > size)
677 l = size;
678 memcpy(f->buf + f->buf_index, buf, l);
679 f->bytes_xfer += size;
680 if (f->ops->writev_buffer) {
681 add_to_iovec(f, f->buf + f->buf_index, l);
683 f->buf_index += l;
684 if (f->buf_index == IO_BUF_SIZE) {
685 qemu_fflush(f);
687 if (qemu_file_get_error(f)) {
688 break;
690 buf += l;
691 size -= l;
695 void qemu_put_byte(QEMUFile *f, int v)
697 if (f->last_error) {
698 return;
701 f->buf[f->buf_index] = v;
702 f->bytes_xfer++;
703 if (f->ops->writev_buffer) {
704 add_to_iovec(f, f->buf + f->buf_index, 1);
706 f->buf_index++;
707 if (f->buf_index == IO_BUF_SIZE) {
708 qemu_fflush(f);
712 static void qemu_file_skip(QEMUFile *f, int size)
714 if (f->buf_index + size <= f->buf_size) {
715 f->buf_index += size;
719 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
721 int pending;
722 int index;
724 assert(!qemu_file_is_writable(f));
726 index = f->buf_index + offset;
727 pending = f->buf_size - index;
728 if (pending < size) {
729 qemu_fill_buffer(f);
730 index = f->buf_index + offset;
731 pending = f->buf_size - index;
734 if (pending <= 0) {
735 return 0;
737 if (size > pending) {
738 size = pending;
741 memcpy(buf, f->buf + index, size);
742 return size;
745 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
747 int pending = size;
748 int done = 0;
750 while (pending > 0) {
751 int res;
753 res = qemu_peek_buffer(f, buf, pending, 0);
754 if (res == 0) {
755 return done;
757 qemu_file_skip(f, res);
758 buf += res;
759 pending -= res;
760 done += res;
762 return done;
765 static int qemu_peek_byte(QEMUFile *f, int offset)
767 int index = f->buf_index + offset;
769 assert(!qemu_file_is_writable(f));
771 if (index >= f->buf_size) {
772 qemu_fill_buffer(f);
773 index = f->buf_index + offset;
774 if (index >= f->buf_size) {
775 return 0;
778 return f->buf[index];
781 int qemu_get_byte(QEMUFile *f)
783 int result;
785 result = qemu_peek_byte(f, 0);
786 qemu_file_skip(f, 1);
787 return result;
790 int64_t qemu_ftell(QEMUFile *f)
792 qemu_fflush(f);
793 return f->pos;
796 int qemu_file_rate_limit(QEMUFile *f)
798 if (qemu_file_get_error(f)) {
799 return 1;
801 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
802 return 1;
804 return 0;
807 int64_t qemu_file_get_rate_limit(QEMUFile *f)
809 return f->xfer_limit;
812 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
814 f->xfer_limit = limit;
817 void qemu_file_reset_rate_limit(QEMUFile *f)
819 f->bytes_xfer = 0;
822 void qemu_put_be16(QEMUFile *f, unsigned int v)
824 qemu_put_byte(f, v >> 8);
825 qemu_put_byte(f, v);
828 void qemu_put_be32(QEMUFile *f, unsigned int v)
830 qemu_put_byte(f, v >> 24);
831 qemu_put_byte(f, v >> 16);
832 qemu_put_byte(f, v >> 8);
833 qemu_put_byte(f, v);
836 void qemu_put_be64(QEMUFile *f, uint64_t v)
838 qemu_put_be32(f, v >> 32);
839 qemu_put_be32(f, v);
842 unsigned int qemu_get_be16(QEMUFile *f)
844 unsigned int v;
845 v = qemu_get_byte(f) << 8;
846 v |= qemu_get_byte(f);
847 return v;
850 unsigned int qemu_get_be32(QEMUFile *f)
852 unsigned int v;
853 v = qemu_get_byte(f) << 24;
854 v |= qemu_get_byte(f) << 16;
855 v |= qemu_get_byte(f) << 8;
856 v |= qemu_get_byte(f);
857 return v;
860 uint64_t qemu_get_be64(QEMUFile *f)
862 uint64_t v;
863 v = (uint64_t)qemu_get_be32(f) << 32;
864 v |= qemu_get_be32(f);
865 return v;
869 /* timer */
871 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
873 uint64_t expire_time;
875 expire_time = qemu_timer_expire_time_ns(ts);
876 qemu_put_be64(f, expire_time);
879 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
881 uint64_t expire_time;
883 expire_time = qemu_get_be64(f);
884 if (expire_time != -1) {
885 qemu_mod_timer_ns(ts, expire_time);
886 } else {
887 qemu_del_timer(ts);
892 /* bool */
894 static int get_bool(QEMUFile *f, void *pv, size_t size)
896 bool *v = pv;
897 *v = qemu_get_byte(f);
898 return 0;
901 static void put_bool(QEMUFile *f, void *pv, size_t size)
903 bool *v = pv;
904 qemu_put_byte(f, *v);
907 const VMStateInfo vmstate_info_bool = {
908 .name = "bool",
909 .get = get_bool,
910 .put = put_bool,
913 /* 8 bit int */
915 static int get_int8(QEMUFile *f, void *pv, size_t size)
917 int8_t *v = pv;
918 qemu_get_s8s(f, v);
919 return 0;
922 static void put_int8(QEMUFile *f, void *pv, size_t size)
924 int8_t *v = pv;
925 qemu_put_s8s(f, v);
928 const VMStateInfo vmstate_info_int8 = {
929 .name = "int8",
930 .get = get_int8,
931 .put = put_int8,
934 /* 16 bit int */
936 static int get_int16(QEMUFile *f, void *pv, size_t size)
938 int16_t *v = pv;
939 qemu_get_sbe16s(f, v);
940 return 0;
943 static void put_int16(QEMUFile *f, void *pv, size_t size)
945 int16_t *v = pv;
946 qemu_put_sbe16s(f, v);
949 const VMStateInfo vmstate_info_int16 = {
950 .name = "int16",
951 .get = get_int16,
952 .put = put_int16,
955 /* 32 bit int */
957 static int get_int32(QEMUFile *f, void *pv, size_t size)
959 int32_t *v = pv;
960 qemu_get_sbe32s(f, v);
961 return 0;
964 static void put_int32(QEMUFile *f, void *pv, size_t size)
966 int32_t *v = pv;
967 qemu_put_sbe32s(f, v);
970 const VMStateInfo vmstate_info_int32 = {
971 .name = "int32",
972 .get = get_int32,
973 .put = put_int32,
976 /* 32 bit int. See that the received value is the same than the one
977 in the field */
979 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
981 int32_t *v = pv;
982 int32_t v2;
983 qemu_get_sbe32s(f, &v2);
985 if (*v == v2)
986 return 0;
987 return -EINVAL;
990 const VMStateInfo vmstate_info_int32_equal = {
991 .name = "int32 equal",
992 .get = get_int32_equal,
993 .put = put_int32,
996 /* 32 bit int. See that the received value is the less or the same
997 than the one in the field */
999 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1001 int32_t *old = pv;
1002 int32_t new;
1003 qemu_get_sbe32s(f, &new);
1005 if (*old <= new)
1006 return 0;
1007 return -EINVAL;
1010 const VMStateInfo vmstate_info_int32_le = {
1011 .name = "int32 equal",
1012 .get = get_int32_le,
1013 .put = put_int32,
1016 /* 64 bit int */
1018 static int get_int64(QEMUFile *f, void *pv, size_t size)
1020 int64_t *v = pv;
1021 qemu_get_sbe64s(f, v);
1022 return 0;
1025 static void put_int64(QEMUFile *f, void *pv, size_t size)
1027 int64_t *v = pv;
1028 qemu_put_sbe64s(f, v);
1031 const VMStateInfo vmstate_info_int64 = {
1032 .name = "int64",
1033 .get = get_int64,
1034 .put = put_int64,
1037 /* 8 bit unsigned int */
1039 static int get_uint8(QEMUFile *f, void *pv, size_t size)
1041 uint8_t *v = pv;
1042 qemu_get_8s(f, v);
1043 return 0;
1046 static void put_uint8(QEMUFile *f, void *pv, size_t size)
1048 uint8_t *v = pv;
1049 qemu_put_8s(f, v);
1052 const VMStateInfo vmstate_info_uint8 = {
1053 .name = "uint8",
1054 .get = get_uint8,
1055 .put = put_uint8,
1058 /* 16 bit unsigned int */
1060 static int get_uint16(QEMUFile *f, void *pv, size_t size)
1062 uint16_t *v = pv;
1063 qemu_get_be16s(f, v);
1064 return 0;
1067 static void put_uint16(QEMUFile *f, void *pv, size_t size)
1069 uint16_t *v = pv;
1070 qemu_put_be16s(f, v);
1073 const VMStateInfo vmstate_info_uint16 = {
1074 .name = "uint16",
1075 .get = get_uint16,
1076 .put = put_uint16,
1079 /* 32 bit unsigned int */
1081 static int get_uint32(QEMUFile *f, void *pv, size_t size)
1083 uint32_t *v = pv;
1084 qemu_get_be32s(f, v);
1085 return 0;
1088 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1090 uint32_t *v = pv;
1091 qemu_put_be32s(f, v);
1094 const VMStateInfo vmstate_info_uint32 = {
1095 .name = "uint32",
1096 .get = get_uint32,
1097 .put = put_uint32,
1100 /* 32 bit uint. See that the received value is the same than the one
1101 in the field */
1103 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1105 uint32_t *v = pv;
1106 uint32_t v2;
1107 qemu_get_be32s(f, &v2);
1109 if (*v == v2) {
1110 return 0;
1112 return -EINVAL;
1115 const VMStateInfo vmstate_info_uint32_equal = {
1116 .name = "uint32 equal",
1117 .get = get_uint32_equal,
1118 .put = put_uint32,
1121 /* 64 bit unsigned int */
1123 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1125 uint64_t *v = pv;
1126 qemu_get_be64s(f, v);
1127 return 0;
1130 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1132 uint64_t *v = pv;
1133 qemu_put_be64s(f, v);
1136 const VMStateInfo vmstate_info_uint64 = {
1137 .name = "uint64",
1138 .get = get_uint64,
1139 .put = put_uint64,
1142 /* 64 bit unsigned int. See that the received value is the same than the one
1143 in the field */
1145 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1147 uint64_t *v = pv;
1148 uint64_t v2;
1149 qemu_get_be64s(f, &v2);
1151 if (*v == v2) {
1152 return 0;
1154 return -EINVAL;
1157 const VMStateInfo vmstate_info_uint64_equal = {
1158 .name = "int64 equal",
1159 .get = get_uint64_equal,
1160 .put = put_uint64,
1163 /* 8 bit int. See that the received value is the same than the one
1164 in the field */
1166 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1168 uint8_t *v = pv;
1169 uint8_t v2;
1170 qemu_get_8s(f, &v2);
1172 if (*v == v2)
1173 return 0;
1174 return -EINVAL;
1177 const VMStateInfo vmstate_info_uint8_equal = {
1178 .name = "uint8 equal",
1179 .get = get_uint8_equal,
1180 .put = put_uint8,
1183 /* 16 bit unsigned int int. See that the received value is the same than the one
1184 in the field */
1186 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1188 uint16_t *v = pv;
1189 uint16_t v2;
1190 qemu_get_be16s(f, &v2);
1192 if (*v == v2)
1193 return 0;
1194 return -EINVAL;
1197 const VMStateInfo vmstate_info_uint16_equal = {
1198 .name = "uint16 equal",
1199 .get = get_uint16_equal,
1200 .put = put_uint16,
1203 /* floating point */
1205 static int get_float64(QEMUFile *f, void *pv, size_t size)
1207 float64 *v = pv;
1209 *v = make_float64(qemu_get_be64(f));
1210 return 0;
1213 static void put_float64(QEMUFile *f, void *pv, size_t size)
1215 uint64_t *v = pv;
1217 qemu_put_be64(f, float64_val(*v));
1220 const VMStateInfo vmstate_info_float64 = {
1221 .name = "float64",
1222 .get = get_float64,
1223 .put = put_float64,
1226 /* timers */
1228 static int get_timer(QEMUFile *f, void *pv, size_t size)
1230 QEMUTimer *v = pv;
1231 qemu_get_timer(f, v);
1232 return 0;
1235 static void put_timer(QEMUFile *f, void *pv, size_t size)
1237 QEMUTimer *v = pv;
1238 qemu_put_timer(f, v);
1241 const VMStateInfo vmstate_info_timer = {
1242 .name = "timer",
1243 .get = get_timer,
1244 .put = put_timer,
1247 /* uint8_t buffers */
1249 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1251 uint8_t *v = pv;
1252 qemu_get_buffer(f, v, size);
1253 return 0;
1256 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1258 uint8_t *v = pv;
1259 qemu_put_buffer(f, v, size);
1262 const VMStateInfo vmstate_info_buffer = {
1263 .name = "buffer",
1264 .get = get_buffer,
1265 .put = put_buffer,
1268 /* unused buffers: space that was used for some fields that are
1269 not useful anymore */
1271 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1273 uint8_t buf[1024];
1274 int block_len;
1276 while (size > 0) {
1277 block_len = MIN(sizeof(buf), size);
1278 size -= block_len;
1279 qemu_get_buffer(f, buf, block_len);
1281 return 0;
1284 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1286 static const uint8_t buf[1024];
1287 int block_len;
1289 while (size > 0) {
1290 block_len = MIN(sizeof(buf), size);
1291 size -= block_len;
1292 qemu_put_buffer(f, buf, block_len);
1296 const VMStateInfo vmstate_info_unused_buffer = {
1297 .name = "unused_buffer",
1298 .get = get_unused_buffer,
1299 .put = put_unused_buffer,
1302 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1303 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1304 * bit words with the bits in big endian order. The in-memory format
1305 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1307 /* This is the number of 64 bit words sent over the wire */
1308 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1309 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1311 unsigned long *bmp = pv;
1312 int i, idx = 0;
1313 for (i = 0; i < BITS_TO_U64S(size); i++) {
1314 uint64_t w = qemu_get_be64(f);
1315 bmp[idx++] = w;
1316 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1317 bmp[idx++] = w >> 32;
1320 return 0;
1323 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1325 unsigned long *bmp = pv;
1326 int i, idx = 0;
1327 for (i = 0; i < BITS_TO_U64S(size); i++) {
1328 uint64_t w = bmp[idx++];
1329 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1330 w |= ((uint64_t)bmp[idx++]) << 32;
1332 qemu_put_be64(f, w);
1336 const VMStateInfo vmstate_info_bitmap = {
1337 .name = "bitmap",
1338 .get = get_bitmap,
1339 .put = put_bitmap,
1342 typedef struct CompatEntry {
1343 char idstr[256];
1344 int instance_id;
1345 } CompatEntry;
1347 typedef struct SaveStateEntry {
1348 QTAILQ_ENTRY(SaveStateEntry) entry;
1349 char idstr[256];
1350 int instance_id;
1351 int alias_id;
1352 int version_id;
1353 int section_id;
1354 SaveVMHandlers *ops;
1355 const VMStateDescription *vmsd;
1356 void *opaque;
1357 CompatEntry *compat;
1358 int no_migrate;
1359 int is_ram;
1360 } SaveStateEntry;
1363 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1364 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1365 static int global_section_id;
1367 static int calculate_new_instance_id(const char *idstr)
1369 SaveStateEntry *se;
1370 int instance_id = 0;
1372 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1373 if (strcmp(idstr, se->idstr) == 0
1374 && instance_id <= se->instance_id) {
1375 instance_id = se->instance_id + 1;
1378 return instance_id;
1381 static int calculate_compat_instance_id(const char *idstr)
1383 SaveStateEntry *se;
1384 int instance_id = 0;
1386 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1387 if (!se->compat)
1388 continue;
1390 if (strcmp(idstr, se->compat->idstr) == 0
1391 && instance_id <= se->compat->instance_id) {
1392 instance_id = se->compat->instance_id + 1;
1395 return instance_id;
1398 /* TODO: Individual devices generally have very little idea about the rest
1399 of the system, so instance_id should be removed/replaced.
1400 Meanwhile pass -1 as instance_id if you do not already have a clearly
1401 distinguishing id for all instances of your device class. */
1402 int register_savevm_live(DeviceState *dev,
1403 const char *idstr,
1404 int instance_id,
1405 int version_id,
1406 SaveVMHandlers *ops,
1407 void *opaque)
1409 SaveStateEntry *se;
1411 se = g_malloc0(sizeof(SaveStateEntry));
1412 se->version_id = version_id;
1413 se->section_id = global_section_id++;
1414 se->ops = ops;
1415 se->opaque = opaque;
1416 se->vmsd = NULL;
1417 se->no_migrate = 0;
1418 /* if this is a live_savem then set is_ram */
1419 if (ops->save_live_setup != NULL) {
1420 se->is_ram = 1;
1423 if (dev) {
1424 char *id = qdev_get_dev_path(dev);
1425 if (id) {
1426 pstrcpy(se->idstr, sizeof(se->idstr), id);
1427 pstrcat(se->idstr, sizeof(se->idstr), "/");
1428 g_free(id);
1430 se->compat = g_malloc0(sizeof(CompatEntry));
1431 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1432 se->compat->instance_id = instance_id == -1 ?
1433 calculate_compat_instance_id(idstr) : instance_id;
1434 instance_id = -1;
1437 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1439 if (instance_id == -1) {
1440 se->instance_id = calculate_new_instance_id(se->idstr);
1441 } else {
1442 se->instance_id = instance_id;
1444 assert(!se->compat || se->instance_id == 0);
1445 /* add at the end of list */
1446 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1447 return 0;
1450 int register_savevm(DeviceState *dev,
1451 const char *idstr,
1452 int instance_id,
1453 int version_id,
1454 SaveStateHandler *save_state,
1455 LoadStateHandler *load_state,
1456 void *opaque)
1458 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1459 ops->save_state = save_state;
1460 ops->load_state = load_state;
1461 return register_savevm_live(dev, idstr, instance_id, version_id,
1462 ops, opaque);
1465 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1467 SaveStateEntry *se, *new_se;
1468 char id[256] = "";
1470 if (dev) {
1471 char *path = qdev_get_dev_path(dev);
1472 if (path) {
1473 pstrcpy(id, sizeof(id), path);
1474 pstrcat(id, sizeof(id), "/");
1475 g_free(path);
1478 pstrcat(id, sizeof(id), idstr);
1480 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1481 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1482 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1483 if (se->compat) {
1484 g_free(se->compat);
1486 g_free(se->ops);
1487 g_free(se);
1492 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1493 const VMStateDescription *vmsd,
1494 void *opaque, int alias_id,
1495 int required_for_version)
1497 SaveStateEntry *se;
1499 /* If this triggers, alias support can be dropped for the vmsd. */
1500 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1502 se = g_malloc0(sizeof(SaveStateEntry));
1503 se->version_id = vmsd->version_id;
1504 se->section_id = global_section_id++;
1505 se->opaque = opaque;
1506 se->vmsd = vmsd;
1507 se->alias_id = alias_id;
1508 se->no_migrate = vmsd->unmigratable;
1510 if (dev) {
1511 char *id = qdev_get_dev_path(dev);
1512 if (id) {
1513 pstrcpy(se->idstr, sizeof(se->idstr), id);
1514 pstrcat(se->idstr, sizeof(se->idstr), "/");
1515 g_free(id);
1517 se->compat = g_malloc0(sizeof(CompatEntry));
1518 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1519 se->compat->instance_id = instance_id == -1 ?
1520 calculate_compat_instance_id(vmsd->name) : instance_id;
1521 instance_id = -1;
1524 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1526 if (instance_id == -1) {
1527 se->instance_id = calculate_new_instance_id(se->idstr);
1528 } else {
1529 se->instance_id = instance_id;
1531 assert(!se->compat || se->instance_id == 0);
1532 /* add at the end of list */
1533 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1534 return 0;
1537 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1538 void *opaque)
1540 SaveStateEntry *se, *new_se;
1542 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1543 if (se->vmsd == vmsd && se->opaque == opaque) {
1544 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1545 if (se->compat) {
1546 g_free(se->compat);
1548 g_free(se);
1553 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1554 void *opaque);
1555 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1556 void *opaque);
1558 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1559 void *opaque, int version_id)
1561 VMStateField *field = vmsd->fields;
1562 int ret;
1564 if (version_id > vmsd->version_id) {
1565 return -EINVAL;
1567 if (version_id < vmsd->minimum_version_id_old) {
1568 return -EINVAL;
1570 if (version_id < vmsd->minimum_version_id) {
1571 return vmsd->load_state_old(f, opaque, version_id);
1573 if (vmsd->pre_load) {
1574 int ret = vmsd->pre_load(opaque);
1575 if (ret)
1576 return ret;
1578 while(field->name) {
1579 if ((field->field_exists &&
1580 field->field_exists(opaque, version_id)) ||
1581 (!field->field_exists &&
1582 field->version_id <= version_id)) {
1583 void *base_addr = opaque + field->offset;
1584 int i, n_elems = 1;
1585 int size = field->size;
1587 if (field->flags & VMS_VBUFFER) {
1588 size = *(int32_t *)(opaque+field->size_offset);
1589 if (field->flags & VMS_MULTIPLY) {
1590 size *= field->size;
1593 if (field->flags & VMS_ARRAY) {
1594 n_elems = field->num;
1595 } else if (field->flags & VMS_VARRAY_INT32) {
1596 n_elems = *(int32_t *)(opaque+field->num_offset);
1597 } else if (field->flags & VMS_VARRAY_UINT32) {
1598 n_elems = *(uint32_t *)(opaque+field->num_offset);
1599 } else if (field->flags & VMS_VARRAY_UINT16) {
1600 n_elems = *(uint16_t *)(opaque+field->num_offset);
1601 } else if (field->flags & VMS_VARRAY_UINT8) {
1602 n_elems = *(uint8_t *)(opaque+field->num_offset);
1604 if (field->flags & VMS_POINTER) {
1605 base_addr = *(void **)base_addr + field->start;
1607 for (i = 0; i < n_elems; i++) {
1608 void *addr = base_addr + size * i;
1610 if (field->flags & VMS_ARRAY_OF_POINTER) {
1611 addr = *(void **)addr;
1613 if (field->flags & VMS_STRUCT) {
1614 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1615 } else {
1616 ret = field->info->get(f, addr, size);
1619 if (ret < 0) {
1620 return ret;
1624 field++;
1626 ret = vmstate_subsection_load(f, vmsd, opaque);
1627 if (ret != 0) {
1628 return ret;
1630 if (vmsd->post_load) {
1631 return vmsd->post_load(opaque, version_id);
1633 return 0;
1636 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1637 void *opaque)
1639 VMStateField *field = vmsd->fields;
1641 if (vmsd->pre_save) {
1642 vmsd->pre_save(opaque);
1644 while(field->name) {
1645 if (!field->field_exists ||
1646 field->field_exists(opaque, vmsd->version_id)) {
1647 void *base_addr = opaque + field->offset;
1648 int i, n_elems = 1;
1649 int size = field->size;
1651 if (field->flags & VMS_VBUFFER) {
1652 size = *(int32_t *)(opaque+field->size_offset);
1653 if (field->flags & VMS_MULTIPLY) {
1654 size *= field->size;
1657 if (field->flags & VMS_ARRAY) {
1658 n_elems = field->num;
1659 } else if (field->flags & VMS_VARRAY_INT32) {
1660 n_elems = *(int32_t *)(opaque+field->num_offset);
1661 } else if (field->flags & VMS_VARRAY_UINT32) {
1662 n_elems = *(uint32_t *)(opaque+field->num_offset);
1663 } else if (field->flags & VMS_VARRAY_UINT16) {
1664 n_elems = *(uint16_t *)(opaque+field->num_offset);
1665 } else if (field->flags & VMS_VARRAY_UINT8) {
1666 n_elems = *(uint8_t *)(opaque+field->num_offset);
1668 if (field->flags & VMS_POINTER) {
1669 base_addr = *(void **)base_addr + field->start;
1671 for (i = 0; i < n_elems; i++) {
1672 void *addr = base_addr + size * i;
1674 if (field->flags & VMS_ARRAY_OF_POINTER) {
1675 addr = *(void **)addr;
1677 if (field->flags & VMS_STRUCT) {
1678 vmstate_save_state(f, field->vmsd, addr);
1679 } else {
1680 field->info->put(f, addr, size);
1684 field++;
1686 vmstate_subsection_save(f, vmsd, opaque);
1689 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1691 if (!se->vmsd) { /* Old style */
1692 return se->ops->load_state(f, se->opaque, version_id);
1694 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1697 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1699 if (!se->vmsd) { /* Old style */
1700 se->ops->save_state(f, se->opaque);
1701 return;
1703 vmstate_save_state(f,se->vmsd, se->opaque);
1706 #define QEMU_VM_FILE_MAGIC 0x5145564d
1707 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1708 #define QEMU_VM_FILE_VERSION 0x00000003
1710 #define QEMU_VM_EOF 0x00
1711 #define QEMU_VM_SECTION_START 0x01
1712 #define QEMU_VM_SECTION_PART 0x02
1713 #define QEMU_VM_SECTION_END 0x03
1714 #define QEMU_VM_SECTION_FULL 0x04
1715 #define QEMU_VM_SUBSECTION 0x05
1717 bool qemu_savevm_state_blocked(Error **errp)
1719 SaveStateEntry *se;
1721 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1722 if (se->no_migrate) {
1723 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1724 return true;
1727 return false;
1730 void qemu_savevm_state_begin(QEMUFile *f,
1731 const MigrationParams *params)
1733 SaveStateEntry *se;
1734 int ret;
1736 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1737 if (!se->ops || !se->ops->set_params) {
1738 continue;
1740 se->ops->set_params(params, se->opaque);
1743 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1744 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1746 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1747 int len;
1749 if (!se->ops || !se->ops->save_live_setup) {
1750 continue;
1752 if (se->ops && se->ops->is_active) {
1753 if (!se->ops->is_active(se->opaque)) {
1754 continue;
1757 /* Section type */
1758 qemu_put_byte(f, QEMU_VM_SECTION_START);
1759 qemu_put_be32(f, se->section_id);
1761 /* ID string */
1762 len = strlen(se->idstr);
1763 qemu_put_byte(f, len);
1764 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1766 qemu_put_be32(f, se->instance_id);
1767 qemu_put_be32(f, se->version_id);
1769 ret = se->ops->save_live_setup(f, se->opaque);
1770 if (ret < 0) {
1771 qemu_file_set_error(f, ret);
1772 break;
1778 * this function has three return values:
1779 * negative: there was one error, and we have -errno.
1780 * 0 : We haven't finished, caller have to go again
1781 * 1 : We have finished, we can go to complete phase
1783 int qemu_savevm_state_iterate(QEMUFile *f)
1785 SaveStateEntry *se;
1786 int ret = 1;
1788 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1789 if (!se->ops || !se->ops->save_live_iterate) {
1790 continue;
1792 if (se->ops && se->ops->is_active) {
1793 if (!se->ops->is_active(se->opaque)) {
1794 continue;
1797 if (qemu_file_rate_limit(f)) {
1798 return 0;
1800 trace_savevm_section_start();
1801 /* Section type */
1802 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1803 qemu_put_be32(f, se->section_id);
1805 ret = se->ops->save_live_iterate(f, se->opaque);
1806 trace_savevm_section_end(se->section_id);
1808 if (ret < 0) {
1809 qemu_file_set_error(f, ret);
1811 if (ret <= 0) {
1812 /* Do not proceed to the next vmstate before this one reported
1813 completion of the current stage. This serializes the migration
1814 and reduces the probability that a faster changing state is
1815 synchronized over and over again. */
1816 break;
1819 return ret;
1822 void qemu_savevm_state_complete(QEMUFile *f)
1824 SaveStateEntry *se;
1825 int ret;
1827 cpu_synchronize_all_states();
1829 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1830 if (!se->ops || !se->ops->save_live_complete) {
1831 continue;
1833 if (se->ops && se->ops->is_active) {
1834 if (!se->ops->is_active(se->opaque)) {
1835 continue;
1838 trace_savevm_section_start();
1839 /* Section type */
1840 qemu_put_byte(f, QEMU_VM_SECTION_END);
1841 qemu_put_be32(f, se->section_id);
1843 ret = se->ops->save_live_complete(f, se->opaque);
1844 trace_savevm_section_end(se->section_id);
1845 if (ret < 0) {
1846 qemu_file_set_error(f, ret);
1847 return;
1851 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1852 int len;
1854 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1855 continue;
1857 trace_savevm_section_start();
1858 /* Section type */
1859 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1860 qemu_put_be32(f, se->section_id);
1862 /* ID string */
1863 len = strlen(se->idstr);
1864 qemu_put_byte(f, len);
1865 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1867 qemu_put_be32(f, se->instance_id);
1868 qemu_put_be32(f, se->version_id);
1870 vmstate_save(f, se);
1871 trace_savevm_section_end(se->section_id);
1874 qemu_put_byte(f, QEMU_VM_EOF);
1875 qemu_fflush(f);
1878 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1880 SaveStateEntry *se;
1881 uint64_t ret = 0;
1883 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1884 if (!se->ops || !se->ops->save_live_pending) {
1885 continue;
1887 if (se->ops && se->ops->is_active) {
1888 if (!se->ops->is_active(se->opaque)) {
1889 continue;
1892 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1894 return ret;
1897 void qemu_savevm_state_cancel(void)
1899 SaveStateEntry *se;
1901 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1902 if (se->ops && se->ops->cancel) {
1903 se->ops->cancel(se->opaque);
1908 static int qemu_savevm_state(QEMUFile *f)
1910 int ret;
1911 MigrationParams params = {
1912 .blk = 0,
1913 .shared = 0
1916 if (qemu_savevm_state_blocked(NULL)) {
1917 return -EINVAL;
1920 qemu_mutex_unlock_iothread();
1921 qemu_savevm_state_begin(f, &params);
1922 qemu_mutex_lock_iothread();
1924 while (qemu_file_get_error(f) == 0) {
1925 if (qemu_savevm_state_iterate(f) > 0) {
1926 break;
1930 ret = qemu_file_get_error(f);
1931 if (ret == 0) {
1932 qemu_savevm_state_complete(f);
1933 ret = qemu_file_get_error(f);
1935 if (ret != 0) {
1936 qemu_savevm_state_cancel();
1938 return ret;
1941 static int qemu_save_device_state(QEMUFile *f)
1943 SaveStateEntry *se;
1945 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1946 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1948 cpu_synchronize_all_states();
1950 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1951 int len;
1953 if (se->is_ram) {
1954 continue;
1956 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1957 continue;
1960 /* Section type */
1961 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1962 qemu_put_be32(f, se->section_id);
1964 /* ID string */
1965 len = strlen(se->idstr);
1966 qemu_put_byte(f, len);
1967 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1969 qemu_put_be32(f, se->instance_id);
1970 qemu_put_be32(f, se->version_id);
1972 vmstate_save(f, se);
1975 qemu_put_byte(f, QEMU_VM_EOF);
1977 return qemu_file_get_error(f);
1980 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1982 SaveStateEntry *se;
1984 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1985 if (!strcmp(se->idstr, idstr) &&
1986 (instance_id == se->instance_id ||
1987 instance_id == se->alias_id))
1988 return se;
1989 /* Migrating from an older version? */
1990 if (strstr(se->idstr, idstr) && se->compat) {
1991 if (!strcmp(se->compat->idstr, idstr) &&
1992 (instance_id == se->compat->instance_id ||
1993 instance_id == se->alias_id))
1994 return se;
1997 return NULL;
2000 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2002 while(sub && sub->needed) {
2003 if (strcmp(idstr, sub->vmsd->name) == 0) {
2004 return sub->vmsd;
2006 sub++;
2008 return NULL;
2011 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2012 void *opaque)
2014 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
2015 char idstr[256];
2016 int ret;
2017 uint8_t version_id, len, size;
2018 const VMStateDescription *sub_vmsd;
2020 len = qemu_peek_byte(f, 1);
2021 if (len < strlen(vmsd->name) + 1) {
2022 /* subsection name has be be "section_name/a" */
2023 return 0;
2025 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2026 if (size != len) {
2027 return 0;
2029 idstr[size] = 0;
2031 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2032 /* it don't have a valid subsection name */
2033 return 0;
2035 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
2036 if (sub_vmsd == NULL) {
2037 return -ENOENT;
2039 qemu_file_skip(f, 1); /* subsection */
2040 qemu_file_skip(f, 1); /* len */
2041 qemu_file_skip(f, len); /* idstr */
2042 version_id = qemu_get_be32(f);
2044 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2045 if (ret) {
2046 return ret;
2049 return 0;
2052 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2053 void *opaque)
2055 const VMStateSubsection *sub = vmsd->subsections;
2057 while (sub && sub->needed) {
2058 if (sub->needed(opaque)) {
2059 const VMStateDescription *vmsd = sub->vmsd;
2060 uint8_t len;
2062 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2063 len = strlen(vmsd->name);
2064 qemu_put_byte(f, len);
2065 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2066 qemu_put_be32(f, vmsd->version_id);
2067 vmstate_save_state(f, vmsd, opaque);
2069 sub++;
2073 typedef struct LoadStateEntry {
2074 QLIST_ENTRY(LoadStateEntry) entry;
2075 SaveStateEntry *se;
2076 int section_id;
2077 int version_id;
2078 } LoadStateEntry;
2080 int qemu_loadvm_state(QEMUFile *f)
2082 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2083 QLIST_HEAD_INITIALIZER(loadvm_handlers);
2084 LoadStateEntry *le, *new_le;
2085 uint8_t section_type;
2086 unsigned int v;
2087 int ret;
2089 if (qemu_savevm_state_blocked(NULL)) {
2090 return -EINVAL;
2093 v = qemu_get_be32(f);
2094 if (v != QEMU_VM_FILE_MAGIC)
2095 return -EINVAL;
2097 v = qemu_get_be32(f);
2098 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2099 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2100 return -ENOTSUP;
2102 if (v != QEMU_VM_FILE_VERSION)
2103 return -ENOTSUP;
2105 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2106 uint32_t instance_id, version_id, section_id;
2107 SaveStateEntry *se;
2108 char idstr[257];
2109 int len;
2111 switch (section_type) {
2112 case QEMU_VM_SECTION_START:
2113 case QEMU_VM_SECTION_FULL:
2114 /* Read section start */
2115 section_id = qemu_get_be32(f);
2116 len = qemu_get_byte(f);
2117 qemu_get_buffer(f, (uint8_t *)idstr, len);
2118 idstr[len] = 0;
2119 instance_id = qemu_get_be32(f);
2120 version_id = qemu_get_be32(f);
2122 /* Find savevm section */
2123 se = find_se(idstr, instance_id);
2124 if (se == NULL) {
2125 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2126 ret = -EINVAL;
2127 goto out;
2130 /* Validate version */
2131 if (version_id > se->version_id) {
2132 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2133 version_id, idstr, se->version_id);
2134 ret = -EINVAL;
2135 goto out;
2138 /* Add entry */
2139 le = g_malloc0(sizeof(*le));
2141 le->se = se;
2142 le->section_id = section_id;
2143 le->version_id = version_id;
2144 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2146 ret = vmstate_load(f, le->se, le->version_id);
2147 if (ret < 0) {
2148 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2149 instance_id, idstr);
2150 goto out;
2152 break;
2153 case QEMU_VM_SECTION_PART:
2154 case QEMU_VM_SECTION_END:
2155 section_id = qemu_get_be32(f);
2157 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2158 if (le->section_id == section_id) {
2159 break;
2162 if (le == NULL) {
2163 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2164 ret = -EINVAL;
2165 goto out;
2168 ret = vmstate_load(f, le->se, le->version_id);
2169 if (ret < 0) {
2170 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2171 section_id);
2172 goto out;
2174 break;
2175 default:
2176 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2177 ret = -EINVAL;
2178 goto out;
2182 cpu_synchronize_all_post_init();
2184 ret = 0;
2186 out:
2187 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2188 QLIST_REMOVE(le, entry);
2189 g_free(le);
2192 if (ret == 0) {
2193 ret = qemu_file_get_error(f);
2196 return ret;
2199 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2200 const char *name)
2202 QEMUSnapshotInfo *sn_tab, *sn;
2203 int nb_sns, i, ret;
2205 ret = -ENOENT;
2206 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2207 if (nb_sns < 0)
2208 return ret;
2209 for(i = 0; i < nb_sns; i++) {
2210 sn = &sn_tab[i];
2211 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2212 *sn_info = *sn;
2213 ret = 0;
2214 break;
2217 g_free(sn_tab);
2218 return ret;
2222 * Deletes snapshots of a given name in all opened images.
2224 static int del_existing_snapshots(Monitor *mon, const char *name)
2226 BlockDriverState *bs;
2227 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2228 int ret;
2230 bs = NULL;
2231 while ((bs = bdrv_next(bs))) {
2232 if (bdrv_can_snapshot(bs) &&
2233 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2235 ret = bdrv_snapshot_delete(bs, name);
2236 if (ret < 0) {
2237 monitor_printf(mon,
2238 "Error while deleting snapshot on '%s'\n",
2239 bdrv_get_device_name(bs));
2240 return -1;
2245 return 0;
2248 void do_savevm(Monitor *mon, const QDict *qdict)
2250 BlockDriverState *bs, *bs1;
2251 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2252 int ret;
2253 QEMUFile *f;
2254 int saved_vm_running;
2255 uint64_t vm_state_size;
2256 qemu_timeval tv;
2257 struct tm tm;
2258 const char *name = qdict_get_try_str(qdict, "name");
2260 /* Verify if there is a device that doesn't support snapshots and is writable */
2261 bs = NULL;
2262 while ((bs = bdrv_next(bs))) {
2264 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2265 continue;
2268 if (!bdrv_can_snapshot(bs)) {
2269 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2270 bdrv_get_device_name(bs));
2271 return;
2275 bs = bdrv_snapshots();
2276 if (!bs) {
2277 monitor_printf(mon, "No block device can accept snapshots\n");
2278 return;
2281 saved_vm_running = runstate_is_running();
2282 vm_stop(RUN_STATE_SAVE_VM);
2284 memset(sn, 0, sizeof(*sn));
2286 /* fill auxiliary fields */
2287 qemu_gettimeofday(&tv);
2288 sn->date_sec = tv.tv_sec;
2289 sn->date_nsec = tv.tv_usec * 1000;
2290 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2292 if (name) {
2293 ret = bdrv_snapshot_find(bs, old_sn, name);
2294 if (ret >= 0) {
2295 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2296 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2297 } else {
2298 pstrcpy(sn->name, sizeof(sn->name), name);
2300 } else {
2301 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2302 localtime_r((const time_t *)&tv.tv_sec, &tm);
2303 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2306 /* Delete old snapshots of the same name */
2307 if (name && del_existing_snapshots(mon, name) < 0) {
2308 goto the_end;
2311 /* save the VM state */
2312 f = qemu_fopen_bdrv(bs, 1);
2313 if (!f) {
2314 monitor_printf(mon, "Could not open VM state file\n");
2315 goto the_end;
2317 ret = qemu_savevm_state(f);
2318 vm_state_size = qemu_ftell(f);
2319 qemu_fclose(f);
2320 if (ret < 0) {
2321 monitor_printf(mon, "Error %d while writing VM\n", ret);
2322 goto the_end;
2325 /* create the snapshots */
2327 bs1 = NULL;
2328 while ((bs1 = bdrv_next(bs1))) {
2329 if (bdrv_can_snapshot(bs1)) {
2330 /* Write VM state size only to the image that contains the state */
2331 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2332 ret = bdrv_snapshot_create(bs1, sn);
2333 if (ret < 0) {
2334 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2335 bdrv_get_device_name(bs1));
2340 the_end:
2341 if (saved_vm_running)
2342 vm_start();
2345 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2347 QEMUFile *f;
2348 int saved_vm_running;
2349 int ret;
2351 saved_vm_running = runstate_is_running();
2352 vm_stop(RUN_STATE_SAVE_VM);
2354 f = qemu_fopen(filename, "wb");
2355 if (!f) {
2356 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2357 goto the_end;
2359 ret = qemu_save_device_state(f);
2360 qemu_fclose(f);
2361 if (ret < 0) {
2362 error_set(errp, QERR_IO_ERROR);
2365 the_end:
2366 if (saved_vm_running)
2367 vm_start();
2370 int load_vmstate(const char *name)
2372 BlockDriverState *bs, *bs_vm_state;
2373 QEMUSnapshotInfo sn;
2374 QEMUFile *f;
2375 int ret;
2377 bs_vm_state = bdrv_snapshots();
2378 if (!bs_vm_state) {
2379 error_report("No block device supports snapshots");
2380 return -ENOTSUP;
2383 /* Don't even try to load empty VM states */
2384 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2385 if (ret < 0) {
2386 return ret;
2387 } else if (sn.vm_state_size == 0) {
2388 error_report("This is a disk-only snapshot. Revert to it offline "
2389 "using qemu-img.");
2390 return -EINVAL;
2393 /* Verify if there is any device that doesn't support snapshots and is
2394 writable and check if the requested snapshot is available too. */
2395 bs = NULL;
2396 while ((bs = bdrv_next(bs))) {
2398 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2399 continue;
2402 if (!bdrv_can_snapshot(bs)) {
2403 error_report("Device '%s' is writable but does not support snapshots.",
2404 bdrv_get_device_name(bs));
2405 return -ENOTSUP;
2408 ret = bdrv_snapshot_find(bs, &sn, name);
2409 if (ret < 0) {
2410 error_report("Device '%s' does not have the requested snapshot '%s'",
2411 bdrv_get_device_name(bs), name);
2412 return ret;
2416 /* Flush all IO requests so they don't interfere with the new state. */
2417 bdrv_drain_all();
2419 bs = NULL;
2420 while ((bs = bdrv_next(bs))) {
2421 if (bdrv_can_snapshot(bs)) {
2422 ret = bdrv_snapshot_goto(bs, name);
2423 if (ret < 0) {
2424 error_report("Error %d while activating snapshot '%s' on '%s'",
2425 ret, name, bdrv_get_device_name(bs));
2426 return ret;
2431 /* restore the VM state */
2432 f = qemu_fopen_bdrv(bs_vm_state, 0);
2433 if (!f) {
2434 error_report("Could not open VM state file");
2435 return -EINVAL;
2438 qemu_system_reset(VMRESET_SILENT);
2439 ret = qemu_loadvm_state(f);
2441 qemu_fclose(f);
2442 if (ret < 0) {
2443 error_report("Error %d while loading VM state", ret);
2444 return ret;
2447 return 0;
2450 void do_delvm(Monitor *mon, const QDict *qdict)
2452 BlockDriverState *bs, *bs1;
2453 int ret;
2454 const char *name = qdict_get_str(qdict, "name");
2456 bs = bdrv_snapshots();
2457 if (!bs) {
2458 monitor_printf(mon, "No block device supports snapshots\n");
2459 return;
2462 bs1 = NULL;
2463 while ((bs1 = bdrv_next(bs1))) {
2464 if (bdrv_can_snapshot(bs1)) {
2465 ret = bdrv_snapshot_delete(bs1, name);
2466 if (ret < 0) {
2467 if (ret == -ENOTSUP)
2468 monitor_printf(mon,
2469 "Snapshots not supported on device '%s'\n",
2470 bdrv_get_device_name(bs1));
2471 else
2472 monitor_printf(mon, "Error %d while deleting snapshot on "
2473 "'%s'\n", ret, bdrv_get_device_name(bs1));
2479 void do_info_snapshots(Monitor *mon, const QDict *qdict)
2481 BlockDriverState *bs, *bs1;
2482 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2483 int nb_sns, i, ret, available;
2484 int total;
2485 int *available_snapshots;
2486 char buf[256];
2488 bs = bdrv_snapshots();
2489 if (!bs) {
2490 monitor_printf(mon, "No available block device supports snapshots\n");
2491 return;
2494 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2495 if (nb_sns < 0) {
2496 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2497 return;
2500 if (nb_sns == 0) {
2501 monitor_printf(mon, "There is no snapshot available.\n");
2502 return;
2505 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2506 total = 0;
2507 for (i = 0; i < nb_sns; i++) {
2508 sn = &sn_tab[i];
2509 available = 1;
2510 bs1 = NULL;
2512 while ((bs1 = bdrv_next(bs1))) {
2513 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2514 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2515 if (ret < 0) {
2516 available = 0;
2517 break;
2522 if (available) {
2523 available_snapshots[total] = i;
2524 total++;
2528 if (total > 0) {
2529 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2530 for (i = 0; i < total; i++) {
2531 sn = &sn_tab[available_snapshots[i]];
2532 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2534 } else {
2535 monitor_printf(mon, "There is no suitable snapshot available\n");
2538 g_free(sn_tab);
2539 g_free(available_snapshots);
2543 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2545 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2546 memory_region_name(mr), dev);
2549 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2551 /* Nothing do to while the implementation is in RAMBlock */
2554 void vmstate_register_ram_global(MemoryRegion *mr)
2556 vmstate_register_ram(mr, NULL);