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
119 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
];
135 typedef struct QEMUFileStdio
141 typedef struct QEMUFileSocket
152 static void fd_coroutine_enter(void *opaque
)
154 FDYieldUntilData
*data
= opaque
;
155 qemu_set_fd_handler(data
->fd
, NULL
, NULL
, NULL
);
156 qemu_coroutine_enter(data
->co
, NULL
);
160 * Yield until a file descriptor becomes readable
162 * Note that this function clobbers the handlers for the file descriptor.
164 static void coroutine_fn
yield_until_fd_readable(int fd
)
166 FDYieldUntilData data
;
168 assert(qemu_in_coroutine());
169 data
.co
= qemu_coroutine_self();
171 qemu_set_fd_handler(fd
, fd_coroutine_enter
, NULL
, &data
);
172 qemu_coroutine_yield();
175 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
)
177 QEMUFileSocket
*s
= opaque
;
179 ssize_t size
= iov_size(iov
, iovcnt
);
181 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
183 len
= -socket_error();
188 static int socket_get_fd(void *opaque
)
190 QEMUFileSocket
*s
= opaque
;
195 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
197 QEMUFileSocket
*s
= opaque
;
201 len
= qemu_recv(s
->fd
, buf
, size
, 0);
205 if (socket_error() == EAGAIN
) {
206 yield_until_fd_readable(s
->fd
);
207 } else if (socket_error() != EINTR
) {
213 len
= -socket_error();
218 static int socket_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
220 QEMUFileSocket
*s
= opaque
;
223 len
= qemu_send_full(s
->fd
, buf
, size
, 0);
225 len
= -socket_error();
230 static int socket_close(void *opaque
)
232 QEMUFileSocket
*s
= opaque
;
238 static int stdio_get_fd(void *opaque
)
240 QEMUFileStdio
*s
= opaque
;
242 return fileno(s
->stdio_file
);
245 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
247 QEMUFileStdio
*s
= opaque
;
248 return fwrite(buf
, 1, size
, s
->stdio_file
);
251 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
253 QEMUFileStdio
*s
= opaque
;
254 FILE *fp
= s
->stdio_file
;
259 bytes
= fread(buf
, 1, size
, fp
);
260 if (bytes
!= 0 || !ferror(fp
)) {
263 if (errno
== EAGAIN
) {
264 yield_until_fd_readable(fileno(fp
));
265 } else if (errno
!= EINTR
) {
272 static int stdio_pclose(void *opaque
)
274 QEMUFileStdio
*s
= opaque
;
276 ret
= pclose(s
->stdio_file
);
279 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
280 /* close succeeded, but non-zero exit code: */
281 ret
= -EIO
; /* fake errno value */
287 static int stdio_fclose(void *opaque
)
289 QEMUFileStdio
*s
= opaque
;
292 if (s
->file
->ops
->put_buffer
) {
293 int fd
= fileno(s
->stdio_file
);
296 ret
= fstat(fd
, &st
);
297 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
299 * If the file handle is a regular file make sure the
300 * data is flushed to disk before signaling success.
309 if (fclose(s
->stdio_file
) == EOF
) {
316 static const QEMUFileOps stdio_pipe_read_ops
= {
317 .get_fd
= stdio_get_fd
,
318 .get_buffer
= stdio_get_buffer
,
319 .close
= stdio_pclose
322 static const QEMUFileOps stdio_pipe_write_ops
= {
323 .get_fd
= stdio_get_fd
,
324 .put_buffer
= stdio_put_buffer
,
325 .close
= stdio_pclose
328 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
333 stdio_file
= popen(command
, mode
);
334 if (stdio_file
== NULL
) {
338 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
339 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
343 s
= g_malloc0(sizeof(QEMUFileStdio
));
345 s
->stdio_file
= stdio_file
;
348 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
350 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
355 static const QEMUFileOps stdio_file_read_ops
= {
356 .get_fd
= stdio_get_fd
,
357 .get_buffer
= stdio_get_buffer
,
358 .close
= stdio_fclose
361 static const QEMUFileOps stdio_file_write_ops
= {
362 .get_fd
= stdio_get_fd
,
363 .put_buffer
= stdio_put_buffer
,
364 .close
= stdio_fclose
367 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
372 (mode
[0] != 'r' && mode
[0] != 'w') ||
373 mode
[1] != 'b' || mode
[2] != 0) {
374 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
378 s
= g_malloc0(sizeof(QEMUFileStdio
));
379 s
->stdio_file
= fdopen(fd
, mode
);
384 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
386 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
395 static const QEMUFileOps socket_read_ops
= {
396 .get_fd
= socket_get_fd
,
397 .get_buffer
= socket_get_buffer
,
398 .close
= socket_close
401 static const QEMUFileOps socket_write_ops
= {
402 .get_fd
= socket_get_fd
,
403 .put_buffer
= socket_put_buffer
,
404 .writev_buffer
= socket_writev_buffer
,
405 .close
= socket_close
408 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
410 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
413 (mode
[0] != 'r' && mode
[0] != 'w') ||
414 mode
[1] != 'b' || mode
[2] != 0) {
415 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
420 if (mode
[0] == 'w') {
421 socket_set_block(s
->fd
);
422 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
424 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
429 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
434 (mode
[0] != 'r' && mode
[0] != 'w') ||
435 mode
[1] != 'b' || mode
[2] != 0) {
436 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
440 s
= g_malloc0(sizeof(QEMUFileStdio
));
442 s
->stdio_file
= fopen(filename
, mode
);
447 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
449 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
457 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
458 int64_t pos
, int size
)
460 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
464 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
466 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
469 static int bdrv_fclose(void *opaque
)
471 return bdrv_flush(opaque
);
474 static const QEMUFileOps bdrv_read_ops
= {
475 .get_buffer
= block_get_buffer
,
479 static const QEMUFileOps bdrv_write_ops
= {
480 .put_buffer
= block_put_buffer
,
484 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
487 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
488 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
491 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
495 f
= g_malloc0(sizeof(QEMUFile
));
503 int qemu_file_get_error(QEMUFile
*f
)
505 return f
->last_error
;
508 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
510 if (f
->last_error
== 0) {
515 /** Flushes QEMUFile buffer
518 static void qemu_fflush(QEMUFile
*f
)
522 if (!f
->ops
->put_buffer
) {
525 if (f
->is_write
&& f
->buf_index
> 0) {
526 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
528 f
->pos
+= f
->buf_index
;
533 qemu_file_set_error(f
, ret
);
537 static void qemu_fill_buffer(QEMUFile
*f
)
542 if (!f
->ops
->get_buffer
)
548 pending
= f
->buf_size
- f
->buf_index
;
550 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
553 f
->buf_size
= pending
;
555 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
556 IO_BUF_SIZE
- pending
);
560 } else if (len
== 0) {
561 qemu_file_set_error(f
, -EIO
);
562 } else if (len
!= -EAGAIN
)
563 qemu_file_set_error(f
, len
);
566 int qemu_get_fd(QEMUFile
*f
)
568 if (f
->ops
->get_fd
) {
569 return f
->ops
->get_fd(f
->opaque
);
576 * Returns negative error value if any error happened on previous operations or
577 * while closing the file. Returns 0 or positive number on success.
579 * The meaning of return value on success depends on the specific backend
582 int qemu_fclose(QEMUFile
*f
)
586 ret
= qemu_file_get_error(f
);
589 int ret2
= f
->ops
->close(f
->opaque
);
594 /* If any error was spotted before closing, we should report it
595 * instead of the close() return value.
604 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
612 if (f
->is_write
== 0 && f
->buf_index
> 0) {
614 "Attempted to write to buffer while read buffer is not empty\n");
619 l
= IO_BUF_SIZE
- f
->buf_index
;
622 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
628 if (f
->buf_index
>= IO_BUF_SIZE
) {
630 if (qemu_file_get_error(f
)) {
637 void qemu_put_byte(QEMUFile
*f
, int v
)
643 if (f
->is_write
== 0 && f
->buf_index
> 0) {
645 "Attempted to write to buffer while read buffer is not empty\n");
649 f
->buf
[f
->buf_index
++] = v
;
651 if (f
->buf_index
>= IO_BUF_SIZE
) {
656 static void qemu_file_skip(QEMUFile
*f
, int size
)
658 if (f
->buf_index
+ size
<= f
->buf_size
) {
659 f
->buf_index
+= size
;
663 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
672 index
= f
->buf_index
+ offset
;
673 pending
= f
->buf_size
- index
;
674 if (pending
< size
) {
676 index
= f
->buf_index
+ offset
;
677 pending
= f
->buf_size
- index
;
683 if (size
> pending
) {
687 memcpy(buf
, f
->buf
+ index
, size
);
691 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
696 while (pending
> 0) {
699 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
703 qemu_file_skip(f
, res
);
711 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
713 int index
= f
->buf_index
+ offset
;
719 if (index
>= f
->buf_size
) {
721 index
= f
->buf_index
+ offset
;
722 if (index
>= f
->buf_size
) {
726 return f
->buf
[index
];
729 int qemu_get_byte(QEMUFile
*f
)
733 result
= qemu_peek_byte(f
, 0);
734 qemu_file_skip(f
, 1);
738 int64_t qemu_ftell(QEMUFile
*f
)
744 int qemu_file_rate_limit(QEMUFile
*f
)
746 if (qemu_file_get_error(f
)) {
749 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
755 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
757 return f
->xfer_limit
;
760 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
762 f
->xfer_limit
= limit
;
765 void qemu_file_reset_rate_limit(QEMUFile
*f
)
770 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
772 qemu_put_byte(f
, v
>> 8);
776 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
778 qemu_put_byte(f
, v
>> 24);
779 qemu_put_byte(f
, v
>> 16);
780 qemu_put_byte(f
, v
>> 8);
784 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
786 qemu_put_be32(f
, v
>> 32);
790 unsigned int qemu_get_be16(QEMUFile
*f
)
793 v
= qemu_get_byte(f
) << 8;
794 v
|= qemu_get_byte(f
);
798 unsigned int qemu_get_be32(QEMUFile
*f
)
801 v
= qemu_get_byte(f
) << 24;
802 v
|= qemu_get_byte(f
) << 16;
803 v
|= qemu_get_byte(f
) << 8;
804 v
|= qemu_get_byte(f
);
808 uint64_t qemu_get_be64(QEMUFile
*f
)
811 v
= (uint64_t)qemu_get_be32(f
) << 32;
812 v
|= qemu_get_be32(f
);
819 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
821 uint64_t expire_time
;
823 expire_time
= qemu_timer_expire_time_ns(ts
);
824 qemu_put_be64(f
, expire_time
);
827 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
829 uint64_t expire_time
;
831 expire_time
= qemu_get_be64(f
);
832 if (expire_time
!= -1) {
833 qemu_mod_timer_ns(ts
, expire_time
);
842 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
845 *v
= qemu_get_byte(f
);
849 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
852 qemu_put_byte(f
, *v
);
855 const VMStateInfo vmstate_info_bool
= {
863 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
870 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
876 const VMStateInfo vmstate_info_int8
= {
884 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
887 qemu_get_sbe16s(f
, v
);
891 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
894 qemu_put_sbe16s(f
, v
);
897 const VMStateInfo vmstate_info_int16
= {
905 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
908 qemu_get_sbe32s(f
, v
);
912 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
915 qemu_put_sbe32s(f
, v
);
918 const VMStateInfo vmstate_info_int32
= {
924 /* 32 bit int. See that the received value is the same than the one
927 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
931 qemu_get_sbe32s(f
, &v2
);
938 const VMStateInfo vmstate_info_int32_equal
= {
939 .name
= "int32 equal",
940 .get
= get_int32_equal
,
944 /* 32 bit int. See that the received value is the less or the same
945 than the one in the field */
947 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
951 qemu_get_sbe32s(f
, &new);
958 const VMStateInfo vmstate_info_int32_le
= {
959 .name
= "int32 equal",
966 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
969 qemu_get_sbe64s(f
, v
);
973 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
976 qemu_put_sbe64s(f
, v
);
979 const VMStateInfo vmstate_info_int64
= {
985 /* 8 bit unsigned int */
987 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
994 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1000 const VMStateInfo vmstate_info_uint8
= {
1006 /* 16 bit unsigned int */
1008 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1011 qemu_get_be16s(f
, v
);
1015 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1018 qemu_put_be16s(f
, v
);
1021 const VMStateInfo vmstate_info_uint16
= {
1027 /* 32 bit unsigned int */
1029 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1032 qemu_get_be32s(f
, v
);
1036 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1039 qemu_put_be32s(f
, v
);
1042 const VMStateInfo vmstate_info_uint32
= {
1048 /* 32 bit uint. See that the received value is the same than the one
1051 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1055 qemu_get_be32s(f
, &v2
);
1063 const VMStateInfo vmstate_info_uint32_equal
= {
1064 .name
= "uint32 equal",
1065 .get
= get_uint32_equal
,
1069 /* 64 bit unsigned int */
1071 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1074 qemu_get_be64s(f
, v
);
1078 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1081 qemu_put_be64s(f
, v
);
1084 const VMStateInfo vmstate_info_uint64
= {
1090 /* 64 bit unsigned int. See that the received value is the same than the one
1093 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1097 qemu_get_be64s(f
, &v2
);
1105 const VMStateInfo vmstate_info_uint64_equal
= {
1106 .name
= "int64 equal",
1107 .get
= get_uint64_equal
,
1111 /* 8 bit int. See that the received value is the same than the one
1114 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1118 qemu_get_8s(f
, &v2
);
1125 const VMStateInfo vmstate_info_uint8_equal
= {
1126 .name
= "uint8 equal",
1127 .get
= get_uint8_equal
,
1131 /* 16 bit unsigned int int. See that the received value is the same than the one
1134 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1138 qemu_get_be16s(f
, &v2
);
1145 const VMStateInfo vmstate_info_uint16_equal
= {
1146 .name
= "uint16 equal",
1147 .get
= get_uint16_equal
,
1151 /* floating point */
1153 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1157 *v
= make_float64(qemu_get_be64(f
));
1161 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1165 qemu_put_be64(f
, float64_val(*v
));
1168 const VMStateInfo vmstate_info_float64
= {
1176 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1179 qemu_get_timer(f
, v
);
1183 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1186 qemu_put_timer(f
, v
);
1189 const VMStateInfo vmstate_info_timer
= {
1195 /* uint8_t buffers */
1197 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1200 qemu_get_buffer(f
, v
, size
);
1204 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1207 qemu_put_buffer(f
, v
, size
);
1210 const VMStateInfo vmstate_info_buffer
= {
1216 /* unused buffers: space that was used for some fields that are
1217 not useful anymore */
1219 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1225 block_len
= MIN(sizeof(buf
), size
);
1227 qemu_get_buffer(f
, buf
, block_len
);
1232 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1234 static const uint8_t buf
[1024];
1238 block_len
= MIN(sizeof(buf
), size
);
1240 qemu_put_buffer(f
, buf
, block_len
);
1244 const VMStateInfo vmstate_info_unused_buffer
= {
1245 .name
= "unused_buffer",
1246 .get
= get_unused_buffer
,
1247 .put
= put_unused_buffer
,
1250 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1251 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1252 * bit words with the bits in big endian order. The in-memory format
1253 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1255 /* This is the number of 64 bit words sent over the wire */
1256 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1257 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1259 unsigned long *bmp
= pv
;
1261 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1262 uint64_t w
= qemu_get_be64(f
);
1264 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1265 bmp
[idx
++] = w
>> 32;
1271 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1273 unsigned long *bmp
= pv
;
1275 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1276 uint64_t w
= bmp
[idx
++];
1277 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1278 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1280 qemu_put_be64(f
, w
);
1284 const VMStateInfo vmstate_info_bitmap
= {
1290 typedef struct CompatEntry
{
1295 typedef struct SaveStateEntry
{
1296 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1302 SaveVMHandlers
*ops
;
1303 const VMStateDescription
*vmsd
;
1305 CompatEntry
*compat
;
1311 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1312 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1313 static int global_section_id
;
1315 static int calculate_new_instance_id(const char *idstr
)
1318 int instance_id
= 0;
1320 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1321 if (strcmp(idstr
, se
->idstr
) == 0
1322 && instance_id
<= se
->instance_id
) {
1323 instance_id
= se
->instance_id
+ 1;
1329 static int calculate_compat_instance_id(const char *idstr
)
1332 int instance_id
= 0;
1334 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1338 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1339 && instance_id
<= se
->compat
->instance_id
) {
1340 instance_id
= se
->compat
->instance_id
+ 1;
1346 /* TODO: Individual devices generally have very little idea about the rest
1347 of the system, so instance_id should be removed/replaced.
1348 Meanwhile pass -1 as instance_id if you do not already have a clearly
1349 distinguishing id for all instances of your device class. */
1350 int register_savevm_live(DeviceState
*dev
,
1354 SaveVMHandlers
*ops
,
1359 se
= g_malloc0(sizeof(SaveStateEntry
));
1360 se
->version_id
= version_id
;
1361 se
->section_id
= global_section_id
++;
1363 se
->opaque
= opaque
;
1366 /* if this is a live_savem then set is_ram */
1367 if (ops
->save_live_setup
!= NULL
) {
1372 char *id
= qdev_get_dev_path(dev
);
1374 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1375 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1378 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1379 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1380 se
->compat
->instance_id
= instance_id
== -1 ?
1381 calculate_compat_instance_id(idstr
) : instance_id
;
1385 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1387 if (instance_id
== -1) {
1388 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1390 se
->instance_id
= instance_id
;
1392 assert(!se
->compat
|| se
->instance_id
== 0);
1393 /* add at the end of list */
1394 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1398 int register_savevm(DeviceState
*dev
,
1402 SaveStateHandler
*save_state
,
1403 LoadStateHandler
*load_state
,
1406 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1407 ops
->save_state
= save_state
;
1408 ops
->load_state
= load_state
;
1409 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1413 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1415 SaveStateEntry
*se
, *new_se
;
1419 char *path
= qdev_get_dev_path(dev
);
1421 pstrcpy(id
, sizeof(id
), path
);
1422 pstrcat(id
, sizeof(id
), "/");
1426 pstrcat(id
, sizeof(id
), idstr
);
1428 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1429 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1430 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1440 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1441 const VMStateDescription
*vmsd
,
1442 void *opaque
, int alias_id
,
1443 int required_for_version
)
1447 /* If this triggers, alias support can be dropped for the vmsd. */
1448 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1450 se
= g_malloc0(sizeof(SaveStateEntry
));
1451 se
->version_id
= vmsd
->version_id
;
1452 se
->section_id
= global_section_id
++;
1453 se
->opaque
= opaque
;
1455 se
->alias_id
= alias_id
;
1456 se
->no_migrate
= vmsd
->unmigratable
;
1459 char *id
= qdev_get_dev_path(dev
);
1461 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1462 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1465 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1466 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1467 se
->compat
->instance_id
= instance_id
== -1 ?
1468 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1472 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1474 if (instance_id
== -1) {
1475 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1477 se
->instance_id
= instance_id
;
1479 assert(!se
->compat
|| se
->instance_id
== 0);
1480 /* add at the end of list */
1481 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1485 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1488 SaveStateEntry
*se
, *new_se
;
1490 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1491 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1492 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1501 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1503 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1506 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1507 void *opaque
, int version_id
)
1509 VMStateField
*field
= vmsd
->fields
;
1512 if (version_id
> vmsd
->version_id
) {
1515 if (version_id
< vmsd
->minimum_version_id_old
) {
1518 if (version_id
< vmsd
->minimum_version_id
) {
1519 return vmsd
->load_state_old(f
, opaque
, version_id
);
1521 if (vmsd
->pre_load
) {
1522 int ret
= vmsd
->pre_load(opaque
);
1526 while(field
->name
) {
1527 if ((field
->field_exists
&&
1528 field
->field_exists(opaque
, version_id
)) ||
1529 (!field
->field_exists
&&
1530 field
->version_id
<= version_id
)) {
1531 void *base_addr
= opaque
+ field
->offset
;
1533 int size
= field
->size
;
1535 if (field
->flags
& VMS_VBUFFER
) {
1536 size
= *(int32_t *)(opaque
+field
->size_offset
);
1537 if (field
->flags
& VMS_MULTIPLY
) {
1538 size
*= field
->size
;
1541 if (field
->flags
& VMS_ARRAY
) {
1542 n_elems
= field
->num
;
1543 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1544 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1545 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1546 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1547 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1548 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1549 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1550 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1552 if (field
->flags
& VMS_POINTER
) {
1553 base_addr
= *(void **)base_addr
+ field
->start
;
1555 for (i
= 0; i
< n_elems
; i
++) {
1556 void *addr
= base_addr
+ size
* i
;
1558 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1559 addr
= *(void **)addr
;
1561 if (field
->flags
& VMS_STRUCT
) {
1562 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1564 ret
= field
->info
->get(f
, addr
, size
);
1574 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1578 if (vmsd
->post_load
) {
1579 return vmsd
->post_load(opaque
, version_id
);
1584 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1587 VMStateField
*field
= vmsd
->fields
;
1589 if (vmsd
->pre_save
) {
1590 vmsd
->pre_save(opaque
);
1592 while(field
->name
) {
1593 if (!field
->field_exists
||
1594 field
->field_exists(opaque
, vmsd
->version_id
)) {
1595 void *base_addr
= opaque
+ field
->offset
;
1597 int size
= field
->size
;
1599 if (field
->flags
& VMS_VBUFFER
) {
1600 size
= *(int32_t *)(opaque
+field
->size_offset
);
1601 if (field
->flags
& VMS_MULTIPLY
) {
1602 size
*= field
->size
;
1605 if (field
->flags
& VMS_ARRAY
) {
1606 n_elems
= field
->num
;
1607 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1608 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1609 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1610 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1611 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1612 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1613 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1614 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1616 if (field
->flags
& VMS_POINTER
) {
1617 base_addr
= *(void **)base_addr
+ field
->start
;
1619 for (i
= 0; i
< n_elems
; i
++) {
1620 void *addr
= base_addr
+ size
* i
;
1622 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1623 addr
= *(void **)addr
;
1625 if (field
->flags
& VMS_STRUCT
) {
1626 vmstate_save_state(f
, field
->vmsd
, addr
);
1628 field
->info
->put(f
, addr
, size
);
1634 vmstate_subsection_save(f
, vmsd
, opaque
);
1637 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1639 if (!se
->vmsd
) { /* Old style */
1640 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1642 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1645 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1647 if (!se
->vmsd
) { /* Old style */
1648 se
->ops
->save_state(f
, se
->opaque
);
1651 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1654 #define QEMU_VM_FILE_MAGIC 0x5145564d
1655 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1656 #define QEMU_VM_FILE_VERSION 0x00000003
1658 #define QEMU_VM_EOF 0x00
1659 #define QEMU_VM_SECTION_START 0x01
1660 #define QEMU_VM_SECTION_PART 0x02
1661 #define QEMU_VM_SECTION_END 0x03
1662 #define QEMU_VM_SECTION_FULL 0x04
1663 #define QEMU_VM_SUBSECTION 0x05
1665 bool qemu_savevm_state_blocked(Error
**errp
)
1669 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1670 if (se
->no_migrate
) {
1671 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1678 void qemu_savevm_state_begin(QEMUFile
*f
,
1679 const MigrationParams
*params
)
1684 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1685 if (!se
->ops
|| !se
->ops
->set_params
) {
1688 se
->ops
->set_params(params
, se
->opaque
);
1691 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1692 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1694 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1697 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1700 if (se
->ops
&& se
->ops
->is_active
) {
1701 if (!se
->ops
->is_active(se
->opaque
)) {
1706 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1707 qemu_put_be32(f
, se
->section_id
);
1710 len
= strlen(se
->idstr
);
1711 qemu_put_byte(f
, len
);
1712 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1714 qemu_put_be32(f
, se
->instance_id
);
1715 qemu_put_be32(f
, se
->version_id
);
1717 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1719 qemu_file_set_error(f
, ret
);
1726 * this function has three return values:
1727 * negative: there was one error, and we have -errno.
1728 * 0 : We haven't finished, caller have to go again
1729 * 1 : We have finished, we can go to complete phase
1731 int qemu_savevm_state_iterate(QEMUFile
*f
)
1736 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1737 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1740 if (se
->ops
&& se
->ops
->is_active
) {
1741 if (!se
->ops
->is_active(se
->opaque
)) {
1745 if (qemu_file_rate_limit(f
)) {
1748 trace_savevm_section_start();
1750 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1751 qemu_put_be32(f
, se
->section_id
);
1753 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1754 trace_savevm_section_end(se
->section_id
);
1757 qemu_file_set_error(f
, ret
);
1760 /* Do not proceed to the next vmstate before this one reported
1761 completion of the current stage. This serializes the migration
1762 and reduces the probability that a faster changing state is
1763 synchronized over and over again. */
1770 void qemu_savevm_state_complete(QEMUFile
*f
)
1775 cpu_synchronize_all_states();
1777 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1778 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1781 if (se
->ops
&& se
->ops
->is_active
) {
1782 if (!se
->ops
->is_active(se
->opaque
)) {
1786 trace_savevm_section_start();
1788 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1789 qemu_put_be32(f
, se
->section_id
);
1791 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1792 trace_savevm_section_end(se
->section_id
);
1794 qemu_file_set_error(f
, ret
);
1799 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1802 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1805 trace_savevm_section_start();
1807 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1808 qemu_put_be32(f
, se
->section_id
);
1811 len
= strlen(se
->idstr
);
1812 qemu_put_byte(f
, len
);
1813 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1815 qemu_put_be32(f
, se
->instance_id
);
1816 qemu_put_be32(f
, se
->version_id
);
1818 vmstate_save(f
, se
);
1819 trace_savevm_section_end(se
->section_id
);
1822 qemu_put_byte(f
, QEMU_VM_EOF
);
1826 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1831 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1832 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1835 if (se
->ops
&& se
->ops
->is_active
) {
1836 if (!se
->ops
->is_active(se
->opaque
)) {
1840 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1845 void qemu_savevm_state_cancel(void)
1849 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1850 if (se
->ops
&& se
->ops
->cancel
) {
1851 se
->ops
->cancel(se
->opaque
);
1856 static int qemu_savevm_state(QEMUFile
*f
)
1859 MigrationParams params
= {
1864 if (qemu_savevm_state_blocked(NULL
)) {
1868 qemu_mutex_unlock_iothread();
1869 qemu_savevm_state_begin(f
, ¶ms
);
1870 qemu_mutex_lock_iothread();
1872 while (qemu_file_get_error(f
) == 0) {
1873 if (qemu_savevm_state_iterate(f
) > 0) {
1878 ret
= qemu_file_get_error(f
);
1880 qemu_savevm_state_complete(f
);
1881 ret
= qemu_file_get_error(f
);
1884 qemu_savevm_state_cancel();
1889 static int qemu_save_device_state(QEMUFile
*f
)
1893 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1894 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1896 cpu_synchronize_all_states();
1898 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1904 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1909 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1910 qemu_put_be32(f
, se
->section_id
);
1913 len
= strlen(se
->idstr
);
1914 qemu_put_byte(f
, len
);
1915 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1917 qemu_put_be32(f
, se
->instance_id
);
1918 qemu_put_be32(f
, se
->version_id
);
1920 vmstate_save(f
, se
);
1923 qemu_put_byte(f
, QEMU_VM_EOF
);
1925 return qemu_file_get_error(f
);
1928 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1932 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1933 if (!strcmp(se
->idstr
, idstr
) &&
1934 (instance_id
== se
->instance_id
||
1935 instance_id
== se
->alias_id
))
1937 /* Migrating from an older version? */
1938 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1939 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1940 (instance_id
== se
->compat
->instance_id
||
1941 instance_id
== se
->alias_id
))
1948 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
1950 while(sub
&& sub
->needed
) {
1951 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
1959 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1962 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
1965 uint8_t version_id
, len
, size
;
1966 const VMStateDescription
*sub_vmsd
;
1968 len
= qemu_peek_byte(f
, 1);
1969 if (len
< strlen(vmsd
->name
) + 1) {
1970 /* subsection name has be be "section_name/a" */
1973 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
1979 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
1980 /* it don't have a valid subsection name */
1983 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
1984 if (sub_vmsd
== NULL
) {
1987 qemu_file_skip(f
, 1); /* subsection */
1988 qemu_file_skip(f
, 1); /* len */
1989 qemu_file_skip(f
, len
); /* idstr */
1990 version_id
= qemu_get_be32(f
);
1992 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2000 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2003 const VMStateSubsection
*sub
= vmsd
->subsections
;
2005 while (sub
&& sub
->needed
) {
2006 if (sub
->needed(opaque
)) {
2007 const VMStateDescription
*vmsd
= sub
->vmsd
;
2010 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2011 len
= strlen(vmsd
->name
);
2012 qemu_put_byte(f
, len
);
2013 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2014 qemu_put_be32(f
, vmsd
->version_id
);
2015 vmstate_save_state(f
, vmsd
, opaque
);
2021 typedef struct LoadStateEntry
{
2022 QLIST_ENTRY(LoadStateEntry
) entry
;
2028 int qemu_loadvm_state(QEMUFile
*f
)
2030 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2031 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2032 LoadStateEntry
*le
, *new_le
;
2033 uint8_t section_type
;
2037 if (qemu_savevm_state_blocked(NULL
)) {
2041 v
= qemu_get_be32(f
);
2042 if (v
!= QEMU_VM_FILE_MAGIC
)
2045 v
= qemu_get_be32(f
);
2046 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2047 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2050 if (v
!= QEMU_VM_FILE_VERSION
)
2053 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2054 uint32_t instance_id
, version_id
, section_id
;
2059 switch (section_type
) {
2060 case QEMU_VM_SECTION_START
:
2061 case QEMU_VM_SECTION_FULL
:
2062 /* Read section start */
2063 section_id
= qemu_get_be32(f
);
2064 len
= qemu_get_byte(f
);
2065 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2067 instance_id
= qemu_get_be32(f
);
2068 version_id
= qemu_get_be32(f
);
2070 /* Find savevm section */
2071 se
= find_se(idstr
, instance_id
);
2073 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2078 /* Validate version */
2079 if (version_id
> se
->version_id
) {
2080 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2081 version_id
, idstr
, se
->version_id
);
2087 le
= g_malloc0(sizeof(*le
));
2090 le
->section_id
= section_id
;
2091 le
->version_id
= version_id
;
2092 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2094 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2096 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2097 instance_id
, idstr
);
2101 case QEMU_VM_SECTION_PART
:
2102 case QEMU_VM_SECTION_END
:
2103 section_id
= qemu_get_be32(f
);
2105 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2106 if (le
->section_id
== section_id
) {
2111 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2116 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2118 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2124 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2130 cpu_synchronize_all_post_init();
2135 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2136 QLIST_REMOVE(le
, entry
);
2141 ret
= qemu_file_get_error(f
);
2147 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2150 QEMUSnapshotInfo
*sn_tab
, *sn
;
2154 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2157 for(i
= 0; i
< nb_sns
; i
++) {
2159 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2170 * Deletes snapshots of a given name in all opened images.
2172 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2174 BlockDriverState
*bs
;
2175 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2179 while ((bs
= bdrv_next(bs
))) {
2180 if (bdrv_can_snapshot(bs
) &&
2181 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2183 ret
= bdrv_snapshot_delete(bs
, name
);
2186 "Error while deleting snapshot on '%s'\n",
2187 bdrv_get_device_name(bs
));
2196 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2198 BlockDriverState
*bs
, *bs1
;
2199 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2202 int saved_vm_running
;
2203 uint64_t vm_state_size
;
2206 const char *name
= qdict_get_try_str(qdict
, "name");
2208 /* Verify if there is a device that doesn't support snapshots and is writable */
2210 while ((bs
= bdrv_next(bs
))) {
2212 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2216 if (!bdrv_can_snapshot(bs
)) {
2217 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2218 bdrv_get_device_name(bs
));
2223 bs
= bdrv_snapshots();
2225 monitor_printf(mon
, "No block device can accept snapshots\n");
2229 saved_vm_running
= runstate_is_running();
2230 vm_stop(RUN_STATE_SAVE_VM
);
2232 memset(sn
, 0, sizeof(*sn
));
2234 /* fill auxiliary fields */
2235 qemu_gettimeofday(&tv
);
2236 sn
->date_sec
= tv
.tv_sec
;
2237 sn
->date_nsec
= tv
.tv_usec
* 1000;
2238 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2241 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2243 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2244 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2246 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2249 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2250 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2251 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2254 /* Delete old snapshots of the same name */
2255 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2259 /* save the VM state */
2260 f
= qemu_fopen_bdrv(bs
, 1);
2262 monitor_printf(mon
, "Could not open VM state file\n");
2265 ret
= qemu_savevm_state(f
);
2266 vm_state_size
= qemu_ftell(f
);
2269 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2273 /* create the snapshots */
2276 while ((bs1
= bdrv_next(bs1
))) {
2277 if (bdrv_can_snapshot(bs1
)) {
2278 /* Write VM state size only to the image that contains the state */
2279 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2280 ret
= bdrv_snapshot_create(bs1
, sn
);
2282 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2283 bdrv_get_device_name(bs1
));
2289 if (saved_vm_running
)
2293 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2296 int saved_vm_running
;
2299 saved_vm_running
= runstate_is_running();
2300 vm_stop(RUN_STATE_SAVE_VM
);
2302 f
= qemu_fopen(filename
, "wb");
2304 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2307 ret
= qemu_save_device_state(f
);
2310 error_set(errp
, QERR_IO_ERROR
);
2314 if (saved_vm_running
)
2318 int load_vmstate(const char *name
)
2320 BlockDriverState
*bs
, *bs_vm_state
;
2321 QEMUSnapshotInfo sn
;
2325 bs_vm_state
= bdrv_snapshots();
2327 error_report("No block device supports snapshots");
2331 /* Don't even try to load empty VM states */
2332 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2335 } else if (sn
.vm_state_size
== 0) {
2336 error_report("This is a disk-only snapshot. Revert to it offline "
2341 /* Verify if there is any device that doesn't support snapshots and is
2342 writable and check if the requested snapshot is available too. */
2344 while ((bs
= bdrv_next(bs
))) {
2346 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2350 if (!bdrv_can_snapshot(bs
)) {
2351 error_report("Device '%s' is writable but does not support snapshots.",
2352 bdrv_get_device_name(bs
));
2356 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2358 error_report("Device '%s' does not have the requested snapshot '%s'",
2359 bdrv_get_device_name(bs
), name
);
2364 /* Flush all IO requests so they don't interfere with the new state. */
2368 while ((bs
= bdrv_next(bs
))) {
2369 if (bdrv_can_snapshot(bs
)) {
2370 ret
= bdrv_snapshot_goto(bs
, name
);
2372 error_report("Error %d while activating snapshot '%s' on '%s'",
2373 ret
, name
, bdrv_get_device_name(bs
));
2379 /* restore the VM state */
2380 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2382 error_report("Could not open VM state file");
2386 qemu_system_reset(VMRESET_SILENT
);
2387 ret
= qemu_loadvm_state(f
);
2391 error_report("Error %d while loading VM state", ret
);
2398 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2400 BlockDriverState
*bs
, *bs1
;
2402 const char *name
= qdict_get_str(qdict
, "name");
2404 bs
= bdrv_snapshots();
2406 monitor_printf(mon
, "No block device supports snapshots\n");
2411 while ((bs1
= bdrv_next(bs1
))) {
2412 if (bdrv_can_snapshot(bs1
)) {
2413 ret
= bdrv_snapshot_delete(bs1
, name
);
2415 if (ret
== -ENOTSUP
)
2417 "Snapshots not supported on device '%s'\n",
2418 bdrv_get_device_name(bs1
));
2420 monitor_printf(mon
, "Error %d while deleting snapshot on "
2421 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2427 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2429 BlockDriverState
*bs
, *bs1
;
2430 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2431 int nb_sns
, i
, ret
, available
;
2433 int *available_snapshots
;
2436 bs
= bdrv_snapshots();
2438 monitor_printf(mon
, "No available block device supports snapshots\n");
2442 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2444 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2449 monitor_printf(mon
, "There is no snapshot available.\n");
2453 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2455 for (i
= 0; i
< nb_sns
; i
++) {
2460 while ((bs1
= bdrv_next(bs1
))) {
2461 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2462 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2471 available_snapshots
[total
] = i
;
2477 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2478 for (i
= 0; i
< total
; i
++) {
2479 sn
= &sn_tab
[available_snapshots
[i
]];
2480 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2483 monitor_printf(mon
, "There is no suitable snapshot available\n");
2487 g_free(available_snapshots
);
2491 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2493 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2494 memory_region_name(mr
), dev
);
2497 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2499 /* Nothing do to while the implementation is in RAMBlock */
2502 void vmstate_register_ram_global(MemoryRegion
*mr
)
2504 vmstate_register_ram(mr
, NULL
);