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
;
653 if (f
->buf_index
>= IO_BUF_SIZE
) {
658 static void qemu_file_skip(QEMUFile
*f
, int size
)
660 if (f
->buf_index
+ size
<= f
->buf_size
) {
661 f
->buf_index
+= size
;
665 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
674 index
= f
->buf_index
+ offset
;
675 pending
= f
->buf_size
- index
;
676 if (pending
< size
) {
678 index
= f
->buf_index
+ offset
;
679 pending
= f
->buf_size
- index
;
685 if (size
> pending
) {
689 memcpy(buf
, f
->buf
+ index
, size
);
693 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
698 while (pending
> 0) {
701 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
705 qemu_file_skip(f
, res
);
713 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
715 int index
= f
->buf_index
+ offset
;
721 if (index
>= f
->buf_size
) {
723 index
= f
->buf_index
+ offset
;
724 if (index
>= f
->buf_size
) {
728 return f
->buf
[index
];
731 int qemu_get_byte(QEMUFile
*f
)
735 result
= qemu_peek_byte(f
, 0);
736 qemu_file_skip(f
, 1);
740 int64_t qemu_ftell(QEMUFile
*f
)
746 int qemu_file_rate_limit(QEMUFile
*f
)
748 if (qemu_file_get_error(f
)) {
751 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
757 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
759 return f
->xfer_limit
;
762 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
764 f
->xfer_limit
= limit
;
767 void qemu_file_reset_rate_limit(QEMUFile
*f
)
772 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
774 qemu_put_byte(f
, v
>> 8);
778 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
780 qemu_put_byte(f
, v
>> 24);
781 qemu_put_byte(f
, v
>> 16);
782 qemu_put_byte(f
, v
>> 8);
786 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
788 qemu_put_be32(f
, v
>> 32);
792 unsigned int qemu_get_be16(QEMUFile
*f
)
795 v
= qemu_get_byte(f
) << 8;
796 v
|= qemu_get_byte(f
);
800 unsigned int qemu_get_be32(QEMUFile
*f
)
803 v
= qemu_get_byte(f
) << 24;
804 v
|= qemu_get_byte(f
) << 16;
805 v
|= qemu_get_byte(f
) << 8;
806 v
|= qemu_get_byte(f
);
810 uint64_t qemu_get_be64(QEMUFile
*f
)
813 v
= (uint64_t)qemu_get_be32(f
) << 32;
814 v
|= qemu_get_be32(f
);
821 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
823 uint64_t expire_time
;
825 expire_time
= qemu_timer_expire_time_ns(ts
);
826 qemu_put_be64(f
, expire_time
);
829 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
831 uint64_t expire_time
;
833 expire_time
= qemu_get_be64(f
);
834 if (expire_time
!= -1) {
835 qemu_mod_timer_ns(ts
, expire_time
);
844 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
847 *v
= qemu_get_byte(f
);
851 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
854 qemu_put_byte(f
, *v
);
857 const VMStateInfo vmstate_info_bool
= {
865 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
872 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
878 const VMStateInfo vmstate_info_int8
= {
886 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
889 qemu_get_sbe16s(f
, v
);
893 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
896 qemu_put_sbe16s(f
, v
);
899 const VMStateInfo vmstate_info_int16
= {
907 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
910 qemu_get_sbe32s(f
, v
);
914 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
917 qemu_put_sbe32s(f
, v
);
920 const VMStateInfo vmstate_info_int32
= {
926 /* 32 bit int. See that the received value is the same than the one
929 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
933 qemu_get_sbe32s(f
, &v2
);
940 const VMStateInfo vmstate_info_int32_equal
= {
941 .name
= "int32 equal",
942 .get
= get_int32_equal
,
946 /* 32 bit int. See that the received value is the less or the same
947 than the one in the field */
949 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
953 qemu_get_sbe32s(f
, &new);
960 const VMStateInfo vmstate_info_int32_le
= {
961 .name
= "int32 equal",
968 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
971 qemu_get_sbe64s(f
, v
);
975 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
978 qemu_put_sbe64s(f
, v
);
981 const VMStateInfo vmstate_info_int64
= {
987 /* 8 bit unsigned int */
989 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
996 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1002 const VMStateInfo vmstate_info_uint8
= {
1008 /* 16 bit unsigned int */
1010 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1013 qemu_get_be16s(f
, v
);
1017 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1020 qemu_put_be16s(f
, v
);
1023 const VMStateInfo vmstate_info_uint16
= {
1029 /* 32 bit unsigned int */
1031 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1034 qemu_get_be32s(f
, v
);
1038 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1041 qemu_put_be32s(f
, v
);
1044 const VMStateInfo vmstate_info_uint32
= {
1050 /* 32 bit uint. See that the received value is the same than the one
1053 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1057 qemu_get_be32s(f
, &v2
);
1065 const VMStateInfo vmstate_info_uint32_equal
= {
1066 .name
= "uint32 equal",
1067 .get
= get_uint32_equal
,
1071 /* 64 bit unsigned int */
1073 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1076 qemu_get_be64s(f
, v
);
1080 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1083 qemu_put_be64s(f
, v
);
1086 const VMStateInfo vmstate_info_uint64
= {
1092 /* 64 bit unsigned int. See that the received value is the same than the one
1095 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1099 qemu_get_be64s(f
, &v2
);
1107 const VMStateInfo vmstate_info_uint64_equal
= {
1108 .name
= "int64 equal",
1109 .get
= get_uint64_equal
,
1113 /* 8 bit int. See that the received value is the same than the one
1116 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1120 qemu_get_8s(f
, &v2
);
1127 const VMStateInfo vmstate_info_uint8_equal
= {
1128 .name
= "uint8 equal",
1129 .get
= get_uint8_equal
,
1133 /* 16 bit unsigned int int. See that the received value is the same than the one
1136 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1140 qemu_get_be16s(f
, &v2
);
1147 const VMStateInfo vmstate_info_uint16_equal
= {
1148 .name
= "uint16 equal",
1149 .get
= get_uint16_equal
,
1153 /* floating point */
1155 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1159 *v
= make_float64(qemu_get_be64(f
));
1163 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1167 qemu_put_be64(f
, float64_val(*v
));
1170 const VMStateInfo vmstate_info_float64
= {
1178 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1181 qemu_get_timer(f
, v
);
1185 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1188 qemu_put_timer(f
, v
);
1191 const VMStateInfo vmstate_info_timer
= {
1197 /* uint8_t buffers */
1199 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1202 qemu_get_buffer(f
, v
, size
);
1206 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1209 qemu_put_buffer(f
, v
, size
);
1212 const VMStateInfo vmstate_info_buffer
= {
1218 /* unused buffers: space that was used for some fields that are
1219 not useful anymore */
1221 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1227 block_len
= MIN(sizeof(buf
), size
);
1229 qemu_get_buffer(f
, buf
, block_len
);
1234 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1236 static const uint8_t buf
[1024];
1240 block_len
= MIN(sizeof(buf
), size
);
1242 qemu_put_buffer(f
, buf
, block_len
);
1246 const VMStateInfo vmstate_info_unused_buffer
= {
1247 .name
= "unused_buffer",
1248 .get
= get_unused_buffer
,
1249 .put
= put_unused_buffer
,
1252 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1253 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1254 * bit words with the bits in big endian order. The in-memory format
1255 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1257 /* This is the number of 64 bit words sent over the wire */
1258 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1259 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1261 unsigned long *bmp
= pv
;
1263 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1264 uint64_t w
= qemu_get_be64(f
);
1266 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1267 bmp
[idx
++] = w
>> 32;
1273 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1275 unsigned long *bmp
= pv
;
1277 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1278 uint64_t w
= bmp
[idx
++];
1279 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1280 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1282 qemu_put_be64(f
, w
);
1286 const VMStateInfo vmstate_info_bitmap
= {
1292 typedef struct CompatEntry
{
1297 typedef struct SaveStateEntry
{
1298 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1304 SaveVMHandlers
*ops
;
1305 const VMStateDescription
*vmsd
;
1307 CompatEntry
*compat
;
1313 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1314 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1315 static int global_section_id
;
1317 static int calculate_new_instance_id(const char *idstr
)
1320 int instance_id
= 0;
1322 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1323 if (strcmp(idstr
, se
->idstr
) == 0
1324 && instance_id
<= se
->instance_id
) {
1325 instance_id
= se
->instance_id
+ 1;
1331 static int calculate_compat_instance_id(const char *idstr
)
1334 int instance_id
= 0;
1336 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1340 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1341 && instance_id
<= se
->compat
->instance_id
) {
1342 instance_id
= se
->compat
->instance_id
+ 1;
1348 /* TODO: Individual devices generally have very little idea about the rest
1349 of the system, so instance_id should be removed/replaced.
1350 Meanwhile pass -1 as instance_id if you do not already have a clearly
1351 distinguishing id for all instances of your device class. */
1352 int register_savevm_live(DeviceState
*dev
,
1356 SaveVMHandlers
*ops
,
1361 se
= g_malloc0(sizeof(SaveStateEntry
));
1362 se
->version_id
= version_id
;
1363 se
->section_id
= global_section_id
++;
1365 se
->opaque
= opaque
;
1368 /* if this is a live_savem then set is_ram */
1369 if (ops
->save_live_setup
!= NULL
) {
1374 char *id
= qdev_get_dev_path(dev
);
1376 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1377 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1380 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1381 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1382 se
->compat
->instance_id
= instance_id
== -1 ?
1383 calculate_compat_instance_id(idstr
) : instance_id
;
1387 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1389 if (instance_id
== -1) {
1390 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1392 se
->instance_id
= instance_id
;
1394 assert(!se
->compat
|| se
->instance_id
== 0);
1395 /* add at the end of list */
1396 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1400 int register_savevm(DeviceState
*dev
,
1404 SaveStateHandler
*save_state
,
1405 LoadStateHandler
*load_state
,
1408 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1409 ops
->save_state
= save_state
;
1410 ops
->load_state
= load_state
;
1411 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1415 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1417 SaveStateEntry
*se
, *new_se
;
1421 char *path
= qdev_get_dev_path(dev
);
1423 pstrcpy(id
, sizeof(id
), path
);
1424 pstrcat(id
, sizeof(id
), "/");
1428 pstrcat(id
, sizeof(id
), idstr
);
1430 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1431 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1432 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1442 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1443 const VMStateDescription
*vmsd
,
1444 void *opaque
, int alias_id
,
1445 int required_for_version
)
1449 /* If this triggers, alias support can be dropped for the vmsd. */
1450 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1452 se
= g_malloc0(sizeof(SaveStateEntry
));
1453 se
->version_id
= vmsd
->version_id
;
1454 se
->section_id
= global_section_id
++;
1455 se
->opaque
= opaque
;
1457 se
->alias_id
= alias_id
;
1458 se
->no_migrate
= vmsd
->unmigratable
;
1461 char *id
= qdev_get_dev_path(dev
);
1463 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1464 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1467 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1468 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1469 se
->compat
->instance_id
= instance_id
== -1 ?
1470 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1474 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1476 if (instance_id
== -1) {
1477 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1479 se
->instance_id
= instance_id
;
1481 assert(!se
->compat
|| se
->instance_id
== 0);
1482 /* add at the end of list */
1483 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1487 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1490 SaveStateEntry
*se
, *new_se
;
1492 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1493 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1494 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1503 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1505 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1508 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1509 void *opaque
, int version_id
)
1511 VMStateField
*field
= vmsd
->fields
;
1514 if (version_id
> vmsd
->version_id
) {
1517 if (version_id
< vmsd
->minimum_version_id_old
) {
1520 if (version_id
< vmsd
->minimum_version_id
) {
1521 return vmsd
->load_state_old(f
, opaque
, version_id
);
1523 if (vmsd
->pre_load
) {
1524 int ret
= vmsd
->pre_load(opaque
);
1528 while(field
->name
) {
1529 if ((field
->field_exists
&&
1530 field
->field_exists(opaque
, version_id
)) ||
1531 (!field
->field_exists
&&
1532 field
->version_id
<= version_id
)) {
1533 void *base_addr
= opaque
+ field
->offset
;
1535 int size
= field
->size
;
1537 if (field
->flags
& VMS_VBUFFER
) {
1538 size
= *(int32_t *)(opaque
+field
->size_offset
);
1539 if (field
->flags
& VMS_MULTIPLY
) {
1540 size
*= field
->size
;
1543 if (field
->flags
& VMS_ARRAY
) {
1544 n_elems
= field
->num
;
1545 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1546 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1547 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1548 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1549 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1550 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1551 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1552 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1554 if (field
->flags
& VMS_POINTER
) {
1555 base_addr
= *(void **)base_addr
+ field
->start
;
1557 for (i
= 0; i
< n_elems
; i
++) {
1558 void *addr
= base_addr
+ size
* i
;
1560 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1561 addr
= *(void **)addr
;
1563 if (field
->flags
& VMS_STRUCT
) {
1564 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1566 ret
= field
->info
->get(f
, addr
, size
);
1576 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1580 if (vmsd
->post_load
) {
1581 return vmsd
->post_load(opaque
, version_id
);
1586 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1589 VMStateField
*field
= vmsd
->fields
;
1591 if (vmsd
->pre_save
) {
1592 vmsd
->pre_save(opaque
);
1594 while(field
->name
) {
1595 if (!field
->field_exists
||
1596 field
->field_exists(opaque
, vmsd
->version_id
)) {
1597 void *base_addr
= opaque
+ field
->offset
;
1599 int size
= field
->size
;
1601 if (field
->flags
& VMS_VBUFFER
) {
1602 size
= *(int32_t *)(opaque
+field
->size_offset
);
1603 if (field
->flags
& VMS_MULTIPLY
) {
1604 size
*= field
->size
;
1607 if (field
->flags
& VMS_ARRAY
) {
1608 n_elems
= field
->num
;
1609 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1610 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1611 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1612 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1613 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1614 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1615 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1616 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1618 if (field
->flags
& VMS_POINTER
) {
1619 base_addr
= *(void **)base_addr
+ field
->start
;
1621 for (i
= 0; i
< n_elems
; i
++) {
1622 void *addr
= base_addr
+ size
* i
;
1624 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1625 addr
= *(void **)addr
;
1627 if (field
->flags
& VMS_STRUCT
) {
1628 vmstate_save_state(f
, field
->vmsd
, addr
);
1630 field
->info
->put(f
, addr
, size
);
1636 vmstate_subsection_save(f
, vmsd
, opaque
);
1639 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1641 if (!se
->vmsd
) { /* Old style */
1642 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1644 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1647 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1649 if (!se
->vmsd
) { /* Old style */
1650 se
->ops
->save_state(f
, se
->opaque
);
1653 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1656 #define QEMU_VM_FILE_MAGIC 0x5145564d
1657 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1658 #define QEMU_VM_FILE_VERSION 0x00000003
1660 #define QEMU_VM_EOF 0x00
1661 #define QEMU_VM_SECTION_START 0x01
1662 #define QEMU_VM_SECTION_PART 0x02
1663 #define QEMU_VM_SECTION_END 0x03
1664 #define QEMU_VM_SECTION_FULL 0x04
1665 #define QEMU_VM_SUBSECTION 0x05
1667 bool qemu_savevm_state_blocked(Error
**errp
)
1671 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1672 if (se
->no_migrate
) {
1673 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1680 void qemu_savevm_state_begin(QEMUFile
*f
,
1681 const MigrationParams
*params
)
1686 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1687 if (!se
->ops
|| !se
->ops
->set_params
) {
1690 se
->ops
->set_params(params
, se
->opaque
);
1693 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1694 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1696 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1699 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1702 if (se
->ops
&& se
->ops
->is_active
) {
1703 if (!se
->ops
->is_active(se
->opaque
)) {
1708 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1709 qemu_put_be32(f
, se
->section_id
);
1712 len
= strlen(se
->idstr
);
1713 qemu_put_byte(f
, len
);
1714 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1716 qemu_put_be32(f
, se
->instance_id
);
1717 qemu_put_be32(f
, se
->version_id
);
1719 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1721 qemu_file_set_error(f
, ret
);
1728 * this function has three return values:
1729 * negative: there was one error, and we have -errno.
1730 * 0 : We haven't finished, caller have to go again
1731 * 1 : We have finished, we can go to complete phase
1733 int qemu_savevm_state_iterate(QEMUFile
*f
)
1738 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1739 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1742 if (se
->ops
&& se
->ops
->is_active
) {
1743 if (!se
->ops
->is_active(se
->opaque
)) {
1747 if (qemu_file_rate_limit(f
)) {
1750 trace_savevm_section_start();
1752 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1753 qemu_put_be32(f
, se
->section_id
);
1755 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1756 trace_savevm_section_end(se
->section_id
);
1759 qemu_file_set_error(f
, ret
);
1762 /* Do not proceed to the next vmstate before this one reported
1763 completion of the current stage. This serializes the migration
1764 and reduces the probability that a faster changing state is
1765 synchronized over and over again. */
1772 void qemu_savevm_state_complete(QEMUFile
*f
)
1777 cpu_synchronize_all_states();
1779 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1780 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1783 if (se
->ops
&& se
->ops
->is_active
) {
1784 if (!se
->ops
->is_active(se
->opaque
)) {
1788 trace_savevm_section_start();
1790 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1791 qemu_put_be32(f
, se
->section_id
);
1793 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1794 trace_savevm_section_end(se
->section_id
);
1796 qemu_file_set_error(f
, ret
);
1801 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1804 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1807 trace_savevm_section_start();
1809 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1810 qemu_put_be32(f
, se
->section_id
);
1813 len
= strlen(se
->idstr
);
1814 qemu_put_byte(f
, len
);
1815 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1817 qemu_put_be32(f
, se
->instance_id
);
1818 qemu_put_be32(f
, se
->version_id
);
1820 vmstate_save(f
, se
);
1821 trace_savevm_section_end(se
->section_id
);
1824 qemu_put_byte(f
, QEMU_VM_EOF
);
1828 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1833 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1834 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1837 if (se
->ops
&& se
->ops
->is_active
) {
1838 if (!se
->ops
->is_active(se
->opaque
)) {
1842 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1847 void qemu_savevm_state_cancel(void)
1851 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1852 if (se
->ops
&& se
->ops
->cancel
) {
1853 se
->ops
->cancel(se
->opaque
);
1858 static int qemu_savevm_state(QEMUFile
*f
)
1861 MigrationParams params
= {
1866 if (qemu_savevm_state_blocked(NULL
)) {
1870 qemu_mutex_unlock_iothread();
1871 qemu_savevm_state_begin(f
, ¶ms
);
1872 qemu_mutex_lock_iothread();
1874 while (qemu_file_get_error(f
) == 0) {
1875 if (qemu_savevm_state_iterate(f
) > 0) {
1880 ret
= qemu_file_get_error(f
);
1882 qemu_savevm_state_complete(f
);
1883 ret
= qemu_file_get_error(f
);
1886 qemu_savevm_state_cancel();
1891 static int qemu_save_device_state(QEMUFile
*f
)
1895 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1896 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1898 cpu_synchronize_all_states();
1900 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1906 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1911 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1912 qemu_put_be32(f
, se
->section_id
);
1915 len
= strlen(se
->idstr
);
1916 qemu_put_byte(f
, len
);
1917 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1919 qemu_put_be32(f
, se
->instance_id
);
1920 qemu_put_be32(f
, se
->version_id
);
1922 vmstate_save(f
, se
);
1925 qemu_put_byte(f
, QEMU_VM_EOF
);
1927 return qemu_file_get_error(f
);
1930 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1934 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1935 if (!strcmp(se
->idstr
, idstr
) &&
1936 (instance_id
== se
->instance_id
||
1937 instance_id
== se
->alias_id
))
1939 /* Migrating from an older version? */
1940 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1941 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1942 (instance_id
== se
->compat
->instance_id
||
1943 instance_id
== se
->alias_id
))
1950 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
1952 while(sub
&& sub
->needed
) {
1953 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
1961 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1964 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
1967 uint8_t version_id
, len
, size
;
1968 const VMStateDescription
*sub_vmsd
;
1970 len
= qemu_peek_byte(f
, 1);
1971 if (len
< strlen(vmsd
->name
) + 1) {
1972 /* subsection name has be be "section_name/a" */
1975 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
1981 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
1982 /* it don't have a valid subsection name */
1985 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
1986 if (sub_vmsd
== NULL
) {
1989 qemu_file_skip(f
, 1); /* subsection */
1990 qemu_file_skip(f
, 1); /* len */
1991 qemu_file_skip(f
, len
); /* idstr */
1992 version_id
= qemu_get_be32(f
);
1994 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2002 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2005 const VMStateSubsection
*sub
= vmsd
->subsections
;
2007 while (sub
&& sub
->needed
) {
2008 if (sub
->needed(opaque
)) {
2009 const VMStateDescription
*vmsd
= sub
->vmsd
;
2012 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2013 len
= strlen(vmsd
->name
);
2014 qemu_put_byte(f
, len
);
2015 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2016 qemu_put_be32(f
, vmsd
->version_id
);
2017 vmstate_save_state(f
, vmsd
, opaque
);
2023 typedef struct LoadStateEntry
{
2024 QLIST_ENTRY(LoadStateEntry
) entry
;
2030 int qemu_loadvm_state(QEMUFile
*f
)
2032 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2033 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2034 LoadStateEntry
*le
, *new_le
;
2035 uint8_t section_type
;
2039 if (qemu_savevm_state_blocked(NULL
)) {
2043 v
= qemu_get_be32(f
);
2044 if (v
!= QEMU_VM_FILE_MAGIC
)
2047 v
= qemu_get_be32(f
);
2048 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2049 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2052 if (v
!= QEMU_VM_FILE_VERSION
)
2055 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2056 uint32_t instance_id
, version_id
, section_id
;
2061 switch (section_type
) {
2062 case QEMU_VM_SECTION_START
:
2063 case QEMU_VM_SECTION_FULL
:
2064 /* Read section start */
2065 section_id
= qemu_get_be32(f
);
2066 len
= qemu_get_byte(f
);
2067 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2069 instance_id
= qemu_get_be32(f
);
2070 version_id
= qemu_get_be32(f
);
2072 /* Find savevm section */
2073 se
= find_se(idstr
, instance_id
);
2075 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2080 /* Validate version */
2081 if (version_id
> se
->version_id
) {
2082 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2083 version_id
, idstr
, se
->version_id
);
2089 le
= g_malloc0(sizeof(*le
));
2092 le
->section_id
= section_id
;
2093 le
->version_id
= version_id
;
2094 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2096 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2098 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2099 instance_id
, idstr
);
2103 case QEMU_VM_SECTION_PART
:
2104 case QEMU_VM_SECTION_END
:
2105 section_id
= qemu_get_be32(f
);
2107 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2108 if (le
->section_id
== section_id
) {
2113 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2118 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2120 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2126 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2132 cpu_synchronize_all_post_init();
2137 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2138 QLIST_REMOVE(le
, entry
);
2143 ret
= qemu_file_get_error(f
);
2149 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2152 QEMUSnapshotInfo
*sn_tab
, *sn
;
2156 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2159 for(i
= 0; i
< nb_sns
; i
++) {
2161 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2172 * Deletes snapshots of a given name in all opened images.
2174 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2176 BlockDriverState
*bs
;
2177 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2181 while ((bs
= bdrv_next(bs
))) {
2182 if (bdrv_can_snapshot(bs
) &&
2183 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2185 ret
= bdrv_snapshot_delete(bs
, name
);
2188 "Error while deleting snapshot on '%s'\n",
2189 bdrv_get_device_name(bs
));
2198 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2200 BlockDriverState
*bs
, *bs1
;
2201 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2204 int saved_vm_running
;
2205 uint64_t vm_state_size
;
2208 const char *name
= qdict_get_try_str(qdict
, "name");
2210 /* Verify if there is a device that doesn't support snapshots and is writable */
2212 while ((bs
= bdrv_next(bs
))) {
2214 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2218 if (!bdrv_can_snapshot(bs
)) {
2219 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2220 bdrv_get_device_name(bs
));
2225 bs
= bdrv_snapshots();
2227 monitor_printf(mon
, "No block device can accept snapshots\n");
2231 saved_vm_running
= runstate_is_running();
2232 vm_stop(RUN_STATE_SAVE_VM
);
2234 memset(sn
, 0, sizeof(*sn
));
2236 /* fill auxiliary fields */
2237 qemu_gettimeofday(&tv
);
2238 sn
->date_sec
= tv
.tv_sec
;
2239 sn
->date_nsec
= tv
.tv_usec
* 1000;
2240 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2243 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2245 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2246 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2248 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2251 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2252 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2253 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2256 /* Delete old snapshots of the same name */
2257 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2261 /* save the VM state */
2262 f
= qemu_fopen_bdrv(bs
, 1);
2264 monitor_printf(mon
, "Could not open VM state file\n");
2267 ret
= qemu_savevm_state(f
);
2268 vm_state_size
= qemu_ftell(f
);
2271 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2275 /* create the snapshots */
2278 while ((bs1
= bdrv_next(bs1
))) {
2279 if (bdrv_can_snapshot(bs1
)) {
2280 /* Write VM state size only to the image that contains the state */
2281 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2282 ret
= bdrv_snapshot_create(bs1
, sn
);
2284 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2285 bdrv_get_device_name(bs1
));
2291 if (saved_vm_running
)
2295 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2298 int saved_vm_running
;
2301 saved_vm_running
= runstate_is_running();
2302 vm_stop(RUN_STATE_SAVE_VM
);
2304 f
= qemu_fopen(filename
, "wb");
2306 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2309 ret
= qemu_save_device_state(f
);
2312 error_set(errp
, QERR_IO_ERROR
);
2316 if (saved_vm_running
)
2320 int load_vmstate(const char *name
)
2322 BlockDriverState
*bs
, *bs_vm_state
;
2323 QEMUSnapshotInfo sn
;
2327 bs_vm_state
= bdrv_snapshots();
2329 error_report("No block device supports snapshots");
2333 /* Don't even try to load empty VM states */
2334 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2337 } else if (sn
.vm_state_size
== 0) {
2338 error_report("This is a disk-only snapshot. Revert to it offline "
2343 /* Verify if there is any device that doesn't support snapshots and is
2344 writable and check if the requested snapshot is available too. */
2346 while ((bs
= bdrv_next(bs
))) {
2348 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2352 if (!bdrv_can_snapshot(bs
)) {
2353 error_report("Device '%s' is writable but does not support snapshots.",
2354 bdrv_get_device_name(bs
));
2358 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2360 error_report("Device '%s' does not have the requested snapshot '%s'",
2361 bdrv_get_device_name(bs
), name
);
2366 /* Flush all IO requests so they don't interfere with the new state. */
2370 while ((bs
= bdrv_next(bs
))) {
2371 if (bdrv_can_snapshot(bs
)) {
2372 ret
= bdrv_snapshot_goto(bs
, name
);
2374 error_report("Error %d while activating snapshot '%s' on '%s'",
2375 ret
, name
, bdrv_get_device_name(bs
));
2381 /* restore the VM state */
2382 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2384 error_report("Could not open VM state file");
2388 qemu_system_reset(VMRESET_SILENT
);
2389 ret
= qemu_loadvm_state(f
);
2393 error_report("Error %d while loading VM state", ret
);
2400 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2402 BlockDriverState
*bs
, *bs1
;
2404 const char *name
= qdict_get_str(qdict
, "name");
2406 bs
= bdrv_snapshots();
2408 monitor_printf(mon
, "No block device supports snapshots\n");
2413 while ((bs1
= bdrv_next(bs1
))) {
2414 if (bdrv_can_snapshot(bs1
)) {
2415 ret
= bdrv_snapshot_delete(bs1
, name
);
2417 if (ret
== -ENOTSUP
)
2419 "Snapshots not supported on device '%s'\n",
2420 bdrv_get_device_name(bs1
));
2422 monitor_printf(mon
, "Error %d while deleting snapshot on "
2423 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2429 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2431 BlockDriverState
*bs
, *bs1
;
2432 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2433 int nb_sns
, i
, ret
, available
;
2435 int *available_snapshots
;
2438 bs
= bdrv_snapshots();
2440 monitor_printf(mon
, "No available block device supports snapshots\n");
2444 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2446 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2451 monitor_printf(mon
, "There is no snapshot available.\n");
2455 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2457 for (i
= 0; i
< nb_sns
; i
++) {
2462 while ((bs1
= bdrv_next(bs1
))) {
2463 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2464 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2473 available_snapshots
[total
] = i
;
2479 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2480 for (i
= 0; i
< total
; i
++) {
2481 sn
= &sn_tab
[available_snapshots
[i
]];
2482 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2485 monitor_printf(mon
, "There is no suitable snapshot available\n");
2489 g_free(available_snapshots
);
2493 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2495 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2496 memory_region_name(mr
), dev
);
2499 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2501 /* Nothing do to while the implementation is in RAMBlock */
2504 void vmstate_register_ram_global(MemoryRegion
*mr
)
2506 vmstate_register_ram(mr
, NULL
);