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
25 #include "config-host.h"
26 #include "qemu-common.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"
41 #include "qemu/bitops.h"
44 #define SELF_ANNOUNCE_ROUNDS 5
47 #define ETH_P_RARP 0x8035
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
,
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 */
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
)
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
);
97 /* delay 50ms, 150ms, 250ms, ... */
98 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
99 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
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)
120 const QEMUFileOps
*ops
;
126 int64_t pos
; /* start of buffer when writing, end of buffer
129 int buf_size
; /* 0 when writing */
130 uint8_t buf
[IO_BUF_SIZE
];
132 struct iovec iov
[MAX_IOV_SIZE
];
138 typedef struct QEMUFileStdio
144 typedef struct QEMUFileSocket
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();
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
,
181 QEMUFileSocket
*s
= opaque
;
183 ssize_t size
= iov_size(iov
, iovcnt
);
185 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
187 len
= -socket_error();
192 static int socket_get_fd(void *opaque
)
194 QEMUFileSocket
*s
= opaque
;
199 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
201 QEMUFileSocket
*s
= opaque
;
205 len
= qemu_recv(s
->fd
, buf
, size
, 0);
209 if (socket_error() == EAGAIN
) {
210 yield_until_fd_readable(s
->fd
);
211 } else if (socket_error() != EINTR
) {
217 len
= -socket_error();
222 static int socket_close(void *opaque
)
224 QEMUFileSocket
*s
= opaque
;
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
;
251 bytes
= fread(buf
, 1, size
, fp
);
252 if (bytes
!= 0 || !ferror(fp
)) {
255 if (errno
== EAGAIN
) {
256 yield_until_fd_readable(fileno(fp
));
257 } else if (errno
!= EINTR
) {
264 static int stdio_pclose(void *opaque
)
266 QEMUFileStdio
*s
= opaque
;
268 ret
= pclose(s
->stdio_file
);
271 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
272 /* close succeeded, but non-zero exit code: */
273 ret
= -EIO
; /* fake errno value */
279 static int stdio_fclose(void *opaque
)
281 QEMUFileStdio
*s
= opaque
;
284 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
285 int fd
= fileno(s
->stdio_file
);
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.
301 if (fclose(s
->stdio_file
) == EOF
) {
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
)
325 stdio_file
= popen(command
, mode
);
326 if (stdio_file
== NULL
) {
330 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
331 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
335 s
= g_malloc0(sizeof(QEMUFileStdio
));
337 s
->stdio_file
= stdio_file
;
340 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
342 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
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
)
364 (mode
[0] != 'r' && mode
[0] != 'w') ||
365 mode
[1] != 'b' || mode
[2] != 0) {
366 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
370 s
= g_malloc0(sizeof(QEMUFileStdio
));
371 s
->stdio_file
= fdopen(fd
, mode
);
376 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
378 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
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
));
404 (mode
[0] != 'r' && mode
[0] != 'w') ||
405 mode
[1] != 'b' || mode
[2] != 0) {
406 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
411 if (mode
[0] == 'w') {
412 qemu_set_block(s
->fd
);
413 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
415 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
420 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
425 (mode
[0] != 'r' && mode
[0] != 'w') ||
426 mode
[1] != 'b' || mode
[2] != 0) {
427 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
431 s
= g_malloc0(sizeof(QEMUFileStdio
));
433 s
->stdio_file
= fopen(filename
, mode
);
438 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
440 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
448 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
454 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
455 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
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
);
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
,
485 static const QEMUFileOps bdrv_write_ops
= {
486 .put_buffer
= block_put_buffer
,
487 .writev_buffer
= block_writev_buffer
,
491 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int 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
)
502 f
= g_malloc0(sizeof(QEMUFile
));
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) {
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
532 static void qemu_fflush(QEMUFile
*f
)
536 if (!qemu_file_is_writable(f
)) {
540 if (f
->ops
->writev_buffer
) {
542 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
545 if (f
->buf_index
> 0) {
546 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
555 qemu_file_set_error(f
, ret
);
559 static void qemu_fill_buffer(QEMUFile
*f
)
564 assert(!qemu_file_is_writable(f
));
566 pending
= f
->buf_size
- f
->buf_index
;
568 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
571 f
->buf_size
= pending
;
573 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
574 IO_BUF_SIZE
- pending
);
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
);
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
600 int qemu_fclose(QEMUFile
*f
)
604 ret
= qemu_file_get_error(f
);
607 int ret2
= f
->ops
->close(f
->opaque
);
612 /* If any error was spotted before closing, we should report it
613 * instead of the close() return value.
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
;
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
) {
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
);
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
)
662 l
= IO_BUF_SIZE
- f
->buf_index
;
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
);
671 if (f
->buf_index
== IO_BUF_SIZE
) {
674 if (qemu_file_get_error(f
)) {
682 void qemu_put_byte(QEMUFile
*f
, int v
)
688 f
->buf
[f
->buf_index
] = v
;
690 if (f
->ops
->writev_buffer
) {
691 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
694 if (f
->buf_index
== IO_BUF_SIZE
) {
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
)
711 assert(!qemu_file_is_writable(f
));
713 index
= f
->buf_index
+ offset
;
714 pending
= f
->buf_size
- index
;
715 if (pending
< size
) {
717 index
= f
->buf_index
+ offset
;
718 pending
= f
->buf_size
- index
;
724 if (size
> pending
) {
728 memcpy(buf
, f
->buf
+ index
, size
);
732 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
737 while (pending
> 0) {
740 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
744 qemu_file_skip(f
, res
);
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
) {
760 index
= f
->buf_index
+ offset
;
761 if (index
>= f
->buf_size
) {
765 return f
->buf
[index
];
768 int qemu_get_byte(QEMUFile
*f
)
772 result
= qemu_peek_byte(f
, 0);
773 qemu_file_skip(f
, 1);
777 int64_t qemu_ftell(QEMUFile
*f
)
783 int qemu_file_rate_limit(QEMUFile
*f
)
785 if (qemu_file_get_error(f
)) {
788 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
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
)
809 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
811 qemu_put_byte(f
, v
>> 8);
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);
823 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
825 qemu_put_be32(f
, v
>> 32);
829 unsigned int qemu_get_be16(QEMUFile
*f
)
832 v
= qemu_get_byte(f
) << 8;
833 v
|= qemu_get_byte(f
);
837 unsigned int qemu_get_be32(QEMUFile
*f
)
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
);
847 uint64_t qemu_get_be64(QEMUFile
*f
)
850 v
= (uint64_t)qemu_get_be32(f
) << 32;
851 v
|= qemu_get_be32(f
);
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
);
881 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
884 *v
= qemu_get_byte(f
);
888 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
891 qemu_put_byte(f
, *v
);
894 const VMStateInfo vmstate_info_bool
= {
902 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
909 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
915 const VMStateInfo vmstate_info_int8
= {
923 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
926 qemu_get_sbe16s(f
, v
);
930 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
933 qemu_put_sbe16s(f
, v
);
936 const VMStateInfo vmstate_info_int16
= {
944 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
947 qemu_get_sbe32s(f
, v
);
951 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
954 qemu_put_sbe32s(f
, v
);
957 const VMStateInfo vmstate_info_int32
= {
963 /* 32 bit int. See that the received value is the same than the one
966 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
970 qemu_get_sbe32s(f
, &v2
);
977 const VMStateInfo vmstate_info_int32_equal
= {
978 .name
= "int32 equal",
979 .get
= get_int32_equal
,
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
)
990 qemu_get_sbe32s(f
, &new);
997 const VMStateInfo vmstate_info_int32_le
= {
998 .name
= "int32 equal",
1005 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1008 qemu_get_sbe64s(f
, v
);
1012 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1015 qemu_put_sbe64s(f
, v
);
1018 const VMStateInfo vmstate_info_int64
= {
1024 /* 8 bit unsigned int */
1026 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1033 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1039 const VMStateInfo vmstate_info_uint8
= {
1045 /* 16 bit unsigned int */
1047 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1050 qemu_get_be16s(f
, v
);
1054 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1057 qemu_put_be16s(f
, v
);
1060 const VMStateInfo vmstate_info_uint16
= {
1066 /* 32 bit unsigned int */
1068 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1071 qemu_get_be32s(f
, v
);
1075 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1078 qemu_put_be32s(f
, v
);
1081 const VMStateInfo vmstate_info_uint32
= {
1087 /* 32 bit uint. See that the received value is the same than the one
1090 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1094 qemu_get_be32s(f
, &v2
);
1102 const VMStateInfo vmstate_info_uint32_equal
= {
1103 .name
= "uint32 equal",
1104 .get
= get_uint32_equal
,
1108 /* 64 bit unsigned int */
1110 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1113 qemu_get_be64s(f
, v
);
1117 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1120 qemu_put_be64s(f
, v
);
1123 const VMStateInfo vmstate_info_uint64
= {
1129 /* 64 bit unsigned int. See that the received value is the same than the one
1132 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1136 qemu_get_be64s(f
, &v2
);
1144 const VMStateInfo vmstate_info_uint64_equal
= {
1145 .name
= "int64 equal",
1146 .get
= get_uint64_equal
,
1150 /* 8 bit int. See that the received value is the same than the one
1153 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1157 qemu_get_8s(f
, &v2
);
1164 const VMStateInfo vmstate_info_uint8_equal
= {
1165 .name
= "uint8 equal",
1166 .get
= get_uint8_equal
,
1170 /* 16 bit unsigned int int. See that the received value is the same than the one
1173 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1177 qemu_get_be16s(f
, &v2
);
1184 const VMStateInfo vmstate_info_uint16_equal
= {
1185 .name
= "uint16 equal",
1186 .get
= get_uint16_equal
,
1190 /* floating point */
1192 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1196 *v
= make_float64(qemu_get_be64(f
));
1200 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1204 qemu_put_be64(f
, float64_val(*v
));
1207 const VMStateInfo vmstate_info_float64
= {
1215 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1218 qemu_get_timer(f
, v
);
1222 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1225 qemu_put_timer(f
, v
);
1228 const VMStateInfo vmstate_info_timer
= {
1234 /* uint8_t buffers */
1236 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1239 qemu_get_buffer(f
, v
, size
);
1243 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1246 qemu_put_buffer(f
, v
, size
);
1249 const VMStateInfo vmstate_info_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
)
1264 block_len
= MIN(sizeof(buf
), size
);
1266 qemu_get_buffer(f
, buf
, block_len
);
1271 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1273 static const uint8_t buf
[1024];
1277 block_len
= MIN(sizeof(buf
), size
);
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
;
1300 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1301 uint64_t w
= qemu_get_be64(f
);
1303 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1304 bmp
[idx
++] = w
>> 32;
1310 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1312 unsigned long *bmp
= pv
;
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
= {
1329 typedef struct CompatEntry
{
1334 typedef struct SaveStateEntry
{
1335 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1341 SaveVMHandlers
*ops
;
1342 const VMStateDescription
*vmsd
;
1344 CompatEntry
*compat
;
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
)
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;
1368 static int calculate_compat_instance_id(const char *idstr
)
1371 int instance_id
= 0;
1373 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1377 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1378 && instance_id
<= se
->compat
->instance_id
) {
1379 instance_id
= se
->compat
->instance_id
+ 1;
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
,
1393 SaveVMHandlers
*ops
,
1398 se
= g_malloc0(sizeof(SaveStateEntry
));
1399 se
->version_id
= version_id
;
1400 se
->section_id
= global_section_id
++;
1402 se
->opaque
= opaque
;
1405 /* if this is a live_savem then set is_ram */
1406 if (ops
->save_live_setup
!= NULL
) {
1411 char *id
= qdev_get_dev_path(dev
);
1413 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1414 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
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
;
1424 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1426 if (instance_id
== -1) {
1427 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
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
);
1437 int register_savevm(DeviceState
*dev
,
1441 SaveStateHandler
*save_state
,
1442 LoadStateHandler
*load_state
,
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
,
1452 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1454 SaveStateEntry
*se
, *new_se
;
1458 char *path
= qdev_get_dev_path(dev
);
1460 pstrcpy(id
, sizeof(id
), path
);
1461 pstrcat(id
, sizeof(id
), "/");
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
);
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
)
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
;
1494 se
->alias_id
= alias_id
;
1495 se
->no_migrate
= vmsd
->unmigratable
;
1498 char *id
= qdev_get_dev_path(dev
);
1500 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1501 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
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
;
1511 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1513 if (instance_id
== -1) {
1514 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
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
);
1524 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
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
);
1540 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1542 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1545 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1546 void *opaque
, int version_id
)
1548 VMStateField
*field
= vmsd
->fields
;
1551 if (version_id
> vmsd
->version_id
) {
1554 if (version_id
< vmsd
->minimum_version_id_old
) {
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
);
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
;
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
);
1603 ret
= field
->info
->get(f
, addr
, size
);
1613 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1617 if (vmsd
->post_load
) {
1618 return vmsd
->post_load(opaque
, version_id
);
1623 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
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
;
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
);
1667 field
->info
->put(f
, addr
, size
);
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
);
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
)
1708 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1709 if (se
->no_migrate
) {
1710 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1717 void qemu_savevm_state_begin(QEMUFile
*f
,
1718 const MigrationParams
*params
)
1723 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1724 if (!se
->ops
|| !se
->ops
->set_params
) {
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
) {
1736 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1739 if (se
->ops
&& se
->ops
->is_active
) {
1740 if (!se
->ops
->is_active(se
->opaque
)) {
1745 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1746 qemu_put_be32(f
, se
->section_id
);
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
);
1758 qemu_file_set_error(f
, ret
);
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
)
1775 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1776 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1779 if (se
->ops
&& se
->ops
->is_active
) {
1780 if (!se
->ops
->is_active(se
->opaque
)) {
1784 if (qemu_file_rate_limit(f
)) {
1787 trace_savevm_section_start();
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
);
1796 qemu_file_set_error(f
, ret
);
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. */
1809 void qemu_savevm_state_complete(QEMUFile
*f
)
1814 cpu_synchronize_all_states();
1816 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1817 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1820 if (se
->ops
&& se
->ops
->is_active
) {
1821 if (!se
->ops
->is_active(se
->opaque
)) {
1825 trace_savevm_section_start();
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
);
1833 qemu_file_set_error(f
, ret
);
1838 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1841 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1844 trace_savevm_section_start();
1846 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1847 qemu_put_be32(f
, se
->section_id
);
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
);
1865 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1870 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1871 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1874 if (se
->ops
&& se
->ops
->is_active
) {
1875 if (!se
->ops
->is_active(se
->opaque
)) {
1879 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1884 void qemu_savevm_state_cancel(void)
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
)
1898 MigrationParams params
= {
1903 if (qemu_savevm_state_blocked(NULL
)) {
1907 qemu_mutex_unlock_iothread();
1908 qemu_savevm_state_begin(f
, ¶ms
);
1909 qemu_mutex_lock_iothread();
1911 while (qemu_file_get_error(f
) == 0) {
1912 if (qemu_savevm_state_iterate(f
) > 0) {
1917 ret
= qemu_file_get_error(f
);
1919 qemu_savevm_state_complete(f
);
1920 ret
= qemu_file_get_error(f
);
1923 qemu_savevm_state_cancel();
1928 static int qemu_save_device_state(QEMUFile
*f
)
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
) {
1943 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1948 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1949 qemu_put_be32(f
, se
->section_id
);
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
)
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
))
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
))
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) {
1998 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2001 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
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" */
2012 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2018 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2019 /* it don't have a valid subsection name */
2022 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2023 if (sub_vmsd
== NULL
) {
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
);
2039 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2042 const VMStateSubsection
*sub
= vmsd
->subsections
;
2044 while (sub
&& sub
->needed
) {
2045 if (sub
->needed(opaque
)) {
2046 const VMStateDescription
*vmsd
= sub
->vmsd
;
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
);
2060 typedef struct LoadStateEntry
{
2061 QLIST_ENTRY(LoadStateEntry
) entry
;
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
;
2076 if (qemu_savevm_state_blocked(NULL
)) {
2080 v
= qemu_get_be32(f
);
2081 if (v
!= QEMU_VM_FILE_MAGIC
)
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");
2089 if (v
!= QEMU_VM_FILE_VERSION
)
2092 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2093 uint32_t instance_id
, version_id
, section_id
;
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
);
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
);
2112 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
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
);
2126 le
= g_malloc0(sizeof(*le
));
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
);
2135 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2136 instance_id
, idstr
);
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
) {
2150 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2155 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2157 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2163 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2169 cpu_synchronize_all_post_init();
2174 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2175 QLIST_REMOVE(le
, entry
);
2180 ret
= qemu_file_get_error(f
);
2186 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2189 QEMUSnapshotInfo
*sn_tab
, *sn
;
2193 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2196 for(i
= 0; i
< nb_sns
; i
++) {
2198 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
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
;
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
);
2225 "Error while deleting snapshot on '%s'\n",
2226 bdrv_get_device_name(bs
));
2235 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2237 BlockDriverState
*bs
, *bs1
;
2238 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2241 int saved_vm_running
;
2242 uint64_t vm_state_size
;
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 */
2249 while ((bs
= bdrv_next(bs
))) {
2251 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
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
));
2262 bs
= bdrv_snapshots();
2264 monitor_printf(mon
, "No block device can accept snapshots\n");
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
);
2280 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2282 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2283 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2285 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
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) {
2298 /* save the VM state */
2299 f
= qemu_fopen_bdrv(bs
, 1);
2301 monitor_printf(mon
, "Could not open VM state file\n");
2304 ret
= qemu_savevm_state(f
);
2305 vm_state_size
= qemu_ftell(f
);
2308 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2312 /* create the snapshots */
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
);
2321 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2322 bdrv_get_device_name(bs1
));
2328 if (saved_vm_running
)
2332 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2335 int saved_vm_running
;
2338 saved_vm_running
= runstate_is_running();
2339 vm_stop(RUN_STATE_SAVE_VM
);
2341 f
= qemu_fopen(filename
, "wb");
2343 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2346 ret
= qemu_save_device_state(f
);
2349 error_set(errp
, QERR_IO_ERROR
);
2353 if (saved_vm_running
)
2357 int load_vmstate(const char *name
)
2359 BlockDriverState
*bs
, *bs_vm_state
;
2360 QEMUSnapshotInfo sn
;
2364 bs_vm_state
= bdrv_snapshots();
2366 error_report("No block device supports snapshots");
2370 /* Don't even try to load empty VM states */
2371 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2374 } else if (sn
.vm_state_size
== 0) {
2375 error_report("This is a disk-only snapshot. Revert to it offline "
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. */
2383 while ((bs
= bdrv_next(bs
))) {
2385 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2389 if (!bdrv_can_snapshot(bs
)) {
2390 error_report("Device '%s' is writable but does not support snapshots.",
2391 bdrv_get_device_name(bs
));
2395 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2397 error_report("Device '%s' does not have the requested snapshot '%s'",
2398 bdrv_get_device_name(bs
), name
);
2403 /* Flush all IO requests so they don't interfere with the new state. */
2407 while ((bs
= bdrv_next(bs
))) {
2408 if (bdrv_can_snapshot(bs
)) {
2409 ret
= bdrv_snapshot_goto(bs
, name
);
2411 error_report("Error %d while activating snapshot '%s' on '%s'",
2412 ret
, name
, bdrv_get_device_name(bs
));
2418 /* restore the VM state */
2419 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2421 error_report("Could not open VM state file");
2425 qemu_system_reset(VMRESET_SILENT
);
2426 ret
= qemu_loadvm_state(f
);
2430 error_report("Error %d while loading VM state", ret
);
2437 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2439 BlockDriverState
*bs
, *bs1
;
2441 const char *name
= qdict_get_str(qdict
, "name");
2443 bs
= bdrv_snapshots();
2445 monitor_printf(mon
, "No block device supports snapshots\n");
2450 while ((bs1
= bdrv_next(bs1
))) {
2451 if (bdrv_can_snapshot(bs1
)) {
2452 ret
= bdrv_snapshot_delete(bs1
, name
);
2454 if (ret
== -ENOTSUP
)
2456 "Snapshots not supported on device '%s'\n",
2457 bdrv_get_device_name(bs1
));
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
;
2472 int *available_snapshots
;
2475 bs
= bdrv_snapshots();
2477 monitor_printf(mon
, "No available block device supports snapshots\n");
2481 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2483 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2488 monitor_printf(mon
, "There is no snapshot available.\n");
2492 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2494 for (i
= 0; i
< nb_sns
; i
++) {
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
);
2510 available_snapshots
[total
] = i
;
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
));
2522 monitor_printf(mon
, "There is no suitable snapshot available\n");
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
);