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"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
46 #define SELF_ANNOUNCE_ROUNDS 5
49 #define ETH_P_RARP 0x8035
51 #define ARP_HTYPE_ETH 0x0001
52 #define ARP_PTYPE_IP 0x0800
53 #define ARP_OP_REQUEST_REV 0x3
55 static int announce_self_create(uint8_t *buf
,
58 /* Ethernet header. */
59 memset(buf
, 0xff, 6); /* destination MAC addr */
60 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
61 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
64 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
65 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
66 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
67 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
69 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
70 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
72 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
74 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf
+ 42, 0x00, 18);
77 return 60; /* len (FCS will be added by hardware) */
80 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
85 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
87 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
91 static void qemu_announce_self_once(void *opaque
)
93 static int count
= SELF_ANNOUNCE_ROUNDS
;
94 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
96 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
99 /* delay 50ms, 150ms, 250ms, ... */
100 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
101 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
103 qemu_del_timer(timer
);
104 qemu_free_timer(timer
);
108 void qemu_announce_self(void)
110 static QEMUTimer
*timer
;
111 timer
= qemu_new_timer_ms(rt_clock
, qemu_announce_self_once
, &timer
);
112 qemu_announce_self_once(&timer
);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 #define IO_BUF_SIZE 32768
119 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
122 const QEMUFileOps
*ops
;
128 int64_t pos
; /* start of buffer when writing, end of buffer
131 int buf_size
; /* 0 when writing */
132 uint8_t buf
[IO_BUF_SIZE
];
134 struct iovec iov
[MAX_IOV_SIZE
];
140 typedef struct QEMUFileStdio
146 typedef struct QEMUFileSocket
157 static void fd_coroutine_enter(void *opaque
)
159 FDYieldUntilData
*data
= opaque
;
160 qemu_set_fd_handler(data
->fd
, NULL
, NULL
, NULL
);
161 qemu_coroutine_enter(data
->co
, NULL
);
165 * Yield until a file descriptor becomes readable
167 * Note that this function clobbers the handlers for the file descriptor.
169 static void coroutine_fn
yield_until_fd_readable(int fd
)
171 FDYieldUntilData data
;
173 assert(qemu_in_coroutine());
174 data
.co
= qemu_coroutine_self();
176 qemu_set_fd_handler(fd
, fd_coroutine_enter
, NULL
, &data
);
177 qemu_coroutine_yield();
180 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
183 QEMUFileSocket
*s
= opaque
;
185 ssize_t size
= iov_size(iov
, iovcnt
);
187 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
189 len
= -socket_error();
194 static int socket_get_fd(void *opaque
)
196 QEMUFileSocket
*s
= opaque
;
201 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
203 QEMUFileSocket
*s
= opaque
;
207 len
= qemu_recv(s
->fd
, buf
, size
, 0);
211 if (socket_error() == EAGAIN
) {
212 yield_until_fd_readable(s
->fd
);
213 } else if (socket_error() != EINTR
) {
219 len
= -socket_error();
224 static int socket_close(void *opaque
)
226 QEMUFileSocket
*s
= opaque
;
232 static int stdio_get_fd(void *opaque
)
234 QEMUFileStdio
*s
= opaque
;
236 return fileno(s
->stdio_file
);
239 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
241 QEMUFileStdio
*s
= opaque
;
242 return fwrite(buf
, 1, size
, s
->stdio_file
);
245 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
247 QEMUFileStdio
*s
= opaque
;
248 FILE *fp
= s
->stdio_file
;
253 bytes
= fread(buf
, 1, size
, fp
);
254 if (bytes
!= 0 || !ferror(fp
)) {
257 if (errno
== EAGAIN
) {
258 yield_until_fd_readable(fileno(fp
));
259 } else if (errno
!= EINTR
) {
266 static int stdio_pclose(void *opaque
)
268 QEMUFileStdio
*s
= opaque
;
270 ret
= pclose(s
->stdio_file
);
273 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
274 /* close succeeded, but non-zero exit code: */
275 ret
= -EIO
; /* fake errno value */
281 static int stdio_fclose(void *opaque
)
283 QEMUFileStdio
*s
= opaque
;
286 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
287 int fd
= fileno(s
->stdio_file
);
290 ret
= fstat(fd
, &st
);
291 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
293 * If the file handle is a regular file make sure the
294 * data is flushed to disk before signaling success.
303 if (fclose(s
->stdio_file
) == EOF
) {
310 static const QEMUFileOps stdio_pipe_read_ops
= {
311 .get_fd
= stdio_get_fd
,
312 .get_buffer
= stdio_get_buffer
,
313 .close
= stdio_pclose
316 static const QEMUFileOps stdio_pipe_write_ops
= {
317 .get_fd
= stdio_get_fd
,
318 .put_buffer
= stdio_put_buffer
,
319 .close
= stdio_pclose
322 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
327 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
328 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
332 stdio_file
= popen(command
, mode
);
333 if (stdio_file
== NULL
) {
337 s
= g_malloc0(sizeof(QEMUFileStdio
));
339 s
->stdio_file
= stdio_file
;
342 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
344 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
349 static const QEMUFileOps stdio_file_read_ops
= {
350 .get_fd
= stdio_get_fd
,
351 .get_buffer
= stdio_get_buffer
,
352 .close
= stdio_fclose
355 static const QEMUFileOps stdio_file_write_ops
= {
356 .get_fd
= stdio_get_fd
,
357 .put_buffer
= stdio_put_buffer
,
358 .close
= stdio_fclose
361 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
364 QEMUFileSocket
*s
= opaque
;
366 ssize_t size
= iov_size(iov
, iovcnt
);
372 /* Find the next start position; skip all full-sized vector elements */
373 while (offset
>= iov
[0].iov_len
) {
374 offset
-= iov
[0].iov_len
;
378 /* skip `offset' bytes from the (now) first element, undo it on exit */
380 iov
[0].iov_base
+= offset
;
381 iov
[0].iov_len
-= offset
;
384 len
= writev(s
->fd
, iov
, iovcnt
);
385 } while (len
== -1 && errno
== EINTR
);
390 /* Undo the changes above */
391 iov
[0].iov_base
-= offset
;
392 iov
[0].iov_len
+= offset
;
394 /* Prepare for the next iteration */
403 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
405 QEMUFileSocket
*s
= opaque
;
409 len
= read(s
->fd
, buf
, size
);
413 if (errno
== EAGAIN
) {
414 yield_until_fd_readable(s
->fd
);
415 } else if (errno
!= EINTR
) {
426 static int unix_close(void *opaque
)
428 QEMUFileSocket
*s
= opaque
;
434 static const QEMUFileOps unix_read_ops
= {
435 .get_fd
= socket_get_fd
,
436 .get_buffer
= unix_get_buffer
,
440 static const QEMUFileOps unix_write_ops
= {
441 .get_fd
= socket_get_fd
,
442 .writev_buffer
= unix_writev_buffer
,
446 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
451 (mode
[0] != 'r' && mode
[0] != 'w') ||
452 mode
[1] != 'b' || mode
[2] != 0) {
453 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
457 s
= g_malloc0(sizeof(QEMUFileSocket
));
461 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
463 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
468 static const QEMUFileOps socket_read_ops
= {
469 .get_fd
= socket_get_fd
,
470 .get_buffer
= socket_get_buffer
,
471 .close
= socket_close
474 static const QEMUFileOps socket_write_ops
= {
475 .get_fd
= socket_get_fd
,
476 .writev_buffer
= socket_writev_buffer
,
477 .close
= socket_close
480 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
482 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
485 (mode
[0] != 'r' && mode
[0] != 'w') ||
486 mode
[1] != 'b' || mode
[2] != 0) {
487 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
492 if (mode
[0] == 'w') {
493 qemu_set_block(s
->fd
);
494 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
496 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
501 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
506 (mode
[0] != 'r' && mode
[0] != 'w') ||
507 mode
[1] != 'b' || mode
[2] != 0) {
508 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
512 s
= g_malloc0(sizeof(QEMUFileStdio
));
514 s
->stdio_file
= fopen(filename
, mode
);
519 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
521 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
529 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
535 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
536 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
544 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
545 int64_t pos
, int size
)
547 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
551 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
553 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
556 static int bdrv_fclose(void *opaque
)
558 return bdrv_flush(opaque
);
561 static const QEMUFileOps bdrv_read_ops
= {
562 .get_buffer
= block_get_buffer
,
566 static const QEMUFileOps bdrv_write_ops
= {
567 .put_buffer
= block_put_buffer
,
568 .writev_buffer
= block_writev_buffer
,
572 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
575 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
576 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
579 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
583 f
= g_malloc0(sizeof(QEMUFile
));
590 int qemu_file_get_error(QEMUFile
*f
)
592 return f
->last_error
;
595 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
597 if (f
->last_error
== 0) {
602 static inline bool qemu_file_is_writable(QEMUFile
*f
)
604 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
608 * Flushes QEMUFile buffer
610 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
613 static void qemu_fflush(QEMUFile
*f
)
617 if (!qemu_file_is_writable(f
)) {
621 if (f
->ops
->writev_buffer
) {
623 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
626 if (f
->buf_index
> 0) {
627 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
636 qemu_file_set_error(f
, ret
);
640 static void qemu_fill_buffer(QEMUFile
*f
)
645 assert(!qemu_file_is_writable(f
));
647 pending
= f
->buf_size
- f
->buf_index
;
649 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
652 f
->buf_size
= pending
;
654 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
655 IO_BUF_SIZE
- pending
);
659 } else if (len
== 0) {
660 qemu_file_set_error(f
, -EIO
);
661 } else if (len
!= -EAGAIN
)
662 qemu_file_set_error(f
, len
);
665 int qemu_get_fd(QEMUFile
*f
)
667 if (f
->ops
->get_fd
) {
668 return f
->ops
->get_fd(f
->opaque
);
675 * Returns negative error value if any error happened on previous operations or
676 * while closing the file. Returns 0 or positive number on success.
678 * The meaning of return value on success depends on the specific backend
681 int qemu_fclose(QEMUFile
*f
)
685 ret
= qemu_file_get_error(f
);
688 int ret2
= f
->ops
->close(f
->opaque
);
693 /* If any error was spotted before closing, we should report it
694 * instead of the close() return value.
703 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
705 /* check for adjacent buffer and coalesce them */
706 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
707 f
->iov
[f
->iovcnt
- 1].iov_len
) {
708 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
710 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
711 f
->iov
[f
->iovcnt
++].iov_len
= size
;
714 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
719 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
721 if (!f
->ops
->writev_buffer
) {
722 qemu_put_buffer(f
, buf
, size
);
730 f
->bytes_xfer
+= size
;
731 add_to_iovec(f
, buf
, size
);
734 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
743 l
= IO_BUF_SIZE
- f
->buf_index
;
746 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
747 f
->bytes_xfer
+= size
;
748 if (f
->ops
->writev_buffer
) {
749 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
752 if (f
->buf_index
== IO_BUF_SIZE
) {
755 if (qemu_file_get_error(f
)) {
763 void qemu_put_byte(QEMUFile
*f
, int v
)
769 f
->buf
[f
->buf_index
] = v
;
771 if (f
->ops
->writev_buffer
) {
772 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
775 if (f
->buf_index
== IO_BUF_SIZE
) {
780 static void qemu_file_skip(QEMUFile
*f
, int size
)
782 if (f
->buf_index
+ size
<= f
->buf_size
) {
783 f
->buf_index
+= size
;
787 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
792 assert(!qemu_file_is_writable(f
));
794 index
= f
->buf_index
+ offset
;
795 pending
= f
->buf_size
- index
;
796 if (pending
< size
) {
798 index
= f
->buf_index
+ offset
;
799 pending
= f
->buf_size
- index
;
805 if (size
> pending
) {
809 memcpy(buf
, f
->buf
+ index
, size
);
813 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
818 while (pending
> 0) {
821 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
825 qemu_file_skip(f
, res
);
833 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
835 int index
= f
->buf_index
+ offset
;
837 assert(!qemu_file_is_writable(f
));
839 if (index
>= f
->buf_size
) {
841 index
= f
->buf_index
+ offset
;
842 if (index
>= f
->buf_size
) {
846 return f
->buf
[index
];
849 int qemu_get_byte(QEMUFile
*f
)
853 result
= qemu_peek_byte(f
, 0);
854 qemu_file_skip(f
, 1);
858 int64_t qemu_ftell(QEMUFile
*f
)
864 int qemu_file_rate_limit(QEMUFile
*f
)
866 if (qemu_file_get_error(f
)) {
869 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
875 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
877 return f
->xfer_limit
;
880 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
882 f
->xfer_limit
= limit
;
885 void qemu_file_reset_rate_limit(QEMUFile
*f
)
890 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
892 qemu_put_byte(f
, v
>> 8);
896 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
898 qemu_put_byte(f
, v
>> 24);
899 qemu_put_byte(f
, v
>> 16);
900 qemu_put_byte(f
, v
>> 8);
904 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
906 qemu_put_be32(f
, v
>> 32);
910 unsigned int qemu_get_be16(QEMUFile
*f
)
913 v
= qemu_get_byte(f
) << 8;
914 v
|= qemu_get_byte(f
);
918 unsigned int qemu_get_be32(QEMUFile
*f
)
921 v
= qemu_get_byte(f
) << 24;
922 v
|= qemu_get_byte(f
) << 16;
923 v
|= qemu_get_byte(f
) << 8;
924 v
|= qemu_get_byte(f
);
928 uint64_t qemu_get_be64(QEMUFile
*f
)
931 v
= (uint64_t)qemu_get_be32(f
) << 32;
932 v
|= qemu_get_be32(f
);
939 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
941 uint64_t expire_time
;
943 expire_time
= qemu_timer_expire_time_ns(ts
);
944 qemu_put_be64(f
, expire_time
);
947 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
949 uint64_t expire_time
;
951 expire_time
= qemu_get_be64(f
);
952 if (expire_time
!= -1) {
953 qemu_mod_timer_ns(ts
, expire_time
);
962 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
965 *v
= qemu_get_byte(f
);
969 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
972 qemu_put_byte(f
, *v
);
975 const VMStateInfo vmstate_info_bool
= {
983 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
990 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
996 const VMStateInfo vmstate_info_int8
= {
1004 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
1007 qemu_get_sbe16s(f
, v
);
1011 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
1014 qemu_put_sbe16s(f
, v
);
1017 const VMStateInfo vmstate_info_int16
= {
1025 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
1028 qemu_get_sbe32s(f
, v
);
1032 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
1035 qemu_put_sbe32s(f
, v
);
1038 const VMStateInfo vmstate_info_int32
= {
1044 /* 32 bit int. See that the received value is the same than the one
1047 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1051 qemu_get_sbe32s(f
, &v2
);
1058 const VMStateInfo vmstate_info_int32_equal
= {
1059 .name
= "int32 equal",
1060 .get
= get_int32_equal
,
1064 /* 32 bit int. See that the received value is the less or the same
1065 than the one in the field */
1067 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
1071 qemu_get_sbe32s(f
, &new);
1078 const VMStateInfo vmstate_info_int32_le
= {
1079 .name
= "int32 equal",
1080 .get
= get_int32_le
,
1086 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1089 qemu_get_sbe64s(f
, v
);
1093 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1096 qemu_put_sbe64s(f
, v
);
1099 const VMStateInfo vmstate_info_int64
= {
1105 /* 8 bit unsigned int */
1107 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1114 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1120 const VMStateInfo vmstate_info_uint8
= {
1126 /* 16 bit unsigned int */
1128 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1131 qemu_get_be16s(f
, v
);
1135 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1138 qemu_put_be16s(f
, v
);
1141 const VMStateInfo vmstate_info_uint16
= {
1147 /* 32 bit unsigned int */
1149 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1152 qemu_get_be32s(f
, v
);
1156 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1159 qemu_put_be32s(f
, v
);
1162 const VMStateInfo vmstate_info_uint32
= {
1168 /* 32 bit uint. See that the received value is the same than the one
1171 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1175 qemu_get_be32s(f
, &v2
);
1183 const VMStateInfo vmstate_info_uint32_equal
= {
1184 .name
= "uint32 equal",
1185 .get
= get_uint32_equal
,
1189 /* 64 bit unsigned int */
1191 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1194 qemu_get_be64s(f
, v
);
1198 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1201 qemu_put_be64s(f
, v
);
1204 const VMStateInfo vmstate_info_uint64
= {
1210 /* 64 bit unsigned int. See that the received value is the same than the one
1213 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1217 qemu_get_be64s(f
, &v2
);
1225 const VMStateInfo vmstate_info_uint64_equal
= {
1226 .name
= "int64 equal",
1227 .get
= get_uint64_equal
,
1231 /* 8 bit int. See that the received value is the same than the one
1234 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1238 qemu_get_8s(f
, &v2
);
1245 const VMStateInfo vmstate_info_uint8_equal
= {
1246 .name
= "uint8 equal",
1247 .get
= get_uint8_equal
,
1251 /* 16 bit unsigned int int. See that the received value is the same than the one
1254 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1258 qemu_get_be16s(f
, &v2
);
1265 const VMStateInfo vmstate_info_uint16_equal
= {
1266 .name
= "uint16 equal",
1267 .get
= get_uint16_equal
,
1271 /* floating point */
1273 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1277 *v
= make_float64(qemu_get_be64(f
));
1281 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1285 qemu_put_be64(f
, float64_val(*v
));
1288 const VMStateInfo vmstate_info_float64
= {
1296 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1299 qemu_get_timer(f
, v
);
1303 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1306 qemu_put_timer(f
, v
);
1309 const VMStateInfo vmstate_info_timer
= {
1315 /* uint8_t buffers */
1317 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1320 qemu_get_buffer(f
, v
, size
);
1324 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1327 qemu_put_buffer(f
, v
, size
);
1330 const VMStateInfo vmstate_info_buffer
= {
1336 /* unused buffers: space that was used for some fields that are
1337 not useful anymore */
1339 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1345 block_len
= MIN(sizeof(buf
), size
);
1347 qemu_get_buffer(f
, buf
, block_len
);
1352 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1354 static const uint8_t buf
[1024];
1358 block_len
= MIN(sizeof(buf
), size
);
1360 qemu_put_buffer(f
, buf
, block_len
);
1364 const VMStateInfo vmstate_info_unused_buffer
= {
1365 .name
= "unused_buffer",
1366 .get
= get_unused_buffer
,
1367 .put
= put_unused_buffer
,
1370 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1371 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1372 * bit words with the bits in big endian order. The in-memory format
1373 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1375 /* This is the number of 64 bit words sent over the wire */
1376 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1377 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1379 unsigned long *bmp
= pv
;
1381 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1382 uint64_t w
= qemu_get_be64(f
);
1384 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1385 bmp
[idx
++] = w
>> 32;
1391 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1393 unsigned long *bmp
= pv
;
1395 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1396 uint64_t w
= bmp
[idx
++];
1397 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1398 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1400 qemu_put_be64(f
, w
);
1404 const VMStateInfo vmstate_info_bitmap
= {
1410 typedef struct CompatEntry
{
1415 typedef struct SaveStateEntry
{
1416 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1422 SaveVMHandlers
*ops
;
1423 const VMStateDescription
*vmsd
;
1425 CompatEntry
*compat
;
1431 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1432 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1433 static int global_section_id
;
1435 static int calculate_new_instance_id(const char *idstr
)
1438 int instance_id
= 0;
1440 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1441 if (strcmp(idstr
, se
->idstr
) == 0
1442 && instance_id
<= se
->instance_id
) {
1443 instance_id
= se
->instance_id
+ 1;
1449 static int calculate_compat_instance_id(const char *idstr
)
1452 int instance_id
= 0;
1454 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1458 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1459 && instance_id
<= se
->compat
->instance_id
) {
1460 instance_id
= se
->compat
->instance_id
+ 1;
1466 /* TODO: Individual devices generally have very little idea about the rest
1467 of the system, so instance_id should be removed/replaced.
1468 Meanwhile pass -1 as instance_id if you do not already have a clearly
1469 distinguishing id for all instances of your device class. */
1470 int register_savevm_live(DeviceState
*dev
,
1474 SaveVMHandlers
*ops
,
1479 se
= g_malloc0(sizeof(SaveStateEntry
));
1480 se
->version_id
= version_id
;
1481 se
->section_id
= global_section_id
++;
1483 se
->opaque
= opaque
;
1486 /* if this is a live_savem then set is_ram */
1487 if (ops
->save_live_setup
!= NULL
) {
1492 char *id
= qdev_get_dev_path(dev
);
1494 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1495 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1498 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1499 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1500 se
->compat
->instance_id
= instance_id
== -1 ?
1501 calculate_compat_instance_id(idstr
) : instance_id
;
1505 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1507 if (instance_id
== -1) {
1508 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1510 se
->instance_id
= instance_id
;
1512 assert(!se
->compat
|| se
->instance_id
== 0);
1513 /* add at the end of list */
1514 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1518 int register_savevm(DeviceState
*dev
,
1522 SaveStateHandler
*save_state
,
1523 LoadStateHandler
*load_state
,
1526 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1527 ops
->save_state
= save_state
;
1528 ops
->load_state
= load_state
;
1529 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1533 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1535 SaveStateEntry
*se
, *new_se
;
1539 char *path
= qdev_get_dev_path(dev
);
1541 pstrcpy(id
, sizeof(id
), path
);
1542 pstrcat(id
, sizeof(id
), "/");
1546 pstrcat(id
, sizeof(id
), idstr
);
1548 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1549 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1550 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1560 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1561 const VMStateDescription
*vmsd
,
1562 void *opaque
, int alias_id
,
1563 int required_for_version
)
1567 /* If this triggers, alias support can be dropped for the vmsd. */
1568 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1570 se
= g_malloc0(sizeof(SaveStateEntry
));
1571 se
->version_id
= vmsd
->version_id
;
1572 se
->section_id
= global_section_id
++;
1573 se
->opaque
= opaque
;
1575 se
->alias_id
= alias_id
;
1576 se
->no_migrate
= vmsd
->unmigratable
;
1579 char *id
= qdev_get_dev_path(dev
);
1581 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1582 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1585 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1586 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1587 se
->compat
->instance_id
= instance_id
== -1 ?
1588 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1592 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1594 if (instance_id
== -1) {
1595 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1597 se
->instance_id
= instance_id
;
1599 assert(!se
->compat
|| se
->instance_id
== 0);
1600 /* add at the end of list */
1601 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1605 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1608 SaveStateEntry
*se
, *new_se
;
1610 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1611 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1612 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1621 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1623 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1626 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1627 void *opaque
, int version_id
)
1629 VMStateField
*field
= vmsd
->fields
;
1632 if (version_id
> vmsd
->version_id
) {
1635 if (version_id
< vmsd
->minimum_version_id_old
) {
1638 if (version_id
< vmsd
->minimum_version_id
) {
1639 return vmsd
->load_state_old(f
, opaque
, version_id
);
1641 if (vmsd
->pre_load
) {
1642 int ret
= vmsd
->pre_load(opaque
);
1646 while(field
->name
) {
1647 if ((field
->field_exists
&&
1648 field
->field_exists(opaque
, version_id
)) ||
1649 (!field
->field_exists
&&
1650 field
->version_id
<= version_id
)) {
1651 void *base_addr
= opaque
+ field
->offset
;
1653 int size
= field
->size
;
1655 if (field
->flags
& VMS_VBUFFER
) {
1656 size
= *(int32_t *)(opaque
+field
->size_offset
);
1657 if (field
->flags
& VMS_MULTIPLY
) {
1658 size
*= field
->size
;
1661 if (field
->flags
& VMS_ARRAY
) {
1662 n_elems
= field
->num
;
1663 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1664 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1665 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1666 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1667 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1668 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1669 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1670 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1672 if (field
->flags
& VMS_POINTER
) {
1673 base_addr
= *(void **)base_addr
+ field
->start
;
1675 for (i
= 0; i
< n_elems
; i
++) {
1676 void *addr
= base_addr
+ size
* i
;
1678 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1679 addr
= *(void **)addr
;
1681 if (field
->flags
& VMS_STRUCT
) {
1682 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1684 ret
= field
->info
->get(f
, addr
, size
);
1694 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1698 if (vmsd
->post_load
) {
1699 return vmsd
->post_load(opaque
, version_id
);
1704 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1707 VMStateField
*field
= vmsd
->fields
;
1709 if (vmsd
->pre_save
) {
1710 vmsd
->pre_save(opaque
);
1712 while(field
->name
) {
1713 if (!field
->field_exists
||
1714 field
->field_exists(opaque
, vmsd
->version_id
)) {
1715 void *base_addr
= opaque
+ field
->offset
;
1717 int size
= field
->size
;
1719 if (field
->flags
& VMS_VBUFFER
) {
1720 size
= *(int32_t *)(opaque
+field
->size_offset
);
1721 if (field
->flags
& VMS_MULTIPLY
) {
1722 size
*= field
->size
;
1725 if (field
->flags
& VMS_ARRAY
) {
1726 n_elems
= field
->num
;
1727 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1728 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1729 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1730 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1731 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1732 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1733 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1734 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1736 if (field
->flags
& VMS_POINTER
) {
1737 base_addr
= *(void **)base_addr
+ field
->start
;
1739 for (i
= 0; i
< n_elems
; i
++) {
1740 void *addr
= base_addr
+ size
* i
;
1742 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1743 addr
= *(void **)addr
;
1745 if (field
->flags
& VMS_STRUCT
) {
1746 vmstate_save_state(f
, field
->vmsd
, addr
);
1748 field
->info
->put(f
, addr
, size
);
1754 vmstate_subsection_save(f
, vmsd
, opaque
);
1757 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1759 if (!se
->vmsd
) { /* Old style */
1760 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1762 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1765 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1767 if (!se
->vmsd
) { /* Old style */
1768 se
->ops
->save_state(f
, se
->opaque
);
1771 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1774 #define QEMU_VM_FILE_MAGIC 0x5145564d
1775 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1776 #define QEMU_VM_FILE_VERSION 0x00000003
1778 #define QEMU_VM_EOF 0x00
1779 #define QEMU_VM_SECTION_START 0x01
1780 #define QEMU_VM_SECTION_PART 0x02
1781 #define QEMU_VM_SECTION_END 0x03
1782 #define QEMU_VM_SECTION_FULL 0x04
1783 #define QEMU_VM_SUBSECTION 0x05
1785 bool qemu_savevm_state_blocked(Error
**errp
)
1789 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1790 if (se
->no_migrate
) {
1791 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1798 void qemu_savevm_state_begin(QEMUFile
*f
,
1799 const MigrationParams
*params
)
1804 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1805 if (!se
->ops
|| !se
->ops
->set_params
) {
1808 se
->ops
->set_params(params
, se
->opaque
);
1811 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1812 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1814 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1817 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1820 if (se
->ops
&& se
->ops
->is_active
) {
1821 if (!se
->ops
->is_active(se
->opaque
)) {
1826 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1827 qemu_put_be32(f
, se
->section_id
);
1830 len
= strlen(se
->idstr
);
1831 qemu_put_byte(f
, len
);
1832 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1834 qemu_put_be32(f
, se
->instance_id
);
1835 qemu_put_be32(f
, se
->version_id
);
1837 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1839 qemu_file_set_error(f
, ret
);
1846 * this function has three return values:
1847 * negative: there was one error, and we have -errno.
1848 * 0 : We haven't finished, caller have to go again
1849 * 1 : We have finished, we can go to complete phase
1851 int qemu_savevm_state_iterate(QEMUFile
*f
)
1856 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1857 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1860 if (se
->ops
&& se
->ops
->is_active
) {
1861 if (!se
->ops
->is_active(se
->opaque
)) {
1865 if (qemu_file_rate_limit(f
)) {
1868 trace_savevm_section_start();
1870 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1871 qemu_put_be32(f
, se
->section_id
);
1873 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1874 trace_savevm_section_end(se
->section_id
);
1877 qemu_file_set_error(f
, ret
);
1880 /* Do not proceed to the next vmstate before this one reported
1881 completion of the current stage. This serializes the migration
1882 and reduces the probability that a faster changing state is
1883 synchronized over and over again. */
1890 void qemu_savevm_state_complete(QEMUFile
*f
)
1895 cpu_synchronize_all_states();
1897 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1898 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1901 if (se
->ops
&& se
->ops
->is_active
) {
1902 if (!se
->ops
->is_active(se
->opaque
)) {
1906 trace_savevm_section_start();
1908 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1909 qemu_put_be32(f
, se
->section_id
);
1911 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1912 trace_savevm_section_end(se
->section_id
);
1914 qemu_file_set_error(f
, ret
);
1919 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1922 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1925 trace_savevm_section_start();
1927 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1928 qemu_put_be32(f
, se
->section_id
);
1931 len
= strlen(se
->idstr
);
1932 qemu_put_byte(f
, len
);
1933 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1935 qemu_put_be32(f
, se
->instance_id
);
1936 qemu_put_be32(f
, se
->version_id
);
1938 vmstate_save(f
, se
);
1939 trace_savevm_section_end(se
->section_id
);
1942 qemu_put_byte(f
, QEMU_VM_EOF
);
1946 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1951 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1952 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1955 if (se
->ops
&& se
->ops
->is_active
) {
1956 if (!se
->ops
->is_active(se
->opaque
)) {
1960 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1965 void qemu_savevm_state_cancel(void)
1969 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1970 if (se
->ops
&& se
->ops
->cancel
) {
1971 se
->ops
->cancel(se
->opaque
);
1976 static int qemu_savevm_state(QEMUFile
*f
)
1979 MigrationParams params
= {
1984 if (qemu_savevm_state_blocked(NULL
)) {
1988 qemu_mutex_unlock_iothread();
1989 qemu_savevm_state_begin(f
, ¶ms
);
1990 qemu_mutex_lock_iothread();
1992 while (qemu_file_get_error(f
) == 0) {
1993 if (qemu_savevm_state_iterate(f
) > 0) {
1998 ret
= qemu_file_get_error(f
);
2000 qemu_savevm_state_complete(f
);
2001 ret
= qemu_file_get_error(f
);
2004 qemu_savevm_state_cancel();
2009 static int qemu_save_device_state(QEMUFile
*f
)
2013 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
2014 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
2016 cpu_synchronize_all_states();
2018 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2024 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
2029 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
2030 qemu_put_be32(f
, se
->section_id
);
2033 len
= strlen(se
->idstr
);
2034 qemu_put_byte(f
, len
);
2035 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
2037 qemu_put_be32(f
, se
->instance_id
);
2038 qemu_put_be32(f
, se
->version_id
);
2040 vmstate_save(f
, se
);
2043 qemu_put_byte(f
, QEMU_VM_EOF
);
2045 return qemu_file_get_error(f
);
2048 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
2052 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2053 if (!strcmp(se
->idstr
, idstr
) &&
2054 (instance_id
== se
->instance_id
||
2055 instance_id
== se
->alias_id
))
2057 /* Migrating from an older version? */
2058 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
2059 if (!strcmp(se
->compat
->idstr
, idstr
) &&
2060 (instance_id
== se
->compat
->instance_id
||
2061 instance_id
== se
->alias_id
))
2068 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
2070 while(sub
&& sub
->needed
) {
2071 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
2079 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2082 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
2085 uint8_t version_id
, len
, size
;
2086 const VMStateDescription
*sub_vmsd
;
2088 len
= qemu_peek_byte(f
, 1);
2089 if (len
< strlen(vmsd
->name
) + 1) {
2090 /* subsection name has be be "section_name/a" */
2093 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2099 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2100 /* it don't have a valid subsection name */
2103 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2104 if (sub_vmsd
== NULL
) {
2107 qemu_file_skip(f
, 1); /* subsection */
2108 qemu_file_skip(f
, 1); /* len */
2109 qemu_file_skip(f
, len
); /* idstr */
2110 version_id
= qemu_get_be32(f
);
2112 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2120 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2123 const VMStateSubsection
*sub
= vmsd
->subsections
;
2125 while (sub
&& sub
->needed
) {
2126 if (sub
->needed(opaque
)) {
2127 const VMStateDescription
*vmsd
= sub
->vmsd
;
2130 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2131 len
= strlen(vmsd
->name
);
2132 qemu_put_byte(f
, len
);
2133 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2134 qemu_put_be32(f
, vmsd
->version_id
);
2135 vmstate_save_state(f
, vmsd
, opaque
);
2141 typedef struct LoadStateEntry
{
2142 QLIST_ENTRY(LoadStateEntry
) entry
;
2148 int qemu_loadvm_state(QEMUFile
*f
)
2150 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2151 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2152 LoadStateEntry
*le
, *new_le
;
2153 uint8_t section_type
;
2157 if (qemu_savevm_state_blocked(NULL
)) {
2161 v
= qemu_get_be32(f
);
2162 if (v
!= QEMU_VM_FILE_MAGIC
)
2165 v
= qemu_get_be32(f
);
2166 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2167 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2170 if (v
!= QEMU_VM_FILE_VERSION
)
2173 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2174 uint32_t instance_id
, version_id
, section_id
;
2179 switch (section_type
) {
2180 case QEMU_VM_SECTION_START
:
2181 case QEMU_VM_SECTION_FULL
:
2182 /* Read section start */
2183 section_id
= qemu_get_be32(f
);
2184 len
= qemu_get_byte(f
);
2185 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2187 instance_id
= qemu_get_be32(f
);
2188 version_id
= qemu_get_be32(f
);
2190 /* Find savevm section */
2191 se
= find_se(idstr
, instance_id
);
2193 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2198 /* Validate version */
2199 if (version_id
> se
->version_id
) {
2200 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2201 version_id
, idstr
, se
->version_id
);
2207 le
= g_malloc0(sizeof(*le
));
2210 le
->section_id
= section_id
;
2211 le
->version_id
= version_id
;
2212 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2214 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2216 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2217 instance_id
, idstr
);
2221 case QEMU_VM_SECTION_PART
:
2222 case QEMU_VM_SECTION_END
:
2223 section_id
= qemu_get_be32(f
);
2225 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2226 if (le
->section_id
== section_id
) {
2231 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2236 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2238 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2244 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2250 cpu_synchronize_all_post_init();
2255 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2256 QLIST_REMOVE(le
, entry
);
2261 ret
= qemu_file_get_error(f
);
2267 static BlockDriverState
*find_vmstate_bs(void)
2269 BlockDriverState
*bs
= NULL
;
2270 while ((bs
= bdrv_next(bs
))) {
2271 if (bdrv_can_snapshot(bs
)) {
2279 * Deletes snapshots of a given name in all opened images.
2281 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2283 BlockDriverState
*bs
;
2284 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2288 while ((bs
= bdrv_next(bs
))) {
2289 if (bdrv_can_snapshot(bs
) &&
2290 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2292 ret
= bdrv_snapshot_delete(bs
, name
);
2295 "Error while deleting snapshot on '%s'\n",
2296 bdrv_get_device_name(bs
));
2305 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2307 BlockDriverState
*bs
, *bs1
;
2308 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2311 int saved_vm_running
;
2312 uint64_t vm_state_size
;
2315 const char *name
= qdict_get_try_str(qdict
, "name");
2317 /* Verify if there is a device that doesn't support snapshots and is writable */
2319 while ((bs
= bdrv_next(bs
))) {
2321 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2325 if (!bdrv_can_snapshot(bs
)) {
2326 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2327 bdrv_get_device_name(bs
));
2332 bs
= find_vmstate_bs();
2334 monitor_printf(mon
, "No block device can accept snapshots\n");
2338 saved_vm_running
= runstate_is_running();
2339 vm_stop(RUN_STATE_SAVE_VM
);
2341 memset(sn
, 0, sizeof(*sn
));
2343 /* fill auxiliary fields */
2344 qemu_gettimeofday(&tv
);
2345 sn
->date_sec
= tv
.tv_sec
;
2346 sn
->date_nsec
= tv
.tv_usec
* 1000;
2347 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2350 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2352 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2353 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2355 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2358 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2359 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2360 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2363 /* Delete old snapshots of the same name */
2364 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2368 /* save the VM state */
2369 f
= qemu_fopen_bdrv(bs
, 1);
2371 monitor_printf(mon
, "Could not open VM state file\n");
2374 ret
= qemu_savevm_state(f
);
2375 vm_state_size
= qemu_ftell(f
);
2378 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2382 /* create the snapshots */
2385 while ((bs1
= bdrv_next(bs1
))) {
2386 if (bdrv_can_snapshot(bs1
)) {
2387 /* Write VM state size only to the image that contains the state */
2388 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2389 ret
= bdrv_snapshot_create(bs1
, sn
);
2391 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2392 bdrv_get_device_name(bs1
));
2398 if (saved_vm_running
)
2402 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2405 int saved_vm_running
;
2408 saved_vm_running
= runstate_is_running();
2409 vm_stop(RUN_STATE_SAVE_VM
);
2411 f
= qemu_fopen(filename
, "wb");
2413 error_setg_file_open(errp
, errno
, filename
);
2416 ret
= qemu_save_device_state(f
);
2419 error_set(errp
, QERR_IO_ERROR
);
2423 if (saved_vm_running
)
2427 int load_vmstate(const char *name
)
2429 BlockDriverState
*bs
, *bs_vm_state
;
2430 QEMUSnapshotInfo sn
;
2434 bs_vm_state
= find_vmstate_bs();
2436 error_report("No block device supports snapshots");
2440 /* Don't even try to load empty VM states */
2441 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2444 } else if (sn
.vm_state_size
== 0) {
2445 error_report("This is a disk-only snapshot. Revert to it offline "
2450 /* Verify if there is any device that doesn't support snapshots and is
2451 writable and check if the requested snapshot is available too. */
2453 while ((bs
= bdrv_next(bs
))) {
2455 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2459 if (!bdrv_can_snapshot(bs
)) {
2460 error_report("Device '%s' is writable but does not support snapshots.",
2461 bdrv_get_device_name(bs
));
2465 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2467 error_report("Device '%s' does not have the requested snapshot '%s'",
2468 bdrv_get_device_name(bs
), name
);
2473 /* Flush all IO requests so they don't interfere with the new state. */
2477 while ((bs
= bdrv_next(bs
))) {
2478 if (bdrv_can_snapshot(bs
)) {
2479 ret
= bdrv_snapshot_goto(bs
, name
);
2481 error_report("Error %d while activating snapshot '%s' on '%s'",
2482 ret
, name
, bdrv_get_device_name(bs
));
2488 /* restore the VM state */
2489 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2491 error_report("Could not open VM state file");
2495 qemu_system_reset(VMRESET_SILENT
);
2496 ret
= qemu_loadvm_state(f
);
2500 error_report("Error %d while loading VM state", ret
);
2507 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2509 BlockDriverState
*bs
, *bs1
;
2511 const char *name
= qdict_get_str(qdict
, "name");
2513 bs
= find_vmstate_bs();
2515 monitor_printf(mon
, "No block device supports snapshots\n");
2520 while ((bs1
= bdrv_next(bs1
))) {
2521 if (bdrv_can_snapshot(bs1
)) {
2522 ret
= bdrv_snapshot_delete(bs1
, name
);
2524 if (ret
== -ENOTSUP
)
2526 "Snapshots not supported on device '%s'\n",
2527 bdrv_get_device_name(bs1
));
2529 monitor_printf(mon
, "Error %d while deleting snapshot on "
2530 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2536 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2538 BlockDriverState
*bs
, *bs1
;
2539 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2540 int nb_sns
, i
, ret
, available
;
2542 int *available_snapshots
;
2544 bs
= find_vmstate_bs();
2546 monitor_printf(mon
, "No available block device supports snapshots\n");
2550 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2552 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2557 monitor_printf(mon
, "There is no snapshot available.\n");
2561 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2563 for (i
= 0; i
< nb_sns
; i
++) {
2568 while ((bs1
= bdrv_next(bs1
))) {
2569 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2570 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2579 available_snapshots
[total
] = i
;
2585 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
2586 monitor_printf(mon
, "\n");
2587 for (i
= 0; i
< total
; i
++) {
2588 sn
= &sn_tab
[available_snapshots
[i
]];
2589 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
2590 monitor_printf(mon
, "\n");
2593 monitor_printf(mon
, "There is no suitable snapshot available\n");
2597 g_free(available_snapshots
);
2601 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2603 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2604 memory_region_name(mr
), dev
);
2607 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2609 /* Nothing do to while the implementation is in RAMBlock */
2612 void vmstate_register_ram_global(MemoryRegion
*mr
)
2614 vmstate_register_ram(mr
, NULL
);