4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
41 #include "qemu/bitops.h"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
46 #define SELF_ANNOUNCE_ROUNDS 5
49 #define ETH_P_RARP 0x8035
51 #define ARP_HTYPE_ETH 0x0001
52 #define ARP_PTYPE_IP 0x0800
53 #define ARP_OP_REQUEST_REV 0x3
55 static int announce_self_create(uint8_t *buf
,
58 /* Ethernet header. */
59 memset(buf
, 0xff, 6); /* destination MAC addr */
60 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
61 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
64 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
65 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
66 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
67 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
69 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
70 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
72 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
74 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf
+ 42, 0x00, 18);
77 return 60; /* len (FCS will be added by hardware) */
80 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
85 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
87 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
91 static void qemu_announce_self_once(void *opaque
)
93 static int count
= SELF_ANNOUNCE_ROUNDS
;
94 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
96 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
99 /* delay 50ms, 150ms, 250ms, ... */
100 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
101 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
103 qemu_del_timer(timer
);
104 qemu_free_timer(timer
);
108 void qemu_announce_self(void)
110 static QEMUTimer
*timer
;
111 timer
= qemu_new_timer_ms(rt_clock
, qemu_announce_self_once
, &timer
);
112 qemu_announce_self_once(&timer
);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 #define IO_BUF_SIZE 32768
119 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
122 const QEMUFileOps
*ops
;
128 int64_t pos
; /* start of buffer when writing, end of buffer
131 int buf_size
; /* 0 when writing */
132 uint8_t buf
[IO_BUF_SIZE
];
134 struct iovec iov
[MAX_IOV_SIZE
];
140 typedef struct QEMUFileStdio
146 typedef struct QEMUFileSocket
152 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
155 QEMUFileSocket
*s
= opaque
;
157 ssize_t size
= iov_size(iov
, iovcnt
);
159 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
161 len
= -socket_error();
166 static int socket_get_fd(void *opaque
)
168 QEMUFileSocket
*s
= opaque
;
173 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
175 QEMUFileSocket
*s
= opaque
;
179 len
= qemu_recv(s
->fd
, buf
, size
, 0);
183 if (socket_error() == EAGAIN
) {
184 yield_until_fd_readable(s
->fd
);
185 } else if (socket_error() != EINTR
) {
191 len
= -socket_error();
196 static int socket_close(void *opaque
)
198 QEMUFileSocket
*s
= opaque
;
204 static int stdio_get_fd(void *opaque
)
206 QEMUFileStdio
*s
= opaque
;
208 return fileno(s
->stdio_file
);
211 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
213 QEMUFileStdio
*s
= opaque
;
214 return fwrite(buf
, 1, size
, s
->stdio_file
);
217 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
219 QEMUFileStdio
*s
= opaque
;
220 FILE *fp
= s
->stdio_file
;
225 bytes
= fread(buf
, 1, size
, fp
);
226 if (bytes
!= 0 || !ferror(fp
)) {
229 if (errno
== EAGAIN
) {
230 yield_until_fd_readable(fileno(fp
));
231 } else if (errno
!= EINTR
) {
238 static int stdio_pclose(void *opaque
)
240 QEMUFileStdio
*s
= opaque
;
242 ret
= pclose(s
->stdio_file
);
245 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
246 /* close succeeded, but non-zero exit code: */
247 ret
= -EIO
; /* fake errno value */
253 static int stdio_fclose(void *opaque
)
255 QEMUFileStdio
*s
= opaque
;
258 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
259 int fd
= fileno(s
->stdio_file
);
262 ret
= fstat(fd
, &st
);
263 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
265 * If the file handle is a regular file make sure the
266 * data is flushed to disk before signaling success.
275 if (fclose(s
->stdio_file
) == EOF
) {
282 static const QEMUFileOps stdio_pipe_read_ops
= {
283 .get_fd
= stdio_get_fd
,
284 .get_buffer
= stdio_get_buffer
,
285 .close
= stdio_pclose
288 static const QEMUFileOps stdio_pipe_write_ops
= {
289 .get_fd
= stdio_get_fd
,
290 .put_buffer
= stdio_put_buffer
,
291 .close
= stdio_pclose
294 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
299 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
300 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
304 stdio_file
= popen(command
, mode
);
305 if (stdio_file
== NULL
) {
309 s
= g_malloc0(sizeof(QEMUFileStdio
));
311 s
->stdio_file
= stdio_file
;
314 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
316 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
321 static const QEMUFileOps stdio_file_read_ops
= {
322 .get_fd
= stdio_get_fd
,
323 .get_buffer
= stdio_get_buffer
,
324 .close
= stdio_fclose
327 static const QEMUFileOps stdio_file_write_ops
= {
328 .get_fd
= stdio_get_fd
,
329 .put_buffer
= stdio_put_buffer
,
330 .close
= stdio_fclose
333 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
336 QEMUFileSocket
*s
= opaque
;
338 ssize_t size
= iov_size(iov
, iovcnt
);
344 /* Find the next start position; skip all full-sized vector elements */
345 while (offset
>= iov
[0].iov_len
) {
346 offset
-= iov
[0].iov_len
;
350 /* skip `offset' bytes from the (now) first element, undo it on exit */
352 iov
[0].iov_base
+= offset
;
353 iov
[0].iov_len
-= offset
;
356 len
= writev(s
->fd
, iov
, iovcnt
);
357 } while (len
== -1 && errno
== EINTR
);
362 /* Undo the changes above */
363 iov
[0].iov_base
-= offset
;
364 iov
[0].iov_len
+= offset
;
366 /* Prepare for the next iteration */
375 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
377 QEMUFileSocket
*s
= opaque
;
381 len
= read(s
->fd
, buf
, size
);
385 if (errno
== EAGAIN
) {
386 yield_until_fd_readable(s
->fd
);
387 } else if (errno
!= EINTR
) {
398 static int unix_close(void *opaque
)
400 QEMUFileSocket
*s
= opaque
;
406 static const QEMUFileOps unix_read_ops
= {
407 .get_fd
= socket_get_fd
,
408 .get_buffer
= unix_get_buffer
,
412 static const QEMUFileOps unix_write_ops
= {
413 .get_fd
= socket_get_fd
,
414 .writev_buffer
= unix_writev_buffer
,
418 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
423 (mode
[0] != 'r' && mode
[0] != 'w') ||
424 mode
[1] != 'b' || mode
[2] != 0) {
425 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
429 s
= g_malloc0(sizeof(QEMUFileSocket
));
433 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
435 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
440 static const QEMUFileOps socket_read_ops
= {
441 .get_fd
= socket_get_fd
,
442 .get_buffer
= socket_get_buffer
,
443 .close
= socket_close
446 static const QEMUFileOps socket_write_ops
= {
447 .get_fd
= socket_get_fd
,
448 .writev_buffer
= socket_writev_buffer
,
449 .close
= socket_close
452 bool qemu_file_mode_is_not_valid(const char *mode
)
455 (mode
[0] != 'r' && mode
[0] != 'w') ||
456 mode
[1] != 'b' || mode
[2] != 0) {
457 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
464 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
468 if (qemu_file_mode_is_not_valid(mode
)) {
472 s
= g_malloc0(sizeof(QEMUFileSocket
));
474 if (mode
[0] == 'w') {
475 qemu_set_block(s
->fd
);
476 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
478 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
483 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
487 if (qemu_file_mode_is_not_valid(mode
)) {
491 s
= g_malloc0(sizeof(QEMUFileStdio
));
493 s
->stdio_file
= fopen(filename
, mode
);
498 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
500 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
508 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
514 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
515 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
523 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
524 int64_t pos
, int size
)
526 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
530 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
532 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
535 static int bdrv_fclose(void *opaque
)
537 return bdrv_flush(opaque
);
540 static const QEMUFileOps bdrv_read_ops
= {
541 .get_buffer
= block_get_buffer
,
545 static const QEMUFileOps bdrv_write_ops
= {
546 .put_buffer
= block_put_buffer
,
547 .writev_buffer
= block_writev_buffer
,
551 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
554 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
555 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
558 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
562 f
= g_malloc0(sizeof(QEMUFile
));
569 int qemu_file_get_error(QEMUFile
*f
)
571 return f
->last_error
;
574 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
576 if (f
->last_error
== 0) {
581 static inline bool qemu_file_is_writable(QEMUFile
*f
)
583 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
587 * Flushes QEMUFile buffer
589 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
592 static void qemu_fflush(QEMUFile
*f
)
596 if (!qemu_file_is_writable(f
)) {
600 if (f
->ops
->writev_buffer
) {
602 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
605 if (f
->buf_index
> 0) {
606 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
615 qemu_file_set_error(f
, ret
);
619 static void qemu_fill_buffer(QEMUFile
*f
)
624 assert(!qemu_file_is_writable(f
));
626 pending
= f
->buf_size
- f
->buf_index
;
628 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
631 f
->buf_size
= pending
;
633 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
634 IO_BUF_SIZE
- pending
);
638 } else if (len
== 0) {
639 qemu_file_set_error(f
, -EIO
);
640 } else if (len
!= -EAGAIN
)
641 qemu_file_set_error(f
, len
);
644 int qemu_get_fd(QEMUFile
*f
)
646 if (f
->ops
->get_fd
) {
647 return f
->ops
->get_fd(f
->opaque
);
652 void qemu_update_position(QEMUFile
*f
, size_t size
)
659 * Returns negative error value if any error happened on previous operations or
660 * while closing the file. Returns 0 or positive number on success.
662 * The meaning of return value on success depends on the specific backend
665 int qemu_fclose(QEMUFile
*f
)
669 ret
= qemu_file_get_error(f
);
672 int ret2
= f
->ops
->close(f
->opaque
);
677 /* If any error was spotted before closing, we should report it
678 * instead of the close() return value.
687 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
689 /* check for adjacent buffer and coalesce them */
690 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
691 f
->iov
[f
->iovcnt
- 1].iov_len
) {
692 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
694 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
695 f
->iov
[f
->iovcnt
++].iov_len
= size
;
698 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
703 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
705 if (!f
->ops
->writev_buffer
) {
706 qemu_put_buffer(f
, buf
, size
);
714 f
->bytes_xfer
+= size
;
715 add_to_iovec(f
, buf
, size
);
718 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
727 l
= IO_BUF_SIZE
- f
->buf_index
;
730 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
731 f
->bytes_xfer
+= size
;
732 if (f
->ops
->writev_buffer
) {
733 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
736 if (f
->buf_index
== IO_BUF_SIZE
) {
739 if (qemu_file_get_error(f
)) {
747 void qemu_put_byte(QEMUFile
*f
, int v
)
753 f
->buf
[f
->buf_index
] = v
;
755 if (f
->ops
->writev_buffer
) {
756 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
759 if (f
->buf_index
== IO_BUF_SIZE
) {
764 static void qemu_file_skip(QEMUFile
*f
, int size
)
766 if (f
->buf_index
+ size
<= f
->buf_size
) {
767 f
->buf_index
+= size
;
771 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
776 assert(!qemu_file_is_writable(f
));
778 index
= f
->buf_index
+ offset
;
779 pending
= f
->buf_size
- index
;
780 if (pending
< size
) {
782 index
= f
->buf_index
+ offset
;
783 pending
= f
->buf_size
- index
;
789 if (size
> pending
) {
793 memcpy(buf
, f
->buf
+ index
, size
);
797 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
802 while (pending
> 0) {
805 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
809 qemu_file_skip(f
, res
);
817 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
819 int index
= f
->buf_index
+ offset
;
821 assert(!qemu_file_is_writable(f
));
823 if (index
>= f
->buf_size
) {
825 index
= f
->buf_index
+ offset
;
826 if (index
>= f
->buf_size
) {
830 return f
->buf
[index
];
833 int qemu_get_byte(QEMUFile
*f
)
837 result
= qemu_peek_byte(f
, 0);
838 qemu_file_skip(f
, 1);
842 int64_t qemu_ftell(QEMUFile
*f
)
848 int qemu_file_rate_limit(QEMUFile
*f
)
850 if (qemu_file_get_error(f
)) {
853 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
859 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
861 return f
->xfer_limit
;
864 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
866 f
->xfer_limit
= limit
;
869 void qemu_file_reset_rate_limit(QEMUFile
*f
)
874 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
876 qemu_put_byte(f
, v
>> 8);
880 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
882 qemu_put_byte(f
, v
>> 24);
883 qemu_put_byte(f
, v
>> 16);
884 qemu_put_byte(f
, v
>> 8);
888 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
890 qemu_put_be32(f
, v
>> 32);
894 unsigned int qemu_get_be16(QEMUFile
*f
)
897 v
= qemu_get_byte(f
) << 8;
898 v
|= qemu_get_byte(f
);
902 unsigned int qemu_get_be32(QEMUFile
*f
)
905 v
= qemu_get_byte(f
) << 24;
906 v
|= qemu_get_byte(f
) << 16;
907 v
|= qemu_get_byte(f
) << 8;
908 v
|= qemu_get_byte(f
);
912 uint64_t qemu_get_be64(QEMUFile
*f
)
915 v
= (uint64_t)qemu_get_be32(f
) << 32;
916 v
|= qemu_get_be32(f
);
923 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
925 uint64_t expire_time
;
927 expire_time
= qemu_timer_expire_time_ns(ts
);
928 qemu_put_be64(f
, expire_time
);
931 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
933 uint64_t expire_time
;
935 expire_time
= qemu_get_be64(f
);
936 if (expire_time
!= -1) {
937 qemu_mod_timer_ns(ts
, expire_time
);
946 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
949 *v
= qemu_get_byte(f
);
953 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
956 qemu_put_byte(f
, *v
);
959 const VMStateInfo vmstate_info_bool
= {
967 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
974 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
980 const VMStateInfo vmstate_info_int8
= {
988 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
991 qemu_get_sbe16s(f
, v
);
995 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
998 qemu_put_sbe16s(f
, v
);
1001 const VMStateInfo vmstate_info_int16
= {
1009 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
1012 qemu_get_sbe32s(f
, v
);
1016 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
1019 qemu_put_sbe32s(f
, v
);
1022 const VMStateInfo vmstate_info_int32
= {
1028 /* 32 bit int. See that the received value is the same than the one
1031 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1035 qemu_get_sbe32s(f
, &v2
);
1042 const VMStateInfo vmstate_info_int32_equal
= {
1043 .name
= "int32 equal",
1044 .get
= get_int32_equal
,
1048 /* 32 bit int. See that the received value is the less or the same
1049 than the one in the field */
1051 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
1055 qemu_get_sbe32s(f
, &new);
1062 const VMStateInfo vmstate_info_int32_le
= {
1063 .name
= "int32 equal",
1064 .get
= get_int32_le
,
1070 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1073 qemu_get_sbe64s(f
, v
);
1077 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1080 qemu_put_sbe64s(f
, v
);
1083 const VMStateInfo vmstate_info_int64
= {
1089 /* 8 bit unsigned int */
1091 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1098 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1104 const VMStateInfo vmstate_info_uint8
= {
1110 /* 16 bit unsigned int */
1112 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1115 qemu_get_be16s(f
, v
);
1119 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1122 qemu_put_be16s(f
, v
);
1125 const VMStateInfo vmstate_info_uint16
= {
1131 /* 32 bit unsigned int */
1133 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1136 qemu_get_be32s(f
, v
);
1140 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1143 qemu_put_be32s(f
, v
);
1146 const VMStateInfo vmstate_info_uint32
= {
1152 /* 32 bit uint. See that the received value is the same than the one
1155 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1159 qemu_get_be32s(f
, &v2
);
1167 const VMStateInfo vmstate_info_uint32_equal
= {
1168 .name
= "uint32 equal",
1169 .get
= get_uint32_equal
,
1173 /* 64 bit unsigned int */
1175 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1178 qemu_get_be64s(f
, v
);
1182 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1185 qemu_put_be64s(f
, v
);
1188 const VMStateInfo vmstate_info_uint64
= {
1194 /* 64 bit unsigned int. See that the received value is the same than the one
1197 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1201 qemu_get_be64s(f
, &v2
);
1209 const VMStateInfo vmstate_info_uint64_equal
= {
1210 .name
= "int64 equal",
1211 .get
= get_uint64_equal
,
1215 /* 8 bit int. See that the received value is the same than the one
1218 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1222 qemu_get_8s(f
, &v2
);
1229 const VMStateInfo vmstate_info_uint8_equal
= {
1230 .name
= "uint8 equal",
1231 .get
= get_uint8_equal
,
1235 /* 16 bit unsigned int int. See that the received value is the same than the one
1238 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1242 qemu_get_be16s(f
, &v2
);
1249 const VMStateInfo vmstate_info_uint16_equal
= {
1250 .name
= "uint16 equal",
1251 .get
= get_uint16_equal
,
1255 /* floating point */
1257 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1261 *v
= make_float64(qemu_get_be64(f
));
1265 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1269 qemu_put_be64(f
, float64_val(*v
));
1272 const VMStateInfo vmstate_info_float64
= {
1280 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1283 qemu_get_timer(f
, v
);
1287 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1290 qemu_put_timer(f
, v
);
1293 const VMStateInfo vmstate_info_timer
= {
1299 /* uint8_t buffers */
1301 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1304 qemu_get_buffer(f
, v
, size
);
1308 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1311 qemu_put_buffer(f
, v
, size
);
1314 const VMStateInfo vmstate_info_buffer
= {
1320 /* unused buffers: space that was used for some fields that are
1321 not useful anymore */
1323 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1329 block_len
= MIN(sizeof(buf
), size
);
1331 qemu_get_buffer(f
, buf
, block_len
);
1336 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1338 static const uint8_t buf
[1024];
1342 block_len
= MIN(sizeof(buf
), size
);
1344 qemu_put_buffer(f
, buf
, block_len
);
1348 const VMStateInfo vmstate_info_unused_buffer
= {
1349 .name
= "unused_buffer",
1350 .get
= get_unused_buffer
,
1351 .put
= put_unused_buffer
,
1354 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1355 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1356 * bit words with the bits in big endian order. The in-memory format
1357 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1359 /* This is the number of 64 bit words sent over the wire */
1360 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1361 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1363 unsigned long *bmp
= pv
;
1365 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1366 uint64_t w
= qemu_get_be64(f
);
1368 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1369 bmp
[idx
++] = w
>> 32;
1375 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1377 unsigned long *bmp
= pv
;
1379 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1380 uint64_t w
= bmp
[idx
++];
1381 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1382 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1384 qemu_put_be64(f
, w
);
1388 const VMStateInfo vmstate_info_bitmap
= {
1394 typedef struct CompatEntry
{
1399 typedef struct SaveStateEntry
{
1400 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1406 SaveVMHandlers
*ops
;
1407 const VMStateDescription
*vmsd
;
1409 CompatEntry
*compat
;
1415 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1416 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1417 static int global_section_id
;
1419 static int calculate_new_instance_id(const char *idstr
)
1422 int instance_id
= 0;
1424 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1425 if (strcmp(idstr
, se
->idstr
) == 0
1426 && instance_id
<= se
->instance_id
) {
1427 instance_id
= se
->instance_id
+ 1;
1433 static int calculate_compat_instance_id(const char *idstr
)
1436 int instance_id
= 0;
1438 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1442 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1443 && instance_id
<= se
->compat
->instance_id
) {
1444 instance_id
= se
->compat
->instance_id
+ 1;
1450 /* TODO: Individual devices generally have very little idea about the rest
1451 of the system, so instance_id should be removed/replaced.
1452 Meanwhile pass -1 as instance_id if you do not already have a clearly
1453 distinguishing id for all instances of your device class. */
1454 int register_savevm_live(DeviceState
*dev
,
1458 SaveVMHandlers
*ops
,
1463 se
= g_malloc0(sizeof(SaveStateEntry
));
1464 se
->version_id
= version_id
;
1465 se
->section_id
= global_section_id
++;
1467 se
->opaque
= opaque
;
1470 /* if this is a live_savem then set is_ram */
1471 if (ops
->save_live_setup
!= NULL
) {
1476 char *id
= qdev_get_dev_path(dev
);
1478 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1479 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1482 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1483 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1484 se
->compat
->instance_id
= instance_id
== -1 ?
1485 calculate_compat_instance_id(idstr
) : instance_id
;
1489 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1491 if (instance_id
== -1) {
1492 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1494 se
->instance_id
= instance_id
;
1496 assert(!se
->compat
|| se
->instance_id
== 0);
1497 /* add at the end of list */
1498 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1502 int register_savevm(DeviceState
*dev
,
1506 SaveStateHandler
*save_state
,
1507 LoadStateHandler
*load_state
,
1510 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1511 ops
->save_state
= save_state
;
1512 ops
->load_state
= load_state
;
1513 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1517 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1519 SaveStateEntry
*se
, *new_se
;
1523 char *path
= qdev_get_dev_path(dev
);
1525 pstrcpy(id
, sizeof(id
), path
);
1526 pstrcat(id
, sizeof(id
), "/");
1530 pstrcat(id
, sizeof(id
), idstr
);
1532 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1533 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1534 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1544 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1545 const VMStateDescription
*vmsd
,
1546 void *opaque
, int alias_id
,
1547 int required_for_version
)
1551 /* If this triggers, alias support can be dropped for the vmsd. */
1552 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1554 se
= g_malloc0(sizeof(SaveStateEntry
));
1555 se
->version_id
= vmsd
->version_id
;
1556 se
->section_id
= global_section_id
++;
1557 se
->opaque
= opaque
;
1559 se
->alias_id
= alias_id
;
1560 se
->no_migrate
= vmsd
->unmigratable
;
1563 char *id
= qdev_get_dev_path(dev
);
1565 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1566 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1569 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1570 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1571 se
->compat
->instance_id
= instance_id
== -1 ?
1572 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1576 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1578 if (instance_id
== -1) {
1579 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1581 se
->instance_id
= instance_id
;
1583 assert(!se
->compat
|| se
->instance_id
== 0);
1584 /* add at the end of list */
1585 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1589 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1592 SaveStateEntry
*se
, *new_se
;
1594 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1595 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1596 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1605 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1607 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1610 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1611 void *opaque
, int version_id
)
1613 VMStateField
*field
= vmsd
->fields
;
1616 if (version_id
> vmsd
->version_id
) {
1619 if (version_id
< vmsd
->minimum_version_id_old
) {
1622 if (version_id
< vmsd
->minimum_version_id
) {
1623 return vmsd
->load_state_old(f
, opaque
, version_id
);
1625 if (vmsd
->pre_load
) {
1626 int ret
= vmsd
->pre_load(opaque
);
1630 while(field
->name
) {
1631 if ((field
->field_exists
&&
1632 field
->field_exists(opaque
, version_id
)) ||
1633 (!field
->field_exists
&&
1634 field
->version_id
<= version_id
)) {
1635 void *base_addr
= opaque
+ field
->offset
;
1637 int size
= field
->size
;
1639 if (field
->flags
& VMS_VBUFFER
) {
1640 size
= *(int32_t *)(opaque
+field
->size_offset
);
1641 if (field
->flags
& VMS_MULTIPLY
) {
1642 size
*= field
->size
;
1645 if (field
->flags
& VMS_ARRAY
) {
1646 n_elems
= field
->num
;
1647 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1648 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1649 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1650 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1651 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1652 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1653 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1654 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1656 if (field
->flags
& VMS_POINTER
) {
1657 base_addr
= *(void **)base_addr
+ field
->start
;
1659 for (i
= 0; i
< n_elems
; i
++) {
1660 void *addr
= base_addr
+ size
* i
;
1662 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1663 addr
= *(void **)addr
;
1665 if (field
->flags
& VMS_STRUCT
) {
1666 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1668 ret
= field
->info
->get(f
, addr
, size
);
1678 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1682 if (vmsd
->post_load
) {
1683 return vmsd
->post_load(opaque
, version_id
);
1688 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1691 VMStateField
*field
= vmsd
->fields
;
1693 if (vmsd
->pre_save
) {
1694 vmsd
->pre_save(opaque
);
1696 while(field
->name
) {
1697 if (!field
->field_exists
||
1698 field
->field_exists(opaque
, vmsd
->version_id
)) {
1699 void *base_addr
= opaque
+ field
->offset
;
1701 int size
= field
->size
;
1703 if (field
->flags
& VMS_VBUFFER
) {
1704 size
= *(int32_t *)(opaque
+field
->size_offset
);
1705 if (field
->flags
& VMS_MULTIPLY
) {
1706 size
*= field
->size
;
1709 if (field
->flags
& VMS_ARRAY
) {
1710 n_elems
= field
->num
;
1711 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1712 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1713 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1714 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1715 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1716 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1717 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1718 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1720 if (field
->flags
& VMS_POINTER
) {
1721 base_addr
= *(void **)base_addr
+ field
->start
;
1723 for (i
= 0; i
< n_elems
; i
++) {
1724 void *addr
= base_addr
+ size
* i
;
1726 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1727 addr
= *(void **)addr
;
1729 if (field
->flags
& VMS_STRUCT
) {
1730 vmstate_save_state(f
, field
->vmsd
, addr
);
1732 field
->info
->put(f
, addr
, size
);
1738 vmstate_subsection_save(f
, vmsd
, opaque
);
1741 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1743 if (!se
->vmsd
) { /* Old style */
1744 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1746 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1749 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1751 if (!se
->vmsd
) { /* Old style */
1752 se
->ops
->save_state(f
, se
->opaque
);
1755 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1758 #define QEMU_VM_FILE_MAGIC 0x5145564d
1759 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1760 #define QEMU_VM_FILE_VERSION 0x00000003
1762 #define QEMU_VM_EOF 0x00
1763 #define QEMU_VM_SECTION_START 0x01
1764 #define QEMU_VM_SECTION_PART 0x02
1765 #define QEMU_VM_SECTION_END 0x03
1766 #define QEMU_VM_SECTION_FULL 0x04
1767 #define QEMU_VM_SUBSECTION 0x05
1769 bool qemu_savevm_state_blocked(Error
**errp
)
1773 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1774 if (se
->no_migrate
) {
1775 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1782 void qemu_savevm_state_begin(QEMUFile
*f
,
1783 const MigrationParams
*params
)
1788 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1789 if (!se
->ops
|| !se
->ops
->set_params
) {
1792 se
->ops
->set_params(params
, se
->opaque
);
1795 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1796 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1798 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1801 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1804 if (se
->ops
&& se
->ops
->is_active
) {
1805 if (!se
->ops
->is_active(se
->opaque
)) {
1810 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1811 qemu_put_be32(f
, se
->section_id
);
1814 len
= strlen(se
->idstr
);
1815 qemu_put_byte(f
, len
);
1816 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1818 qemu_put_be32(f
, se
->instance_id
);
1819 qemu_put_be32(f
, se
->version_id
);
1821 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1823 qemu_file_set_error(f
, ret
);
1830 * this function has three return values:
1831 * negative: there was one error, and we have -errno.
1832 * 0 : We haven't finished, caller have to go again
1833 * 1 : We have finished, we can go to complete phase
1835 int qemu_savevm_state_iterate(QEMUFile
*f
)
1840 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1841 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1844 if (se
->ops
&& se
->ops
->is_active
) {
1845 if (!se
->ops
->is_active(se
->opaque
)) {
1849 if (qemu_file_rate_limit(f
)) {
1852 trace_savevm_section_start();
1854 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1855 qemu_put_be32(f
, se
->section_id
);
1857 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1858 trace_savevm_section_end(se
->section_id
);
1861 qemu_file_set_error(f
, ret
);
1864 /* Do not proceed to the next vmstate before this one reported
1865 completion of the current stage. This serializes the migration
1866 and reduces the probability that a faster changing state is
1867 synchronized over and over again. */
1874 void qemu_savevm_state_complete(QEMUFile
*f
)
1879 cpu_synchronize_all_states();
1881 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1882 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1885 if (se
->ops
&& se
->ops
->is_active
) {
1886 if (!se
->ops
->is_active(se
->opaque
)) {
1890 trace_savevm_section_start();
1892 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1893 qemu_put_be32(f
, se
->section_id
);
1895 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1896 trace_savevm_section_end(se
->section_id
);
1898 qemu_file_set_error(f
, ret
);
1903 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1906 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1909 trace_savevm_section_start();
1911 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1912 qemu_put_be32(f
, se
->section_id
);
1915 len
= strlen(se
->idstr
);
1916 qemu_put_byte(f
, len
);
1917 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1919 qemu_put_be32(f
, se
->instance_id
);
1920 qemu_put_be32(f
, se
->version_id
);
1922 vmstate_save(f
, se
);
1923 trace_savevm_section_end(se
->section_id
);
1926 qemu_put_byte(f
, QEMU_VM_EOF
);
1930 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1935 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1936 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1939 if (se
->ops
&& se
->ops
->is_active
) {
1940 if (!se
->ops
->is_active(se
->opaque
)) {
1944 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1949 void qemu_savevm_state_cancel(void)
1953 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1954 if (se
->ops
&& se
->ops
->cancel
) {
1955 se
->ops
->cancel(se
->opaque
);
1960 static int qemu_savevm_state(QEMUFile
*f
)
1963 MigrationParams params
= {
1968 if (qemu_savevm_state_blocked(NULL
)) {
1972 qemu_mutex_unlock_iothread();
1973 qemu_savevm_state_begin(f
, ¶ms
);
1974 qemu_mutex_lock_iothread();
1976 while (qemu_file_get_error(f
) == 0) {
1977 if (qemu_savevm_state_iterate(f
) > 0) {
1982 ret
= qemu_file_get_error(f
);
1984 qemu_savevm_state_complete(f
);
1985 ret
= qemu_file_get_error(f
);
1988 qemu_savevm_state_cancel();
1993 static int qemu_save_device_state(QEMUFile
*f
)
1997 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1998 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
2000 cpu_synchronize_all_states();
2002 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2008 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
2013 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
2014 qemu_put_be32(f
, se
->section_id
);
2017 len
= strlen(se
->idstr
);
2018 qemu_put_byte(f
, len
);
2019 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
2021 qemu_put_be32(f
, se
->instance_id
);
2022 qemu_put_be32(f
, se
->version_id
);
2024 vmstate_save(f
, se
);
2027 qemu_put_byte(f
, QEMU_VM_EOF
);
2029 return qemu_file_get_error(f
);
2032 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
2036 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2037 if (!strcmp(se
->idstr
, idstr
) &&
2038 (instance_id
== se
->instance_id
||
2039 instance_id
== se
->alias_id
))
2041 /* Migrating from an older version? */
2042 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
2043 if (!strcmp(se
->compat
->idstr
, idstr
) &&
2044 (instance_id
== se
->compat
->instance_id
||
2045 instance_id
== se
->alias_id
))
2052 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
2054 while(sub
&& sub
->needed
) {
2055 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
2063 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2066 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
2069 uint8_t version_id
, len
, size
;
2070 const VMStateDescription
*sub_vmsd
;
2072 len
= qemu_peek_byte(f
, 1);
2073 if (len
< strlen(vmsd
->name
) + 1) {
2074 /* subsection name has be be "section_name/a" */
2077 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2083 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2084 /* it don't have a valid subsection name */
2087 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2088 if (sub_vmsd
== NULL
) {
2091 qemu_file_skip(f
, 1); /* subsection */
2092 qemu_file_skip(f
, 1); /* len */
2093 qemu_file_skip(f
, len
); /* idstr */
2094 version_id
= qemu_get_be32(f
);
2096 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2104 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2107 const VMStateSubsection
*sub
= vmsd
->subsections
;
2109 while (sub
&& sub
->needed
) {
2110 if (sub
->needed(opaque
)) {
2111 const VMStateDescription
*vmsd
= sub
->vmsd
;
2114 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2115 len
= strlen(vmsd
->name
);
2116 qemu_put_byte(f
, len
);
2117 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2118 qemu_put_be32(f
, vmsd
->version_id
);
2119 vmstate_save_state(f
, vmsd
, opaque
);
2125 typedef struct LoadStateEntry
{
2126 QLIST_ENTRY(LoadStateEntry
) entry
;
2132 int qemu_loadvm_state(QEMUFile
*f
)
2134 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2135 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2136 LoadStateEntry
*le
, *new_le
;
2137 uint8_t section_type
;
2141 if (qemu_savevm_state_blocked(NULL
)) {
2145 v
= qemu_get_be32(f
);
2146 if (v
!= QEMU_VM_FILE_MAGIC
)
2149 v
= qemu_get_be32(f
);
2150 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2151 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2154 if (v
!= QEMU_VM_FILE_VERSION
)
2157 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2158 uint32_t instance_id
, version_id
, section_id
;
2163 switch (section_type
) {
2164 case QEMU_VM_SECTION_START
:
2165 case QEMU_VM_SECTION_FULL
:
2166 /* Read section start */
2167 section_id
= qemu_get_be32(f
);
2168 len
= qemu_get_byte(f
);
2169 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2171 instance_id
= qemu_get_be32(f
);
2172 version_id
= qemu_get_be32(f
);
2174 /* Find savevm section */
2175 se
= find_se(idstr
, instance_id
);
2177 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2182 /* Validate version */
2183 if (version_id
> se
->version_id
) {
2184 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2185 version_id
, idstr
, se
->version_id
);
2191 le
= g_malloc0(sizeof(*le
));
2194 le
->section_id
= section_id
;
2195 le
->version_id
= version_id
;
2196 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2198 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2200 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2201 instance_id
, idstr
);
2205 case QEMU_VM_SECTION_PART
:
2206 case QEMU_VM_SECTION_END
:
2207 section_id
= qemu_get_be32(f
);
2209 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2210 if (le
->section_id
== section_id
) {
2215 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2220 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2222 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2228 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2234 cpu_synchronize_all_post_init();
2239 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2240 QLIST_REMOVE(le
, entry
);
2245 ret
= qemu_file_get_error(f
);
2251 static BlockDriverState
*find_vmstate_bs(void)
2253 BlockDriverState
*bs
= NULL
;
2254 while ((bs
= bdrv_next(bs
))) {
2255 if (bdrv_can_snapshot(bs
)) {
2263 * Deletes snapshots of a given name in all opened images.
2265 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2267 BlockDriverState
*bs
;
2268 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2272 while ((bs
= bdrv_next(bs
))) {
2273 if (bdrv_can_snapshot(bs
) &&
2274 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2276 ret
= bdrv_snapshot_delete(bs
, name
);
2279 "Error while deleting snapshot on '%s'\n",
2280 bdrv_get_device_name(bs
));
2289 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2291 BlockDriverState
*bs
, *bs1
;
2292 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2295 int saved_vm_running
;
2296 uint64_t vm_state_size
;
2299 const char *name
= qdict_get_try_str(qdict
, "name");
2301 /* Verify if there is a device that doesn't support snapshots and is writable */
2303 while ((bs
= bdrv_next(bs
))) {
2305 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2309 if (!bdrv_can_snapshot(bs
)) {
2310 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2311 bdrv_get_device_name(bs
));
2316 bs
= find_vmstate_bs();
2318 monitor_printf(mon
, "No block device can accept snapshots\n");
2322 saved_vm_running
= runstate_is_running();
2323 vm_stop(RUN_STATE_SAVE_VM
);
2325 memset(sn
, 0, sizeof(*sn
));
2327 /* fill auxiliary fields */
2328 qemu_gettimeofday(&tv
);
2329 sn
->date_sec
= tv
.tv_sec
;
2330 sn
->date_nsec
= tv
.tv_usec
* 1000;
2331 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2334 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2336 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2337 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2339 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2342 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2343 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2344 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2347 /* Delete old snapshots of the same name */
2348 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2352 /* save the VM state */
2353 f
= qemu_fopen_bdrv(bs
, 1);
2355 monitor_printf(mon
, "Could not open VM state file\n");
2358 ret
= qemu_savevm_state(f
);
2359 vm_state_size
= qemu_ftell(f
);
2362 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2366 /* create the snapshots */
2369 while ((bs1
= bdrv_next(bs1
))) {
2370 if (bdrv_can_snapshot(bs1
)) {
2371 /* Write VM state size only to the image that contains the state */
2372 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2373 ret
= bdrv_snapshot_create(bs1
, sn
);
2375 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2376 bdrv_get_device_name(bs1
));
2382 if (saved_vm_running
)
2386 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2389 int saved_vm_running
;
2392 saved_vm_running
= runstate_is_running();
2393 vm_stop(RUN_STATE_SAVE_VM
);
2395 f
= qemu_fopen(filename
, "wb");
2397 error_setg_file_open(errp
, errno
, filename
);
2400 ret
= qemu_save_device_state(f
);
2403 error_set(errp
, QERR_IO_ERROR
);
2407 if (saved_vm_running
)
2411 int load_vmstate(const char *name
)
2413 BlockDriverState
*bs
, *bs_vm_state
;
2414 QEMUSnapshotInfo sn
;
2418 bs_vm_state
= find_vmstate_bs();
2420 error_report("No block device supports snapshots");
2424 /* Don't even try to load empty VM states */
2425 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2428 } else if (sn
.vm_state_size
== 0) {
2429 error_report("This is a disk-only snapshot. Revert to it offline "
2434 /* Verify if there is any device that doesn't support snapshots and is
2435 writable and check if the requested snapshot is available too. */
2437 while ((bs
= bdrv_next(bs
))) {
2439 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2443 if (!bdrv_can_snapshot(bs
)) {
2444 error_report("Device '%s' is writable but does not support snapshots.",
2445 bdrv_get_device_name(bs
));
2449 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2451 error_report("Device '%s' does not have the requested snapshot '%s'",
2452 bdrv_get_device_name(bs
), name
);
2457 /* Flush all IO requests so they don't interfere with the new state. */
2461 while ((bs
= bdrv_next(bs
))) {
2462 if (bdrv_can_snapshot(bs
)) {
2463 ret
= bdrv_snapshot_goto(bs
, name
);
2465 error_report("Error %d while activating snapshot '%s' on '%s'",
2466 ret
, name
, bdrv_get_device_name(bs
));
2472 /* restore the VM state */
2473 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2475 error_report("Could not open VM state file");
2479 qemu_system_reset(VMRESET_SILENT
);
2480 ret
= qemu_loadvm_state(f
);
2484 error_report("Error %d while loading VM state", ret
);
2491 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2493 BlockDriverState
*bs
, *bs1
;
2495 const char *name
= qdict_get_str(qdict
, "name");
2497 bs
= find_vmstate_bs();
2499 monitor_printf(mon
, "No block device supports snapshots\n");
2504 while ((bs1
= bdrv_next(bs1
))) {
2505 if (bdrv_can_snapshot(bs1
)) {
2506 ret
= bdrv_snapshot_delete(bs1
, name
);
2508 if (ret
== -ENOTSUP
)
2510 "Snapshots not supported on device '%s'\n",
2511 bdrv_get_device_name(bs1
));
2513 monitor_printf(mon
, "Error %d while deleting snapshot on "
2514 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2520 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2522 BlockDriverState
*bs
, *bs1
;
2523 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2524 int nb_sns
, i
, ret
, available
;
2526 int *available_snapshots
;
2528 bs
= find_vmstate_bs();
2530 monitor_printf(mon
, "No available block device supports snapshots\n");
2534 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2536 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2541 monitor_printf(mon
, "There is no snapshot available.\n");
2545 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2547 for (i
= 0; i
< nb_sns
; i
++) {
2552 while ((bs1
= bdrv_next(bs1
))) {
2553 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2554 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2563 available_snapshots
[total
] = i
;
2569 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
2570 monitor_printf(mon
, "\n");
2571 for (i
= 0; i
< total
; i
++) {
2572 sn
= &sn_tab
[available_snapshots
[i
]];
2573 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
2574 monitor_printf(mon
, "\n");
2577 monitor_printf(mon
, "There is no suitable snapshot available\n");
2581 g_free(available_snapshots
);
2585 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2587 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2588 memory_region_name(mr
), dev
);
2591 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2593 /* Nothing do to while the implementation is in RAMBlock */
2596 void vmstate_register_ram_global(MemoryRegion
*mr
)
2598 vmstate_register_ram(mr
, NULL
);