4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
41 #include "qemu/bitops.h"
43 #include "block/snapshot.h"
44 #include "block/qapi.h"
46 #define SELF_ANNOUNCE_ROUNDS 5
49 #define ETH_P_RARP 0x8035
51 #define ARP_HTYPE_ETH 0x0001
52 #define ARP_PTYPE_IP 0x0800
53 #define ARP_OP_REQUEST_REV 0x3
55 static int announce_self_create(uint8_t *buf
,
58 /* Ethernet header. */
59 memset(buf
, 0xff, 6); /* destination MAC addr */
60 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
61 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
64 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
65 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
66 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
67 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
69 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
70 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
72 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
74 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf
+ 42, 0x00, 18);
77 return 60; /* len (FCS will be added by hardware) */
80 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
85 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
87 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
91 static void qemu_announce_self_once(void *opaque
)
93 static int count
= SELF_ANNOUNCE_ROUNDS
;
94 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
96 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
99 /* delay 50ms, 150ms, 250ms, ... */
100 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
101 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
103 qemu_del_timer(timer
);
104 qemu_free_timer(timer
);
108 void qemu_announce_self(void)
110 static QEMUTimer
*timer
;
111 timer
= qemu_new_timer_ms(rt_clock
, qemu_announce_self_once
, &timer
);
112 qemu_announce_self_once(&timer
);
115 /***********************************************************/
116 /* savevm/loadvm support */
118 #define IO_BUF_SIZE 32768
119 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
122 const QEMUFileOps
*ops
;
128 int64_t pos
; /* start of buffer when writing, end of buffer
131 int buf_size
; /* 0 when writing */
132 uint8_t buf
[IO_BUF_SIZE
];
134 struct iovec iov
[MAX_IOV_SIZE
];
140 typedef struct QEMUFileStdio
146 typedef struct QEMUFileSocket
157 static void fd_coroutine_enter(void *opaque
)
159 FDYieldUntilData
*data
= opaque
;
160 qemu_set_fd_handler(data
->fd
, NULL
, NULL
, NULL
);
161 qemu_coroutine_enter(data
->co
, NULL
);
165 * Yield until a file descriptor becomes readable
167 * Note that this function clobbers the handlers for the file descriptor.
169 static void coroutine_fn
yield_until_fd_readable(int fd
)
171 FDYieldUntilData data
;
173 assert(qemu_in_coroutine());
174 data
.co
= qemu_coroutine_self();
176 qemu_set_fd_handler(fd
, fd_coroutine_enter
, NULL
, &data
);
177 qemu_coroutine_yield();
180 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
183 QEMUFileSocket
*s
= opaque
;
185 ssize_t size
= iov_size(iov
, iovcnt
);
187 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
189 len
= -socket_error();
194 static int socket_get_fd(void *opaque
)
196 QEMUFileSocket
*s
= opaque
;
201 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
203 QEMUFileSocket
*s
= opaque
;
207 len
= qemu_recv(s
->fd
, buf
, size
, 0);
211 if (socket_error() == EAGAIN
) {
212 yield_until_fd_readable(s
->fd
);
213 } else if (socket_error() != EINTR
) {
219 len
= -socket_error();
224 static int socket_close(void *opaque
)
226 QEMUFileSocket
*s
= opaque
;
232 static int stdio_get_fd(void *opaque
)
234 QEMUFileStdio
*s
= opaque
;
236 return fileno(s
->stdio_file
);
239 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
241 QEMUFileStdio
*s
= opaque
;
242 return fwrite(buf
, 1, size
, s
->stdio_file
);
245 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
247 QEMUFileStdio
*s
= opaque
;
248 FILE *fp
= s
->stdio_file
;
253 bytes
= fread(buf
, 1, size
, fp
);
254 if (bytes
!= 0 || !ferror(fp
)) {
257 if (errno
== EAGAIN
) {
258 yield_until_fd_readable(fileno(fp
));
259 } else if (errno
!= EINTR
) {
266 static int stdio_pclose(void *opaque
)
268 QEMUFileStdio
*s
= opaque
;
270 ret
= pclose(s
->stdio_file
);
273 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
274 /* close succeeded, but non-zero exit code: */
275 ret
= -EIO
; /* fake errno value */
281 static int stdio_fclose(void *opaque
)
283 QEMUFileStdio
*s
= opaque
;
286 if (s
->file
->ops
->put_buffer
|| s
->file
->ops
->writev_buffer
) {
287 int fd
= fileno(s
->stdio_file
);
290 ret
= fstat(fd
, &st
);
291 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
293 * If the file handle is a regular file make sure the
294 * data is flushed to disk before signaling success.
303 if (fclose(s
->stdio_file
) == EOF
) {
310 static const QEMUFileOps stdio_pipe_read_ops
= {
311 .get_fd
= stdio_get_fd
,
312 .get_buffer
= stdio_get_buffer
,
313 .close
= stdio_pclose
316 static const QEMUFileOps stdio_pipe_write_ops
= {
317 .get_fd
= stdio_get_fd
,
318 .put_buffer
= stdio_put_buffer
,
319 .close
= stdio_pclose
322 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
327 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
328 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
332 stdio_file
= popen(command
, mode
);
333 if (stdio_file
== NULL
) {
337 s
= g_malloc0(sizeof(QEMUFileStdio
));
339 s
->stdio_file
= stdio_file
;
342 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
344 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
349 static const QEMUFileOps stdio_file_read_ops
= {
350 .get_fd
= stdio_get_fd
,
351 .get_buffer
= stdio_get_buffer
,
352 .close
= stdio_fclose
355 static const QEMUFileOps stdio_file_write_ops
= {
356 .get_fd
= stdio_get_fd
,
357 .put_buffer
= stdio_put_buffer
,
358 .close
= stdio_fclose
361 static ssize_t
unix_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
364 QEMUFileSocket
*s
= opaque
;
366 ssize_t size
= iov_size(iov
, iovcnt
);
372 /* Find the next start position; skip all full-sized vector elements */
373 while (offset
>= iov
[0].iov_len
) {
374 offset
-= iov
[0].iov_len
;
378 /* skip `offset' bytes from the (now) first element, undo it on exit */
380 iov
[0].iov_base
+= offset
;
381 iov
[0].iov_len
-= offset
;
384 len
= writev(s
->fd
, iov
, iovcnt
);
385 } while (len
== -1 && errno
== EINTR
);
390 /* Undo the changes above */
391 iov
[0].iov_base
-= offset
;
392 iov
[0].iov_len
+= offset
;
394 /* Prepare for the next iteration */
403 static int unix_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
405 QEMUFileSocket
*s
= opaque
;
409 len
= read(s
->fd
, buf
, size
);
413 if (errno
== EAGAIN
) {
414 yield_until_fd_readable(s
->fd
);
415 } else if (errno
!= EINTR
) {
426 static int unix_close(void *opaque
)
428 QEMUFileSocket
*s
= opaque
;
434 static const QEMUFileOps unix_read_ops
= {
435 .get_fd
= socket_get_fd
,
436 .get_buffer
= unix_get_buffer
,
440 static const QEMUFileOps unix_write_ops
= {
441 .get_fd
= socket_get_fd
,
442 .writev_buffer
= unix_writev_buffer
,
446 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
451 (mode
[0] != 'r' && mode
[0] != 'w') ||
452 mode
[1] != 'b' || mode
[2] != 0) {
453 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
457 s
= g_malloc0(sizeof(QEMUFileSocket
));
461 s
->file
= qemu_fopen_ops(s
, &unix_read_ops
);
463 s
->file
= qemu_fopen_ops(s
, &unix_write_ops
);
468 static const QEMUFileOps socket_read_ops
= {
469 .get_fd
= socket_get_fd
,
470 .get_buffer
= socket_get_buffer
,
471 .close
= socket_close
474 static const QEMUFileOps socket_write_ops
= {
475 .get_fd
= socket_get_fd
,
476 .writev_buffer
= socket_writev_buffer
,
477 .close
= socket_close
480 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
485 (mode
[0] != 'r' && mode
[0] != 'w') ||
486 mode
[1] != 'b' || mode
[2] != 0) {
487 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
491 s
= g_malloc0(sizeof(QEMUFileSocket
));
493 if (mode
[0] == 'w') {
494 qemu_set_block(s
->fd
);
495 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
497 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
502 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
507 (mode
[0] != 'r' && mode
[0] != 'w') ||
508 mode
[1] != 'b' || mode
[2] != 0) {
509 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
513 s
= g_malloc0(sizeof(QEMUFileStdio
));
515 s
->stdio_file
= fopen(filename
, mode
);
520 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
522 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
530 static ssize_t
block_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
,
536 qemu_iovec_init_external(&qiov
, iov
, iovcnt
);
537 ret
= bdrv_writev_vmstate(opaque
, &qiov
, pos
);
545 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
546 int64_t pos
, int size
)
548 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
552 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
554 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
557 static int bdrv_fclose(void *opaque
)
559 return bdrv_flush(opaque
);
562 static const QEMUFileOps bdrv_read_ops
= {
563 .get_buffer
= block_get_buffer
,
567 static const QEMUFileOps bdrv_write_ops
= {
568 .put_buffer
= block_put_buffer
,
569 .writev_buffer
= block_writev_buffer
,
573 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
576 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
577 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
580 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
584 f
= g_malloc0(sizeof(QEMUFile
));
591 int qemu_file_get_error(QEMUFile
*f
)
593 return f
->last_error
;
596 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
598 if (f
->last_error
== 0) {
603 static inline bool qemu_file_is_writable(QEMUFile
*f
)
605 return f
->ops
->writev_buffer
|| f
->ops
->put_buffer
;
609 * Flushes QEMUFile buffer
611 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
614 static void qemu_fflush(QEMUFile
*f
)
618 if (!qemu_file_is_writable(f
)) {
622 if (f
->ops
->writev_buffer
) {
624 ret
= f
->ops
->writev_buffer(f
->opaque
, f
->iov
, f
->iovcnt
, f
->pos
);
627 if (f
->buf_index
> 0) {
628 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
637 qemu_file_set_error(f
, ret
);
641 static void qemu_fill_buffer(QEMUFile
*f
)
646 assert(!qemu_file_is_writable(f
));
648 pending
= f
->buf_size
- f
->buf_index
;
650 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
653 f
->buf_size
= pending
;
655 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
656 IO_BUF_SIZE
- pending
);
660 } else if (len
== 0) {
661 qemu_file_set_error(f
, -EIO
);
662 } else if (len
!= -EAGAIN
)
663 qemu_file_set_error(f
, len
);
666 int qemu_get_fd(QEMUFile
*f
)
668 if (f
->ops
->get_fd
) {
669 return f
->ops
->get_fd(f
->opaque
);
674 void qemu_update_position(QEMUFile
*f
, size_t size
)
681 * Returns negative error value if any error happened on previous operations or
682 * while closing the file. Returns 0 or positive number on success.
684 * The meaning of return value on success depends on the specific backend
687 int qemu_fclose(QEMUFile
*f
)
691 ret
= qemu_file_get_error(f
);
694 int ret2
= f
->ops
->close(f
->opaque
);
699 /* If any error was spotted before closing, we should report it
700 * instead of the close() return value.
709 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
711 /* check for adjacent buffer and coalesce them */
712 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
713 f
->iov
[f
->iovcnt
- 1].iov_len
) {
714 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
716 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
717 f
->iov
[f
->iovcnt
++].iov_len
= size
;
720 if (f
->iovcnt
>= MAX_IOV_SIZE
) {
725 void qemu_put_buffer_async(QEMUFile
*f
, const uint8_t *buf
, int size
)
727 if (!f
->ops
->writev_buffer
) {
728 qemu_put_buffer(f
, buf
, size
);
736 f
->bytes_xfer
+= size
;
737 add_to_iovec(f
, buf
, size
);
740 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
749 l
= IO_BUF_SIZE
- f
->buf_index
;
752 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
753 f
->bytes_xfer
+= size
;
754 if (f
->ops
->writev_buffer
) {
755 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
758 if (f
->buf_index
== IO_BUF_SIZE
) {
761 if (qemu_file_get_error(f
)) {
769 void qemu_put_byte(QEMUFile
*f
, int v
)
775 f
->buf
[f
->buf_index
] = v
;
777 if (f
->ops
->writev_buffer
) {
778 add_to_iovec(f
, f
->buf
+ f
->buf_index
, 1);
781 if (f
->buf_index
== IO_BUF_SIZE
) {
786 static void qemu_file_skip(QEMUFile
*f
, int size
)
788 if (f
->buf_index
+ size
<= f
->buf_size
) {
789 f
->buf_index
+= size
;
793 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
798 assert(!qemu_file_is_writable(f
));
800 index
= f
->buf_index
+ offset
;
801 pending
= f
->buf_size
- index
;
802 if (pending
< size
) {
804 index
= f
->buf_index
+ offset
;
805 pending
= f
->buf_size
- index
;
811 if (size
> pending
) {
815 memcpy(buf
, f
->buf
+ index
, size
);
819 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
824 while (pending
> 0) {
827 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
831 qemu_file_skip(f
, res
);
839 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
841 int index
= f
->buf_index
+ offset
;
843 assert(!qemu_file_is_writable(f
));
845 if (index
>= f
->buf_size
) {
847 index
= f
->buf_index
+ offset
;
848 if (index
>= f
->buf_size
) {
852 return f
->buf
[index
];
855 int qemu_get_byte(QEMUFile
*f
)
859 result
= qemu_peek_byte(f
, 0);
860 qemu_file_skip(f
, 1);
864 int64_t qemu_ftell(QEMUFile
*f
)
870 int qemu_file_rate_limit(QEMUFile
*f
)
872 if (qemu_file_get_error(f
)) {
875 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
881 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
883 return f
->xfer_limit
;
886 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
888 f
->xfer_limit
= limit
;
891 void qemu_file_reset_rate_limit(QEMUFile
*f
)
896 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
898 qemu_put_byte(f
, v
>> 8);
902 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
904 qemu_put_byte(f
, v
>> 24);
905 qemu_put_byte(f
, v
>> 16);
906 qemu_put_byte(f
, v
>> 8);
910 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
912 qemu_put_be32(f
, v
>> 32);
916 unsigned int qemu_get_be16(QEMUFile
*f
)
919 v
= qemu_get_byte(f
) << 8;
920 v
|= qemu_get_byte(f
);
924 unsigned int qemu_get_be32(QEMUFile
*f
)
927 v
= qemu_get_byte(f
) << 24;
928 v
|= qemu_get_byte(f
) << 16;
929 v
|= qemu_get_byte(f
) << 8;
930 v
|= qemu_get_byte(f
);
934 uint64_t qemu_get_be64(QEMUFile
*f
)
937 v
= (uint64_t)qemu_get_be32(f
) << 32;
938 v
|= qemu_get_be32(f
);
945 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
947 uint64_t expire_time
;
949 expire_time
= qemu_timer_expire_time_ns(ts
);
950 qemu_put_be64(f
, expire_time
);
953 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
955 uint64_t expire_time
;
957 expire_time
= qemu_get_be64(f
);
958 if (expire_time
!= -1) {
959 qemu_mod_timer_ns(ts
, expire_time
);
968 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
971 *v
= qemu_get_byte(f
);
975 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
978 qemu_put_byte(f
, *v
);
981 const VMStateInfo vmstate_info_bool
= {
989 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
996 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
1002 const VMStateInfo vmstate_info_int8
= {
1010 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
1013 qemu_get_sbe16s(f
, v
);
1017 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
1020 qemu_put_sbe16s(f
, v
);
1023 const VMStateInfo vmstate_info_int16
= {
1031 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
1034 qemu_get_sbe32s(f
, v
);
1038 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
1041 qemu_put_sbe32s(f
, v
);
1044 const VMStateInfo vmstate_info_int32
= {
1050 /* 32 bit int. See that the received value is the same than the one
1053 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1057 qemu_get_sbe32s(f
, &v2
);
1064 const VMStateInfo vmstate_info_int32_equal
= {
1065 .name
= "int32 equal",
1066 .get
= get_int32_equal
,
1070 /* 32 bit int. See that the received value is the less or the same
1071 than the one in the field */
1073 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
1077 qemu_get_sbe32s(f
, &new);
1084 const VMStateInfo vmstate_info_int32_le
= {
1085 .name
= "int32 equal",
1086 .get
= get_int32_le
,
1092 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
1095 qemu_get_sbe64s(f
, v
);
1099 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
1102 qemu_put_sbe64s(f
, v
);
1105 const VMStateInfo vmstate_info_int64
= {
1111 /* 8 bit unsigned int */
1113 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1120 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1126 const VMStateInfo vmstate_info_uint8
= {
1132 /* 16 bit unsigned int */
1134 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1137 qemu_get_be16s(f
, v
);
1141 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1144 qemu_put_be16s(f
, v
);
1147 const VMStateInfo vmstate_info_uint16
= {
1153 /* 32 bit unsigned int */
1155 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1158 qemu_get_be32s(f
, v
);
1162 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1165 qemu_put_be32s(f
, v
);
1168 const VMStateInfo vmstate_info_uint32
= {
1174 /* 32 bit uint. See that the received value is the same than the one
1177 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1181 qemu_get_be32s(f
, &v2
);
1189 const VMStateInfo vmstate_info_uint32_equal
= {
1190 .name
= "uint32 equal",
1191 .get
= get_uint32_equal
,
1195 /* 64 bit unsigned int */
1197 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1200 qemu_get_be64s(f
, v
);
1204 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1207 qemu_put_be64s(f
, v
);
1210 const VMStateInfo vmstate_info_uint64
= {
1216 /* 64 bit unsigned int. See that the received value is the same than the one
1219 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1223 qemu_get_be64s(f
, &v2
);
1231 const VMStateInfo vmstate_info_uint64_equal
= {
1232 .name
= "int64 equal",
1233 .get
= get_uint64_equal
,
1237 /* 8 bit int. See that the received value is the same than the one
1240 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1244 qemu_get_8s(f
, &v2
);
1251 const VMStateInfo vmstate_info_uint8_equal
= {
1252 .name
= "uint8 equal",
1253 .get
= get_uint8_equal
,
1257 /* 16 bit unsigned int int. See that the received value is the same than the one
1260 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1264 qemu_get_be16s(f
, &v2
);
1271 const VMStateInfo vmstate_info_uint16_equal
= {
1272 .name
= "uint16 equal",
1273 .get
= get_uint16_equal
,
1277 /* floating point */
1279 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1283 *v
= make_float64(qemu_get_be64(f
));
1287 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1291 qemu_put_be64(f
, float64_val(*v
));
1294 const VMStateInfo vmstate_info_float64
= {
1302 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1305 qemu_get_timer(f
, v
);
1309 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1312 qemu_put_timer(f
, v
);
1315 const VMStateInfo vmstate_info_timer
= {
1321 /* uint8_t buffers */
1323 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1326 qemu_get_buffer(f
, v
, size
);
1330 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1333 qemu_put_buffer(f
, v
, size
);
1336 const VMStateInfo vmstate_info_buffer
= {
1342 /* unused buffers: space that was used for some fields that are
1343 not useful anymore */
1345 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1351 block_len
= MIN(sizeof(buf
), size
);
1353 qemu_get_buffer(f
, buf
, block_len
);
1358 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1360 static const uint8_t buf
[1024];
1364 block_len
= MIN(sizeof(buf
), size
);
1366 qemu_put_buffer(f
, buf
, block_len
);
1370 const VMStateInfo vmstate_info_unused_buffer
= {
1371 .name
= "unused_buffer",
1372 .get
= get_unused_buffer
,
1373 .put
= put_unused_buffer
,
1376 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1377 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1378 * bit words with the bits in big endian order. The in-memory format
1379 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1381 /* This is the number of 64 bit words sent over the wire */
1382 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1383 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1385 unsigned long *bmp
= pv
;
1387 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1388 uint64_t w
= qemu_get_be64(f
);
1390 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1391 bmp
[idx
++] = w
>> 32;
1397 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1399 unsigned long *bmp
= pv
;
1401 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1402 uint64_t w
= bmp
[idx
++];
1403 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1404 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1406 qemu_put_be64(f
, w
);
1410 const VMStateInfo vmstate_info_bitmap
= {
1416 typedef struct CompatEntry
{
1421 typedef struct SaveStateEntry
{
1422 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1428 SaveVMHandlers
*ops
;
1429 const VMStateDescription
*vmsd
;
1431 CompatEntry
*compat
;
1437 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1438 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1439 static int global_section_id
;
1441 static int calculate_new_instance_id(const char *idstr
)
1444 int instance_id
= 0;
1446 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1447 if (strcmp(idstr
, se
->idstr
) == 0
1448 && instance_id
<= se
->instance_id
) {
1449 instance_id
= se
->instance_id
+ 1;
1455 static int calculate_compat_instance_id(const char *idstr
)
1458 int instance_id
= 0;
1460 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1464 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1465 && instance_id
<= se
->compat
->instance_id
) {
1466 instance_id
= se
->compat
->instance_id
+ 1;
1472 /* TODO: Individual devices generally have very little idea about the rest
1473 of the system, so instance_id should be removed/replaced.
1474 Meanwhile pass -1 as instance_id if you do not already have a clearly
1475 distinguishing id for all instances of your device class. */
1476 int register_savevm_live(DeviceState
*dev
,
1480 SaveVMHandlers
*ops
,
1485 se
= g_malloc0(sizeof(SaveStateEntry
));
1486 se
->version_id
= version_id
;
1487 se
->section_id
= global_section_id
++;
1489 se
->opaque
= opaque
;
1492 /* if this is a live_savem then set is_ram */
1493 if (ops
->save_live_setup
!= NULL
) {
1498 char *id
= qdev_get_dev_path(dev
);
1500 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1501 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1504 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1505 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1506 se
->compat
->instance_id
= instance_id
== -1 ?
1507 calculate_compat_instance_id(idstr
) : instance_id
;
1511 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1513 if (instance_id
== -1) {
1514 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1516 se
->instance_id
= instance_id
;
1518 assert(!se
->compat
|| se
->instance_id
== 0);
1519 /* add at the end of list */
1520 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1524 int register_savevm(DeviceState
*dev
,
1528 SaveStateHandler
*save_state
,
1529 LoadStateHandler
*load_state
,
1532 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1533 ops
->save_state
= save_state
;
1534 ops
->load_state
= load_state
;
1535 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1539 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1541 SaveStateEntry
*se
, *new_se
;
1545 char *path
= qdev_get_dev_path(dev
);
1547 pstrcpy(id
, sizeof(id
), path
);
1548 pstrcat(id
, sizeof(id
), "/");
1552 pstrcat(id
, sizeof(id
), idstr
);
1554 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1555 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1556 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1566 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1567 const VMStateDescription
*vmsd
,
1568 void *opaque
, int alias_id
,
1569 int required_for_version
)
1573 /* If this triggers, alias support can be dropped for the vmsd. */
1574 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1576 se
= g_malloc0(sizeof(SaveStateEntry
));
1577 se
->version_id
= vmsd
->version_id
;
1578 se
->section_id
= global_section_id
++;
1579 se
->opaque
= opaque
;
1581 se
->alias_id
= alias_id
;
1582 se
->no_migrate
= vmsd
->unmigratable
;
1585 char *id
= qdev_get_dev_path(dev
);
1587 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1588 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1591 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1592 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1593 se
->compat
->instance_id
= instance_id
== -1 ?
1594 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1598 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1600 if (instance_id
== -1) {
1601 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1603 se
->instance_id
= instance_id
;
1605 assert(!se
->compat
|| se
->instance_id
== 0);
1606 /* add at the end of list */
1607 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1611 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1614 SaveStateEntry
*se
, *new_se
;
1616 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1617 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1618 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1627 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1629 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1632 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1633 void *opaque
, int version_id
)
1635 VMStateField
*field
= vmsd
->fields
;
1638 if (version_id
> vmsd
->version_id
) {
1641 if (version_id
< vmsd
->minimum_version_id_old
) {
1644 if (version_id
< vmsd
->minimum_version_id
) {
1645 return vmsd
->load_state_old(f
, opaque
, version_id
);
1647 if (vmsd
->pre_load
) {
1648 int ret
= vmsd
->pre_load(opaque
);
1652 while(field
->name
) {
1653 if ((field
->field_exists
&&
1654 field
->field_exists(opaque
, version_id
)) ||
1655 (!field
->field_exists
&&
1656 field
->version_id
<= version_id
)) {
1657 void *base_addr
= opaque
+ field
->offset
;
1659 int size
= field
->size
;
1661 if (field
->flags
& VMS_VBUFFER
) {
1662 size
= *(int32_t *)(opaque
+field
->size_offset
);
1663 if (field
->flags
& VMS_MULTIPLY
) {
1664 size
*= field
->size
;
1667 if (field
->flags
& VMS_ARRAY
) {
1668 n_elems
= field
->num
;
1669 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1670 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1671 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1672 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1673 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1674 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1675 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1676 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1678 if (field
->flags
& VMS_POINTER
) {
1679 base_addr
= *(void **)base_addr
+ field
->start
;
1681 for (i
= 0; i
< n_elems
; i
++) {
1682 void *addr
= base_addr
+ size
* i
;
1684 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1685 addr
= *(void **)addr
;
1687 if (field
->flags
& VMS_STRUCT
) {
1688 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1690 ret
= field
->info
->get(f
, addr
, size
);
1700 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1704 if (vmsd
->post_load
) {
1705 return vmsd
->post_load(opaque
, version_id
);
1710 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1713 VMStateField
*field
= vmsd
->fields
;
1715 if (vmsd
->pre_save
) {
1716 vmsd
->pre_save(opaque
);
1718 while(field
->name
) {
1719 if (!field
->field_exists
||
1720 field
->field_exists(opaque
, vmsd
->version_id
)) {
1721 void *base_addr
= opaque
+ field
->offset
;
1723 int size
= field
->size
;
1725 if (field
->flags
& VMS_VBUFFER
) {
1726 size
= *(int32_t *)(opaque
+field
->size_offset
);
1727 if (field
->flags
& VMS_MULTIPLY
) {
1728 size
*= field
->size
;
1731 if (field
->flags
& VMS_ARRAY
) {
1732 n_elems
= field
->num
;
1733 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1734 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1735 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1736 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1737 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1738 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1739 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1740 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1742 if (field
->flags
& VMS_POINTER
) {
1743 base_addr
= *(void **)base_addr
+ field
->start
;
1745 for (i
= 0; i
< n_elems
; i
++) {
1746 void *addr
= base_addr
+ size
* i
;
1748 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1749 addr
= *(void **)addr
;
1751 if (field
->flags
& VMS_STRUCT
) {
1752 vmstate_save_state(f
, field
->vmsd
, addr
);
1754 field
->info
->put(f
, addr
, size
);
1760 vmstate_subsection_save(f
, vmsd
, opaque
);
1763 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1765 if (!se
->vmsd
) { /* Old style */
1766 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1768 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1771 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1773 if (!se
->vmsd
) { /* Old style */
1774 se
->ops
->save_state(f
, se
->opaque
);
1777 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1780 #define QEMU_VM_FILE_MAGIC 0x5145564d
1781 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1782 #define QEMU_VM_FILE_VERSION 0x00000003
1784 #define QEMU_VM_EOF 0x00
1785 #define QEMU_VM_SECTION_START 0x01
1786 #define QEMU_VM_SECTION_PART 0x02
1787 #define QEMU_VM_SECTION_END 0x03
1788 #define QEMU_VM_SECTION_FULL 0x04
1789 #define QEMU_VM_SUBSECTION 0x05
1791 bool qemu_savevm_state_blocked(Error
**errp
)
1795 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1796 if (se
->no_migrate
) {
1797 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1804 void qemu_savevm_state_begin(QEMUFile
*f
,
1805 const MigrationParams
*params
)
1810 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1811 if (!se
->ops
|| !se
->ops
->set_params
) {
1814 se
->ops
->set_params(params
, se
->opaque
);
1817 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1818 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1820 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1823 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1826 if (se
->ops
&& se
->ops
->is_active
) {
1827 if (!se
->ops
->is_active(se
->opaque
)) {
1832 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1833 qemu_put_be32(f
, se
->section_id
);
1836 len
= strlen(se
->idstr
);
1837 qemu_put_byte(f
, len
);
1838 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1840 qemu_put_be32(f
, se
->instance_id
);
1841 qemu_put_be32(f
, se
->version_id
);
1843 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1845 qemu_file_set_error(f
, ret
);
1852 * this function has three return values:
1853 * negative: there was one error, and we have -errno.
1854 * 0 : We haven't finished, caller have to go again
1855 * 1 : We have finished, we can go to complete phase
1857 int qemu_savevm_state_iterate(QEMUFile
*f
)
1862 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1863 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1866 if (se
->ops
&& se
->ops
->is_active
) {
1867 if (!se
->ops
->is_active(se
->opaque
)) {
1871 if (qemu_file_rate_limit(f
)) {
1874 trace_savevm_section_start();
1876 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1877 qemu_put_be32(f
, se
->section_id
);
1879 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1880 trace_savevm_section_end(se
->section_id
);
1883 qemu_file_set_error(f
, ret
);
1886 /* Do not proceed to the next vmstate before this one reported
1887 completion of the current stage. This serializes the migration
1888 and reduces the probability that a faster changing state is
1889 synchronized over and over again. */
1896 void qemu_savevm_state_complete(QEMUFile
*f
)
1901 cpu_synchronize_all_states();
1903 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1904 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1907 if (se
->ops
&& se
->ops
->is_active
) {
1908 if (!se
->ops
->is_active(se
->opaque
)) {
1912 trace_savevm_section_start();
1914 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1915 qemu_put_be32(f
, se
->section_id
);
1917 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1918 trace_savevm_section_end(se
->section_id
);
1920 qemu_file_set_error(f
, ret
);
1925 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1928 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1931 trace_savevm_section_start();
1933 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1934 qemu_put_be32(f
, se
->section_id
);
1937 len
= strlen(se
->idstr
);
1938 qemu_put_byte(f
, len
);
1939 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1941 qemu_put_be32(f
, se
->instance_id
);
1942 qemu_put_be32(f
, se
->version_id
);
1944 vmstate_save(f
, se
);
1945 trace_savevm_section_end(se
->section_id
);
1948 qemu_put_byte(f
, QEMU_VM_EOF
);
1952 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1957 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1958 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1961 if (se
->ops
&& se
->ops
->is_active
) {
1962 if (!se
->ops
->is_active(se
->opaque
)) {
1966 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1971 void qemu_savevm_state_cancel(void)
1975 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1976 if (se
->ops
&& se
->ops
->cancel
) {
1977 se
->ops
->cancel(se
->opaque
);
1982 static int qemu_savevm_state(QEMUFile
*f
)
1985 MigrationParams params
= {
1990 if (qemu_savevm_state_blocked(NULL
)) {
1994 qemu_mutex_unlock_iothread();
1995 qemu_savevm_state_begin(f
, ¶ms
);
1996 qemu_mutex_lock_iothread();
1998 while (qemu_file_get_error(f
) == 0) {
1999 if (qemu_savevm_state_iterate(f
) > 0) {
2004 ret
= qemu_file_get_error(f
);
2006 qemu_savevm_state_complete(f
);
2007 ret
= qemu_file_get_error(f
);
2010 qemu_savevm_state_cancel();
2015 static int qemu_save_device_state(QEMUFile
*f
)
2019 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
2020 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
2022 cpu_synchronize_all_states();
2024 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2030 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
2035 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
2036 qemu_put_be32(f
, se
->section_id
);
2039 len
= strlen(se
->idstr
);
2040 qemu_put_byte(f
, len
);
2041 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
2043 qemu_put_be32(f
, se
->instance_id
);
2044 qemu_put_be32(f
, se
->version_id
);
2046 vmstate_save(f
, se
);
2049 qemu_put_byte(f
, QEMU_VM_EOF
);
2051 return qemu_file_get_error(f
);
2054 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
2058 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
2059 if (!strcmp(se
->idstr
, idstr
) &&
2060 (instance_id
== se
->instance_id
||
2061 instance_id
== se
->alias_id
))
2063 /* Migrating from an older version? */
2064 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
2065 if (!strcmp(se
->compat
->idstr
, idstr
) &&
2066 (instance_id
== se
->compat
->instance_id
||
2067 instance_id
== se
->alias_id
))
2074 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
2076 while(sub
&& sub
->needed
) {
2077 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
2085 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2088 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
2091 uint8_t version_id
, len
, size
;
2092 const VMStateDescription
*sub_vmsd
;
2094 len
= qemu_peek_byte(f
, 1);
2095 if (len
< strlen(vmsd
->name
) + 1) {
2096 /* subsection name has be be "section_name/a" */
2099 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2105 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2106 /* it don't have a valid subsection name */
2109 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2110 if (sub_vmsd
== NULL
) {
2113 qemu_file_skip(f
, 1); /* subsection */
2114 qemu_file_skip(f
, 1); /* len */
2115 qemu_file_skip(f
, len
); /* idstr */
2116 version_id
= qemu_get_be32(f
);
2118 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2126 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2129 const VMStateSubsection
*sub
= vmsd
->subsections
;
2131 while (sub
&& sub
->needed
) {
2132 if (sub
->needed(opaque
)) {
2133 const VMStateDescription
*vmsd
= sub
->vmsd
;
2136 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2137 len
= strlen(vmsd
->name
);
2138 qemu_put_byte(f
, len
);
2139 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2140 qemu_put_be32(f
, vmsd
->version_id
);
2141 vmstate_save_state(f
, vmsd
, opaque
);
2147 typedef struct LoadStateEntry
{
2148 QLIST_ENTRY(LoadStateEntry
) entry
;
2154 int qemu_loadvm_state(QEMUFile
*f
)
2156 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2157 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2158 LoadStateEntry
*le
, *new_le
;
2159 uint8_t section_type
;
2163 if (qemu_savevm_state_blocked(NULL
)) {
2167 v
= qemu_get_be32(f
);
2168 if (v
!= QEMU_VM_FILE_MAGIC
)
2171 v
= qemu_get_be32(f
);
2172 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2173 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2176 if (v
!= QEMU_VM_FILE_VERSION
)
2179 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2180 uint32_t instance_id
, version_id
, section_id
;
2185 switch (section_type
) {
2186 case QEMU_VM_SECTION_START
:
2187 case QEMU_VM_SECTION_FULL
:
2188 /* Read section start */
2189 section_id
= qemu_get_be32(f
);
2190 len
= qemu_get_byte(f
);
2191 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2193 instance_id
= qemu_get_be32(f
);
2194 version_id
= qemu_get_be32(f
);
2196 /* Find savevm section */
2197 se
= find_se(idstr
, instance_id
);
2199 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2204 /* Validate version */
2205 if (version_id
> se
->version_id
) {
2206 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2207 version_id
, idstr
, se
->version_id
);
2213 le
= g_malloc0(sizeof(*le
));
2216 le
->section_id
= section_id
;
2217 le
->version_id
= version_id
;
2218 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2220 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2222 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2223 instance_id
, idstr
);
2227 case QEMU_VM_SECTION_PART
:
2228 case QEMU_VM_SECTION_END
:
2229 section_id
= qemu_get_be32(f
);
2231 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2232 if (le
->section_id
== section_id
) {
2237 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2242 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2244 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2250 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2256 cpu_synchronize_all_post_init();
2261 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2262 QLIST_REMOVE(le
, entry
);
2267 ret
= qemu_file_get_error(f
);
2273 static BlockDriverState
*find_vmstate_bs(void)
2275 BlockDriverState
*bs
= NULL
;
2276 while ((bs
= bdrv_next(bs
))) {
2277 if (bdrv_can_snapshot(bs
)) {
2285 * Deletes snapshots of a given name in all opened images.
2287 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2289 BlockDriverState
*bs
;
2290 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2294 while ((bs
= bdrv_next(bs
))) {
2295 if (bdrv_can_snapshot(bs
) &&
2296 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2298 ret
= bdrv_snapshot_delete(bs
, name
);
2301 "Error while deleting snapshot on '%s'\n",
2302 bdrv_get_device_name(bs
));
2311 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2313 BlockDriverState
*bs
, *bs1
;
2314 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2317 int saved_vm_running
;
2318 uint64_t vm_state_size
;
2321 const char *name
= qdict_get_try_str(qdict
, "name");
2323 /* Verify if there is a device that doesn't support snapshots and is writable */
2325 while ((bs
= bdrv_next(bs
))) {
2327 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2331 if (!bdrv_can_snapshot(bs
)) {
2332 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2333 bdrv_get_device_name(bs
));
2338 bs
= find_vmstate_bs();
2340 monitor_printf(mon
, "No block device can accept snapshots\n");
2344 saved_vm_running
= runstate_is_running();
2345 vm_stop(RUN_STATE_SAVE_VM
);
2347 memset(sn
, 0, sizeof(*sn
));
2349 /* fill auxiliary fields */
2350 qemu_gettimeofday(&tv
);
2351 sn
->date_sec
= tv
.tv_sec
;
2352 sn
->date_nsec
= tv
.tv_usec
* 1000;
2353 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2356 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2358 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2359 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2361 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2364 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2365 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2366 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2369 /* Delete old snapshots of the same name */
2370 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2374 /* save the VM state */
2375 f
= qemu_fopen_bdrv(bs
, 1);
2377 monitor_printf(mon
, "Could not open VM state file\n");
2380 ret
= qemu_savevm_state(f
);
2381 vm_state_size
= qemu_ftell(f
);
2384 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2388 /* create the snapshots */
2391 while ((bs1
= bdrv_next(bs1
))) {
2392 if (bdrv_can_snapshot(bs1
)) {
2393 /* Write VM state size only to the image that contains the state */
2394 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2395 ret
= bdrv_snapshot_create(bs1
, sn
);
2397 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2398 bdrv_get_device_name(bs1
));
2404 if (saved_vm_running
)
2408 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2411 int saved_vm_running
;
2414 saved_vm_running
= runstate_is_running();
2415 vm_stop(RUN_STATE_SAVE_VM
);
2417 f
= qemu_fopen(filename
, "wb");
2419 error_setg_file_open(errp
, errno
, filename
);
2422 ret
= qemu_save_device_state(f
);
2425 error_set(errp
, QERR_IO_ERROR
);
2429 if (saved_vm_running
)
2433 int load_vmstate(const char *name
)
2435 BlockDriverState
*bs
, *bs_vm_state
;
2436 QEMUSnapshotInfo sn
;
2440 bs_vm_state
= find_vmstate_bs();
2442 error_report("No block device supports snapshots");
2446 /* Don't even try to load empty VM states */
2447 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2450 } else if (sn
.vm_state_size
== 0) {
2451 error_report("This is a disk-only snapshot. Revert to it offline "
2456 /* Verify if there is any device that doesn't support snapshots and is
2457 writable and check if the requested snapshot is available too. */
2459 while ((bs
= bdrv_next(bs
))) {
2461 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2465 if (!bdrv_can_snapshot(bs
)) {
2466 error_report("Device '%s' is writable but does not support snapshots.",
2467 bdrv_get_device_name(bs
));
2471 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2473 error_report("Device '%s' does not have the requested snapshot '%s'",
2474 bdrv_get_device_name(bs
), name
);
2479 /* Flush all IO requests so they don't interfere with the new state. */
2483 while ((bs
= bdrv_next(bs
))) {
2484 if (bdrv_can_snapshot(bs
)) {
2485 ret
= bdrv_snapshot_goto(bs
, name
);
2487 error_report("Error %d while activating snapshot '%s' on '%s'",
2488 ret
, name
, bdrv_get_device_name(bs
));
2494 /* restore the VM state */
2495 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2497 error_report("Could not open VM state file");
2501 qemu_system_reset(VMRESET_SILENT
);
2502 ret
= qemu_loadvm_state(f
);
2506 error_report("Error %d while loading VM state", ret
);
2513 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2515 BlockDriverState
*bs
, *bs1
;
2517 const char *name
= qdict_get_str(qdict
, "name");
2519 bs
= find_vmstate_bs();
2521 monitor_printf(mon
, "No block device supports snapshots\n");
2526 while ((bs1
= bdrv_next(bs1
))) {
2527 if (bdrv_can_snapshot(bs1
)) {
2528 ret
= bdrv_snapshot_delete(bs1
, name
);
2530 if (ret
== -ENOTSUP
)
2532 "Snapshots not supported on device '%s'\n",
2533 bdrv_get_device_name(bs1
));
2535 monitor_printf(mon
, "Error %d while deleting snapshot on "
2536 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2542 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2544 BlockDriverState
*bs
, *bs1
;
2545 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2546 int nb_sns
, i
, ret
, available
;
2548 int *available_snapshots
;
2550 bs
= find_vmstate_bs();
2552 monitor_printf(mon
, "No available block device supports snapshots\n");
2556 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2558 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2563 monitor_printf(mon
, "There is no snapshot available.\n");
2567 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2569 for (i
= 0; i
< nb_sns
; i
++) {
2574 while ((bs1
= bdrv_next(bs1
))) {
2575 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2576 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2585 available_snapshots
[total
] = i
;
2591 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, NULL
);
2592 monitor_printf(mon
, "\n");
2593 for (i
= 0; i
< total
; i
++) {
2594 sn
= &sn_tab
[available_snapshots
[i
]];
2595 bdrv_snapshot_dump((fprintf_function
)monitor_printf
, mon
, sn
);
2596 monitor_printf(mon
, "\n");
2599 monitor_printf(mon
, "There is no suitable snapshot available\n");
2603 g_free(available_snapshots
);
2607 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2609 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2610 memory_region_name(mr
), dev
);
2613 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2615 /* Nothing do to while the implementation is in RAMBlock */
2618 void vmstate_register_ram_global(MemoryRegion
*mr
)
2620 vmstate_register_ram(mr
, NULL
);