4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
41 #include "qemu/bitops.h"
44 #define SELF_ANNOUNCE_ROUNDS 5
47 #define ETH_P_RARP 0x8035
49 #define ARP_HTYPE_ETH 0x0001
50 #define ARP_PTYPE_IP 0x0800
51 #define ARP_OP_REQUEST_REV 0x3
53 static int announce_self_create(uint8_t *buf
,
56 /* Ethernet header. */
57 memset(buf
, 0xff, 6); /* destination MAC addr */
58 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
59 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
62 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
63 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
64 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
65 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
67 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
68 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
70 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
72 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf
+ 42, 0x00, 18);
75 return 60; /* len (FCS will be added by hardware) */
78 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
83 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
85 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
89 static void qemu_announce_self_once(void *opaque
)
91 static int count
= SELF_ANNOUNCE_ROUNDS
;
92 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
94 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
97 /* delay 50ms, 150ms, 250ms, ... */
98 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
99 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
101 qemu_del_timer(timer
);
102 qemu_free_timer(timer
);
106 void qemu_announce_self(void)
108 static QEMUTimer
*timer
;
109 timer
= qemu_new_timer_ms(rt_clock
, qemu_announce_self_once
, &timer
);
110 qemu_announce_self_once(&timer
);
113 /***********************************************************/
114 /* savevm/loadvm support */
116 #define IO_BUF_SIZE 32768
117 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
120 const QEMUFileOps
*ops
;
126 int64_t pos
; /* start of buffer when writing, end of buffer
129 int buf_size
; /* 0 when writing */
130 uint8_t buf
[IO_BUF_SIZE
];
132 struct iovec iov
[MAX_IOV_SIZE
];
138 typedef struct QEMUFileStdio
144 typedef struct QEMUFileSocket
155 static void fd_coroutine_enter(void *opaque
)
157 FDYieldUntilData
*data
= opaque
;
158 qemu_set_fd_handler(data
->fd
, NULL
, NULL
, NULL
);
159 qemu_coroutine_enter(data
->co
, NULL
);
163 * Yield until a file descriptor becomes readable
165 * Note that this function clobbers the handlers for the file descriptor.
167 static void coroutine_fn
yield_until_fd_readable(int fd
)
169 FDYieldUntilData data
;
171 assert(qemu_in_coroutine());
172 data
.co
= qemu_coroutine_self();
174 qemu_set_fd_handler(fd
, fd_coroutine_enter
, NULL
, &data
);
175 qemu_coroutine_yield();
178 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
181 QEMUFileSocket
*s
= opaque
;
183 ssize_t size
= iov_size(iov
, iovcnt
);
185 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
187 len
= -socket_error();
192 static int socket_get_fd(void *opaque
)
194 QEMUFileSocket
*s
= opaque
;
199 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
201 QEMUFileSocket
*s
= opaque
;
205 len
= qemu_recv(s
->fd
, buf
, size
, 0);
209 if (socket_error() == EAGAIN
) {
210 yield_until_fd_readable(s
->fd
);
211 } else if (socket_error() != EINTR
) {
217 len
= -socket_error();
222 static int socket_close(void *opaque
)
224 QEMUFileSocket
*s
= opaque
;
230 static int stdio_get_fd(void *opaque
)
232 QEMUFileStdio
*s
= opaque
;
234 return fileno(s
->stdio_file
);
237 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
239 QEMUFileStdio
*s
= opaque
;
240 return fwrite(buf
, 1, size
, s
->stdio_file
);
243 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
245 QEMUFileStdio
*s
= opaque
;
246 FILE *fp
= s
->stdio_file
;
251 bytes
= fread(buf
, 1, size
, fp
);
252 if (bytes
!= 0 || !ferror(fp
)) {
255 if (errno
== EAGAIN
) {
256 yield_until_fd_readable(fileno(fp
));
257 } else if (errno
!= EINTR
) {
264 static int stdio_pclose(void *opaque
)
266 QEMUFileStdio
*s
= opaque
;
268 ret
= pclose(s
->stdio_file
);
271 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
272 /* close succeeded, but non-zero exit code: */
273 ret
= -EIO
; /* fake errno value */
279 static int stdio_fclose(void *opaque
)
281 QEMUFileStdio
*s
= opaque
;
284 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
285 int fd
= fileno(s
->stdio_file
);
288 ret
= fstat(fd
, &st
);
289 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
291 * If the file handle is a regular file make sure the
292 * data is flushed to disk before signaling success.
301 if (fclose(s
->stdio_file
) == EOF
) {
308 static const QEMUFileOps stdio_pipe_read_ops
= {
309 .get_fd
= stdio_get_fd
,
310 .get_buffer
= stdio_get_buffer
,
311 .close
= stdio_pclose
314 static const QEMUFileOps stdio_pipe_write_ops
= {
315 .get_fd
= stdio_get_fd
,
316 .put_buffer
= stdio_put_buffer
,
317 .close
= stdio_pclose
320 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
325 stdio_file
= popen(command
, mode
);
326 if (stdio_file
== NULL
) {
330 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
331 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
335 s
= g_malloc0(sizeof(QEMUFileStdio
));
337 s
->stdio_file
= stdio_file
;
340 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
342 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
347 static const QEMUFileOps stdio_file_read_ops
= {
348 .get_fd
= stdio_get_fd
,
349 .get_buffer
= stdio_get_buffer
,
350 .close
= stdio_fclose
353 static const QEMUFileOps stdio_file_write_ops
= {
354 .get_fd
= stdio_get_fd
,
355 .put_buffer
= stdio_put_buffer
,
356 .close
= stdio_fclose
359 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
362 QEMUFileSocket
*s
= opaque
;
364 ssize_t size
= iov_size(iov
, iovcnt
);
370 /* Find the next start position; skip all full-sized vector elements */
371 while (offset
>= iov
[0].iov_len
) {
372 offset
-= iov
[0].iov_len
;
376 /* skip `offset' bytes from the (now) first element, undo it on exit */
378 iov
[0].iov_base
+= offset
;
379 iov
[0].iov_len
-= offset
;
382 len
= writev(s
->fd
, iov
, iovcnt
);
383 } while (len
== -1 && errno
== EINTR
);
388 /* Undo the changes above */
389 iov
[0].iov_base
-= offset
;
390 iov
[0].iov_len
+= offset
;
392 /* Prepare for the next iteration */
401 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
403 QEMUFileSocket
*s
= opaque
;
407 len
= read(s
->fd
, buf
, size
);
411 if (errno
== EAGAIN
) {
412 yield_until_fd_readable(s
->fd
);
413 } else if (errno
!= EINTR
) {
424 static int unix_close(void *opaque
)
426 QEMUFileSocket
*s
= opaque
;
432 static const QEMUFileOps unix_read_ops
= {
433 .get_fd
= socket_get_fd
,
434 .get_buffer
= unix_get_buffer
,
438 static const QEMUFileOps unix_write_ops
= {
439 .get_fd
= socket_get_fd
,
440 .writev_buffer
= unix_writev_buffer
,
444 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
449 (mode
[0] != 'r' && mode
[0] != 'w') ||
450 mode
[1] != 'b' || mode
[2] != 0) {
451 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
455 s
= g_malloc0(sizeof(QEMUFileSocket
));
459 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
461 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
466 static const QEMUFileOps socket_read_ops
= {
467 .get_fd
= socket_get_fd
,
468 .get_buffer
= socket_get_buffer
,
469 .close
= socket_close
472 static const QEMUFileOps socket_write_ops
= {
473 .get_fd
= socket_get_fd
,
474 .writev_buffer
= socket_writev_buffer
,
475 .close
= socket_close
478 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
480 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
483 (mode
[0] != 'r' && mode
[0] != 'w') ||
484 mode
[1] != 'b' || mode
[2] != 0) {
485 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
490 if (mode
[0] == 'w') {
491 qemu_set_block(s
->fd
);
492 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
494 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
499 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
504 (mode
[0] != 'r' && mode
[0] != 'w') ||
505 mode
[1] != 'b' || mode
[2] != 0) {
506 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
510 s
= g_malloc0(sizeof(QEMUFileStdio
));
512 s
->stdio_file
= fopen(filename
, mode
);
517 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
519 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
527 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
533 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
534 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
542 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
543 int64_t pos
, int size
)
545 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
549 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
551 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
554 static int bdrv_fclose(void *opaque
)
556 return bdrv_flush(opaque
);
559 static const QEMUFileOps bdrv_read_ops
= {
560 .get_buffer
= block_get_buffer
,
564 static const QEMUFileOps bdrv_write_ops
= {
565 .put_buffer
= block_put_buffer
,
566 .writev_buffer
= block_writev_buffer
,
570 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
573 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
574 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
577 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
581 f
= g_malloc0(sizeof(QEMUFile
));
588 int qemu_file_get_error(QEMUFile
*f
)
590 return f
->last_error
;
593 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
595 if (f
->last_error
== 0) {
600 static inline bool qemu_file_is_writable(QEMUFile
*f
)
602 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
606 * Flushes QEMUFile buffer
608 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
611 static void qemu_fflush(QEMUFile
*f
)
615 if (!qemu_file_is_writable(f
)) {
619 if (f
->ops
->writev_buffer
) {
621 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
624 if (f
->buf_index
> 0) {
625 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
634 qemu_file_set_error(f
, ret
);
638 static void qemu_fill_buffer(QEMUFile
*f
)
643 assert(!qemu_file_is_writable(f
));
645 pending
= f
->buf_size
- f
->buf_index
;
647 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
650 f
->buf_size
= pending
;
652 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
653 IO_BUF_SIZE
- pending
);
657 } else if (len
== 0) {
658 qemu_file_set_error(f
, -EIO
);
659 } else if (len
!= -EAGAIN
)
660 qemu_file_set_error(f
, len
);
663 int qemu_get_fd(QEMUFile
*f
)
665 if (f
->ops
->get_fd
) {
666 return f
->ops
->get_fd(f
->opaque
);
673 * Returns negative error value if any error happened on previous operations or
674 * while closing the file. Returns 0 or positive number on success.
676 * The meaning of return value on success depends on the specific backend
679 int qemu_fclose(QEMUFile
*f
)
683 ret
= qemu_file_get_error(f
);
686 int ret2
= f
->ops
->close(f
->opaque
);
691 /* If any error was spotted before closing, we should report it
692 * instead of the close() return value.
701 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
703 /* check for adjacent buffer and coalesce them */
704 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
705 f
->iov
[f
->iovcnt
- 1].iov_len
) {
706 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
708 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
709 f
->iov
[f
->iovcnt
++].iov_len
= size
;
712 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
717 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
719 if (!f
->ops
->writev_buffer
) {
720 qemu_put_buffer(f
, buf
, size
);
728 f
->bytes_xfer
+= size
;
729 add_to_iovec(f
, buf
, size
);
732 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
741 l
= IO_BUF_SIZE
- f
->buf_index
;
744 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
745 f
->bytes_xfer
+= size
;
746 if (f
->ops
->writev_buffer
) {
747 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
750 if (f
->buf_index
== IO_BUF_SIZE
) {
753 if (qemu_file_get_error(f
)) {
761 void qemu_put_byte(QEMUFile
*f
, int v
)
767 f
->buf
[f
->buf_index
] = v
;
769 if (f
->ops
->writev_buffer
) {
770 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
773 if (f
->buf_index
== IO_BUF_SIZE
) {
778 static void qemu_file_skip(QEMUFile
*f
, int size
)
780 if (f
->buf_index
+ size
<= f
->buf_size
) {
781 f
->buf_index
+= size
;
785 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
790 assert(!qemu_file_is_writable(f
));
792 index
= f
->buf_index
+ offset
;
793 pending
= f
->buf_size
- index
;
794 if (pending
< size
) {
796 index
= f
->buf_index
+ offset
;
797 pending
= f
->buf_size
- index
;
803 if (size
> pending
) {
807 memcpy(buf
, f
->buf
+ index
, size
);
811 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
816 while (pending
> 0) {
819 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
823 qemu_file_skip(f
, res
);
831 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
833 int index
= f
->buf_index
+ offset
;
835 assert(!qemu_file_is_writable(f
));
837 if (index
>= f
->buf_size
) {
839 index
= f
->buf_index
+ offset
;
840 if (index
>= f
->buf_size
) {
844 return f
->buf
[index
];
847 int qemu_get_byte(QEMUFile
*f
)
851 result
= qemu_peek_byte(f
, 0);
852 qemu_file_skip(f
, 1);
856 int64_t qemu_ftell(QEMUFile
*f
)
862 int qemu_file_rate_limit(QEMUFile
*f
)
864 if (qemu_file_get_error(f
)) {
867 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
873 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
875 return f
->xfer_limit
;
878 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
880 f
->xfer_limit
= limit
;
883 void qemu_file_reset_rate_limit(QEMUFile
*f
)
888 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
890 qemu_put_byte(f
, v
>> 8);
894 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
896 qemu_put_byte(f
, v
>> 24);
897 qemu_put_byte(f
, v
>> 16);
898 qemu_put_byte(f
, v
>> 8);
902 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
904 qemu_put_be32(f
, v
>> 32);
908 unsigned int qemu_get_be16(QEMUFile
*f
)
911 v
= qemu_get_byte(f
) << 8;
912 v
|= qemu_get_byte(f
);
916 unsigned int qemu_get_be32(QEMUFile
*f
)
919 v
= qemu_get_byte(f
) << 24;
920 v
|= qemu_get_byte(f
) << 16;
921 v
|= qemu_get_byte(f
) << 8;
922 v
|= qemu_get_byte(f
);
926 uint64_t qemu_get_be64(QEMUFile
*f
)
929 v
= (uint64_t)qemu_get_be32(f
) << 32;
930 v
|= qemu_get_be32(f
);
937 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
939 uint64_t expire_time
;
941 expire_time
= qemu_timer_expire_time_ns(ts
);
942 qemu_put_be64(f
, expire_time
);
945 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
947 uint64_t expire_time
;
949 expire_time
= qemu_get_be64(f
);
950 if (expire_time
!= -1) {
951 qemu_mod_timer_ns(ts
, expire_time
);
960 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
963 *v
= qemu_get_byte(f
);
967 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
970 qemu_put_byte(f
, *v
);
973 const VMStateInfo vmstate_info_bool
= {
981 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
988 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
994 const VMStateInfo vmstate_info_int8
= {
1002 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
1005 qemu_get_sbe16s(f
, v
);
1009 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
1012 qemu_put_sbe16s(f
, v
);
1015 const VMStateInfo vmstate_info_int16
= {
1023 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
1026 qemu_get_sbe32s(f
, v
);
1030 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
1033 qemu_put_sbe32s(f
, v
);
1036 const VMStateInfo vmstate_info_int32
= {
1042 /* 32 bit int. See that the received value is the same than the one
1045 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1049 qemu_get_sbe32s(f
, &v2
);
1056 const VMStateInfo vmstate_info_int32_equal
= {
1057 .name
= "int32 equal",
1058 .get
= get_int32_equal
,
1062 /* 32 bit int. See that the received value is the less or the same
1063 than the one in the field */
1065 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
1069 qemu_get_sbe32s(f
, &new);
1076 const VMStateInfo vmstate_info_int32_le
= {
1077 .name
= "int32 equal",
1078 .get
= get_int32_le
,
1084 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1087 qemu_get_sbe64s(f
, v
);
1091 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1094 qemu_put_sbe64s(f
, v
);
1097 const VMStateInfo vmstate_info_int64
= {
1103 /* 8 bit unsigned int */
1105 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1112 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1118 const VMStateInfo vmstate_info_uint8
= {
1124 /* 16 bit unsigned int */
1126 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1129 qemu_get_be16s(f
, v
);
1133 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1136 qemu_put_be16s(f
, v
);
1139 const VMStateInfo vmstate_info_uint16
= {
1145 /* 32 bit unsigned int */
1147 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1150 qemu_get_be32s(f
, v
);
1154 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1157 qemu_put_be32s(f
, v
);
1160 const VMStateInfo vmstate_info_uint32
= {
1166 /* 32 bit uint. See that the received value is the same than the one
1169 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1173 qemu_get_be32s(f
, &v2
);
1181 const VMStateInfo vmstate_info_uint32_equal
= {
1182 .name
= "uint32 equal",
1183 .get
= get_uint32_equal
,
1187 /* 64 bit unsigned int */
1189 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1192 qemu_get_be64s(f
, v
);
1196 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1199 qemu_put_be64s(f
, v
);
1202 const VMStateInfo vmstate_info_uint64
= {
1208 /* 64 bit unsigned int. See that the received value is the same than the one
1211 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1215 qemu_get_be64s(f
, &v2
);
1223 const VMStateInfo vmstate_info_uint64_equal
= {
1224 .name
= "int64 equal",
1225 .get
= get_uint64_equal
,
1229 /* 8 bit int. See that the received value is the same than the one
1232 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1236 qemu_get_8s(f
, &v2
);
1243 const VMStateInfo vmstate_info_uint8_equal
= {
1244 .name
= "uint8 equal",
1245 .get
= get_uint8_equal
,
1249 /* 16 bit unsigned int int. See that the received value is the same than the one
1252 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1256 qemu_get_be16s(f
, &v2
);
1263 const VMStateInfo vmstate_info_uint16_equal
= {
1264 .name
= "uint16 equal",
1265 .get
= get_uint16_equal
,
1269 /* floating point */
1271 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1275 *v
= make_float64(qemu_get_be64(f
));
1279 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1283 qemu_put_be64(f
, float64_val(*v
));
1286 const VMStateInfo vmstate_info_float64
= {
1294 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1297 qemu_get_timer(f
, v
);
1301 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1304 qemu_put_timer(f
, v
);
1307 const VMStateInfo vmstate_info_timer
= {
1313 /* uint8_t buffers */
1315 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1318 qemu_get_buffer(f
, v
, size
);
1322 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1325 qemu_put_buffer(f
, v
, size
);
1328 const VMStateInfo vmstate_info_buffer
= {
1334 /* unused buffers: space that was used for some fields that are
1335 not useful anymore */
1337 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1343 block_len
= MIN(sizeof(buf
), size
);
1345 qemu_get_buffer(f
, buf
, block_len
);
1350 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1352 static const uint8_t buf
[1024];
1356 block_len
= MIN(sizeof(buf
), size
);
1358 qemu_put_buffer(f
, buf
, block_len
);
1362 const VMStateInfo vmstate_info_unused_buffer
= {
1363 .name
= "unused_buffer",
1364 .get
= get_unused_buffer
,
1365 .put
= put_unused_buffer
,
1368 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1369 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1370 * bit words with the bits in big endian order. The in-memory format
1371 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1373 /* This is the number of 64 bit words sent over the wire */
1374 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1375 static int get_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
= qemu_get_be64(f
);
1382 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1383 bmp
[idx
++] = w
>> 32;
1389 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1391 unsigned long *bmp
= pv
;
1393 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1394 uint64_t w
= bmp
[idx
++];
1395 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1396 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1398 qemu_put_be64(f
, w
);
1402 const VMStateInfo vmstate_info_bitmap
= {
1408 typedef struct CompatEntry
{
1413 typedef struct SaveStateEntry
{
1414 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1420 SaveVMHandlers
*ops
;
1421 const VMStateDescription
*vmsd
;
1423 CompatEntry
*compat
;
1429 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1430 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1431 static int global_section_id
;
1433 static int calculate_new_instance_id(const char *idstr
)
1436 int instance_id
= 0;
1438 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1439 if (strcmp(idstr
, se
->idstr
) == 0
1440 && instance_id
<= se
->instance_id
) {
1441 instance_id
= se
->instance_id
+ 1;
1447 static int calculate_compat_instance_id(const char *idstr
)
1450 int instance_id
= 0;
1452 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1456 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1457 && instance_id
<= se
->compat
->instance_id
) {
1458 instance_id
= se
->compat
->instance_id
+ 1;
1464 /* TODO: Individual devices generally have very little idea about the rest
1465 of the system, so instance_id should be removed/replaced.
1466 Meanwhile pass -1 as instance_id if you do not already have a clearly
1467 distinguishing id for all instances of your device class. */
1468 int register_savevm_live(DeviceState
*dev
,
1472 SaveVMHandlers
*ops
,
1477 se
= g_malloc0(sizeof(SaveStateEntry
));
1478 se
->version_id
= version_id
;
1479 se
->section_id
= global_section_id
++;
1481 se
->opaque
= opaque
;
1484 /* if this is a live_savem then set is_ram */
1485 if (ops
->save_live_setup
!= NULL
) {
1490 char *id
= qdev_get_dev_path(dev
);
1492 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1493 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1496 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1497 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1498 se
->compat
->instance_id
= instance_id
== -1 ?
1499 calculate_compat_instance_id(idstr
) : instance_id
;
1503 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1505 if (instance_id
== -1) {
1506 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1508 se
->instance_id
= instance_id
;
1510 assert(!se
->compat
|| se
->instance_id
== 0);
1511 /* add at the end of list */
1512 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1516 int register_savevm(DeviceState
*dev
,
1520 SaveStateHandler
*save_state
,
1521 LoadStateHandler
*load_state
,
1524 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1525 ops
->save_state
= save_state
;
1526 ops
->load_state
= load_state
;
1527 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1531 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1533 SaveStateEntry
*se
, *new_se
;
1537 char *path
= qdev_get_dev_path(dev
);
1539 pstrcpy(id
, sizeof(id
), path
);
1540 pstrcat(id
, sizeof(id
), "/");
1544 pstrcat(id
, sizeof(id
), idstr
);
1546 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1547 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1548 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1558 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1559 const VMStateDescription
*vmsd
,
1560 void *opaque
, int alias_id
,
1561 int required_for_version
)
1565 /* If this triggers, alias support can be dropped for the vmsd. */
1566 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1568 se
= g_malloc0(sizeof(SaveStateEntry
));
1569 se
->version_id
= vmsd
->version_id
;
1570 se
->section_id
= global_section_id
++;
1571 se
->opaque
= opaque
;
1573 se
->alias_id
= alias_id
;
1574 se
->no_migrate
= vmsd
->unmigratable
;
1577 char *id
= qdev_get_dev_path(dev
);
1579 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1580 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1583 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1584 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1585 se
->compat
->instance_id
= instance_id
== -1 ?
1586 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1590 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1592 if (instance_id
== -1) {
1593 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1595 se
->instance_id
= instance_id
;
1597 assert(!se
->compat
|| se
->instance_id
== 0);
1598 /* add at the end of list */
1599 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1603 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1606 SaveStateEntry
*se
, *new_se
;
1608 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1609 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1610 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1619 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1621 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1624 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1625 void *opaque
, int version_id
)
1627 VMStateField
*field
= vmsd
->fields
;
1630 if (version_id
> vmsd
->version_id
) {
1633 if (version_id
< vmsd
->minimum_version_id_old
) {
1636 if (version_id
< vmsd
->minimum_version_id
) {
1637 return vmsd
->load_state_old(f
, opaque
, version_id
);
1639 if (vmsd
->pre_load
) {
1640 int ret
= vmsd
->pre_load(opaque
);
1644 while(field
->name
) {
1645 if ((field
->field_exists
&&
1646 field
->field_exists(opaque
, version_id
)) ||
1647 (!field
->field_exists
&&
1648 field
->version_id
<= version_id
)) {
1649 void *base_addr
= opaque
+ field
->offset
;
1651 int size
= field
->size
;
1653 if (field
->flags
& VMS_VBUFFER
) {
1654 size
= *(int32_t *)(opaque
+field
->size_offset
);
1655 if (field
->flags
& VMS_MULTIPLY
) {
1656 size
*= field
->size
;
1659 if (field
->flags
& VMS_ARRAY
) {
1660 n_elems
= field
->num
;
1661 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1662 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1663 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1664 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1665 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1666 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1667 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1668 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1670 if (field
->flags
& VMS_POINTER
) {
1671 base_addr
= *(void **)base_addr
+ field
->start
;
1673 for (i
= 0; i
< n_elems
; i
++) {
1674 void *addr
= base_addr
+ size
* i
;
1676 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1677 addr
= *(void **)addr
;
1679 if (field
->flags
& VMS_STRUCT
) {
1680 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1682 ret
= field
->info
->get(f
, addr
, size
);
1692 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1696 if (vmsd
->post_load
) {
1697 return vmsd
->post_load(opaque
, version_id
);
1702 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1705 VMStateField
*field
= vmsd
->fields
;
1707 if (vmsd
->pre_save
) {
1708 vmsd
->pre_save(opaque
);
1710 while(field
->name
) {
1711 if (!field
->field_exists
||
1712 field
->field_exists(opaque
, vmsd
->version_id
)) {
1713 void *base_addr
= opaque
+ field
->offset
;
1715 int size
= field
->size
;
1717 if (field
->flags
& VMS_VBUFFER
) {
1718 size
= *(int32_t *)(opaque
+field
->size_offset
);
1719 if (field
->flags
& VMS_MULTIPLY
) {
1720 size
*= field
->size
;
1723 if (field
->flags
& VMS_ARRAY
) {
1724 n_elems
= field
->num
;
1725 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1726 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1727 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1728 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1729 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1730 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1731 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1732 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1734 if (field
->flags
& VMS_POINTER
) {
1735 base_addr
= *(void **)base_addr
+ field
->start
;
1737 for (i
= 0; i
< n_elems
; i
++) {
1738 void *addr
= base_addr
+ size
* i
;
1740 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1741 addr
= *(void **)addr
;
1743 if (field
->flags
& VMS_STRUCT
) {
1744 vmstate_save_state(f
, field
->vmsd
, addr
);
1746 field
->info
->put(f
, addr
, size
);
1752 vmstate_subsection_save(f
, vmsd
, opaque
);
1755 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1757 if (!se
->vmsd
) { /* Old style */
1758 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1760 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1763 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1765 if (!se
->vmsd
) { /* Old style */
1766 se
->ops
->save_state(f
, se
->opaque
);
1769 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1772 #define QEMU_VM_FILE_MAGIC 0x5145564d
1773 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1774 #define QEMU_VM_FILE_VERSION 0x00000003
1776 #define QEMU_VM_EOF 0x00
1777 #define QEMU_VM_SECTION_START 0x01
1778 #define QEMU_VM_SECTION_PART 0x02
1779 #define QEMU_VM_SECTION_END 0x03
1780 #define QEMU_VM_SECTION_FULL 0x04
1781 #define QEMU_VM_SUBSECTION 0x05
1783 bool qemu_savevm_state_blocked(Error
**errp
)
1787 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1788 if (se
->no_migrate
) {
1789 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1796 void qemu_savevm_state_begin(QEMUFile
*f
,
1797 const MigrationParams
*params
)
1802 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1803 if (!se
->ops
|| !se
->ops
->set_params
) {
1806 se
->ops
->set_params(params
, se
->opaque
);
1809 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1810 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1812 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1815 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1818 if (se
->ops
&& se
->ops
->is_active
) {
1819 if (!se
->ops
->is_active(se
->opaque
)) {
1824 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1825 qemu_put_be32(f
, se
->section_id
);
1828 len
= strlen(se
->idstr
);
1829 qemu_put_byte(f
, len
);
1830 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1832 qemu_put_be32(f
, se
->instance_id
);
1833 qemu_put_be32(f
, se
->version_id
);
1835 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1837 qemu_file_set_error(f
, ret
);
1844 * this function has three return values:
1845 * negative: there was one error, and we have -errno.
1846 * 0 : We haven't finished, caller have to go again
1847 * 1 : We have finished, we can go to complete phase
1849 int qemu_savevm_state_iterate(QEMUFile
*f
)
1854 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1855 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1858 if (se
->ops
&& se
->ops
->is_active
) {
1859 if (!se
->ops
->is_active(se
->opaque
)) {
1863 if (qemu_file_rate_limit(f
)) {
1866 trace_savevm_section_start();
1868 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1869 qemu_put_be32(f
, se
->section_id
);
1871 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1872 trace_savevm_section_end(se
->section_id
);
1875 qemu_file_set_error(f
, ret
);
1878 /* Do not proceed to the next vmstate before this one reported
1879 completion of the current stage. This serializes the migration
1880 and reduces the probability that a faster changing state is
1881 synchronized over and over again. */
1888 void qemu_savevm_state_complete(QEMUFile
*f
)
1893 cpu_synchronize_all_states();
1895 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1896 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1899 if (se
->ops
&& se
->ops
->is_active
) {
1900 if (!se
->ops
->is_active(se
->opaque
)) {
1904 trace_savevm_section_start();
1906 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1907 qemu_put_be32(f
, se
->section_id
);
1909 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1910 trace_savevm_section_end(se
->section_id
);
1912 qemu_file_set_error(f
, ret
);
1917 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1920 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1923 trace_savevm_section_start();
1925 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1926 qemu_put_be32(f
, se
->section_id
);
1929 len
= strlen(se
->idstr
);
1930 qemu_put_byte(f
, len
);
1931 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1933 qemu_put_be32(f
, se
->instance_id
);
1934 qemu_put_be32(f
, se
->version_id
);
1936 vmstate_save(f
, se
);
1937 trace_savevm_section_end(se
->section_id
);
1940 qemu_put_byte(f
, QEMU_VM_EOF
);
1944 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1949 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1950 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1953 if (se
->ops
&& se
->ops
->is_active
) {
1954 if (!se
->ops
->is_active(se
->opaque
)) {
1958 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1963 void qemu_savevm_state_cancel(void)
1967 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1968 if (se
->ops
&& se
->ops
->cancel
) {
1969 se
->ops
->cancel(se
->opaque
);
1974 static int qemu_savevm_state(QEMUFile
*f
)
1977 MigrationParams params
= {
1982 if (qemu_savevm_state_blocked(NULL
)) {
1986 qemu_mutex_unlock_iothread();
1987 qemu_savevm_state_begin(f
, ¶ms
);
1988 qemu_mutex_lock_iothread();
1990 while (qemu_file_get_error(f
) == 0) {
1991 if (qemu_savevm_state_iterate(f
) > 0) {
1996 ret
= qemu_file_get_error(f
);
1998 qemu_savevm_state_complete(f
);
1999 ret
= qemu_file_get_error(f
);
2002 qemu_savevm_state_cancel();
2007 static int qemu_save_device_state(QEMUFile
*f
)
2011 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
2012 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
2014 cpu_synchronize_all_states();
2016 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2022 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
2027 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
2028 qemu_put_be32(f
, se
->section_id
);
2031 len
= strlen(se
->idstr
);
2032 qemu_put_byte(f
, len
);
2033 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
2035 qemu_put_be32(f
, se
->instance_id
);
2036 qemu_put_be32(f
, se
->version_id
);
2038 vmstate_save(f
, se
);
2041 qemu_put_byte(f
, QEMU_VM_EOF
);
2043 return qemu_file_get_error(f
);
2046 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
2050 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2051 if (!strcmp(se
->idstr
, idstr
) &&
2052 (instance_id
== se
->instance_id
||
2053 instance_id
== se
->alias_id
))
2055 /* Migrating from an older version? */
2056 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
2057 if (!strcmp(se
->compat
->idstr
, idstr
) &&
2058 (instance_id
== se
->compat
->instance_id
||
2059 instance_id
== se
->alias_id
))
2066 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
2068 while(sub
&& sub
->needed
) {
2069 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
2077 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2080 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
2083 uint8_t version_id
, len
, size
;
2084 const VMStateDescription
*sub_vmsd
;
2086 len
= qemu_peek_byte(f
, 1);
2087 if (len
< strlen(vmsd
->name
) + 1) {
2088 /* subsection name has be be "section_name/a" */
2091 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2097 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2098 /* it don't have a valid subsection name */
2101 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2102 if (sub_vmsd
== NULL
) {
2105 qemu_file_skip(f
, 1); /* subsection */
2106 qemu_file_skip(f
, 1); /* len */
2107 qemu_file_skip(f
, len
); /* idstr */
2108 version_id
= qemu_get_be32(f
);
2110 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2118 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2121 const VMStateSubsection
*sub
= vmsd
->subsections
;
2123 while (sub
&& sub
->needed
) {
2124 if (sub
->needed(opaque
)) {
2125 const VMStateDescription
*vmsd
= sub
->vmsd
;
2128 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2129 len
= strlen(vmsd
->name
);
2130 qemu_put_byte(f
, len
);
2131 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2132 qemu_put_be32(f
, vmsd
->version_id
);
2133 vmstate_save_state(f
, vmsd
, opaque
);
2139 typedef struct LoadStateEntry
{
2140 QLIST_ENTRY(LoadStateEntry
) entry
;
2146 int qemu_loadvm_state(QEMUFile
*f
)
2148 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2149 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2150 LoadStateEntry
*le
, *new_le
;
2151 uint8_t section_type
;
2155 if (qemu_savevm_state_blocked(NULL
)) {
2159 v
= qemu_get_be32(f
);
2160 if (v
!= QEMU_VM_FILE_MAGIC
)
2163 v
= qemu_get_be32(f
);
2164 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2165 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2168 if (v
!= QEMU_VM_FILE_VERSION
)
2171 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2172 uint32_t instance_id
, version_id
, section_id
;
2177 switch (section_type
) {
2178 case QEMU_VM_SECTION_START
:
2179 case QEMU_VM_SECTION_FULL
:
2180 /* Read section start */
2181 section_id
= qemu_get_be32(f
);
2182 len
= qemu_get_byte(f
);
2183 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2185 instance_id
= qemu_get_be32(f
);
2186 version_id
= qemu_get_be32(f
);
2188 /* Find savevm section */
2189 se
= find_se(idstr
, instance_id
);
2191 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2196 /* Validate version */
2197 if (version_id
> se
->version_id
) {
2198 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2199 version_id
, idstr
, se
->version_id
);
2205 le
= g_malloc0(sizeof(*le
));
2208 le
->section_id
= section_id
;
2209 le
->version_id
= version_id
;
2210 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2212 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2214 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2215 instance_id
, idstr
);
2219 case QEMU_VM_SECTION_PART
:
2220 case QEMU_VM_SECTION_END
:
2221 section_id
= qemu_get_be32(f
);
2223 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2224 if (le
->section_id
== section_id
) {
2229 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2234 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2236 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2242 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2248 cpu_synchronize_all_post_init();
2253 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2254 QLIST_REMOVE(le
, entry
);
2259 ret
= qemu_file_get_error(f
);
2265 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2268 QEMUSnapshotInfo
*sn_tab
, *sn
;
2272 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2275 for(i
= 0; i
< nb_sns
; i
++) {
2277 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2288 * Deletes snapshots of a given name in all opened images.
2290 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2292 BlockDriverState
*bs
;
2293 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2297 while ((bs
= bdrv_next(bs
))) {
2298 if (bdrv_can_snapshot(bs
) &&
2299 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2301 ret
= bdrv_snapshot_delete(bs
, name
);
2304 "Error while deleting snapshot on '%s'\n",
2305 bdrv_get_device_name(bs
));
2314 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2316 BlockDriverState
*bs
, *bs1
;
2317 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2320 int saved_vm_running
;
2321 uint64_t vm_state_size
;
2324 const char *name
= qdict_get_try_str(qdict
, "name");
2326 /* Verify if there is a device that doesn't support snapshots and is writable */
2328 while ((bs
= bdrv_next(bs
))) {
2330 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2334 if (!bdrv_can_snapshot(bs
)) {
2335 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2336 bdrv_get_device_name(bs
));
2341 bs
= bdrv_snapshots();
2343 monitor_printf(mon
, "No block device can accept snapshots\n");
2347 saved_vm_running
= runstate_is_running();
2348 vm_stop(RUN_STATE_SAVE_VM
);
2350 memset(sn
, 0, sizeof(*sn
));
2352 /* fill auxiliary fields */
2353 qemu_gettimeofday(&tv
);
2354 sn
->date_sec
= tv
.tv_sec
;
2355 sn
->date_nsec
= tv
.tv_usec
* 1000;
2356 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2359 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2361 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2362 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2364 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2367 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2368 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2369 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2372 /* Delete old snapshots of the same name */
2373 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2377 /* save the VM state */
2378 f
= qemu_fopen_bdrv(bs
, 1);
2380 monitor_printf(mon
, "Could not open VM state file\n");
2383 ret
= qemu_savevm_state(f
);
2384 vm_state_size
= qemu_ftell(f
);
2387 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2391 /* create the snapshots */
2394 while ((bs1
= bdrv_next(bs1
))) {
2395 if (bdrv_can_snapshot(bs1
)) {
2396 /* Write VM state size only to the image that contains the state */
2397 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2398 ret
= bdrv_snapshot_create(bs1
, sn
);
2400 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2401 bdrv_get_device_name(bs1
));
2407 if (saved_vm_running
)
2411 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2414 int saved_vm_running
;
2417 saved_vm_running
= runstate_is_running();
2418 vm_stop(RUN_STATE_SAVE_VM
);
2420 f
= qemu_fopen(filename
, "wb");
2422 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2425 ret
= qemu_save_device_state(f
);
2428 error_set(errp
, QERR_IO_ERROR
);
2432 if (saved_vm_running
)
2436 int load_vmstate(const char *name
)
2438 BlockDriverState
*bs
, *bs_vm_state
;
2439 QEMUSnapshotInfo sn
;
2443 bs_vm_state
= bdrv_snapshots();
2445 error_report("No block device supports snapshots");
2449 /* Don't even try to load empty VM states */
2450 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2453 } else if (sn
.vm_state_size
== 0) {
2454 error_report("This is a disk-only snapshot. Revert to it offline "
2459 /* Verify if there is any device that doesn't support snapshots and is
2460 writable and check if the requested snapshot is available too. */
2462 while ((bs
= bdrv_next(bs
))) {
2464 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2468 if (!bdrv_can_snapshot(bs
)) {
2469 error_report("Device '%s' is writable but does not support snapshots.",
2470 bdrv_get_device_name(bs
));
2474 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2476 error_report("Device '%s' does not have the requested snapshot '%s'",
2477 bdrv_get_device_name(bs
), name
);
2482 /* Flush all IO requests so they don't interfere with the new state. */
2486 while ((bs
= bdrv_next(bs
))) {
2487 if (bdrv_can_snapshot(bs
)) {
2488 ret
= bdrv_snapshot_goto(bs
, name
);
2490 error_report("Error %d while activating snapshot '%s' on '%s'",
2491 ret
, name
, bdrv_get_device_name(bs
));
2497 /* restore the VM state */
2498 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2500 error_report("Could not open VM state file");
2504 qemu_system_reset(VMRESET_SILENT
);
2505 ret
= qemu_loadvm_state(f
);
2509 error_report("Error %d while loading VM state", ret
);
2516 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2518 BlockDriverState
*bs
, *bs1
;
2520 const char *name
= qdict_get_str(qdict
, "name");
2522 bs
= bdrv_snapshots();
2524 monitor_printf(mon
, "No block device supports snapshots\n");
2529 while ((bs1
= bdrv_next(bs1
))) {
2530 if (bdrv_can_snapshot(bs1
)) {
2531 ret
= bdrv_snapshot_delete(bs1
, name
);
2533 if (ret
== -ENOTSUP
)
2535 "Snapshots not supported on device '%s'\n",
2536 bdrv_get_device_name(bs1
));
2538 monitor_printf(mon
, "Error %d while deleting snapshot on "
2539 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2545 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2547 BlockDriverState
*bs
, *bs1
;
2548 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2549 int nb_sns
, i
, ret
, available
;
2551 int *available_snapshots
;
2554 bs
= bdrv_snapshots();
2556 monitor_printf(mon
, "No available block device supports snapshots\n");
2560 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2562 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2567 monitor_printf(mon
, "There is no snapshot available.\n");
2571 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2573 for (i
= 0; i
< nb_sns
; i
++) {
2578 while ((bs1
= bdrv_next(bs1
))) {
2579 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2580 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2589 available_snapshots
[total
] = i
;
2595 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2596 for (i
= 0; i
< total
; i
++) {
2597 sn
= &sn_tab
[available_snapshots
[i
]];
2598 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2601 monitor_printf(mon
, "There is no suitable snapshot available\n");
2605 g_free(available_snapshots
);
2609 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2611 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2612 memory_region_name(mr
), dev
);
2615 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2617 /* Nothing do to while the implementation is in RAMBlock */
2620 void vmstate_register_ram_global(MemoryRegion
*mr
)
2622 vmstate_register_ram(mr
, NULL
);