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_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
224 QEMUFileSocket
*s
= opaque
;
227 len
= qemu_send_full(s
->fd
, buf
, size
, 0);
229 len
= -socket_error();
234 static int socket_close(void *opaque
)
236 QEMUFileSocket
*s
= opaque
;
242 static int stdio_get_fd(void *opaque
)
244 QEMUFileStdio
*s
= opaque
;
246 return fileno(s
->stdio_file
);
249 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
251 QEMUFileStdio
*s
= opaque
;
252 return fwrite(buf
, 1, size
, s
->stdio_file
);
255 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
257 QEMUFileStdio
*s
= opaque
;
258 FILE *fp
= s
->stdio_file
;
263 bytes
= fread(buf
, 1, size
, fp
);
264 if (bytes
!= 0 || !ferror(fp
)) {
267 if (errno
== EAGAIN
) {
268 yield_until_fd_readable(fileno(fp
));
269 } else if (errno
!= EINTR
) {
276 static int stdio_pclose(void *opaque
)
278 QEMUFileStdio
*s
= opaque
;
280 ret
= pclose(s
->stdio_file
);
283 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
284 /* close succeeded, but non-zero exit code: */
285 ret
= -EIO
; /* fake errno value */
291 static int stdio_fclose(void *opaque
)
293 QEMUFileStdio
*s
= opaque
;
296 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
297 int fd
= fileno(s
->stdio_file
);
300 ret
= fstat(fd
, &st
);
301 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
303 * If the file handle is a regular file make sure the
304 * data is flushed to disk before signaling success.
313 if (fclose(s
->stdio_file
) == EOF
) {
320 static const QEMUFileOps stdio_pipe_read_ops
= {
321 .get_fd
= stdio_get_fd
,
322 .get_buffer
= stdio_get_buffer
,
323 .close
= stdio_pclose
326 static const QEMUFileOps stdio_pipe_write_ops
= {
327 .get_fd
= stdio_get_fd
,
328 .put_buffer
= stdio_put_buffer
,
329 .close
= stdio_pclose
332 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
337 stdio_file
= popen(command
, mode
);
338 if (stdio_file
== NULL
) {
342 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
343 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
347 s
= g_malloc0(sizeof(QEMUFileStdio
));
349 s
->stdio_file
= stdio_file
;
352 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
354 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
359 static const QEMUFileOps stdio_file_read_ops
= {
360 .get_fd
= stdio_get_fd
,
361 .get_buffer
= stdio_get_buffer
,
362 .close
= stdio_fclose
365 static const QEMUFileOps stdio_file_write_ops
= {
366 .get_fd
= stdio_get_fd
,
367 .put_buffer
= stdio_put_buffer
,
368 .close
= stdio_fclose
371 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
376 (mode
[0] != 'r' && mode
[0] != 'w') ||
377 mode
[1] != 'b' || mode
[2] != 0) {
378 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
382 s
= g_malloc0(sizeof(QEMUFileStdio
));
383 s
->stdio_file
= fdopen(fd
, mode
);
388 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
390 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
399 static const QEMUFileOps socket_read_ops
= {
400 .get_fd
= socket_get_fd
,
401 .get_buffer
= socket_get_buffer
,
402 .close
= socket_close
405 static const QEMUFileOps socket_write_ops
= {
406 .get_fd
= socket_get_fd
,
407 .put_buffer
= socket_put_buffer
,
408 .writev_buffer
= socket_writev_buffer
,
409 .close
= socket_close
412 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
414 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
417 (mode
[0] != 'r' && mode
[0] != 'w') ||
418 mode
[1] != 'b' || mode
[2] != 0) {
419 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
424 if (mode
[0] == 'w') {
425 qemu_set_block(s
->fd
);
426 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
428 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
433 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
438 (mode
[0] != 'r' && mode
[0] != 'w') ||
439 mode
[1] != 'b' || mode
[2] != 0) {
440 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
444 s
= g_malloc0(sizeof(QEMUFileStdio
));
446 s
->stdio_file
= fopen(filename
, mode
);
451 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
453 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
461 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
467 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
468 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
476 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
477 int64_t pos
, int size
)
479 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
483 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
485 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
488 static int bdrv_fclose(void *opaque
)
490 return bdrv_flush(opaque
);
493 static const QEMUFileOps bdrv_read_ops
= {
494 .get_buffer
= block_get_buffer
,
498 static const QEMUFileOps bdrv_write_ops
= {
499 .put_buffer
= block_put_buffer
,
500 .writev_buffer
= block_writev_buffer
,
504 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
507 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
508 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
511 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
515 f
= g_malloc0(sizeof(QEMUFile
));
522 int qemu_file_get_error(QEMUFile
*f
)
524 return f
->last_error
;
527 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
529 if (f
->last_error
== 0) {
534 static inline bool qemu_file_is_writable(QEMUFile
*f
)
536 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
540 * Flushes QEMUFile buffer
542 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
545 static void qemu_fflush(QEMUFile
*f
)
549 if (!qemu_file_is_writable(f
)) {
553 if (f
->ops
->writev_buffer
) {
555 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
558 if (f
->buf_index
> 0) {
559 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
568 qemu_file_set_error(f
, ret
);
572 static void qemu_fill_buffer(QEMUFile
*f
)
577 assert(!qemu_file_is_writable(f
));
579 pending
= f
->buf_size
- f
->buf_index
;
581 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
584 f
->buf_size
= pending
;
586 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
587 IO_BUF_SIZE
- pending
);
591 } else if (len
== 0) {
592 qemu_file_set_error(f
, -EIO
);
593 } else if (len
!= -EAGAIN
)
594 qemu_file_set_error(f
, len
);
597 int qemu_get_fd(QEMUFile
*f
)
599 if (f
->ops
->get_fd
) {
600 return f
->ops
->get_fd(f
->opaque
);
607 * Returns negative error value if any error happened on previous operations or
608 * while closing the file. Returns 0 or positive number on success.
610 * The meaning of return value on success depends on the specific backend
613 int qemu_fclose(QEMUFile
*f
)
617 ret
= qemu_file_get_error(f
);
620 int ret2
= f
->ops
->close(f
->opaque
);
625 /* If any error was spotted before closing, we should report it
626 * instead of the close() return value.
635 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
637 /* check for adjacent buffer and coalesce them */
638 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
639 f
->iov
[f
->iovcnt
- 1].iov_len
) {
640 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
642 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
643 f
->iov
[f
->iovcnt
++].iov_len
= size
;
646 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
651 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
653 if (!f
->ops
->writev_buffer
) {
654 qemu_put_buffer(f
, buf
, size
);
662 f
->bytes_xfer
+= size
;
663 add_to_iovec(f
, buf
, size
);
666 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
675 l
= IO_BUF_SIZE
- f
->buf_index
;
678 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
679 f
->bytes_xfer
+= size
;
680 if (f
->ops
->writev_buffer
) {
681 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
684 if (f
->buf_index
== IO_BUF_SIZE
) {
687 if (qemu_file_get_error(f
)) {
695 void qemu_put_byte(QEMUFile
*f
, int v
)
701 f
->buf
[f
->buf_index
] = v
;
703 if (f
->ops
->writev_buffer
) {
704 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
707 if (f
->buf_index
== IO_BUF_SIZE
) {
712 static void qemu_file_skip(QEMUFile
*f
, int size
)
714 if (f
->buf_index
+ size
<= f
->buf_size
) {
715 f
->buf_index
+= size
;
719 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
724 assert(!qemu_file_is_writable(f
));
726 index
= f
->buf_index
+ offset
;
727 pending
= f
->buf_size
- index
;
728 if (pending
< size
) {
730 index
= f
->buf_index
+ offset
;
731 pending
= f
->buf_size
- index
;
737 if (size
> pending
) {
741 memcpy(buf
, f
->buf
+ index
, size
);
745 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
750 while (pending
> 0) {
753 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
757 qemu_file_skip(f
, res
);
765 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
767 int index
= f
->buf_index
+ offset
;
769 assert(!qemu_file_is_writable(f
));
771 if (index
>= f
->buf_size
) {
773 index
= f
->buf_index
+ offset
;
774 if (index
>= f
->buf_size
) {
778 return f
->buf
[index
];
781 int qemu_get_byte(QEMUFile
*f
)
785 result
= qemu_peek_byte(f
, 0);
786 qemu_file_skip(f
, 1);
790 int64_t qemu_ftell(QEMUFile
*f
)
796 int qemu_file_rate_limit(QEMUFile
*f
)
798 if (qemu_file_get_error(f
)) {
801 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
807 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
809 return f
->xfer_limit
;
812 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
814 f
->xfer_limit
= limit
;
817 void qemu_file_reset_rate_limit(QEMUFile
*f
)
822 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
824 qemu_put_byte(f
, v
>> 8);
828 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
830 qemu_put_byte(f
, v
>> 24);
831 qemu_put_byte(f
, v
>> 16);
832 qemu_put_byte(f
, v
>> 8);
836 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
838 qemu_put_be32(f
, v
>> 32);
842 unsigned int qemu_get_be16(QEMUFile
*f
)
845 v
= qemu_get_byte(f
) << 8;
846 v
|= qemu_get_byte(f
);
850 unsigned int qemu_get_be32(QEMUFile
*f
)
853 v
= qemu_get_byte(f
) << 24;
854 v
|= qemu_get_byte(f
) << 16;
855 v
|= qemu_get_byte(f
) << 8;
856 v
|= qemu_get_byte(f
);
860 uint64_t qemu_get_be64(QEMUFile
*f
)
863 v
= (uint64_t)qemu_get_be32(f
) << 32;
864 v
|= qemu_get_be32(f
);
871 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
873 uint64_t expire_time
;
875 expire_time
= qemu_timer_expire_time_ns(ts
);
876 qemu_put_be64(f
, expire_time
);
879 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
881 uint64_t expire_time
;
883 expire_time
= qemu_get_be64(f
);
884 if (expire_time
!= -1) {
885 qemu_mod_timer_ns(ts
, expire_time
);
894 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
897 *v
= qemu_get_byte(f
);
901 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
904 qemu_put_byte(f
, *v
);
907 const VMStateInfo vmstate_info_bool
= {
915 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
922 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
928 const VMStateInfo vmstate_info_int8
= {
936 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
939 qemu_get_sbe16s(f
, v
);
943 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
946 qemu_put_sbe16s(f
, v
);
949 const VMStateInfo vmstate_info_int16
= {
957 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
960 qemu_get_sbe32s(f
, v
);
964 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
967 qemu_put_sbe32s(f
, v
);
970 const VMStateInfo vmstate_info_int32
= {
976 /* 32 bit int. See that the received value is the same than the one
979 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
983 qemu_get_sbe32s(f
, &v2
);
990 const VMStateInfo vmstate_info_int32_equal
= {
991 .name
= "int32 equal",
992 .get
= get_int32_equal
,
996 /* 32 bit int. See that the received value is the less or the same
997 than the one in the field */
999 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
1003 qemu_get_sbe32s(f
, &new);
1010 const VMStateInfo vmstate_info_int32_le
= {
1011 .name
= "int32 equal",
1012 .get
= get_int32_le
,
1018 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1021 qemu_get_sbe64s(f
, v
);
1025 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1028 qemu_put_sbe64s(f
, v
);
1031 const VMStateInfo vmstate_info_int64
= {
1037 /* 8 bit unsigned int */
1039 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1046 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1052 const VMStateInfo vmstate_info_uint8
= {
1058 /* 16 bit unsigned int */
1060 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1063 qemu_get_be16s(f
, v
);
1067 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1070 qemu_put_be16s(f
, v
);
1073 const VMStateInfo vmstate_info_uint16
= {
1079 /* 32 bit unsigned int */
1081 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1084 qemu_get_be32s(f
, v
);
1088 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1091 qemu_put_be32s(f
, v
);
1094 const VMStateInfo vmstate_info_uint32
= {
1100 /* 32 bit uint. See that the received value is the same than the one
1103 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1107 qemu_get_be32s(f
, &v2
);
1115 const VMStateInfo vmstate_info_uint32_equal
= {
1116 .name
= "uint32 equal",
1117 .get
= get_uint32_equal
,
1121 /* 64 bit unsigned int */
1123 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1126 qemu_get_be64s(f
, v
);
1130 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1133 qemu_put_be64s(f
, v
);
1136 const VMStateInfo vmstate_info_uint64
= {
1142 /* 64 bit unsigned int. See that the received value is the same than the one
1145 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1149 qemu_get_be64s(f
, &v2
);
1157 const VMStateInfo vmstate_info_uint64_equal
= {
1158 .name
= "int64 equal",
1159 .get
= get_uint64_equal
,
1163 /* 8 bit int. See that the received value is the same than the one
1166 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1170 qemu_get_8s(f
, &v2
);
1177 const VMStateInfo vmstate_info_uint8_equal
= {
1178 .name
= "uint8 equal",
1179 .get
= get_uint8_equal
,
1183 /* 16 bit unsigned int int. See that the received value is the same than the one
1186 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1190 qemu_get_be16s(f
, &v2
);
1197 const VMStateInfo vmstate_info_uint16_equal
= {
1198 .name
= "uint16 equal",
1199 .get
= get_uint16_equal
,
1203 /* floating point */
1205 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1209 *v
= make_float64(qemu_get_be64(f
));
1213 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1217 qemu_put_be64(f
, float64_val(*v
));
1220 const VMStateInfo vmstate_info_float64
= {
1228 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1231 qemu_get_timer(f
, v
);
1235 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1238 qemu_put_timer(f
, v
);
1241 const VMStateInfo vmstate_info_timer
= {
1247 /* uint8_t buffers */
1249 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1252 qemu_get_buffer(f
, v
, size
);
1256 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1259 qemu_put_buffer(f
, v
, size
);
1262 const VMStateInfo vmstate_info_buffer
= {
1268 /* unused buffers: space that was used for some fields that are
1269 not useful anymore */
1271 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1277 block_len
= MIN(sizeof(buf
), size
);
1279 qemu_get_buffer(f
, buf
, block_len
);
1284 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1286 static const uint8_t buf
[1024];
1290 block_len
= MIN(sizeof(buf
), size
);
1292 qemu_put_buffer(f
, buf
, block_len
);
1296 const VMStateInfo vmstate_info_unused_buffer
= {
1297 .name
= "unused_buffer",
1298 .get
= get_unused_buffer
,
1299 .put
= put_unused_buffer
,
1302 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1303 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1304 * bit words with the bits in big endian order. The in-memory format
1305 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1307 /* This is the number of 64 bit words sent over the wire */
1308 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1309 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1311 unsigned long *bmp
= pv
;
1313 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1314 uint64_t w
= qemu_get_be64(f
);
1316 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1317 bmp
[idx
++] = w
>> 32;
1323 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1325 unsigned long *bmp
= pv
;
1327 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1328 uint64_t w
= bmp
[idx
++];
1329 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1330 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1332 qemu_put_be64(f
, w
);
1336 const VMStateInfo vmstate_info_bitmap
= {
1342 typedef struct CompatEntry
{
1347 typedef struct SaveStateEntry
{
1348 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1354 SaveVMHandlers
*ops
;
1355 const VMStateDescription
*vmsd
;
1357 CompatEntry
*compat
;
1363 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1364 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1365 static int global_section_id
;
1367 static int calculate_new_instance_id(const char *idstr
)
1370 int instance_id
= 0;
1372 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1373 if (strcmp(idstr
, se
->idstr
) == 0
1374 && instance_id
<= se
->instance_id
) {
1375 instance_id
= se
->instance_id
+ 1;
1381 static int calculate_compat_instance_id(const char *idstr
)
1384 int instance_id
= 0;
1386 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1390 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1391 && instance_id
<= se
->compat
->instance_id
) {
1392 instance_id
= se
->compat
->instance_id
+ 1;
1398 /* TODO: Individual devices generally have very little idea about the rest
1399 of the system, so instance_id should be removed/replaced.
1400 Meanwhile pass -1 as instance_id if you do not already have a clearly
1401 distinguishing id for all instances of your device class. */
1402 int register_savevm_live(DeviceState
*dev
,
1406 SaveVMHandlers
*ops
,
1411 se
= g_malloc0(sizeof(SaveStateEntry
));
1412 se
->version_id
= version_id
;
1413 se
->section_id
= global_section_id
++;
1415 se
->opaque
= opaque
;
1418 /* if this is a live_savem then set is_ram */
1419 if (ops
->save_live_setup
!= NULL
) {
1424 char *id
= qdev_get_dev_path(dev
);
1426 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1427 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1430 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1431 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1432 se
->compat
->instance_id
= instance_id
== -1 ?
1433 calculate_compat_instance_id(idstr
) : instance_id
;
1437 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1439 if (instance_id
== -1) {
1440 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1442 se
->instance_id
= instance_id
;
1444 assert(!se
->compat
|| se
->instance_id
== 0);
1445 /* add at the end of list */
1446 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1450 int register_savevm(DeviceState
*dev
,
1454 SaveStateHandler
*save_state
,
1455 LoadStateHandler
*load_state
,
1458 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1459 ops
->save_state
= save_state
;
1460 ops
->load_state
= load_state
;
1461 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1465 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1467 SaveStateEntry
*se
, *new_se
;
1471 char *path
= qdev_get_dev_path(dev
);
1473 pstrcpy(id
, sizeof(id
), path
);
1474 pstrcat(id
, sizeof(id
), "/");
1478 pstrcat(id
, sizeof(id
), idstr
);
1480 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1481 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1482 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1492 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1493 const VMStateDescription
*vmsd
,
1494 void *opaque
, int alias_id
,
1495 int required_for_version
)
1499 /* If this triggers, alias support can be dropped for the vmsd. */
1500 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1502 se
= g_malloc0(sizeof(SaveStateEntry
));
1503 se
->version_id
= vmsd
->version_id
;
1504 se
->section_id
= global_section_id
++;
1505 se
->opaque
= opaque
;
1507 se
->alias_id
= alias_id
;
1508 se
->no_migrate
= vmsd
->unmigratable
;
1511 char *id
= qdev_get_dev_path(dev
);
1513 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1514 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1517 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1518 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1519 se
->compat
->instance_id
= instance_id
== -1 ?
1520 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1524 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1526 if (instance_id
== -1) {
1527 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1529 se
->instance_id
= instance_id
;
1531 assert(!se
->compat
|| se
->instance_id
== 0);
1532 /* add at the end of list */
1533 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1537 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1540 SaveStateEntry
*se
, *new_se
;
1542 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1543 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1544 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1553 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1555 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1558 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1559 void *opaque
, int version_id
)
1561 VMStateField
*field
= vmsd
->fields
;
1564 if (version_id
> vmsd
->version_id
) {
1567 if (version_id
< vmsd
->minimum_version_id_old
) {
1570 if (version_id
< vmsd
->minimum_version_id
) {
1571 return vmsd
->load_state_old(f
, opaque
, version_id
);
1573 if (vmsd
->pre_load
) {
1574 int ret
= vmsd
->pre_load(opaque
);
1578 while(field
->name
) {
1579 if ((field
->field_exists
&&
1580 field
->field_exists(opaque
, version_id
)) ||
1581 (!field
->field_exists
&&
1582 field
->version_id
<= version_id
)) {
1583 void *base_addr
= opaque
+ field
->offset
;
1585 int size
= field
->size
;
1587 if (field
->flags
& VMS_VBUFFER
) {
1588 size
= *(int32_t *)(opaque
+field
->size_offset
);
1589 if (field
->flags
& VMS_MULTIPLY
) {
1590 size
*= field
->size
;
1593 if (field
->flags
& VMS_ARRAY
) {
1594 n_elems
= field
->num
;
1595 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1596 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1597 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1598 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1599 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1600 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1601 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1602 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1604 if (field
->flags
& VMS_POINTER
) {
1605 base_addr
= *(void **)base_addr
+ field
->start
;
1607 for (i
= 0; i
< n_elems
; i
++) {
1608 void *addr
= base_addr
+ size
* i
;
1610 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1611 addr
= *(void **)addr
;
1613 if (field
->flags
& VMS_STRUCT
) {
1614 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1616 ret
= field
->info
->get(f
, addr
, size
);
1626 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1630 if (vmsd
->post_load
) {
1631 return vmsd
->post_load(opaque
, version_id
);
1636 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1639 VMStateField
*field
= vmsd
->fields
;
1641 if (vmsd
->pre_save
) {
1642 vmsd
->pre_save(opaque
);
1644 while(field
->name
) {
1645 if (!field
->field_exists
||
1646 field
->field_exists(opaque
, vmsd
->version_id
)) {
1647 void *base_addr
= opaque
+ field
->offset
;
1649 int size
= field
->size
;
1651 if (field
->flags
& VMS_VBUFFER
) {
1652 size
= *(int32_t *)(opaque
+field
->size_offset
);
1653 if (field
->flags
& VMS_MULTIPLY
) {
1654 size
*= field
->size
;
1657 if (field
->flags
& VMS_ARRAY
) {
1658 n_elems
= field
->num
;
1659 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1660 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1661 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1662 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1663 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1664 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1665 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1666 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1668 if (field
->flags
& VMS_POINTER
) {
1669 base_addr
= *(void **)base_addr
+ field
->start
;
1671 for (i
= 0; i
< n_elems
; i
++) {
1672 void *addr
= base_addr
+ size
* i
;
1674 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1675 addr
= *(void **)addr
;
1677 if (field
->flags
& VMS_STRUCT
) {
1678 vmstate_save_state(f
, field
->vmsd
, addr
);
1680 field
->info
->put(f
, addr
, size
);
1686 vmstate_subsection_save(f
, vmsd
, opaque
);
1689 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1691 if (!se
->vmsd
) { /* Old style */
1692 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1694 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1697 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1699 if (!se
->vmsd
) { /* Old style */
1700 se
->ops
->save_state(f
, se
->opaque
);
1703 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1706 #define QEMU_VM_FILE_MAGIC 0x5145564d
1707 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1708 #define QEMU_VM_FILE_VERSION 0x00000003
1710 #define QEMU_VM_EOF 0x00
1711 #define QEMU_VM_SECTION_START 0x01
1712 #define QEMU_VM_SECTION_PART 0x02
1713 #define QEMU_VM_SECTION_END 0x03
1714 #define QEMU_VM_SECTION_FULL 0x04
1715 #define QEMU_VM_SUBSECTION 0x05
1717 bool qemu_savevm_state_blocked(Error
**errp
)
1721 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1722 if (se
->no_migrate
) {
1723 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1730 void qemu_savevm_state_begin(QEMUFile
*f
,
1731 const MigrationParams
*params
)
1736 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1737 if (!se
->ops
|| !se
->ops
->set_params
) {
1740 se
->ops
->set_params(params
, se
->opaque
);
1743 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1744 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1746 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1749 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1752 if (se
->ops
&& se
->ops
->is_active
) {
1753 if (!se
->ops
->is_active(se
->opaque
)) {
1758 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1759 qemu_put_be32(f
, se
->section_id
);
1762 len
= strlen(se
->idstr
);
1763 qemu_put_byte(f
, len
);
1764 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1766 qemu_put_be32(f
, se
->instance_id
);
1767 qemu_put_be32(f
, se
->version_id
);
1769 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1771 qemu_file_set_error(f
, ret
);
1778 * this function has three return values:
1779 * negative: there was one error, and we have -errno.
1780 * 0 : We haven't finished, caller have to go again
1781 * 1 : We have finished, we can go to complete phase
1783 int qemu_savevm_state_iterate(QEMUFile
*f
)
1788 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1789 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1792 if (se
->ops
&& se
->ops
->is_active
) {
1793 if (!se
->ops
->is_active(se
->opaque
)) {
1797 if (qemu_file_rate_limit(f
)) {
1800 trace_savevm_section_start();
1802 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1803 qemu_put_be32(f
, se
->section_id
);
1805 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1806 trace_savevm_section_end(se
->section_id
);
1809 qemu_file_set_error(f
, ret
);
1812 /* Do not proceed to the next vmstate before this one reported
1813 completion of the current stage. This serializes the migration
1814 and reduces the probability that a faster changing state is
1815 synchronized over and over again. */
1822 void qemu_savevm_state_complete(QEMUFile
*f
)
1827 cpu_synchronize_all_states();
1829 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1830 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1833 if (se
->ops
&& se
->ops
->is_active
) {
1834 if (!se
->ops
->is_active(se
->opaque
)) {
1838 trace_savevm_section_start();
1840 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1841 qemu_put_be32(f
, se
->section_id
);
1843 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1844 trace_savevm_section_end(se
->section_id
);
1846 qemu_file_set_error(f
, ret
);
1851 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1854 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1857 trace_savevm_section_start();
1859 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1860 qemu_put_be32(f
, se
->section_id
);
1863 len
= strlen(se
->idstr
);
1864 qemu_put_byte(f
, len
);
1865 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1867 qemu_put_be32(f
, se
->instance_id
);
1868 qemu_put_be32(f
, se
->version_id
);
1870 vmstate_save(f
, se
);
1871 trace_savevm_section_end(se
->section_id
);
1874 qemu_put_byte(f
, QEMU_VM_EOF
);
1878 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1883 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1884 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1887 if (se
->ops
&& se
->ops
->is_active
) {
1888 if (!se
->ops
->is_active(se
->opaque
)) {
1892 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1897 void qemu_savevm_state_cancel(void)
1901 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1902 if (se
->ops
&& se
->ops
->cancel
) {
1903 se
->ops
->cancel(se
->opaque
);
1908 static int qemu_savevm_state(QEMUFile
*f
)
1911 MigrationParams params
= {
1916 if (qemu_savevm_state_blocked(NULL
)) {
1920 qemu_mutex_unlock_iothread();
1921 qemu_savevm_state_begin(f
, ¶ms
);
1922 qemu_mutex_lock_iothread();
1924 while (qemu_file_get_error(f
) == 0) {
1925 if (qemu_savevm_state_iterate(f
) > 0) {
1930 ret
= qemu_file_get_error(f
);
1932 qemu_savevm_state_complete(f
);
1933 ret
= qemu_file_get_error(f
);
1936 qemu_savevm_state_cancel();
1941 static int qemu_save_device_state(QEMUFile
*f
)
1945 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1946 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1948 cpu_synchronize_all_states();
1950 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1956 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1961 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1962 qemu_put_be32(f
, se
->section_id
);
1965 len
= strlen(se
->idstr
);
1966 qemu_put_byte(f
, len
);
1967 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1969 qemu_put_be32(f
, se
->instance_id
);
1970 qemu_put_be32(f
, se
->version_id
);
1972 vmstate_save(f
, se
);
1975 qemu_put_byte(f
, QEMU_VM_EOF
);
1977 return qemu_file_get_error(f
);
1980 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1984 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1985 if (!strcmp(se
->idstr
, idstr
) &&
1986 (instance_id
== se
->instance_id
||
1987 instance_id
== se
->alias_id
))
1989 /* Migrating from an older version? */
1990 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1991 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1992 (instance_id
== se
->compat
->instance_id
||
1993 instance_id
== se
->alias_id
))
2000 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
2002 while(sub
&& sub
->needed
) {
2003 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
2011 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2014 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
2017 uint8_t version_id
, len
, size
;
2018 const VMStateDescription
*sub_vmsd
;
2020 len
= qemu_peek_byte(f
, 1);
2021 if (len
< strlen(vmsd
->name
) + 1) {
2022 /* subsection name has be be "section_name/a" */
2025 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2031 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2032 /* it don't have a valid subsection name */
2035 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2036 if (sub_vmsd
== NULL
) {
2039 qemu_file_skip(f
, 1); /* subsection */
2040 qemu_file_skip(f
, 1); /* len */
2041 qemu_file_skip(f
, len
); /* idstr */
2042 version_id
= qemu_get_be32(f
);
2044 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2052 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2055 const VMStateSubsection
*sub
= vmsd
->subsections
;
2057 while (sub
&& sub
->needed
) {
2058 if (sub
->needed(opaque
)) {
2059 const VMStateDescription
*vmsd
= sub
->vmsd
;
2062 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2063 len
= strlen(vmsd
->name
);
2064 qemu_put_byte(f
, len
);
2065 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2066 qemu_put_be32(f
, vmsd
->version_id
);
2067 vmstate_save_state(f
, vmsd
, opaque
);
2073 typedef struct LoadStateEntry
{
2074 QLIST_ENTRY(LoadStateEntry
) entry
;
2080 int qemu_loadvm_state(QEMUFile
*f
)
2082 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2083 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2084 LoadStateEntry
*le
, *new_le
;
2085 uint8_t section_type
;
2089 if (qemu_savevm_state_blocked(NULL
)) {
2093 v
= qemu_get_be32(f
);
2094 if (v
!= QEMU_VM_FILE_MAGIC
)
2097 v
= qemu_get_be32(f
);
2098 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2099 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2102 if (v
!= QEMU_VM_FILE_VERSION
)
2105 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2106 uint32_t instance_id
, version_id
, section_id
;
2111 switch (section_type
) {
2112 case QEMU_VM_SECTION_START
:
2113 case QEMU_VM_SECTION_FULL
:
2114 /* Read section start */
2115 section_id
= qemu_get_be32(f
);
2116 len
= qemu_get_byte(f
);
2117 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2119 instance_id
= qemu_get_be32(f
);
2120 version_id
= qemu_get_be32(f
);
2122 /* Find savevm section */
2123 se
= find_se(idstr
, instance_id
);
2125 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2130 /* Validate version */
2131 if (version_id
> se
->version_id
) {
2132 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2133 version_id
, idstr
, se
->version_id
);
2139 le
= g_malloc0(sizeof(*le
));
2142 le
->section_id
= section_id
;
2143 le
->version_id
= version_id
;
2144 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2146 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2148 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2149 instance_id
, idstr
);
2153 case QEMU_VM_SECTION_PART
:
2154 case QEMU_VM_SECTION_END
:
2155 section_id
= qemu_get_be32(f
);
2157 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2158 if (le
->section_id
== section_id
) {
2163 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2168 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2170 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2176 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2182 cpu_synchronize_all_post_init();
2187 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2188 QLIST_REMOVE(le
, entry
);
2193 ret
= qemu_file_get_error(f
);
2199 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2202 QEMUSnapshotInfo
*sn_tab
, *sn
;
2206 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2209 for(i
= 0; i
< nb_sns
; i
++) {
2211 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2222 * Deletes snapshots of a given name in all opened images.
2224 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2226 BlockDriverState
*bs
;
2227 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2231 while ((bs
= bdrv_next(bs
))) {
2232 if (bdrv_can_snapshot(bs
) &&
2233 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2235 ret
= bdrv_snapshot_delete(bs
, name
);
2238 "Error while deleting snapshot on '%s'\n",
2239 bdrv_get_device_name(bs
));
2248 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2250 BlockDriverState
*bs
, *bs1
;
2251 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2254 int saved_vm_running
;
2255 uint64_t vm_state_size
;
2258 const char *name
= qdict_get_try_str(qdict
, "name");
2260 /* Verify if there is a device that doesn't support snapshots and is writable */
2262 while ((bs
= bdrv_next(bs
))) {
2264 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2268 if (!bdrv_can_snapshot(bs
)) {
2269 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2270 bdrv_get_device_name(bs
));
2275 bs
= bdrv_snapshots();
2277 monitor_printf(mon
, "No block device can accept snapshots\n");
2281 saved_vm_running
= runstate_is_running();
2282 vm_stop(RUN_STATE_SAVE_VM
);
2284 memset(sn
, 0, sizeof(*sn
));
2286 /* fill auxiliary fields */
2287 qemu_gettimeofday(&tv
);
2288 sn
->date_sec
= tv
.tv_sec
;
2289 sn
->date_nsec
= tv
.tv_usec
* 1000;
2290 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2293 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2295 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2296 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2298 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2301 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2302 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2303 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2306 /* Delete old snapshots of the same name */
2307 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2311 /* save the VM state */
2312 f
= qemu_fopen_bdrv(bs
, 1);
2314 monitor_printf(mon
, "Could not open VM state file\n");
2317 ret
= qemu_savevm_state(f
);
2318 vm_state_size
= qemu_ftell(f
);
2321 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2325 /* create the snapshots */
2328 while ((bs1
= bdrv_next(bs1
))) {
2329 if (bdrv_can_snapshot(bs1
)) {
2330 /* Write VM state size only to the image that contains the state */
2331 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2332 ret
= bdrv_snapshot_create(bs1
, sn
);
2334 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2335 bdrv_get_device_name(bs1
));
2341 if (saved_vm_running
)
2345 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2348 int saved_vm_running
;
2351 saved_vm_running
= runstate_is_running();
2352 vm_stop(RUN_STATE_SAVE_VM
);
2354 f
= qemu_fopen(filename
, "wb");
2356 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2359 ret
= qemu_save_device_state(f
);
2362 error_set(errp
, QERR_IO_ERROR
);
2366 if (saved_vm_running
)
2370 int load_vmstate(const char *name
)
2372 BlockDriverState
*bs
, *bs_vm_state
;
2373 QEMUSnapshotInfo sn
;
2377 bs_vm_state
= bdrv_snapshots();
2379 error_report("No block device supports snapshots");
2383 /* Don't even try to load empty VM states */
2384 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2387 } else if (sn
.vm_state_size
== 0) {
2388 error_report("This is a disk-only snapshot. Revert to it offline "
2393 /* Verify if there is any device that doesn't support snapshots and is
2394 writable and check if the requested snapshot is available too. */
2396 while ((bs
= bdrv_next(bs
))) {
2398 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2402 if (!bdrv_can_snapshot(bs
)) {
2403 error_report("Device '%s' is writable but does not support snapshots.",
2404 bdrv_get_device_name(bs
));
2408 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2410 error_report("Device '%s' does not have the requested snapshot '%s'",
2411 bdrv_get_device_name(bs
), name
);
2416 /* Flush all IO requests so they don't interfere with the new state. */
2420 while ((bs
= bdrv_next(bs
))) {
2421 if (bdrv_can_snapshot(bs
)) {
2422 ret
= bdrv_snapshot_goto(bs
, name
);
2424 error_report("Error %d while activating snapshot '%s' on '%s'",
2425 ret
, name
, bdrv_get_device_name(bs
));
2431 /* restore the VM state */
2432 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2434 error_report("Could not open VM state file");
2438 qemu_system_reset(VMRESET_SILENT
);
2439 ret
= qemu_loadvm_state(f
);
2443 error_report("Error %d while loading VM state", ret
);
2450 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2452 BlockDriverState
*bs
, *bs1
;
2454 const char *name
= qdict_get_str(qdict
, "name");
2456 bs
= bdrv_snapshots();
2458 monitor_printf(mon
, "No block device supports snapshots\n");
2463 while ((bs1
= bdrv_next(bs1
))) {
2464 if (bdrv_can_snapshot(bs1
)) {
2465 ret
= bdrv_snapshot_delete(bs1
, name
);
2467 if (ret
== -ENOTSUP
)
2469 "Snapshots not supported on device '%s'\n",
2470 bdrv_get_device_name(bs1
));
2472 monitor_printf(mon
, "Error %d while deleting snapshot on "
2473 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2479 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2481 BlockDriverState
*bs
, *bs1
;
2482 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2483 int nb_sns
, i
, ret
, available
;
2485 int *available_snapshots
;
2488 bs
= bdrv_snapshots();
2490 monitor_printf(mon
, "No available block device supports snapshots\n");
2494 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2496 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2501 monitor_printf(mon
, "There is no snapshot available.\n");
2505 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2507 for (i
= 0; i
< nb_sns
; i
++) {
2512 while ((bs1
= bdrv_next(bs1
))) {
2513 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2514 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2523 available_snapshots
[total
] = i
;
2529 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2530 for (i
= 0; i
< total
; i
++) {
2531 sn
= &sn_tab
[available_snapshots
[i
]];
2532 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2535 monitor_printf(mon
, "There is no suitable snapshot available\n");
2539 g_free(available_snapshots
);
2543 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2545 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2546 memory_region_name(mr
), dev
);
2549 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2551 /* Nothing do to while the implementation is in RAMBlock */
2554 void vmstate_register_ram_global(MemoryRegion
*mr
)
2556 vmstate_register_ram(mr
, NULL
);