qemu-file: drop socket_put_buffer
[qemu.git] / savevm.c
blobffabbff12c41e9e984eb8b283f9c4ceecd297aec
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "config-host.h"
26 #include "qemu-common.h"
27 #include "hw/hw.h"
28 #include "hw/qdev.h"
29 #include "net/net.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
40 #include "trace.h"
41 #include "qemu/bitops.h"
42 #include "qemu/iov.h"
44 #define SELF_ANNOUNCE_ROUNDS 5
46 #ifndef ETH_P_RARP
47 #define ETH_P_RARP 0x8035
48 #endif
49 #define ARP_HTYPE_ETH 0x0001
50 #define ARP_PTYPE_IP 0x0800
51 #define ARP_OP_REQUEST_REV 0x3
53 static int announce_self_create(uint8_t *buf,
54 uint8_t *mac_addr)
56 /* Ethernet header. */
57 memset(buf, 0xff, 6); /* destination MAC addr */
58 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
59 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
61 /* RARP header. */
62 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
63 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
64 *(buf + 18) = 6; /* hardware addr length (ethernet) */
65 *(buf + 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
67 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
68 memset(buf + 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
70 memset(buf + 38, 0x00, 4); /* target protocol addr */
72 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf + 42, 0x00, 18);
75 return 60; /* len (FCS will be added by hardware) */
78 static void qemu_announce_self_iter(NICState *nic, void *opaque)
80 uint8_t buf[60];
81 int len;
83 len = announce_self_create(buf, nic->conf->macaddr.a);
85 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
89 static void qemu_announce_self_once(void *opaque)
91 static int count = SELF_ANNOUNCE_ROUNDS;
92 QEMUTimer *timer = *(QEMUTimer **)opaque;
94 qemu_foreach_nic(qemu_announce_self_iter, NULL);
96 if (--count) {
97 /* delay 50ms, 150ms, 250ms, ... */
98 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
99 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
100 } else {
101 qemu_del_timer(timer);
102 qemu_free_timer(timer);
106 void qemu_announce_self(void)
108 static QEMUTimer *timer;
109 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
110 qemu_announce_self_once(&timer);
113 /***********************************************************/
114 /* savevm/loadvm support */
116 #define IO_BUF_SIZE 32768
117 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
119 struct QEMUFile {
120 const QEMUFileOps *ops;
121 void *opaque;
123 int64_t bytes_xfer;
124 int64_t xfer_limit;
126 int64_t pos; /* start of buffer when writing, end of buffer
127 when reading */
128 int buf_index;
129 int buf_size; /* 0 when writing */
130 uint8_t buf[IO_BUF_SIZE];
132 struct iovec iov[MAX_IOV_SIZE];
133 unsigned int iovcnt;
135 int last_error;
138 typedef struct QEMUFileStdio
140 FILE *stdio_file;
141 QEMUFile *file;
142 } QEMUFileStdio;
144 typedef struct QEMUFileSocket
146 int fd;
147 QEMUFile *file;
148 } QEMUFileSocket;
150 typedef struct {
151 Coroutine *co;
152 int fd;
153 } FDYieldUntilData;
155 static void fd_coroutine_enter(void *opaque)
157 FDYieldUntilData *data = opaque;
158 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
159 qemu_coroutine_enter(data->co, NULL);
163 * Yield until a file descriptor becomes readable
165 * Note that this function clobbers the handlers for the file descriptor.
167 static void coroutine_fn yield_until_fd_readable(int fd)
169 FDYieldUntilData data;
171 assert(qemu_in_coroutine());
172 data.co = qemu_coroutine_self();
173 data.fd = fd;
174 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
175 qemu_coroutine_yield();
178 static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
179 int64_t pos)
181 QEMUFileSocket *s = opaque;
182 ssize_t len;
183 ssize_t size = iov_size(iov, iovcnt);
185 len = iov_send(s->fd, iov, iovcnt, 0, size);
186 if (len < size) {
187 len = -socket_error();
189 return len;
192 static int socket_get_fd(void *opaque)
194 QEMUFileSocket *s = opaque;
196 return s->fd;
199 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
201 QEMUFileSocket *s = opaque;
202 ssize_t len;
204 for (;;) {
205 len = qemu_recv(s->fd, buf, size, 0);
206 if (len != -1) {
207 break;
209 if (socket_error() == EAGAIN) {
210 yield_until_fd_readable(s->fd);
211 } else if (socket_error() != EINTR) {
212 break;
216 if (len == -1) {
217 len = -socket_error();
219 return len;
222 static int socket_close(void *opaque)
224 QEMUFileSocket *s = opaque;
225 closesocket(s->fd);
226 g_free(s);
227 return 0;
230 static int stdio_get_fd(void *opaque)
232 QEMUFileStdio *s = opaque;
234 return fileno(s->stdio_file);
237 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
239 QEMUFileStdio *s = opaque;
240 return fwrite(buf, 1, size, s->stdio_file);
243 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
245 QEMUFileStdio *s = opaque;
246 FILE *fp = s->stdio_file;
247 int bytes;
249 for (;;) {
250 clearerr(fp);
251 bytes = fread(buf, 1, size, fp);
252 if (bytes != 0 || !ferror(fp)) {
253 break;
255 if (errno == EAGAIN) {
256 yield_until_fd_readable(fileno(fp));
257 } else if (errno != EINTR) {
258 break;
261 return bytes;
264 static int stdio_pclose(void *opaque)
266 QEMUFileStdio *s = opaque;
267 int ret;
268 ret = pclose(s->stdio_file);
269 if (ret == -1) {
270 ret = -errno;
271 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
272 /* close succeeded, but non-zero exit code: */
273 ret = -EIO; /* fake errno value */
275 g_free(s);
276 return ret;
279 static int stdio_fclose(void *opaque)
281 QEMUFileStdio *s = opaque;
282 int ret = 0;
284 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
285 int fd = fileno(s->stdio_file);
286 struct stat st;
288 ret = fstat(fd, &st);
289 if (ret == 0 && S_ISREG(st.st_mode)) {
291 * If the file handle is a regular file make sure the
292 * data is flushed to disk before signaling success.
294 ret = fsync(fd);
295 if (ret != 0) {
296 ret = -errno;
297 return ret;
301 if (fclose(s->stdio_file) == EOF) {
302 ret = -errno;
304 g_free(s);
305 return ret;
308 static const QEMUFileOps stdio_pipe_read_ops = {
309 .get_fd = stdio_get_fd,
310 .get_buffer = stdio_get_buffer,
311 .close = stdio_pclose
314 static const QEMUFileOps stdio_pipe_write_ops = {
315 .get_fd = stdio_get_fd,
316 .put_buffer = stdio_put_buffer,
317 .close = stdio_pclose
320 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
322 FILE *stdio_file;
323 QEMUFileStdio *s;
325 stdio_file = popen(command, mode);
326 if (stdio_file == NULL) {
327 return NULL;
330 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
331 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
332 return NULL;
335 s = g_malloc0(sizeof(QEMUFileStdio));
337 s->stdio_file = stdio_file;
339 if(mode[0] == 'r') {
340 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
341 } else {
342 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
344 return s->file;
347 static const QEMUFileOps stdio_file_read_ops = {
348 .get_fd = stdio_get_fd,
349 .get_buffer = stdio_get_buffer,
350 .close = stdio_fclose
353 static const QEMUFileOps stdio_file_write_ops = {
354 .get_fd = stdio_get_fd,
355 .put_buffer = stdio_put_buffer,
356 .close = stdio_fclose
359 QEMUFile *qemu_fdopen(int fd, const char *mode)
361 QEMUFileStdio *s;
363 if (mode == NULL ||
364 (mode[0] != 'r' && mode[0] != 'w') ||
365 mode[1] != 'b' || mode[2] != 0) {
366 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
367 return NULL;
370 s = g_malloc0(sizeof(QEMUFileStdio));
371 s->stdio_file = fdopen(fd, mode);
372 if (!s->stdio_file)
373 goto fail;
375 if(mode[0] == 'r') {
376 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
377 } else {
378 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
380 return s->file;
382 fail:
383 g_free(s);
384 return NULL;
387 static const QEMUFileOps socket_read_ops = {
388 .get_fd = socket_get_fd,
389 .get_buffer = socket_get_buffer,
390 .close = socket_close
393 static const QEMUFileOps socket_write_ops = {
394 .get_fd = socket_get_fd,
395 .writev_buffer = socket_writev_buffer,
396 .close = socket_close
399 QEMUFile *qemu_fopen_socket(int fd, const char *mode)
401 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
403 if (mode == NULL ||
404 (mode[0] != 'r' && mode[0] != 'w') ||
405 mode[1] != 'b' || mode[2] != 0) {
406 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
407 return NULL;
410 s->fd = fd;
411 if (mode[0] == 'w') {
412 qemu_set_block(s->fd);
413 s->file = qemu_fopen_ops(s, &socket_write_ops);
414 } else {
415 s->file = qemu_fopen_ops(s, &socket_read_ops);
417 return s->file;
420 QEMUFile *qemu_fopen(const char *filename, const char *mode)
422 QEMUFileStdio *s;
424 if (mode == NULL ||
425 (mode[0] != 'r' && mode[0] != 'w') ||
426 mode[1] != 'b' || mode[2] != 0) {
427 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
428 return NULL;
431 s = g_malloc0(sizeof(QEMUFileStdio));
433 s->stdio_file = fopen(filename, mode);
434 if (!s->stdio_file)
435 goto fail;
437 if(mode[0] == 'w') {
438 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
439 } else {
440 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
442 return s->file;
443 fail:
444 g_free(s);
445 return NULL;
448 static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
449 int64_t pos)
451 int ret;
452 QEMUIOVector qiov;
454 qemu_iovec_init_external(&qiov, iov, iovcnt);
455 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
456 if (ret < 0) {
457 return ret;
460 return qiov.size;
463 static int block_put_buffer(void *opaque, const uint8_t *buf,
464 int64_t pos, int size)
466 bdrv_save_vmstate(opaque, buf, pos, size);
467 return size;
470 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
472 return bdrv_load_vmstate(opaque, buf, pos, size);
475 static int bdrv_fclose(void *opaque)
477 return bdrv_flush(opaque);
480 static const QEMUFileOps bdrv_read_ops = {
481 .get_buffer = block_get_buffer,
482 .close = bdrv_fclose
485 static const QEMUFileOps bdrv_write_ops = {
486 .put_buffer = block_put_buffer,
487 .writev_buffer = block_writev_buffer,
488 .close = bdrv_fclose
491 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
493 if (is_writable)
494 return qemu_fopen_ops(bs, &bdrv_write_ops);
495 return qemu_fopen_ops(bs, &bdrv_read_ops);
498 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
500 QEMUFile *f;
502 f = g_malloc0(sizeof(QEMUFile));
504 f->opaque = opaque;
505 f->ops = ops;
506 return f;
509 int qemu_file_get_error(QEMUFile *f)
511 return f->last_error;
514 static void qemu_file_set_error(QEMUFile *f, int ret)
516 if (f->last_error == 0) {
517 f->last_error = ret;
521 static inline bool qemu_file_is_writable(QEMUFile *f)
523 return f->ops->writev_buffer || f->ops->put_buffer;
527 * Flushes QEMUFile buffer
529 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
530 * put_buffer ops.
532 static void qemu_fflush(QEMUFile *f)
534 ssize_t ret = 0;
536 if (!qemu_file_is_writable(f)) {
537 return;
540 if (f->ops->writev_buffer) {
541 if (f->iovcnt > 0) {
542 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
544 } else {
545 if (f->buf_index > 0) {
546 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
549 if (ret >= 0) {
550 f->pos += ret;
552 f->buf_index = 0;
553 f->iovcnt = 0;
554 if (ret < 0) {
555 qemu_file_set_error(f, ret);
559 static void qemu_fill_buffer(QEMUFile *f)
561 int len;
562 int pending;
564 assert(!qemu_file_is_writable(f));
566 pending = f->buf_size - f->buf_index;
567 if (pending > 0) {
568 memmove(f->buf, f->buf + f->buf_index, pending);
570 f->buf_index = 0;
571 f->buf_size = pending;
573 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
574 IO_BUF_SIZE - pending);
575 if (len > 0) {
576 f->buf_size += len;
577 f->pos += len;
578 } else if (len == 0) {
579 qemu_file_set_error(f, -EIO);
580 } else if (len != -EAGAIN)
581 qemu_file_set_error(f, len);
584 int qemu_get_fd(QEMUFile *f)
586 if (f->ops->get_fd) {
587 return f->ops->get_fd(f->opaque);
589 return -1;
592 /** Closes the file
594 * Returns negative error value if any error happened on previous operations or
595 * while closing the file. Returns 0 or positive number on success.
597 * The meaning of return value on success depends on the specific backend
598 * being used.
600 int qemu_fclose(QEMUFile *f)
602 int ret;
603 qemu_fflush(f);
604 ret = qemu_file_get_error(f);
606 if (f->ops->close) {
607 int ret2 = f->ops->close(f->opaque);
608 if (ret >= 0) {
609 ret = ret2;
612 /* If any error was spotted before closing, we should report it
613 * instead of the close() return value.
615 if (f->last_error) {
616 ret = f->last_error;
618 g_free(f);
619 return ret;
622 static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
624 /* check for adjacent buffer and coalesce them */
625 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
626 f->iov[f->iovcnt - 1].iov_len) {
627 f->iov[f->iovcnt - 1].iov_len += size;
628 } else {
629 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
630 f->iov[f->iovcnt++].iov_len = size;
633 if (f->iovcnt >= MAX_IOV_SIZE) {
634 qemu_fflush(f);
638 void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
640 if (!f->ops->writev_buffer) {
641 qemu_put_buffer(f, buf, size);
642 return;
645 if (f->last_error) {
646 return;
649 f->bytes_xfer += size;
650 add_to_iovec(f, buf, size);
653 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
655 int l;
657 if (f->last_error) {
658 return;
661 while (size > 0) {
662 l = IO_BUF_SIZE - f->buf_index;
663 if (l > size)
664 l = size;
665 memcpy(f->buf + f->buf_index, buf, l);
666 f->bytes_xfer += size;
667 if (f->ops->writev_buffer) {
668 add_to_iovec(f, f->buf + f->buf_index, l);
670 f->buf_index += l;
671 if (f->buf_index == IO_BUF_SIZE) {
672 qemu_fflush(f);
674 if (qemu_file_get_error(f)) {
675 break;
677 buf += l;
678 size -= l;
682 void qemu_put_byte(QEMUFile *f, int v)
684 if (f->last_error) {
685 return;
688 f->buf[f->buf_index] = v;
689 f->bytes_xfer++;
690 if (f->ops->writev_buffer) {
691 add_to_iovec(f, f->buf + f->buf_index, 1);
693 f->buf_index++;
694 if (f->buf_index == IO_BUF_SIZE) {
695 qemu_fflush(f);
699 static void qemu_file_skip(QEMUFile *f, int size)
701 if (f->buf_index + size <= f->buf_size) {
702 f->buf_index += size;
706 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
708 int pending;
709 int index;
711 assert(!qemu_file_is_writable(f));
713 index = f->buf_index + offset;
714 pending = f->buf_size - index;
715 if (pending < size) {
716 qemu_fill_buffer(f);
717 index = f->buf_index + offset;
718 pending = f->buf_size - index;
721 if (pending <= 0) {
722 return 0;
724 if (size > pending) {
725 size = pending;
728 memcpy(buf, f->buf + index, size);
729 return size;
732 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
734 int pending = size;
735 int done = 0;
737 while (pending > 0) {
738 int res;
740 res = qemu_peek_buffer(f, buf, pending, 0);
741 if (res == 0) {
742 return done;
744 qemu_file_skip(f, res);
745 buf += res;
746 pending -= res;
747 done += res;
749 return done;
752 static int qemu_peek_byte(QEMUFile *f, int offset)
754 int index = f->buf_index + offset;
756 assert(!qemu_file_is_writable(f));
758 if (index >= f->buf_size) {
759 qemu_fill_buffer(f);
760 index = f->buf_index + offset;
761 if (index >= f->buf_size) {
762 return 0;
765 return f->buf[index];
768 int qemu_get_byte(QEMUFile *f)
770 int result;
772 result = qemu_peek_byte(f, 0);
773 qemu_file_skip(f, 1);
774 return result;
777 int64_t qemu_ftell(QEMUFile *f)
779 qemu_fflush(f);
780 return f->pos;
783 int qemu_file_rate_limit(QEMUFile *f)
785 if (qemu_file_get_error(f)) {
786 return 1;
788 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
789 return 1;
791 return 0;
794 int64_t qemu_file_get_rate_limit(QEMUFile *f)
796 return f->xfer_limit;
799 void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
801 f->xfer_limit = limit;
804 void qemu_file_reset_rate_limit(QEMUFile *f)
806 f->bytes_xfer = 0;
809 void qemu_put_be16(QEMUFile *f, unsigned int v)
811 qemu_put_byte(f, v >> 8);
812 qemu_put_byte(f, v);
815 void qemu_put_be32(QEMUFile *f, unsigned int v)
817 qemu_put_byte(f, v >> 24);
818 qemu_put_byte(f, v >> 16);
819 qemu_put_byte(f, v >> 8);
820 qemu_put_byte(f, v);
823 void qemu_put_be64(QEMUFile *f, uint64_t v)
825 qemu_put_be32(f, v >> 32);
826 qemu_put_be32(f, v);
829 unsigned int qemu_get_be16(QEMUFile *f)
831 unsigned int v;
832 v = qemu_get_byte(f) << 8;
833 v |= qemu_get_byte(f);
834 return v;
837 unsigned int qemu_get_be32(QEMUFile *f)
839 unsigned int v;
840 v = qemu_get_byte(f) << 24;
841 v |= qemu_get_byte(f) << 16;
842 v |= qemu_get_byte(f) << 8;
843 v |= qemu_get_byte(f);
844 return v;
847 uint64_t qemu_get_be64(QEMUFile *f)
849 uint64_t v;
850 v = (uint64_t)qemu_get_be32(f) << 32;
851 v |= qemu_get_be32(f);
852 return v;
856 /* timer */
858 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
860 uint64_t expire_time;
862 expire_time = qemu_timer_expire_time_ns(ts);
863 qemu_put_be64(f, expire_time);
866 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
868 uint64_t expire_time;
870 expire_time = qemu_get_be64(f);
871 if (expire_time != -1) {
872 qemu_mod_timer_ns(ts, expire_time);
873 } else {
874 qemu_del_timer(ts);
879 /* bool */
881 static int get_bool(QEMUFile *f, void *pv, size_t size)
883 bool *v = pv;
884 *v = qemu_get_byte(f);
885 return 0;
888 static void put_bool(QEMUFile *f, void *pv, size_t size)
890 bool *v = pv;
891 qemu_put_byte(f, *v);
894 const VMStateInfo vmstate_info_bool = {
895 .name = "bool",
896 .get = get_bool,
897 .put = put_bool,
900 /* 8 bit int */
902 static int get_int8(QEMUFile *f, void *pv, size_t size)
904 int8_t *v = pv;
905 qemu_get_s8s(f, v);
906 return 0;
909 static void put_int8(QEMUFile *f, void *pv, size_t size)
911 int8_t *v = pv;
912 qemu_put_s8s(f, v);
915 const VMStateInfo vmstate_info_int8 = {
916 .name = "int8",
917 .get = get_int8,
918 .put = put_int8,
921 /* 16 bit int */
923 static int get_int16(QEMUFile *f, void *pv, size_t size)
925 int16_t *v = pv;
926 qemu_get_sbe16s(f, v);
927 return 0;
930 static void put_int16(QEMUFile *f, void *pv, size_t size)
932 int16_t *v = pv;
933 qemu_put_sbe16s(f, v);
936 const VMStateInfo vmstate_info_int16 = {
937 .name = "int16",
938 .get = get_int16,
939 .put = put_int16,
942 /* 32 bit int */
944 static int get_int32(QEMUFile *f, void *pv, size_t size)
946 int32_t *v = pv;
947 qemu_get_sbe32s(f, v);
948 return 0;
951 static void put_int32(QEMUFile *f, void *pv, size_t size)
953 int32_t *v = pv;
954 qemu_put_sbe32s(f, v);
957 const VMStateInfo vmstate_info_int32 = {
958 .name = "int32",
959 .get = get_int32,
960 .put = put_int32,
963 /* 32 bit int. See that the received value is the same than the one
964 in the field */
966 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
968 int32_t *v = pv;
969 int32_t v2;
970 qemu_get_sbe32s(f, &v2);
972 if (*v == v2)
973 return 0;
974 return -EINVAL;
977 const VMStateInfo vmstate_info_int32_equal = {
978 .name = "int32 equal",
979 .get = get_int32_equal,
980 .put = put_int32,
983 /* 32 bit int. See that the received value is the less or the same
984 than the one in the field */
986 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
988 int32_t *old = pv;
989 int32_t new;
990 qemu_get_sbe32s(f, &new);
992 if (*old <= new)
993 return 0;
994 return -EINVAL;
997 const VMStateInfo vmstate_info_int32_le = {
998 .name = "int32 equal",
999 .get = get_int32_le,
1000 .put = put_int32,
1003 /* 64 bit int */
1005 static int get_int64(QEMUFile *f, void *pv, size_t size)
1007 int64_t *v = pv;
1008 qemu_get_sbe64s(f, v);
1009 return 0;
1012 static void put_int64(QEMUFile *f, void *pv, size_t size)
1014 int64_t *v = pv;
1015 qemu_put_sbe64s(f, v);
1018 const VMStateInfo vmstate_info_int64 = {
1019 .name = "int64",
1020 .get = get_int64,
1021 .put = put_int64,
1024 /* 8 bit unsigned int */
1026 static int get_uint8(QEMUFile *f, void *pv, size_t size)
1028 uint8_t *v = pv;
1029 qemu_get_8s(f, v);
1030 return 0;
1033 static void put_uint8(QEMUFile *f, void *pv, size_t size)
1035 uint8_t *v = pv;
1036 qemu_put_8s(f, v);
1039 const VMStateInfo vmstate_info_uint8 = {
1040 .name = "uint8",
1041 .get = get_uint8,
1042 .put = put_uint8,
1045 /* 16 bit unsigned int */
1047 static int get_uint16(QEMUFile *f, void *pv, size_t size)
1049 uint16_t *v = pv;
1050 qemu_get_be16s(f, v);
1051 return 0;
1054 static void put_uint16(QEMUFile *f, void *pv, size_t size)
1056 uint16_t *v = pv;
1057 qemu_put_be16s(f, v);
1060 const VMStateInfo vmstate_info_uint16 = {
1061 .name = "uint16",
1062 .get = get_uint16,
1063 .put = put_uint16,
1066 /* 32 bit unsigned int */
1068 static int get_uint32(QEMUFile *f, void *pv, size_t size)
1070 uint32_t *v = pv;
1071 qemu_get_be32s(f, v);
1072 return 0;
1075 static void put_uint32(QEMUFile *f, void *pv, size_t size)
1077 uint32_t *v = pv;
1078 qemu_put_be32s(f, v);
1081 const VMStateInfo vmstate_info_uint32 = {
1082 .name = "uint32",
1083 .get = get_uint32,
1084 .put = put_uint32,
1087 /* 32 bit uint. See that the received value is the same than the one
1088 in the field */
1090 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1092 uint32_t *v = pv;
1093 uint32_t v2;
1094 qemu_get_be32s(f, &v2);
1096 if (*v == v2) {
1097 return 0;
1099 return -EINVAL;
1102 const VMStateInfo vmstate_info_uint32_equal = {
1103 .name = "uint32 equal",
1104 .get = get_uint32_equal,
1105 .put = put_uint32,
1108 /* 64 bit unsigned int */
1110 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1112 uint64_t *v = pv;
1113 qemu_get_be64s(f, v);
1114 return 0;
1117 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1119 uint64_t *v = pv;
1120 qemu_put_be64s(f, v);
1123 const VMStateInfo vmstate_info_uint64 = {
1124 .name = "uint64",
1125 .get = get_uint64,
1126 .put = put_uint64,
1129 /* 64 bit unsigned int. See that the received value is the same than the one
1130 in the field */
1132 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1134 uint64_t *v = pv;
1135 uint64_t v2;
1136 qemu_get_be64s(f, &v2);
1138 if (*v == v2) {
1139 return 0;
1141 return -EINVAL;
1144 const VMStateInfo vmstate_info_uint64_equal = {
1145 .name = "int64 equal",
1146 .get = get_uint64_equal,
1147 .put = put_uint64,
1150 /* 8 bit int. See that the received value is the same than the one
1151 in the field */
1153 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1155 uint8_t *v = pv;
1156 uint8_t v2;
1157 qemu_get_8s(f, &v2);
1159 if (*v == v2)
1160 return 0;
1161 return -EINVAL;
1164 const VMStateInfo vmstate_info_uint8_equal = {
1165 .name = "uint8 equal",
1166 .get = get_uint8_equal,
1167 .put = put_uint8,
1170 /* 16 bit unsigned int int. See that the received value is the same than the one
1171 in the field */
1173 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1175 uint16_t *v = pv;
1176 uint16_t v2;
1177 qemu_get_be16s(f, &v2);
1179 if (*v == v2)
1180 return 0;
1181 return -EINVAL;
1184 const VMStateInfo vmstate_info_uint16_equal = {
1185 .name = "uint16 equal",
1186 .get = get_uint16_equal,
1187 .put = put_uint16,
1190 /* floating point */
1192 static int get_float64(QEMUFile *f, void *pv, size_t size)
1194 float64 *v = pv;
1196 *v = make_float64(qemu_get_be64(f));
1197 return 0;
1200 static void put_float64(QEMUFile *f, void *pv, size_t size)
1202 uint64_t *v = pv;
1204 qemu_put_be64(f, float64_val(*v));
1207 const VMStateInfo vmstate_info_float64 = {
1208 .name = "float64",
1209 .get = get_float64,
1210 .put = put_float64,
1213 /* timers */
1215 static int get_timer(QEMUFile *f, void *pv, size_t size)
1217 QEMUTimer *v = pv;
1218 qemu_get_timer(f, v);
1219 return 0;
1222 static void put_timer(QEMUFile *f, void *pv, size_t size)
1224 QEMUTimer *v = pv;
1225 qemu_put_timer(f, v);
1228 const VMStateInfo vmstate_info_timer = {
1229 .name = "timer",
1230 .get = get_timer,
1231 .put = put_timer,
1234 /* uint8_t buffers */
1236 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1238 uint8_t *v = pv;
1239 qemu_get_buffer(f, v, size);
1240 return 0;
1243 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1245 uint8_t *v = pv;
1246 qemu_put_buffer(f, v, size);
1249 const VMStateInfo vmstate_info_buffer = {
1250 .name = "buffer",
1251 .get = get_buffer,
1252 .put = put_buffer,
1255 /* unused buffers: space that was used for some fields that are
1256 not useful anymore */
1258 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1260 uint8_t buf[1024];
1261 int block_len;
1263 while (size > 0) {
1264 block_len = MIN(sizeof(buf), size);
1265 size -= block_len;
1266 qemu_get_buffer(f, buf, block_len);
1268 return 0;
1271 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1273 static const 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_put_buffer(f, buf, block_len);
1283 const VMStateInfo vmstate_info_unused_buffer = {
1284 .name = "unused_buffer",
1285 .get = get_unused_buffer,
1286 .put = put_unused_buffer,
1289 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1290 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1291 * bit words with the bits in big endian order. The in-memory format
1292 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1294 /* This is the number of 64 bit words sent over the wire */
1295 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1296 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1298 unsigned long *bmp = pv;
1299 int i, idx = 0;
1300 for (i = 0; i < BITS_TO_U64S(size); i++) {
1301 uint64_t w = qemu_get_be64(f);
1302 bmp[idx++] = w;
1303 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1304 bmp[idx++] = w >> 32;
1307 return 0;
1310 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1312 unsigned long *bmp = pv;
1313 int i, idx = 0;
1314 for (i = 0; i < BITS_TO_U64S(size); i++) {
1315 uint64_t w = bmp[idx++];
1316 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1317 w |= ((uint64_t)bmp[idx++]) << 32;
1319 qemu_put_be64(f, w);
1323 const VMStateInfo vmstate_info_bitmap = {
1324 .name = "bitmap",
1325 .get = get_bitmap,
1326 .put = put_bitmap,
1329 typedef struct CompatEntry {
1330 char idstr[256];
1331 int instance_id;
1332 } CompatEntry;
1334 typedef struct SaveStateEntry {
1335 QTAILQ_ENTRY(SaveStateEntry) entry;
1336 char idstr[256];
1337 int instance_id;
1338 int alias_id;
1339 int version_id;
1340 int section_id;
1341 SaveVMHandlers *ops;
1342 const VMStateDescription *vmsd;
1343 void *opaque;
1344 CompatEntry *compat;
1345 int no_migrate;
1346 int is_ram;
1347 } SaveStateEntry;
1350 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1351 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1352 static int global_section_id;
1354 static int calculate_new_instance_id(const char *idstr)
1356 SaveStateEntry *se;
1357 int instance_id = 0;
1359 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1360 if (strcmp(idstr, se->idstr) == 0
1361 && instance_id <= se->instance_id) {
1362 instance_id = se->instance_id + 1;
1365 return instance_id;
1368 static int calculate_compat_instance_id(const char *idstr)
1370 SaveStateEntry *se;
1371 int instance_id = 0;
1373 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1374 if (!se->compat)
1375 continue;
1377 if (strcmp(idstr, se->compat->idstr) == 0
1378 && instance_id <= se->compat->instance_id) {
1379 instance_id = se->compat->instance_id + 1;
1382 return instance_id;
1385 /* TODO: Individual devices generally have very little idea about the rest
1386 of the system, so instance_id should be removed/replaced.
1387 Meanwhile pass -1 as instance_id if you do not already have a clearly
1388 distinguishing id for all instances of your device class. */
1389 int register_savevm_live(DeviceState *dev,
1390 const char *idstr,
1391 int instance_id,
1392 int version_id,
1393 SaveVMHandlers *ops,
1394 void *opaque)
1396 SaveStateEntry *se;
1398 se = g_malloc0(sizeof(SaveStateEntry));
1399 se->version_id = version_id;
1400 se->section_id = global_section_id++;
1401 se->ops = ops;
1402 se->opaque = opaque;
1403 se->vmsd = NULL;
1404 se->no_migrate = 0;
1405 /* if this is a live_savem then set is_ram */
1406 if (ops->save_live_setup != NULL) {
1407 se->is_ram = 1;
1410 if (dev) {
1411 char *id = qdev_get_dev_path(dev);
1412 if (id) {
1413 pstrcpy(se->idstr, sizeof(se->idstr), id);
1414 pstrcat(se->idstr, sizeof(se->idstr), "/");
1415 g_free(id);
1417 se->compat = g_malloc0(sizeof(CompatEntry));
1418 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1419 se->compat->instance_id = instance_id == -1 ?
1420 calculate_compat_instance_id(idstr) : instance_id;
1421 instance_id = -1;
1424 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1426 if (instance_id == -1) {
1427 se->instance_id = calculate_new_instance_id(se->idstr);
1428 } else {
1429 se->instance_id = instance_id;
1431 assert(!se->compat || se->instance_id == 0);
1432 /* add at the end of list */
1433 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1434 return 0;
1437 int register_savevm(DeviceState *dev,
1438 const char *idstr,
1439 int instance_id,
1440 int version_id,
1441 SaveStateHandler *save_state,
1442 LoadStateHandler *load_state,
1443 void *opaque)
1445 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1446 ops->save_state = save_state;
1447 ops->load_state = load_state;
1448 return register_savevm_live(dev, idstr, instance_id, version_id,
1449 ops, opaque);
1452 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1454 SaveStateEntry *se, *new_se;
1455 char id[256] = "";
1457 if (dev) {
1458 char *path = qdev_get_dev_path(dev);
1459 if (path) {
1460 pstrcpy(id, sizeof(id), path);
1461 pstrcat(id, sizeof(id), "/");
1462 g_free(path);
1465 pstrcat(id, sizeof(id), idstr);
1467 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1468 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1469 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1470 if (se->compat) {
1471 g_free(se->compat);
1473 g_free(se->ops);
1474 g_free(se);
1479 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1480 const VMStateDescription *vmsd,
1481 void *opaque, int alias_id,
1482 int required_for_version)
1484 SaveStateEntry *se;
1486 /* If this triggers, alias support can be dropped for the vmsd. */
1487 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1489 se = g_malloc0(sizeof(SaveStateEntry));
1490 se->version_id = vmsd->version_id;
1491 se->section_id = global_section_id++;
1492 se->opaque = opaque;
1493 se->vmsd = vmsd;
1494 se->alias_id = alias_id;
1495 se->no_migrate = vmsd->unmigratable;
1497 if (dev) {
1498 char *id = qdev_get_dev_path(dev);
1499 if (id) {
1500 pstrcpy(se->idstr, sizeof(se->idstr), id);
1501 pstrcat(se->idstr, sizeof(se->idstr), "/");
1502 g_free(id);
1504 se->compat = g_malloc0(sizeof(CompatEntry));
1505 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1506 se->compat->instance_id = instance_id == -1 ?
1507 calculate_compat_instance_id(vmsd->name) : instance_id;
1508 instance_id = -1;
1511 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1513 if (instance_id == -1) {
1514 se->instance_id = calculate_new_instance_id(se->idstr);
1515 } else {
1516 se->instance_id = instance_id;
1518 assert(!se->compat || se->instance_id == 0);
1519 /* add at the end of list */
1520 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1521 return 0;
1524 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1525 void *opaque)
1527 SaveStateEntry *se, *new_se;
1529 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1530 if (se->vmsd == vmsd && se->opaque == opaque) {
1531 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1532 if (se->compat) {
1533 g_free(se->compat);
1535 g_free(se);
1540 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1541 void *opaque);
1542 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1543 void *opaque);
1545 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1546 void *opaque, int version_id)
1548 VMStateField *field = vmsd->fields;
1549 int ret;
1551 if (version_id > vmsd->version_id) {
1552 return -EINVAL;
1554 if (version_id < vmsd->minimum_version_id_old) {
1555 return -EINVAL;
1557 if (version_id < vmsd->minimum_version_id) {
1558 return vmsd->load_state_old(f, opaque, version_id);
1560 if (vmsd->pre_load) {
1561 int ret = vmsd->pre_load(opaque);
1562 if (ret)
1563 return ret;
1565 while(field->name) {
1566 if ((field->field_exists &&
1567 field->field_exists(opaque, version_id)) ||
1568 (!field->field_exists &&
1569 field->version_id <= version_id)) {
1570 void *base_addr = opaque + field->offset;
1571 int i, n_elems = 1;
1572 int size = field->size;
1574 if (field->flags & VMS_VBUFFER) {
1575 size = *(int32_t *)(opaque+field->size_offset);
1576 if (field->flags & VMS_MULTIPLY) {
1577 size *= field->size;
1580 if (field->flags & VMS_ARRAY) {
1581 n_elems = field->num;
1582 } else if (field->flags & VMS_VARRAY_INT32) {
1583 n_elems = *(int32_t *)(opaque+field->num_offset);
1584 } else if (field->flags & VMS_VARRAY_UINT32) {
1585 n_elems = *(uint32_t *)(opaque+field->num_offset);
1586 } else if (field->flags & VMS_VARRAY_UINT16) {
1587 n_elems = *(uint16_t *)(opaque+field->num_offset);
1588 } else if (field->flags & VMS_VARRAY_UINT8) {
1589 n_elems = *(uint8_t *)(opaque+field->num_offset);
1591 if (field->flags & VMS_POINTER) {
1592 base_addr = *(void **)base_addr + field->start;
1594 for (i = 0; i < n_elems; i++) {
1595 void *addr = base_addr + size * i;
1597 if (field->flags & VMS_ARRAY_OF_POINTER) {
1598 addr = *(void **)addr;
1600 if (field->flags & VMS_STRUCT) {
1601 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1602 } else {
1603 ret = field->info->get(f, addr, size);
1606 if (ret < 0) {
1607 return ret;
1611 field++;
1613 ret = vmstate_subsection_load(f, vmsd, opaque);
1614 if (ret != 0) {
1615 return ret;
1617 if (vmsd->post_load) {
1618 return vmsd->post_load(opaque, version_id);
1620 return 0;
1623 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1624 void *opaque)
1626 VMStateField *field = vmsd->fields;
1628 if (vmsd->pre_save) {
1629 vmsd->pre_save(opaque);
1631 while(field->name) {
1632 if (!field->field_exists ||
1633 field->field_exists(opaque, vmsd->version_id)) {
1634 void *base_addr = opaque + field->offset;
1635 int i, n_elems = 1;
1636 int size = field->size;
1638 if (field->flags & VMS_VBUFFER) {
1639 size = *(int32_t *)(opaque+field->size_offset);
1640 if (field->flags & VMS_MULTIPLY) {
1641 size *= field->size;
1644 if (field->flags & VMS_ARRAY) {
1645 n_elems = field->num;
1646 } else if (field->flags & VMS_VARRAY_INT32) {
1647 n_elems = *(int32_t *)(opaque+field->num_offset);
1648 } else if (field->flags & VMS_VARRAY_UINT32) {
1649 n_elems = *(uint32_t *)(opaque+field->num_offset);
1650 } else if (field->flags & VMS_VARRAY_UINT16) {
1651 n_elems = *(uint16_t *)(opaque+field->num_offset);
1652 } else if (field->flags & VMS_VARRAY_UINT8) {
1653 n_elems = *(uint8_t *)(opaque+field->num_offset);
1655 if (field->flags & VMS_POINTER) {
1656 base_addr = *(void **)base_addr + field->start;
1658 for (i = 0; i < n_elems; i++) {
1659 void *addr = base_addr + size * i;
1661 if (field->flags & VMS_ARRAY_OF_POINTER) {
1662 addr = *(void **)addr;
1664 if (field->flags & VMS_STRUCT) {
1665 vmstate_save_state(f, field->vmsd, addr);
1666 } else {
1667 field->info->put(f, addr, size);
1671 field++;
1673 vmstate_subsection_save(f, vmsd, opaque);
1676 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1678 if (!se->vmsd) { /* Old style */
1679 return se->ops->load_state(f, se->opaque, version_id);
1681 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1684 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1686 if (!se->vmsd) { /* Old style */
1687 se->ops->save_state(f, se->opaque);
1688 return;
1690 vmstate_save_state(f,se->vmsd, se->opaque);
1693 #define QEMU_VM_FILE_MAGIC 0x5145564d
1694 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1695 #define QEMU_VM_FILE_VERSION 0x00000003
1697 #define QEMU_VM_EOF 0x00
1698 #define QEMU_VM_SECTION_START 0x01
1699 #define QEMU_VM_SECTION_PART 0x02
1700 #define QEMU_VM_SECTION_END 0x03
1701 #define QEMU_VM_SECTION_FULL 0x04
1702 #define QEMU_VM_SUBSECTION 0x05
1704 bool qemu_savevm_state_blocked(Error **errp)
1706 SaveStateEntry *se;
1708 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1709 if (se->no_migrate) {
1710 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1711 return true;
1714 return false;
1717 void qemu_savevm_state_begin(QEMUFile *f,
1718 const MigrationParams *params)
1720 SaveStateEntry *se;
1721 int ret;
1723 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1724 if (!se->ops || !se->ops->set_params) {
1725 continue;
1727 se->ops->set_params(params, se->opaque);
1730 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1731 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1733 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1734 int len;
1736 if (!se->ops || !se->ops->save_live_setup) {
1737 continue;
1739 if (se->ops && se->ops->is_active) {
1740 if (!se->ops->is_active(se->opaque)) {
1741 continue;
1744 /* Section type */
1745 qemu_put_byte(f, QEMU_VM_SECTION_START);
1746 qemu_put_be32(f, se->section_id);
1748 /* ID string */
1749 len = strlen(se->idstr);
1750 qemu_put_byte(f, len);
1751 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1753 qemu_put_be32(f, se->instance_id);
1754 qemu_put_be32(f, se->version_id);
1756 ret = se->ops->save_live_setup(f, se->opaque);
1757 if (ret < 0) {
1758 qemu_file_set_error(f, ret);
1759 break;
1765 * this function has three return values:
1766 * negative: there was one error, and we have -errno.
1767 * 0 : We haven't finished, caller have to go again
1768 * 1 : We have finished, we can go to complete phase
1770 int qemu_savevm_state_iterate(QEMUFile *f)
1772 SaveStateEntry *se;
1773 int ret = 1;
1775 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1776 if (!se->ops || !se->ops->save_live_iterate) {
1777 continue;
1779 if (se->ops && se->ops->is_active) {
1780 if (!se->ops->is_active(se->opaque)) {
1781 continue;
1784 if (qemu_file_rate_limit(f)) {
1785 return 0;
1787 trace_savevm_section_start();
1788 /* Section type */
1789 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1790 qemu_put_be32(f, se->section_id);
1792 ret = se->ops->save_live_iterate(f, se->opaque);
1793 trace_savevm_section_end(se->section_id);
1795 if (ret < 0) {
1796 qemu_file_set_error(f, ret);
1798 if (ret <= 0) {
1799 /* Do not proceed to the next vmstate before this one reported
1800 completion of the current stage. This serializes the migration
1801 and reduces the probability that a faster changing state is
1802 synchronized over and over again. */
1803 break;
1806 return ret;
1809 void qemu_savevm_state_complete(QEMUFile *f)
1811 SaveStateEntry *se;
1812 int ret;
1814 cpu_synchronize_all_states();
1816 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1817 if (!se->ops || !se->ops->save_live_complete) {
1818 continue;
1820 if (se->ops && se->ops->is_active) {
1821 if (!se->ops->is_active(se->opaque)) {
1822 continue;
1825 trace_savevm_section_start();
1826 /* Section type */
1827 qemu_put_byte(f, QEMU_VM_SECTION_END);
1828 qemu_put_be32(f, se->section_id);
1830 ret = se->ops->save_live_complete(f, se->opaque);
1831 trace_savevm_section_end(se->section_id);
1832 if (ret < 0) {
1833 qemu_file_set_error(f, ret);
1834 return;
1838 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1839 int len;
1841 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1842 continue;
1844 trace_savevm_section_start();
1845 /* Section type */
1846 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1847 qemu_put_be32(f, se->section_id);
1849 /* ID string */
1850 len = strlen(se->idstr);
1851 qemu_put_byte(f, len);
1852 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1854 qemu_put_be32(f, se->instance_id);
1855 qemu_put_be32(f, se->version_id);
1857 vmstate_save(f, se);
1858 trace_savevm_section_end(se->section_id);
1861 qemu_put_byte(f, QEMU_VM_EOF);
1862 qemu_fflush(f);
1865 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1867 SaveStateEntry *se;
1868 uint64_t ret = 0;
1870 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1871 if (!se->ops || !se->ops->save_live_pending) {
1872 continue;
1874 if (se->ops && se->ops->is_active) {
1875 if (!se->ops->is_active(se->opaque)) {
1876 continue;
1879 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1881 return ret;
1884 void qemu_savevm_state_cancel(void)
1886 SaveStateEntry *se;
1888 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1889 if (se->ops && se->ops->cancel) {
1890 se->ops->cancel(se->opaque);
1895 static int qemu_savevm_state(QEMUFile *f)
1897 int ret;
1898 MigrationParams params = {
1899 .blk = 0,
1900 .shared = 0
1903 if (qemu_savevm_state_blocked(NULL)) {
1904 return -EINVAL;
1907 qemu_mutex_unlock_iothread();
1908 qemu_savevm_state_begin(f, &params);
1909 qemu_mutex_lock_iothread();
1911 while (qemu_file_get_error(f) == 0) {
1912 if (qemu_savevm_state_iterate(f) > 0) {
1913 break;
1917 ret = qemu_file_get_error(f);
1918 if (ret == 0) {
1919 qemu_savevm_state_complete(f);
1920 ret = qemu_file_get_error(f);
1922 if (ret != 0) {
1923 qemu_savevm_state_cancel();
1925 return ret;
1928 static int qemu_save_device_state(QEMUFile *f)
1930 SaveStateEntry *se;
1932 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1933 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1935 cpu_synchronize_all_states();
1937 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1938 int len;
1940 if (se->is_ram) {
1941 continue;
1943 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1944 continue;
1947 /* Section type */
1948 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1949 qemu_put_be32(f, se->section_id);
1951 /* ID string */
1952 len = strlen(se->idstr);
1953 qemu_put_byte(f, len);
1954 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1956 qemu_put_be32(f, se->instance_id);
1957 qemu_put_be32(f, se->version_id);
1959 vmstate_save(f, se);
1962 qemu_put_byte(f, QEMU_VM_EOF);
1964 return qemu_file_get_error(f);
1967 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1969 SaveStateEntry *se;
1971 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1972 if (!strcmp(se->idstr, idstr) &&
1973 (instance_id == se->instance_id ||
1974 instance_id == se->alias_id))
1975 return se;
1976 /* Migrating from an older version? */
1977 if (strstr(se->idstr, idstr) && se->compat) {
1978 if (!strcmp(se->compat->idstr, idstr) &&
1979 (instance_id == se->compat->instance_id ||
1980 instance_id == se->alias_id))
1981 return se;
1984 return NULL;
1987 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1989 while(sub && sub->needed) {
1990 if (strcmp(idstr, sub->vmsd->name) == 0) {
1991 return sub->vmsd;
1993 sub++;
1995 return NULL;
1998 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1999 void *opaque)
2001 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
2002 char idstr[256];
2003 int ret;
2004 uint8_t version_id, len, size;
2005 const VMStateDescription *sub_vmsd;
2007 len = qemu_peek_byte(f, 1);
2008 if (len < strlen(vmsd->name) + 1) {
2009 /* subsection name has be be "section_name/a" */
2010 return 0;
2012 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2013 if (size != len) {
2014 return 0;
2016 idstr[size] = 0;
2018 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2019 /* it don't have a valid subsection name */
2020 return 0;
2022 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
2023 if (sub_vmsd == NULL) {
2024 return -ENOENT;
2026 qemu_file_skip(f, 1); /* subsection */
2027 qemu_file_skip(f, 1); /* len */
2028 qemu_file_skip(f, len); /* idstr */
2029 version_id = qemu_get_be32(f);
2031 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2032 if (ret) {
2033 return ret;
2036 return 0;
2039 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2040 void *opaque)
2042 const VMStateSubsection *sub = vmsd->subsections;
2044 while (sub && sub->needed) {
2045 if (sub->needed(opaque)) {
2046 const VMStateDescription *vmsd = sub->vmsd;
2047 uint8_t len;
2049 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2050 len = strlen(vmsd->name);
2051 qemu_put_byte(f, len);
2052 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2053 qemu_put_be32(f, vmsd->version_id);
2054 vmstate_save_state(f, vmsd, opaque);
2056 sub++;
2060 typedef struct LoadStateEntry {
2061 QLIST_ENTRY(LoadStateEntry) entry;
2062 SaveStateEntry *se;
2063 int section_id;
2064 int version_id;
2065 } LoadStateEntry;
2067 int qemu_loadvm_state(QEMUFile *f)
2069 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2070 QLIST_HEAD_INITIALIZER(loadvm_handlers);
2071 LoadStateEntry *le, *new_le;
2072 uint8_t section_type;
2073 unsigned int v;
2074 int ret;
2076 if (qemu_savevm_state_blocked(NULL)) {
2077 return -EINVAL;
2080 v = qemu_get_be32(f);
2081 if (v != QEMU_VM_FILE_MAGIC)
2082 return -EINVAL;
2084 v = qemu_get_be32(f);
2085 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2086 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2087 return -ENOTSUP;
2089 if (v != QEMU_VM_FILE_VERSION)
2090 return -ENOTSUP;
2092 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2093 uint32_t instance_id, version_id, section_id;
2094 SaveStateEntry *se;
2095 char idstr[257];
2096 int len;
2098 switch (section_type) {
2099 case QEMU_VM_SECTION_START:
2100 case QEMU_VM_SECTION_FULL:
2101 /* Read section start */
2102 section_id = qemu_get_be32(f);
2103 len = qemu_get_byte(f);
2104 qemu_get_buffer(f, (uint8_t *)idstr, len);
2105 idstr[len] = 0;
2106 instance_id = qemu_get_be32(f);
2107 version_id = qemu_get_be32(f);
2109 /* Find savevm section */
2110 se = find_se(idstr, instance_id);
2111 if (se == NULL) {
2112 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2113 ret = -EINVAL;
2114 goto out;
2117 /* Validate version */
2118 if (version_id > se->version_id) {
2119 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2120 version_id, idstr, se->version_id);
2121 ret = -EINVAL;
2122 goto out;
2125 /* Add entry */
2126 le = g_malloc0(sizeof(*le));
2128 le->se = se;
2129 le->section_id = section_id;
2130 le->version_id = version_id;
2131 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
2133 ret = vmstate_load(f, le->se, le->version_id);
2134 if (ret < 0) {
2135 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2136 instance_id, idstr);
2137 goto out;
2139 break;
2140 case QEMU_VM_SECTION_PART:
2141 case QEMU_VM_SECTION_END:
2142 section_id = qemu_get_be32(f);
2144 QLIST_FOREACH(le, &loadvm_handlers, entry) {
2145 if (le->section_id == section_id) {
2146 break;
2149 if (le == NULL) {
2150 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2151 ret = -EINVAL;
2152 goto out;
2155 ret = vmstate_load(f, le->se, le->version_id);
2156 if (ret < 0) {
2157 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2158 section_id);
2159 goto out;
2161 break;
2162 default:
2163 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2164 ret = -EINVAL;
2165 goto out;
2169 cpu_synchronize_all_post_init();
2171 ret = 0;
2173 out:
2174 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2175 QLIST_REMOVE(le, entry);
2176 g_free(le);
2179 if (ret == 0) {
2180 ret = qemu_file_get_error(f);
2183 return ret;
2186 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2187 const char *name)
2189 QEMUSnapshotInfo *sn_tab, *sn;
2190 int nb_sns, i, ret;
2192 ret = -ENOENT;
2193 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2194 if (nb_sns < 0)
2195 return ret;
2196 for(i = 0; i < nb_sns; i++) {
2197 sn = &sn_tab[i];
2198 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2199 *sn_info = *sn;
2200 ret = 0;
2201 break;
2204 g_free(sn_tab);
2205 return ret;
2209 * Deletes snapshots of a given name in all opened images.
2211 static int del_existing_snapshots(Monitor *mon, const char *name)
2213 BlockDriverState *bs;
2214 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2215 int ret;
2217 bs = NULL;
2218 while ((bs = bdrv_next(bs))) {
2219 if (bdrv_can_snapshot(bs) &&
2220 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2222 ret = bdrv_snapshot_delete(bs, name);
2223 if (ret < 0) {
2224 monitor_printf(mon,
2225 "Error while deleting snapshot on '%s'\n",
2226 bdrv_get_device_name(bs));
2227 return -1;
2232 return 0;
2235 void do_savevm(Monitor *mon, const QDict *qdict)
2237 BlockDriverState *bs, *bs1;
2238 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2239 int ret;
2240 QEMUFile *f;
2241 int saved_vm_running;
2242 uint64_t vm_state_size;
2243 qemu_timeval tv;
2244 struct tm tm;
2245 const char *name = qdict_get_try_str(qdict, "name");
2247 /* Verify if there is a device that doesn't support snapshots and is writable */
2248 bs = NULL;
2249 while ((bs = bdrv_next(bs))) {
2251 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2252 continue;
2255 if (!bdrv_can_snapshot(bs)) {
2256 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2257 bdrv_get_device_name(bs));
2258 return;
2262 bs = bdrv_snapshots();
2263 if (!bs) {
2264 monitor_printf(mon, "No block device can accept snapshots\n");
2265 return;
2268 saved_vm_running = runstate_is_running();
2269 vm_stop(RUN_STATE_SAVE_VM);
2271 memset(sn, 0, sizeof(*sn));
2273 /* fill auxiliary fields */
2274 qemu_gettimeofday(&tv);
2275 sn->date_sec = tv.tv_sec;
2276 sn->date_nsec = tv.tv_usec * 1000;
2277 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2279 if (name) {
2280 ret = bdrv_snapshot_find(bs, old_sn, name);
2281 if (ret >= 0) {
2282 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2283 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2284 } else {
2285 pstrcpy(sn->name, sizeof(sn->name), name);
2287 } else {
2288 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2289 localtime_r((const time_t *)&tv.tv_sec, &tm);
2290 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2293 /* Delete old snapshots of the same name */
2294 if (name && del_existing_snapshots(mon, name) < 0) {
2295 goto the_end;
2298 /* save the VM state */
2299 f = qemu_fopen_bdrv(bs, 1);
2300 if (!f) {
2301 monitor_printf(mon, "Could not open VM state file\n");
2302 goto the_end;
2304 ret = qemu_savevm_state(f);
2305 vm_state_size = qemu_ftell(f);
2306 qemu_fclose(f);
2307 if (ret < 0) {
2308 monitor_printf(mon, "Error %d while writing VM\n", ret);
2309 goto the_end;
2312 /* create the snapshots */
2314 bs1 = NULL;
2315 while ((bs1 = bdrv_next(bs1))) {
2316 if (bdrv_can_snapshot(bs1)) {
2317 /* Write VM state size only to the image that contains the state */
2318 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2319 ret = bdrv_snapshot_create(bs1, sn);
2320 if (ret < 0) {
2321 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2322 bdrv_get_device_name(bs1));
2327 the_end:
2328 if (saved_vm_running)
2329 vm_start();
2332 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2334 QEMUFile *f;
2335 int saved_vm_running;
2336 int ret;
2338 saved_vm_running = runstate_is_running();
2339 vm_stop(RUN_STATE_SAVE_VM);
2341 f = qemu_fopen(filename, "wb");
2342 if (!f) {
2343 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2344 goto the_end;
2346 ret = qemu_save_device_state(f);
2347 qemu_fclose(f);
2348 if (ret < 0) {
2349 error_set(errp, QERR_IO_ERROR);
2352 the_end:
2353 if (saved_vm_running)
2354 vm_start();
2357 int load_vmstate(const char *name)
2359 BlockDriverState *bs, *bs_vm_state;
2360 QEMUSnapshotInfo sn;
2361 QEMUFile *f;
2362 int ret;
2364 bs_vm_state = bdrv_snapshots();
2365 if (!bs_vm_state) {
2366 error_report("No block device supports snapshots");
2367 return -ENOTSUP;
2370 /* Don't even try to load empty VM states */
2371 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2372 if (ret < 0) {
2373 return ret;
2374 } else if (sn.vm_state_size == 0) {
2375 error_report("This is a disk-only snapshot. Revert to it offline "
2376 "using qemu-img.");
2377 return -EINVAL;
2380 /* Verify if there is any device that doesn't support snapshots and is
2381 writable and check if the requested snapshot is available too. */
2382 bs = NULL;
2383 while ((bs = bdrv_next(bs))) {
2385 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2386 continue;
2389 if (!bdrv_can_snapshot(bs)) {
2390 error_report("Device '%s' is writable but does not support snapshots.",
2391 bdrv_get_device_name(bs));
2392 return -ENOTSUP;
2395 ret = bdrv_snapshot_find(bs, &sn, name);
2396 if (ret < 0) {
2397 error_report("Device '%s' does not have the requested snapshot '%s'",
2398 bdrv_get_device_name(bs), name);
2399 return ret;
2403 /* Flush all IO requests so they don't interfere with the new state. */
2404 bdrv_drain_all();
2406 bs = NULL;
2407 while ((bs = bdrv_next(bs))) {
2408 if (bdrv_can_snapshot(bs)) {
2409 ret = bdrv_snapshot_goto(bs, name);
2410 if (ret < 0) {
2411 error_report("Error %d while activating snapshot '%s' on '%s'",
2412 ret, name, bdrv_get_device_name(bs));
2413 return ret;
2418 /* restore the VM state */
2419 f = qemu_fopen_bdrv(bs_vm_state, 0);
2420 if (!f) {
2421 error_report("Could not open VM state file");
2422 return -EINVAL;
2425 qemu_system_reset(VMRESET_SILENT);
2426 ret = qemu_loadvm_state(f);
2428 qemu_fclose(f);
2429 if (ret < 0) {
2430 error_report("Error %d while loading VM state", ret);
2431 return ret;
2434 return 0;
2437 void do_delvm(Monitor *mon, const QDict *qdict)
2439 BlockDriverState *bs, *bs1;
2440 int ret;
2441 const char *name = qdict_get_str(qdict, "name");
2443 bs = bdrv_snapshots();
2444 if (!bs) {
2445 monitor_printf(mon, "No block device supports snapshots\n");
2446 return;
2449 bs1 = NULL;
2450 while ((bs1 = bdrv_next(bs1))) {
2451 if (bdrv_can_snapshot(bs1)) {
2452 ret = bdrv_snapshot_delete(bs1, name);
2453 if (ret < 0) {
2454 if (ret == -ENOTSUP)
2455 monitor_printf(mon,
2456 "Snapshots not supported on device '%s'\n",
2457 bdrv_get_device_name(bs1));
2458 else
2459 monitor_printf(mon, "Error %d while deleting snapshot on "
2460 "'%s'\n", ret, bdrv_get_device_name(bs1));
2466 void do_info_snapshots(Monitor *mon, const QDict *qdict)
2468 BlockDriverState *bs, *bs1;
2469 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2470 int nb_sns, i, ret, available;
2471 int total;
2472 int *available_snapshots;
2473 char buf[256];
2475 bs = bdrv_snapshots();
2476 if (!bs) {
2477 monitor_printf(mon, "No available block device supports snapshots\n");
2478 return;
2481 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2482 if (nb_sns < 0) {
2483 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2484 return;
2487 if (nb_sns == 0) {
2488 monitor_printf(mon, "There is no snapshot available.\n");
2489 return;
2492 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2493 total = 0;
2494 for (i = 0; i < nb_sns; i++) {
2495 sn = &sn_tab[i];
2496 available = 1;
2497 bs1 = NULL;
2499 while ((bs1 = bdrv_next(bs1))) {
2500 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2501 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2502 if (ret < 0) {
2503 available = 0;
2504 break;
2509 if (available) {
2510 available_snapshots[total] = i;
2511 total++;
2515 if (total > 0) {
2516 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2517 for (i = 0; i < total; i++) {
2518 sn = &sn_tab[available_snapshots[i]];
2519 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2521 } else {
2522 monitor_printf(mon, "There is no suitable snapshot available\n");
2525 g_free(sn_tab);
2526 g_free(available_snapshots);
2530 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2532 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2533 memory_region_name(mr), dev);
2536 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2538 /* Nothing do to while the implementation is in RAMBlock */
2541 void vmstate_register_ram_global(MemoryRegion *mr)
2543 vmstate_register_ram(mr, NULL);