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
)
180 QEMUFileSocket
*s
= opaque
;
182 ssize_t size
= iov_size(iov
, iovcnt
);
184 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
186 len
= -socket_error();
191 static int socket_get_fd(void *opaque
)
193 QEMUFileSocket
*s
= opaque
;
198 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
200 QEMUFileSocket
*s
= opaque
;
204 len
= qemu_recv(s
->fd
, buf
, size
, 0);
208 if (socket_error() == EAGAIN
) {
209 yield_until_fd_readable(s
->fd
);
210 } else if (socket_error() != EINTR
) {
216 len
= -socket_error();
221 static int socket_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
223 QEMUFileSocket
*s
= opaque
;
226 len
= qemu_send_full(s
->fd
, buf
, size
, 0);
228 len
= -socket_error();
233 static int socket_close(void *opaque
)
235 QEMUFileSocket
*s
= opaque
;
241 static int stdio_get_fd(void *opaque
)
243 QEMUFileStdio
*s
= opaque
;
245 return fileno(s
->stdio_file
);
248 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
250 QEMUFileStdio
*s
= opaque
;
251 return fwrite(buf
, 1, size
, s
->stdio_file
);
254 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
256 QEMUFileStdio
*s
= opaque
;
257 FILE *fp
= s
->stdio_file
;
262 bytes
= fread(buf
, 1, size
, fp
);
263 if (bytes
!= 0 || !ferror(fp
)) {
266 if (errno
== EAGAIN
) {
267 yield_until_fd_readable(fileno(fp
));
268 } else if (errno
!= EINTR
) {
275 static int stdio_pclose(void *opaque
)
277 QEMUFileStdio
*s
= opaque
;
279 ret
= pclose(s
->stdio_file
);
282 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
283 /* close succeeded, but non-zero exit code: */
284 ret
= -EIO
; /* fake errno value */
290 static int stdio_fclose(void *opaque
)
292 QEMUFileStdio
*s
= opaque
;
295 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
296 int fd
= fileno(s
->stdio_file
);
299 ret
= fstat(fd
, &st
);
300 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
302 * If the file handle is a regular file make sure the
303 * data is flushed to disk before signaling success.
312 if (fclose(s
->stdio_file
) == EOF
) {
319 static const QEMUFileOps stdio_pipe_read_ops
= {
320 .get_fd
= stdio_get_fd
,
321 .get_buffer
= stdio_get_buffer
,
322 .close
= stdio_pclose
325 static const QEMUFileOps stdio_pipe_write_ops
= {
326 .get_fd
= stdio_get_fd
,
327 .put_buffer
= stdio_put_buffer
,
328 .close
= stdio_pclose
331 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
336 stdio_file
= popen(command
, mode
);
337 if (stdio_file
== NULL
) {
341 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
342 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
346 s
= g_malloc0(sizeof(QEMUFileStdio
));
348 s
->stdio_file
= stdio_file
;
351 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
353 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
358 static const QEMUFileOps stdio_file_read_ops
= {
359 .get_fd
= stdio_get_fd
,
360 .get_buffer
= stdio_get_buffer
,
361 .close
= stdio_fclose
364 static const QEMUFileOps stdio_file_write_ops
= {
365 .get_fd
= stdio_get_fd
,
366 .put_buffer
= stdio_put_buffer
,
367 .close
= stdio_fclose
370 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
375 (mode
[0] != 'r' && mode
[0] != 'w') ||
376 mode
[1] != 'b' || mode
[2] != 0) {
377 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
381 s
= g_malloc0(sizeof(QEMUFileStdio
));
382 s
->stdio_file
= fdopen(fd
, mode
);
387 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
389 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
398 static const QEMUFileOps socket_read_ops
= {
399 .get_fd
= socket_get_fd
,
400 .get_buffer
= socket_get_buffer
,
401 .close
= socket_close
404 static const QEMUFileOps socket_write_ops
= {
405 .get_fd
= socket_get_fd
,
406 .put_buffer
= socket_put_buffer
,
407 .writev_buffer
= socket_writev_buffer
,
408 .close
= socket_close
411 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
413 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
416 (mode
[0] != 'r' && mode
[0] != 'w') ||
417 mode
[1] != 'b' || mode
[2] != 0) {
418 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
423 if (mode
[0] == 'w') {
424 qemu_set_block(s
->fd
);
425 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
427 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
432 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
437 (mode
[0] != 'r' && mode
[0] != 'w') ||
438 mode
[1] != 'b' || mode
[2] != 0) {
439 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
443 s
= g_malloc0(sizeof(QEMUFileStdio
));
445 s
->stdio_file
= fopen(filename
, mode
);
450 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
452 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
460 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
461 int64_t pos
, int size
)
463 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
467 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
469 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
472 static int bdrv_fclose(void *opaque
)
474 return bdrv_flush(opaque
);
477 static const QEMUFileOps bdrv_read_ops
= {
478 .get_buffer
= block_get_buffer
,
482 static const QEMUFileOps bdrv_write_ops
= {
483 .put_buffer
= block_put_buffer
,
487 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
490 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
491 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
494 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
498 f
= g_malloc0(sizeof(QEMUFile
));
505 int qemu_file_get_error(QEMUFile
*f
)
507 return f
->last_error
;
510 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
512 if (f
->last_error
== 0) {
517 static inline bool qemu_file_is_writable(QEMUFile
*f
)
519 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
523 * Flushes QEMUFile buffer
525 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
528 static void qemu_fflush(QEMUFile
*f
)
532 if (!qemu_file_is_writable(f
)) {
536 if (f
->ops
->writev_buffer
) {
538 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
);
541 if (f
->buf_index
> 0) {
542 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
551 qemu_file_set_error(f
, ret
);
555 static void qemu_fill_buffer(QEMUFile
*f
)
560 assert(!qemu_file_is_writable(f
));
562 pending
= f
->buf_size
- f
->buf_index
;
564 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
567 f
->buf_size
= pending
;
569 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
570 IO_BUF_SIZE
- pending
);
574 } else if (len
== 0) {
575 qemu_file_set_error(f
, -EIO
);
576 } else if (len
!= -EAGAIN
)
577 qemu_file_set_error(f
, len
);
580 int qemu_get_fd(QEMUFile
*f
)
582 if (f
->ops
->get_fd
) {
583 return f
->ops
->get_fd(f
->opaque
);
590 * Returns negative error value if any error happened on previous operations or
591 * while closing the file. Returns 0 or positive number on success.
593 * The meaning of return value on success depends on the specific backend
596 int qemu_fclose(QEMUFile
*f
)
600 ret
= qemu_file_get_error(f
);
603 int ret2
= f
->ops
->close(f
->opaque
);
608 /* If any error was spotted before closing, we should report it
609 * instead of the close() return value.
618 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
620 /* check for adjacent buffer and coalesce them */
621 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
622 f
->iov
[f
->iovcnt
- 1].iov_len
) {
623 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
625 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
626 f
->iov
[f
->iovcnt
++].iov_len
= size
;
629 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
634 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
636 if (!f
->ops
->writev_buffer
) {
637 qemu_put_buffer(f
, buf
, size
);
645 f
->bytes_xfer
+= size
;
646 add_to_iovec(f
, buf
, size
);
649 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
658 l
= IO_BUF_SIZE
- f
->buf_index
;
661 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
662 f
->bytes_xfer
+= size
;
663 if (f
->ops
->writev_buffer
) {
664 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
667 if (f
->buf_index
== IO_BUF_SIZE
) {
670 if (qemu_file_get_error(f
)) {
678 void qemu_put_byte(QEMUFile
*f
, int v
)
684 f
->buf
[f
->buf_index
] = v
;
686 if (f
->ops
->writev_buffer
) {
687 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
690 if (f
->buf_index
== IO_BUF_SIZE
) {
695 static void qemu_file_skip(QEMUFile
*f
, int size
)
697 if (f
->buf_index
+ size
<= f
->buf_size
) {
698 f
->buf_index
+= size
;
702 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
707 assert(!qemu_file_is_writable(f
));
709 index
= f
->buf_index
+ offset
;
710 pending
= f
->buf_size
- index
;
711 if (pending
< size
) {
713 index
= f
->buf_index
+ offset
;
714 pending
= f
->buf_size
- index
;
720 if (size
> pending
) {
724 memcpy(buf
, f
->buf
+ index
, size
);
728 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
733 while (pending
> 0) {
736 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
740 qemu_file_skip(f
, res
);
748 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
750 int index
= f
->buf_index
+ offset
;
752 assert(!qemu_file_is_writable(f
));
754 if (index
>= f
->buf_size
) {
756 index
= f
->buf_index
+ offset
;
757 if (index
>= f
->buf_size
) {
761 return f
->buf
[index
];
764 int qemu_get_byte(QEMUFile
*f
)
768 result
= qemu_peek_byte(f
, 0);
769 qemu_file_skip(f
, 1);
773 int64_t qemu_ftell(QEMUFile
*f
)
779 int qemu_file_rate_limit(QEMUFile
*f
)
781 if (qemu_file_get_error(f
)) {
784 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
790 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
792 return f
->xfer_limit
;
795 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
797 f
->xfer_limit
= limit
;
800 void qemu_file_reset_rate_limit(QEMUFile
*f
)
805 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
807 qemu_put_byte(f
, v
>> 8);
811 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
813 qemu_put_byte(f
, v
>> 24);
814 qemu_put_byte(f
, v
>> 16);
815 qemu_put_byte(f
, v
>> 8);
819 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
821 qemu_put_be32(f
, v
>> 32);
825 unsigned int qemu_get_be16(QEMUFile
*f
)
828 v
= qemu_get_byte(f
) << 8;
829 v
|= qemu_get_byte(f
);
833 unsigned int qemu_get_be32(QEMUFile
*f
)
836 v
= qemu_get_byte(f
) << 24;
837 v
|= qemu_get_byte(f
) << 16;
838 v
|= qemu_get_byte(f
) << 8;
839 v
|= qemu_get_byte(f
);
843 uint64_t qemu_get_be64(QEMUFile
*f
)
846 v
= (uint64_t)qemu_get_be32(f
) << 32;
847 v
|= qemu_get_be32(f
);
854 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
856 uint64_t expire_time
;
858 expire_time
= qemu_timer_expire_time_ns(ts
);
859 qemu_put_be64(f
, expire_time
);
862 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
864 uint64_t expire_time
;
866 expire_time
= qemu_get_be64(f
);
867 if (expire_time
!= -1) {
868 qemu_mod_timer_ns(ts
, expire_time
);
877 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
880 *v
= qemu_get_byte(f
);
884 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
887 qemu_put_byte(f
, *v
);
890 const VMStateInfo vmstate_info_bool
= {
898 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
905 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
911 const VMStateInfo vmstate_info_int8
= {
919 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
922 qemu_get_sbe16s(f
, v
);
926 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
929 qemu_put_sbe16s(f
, v
);
932 const VMStateInfo vmstate_info_int16
= {
940 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
943 qemu_get_sbe32s(f
, v
);
947 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
950 qemu_put_sbe32s(f
, v
);
953 const VMStateInfo vmstate_info_int32
= {
959 /* 32 bit int. See that the received value is the same than the one
962 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
966 qemu_get_sbe32s(f
, &v2
);
973 const VMStateInfo vmstate_info_int32_equal
= {
974 .name
= "int32 equal",
975 .get
= get_int32_equal
,
979 /* 32 bit int. See that the received value is the less or the same
980 than the one in the field */
982 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
986 qemu_get_sbe32s(f
, &new);
993 const VMStateInfo vmstate_info_int32_le
= {
994 .name
= "int32 equal",
1001 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1004 qemu_get_sbe64s(f
, v
);
1008 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1011 qemu_put_sbe64s(f
, v
);
1014 const VMStateInfo vmstate_info_int64
= {
1020 /* 8 bit unsigned int */
1022 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1029 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1035 const VMStateInfo vmstate_info_uint8
= {
1041 /* 16 bit unsigned int */
1043 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1046 qemu_get_be16s(f
, v
);
1050 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1053 qemu_put_be16s(f
, v
);
1056 const VMStateInfo vmstate_info_uint16
= {
1062 /* 32 bit unsigned int */
1064 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1067 qemu_get_be32s(f
, v
);
1071 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1074 qemu_put_be32s(f
, v
);
1077 const VMStateInfo vmstate_info_uint32
= {
1083 /* 32 bit uint. See that the received value is the same than the one
1086 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1090 qemu_get_be32s(f
, &v2
);
1098 const VMStateInfo vmstate_info_uint32_equal
= {
1099 .name
= "uint32 equal",
1100 .get
= get_uint32_equal
,
1104 /* 64 bit unsigned int */
1106 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1109 qemu_get_be64s(f
, v
);
1113 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1116 qemu_put_be64s(f
, v
);
1119 const VMStateInfo vmstate_info_uint64
= {
1125 /* 64 bit unsigned int. See that the received value is the same than the one
1128 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1132 qemu_get_be64s(f
, &v2
);
1140 const VMStateInfo vmstate_info_uint64_equal
= {
1141 .name
= "int64 equal",
1142 .get
= get_uint64_equal
,
1146 /* 8 bit int. See that the received value is the same than the one
1149 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1153 qemu_get_8s(f
, &v2
);
1160 const VMStateInfo vmstate_info_uint8_equal
= {
1161 .name
= "uint8 equal",
1162 .get
= get_uint8_equal
,
1166 /* 16 bit unsigned int int. See that the received value is the same than the one
1169 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1173 qemu_get_be16s(f
, &v2
);
1180 const VMStateInfo vmstate_info_uint16_equal
= {
1181 .name
= "uint16 equal",
1182 .get
= get_uint16_equal
,
1186 /* floating point */
1188 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1192 *v
= make_float64(qemu_get_be64(f
));
1196 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1200 qemu_put_be64(f
, float64_val(*v
));
1203 const VMStateInfo vmstate_info_float64
= {
1211 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1214 qemu_get_timer(f
, v
);
1218 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1221 qemu_put_timer(f
, v
);
1224 const VMStateInfo vmstate_info_timer
= {
1230 /* uint8_t buffers */
1232 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1235 qemu_get_buffer(f
, v
, size
);
1239 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1242 qemu_put_buffer(f
, v
, size
);
1245 const VMStateInfo vmstate_info_buffer
= {
1251 /* unused buffers: space that was used for some fields that are
1252 not useful anymore */
1254 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1260 block_len
= MIN(sizeof(buf
), size
);
1262 qemu_get_buffer(f
, buf
, block_len
);
1267 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1269 static const uint8_t buf
[1024];
1273 block_len
= MIN(sizeof(buf
), size
);
1275 qemu_put_buffer(f
, buf
, block_len
);
1279 const VMStateInfo vmstate_info_unused_buffer
= {
1280 .name
= "unused_buffer",
1281 .get
= get_unused_buffer
,
1282 .put
= put_unused_buffer
,
1285 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1286 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1287 * bit words with the bits in big endian order. The in-memory format
1288 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1290 /* This is the number of 64 bit words sent over the wire */
1291 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1292 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1294 unsigned long *bmp
= pv
;
1296 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1297 uint64_t w
= qemu_get_be64(f
);
1299 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1300 bmp
[idx
++] = w
>> 32;
1306 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1308 unsigned long *bmp
= pv
;
1310 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1311 uint64_t w
= bmp
[idx
++];
1312 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1313 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1315 qemu_put_be64(f
, w
);
1319 const VMStateInfo vmstate_info_bitmap
= {
1325 typedef struct CompatEntry
{
1330 typedef struct SaveStateEntry
{
1331 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1337 SaveVMHandlers
*ops
;
1338 const VMStateDescription
*vmsd
;
1340 CompatEntry
*compat
;
1346 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1347 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1348 static int global_section_id
;
1350 static int calculate_new_instance_id(const char *idstr
)
1353 int instance_id
= 0;
1355 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1356 if (strcmp(idstr
, se
->idstr
) == 0
1357 && instance_id
<= se
->instance_id
) {
1358 instance_id
= se
->instance_id
+ 1;
1364 static int calculate_compat_instance_id(const char *idstr
)
1367 int instance_id
= 0;
1369 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1373 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1374 && instance_id
<= se
->compat
->instance_id
) {
1375 instance_id
= se
->compat
->instance_id
+ 1;
1381 /* TODO: Individual devices generally have very little idea about the rest
1382 of the system, so instance_id should be removed/replaced.
1383 Meanwhile pass -1 as instance_id if you do not already have a clearly
1384 distinguishing id for all instances of your device class. */
1385 int register_savevm_live(DeviceState
*dev
,
1389 SaveVMHandlers
*ops
,
1394 se
= g_malloc0(sizeof(SaveStateEntry
));
1395 se
->version_id
= version_id
;
1396 se
->section_id
= global_section_id
++;
1398 se
->opaque
= opaque
;
1401 /* if this is a live_savem then set is_ram */
1402 if (ops
->save_live_setup
!= NULL
) {
1407 char *id
= qdev_get_dev_path(dev
);
1409 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1410 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1413 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1414 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1415 se
->compat
->instance_id
= instance_id
== -1 ?
1416 calculate_compat_instance_id(idstr
) : instance_id
;
1420 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1422 if (instance_id
== -1) {
1423 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1425 se
->instance_id
= instance_id
;
1427 assert(!se
->compat
|| se
->instance_id
== 0);
1428 /* add at the end of list */
1429 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1433 int register_savevm(DeviceState
*dev
,
1437 SaveStateHandler
*save_state
,
1438 LoadStateHandler
*load_state
,
1441 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1442 ops
->save_state
= save_state
;
1443 ops
->load_state
= load_state
;
1444 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1448 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1450 SaveStateEntry
*se
, *new_se
;
1454 char *path
= qdev_get_dev_path(dev
);
1456 pstrcpy(id
, sizeof(id
), path
);
1457 pstrcat(id
, sizeof(id
), "/");
1461 pstrcat(id
, sizeof(id
), idstr
);
1463 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1464 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1465 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1475 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1476 const VMStateDescription
*vmsd
,
1477 void *opaque
, int alias_id
,
1478 int required_for_version
)
1482 /* If this triggers, alias support can be dropped for the vmsd. */
1483 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1485 se
= g_malloc0(sizeof(SaveStateEntry
));
1486 se
->version_id
= vmsd
->version_id
;
1487 se
->section_id
= global_section_id
++;
1488 se
->opaque
= opaque
;
1490 se
->alias_id
= alias_id
;
1491 se
->no_migrate
= vmsd
->unmigratable
;
1494 char *id
= qdev_get_dev_path(dev
);
1496 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1497 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1500 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1501 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1502 se
->compat
->instance_id
= instance_id
== -1 ?
1503 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1507 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1509 if (instance_id
== -1) {
1510 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1512 se
->instance_id
= instance_id
;
1514 assert(!se
->compat
|| se
->instance_id
== 0);
1515 /* add at the end of list */
1516 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1520 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1523 SaveStateEntry
*se
, *new_se
;
1525 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1526 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1527 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1536 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1538 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1541 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1542 void *opaque
, int version_id
)
1544 VMStateField
*field
= vmsd
->fields
;
1547 if (version_id
> vmsd
->version_id
) {
1550 if (version_id
< vmsd
->minimum_version_id_old
) {
1553 if (version_id
< vmsd
->minimum_version_id
) {
1554 return vmsd
->load_state_old(f
, opaque
, version_id
);
1556 if (vmsd
->pre_load
) {
1557 int ret
= vmsd
->pre_load(opaque
);
1561 while(field
->name
) {
1562 if ((field
->field_exists
&&
1563 field
->field_exists(opaque
, version_id
)) ||
1564 (!field
->field_exists
&&
1565 field
->version_id
<= version_id
)) {
1566 void *base_addr
= opaque
+ field
->offset
;
1568 int size
= field
->size
;
1570 if (field
->flags
& VMS_VBUFFER
) {
1571 size
= *(int32_t *)(opaque
+field
->size_offset
);
1572 if (field
->flags
& VMS_MULTIPLY
) {
1573 size
*= field
->size
;
1576 if (field
->flags
& VMS_ARRAY
) {
1577 n_elems
= field
->num
;
1578 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1579 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1580 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1581 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1582 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1583 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1584 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1585 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1587 if (field
->flags
& VMS_POINTER
) {
1588 base_addr
= *(void **)base_addr
+ field
->start
;
1590 for (i
= 0; i
< n_elems
; i
++) {
1591 void *addr
= base_addr
+ size
* i
;
1593 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1594 addr
= *(void **)addr
;
1596 if (field
->flags
& VMS_STRUCT
) {
1597 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1599 ret
= field
->info
->get(f
, addr
, size
);
1609 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1613 if (vmsd
->post_load
) {
1614 return vmsd
->post_load(opaque
, version_id
);
1619 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1622 VMStateField
*field
= vmsd
->fields
;
1624 if (vmsd
->pre_save
) {
1625 vmsd
->pre_save(opaque
);
1627 while(field
->name
) {
1628 if (!field
->field_exists
||
1629 field
->field_exists(opaque
, vmsd
->version_id
)) {
1630 void *base_addr
= opaque
+ field
->offset
;
1632 int size
= field
->size
;
1634 if (field
->flags
& VMS_VBUFFER
) {
1635 size
= *(int32_t *)(opaque
+field
->size_offset
);
1636 if (field
->flags
& VMS_MULTIPLY
) {
1637 size
*= field
->size
;
1640 if (field
->flags
& VMS_ARRAY
) {
1641 n_elems
= field
->num
;
1642 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1643 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1644 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1645 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1646 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1647 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1648 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1649 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1651 if (field
->flags
& VMS_POINTER
) {
1652 base_addr
= *(void **)base_addr
+ field
->start
;
1654 for (i
= 0; i
< n_elems
; i
++) {
1655 void *addr
= base_addr
+ size
* i
;
1657 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1658 addr
= *(void **)addr
;
1660 if (field
->flags
& VMS_STRUCT
) {
1661 vmstate_save_state(f
, field
->vmsd
, addr
);
1663 field
->info
->put(f
, addr
, size
);
1669 vmstate_subsection_save(f
, vmsd
, opaque
);
1672 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1674 if (!se
->vmsd
) { /* Old style */
1675 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1677 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1680 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1682 if (!se
->vmsd
) { /* Old style */
1683 se
->ops
->save_state(f
, se
->opaque
);
1686 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1689 #define QEMU_VM_FILE_MAGIC 0x5145564d
1690 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1691 #define QEMU_VM_FILE_VERSION 0x00000003
1693 #define QEMU_VM_EOF 0x00
1694 #define QEMU_VM_SECTION_START 0x01
1695 #define QEMU_VM_SECTION_PART 0x02
1696 #define QEMU_VM_SECTION_END 0x03
1697 #define QEMU_VM_SECTION_FULL 0x04
1698 #define QEMU_VM_SUBSECTION 0x05
1700 bool qemu_savevm_state_blocked(Error
**errp
)
1704 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1705 if (se
->no_migrate
) {
1706 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1713 void qemu_savevm_state_begin(QEMUFile
*f
,
1714 const MigrationParams
*params
)
1719 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1720 if (!se
->ops
|| !se
->ops
->set_params
) {
1723 se
->ops
->set_params(params
, se
->opaque
);
1726 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1727 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1729 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1732 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1735 if (se
->ops
&& se
->ops
->is_active
) {
1736 if (!se
->ops
->is_active(se
->opaque
)) {
1741 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1742 qemu_put_be32(f
, se
->section_id
);
1745 len
= strlen(se
->idstr
);
1746 qemu_put_byte(f
, len
);
1747 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1749 qemu_put_be32(f
, se
->instance_id
);
1750 qemu_put_be32(f
, se
->version_id
);
1752 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1754 qemu_file_set_error(f
, ret
);
1761 * this function has three return values:
1762 * negative: there was one error, and we have -errno.
1763 * 0 : We haven't finished, caller have to go again
1764 * 1 : We have finished, we can go to complete phase
1766 int qemu_savevm_state_iterate(QEMUFile
*f
)
1771 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1772 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1775 if (se
->ops
&& se
->ops
->is_active
) {
1776 if (!se
->ops
->is_active(se
->opaque
)) {
1780 if (qemu_file_rate_limit(f
)) {
1783 trace_savevm_section_start();
1785 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1786 qemu_put_be32(f
, se
->section_id
);
1788 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1789 trace_savevm_section_end(se
->section_id
);
1792 qemu_file_set_error(f
, ret
);
1795 /* Do not proceed to the next vmstate before this one reported
1796 completion of the current stage. This serializes the migration
1797 and reduces the probability that a faster changing state is
1798 synchronized over and over again. */
1805 void qemu_savevm_state_complete(QEMUFile
*f
)
1810 cpu_synchronize_all_states();
1812 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1813 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1816 if (se
->ops
&& se
->ops
->is_active
) {
1817 if (!se
->ops
->is_active(se
->opaque
)) {
1821 trace_savevm_section_start();
1823 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1824 qemu_put_be32(f
, se
->section_id
);
1826 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1827 trace_savevm_section_end(se
->section_id
);
1829 qemu_file_set_error(f
, ret
);
1834 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1837 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1840 trace_savevm_section_start();
1842 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1843 qemu_put_be32(f
, se
->section_id
);
1846 len
= strlen(se
->idstr
);
1847 qemu_put_byte(f
, len
);
1848 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1850 qemu_put_be32(f
, se
->instance_id
);
1851 qemu_put_be32(f
, se
->version_id
);
1853 vmstate_save(f
, se
);
1854 trace_savevm_section_end(se
->section_id
);
1857 qemu_put_byte(f
, QEMU_VM_EOF
);
1861 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1866 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1867 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1870 if (se
->ops
&& se
->ops
->is_active
) {
1871 if (!se
->ops
->is_active(se
->opaque
)) {
1875 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1880 void qemu_savevm_state_cancel(void)
1884 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1885 if (se
->ops
&& se
->ops
->cancel
) {
1886 se
->ops
->cancel(se
->opaque
);
1891 static int qemu_savevm_state(QEMUFile
*f
)
1894 MigrationParams params
= {
1899 if (qemu_savevm_state_blocked(NULL
)) {
1903 qemu_mutex_unlock_iothread();
1904 qemu_savevm_state_begin(f
, ¶ms
);
1905 qemu_mutex_lock_iothread();
1907 while (qemu_file_get_error(f
) == 0) {
1908 if (qemu_savevm_state_iterate(f
) > 0) {
1913 ret
= qemu_file_get_error(f
);
1915 qemu_savevm_state_complete(f
);
1916 ret
= qemu_file_get_error(f
);
1919 qemu_savevm_state_cancel();
1924 static int qemu_save_device_state(QEMUFile
*f
)
1928 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1929 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1931 cpu_synchronize_all_states();
1933 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1939 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1944 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1945 qemu_put_be32(f
, se
->section_id
);
1948 len
= strlen(se
->idstr
);
1949 qemu_put_byte(f
, len
);
1950 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1952 qemu_put_be32(f
, se
->instance_id
);
1953 qemu_put_be32(f
, se
->version_id
);
1955 vmstate_save(f
, se
);
1958 qemu_put_byte(f
, QEMU_VM_EOF
);
1960 return qemu_file_get_error(f
);
1963 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1967 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1968 if (!strcmp(se
->idstr
, idstr
) &&
1969 (instance_id
== se
->instance_id
||
1970 instance_id
== se
->alias_id
))
1972 /* Migrating from an older version? */
1973 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1974 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1975 (instance_id
== se
->compat
->instance_id
||
1976 instance_id
== se
->alias_id
))
1983 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
1985 while(sub
&& sub
->needed
) {
1986 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
1994 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1997 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
2000 uint8_t version_id
, len
, size
;
2001 const VMStateDescription
*sub_vmsd
;
2003 len
= qemu_peek_byte(f
, 1);
2004 if (len
< strlen(vmsd
->name
) + 1) {
2005 /* subsection name has be be "section_name/a" */
2008 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2014 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2015 /* it don't have a valid subsection name */
2018 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2019 if (sub_vmsd
== NULL
) {
2022 qemu_file_skip(f
, 1); /* subsection */
2023 qemu_file_skip(f
, 1); /* len */
2024 qemu_file_skip(f
, len
); /* idstr */
2025 version_id
= qemu_get_be32(f
);
2027 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2035 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2038 const VMStateSubsection
*sub
= vmsd
->subsections
;
2040 while (sub
&& sub
->needed
) {
2041 if (sub
->needed(opaque
)) {
2042 const VMStateDescription
*vmsd
= sub
->vmsd
;
2045 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2046 len
= strlen(vmsd
->name
);
2047 qemu_put_byte(f
, len
);
2048 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2049 qemu_put_be32(f
, vmsd
->version_id
);
2050 vmstate_save_state(f
, vmsd
, opaque
);
2056 typedef struct LoadStateEntry
{
2057 QLIST_ENTRY(LoadStateEntry
) entry
;
2063 int qemu_loadvm_state(QEMUFile
*f
)
2065 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2066 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2067 LoadStateEntry
*le
, *new_le
;
2068 uint8_t section_type
;
2072 if (qemu_savevm_state_blocked(NULL
)) {
2076 v
= qemu_get_be32(f
);
2077 if (v
!= QEMU_VM_FILE_MAGIC
)
2080 v
= qemu_get_be32(f
);
2081 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2082 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2085 if (v
!= QEMU_VM_FILE_VERSION
)
2088 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2089 uint32_t instance_id
, version_id
, section_id
;
2094 switch (section_type
) {
2095 case QEMU_VM_SECTION_START
:
2096 case QEMU_VM_SECTION_FULL
:
2097 /* Read section start */
2098 section_id
= qemu_get_be32(f
);
2099 len
= qemu_get_byte(f
);
2100 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2102 instance_id
= qemu_get_be32(f
);
2103 version_id
= qemu_get_be32(f
);
2105 /* Find savevm section */
2106 se
= find_se(idstr
, instance_id
);
2108 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2113 /* Validate version */
2114 if (version_id
> se
->version_id
) {
2115 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2116 version_id
, idstr
, se
->version_id
);
2122 le
= g_malloc0(sizeof(*le
));
2125 le
->section_id
= section_id
;
2126 le
->version_id
= version_id
;
2127 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2129 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2131 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2132 instance_id
, idstr
);
2136 case QEMU_VM_SECTION_PART
:
2137 case QEMU_VM_SECTION_END
:
2138 section_id
= qemu_get_be32(f
);
2140 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2141 if (le
->section_id
== section_id
) {
2146 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2151 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2153 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2159 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2165 cpu_synchronize_all_post_init();
2170 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2171 QLIST_REMOVE(le
, entry
);
2176 ret
= qemu_file_get_error(f
);
2182 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2185 QEMUSnapshotInfo
*sn_tab
, *sn
;
2189 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2192 for(i
= 0; i
< nb_sns
; i
++) {
2194 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2205 * Deletes snapshots of a given name in all opened images.
2207 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2209 BlockDriverState
*bs
;
2210 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2214 while ((bs
= bdrv_next(bs
))) {
2215 if (bdrv_can_snapshot(bs
) &&
2216 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2218 ret
= bdrv_snapshot_delete(bs
, name
);
2221 "Error while deleting snapshot on '%s'\n",
2222 bdrv_get_device_name(bs
));
2231 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2233 BlockDriverState
*bs
, *bs1
;
2234 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2237 int saved_vm_running
;
2238 uint64_t vm_state_size
;
2241 const char *name
= qdict_get_try_str(qdict
, "name");
2243 /* Verify if there is a device that doesn't support snapshots and is writable */
2245 while ((bs
= bdrv_next(bs
))) {
2247 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2251 if (!bdrv_can_snapshot(bs
)) {
2252 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2253 bdrv_get_device_name(bs
));
2258 bs
= bdrv_snapshots();
2260 monitor_printf(mon
, "No block device can accept snapshots\n");
2264 saved_vm_running
= runstate_is_running();
2265 vm_stop(RUN_STATE_SAVE_VM
);
2267 memset(sn
, 0, sizeof(*sn
));
2269 /* fill auxiliary fields */
2270 qemu_gettimeofday(&tv
);
2271 sn
->date_sec
= tv
.tv_sec
;
2272 sn
->date_nsec
= tv
.tv_usec
* 1000;
2273 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2276 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2278 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2279 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2281 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2284 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2285 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2286 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2289 /* Delete old snapshots of the same name */
2290 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2294 /* save the VM state */
2295 f
= qemu_fopen_bdrv(bs
, 1);
2297 monitor_printf(mon
, "Could not open VM state file\n");
2300 ret
= qemu_savevm_state(f
);
2301 vm_state_size
= qemu_ftell(f
);
2304 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2308 /* create the snapshots */
2311 while ((bs1
= bdrv_next(bs1
))) {
2312 if (bdrv_can_snapshot(bs1
)) {
2313 /* Write VM state size only to the image that contains the state */
2314 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2315 ret
= bdrv_snapshot_create(bs1
, sn
);
2317 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2318 bdrv_get_device_name(bs1
));
2324 if (saved_vm_running
)
2328 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2331 int saved_vm_running
;
2334 saved_vm_running
= runstate_is_running();
2335 vm_stop(RUN_STATE_SAVE_VM
);
2337 f
= qemu_fopen(filename
, "wb");
2339 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2342 ret
= qemu_save_device_state(f
);
2345 error_set(errp
, QERR_IO_ERROR
);
2349 if (saved_vm_running
)
2353 int load_vmstate(const char *name
)
2355 BlockDriverState
*bs
, *bs_vm_state
;
2356 QEMUSnapshotInfo sn
;
2360 bs_vm_state
= bdrv_snapshots();
2362 error_report("No block device supports snapshots");
2366 /* Don't even try to load empty VM states */
2367 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2370 } else if (sn
.vm_state_size
== 0) {
2371 error_report("This is a disk-only snapshot. Revert to it offline "
2376 /* Verify if there is any device that doesn't support snapshots and is
2377 writable and check if the requested snapshot is available too. */
2379 while ((bs
= bdrv_next(bs
))) {
2381 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2385 if (!bdrv_can_snapshot(bs
)) {
2386 error_report("Device '%s' is writable but does not support snapshots.",
2387 bdrv_get_device_name(bs
));
2391 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2393 error_report("Device '%s' does not have the requested snapshot '%s'",
2394 bdrv_get_device_name(bs
), name
);
2399 /* Flush all IO requests so they don't interfere with the new state. */
2403 while ((bs
= bdrv_next(bs
))) {
2404 if (bdrv_can_snapshot(bs
)) {
2405 ret
= bdrv_snapshot_goto(bs
, name
);
2407 error_report("Error %d while activating snapshot '%s' on '%s'",
2408 ret
, name
, bdrv_get_device_name(bs
));
2414 /* restore the VM state */
2415 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2417 error_report("Could not open VM state file");
2421 qemu_system_reset(VMRESET_SILENT
);
2422 ret
= qemu_loadvm_state(f
);
2426 error_report("Error %d while loading VM state", ret
);
2433 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2435 BlockDriverState
*bs
, *bs1
;
2437 const char *name
= qdict_get_str(qdict
, "name");
2439 bs
= bdrv_snapshots();
2441 monitor_printf(mon
, "No block device supports snapshots\n");
2446 while ((bs1
= bdrv_next(bs1
))) {
2447 if (bdrv_can_snapshot(bs1
)) {
2448 ret
= bdrv_snapshot_delete(bs1
, name
);
2450 if (ret
== -ENOTSUP
)
2452 "Snapshots not supported on device '%s'\n",
2453 bdrv_get_device_name(bs1
));
2455 monitor_printf(mon
, "Error %d while deleting snapshot on "
2456 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2462 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2464 BlockDriverState
*bs
, *bs1
;
2465 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2466 int nb_sns
, i
, ret
, available
;
2468 int *available_snapshots
;
2471 bs
= bdrv_snapshots();
2473 monitor_printf(mon
, "No available block device supports snapshots\n");
2477 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2479 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2484 monitor_printf(mon
, "There is no snapshot available.\n");
2488 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2490 for (i
= 0; i
< nb_sns
; i
++) {
2495 while ((bs1
= bdrv_next(bs1
))) {
2496 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2497 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2506 available_snapshots
[total
] = i
;
2512 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2513 for (i
= 0; i
< total
; i
++) {
2514 sn
= &sn_tab
[available_snapshots
[i
]];
2515 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2518 monitor_printf(mon
, "There is no suitable snapshot available\n");
2522 g_free(available_snapshots
);
2526 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2528 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2529 memory_region_name(mr
), dev
);
2532 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2534 /* Nothing do to while the implementation is in RAMBlock */
2537 void vmstate_register_ram_global(MemoryRegion
*mr
)
2539 vmstate_register_ram(mr
, NULL
);