4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
41 #include "qemu/bitops.h"
44 #define SELF_ANNOUNCE_ROUNDS 5
47 #define ETH_P_RARP 0x8035
49 #define ARP_HTYPE_ETH 0x0001
50 #define ARP_PTYPE_IP 0x0800
51 #define ARP_OP_REQUEST_REV 0x3
53 static int announce_self_create(uint8_t *buf
,
56 /* Ethernet header. */
57 memset(buf
, 0xff, 6); /* destination MAC addr */
58 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
59 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
62 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
63 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
64 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
65 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
67 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
68 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
70 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
72 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf
+ 42, 0x00, 18);
75 return 60; /* len (FCS will be added by hardware) */
78 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
83 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
85 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
89 static void qemu_announce_self_once(void *opaque
)
91 static int count
= SELF_ANNOUNCE_ROUNDS
;
92 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
94 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
97 /* delay 50ms, 150ms, 250ms, ... */
98 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
99 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
101 qemu_del_timer(timer
);
102 qemu_free_timer(timer
);
106 void qemu_announce_self(void)
108 static QEMUTimer
*timer
;
109 timer
= qemu_new_timer_ms(rt_clock
, qemu_announce_self_once
, &timer
);
110 qemu_announce_self_once(&timer
);
113 /***********************************************************/
114 /* savevm/loadvm support */
116 #define IO_BUF_SIZE 32768
117 #define MAX_IOV_SIZE MIN(IOV_MAX, 64)
120 const QEMUFileOps
*ops
;
127 int64_t pos
; /* start of buffer when writing, end of buffer
130 int buf_size
; /* 0 when writing */
131 uint8_t buf
[IO_BUF_SIZE
];
133 struct iovec iov
[MAX_IOV_SIZE
];
139 typedef struct QEMUFileStdio
145 typedef struct QEMUFileSocket
156 static void fd_coroutine_enter(void *opaque
)
158 FDYieldUntilData
*data
= opaque
;
159 qemu_set_fd_handler(data
->fd
, NULL
, NULL
, NULL
);
160 qemu_coroutine_enter(data
->co
, NULL
);
164 * Yield until a file descriptor becomes readable
166 * Note that this function clobbers the handlers for the file descriptor.
168 static void coroutine_fn
yield_until_fd_readable(int fd
)
170 FDYieldUntilData data
;
172 assert(qemu_in_coroutine());
173 data
.co
= qemu_coroutine_self();
175 qemu_set_fd_handler(fd
, fd_coroutine_enter
, NULL
, &data
);
176 qemu_coroutine_yield();
179 static ssize_t
socket_writev_buffer(void *opaque
, struct iovec
*iov
, int iovcnt
)
181 QEMUFileSocket
*s
= opaque
;
183 ssize_t size
= iov_size(iov
, iovcnt
);
185 len
= iov_send(s
->fd
, iov
, iovcnt
, 0, size
);
187 len
= -socket_error();
192 static int socket_get_fd(void *opaque
)
194 QEMUFileSocket
*s
= opaque
;
199 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
201 QEMUFileSocket
*s
= opaque
;
205 len
= qemu_recv(s
->fd
, buf
, size
, 0);
209 if (socket_error() == EAGAIN
) {
210 yield_until_fd_readable(s
->fd
);
211 } else if (socket_error() != EINTR
) {
217 len
= -socket_error();
222 static int socket_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
224 QEMUFileSocket
*s
= opaque
;
227 len
= qemu_send_full(s
->fd
, buf
, size
, 0);
229 len
= -socket_error();
234 static int socket_close(void *opaque
)
236 QEMUFileSocket
*s
= opaque
;
242 static int stdio_get_fd(void *opaque
)
244 QEMUFileStdio
*s
= opaque
;
246 return fileno(s
->stdio_file
);
249 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
251 QEMUFileStdio
*s
= opaque
;
252 return fwrite(buf
, 1, size
, s
->stdio_file
);
255 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
257 QEMUFileStdio
*s
= opaque
;
258 FILE *fp
= s
->stdio_file
;
263 bytes
= fread(buf
, 1, size
, fp
);
264 if (bytes
!= 0 || !ferror(fp
)) {
267 if (errno
== EAGAIN
) {
268 yield_until_fd_readable(fileno(fp
));
269 } else if (errno
!= EINTR
) {
276 static int stdio_pclose(void *opaque
)
278 QEMUFileStdio
*s
= opaque
;
280 ret
= pclose(s
->stdio_file
);
283 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
284 /* close succeeded, but non-zero exit code: */
285 ret
= -EIO
; /* fake errno value */
291 static int stdio_fclose(void *opaque
)
293 QEMUFileStdio
*s
= opaque
;
296 if (s
->file
->ops
->put_buffer
) {
297 int fd
= fileno(s
->stdio_file
);
300 ret
= fstat(fd
, &st
);
301 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
303 * If the file handle is a regular file make sure the
304 * data is flushed to disk before signaling success.
313 if (fclose(s
->stdio_file
) == EOF
) {
320 static const QEMUFileOps stdio_pipe_read_ops
= {
321 .get_fd
= stdio_get_fd
,
322 .get_buffer
= stdio_get_buffer
,
323 .close
= stdio_pclose
326 static const QEMUFileOps stdio_pipe_write_ops
= {
327 .get_fd
= stdio_get_fd
,
328 .put_buffer
= stdio_put_buffer
,
329 .close
= stdio_pclose
332 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
337 stdio_file
= popen(command
, mode
);
338 if (stdio_file
== NULL
) {
342 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
343 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
347 s
= g_malloc0(sizeof(QEMUFileStdio
));
349 s
->stdio_file
= stdio_file
;
352 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
354 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
359 static const QEMUFileOps stdio_file_read_ops
= {
360 .get_fd
= stdio_get_fd
,
361 .get_buffer
= stdio_get_buffer
,
362 .close
= stdio_fclose
365 static const QEMUFileOps stdio_file_write_ops
= {
366 .get_fd
= stdio_get_fd
,
367 .put_buffer
= stdio_put_buffer
,
368 .close
= stdio_fclose
371 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
376 (mode
[0] != 'r' && mode
[0] != 'w') ||
377 mode
[1] != 'b' || mode
[2] != 0) {
378 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
382 s
= g_malloc0(sizeof(QEMUFileStdio
));
383 s
->stdio_file
= fdopen(fd
, mode
);
388 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
390 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
399 static const QEMUFileOps socket_read_ops
= {
400 .get_fd
= socket_get_fd
,
401 .get_buffer
= socket_get_buffer
,
402 .close
= socket_close
405 static const QEMUFileOps socket_write_ops
= {
406 .get_fd
= socket_get_fd
,
407 .put_buffer
= socket_put_buffer
,
408 .writev_buffer
= socket_writev_buffer
,
409 .close
= socket_close
412 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
414 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
417 (mode
[0] != 'r' && mode
[0] != 'w') ||
418 mode
[1] != 'b' || mode
[2] != 0) {
419 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
424 if (mode
[0] == 'w') {
425 socket_set_block(s
->fd
);
426 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
428 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
433 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
438 (mode
[0] != 'r' && mode
[0] != 'w') ||
439 mode
[1] != 'b' || mode
[2] != 0) {
440 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
444 s
= g_malloc0(sizeof(QEMUFileStdio
));
446 s
->stdio_file
= fopen(filename
, mode
);
451 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
453 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
461 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
462 int64_t pos
, int size
)
464 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
468 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
470 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
473 static int bdrv_fclose(void *opaque
)
475 return bdrv_flush(opaque
);
478 static const QEMUFileOps bdrv_read_ops
= {
479 .get_buffer
= block_get_buffer
,
483 static const QEMUFileOps bdrv_write_ops
= {
484 .put_buffer
= block_put_buffer
,
488 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
491 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
492 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
495 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
499 f
= g_malloc0(sizeof(QEMUFile
));
507 int qemu_file_get_error(QEMUFile
*f
)
509 return f
->last_error
;
512 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
514 if (f
->last_error
== 0) {
519 /** Flushes QEMUFile buffer
522 static void qemu_fflush(QEMUFile
*f
)
526 if (!f
->ops
->put_buffer
) {
529 if (f
->is_write
&& f
->buf_index
> 0) {
530 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
532 f
->pos
+= f
->buf_index
;
538 qemu_file_set_error(f
, ret
);
542 static void qemu_fill_buffer(QEMUFile
*f
)
547 if (!f
->ops
->get_buffer
)
553 pending
= f
->buf_size
- f
->buf_index
;
555 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
558 f
->buf_size
= pending
;
560 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
561 IO_BUF_SIZE
- pending
);
565 } else if (len
== 0) {
566 qemu_file_set_error(f
, -EIO
);
567 } else if (len
!= -EAGAIN
)
568 qemu_file_set_error(f
, len
);
571 int qemu_get_fd(QEMUFile
*f
)
573 if (f
->ops
->get_fd
) {
574 return f
->ops
->get_fd(f
->opaque
);
581 * Returns negative error value if any error happened on previous operations or
582 * while closing the file. Returns 0 or positive number on success.
584 * The meaning of return value on success depends on the specific backend
587 int qemu_fclose(QEMUFile
*f
)
591 ret
= qemu_file_get_error(f
);
594 int ret2
= f
->ops
->close(f
->opaque
);
599 /* If any error was spotted before closing, we should report it
600 * instead of the close() return value.
609 static void add_to_iovec(QEMUFile
*f
, const uint8_t *buf
, int size
)
611 /* check for adjacent buffer and coalesce them */
612 if (f
->iovcnt
> 0 && buf
== f
->iov
[f
->iovcnt
- 1].iov_base
+
613 f
->iov
[f
->iovcnt
- 1].iov_len
) {
614 f
->iov
[f
->iovcnt
- 1].iov_len
+= size
;
616 f
->iov
[f
->iovcnt
].iov_base
= (uint8_t *)buf
;
617 f
->iov
[f
->iovcnt
++].iov_len
= size
;
621 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
629 if (f
->is_write
== 0 && f
->buf_index
> 0) {
631 "Attempted to write to buffer while read buffer is not empty\n");
636 l
= IO_BUF_SIZE
- f
->buf_index
;
639 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
640 add_to_iovec(f
, f
->buf
+ f
->buf_index
, l
);
646 if (f
->buf_index
>= IO_BUF_SIZE
|| f
->iovcnt
>= MAX_IOV_SIZE
) {
648 if (qemu_file_get_error(f
)) {
655 void qemu_put_byte(QEMUFile
*f
, int v
)
661 if (f
->is_write
== 0 && f
->buf_index
> 0) {
663 "Attempted to write to buffer while read buffer is not empty\n");
667 f
->buf
[f
->buf_index
++] = v
;
671 add_to_iovec(f
, f
->buf
+ (f
->buf_index
- 1), 1);
673 if (f
->buf_index
>= IO_BUF_SIZE
|| f
->iovcnt
>= MAX_IOV_SIZE
) {
678 static void qemu_file_skip(QEMUFile
*f
, int size
)
680 if (f
->buf_index
+ size
<= f
->buf_size
) {
681 f
->buf_index
+= size
;
685 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
694 index
= f
->buf_index
+ offset
;
695 pending
= f
->buf_size
- index
;
696 if (pending
< size
) {
698 index
= f
->buf_index
+ offset
;
699 pending
= f
->buf_size
- index
;
705 if (size
> pending
) {
709 memcpy(buf
, f
->buf
+ index
, size
);
713 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
718 while (pending
> 0) {
721 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
725 qemu_file_skip(f
, res
);
733 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
735 int index
= f
->buf_index
+ offset
;
741 if (index
>= f
->buf_size
) {
743 index
= f
->buf_index
+ offset
;
744 if (index
>= f
->buf_size
) {
748 return f
->buf
[index
];
751 int qemu_get_byte(QEMUFile
*f
)
755 result
= qemu_peek_byte(f
, 0);
756 qemu_file_skip(f
, 1);
760 int64_t qemu_ftell(QEMUFile
*f
)
766 int qemu_file_rate_limit(QEMUFile
*f
)
768 if (qemu_file_get_error(f
)) {
771 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
777 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
779 return f
->xfer_limit
;
782 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
784 f
->xfer_limit
= limit
;
787 void qemu_file_reset_rate_limit(QEMUFile
*f
)
792 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
794 qemu_put_byte(f
, v
>> 8);
798 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
800 qemu_put_byte(f
, v
>> 24);
801 qemu_put_byte(f
, v
>> 16);
802 qemu_put_byte(f
, v
>> 8);
806 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
808 qemu_put_be32(f
, v
>> 32);
812 unsigned int qemu_get_be16(QEMUFile
*f
)
815 v
= qemu_get_byte(f
) << 8;
816 v
|= qemu_get_byte(f
);
820 unsigned int qemu_get_be32(QEMUFile
*f
)
823 v
= qemu_get_byte(f
) << 24;
824 v
|= qemu_get_byte(f
) << 16;
825 v
|= qemu_get_byte(f
) << 8;
826 v
|= qemu_get_byte(f
);
830 uint64_t qemu_get_be64(QEMUFile
*f
)
833 v
= (uint64_t)qemu_get_be32(f
) << 32;
834 v
|= qemu_get_be32(f
);
841 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
843 uint64_t expire_time
;
845 expire_time
= qemu_timer_expire_time_ns(ts
);
846 qemu_put_be64(f
, expire_time
);
849 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
851 uint64_t expire_time
;
853 expire_time
= qemu_get_be64(f
);
854 if (expire_time
!= -1) {
855 qemu_mod_timer_ns(ts
, expire_time
);
864 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
867 *v
= qemu_get_byte(f
);
871 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
874 qemu_put_byte(f
, *v
);
877 const VMStateInfo vmstate_info_bool
= {
885 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
892 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
898 const VMStateInfo vmstate_info_int8
= {
906 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
909 qemu_get_sbe16s(f
, v
);
913 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
916 qemu_put_sbe16s(f
, v
);
919 const VMStateInfo vmstate_info_int16
= {
927 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
930 qemu_get_sbe32s(f
, v
);
934 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
937 qemu_put_sbe32s(f
, v
);
940 const VMStateInfo vmstate_info_int32
= {
946 /* 32 bit int. See that the received value is the same than the one
949 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
953 qemu_get_sbe32s(f
, &v2
);
960 const VMStateInfo vmstate_info_int32_equal
= {
961 .name
= "int32 equal",
962 .get
= get_int32_equal
,
966 /* 32 bit int. See that the received value is the less or the same
967 than the one in the field */
969 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
973 qemu_get_sbe32s(f
, &new);
980 const VMStateInfo vmstate_info_int32_le
= {
981 .name
= "int32 equal",
988 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
991 qemu_get_sbe64s(f
, v
);
995 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
998 qemu_put_sbe64s(f
, v
);
1001 const VMStateInfo vmstate_info_int64
= {
1007 /* 8 bit unsigned int */
1009 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1016 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
1022 const VMStateInfo vmstate_info_uint8
= {
1028 /* 16 bit unsigned int */
1030 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1033 qemu_get_be16s(f
, v
);
1037 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1040 qemu_put_be16s(f
, v
);
1043 const VMStateInfo vmstate_info_uint16
= {
1049 /* 32 bit unsigned int */
1051 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1054 qemu_get_be32s(f
, v
);
1058 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1061 qemu_put_be32s(f
, v
);
1064 const VMStateInfo vmstate_info_uint32
= {
1070 /* 32 bit uint. See that the received value is the same than the one
1073 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1077 qemu_get_be32s(f
, &v2
);
1085 const VMStateInfo vmstate_info_uint32_equal
= {
1086 .name
= "uint32 equal",
1087 .get
= get_uint32_equal
,
1091 /* 64 bit unsigned int */
1093 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1096 qemu_get_be64s(f
, v
);
1100 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1103 qemu_put_be64s(f
, v
);
1106 const VMStateInfo vmstate_info_uint64
= {
1112 /* 64 bit unsigned int. See that the received value is the same than the one
1115 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1119 qemu_get_be64s(f
, &v2
);
1127 const VMStateInfo vmstate_info_uint64_equal
= {
1128 .name
= "int64 equal",
1129 .get
= get_uint64_equal
,
1133 /* 8 bit int. See that the received value is the same than the one
1136 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1140 qemu_get_8s(f
, &v2
);
1147 const VMStateInfo vmstate_info_uint8_equal
= {
1148 .name
= "uint8 equal",
1149 .get
= get_uint8_equal
,
1153 /* 16 bit unsigned int int. See that the received value is the same than the one
1156 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1160 qemu_get_be16s(f
, &v2
);
1167 const VMStateInfo vmstate_info_uint16_equal
= {
1168 .name
= "uint16 equal",
1169 .get
= get_uint16_equal
,
1173 /* floating point */
1175 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1179 *v
= make_float64(qemu_get_be64(f
));
1183 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1187 qemu_put_be64(f
, float64_val(*v
));
1190 const VMStateInfo vmstate_info_float64
= {
1198 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1201 qemu_get_timer(f
, v
);
1205 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1208 qemu_put_timer(f
, v
);
1211 const VMStateInfo vmstate_info_timer
= {
1217 /* uint8_t buffers */
1219 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1222 qemu_get_buffer(f
, v
, size
);
1226 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1229 qemu_put_buffer(f
, v
, size
);
1232 const VMStateInfo vmstate_info_buffer
= {
1238 /* unused buffers: space that was used for some fields that are
1239 not useful anymore */
1241 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1247 block_len
= MIN(sizeof(buf
), size
);
1249 qemu_get_buffer(f
, buf
, block_len
);
1254 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1256 static const uint8_t buf
[1024];
1260 block_len
= MIN(sizeof(buf
), size
);
1262 qemu_put_buffer(f
, buf
, block_len
);
1266 const VMStateInfo vmstate_info_unused_buffer
= {
1267 .name
= "unused_buffer",
1268 .get
= get_unused_buffer
,
1269 .put
= put_unused_buffer
,
1272 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1273 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1274 * bit words with the bits in big endian order. The in-memory format
1275 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1277 /* This is the number of 64 bit words sent over the wire */
1278 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1279 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1281 unsigned long *bmp
= pv
;
1283 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1284 uint64_t w
= qemu_get_be64(f
);
1286 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1287 bmp
[idx
++] = w
>> 32;
1293 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1295 unsigned long *bmp
= pv
;
1297 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1298 uint64_t w
= bmp
[idx
++];
1299 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1300 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1302 qemu_put_be64(f
, w
);
1306 const VMStateInfo vmstate_info_bitmap
= {
1312 typedef struct CompatEntry
{
1317 typedef struct SaveStateEntry
{
1318 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1324 SaveVMHandlers
*ops
;
1325 const VMStateDescription
*vmsd
;
1327 CompatEntry
*compat
;
1333 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1334 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1335 static int global_section_id
;
1337 static int calculate_new_instance_id(const char *idstr
)
1340 int instance_id
= 0;
1342 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1343 if (strcmp(idstr
, se
->idstr
) == 0
1344 && instance_id
<= se
->instance_id
) {
1345 instance_id
= se
->instance_id
+ 1;
1351 static int calculate_compat_instance_id(const char *idstr
)
1354 int instance_id
= 0;
1356 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1360 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1361 && instance_id
<= se
->compat
->instance_id
) {
1362 instance_id
= se
->compat
->instance_id
+ 1;
1368 /* TODO: Individual devices generally have very little idea about the rest
1369 of the system, so instance_id should be removed/replaced.
1370 Meanwhile pass -1 as instance_id if you do not already have a clearly
1371 distinguishing id for all instances of your device class. */
1372 int register_savevm_live(DeviceState
*dev
,
1376 SaveVMHandlers
*ops
,
1381 se
= g_malloc0(sizeof(SaveStateEntry
));
1382 se
->version_id
= version_id
;
1383 se
->section_id
= global_section_id
++;
1385 se
->opaque
= opaque
;
1388 /* if this is a live_savem then set is_ram */
1389 if (ops
->save_live_setup
!= NULL
) {
1394 char *id
= qdev_get_dev_path(dev
);
1396 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1397 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1400 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1401 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1402 se
->compat
->instance_id
= instance_id
== -1 ?
1403 calculate_compat_instance_id(idstr
) : instance_id
;
1407 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1409 if (instance_id
== -1) {
1410 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1412 se
->instance_id
= instance_id
;
1414 assert(!se
->compat
|| se
->instance_id
== 0);
1415 /* add at the end of list */
1416 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1420 int register_savevm(DeviceState
*dev
,
1424 SaveStateHandler
*save_state
,
1425 LoadStateHandler
*load_state
,
1428 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1429 ops
->save_state
= save_state
;
1430 ops
->load_state
= load_state
;
1431 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1435 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1437 SaveStateEntry
*se
, *new_se
;
1441 char *path
= qdev_get_dev_path(dev
);
1443 pstrcpy(id
, sizeof(id
), path
);
1444 pstrcat(id
, sizeof(id
), "/");
1448 pstrcat(id
, sizeof(id
), idstr
);
1450 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1451 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1452 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1462 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1463 const VMStateDescription
*vmsd
,
1464 void *opaque
, int alias_id
,
1465 int required_for_version
)
1469 /* If this triggers, alias support can be dropped for the vmsd. */
1470 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1472 se
= g_malloc0(sizeof(SaveStateEntry
));
1473 se
->version_id
= vmsd
->version_id
;
1474 se
->section_id
= global_section_id
++;
1475 se
->opaque
= opaque
;
1477 se
->alias_id
= alias_id
;
1478 se
->no_migrate
= vmsd
->unmigratable
;
1481 char *id
= qdev_get_dev_path(dev
);
1483 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1484 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1487 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1488 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1489 se
->compat
->instance_id
= instance_id
== -1 ?
1490 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1494 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1496 if (instance_id
== -1) {
1497 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1499 se
->instance_id
= instance_id
;
1501 assert(!se
->compat
|| se
->instance_id
== 0);
1502 /* add at the end of list */
1503 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1507 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1510 SaveStateEntry
*se
, *new_se
;
1512 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1513 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1514 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1523 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1525 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1528 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1529 void *opaque
, int version_id
)
1531 VMStateField
*field
= vmsd
->fields
;
1534 if (version_id
> vmsd
->version_id
) {
1537 if (version_id
< vmsd
->minimum_version_id_old
) {
1540 if (version_id
< vmsd
->minimum_version_id
) {
1541 return vmsd
->load_state_old(f
, opaque
, version_id
);
1543 if (vmsd
->pre_load
) {
1544 int ret
= vmsd
->pre_load(opaque
);
1548 while(field
->name
) {
1549 if ((field
->field_exists
&&
1550 field
->field_exists(opaque
, version_id
)) ||
1551 (!field
->field_exists
&&
1552 field
->version_id
<= version_id
)) {
1553 void *base_addr
= opaque
+ field
->offset
;
1555 int size
= field
->size
;
1557 if (field
->flags
& VMS_VBUFFER
) {
1558 size
= *(int32_t *)(opaque
+field
->size_offset
);
1559 if (field
->flags
& VMS_MULTIPLY
) {
1560 size
*= field
->size
;
1563 if (field
->flags
& VMS_ARRAY
) {
1564 n_elems
= field
->num
;
1565 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1566 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1567 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1568 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1569 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1570 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1571 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1572 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1574 if (field
->flags
& VMS_POINTER
) {
1575 base_addr
= *(void **)base_addr
+ field
->start
;
1577 for (i
= 0; i
< n_elems
; i
++) {
1578 void *addr
= base_addr
+ size
* i
;
1580 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1581 addr
= *(void **)addr
;
1583 if (field
->flags
& VMS_STRUCT
) {
1584 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1586 ret
= field
->info
->get(f
, addr
, size
);
1596 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1600 if (vmsd
->post_load
) {
1601 return vmsd
->post_load(opaque
, version_id
);
1606 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1609 VMStateField
*field
= vmsd
->fields
;
1611 if (vmsd
->pre_save
) {
1612 vmsd
->pre_save(opaque
);
1614 while(field
->name
) {
1615 if (!field
->field_exists
||
1616 field
->field_exists(opaque
, vmsd
->version_id
)) {
1617 void *base_addr
= opaque
+ field
->offset
;
1619 int size
= field
->size
;
1621 if (field
->flags
& VMS_VBUFFER
) {
1622 size
= *(int32_t *)(opaque
+field
->size_offset
);
1623 if (field
->flags
& VMS_MULTIPLY
) {
1624 size
*= field
->size
;
1627 if (field
->flags
& VMS_ARRAY
) {
1628 n_elems
= field
->num
;
1629 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1630 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1631 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1632 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1633 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1634 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1635 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1636 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1638 if (field
->flags
& VMS_POINTER
) {
1639 base_addr
= *(void **)base_addr
+ field
->start
;
1641 for (i
= 0; i
< n_elems
; i
++) {
1642 void *addr
= base_addr
+ size
* i
;
1644 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1645 addr
= *(void **)addr
;
1647 if (field
->flags
& VMS_STRUCT
) {
1648 vmstate_save_state(f
, field
->vmsd
, addr
);
1650 field
->info
->put(f
, addr
, size
);
1656 vmstate_subsection_save(f
, vmsd
, opaque
);
1659 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1661 if (!se
->vmsd
) { /* Old style */
1662 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1664 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1667 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1669 if (!se
->vmsd
) { /* Old style */
1670 se
->ops
->save_state(f
, se
->opaque
);
1673 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1676 #define QEMU_VM_FILE_MAGIC 0x5145564d
1677 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1678 #define QEMU_VM_FILE_VERSION 0x00000003
1680 #define QEMU_VM_EOF 0x00
1681 #define QEMU_VM_SECTION_START 0x01
1682 #define QEMU_VM_SECTION_PART 0x02
1683 #define QEMU_VM_SECTION_END 0x03
1684 #define QEMU_VM_SECTION_FULL 0x04
1685 #define QEMU_VM_SUBSECTION 0x05
1687 bool qemu_savevm_state_blocked(Error
**errp
)
1691 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1692 if (se
->no_migrate
) {
1693 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1700 void qemu_savevm_state_begin(QEMUFile
*f
,
1701 const MigrationParams
*params
)
1706 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1707 if (!se
->ops
|| !se
->ops
->set_params
) {
1710 se
->ops
->set_params(params
, se
->opaque
);
1713 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1714 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1716 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1719 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1722 if (se
->ops
&& se
->ops
->is_active
) {
1723 if (!se
->ops
->is_active(se
->opaque
)) {
1728 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1729 qemu_put_be32(f
, se
->section_id
);
1732 len
= strlen(se
->idstr
);
1733 qemu_put_byte(f
, len
);
1734 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1736 qemu_put_be32(f
, se
->instance_id
);
1737 qemu_put_be32(f
, se
->version_id
);
1739 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1741 qemu_file_set_error(f
, ret
);
1748 * this function has three return values:
1749 * negative: there was one error, and we have -errno.
1750 * 0 : We haven't finished, caller have to go again
1751 * 1 : We have finished, we can go to complete phase
1753 int qemu_savevm_state_iterate(QEMUFile
*f
)
1758 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1759 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1762 if (se
->ops
&& se
->ops
->is_active
) {
1763 if (!se
->ops
->is_active(se
->opaque
)) {
1767 if (qemu_file_rate_limit(f
)) {
1770 trace_savevm_section_start();
1772 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1773 qemu_put_be32(f
, se
->section_id
);
1775 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1776 trace_savevm_section_end(se
->section_id
);
1779 qemu_file_set_error(f
, ret
);
1782 /* Do not proceed to the next vmstate before this one reported
1783 completion of the current stage. This serializes the migration
1784 and reduces the probability that a faster changing state is
1785 synchronized over and over again. */
1792 void qemu_savevm_state_complete(QEMUFile
*f
)
1797 cpu_synchronize_all_states();
1799 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1800 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1803 if (se
->ops
&& se
->ops
->is_active
) {
1804 if (!se
->ops
->is_active(se
->opaque
)) {
1808 trace_savevm_section_start();
1810 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1811 qemu_put_be32(f
, se
->section_id
);
1813 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1814 trace_savevm_section_end(se
->section_id
);
1816 qemu_file_set_error(f
, ret
);
1821 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1824 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1827 trace_savevm_section_start();
1829 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1830 qemu_put_be32(f
, se
->section_id
);
1833 len
= strlen(se
->idstr
);
1834 qemu_put_byte(f
, len
);
1835 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1837 qemu_put_be32(f
, se
->instance_id
);
1838 qemu_put_be32(f
, se
->version_id
);
1840 vmstate_save(f
, se
);
1841 trace_savevm_section_end(se
->section_id
);
1844 qemu_put_byte(f
, QEMU_VM_EOF
);
1848 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1853 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1854 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1857 if (se
->ops
&& se
->ops
->is_active
) {
1858 if (!se
->ops
->is_active(se
->opaque
)) {
1862 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1867 void qemu_savevm_state_cancel(void)
1871 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1872 if (se
->ops
&& se
->ops
->cancel
) {
1873 se
->ops
->cancel(se
->opaque
);
1878 static int qemu_savevm_state(QEMUFile
*f
)
1881 MigrationParams params
= {
1886 if (qemu_savevm_state_blocked(NULL
)) {
1890 qemu_mutex_unlock_iothread();
1891 qemu_savevm_state_begin(f
, ¶ms
);
1892 qemu_mutex_lock_iothread();
1894 while (qemu_file_get_error(f
) == 0) {
1895 if (qemu_savevm_state_iterate(f
) > 0) {
1900 ret
= qemu_file_get_error(f
);
1902 qemu_savevm_state_complete(f
);
1903 ret
= qemu_file_get_error(f
);
1906 qemu_savevm_state_cancel();
1911 static int qemu_save_device_state(QEMUFile
*f
)
1915 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1916 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1918 cpu_synchronize_all_states();
1920 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1926 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1931 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1932 qemu_put_be32(f
, se
->section_id
);
1935 len
= strlen(se
->idstr
);
1936 qemu_put_byte(f
, len
);
1937 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1939 qemu_put_be32(f
, se
->instance_id
);
1940 qemu_put_be32(f
, se
->version_id
);
1942 vmstate_save(f
, se
);
1945 qemu_put_byte(f
, QEMU_VM_EOF
);
1947 return qemu_file_get_error(f
);
1950 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1954 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1955 if (!strcmp(se
->idstr
, idstr
) &&
1956 (instance_id
== se
->instance_id
||
1957 instance_id
== se
->alias_id
))
1959 /* Migrating from an older version? */
1960 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1961 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1962 (instance_id
== se
->compat
->instance_id
||
1963 instance_id
== se
->alias_id
))
1970 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
1972 while(sub
&& sub
->needed
) {
1973 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
1981 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1984 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
1987 uint8_t version_id
, len
, size
;
1988 const VMStateDescription
*sub_vmsd
;
1990 len
= qemu_peek_byte(f
, 1);
1991 if (len
< strlen(vmsd
->name
) + 1) {
1992 /* subsection name has be be "section_name/a" */
1995 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
2001 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
2002 /* it don't have a valid subsection name */
2005 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
2006 if (sub_vmsd
== NULL
) {
2009 qemu_file_skip(f
, 1); /* subsection */
2010 qemu_file_skip(f
, 1); /* len */
2011 qemu_file_skip(f
, len
); /* idstr */
2012 version_id
= qemu_get_be32(f
);
2014 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
2022 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
2025 const VMStateSubsection
*sub
= vmsd
->subsections
;
2027 while (sub
&& sub
->needed
) {
2028 if (sub
->needed(opaque
)) {
2029 const VMStateDescription
*vmsd
= sub
->vmsd
;
2032 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
2033 len
= strlen(vmsd
->name
);
2034 qemu_put_byte(f
, len
);
2035 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
2036 qemu_put_be32(f
, vmsd
->version_id
);
2037 vmstate_save_state(f
, vmsd
, opaque
);
2043 typedef struct LoadStateEntry
{
2044 QLIST_ENTRY(LoadStateEntry
) entry
;
2050 int qemu_loadvm_state(QEMUFile
*f
)
2052 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2053 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2054 LoadStateEntry
*le
, *new_le
;
2055 uint8_t section_type
;
2059 if (qemu_savevm_state_blocked(NULL
)) {
2063 v
= qemu_get_be32(f
);
2064 if (v
!= QEMU_VM_FILE_MAGIC
)
2067 v
= qemu_get_be32(f
);
2068 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2069 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2072 if (v
!= QEMU_VM_FILE_VERSION
)
2075 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2076 uint32_t instance_id
, version_id
, section_id
;
2081 switch (section_type
) {
2082 case QEMU_VM_SECTION_START
:
2083 case QEMU_VM_SECTION_FULL
:
2084 /* Read section start */
2085 section_id
= qemu_get_be32(f
);
2086 len
= qemu_get_byte(f
);
2087 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2089 instance_id
= qemu_get_be32(f
);
2090 version_id
= qemu_get_be32(f
);
2092 /* Find savevm section */
2093 se
= find_se(idstr
, instance_id
);
2095 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2100 /* Validate version */
2101 if (version_id
> se
->version_id
) {
2102 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2103 version_id
, idstr
, se
->version_id
);
2109 le
= g_malloc0(sizeof(*le
));
2112 le
->section_id
= section_id
;
2113 le
->version_id
= version_id
;
2114 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2116 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2118 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2119 instance_id
, idstr
);
2123 case QEMU_VM_SECTION_PART
:
2124 case QEMU_VM_SECTION_END
:
2125 section_id
= qemu_get_be32(f
);
2127 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2128 if (le
->section_id
== section_id
) {
2133 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2138 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2140 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2146 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2152 cpu_synchronize_all_post_init();
2157 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2158 QLIST_REMOVE(le
, entry
);
2163 ret
= qemu_file_get_error(f
);
2169 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2172 QEMUSnapshotInfo
*sn_tab
, *sn
;
2176 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2179 for(i
= 0; i
< nb_sns
; i
++) {
2181 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2192 * Deletes snapshots of a given name in all opened images.
2194 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2196 BlockDriverState
*bs
;
2197 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2201 while ((bs
= bdrv_next(bs
))) {
2202 if (bdrv_can_snapshot(bs
) &&
2203 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2205 ret
= bdrv_snapshot_delete(bs
, name
);
2208 "Error while deleting snapshot on '%s'\n",
2209 bdrv_get_device_name(bs
));
2218 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2220 BlockDriverState
*bs
, *bs1
;
2221 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2224 int saved_vm_running
;
2225 uint64_t vm_state_size
;
2228 const char *name
= qdict_get_try_str(qdict
, "name");
2230 /* Verify if there is a device that doesn't support snapshots and is writable */
2232 while ((bs
= bdrv_next(bs
))) {
2234 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2238 if (!bdrv_can_snapshot(bs
)) {
2239 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2240 bdrv_get_device_name(bs
));
2245 bs
= bdrv_snapshots();
2247 monitor_printf(mon
, "No block device can accept snapshots\n");
2251 saved_vm_running
= runstate_is_running();
2252 vm_stop(RUN_STATE_SAVE_VM
);
2254 memset(sn
, 0, sizeof(*sn
));
2256 /* fill auxiliary fields */
2257 qemu_gettimeofday(&tv
);
2258 sn
->date_sec
= tv
.tv_sec
;
2259 sn
->date_nsec
= tv
.tv_usec
* 1000;
2260 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2263 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2265 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2266 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2268 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2271 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2272 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2273 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2276 /* Delete old snapshots of the same name */
2277 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2281 /* save the VM state */
2282 f
= qemu_fopen_bdrv(bs
, 1);
2284 monitor_printf(mon
, "Could not open VM state file\n");
2287 ret
= qemu_savevm_state(f
);
2288 vm_state_size
= qemu_ftell(f
);
2291 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2295 /* create the snapshots */
2298 while ((bs1
= bdrv_next(bs1
))) {
2299 if (bdrv_can_snapshot(bs1
)) {
2300 /* Write VM state size only to the image that contains the state */
2301 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2302 ret
= bdrv_snapshot_create(bs1
, sn
);
2304 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2305 bdrv_get_device_name(bs1
));
2311 if (saved_vm_running
)
2315 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2318 int saved_vm_running
;
2321 saved_vm_running
= runstate_is_running();
2322 vm_stop(RUN_STATE_SAVE_VM
);
2324 f
= qemu_fopen(filename
, "wb");
2326 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2329 ret
= qemu_save_device_state(f
);
2332 error_set(errp
, QERR_IO_ERROR
);
2336 if (saved_vm_running
)
2340 int load_vmstate(const char *name
)
2342 BlockDriverState
*bs
, *bs_vm_state
;
2343 QEMUSnapshotInfo sn
;
2347 bs_vm_state
= bdrv_snapshots();
2349 error_report("No block device supports snapshots");
2353 /* Don't even try to load empty VM states */
2354 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2357 } else if (sn
.vm_state_size
== 0) {
2358 error_report("This is a disk-only snapshot. Revert to it offline "
2363 /* Verify if there is any device that doesn't support snapshots and is
2364 writable and check if the requested snapshot is available too. */
2366 while ((bs
= bdrv_next(bs
))) {
2368 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2372 if (!bdrv_can_snapshot(bs
)) {
2373 error_report("Device '%s' is writable but does not support snapshots.",
2374 bdrv_get_device_name(bs
));
2378 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2380 error_report("Device '%s' does not have the requested snapshot '%s'",
2381 bdrv_get_device_name(bs
), name
);
2386 /* Flush all IO requests so they don't interfere with the new state. */
2390 while ((bs
= bdrv_next(bs
))) {
2391 if (bdrv_can_snapshot(bs
)) {
2392 ret
= bdrv_snapshot_goto(bs
, name
);
2394 error_report("Error %d while activating snapshot '%s' on '%s'",
2395 ret
, name
, bdrv_get_device_name(bs
));
2401 /* restore the VM state */
2402 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2404 error_report("Could not open VM state file");
2408 qemu_system_reset(VMRESET_SILENT
);
2409 ret
= qemu_loadvm_state(f
);
2413 error_report("Error %d while loading VM state", ret
);
2420 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2422 BlockDriverState
*bs
, *bs1
;
2424 const char *name
= qdict_get_str(qdict
, "name");
2426 bs
= bdrv_snapshots();
2428 monitor_printf(mon
, "No block device supports snapshots\n");
2433 while ((bs1
= bdrv_next(bs1
))) {
2434 if (bdrv_can_snapshot(bs1
)) {
2435 ret
= bdrv_snapshot_delete(bs1
, name
);
2437 if (ret
== -ENOTSUP
)
2439 "Snapshots not supported on device '%s'\n",
2440 bdrv_get_device_name(bs1
));
2442 monitor_printf(mon
, "Error %d while deleting snapshot on "
2443 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2449 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2451 BlockDriverState
*bs
, *bs1
;
2452 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2453 int nb_sns
, i
, ret
, available
;
2455 int *available_snapshots
;
2458 bs
= bdrv_snapshots();
2460 monitor_printf(mon
, "No available block device supports snapshots\n");
2464 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2466 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2471 monitor_printf(mon
, "There is no snapshot available.\n");
2475 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2477 for (i
= 0; i
< nb_sns
; i
++) {
2482 while ((bs1
= bdrv_next(bs1
))) {
2483 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2484 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2493 available_snapshots
[total
] = i
;
2499 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2500 for (i
= 0; i
< total
; i
++) {
2501 sn
= &sn_tab
[available_snapshots
[i
]];
2502 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2505 monitor_printf(mon
, "There is no suitable snapshot available\n");
2509 g_free(available_snapshots
);
2513 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2515 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2516 memory_region_name(mr
), dev
);
2519 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2521 /* Nothing do to while the implementation is in RAMBlock */
2524 void vmstate_register_ram_global(MemoryRegion
*mr
)
2526 vmstate_register_ram(mr
, NULL
);