4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #include "config-host.h"
26 #include "qemu-common.h"
30 #include "monitor/monitor.h"
31 #include "sysemu/sysemu.h"
32 #include "qemu/timer.h"
33 #include "audio/audio.h"
34 #include "migration/migration.h"
35 #include "qemu/sockets.h"
36 #include "qemu/queue.h"
37 #include "sysemu/cpus.h"
38 #include "exec/memory.h"
39 #include "qmp-commands.h"
41 #include "qemu/bitops.h"
43 #define SELF_ANNOUNCE_ROUNDS 5
46 #define ETH_P_RARP 0x8035
48 #define ARP_HTYPE_ETH 0x0001
49 #define ARP_PTYPE_IP 0x0800
50 #define ARP_OP_REQUEST_REV 0x3
52 static int announce_self_create(uint8_t *buf
,
55 /* Ethernet header. */
56 memset(buf
, 0xff, 6); /* destination MAC addr */
57 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
58 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
61 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
62 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
63 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
64 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
65 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
66 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
67 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
68 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
69 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
71 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
72 memset(buf
+ 42, 0x00, 18);
74 return 60; /* len (FCS will be added by hardware) */
77 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
82 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
84 qemu_send_packet_raw(qemu_get_queue(nic
), buf
, len
);
88 static void qemu_announce_self_once(void *opaque
)
90 static int count
= SELF_ANNOUNCE_ROUNDS
;
91 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
93 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
96 /* delay 50ms, 150ms, 250ms, ... */
97 qemu_mod_timer(timer
, qemu_get_clock_ms(rt_clock
) +
98 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
100 qemu_del_timer(timer
);
101 qemu_free_timer(timer
);
105 void qemu_announce_self(void)
107 static QEMUTimer
*timer
;
108 timer
= qemu_new_timer_ms(rt_clock
, qemu_announce_self_once
, &timer
);
109 qemu_announce_self_once(&timer
);
112 /***********************************************************/
113 /* savevm/loadvm support */
115 #define IO_BUF_SIZE 32768
118 const QEMUFileOps
*ops
;
125 int64_t pos
; /* start of buffer when writing, end of buffer
128 int buf_size
; /* 0 when writing */
129 uint8_t buf
[IO_BUF_SIZE
];
134 typedef struct QEMUFileStdio
140 typedef struct QEMUFileSocket
151 static void fd_coroutine_enter(void *opaque
)
153 FDYieldUntilData
*data
= opaque
;
154 qemu_set_fd_handler(data
->fd
, NULL
, NULL
, NULL
);
155 qemu_coroutine_enter(data
->co
, NULL
);
159 * Yield until a file descriptor becomes readable
161 * Note that this function clobbers the handlers for the file descriptor.
163 static void coroutine_fn
yield_until_fd_readable(int fd
)
165 FDYieldUntilData data
;
167 assert(qemu_in_coroutine());
168 data
.co
= qemu_coroutine_self();
170 qemu_set_fd_handler(fd
, fd_coroutine_enter
, NULL
, &data
);
171 qemu_coroutine_yield();
174 static int socket_get_fd(void *opaque
)
176 QEMUFileSocket
*s
= opaque
;
181 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
183 QEMUFileSocket
*s
= opaque
;
187 len
= qemu_recv(s
->fd
, buf
, size
, 0);
191 if (socket_error() == EAGAIN
) {
192 yield_until_fd_readable(s
->fd
);
193 } else if (socket_error() != EINTR
) {
199 len
= -socket_error();
204 static int socket_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
206 QEMUFileSocket
*s
= opaque
;
209 len
= qemu_send_full(s
->fd
, buf
, size
, 0);
211 len
= -socket_error();
216 static int socket_close(void *opaque
)
218 QEMUFileSocket
*s
= opaque
;
224 static int stdio_get_fd(void *opaque
)
226 QEMUFileStdio
*s
= opaque
;
228 return fileno(s
->stdio_file
);
231 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
233 QEMUFileStdio
*s
= opaque
;
234 return fwrite(buf
, 1, size
, s
->stdio_file
);
237 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
239 QEMUFileStdio
*s
= opaque
;
240 FILE *fp
= s
->stdio_file
;
245 bytes
= fread(buf
, 1, size
, fp
);
246 if (bytes
!= 0 || !ferror(fp
)) {
249 if (errno
== EAGAIN
) {
250 yield_until_fd_readable(fileno(fp
));
251 } else if (errno
!= EINTR
) {
258 static int stdio_pclose(void *opaque
)
260 QEMUFileStdio
*s
= opaque
;
262 ret
= pclose(s
->stdio_file
);
265 } else if (!WIFEXITED(ret
) || WEXITSTATUS(ret
) != 0) {
266 /* close succeeded, but non-zero exit code: */
267 ret
= -EIO
; /* fake errno value */
273 static int stdio_fclose(void *opaque
)
275 QEMUFileStdio
*s
= opaque
;
278 if (s
->file
->ops
->put_buffer
) {
279 int fd
= fileno(s
->stdio_file
);
282 ret
= fstat(fd
, &st
);
283 if (ret
== 0 && S_ISREG(st
.st_mode
)) {
285 * If the file handle is a regular file make sure the
286 * data is flushed to disk before signaling success.
295 if (fclose(s
->stdio_file
) == EOF
) {
302 static const QEMUFileOps stdio_pipe_read_ops
= {
303 .get_fd
= stdio_get_fd
,
304 .get_buffer
= stdio_get_buffer
,
305 .close
= stdio_pclose
308 static const QEMUFileOps stdio_pipe_write_ops
= {
309 .get_fd
= stdio_get_fd
,
310 .put_buffer
= stdio_put_buffer
,
311 .close
= stdio_pclose
314 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
319 stdio_file
= popen(command
, mode
);
320 if (stdio_file
== NULL
) {
324 if (mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
325 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
329 s
= g_malloc0(sizeof(QEMUFileStdio
));
331 s
->stdio_file
= stdio_file
;
334 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_read_ops
);
336 s
->file
= qemu_fopen_ops(s
, &stdio_pipe_write_ops
);
341 static const QEMUFileOps stdio_file_read_ops
= {
342 .get_fd
= stdio_get_fd
,
343 .get_buffer
= stdio_get_buffer
,
344 .close
= stdio_fclose
347 static const QEMUFileOps stdio_file_write_ops
= {
348 .get_fd
= stdio_get_fd
,
349 .put_buffer
= stdio_put_buffer
,
350 .close
= stdio_fclose
353 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
358 (mode
[0] != 'r' && mode
[0] != 'w') ||
359 mode
[1] != 'b' || mode
[2] != 0) {
360 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
364 s
= g_malloc0(sizeof(QEMUFileStdio
));
365 s
->stdio_file
= fdopen(fd
, mode
);
370 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
372 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
381 static const QEMUFileOps socket_read_ops
= {
382 .get_fd
= socket_get_fd
,
383 .get_buffer
= socket_get_buffer
,
384 .close
= socket_close
387 static const QEMUFileOps socket_write_ops
= {
388 .get_fd
= socket_get_fd
,
389 .put_buffer
= socket_put_buffer
,
390 .close
= socket_close
393 QEMUFile
*qemu_fopen_socket(int fd
, const char *mode
)
395 QEMUFileSocket
*s
= g_malloc0(sizeof(QEMUFileSocket
));
398 (mode
[0] != 'r' && mode
[0] != 'w') ||
399 mode
[1] != 'b' || mode
[2] != 0) {
400 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
405 if (mode
[0] == 'w') {
406 socket_set_block(s
->fd
);
407 s
->file
= qemu_fopen_ops(s
, &socket_write_ops
);
409 s
->file
= qemu_fopen_ops(s
, &socket_read_ops
);
414 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
419 (mode
[0] != 'r' && mode
[0] != 'w') ||
420 mode
[1] != 'b' || mode
[2] != 0) {
421 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
425 s
= g_malloc0(sizeof(QEMUFileStdio
));
427 s
->stdio_file
= fopen(filename
, mode
);
432 s
->file
= qemu_fopen_ops(s
, &stdio_file_write_ops
);
434 s
->file
= qemu_fopen_ops(s
, &stdio_file_read_ops
);
442 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
443 int64_t pos
, int size
)
445 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
449 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
451 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
454 static int bdrv_fclose(void *opaque
)
456 return bdrv_flush(opaque
);
459 static const QEMUFileOps bdrv_read_ops
= {
460 .get_buffer
= block_get_buffer
,
464 static const QEMUFileOps bdrv_write_ops
= {
465 .put_buffer
= block_put_buffer
,
469 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
472 return qemu_fopen_ops(bs
, &bdrv_write_ops
);
473 return qemu_fopen_ops(bs
, &bdrv_read_ops
);
476 QEMUFile
*qemu_fopen_ops(void *opaque
, const QEMUFileOps
*ops
)
480 f
= g_malloc0(sizeof(QEMUFile
));
488 int qemu_file_get_error(QEMUFile
*f
)
490 return f
->last_error
;
493 static void qemu_file_set_error(QEMUFile
*f
, int ret
)
495 if (f
->last_error
== 0) {
500 /** Flushes QEMUFile buffer
503 static void qemu_fflush(QEMUFile
*f
)
507 if (!f
->ops
->put_buffer
) {
510 if (f
->is_write
&& f
->buf_index
> 0) {
511 ret
= f
->ops
->put_buffer(f
->opaque
, f
->buf
, f
->pos
, f
->buf_index
);
513 f
->pos
+= f
->buf_index
;
518 qemu_file_set_error(f
, ret
);
522 static void qemu_fill_buffer(QEMUFile
*f
)
527 if (!f
->ops
->get_buffer
)
533 pending
= f
->buf_size
- f
->buf_index
;
535 memmove(f
->buf
, f
->buf
+ f
->buf_index
, pending
);
538 f
->buf_size
= pending
;
540 len
= f
->ops
->get_buffer(f
->opaque
, f
->buf
+ pending
, f
->pos
,
541 IO_BUF_SIZE
- pending
);
545 } else if (len
== 0) {
546 qemu_file_set_error(f
, -EIO
);
547 } else if (len
!= -EAGAIN
)
548 qemu_file_set_error(f
, len
);
551 int qemu_get_fd(QEMUFile
*f
)
553 if (f
->ops
->get_fd
) {
554 return f
->ops
->get_fd(f
->opaque
);
561 * Returns negative error value if any error happened on previous operations or
562 * while closing the file. Returns 0 or positive number on success.
564 * The meaning of return value on success depends on the specific backend
567 int qemu_fclose(QEMUFile
*f
)
571 ret
= qemu_file_get_error(f
);
574 int ret2
= f
->ops
->close(f
->opaque
);
579 /* If any error was spotted before closing, we should report it
580 * instead of the close() return value.
589 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
597 if (f
->is_write
== 0 && f
->buf_index
> 0) {
599 "Attempted to write to buffer while read buffer is not empty\n");
604 l
= IO_BUF_SIZE
- f
->buf_index
;
607 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
613 if (f
->buf_index
>= IO_BUF_SIZE
) {
615 if (qemu_file_get_error(f
)) {
622 void qemu_put_byte(QEMUFile
*f
, int v
)
628 if (f
->is_write
== 0 && f
->buf_index
> 0) {
630 "Attempted to write to buffer while read buffer is not empty\n");
634 f
->buf
[f
->buf_index
++] = v
;
636 if (f
->buf_index
>= IO_BUF_SIZE
) {
641 static void qemu_file_skip(QEMUFile
*f
, int size
)
643 if (f
->buf_index
+ size
<= f
->buf_size
) {
644 f
->buf_index
+= size
;
648 static int qemu_peek_buffer(QEMUFile
*f
, uint8_t *buf
, int size
, size_t offset
)
657 index
= f
->buf_index
+ offset
;
658 pending
= f
->buf_size
- index
;
659 if (pending
< size
) {
661 index
= f
->buf_index
+ offset
;
662 pending
= f
->buf_size
- index
;
668 if (size
> pending
) {
672 memcpy(buf
, f
->buf
+ index
, size
);
676 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size
)
681 while (pending
> 0) {
684 res
= qemu_peek_buffer(f
, buf
, pending
, 0);
688 qemu_file_skip(f
, res
);
696 static int qemu_peek_byte(QEMUFile
*f
, int offset
)
698 int index
= f
->buf_index
+ offset
;
704 if (index
>= f
->buf_size
) {
706 index
= f
->buf_index
+ offset
;
707 if (index
>= f
->buf_size
) {
711 return f
->buf
[index
];
714 int qemu_get_byte(QEMUFile
*f
)
718 result
= qemu_peek_byte(f
, 0);
719 qemu_file_skip(f
, 1);
723 int64_t qemu_ftell(QEMUFile
*f
)
729 int qemu_file_rate_limit(QEMUFile
*f
)
731 if (qemu_file_get_error(f
)) {
734 if (f
->xfer_limit
> 0 && f
->bytes_xfer
> f
->xfer_limit
) {
740 int64_t qemu_file_get_rate_limit(QEMUFile
*f
)
742 return f
->xfer_limit
;
745 void qemu_file_set_rate_limit(QEMUFile
*f
, int64_t limit
)
747 f
->xfer_limit
= limit
;
750 void qemu_file_reset_rate_limit(QEMUFile
*f
)
755 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
757 qemu_put_byte(f
, v
>> 8);
761 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
763 qemu_put_byte(f
, v
>> 24);
764 qemu_put_byte(f
, v
>> 16);
765 qemu_put_byte(f
, v
>> 8);
769 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
771 qemu_put_be32(f
, v
>> 32);
775 unsigned int qemu_get_be16(QEMUFile
*f
)
778 v
= qemu_get_byte(f
) << 8;
779 v
|= qemu_get_byte(f
);
783 unsigned int qemu_get_be32(QEMUFile
*f
)
786 v
= qemu_get_byte(f
) << 24;
787 v
|= qemu_get_byte(f
) << 16;
788 v
|= qemu_get_byte(f
) << 8;
789 v
|= qemu_get_byte(f
);
793 uint64_t qemu_get_be64(QEMUFile
*f
)
796 v
= (uint64_t)qemu_get_be32(f
) << 32;
797 v
|= qemu_get_be32(f
);
804 void qemu_put_timer(QEMUFile
*f
, QEMUTimer
*ts
)
806 uint64_t expire_time
;
808 expire_time
= qemu_timer_expire_time_ns(ts
);
809 qemu_put_be64(f
, expire_time
);
812 void qemu_get_timer(QEMUFile
*f
, QEMUTimer
*ts
)
814 uint64_t expire_time
;
816 expire_time
= qemu_get_be64(f
);
817 if (expire_time
!= -1) {
818 qemu_mod_timer_ns(ts
, expire_time
);
827 static int get_bool(QEMUFile
*f
, void *pv
, size_t size
)
830 *v
= qemu_get_byte(f
);
834 static void put_bool(QEMUFile
*f
, void *pv
, size_t size
)
837 qemu_put_byte(f
, *v
);
840 const VMStateInfo vmstate_info_bool
= {
848 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
855 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
861 const VMStateInfo vmstate_info_int8
= {
869 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
872 qemu_get_sbe16s(f
, v
);
876 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
879 qemu_put_sbe16s(f
, v
);
882 const VMStateInfo vmstate_info_int16
= {
890 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
893 qemu_get_sbe32s(f
, v
);
897 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
900 qemu_put_sbe32s(f
, v
);
903 const VMStateInfo vmstate_info_int32
= {
909 /* 32 bit int. See that the received value is the same than the one
912 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
916 qemu_get_sbe32s(f
, &v2
);
923 const VMStateInfo vmstate_info_int32_equal
= {
924 .name
= "int32 equal",
925 .get
= get_int32_equal
,
929 /* 32 bit int. See that the received value is the less or the same
930 than the one in the field */
932 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
936 qemu_get_sbe32s(f
, &new);
943 const VMStateInfo vmstate_info_int32_le
= {
944 .name
= "int32 equal",
951 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
954 qemu_get_sbe64s(f
, v
);
958 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
961 qemu_put_sbe64s(f
, v
);
964 const VMStateInfo vmstate_info_int64
= {
970 /* 8 bit unsigned int */
972 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
979 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
985 const VMStateInfo vmstate_info_uint8
= {
991 /* 16 bit unsigned int */
993 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
996 qemu_get_be16s(f
, v
);
1000 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
1003 qemu_put_be16s(f
, v
);
1006 const VMStateInfo vmstate_info_uint16
= {
1012 /* 32 bit unsigned int */
1014 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1017 qemu_get_be32s(f
, v
);
1021 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
1024 qemu_put_be32s(f
, v
);
1027 const VMStateInfo vmstate_info_uint32
= {
1033 /* 32 bit uint. See that the received value is the same than the one
1036 static int get_uint32_equal(QEMUFile
*f
, void *pv
, size_t size
)
1040 qemu_get_be32s(f
, &v2
);
1048 const VMStateInfo vmstate_info_uint32_equal
= {
1049 .name
= "uint32 equal",
1050 .get
= get_uint32_equal
,
1054 /* 64 bit unsigned int */
1056 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1059 qemu_get_be64s(f
, v
);
1063 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
1066 qemu_put_be64s(f
, v
);
1069 const VMStateInfo vmstate_info_uint64
= {
1075 /* 64 bit unsigned int. See that the received value is the same than the one
1078 static int get_uint64_equal(QEMUFile
*f
, void *pv
, size_t size
)
1082 qemu_get_be64s(f
, &v2
);
1090 const VMStateInfo vmstate_info_uint64_equal
= {
1091 .name
= "int64 equal",
1092 .get
= get_uint64_equal
,
1096 /* 8 bit int. See that the received value is the same than the one
1099 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
1103 qemu_get_8s(f
, &v2
);
1110 const VMStateInfo vmstate_info_uint8_equal
= {
1111 .name
= "uint8 equal",
1112 .get
= get_uint8_equal
,
1116 /* 16 bit unsigned int int. See that the received value is the same than the one
1119 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
1123 qemu_get_be16s(f
, &v2
);
1130 const VMStateInfo vmstate_info_uint16_equal
= {
1131 .name
= "uint16 equal",
1132 .get
= get_uint16_equal
,
1136 /* floating point */
1138 static int get_float64(QEMUFile
*f
, void *pv
, size_t size
)
1142 *v
= make_float64(qemu_get_be64(f
));
1146 static void put_float64(QEMUFile
*f
, void *pv
, size_t size
)
1150 qemu_put_be64(f
, float64_val(*v
));
1153 const VMStateInfo vmstate_info_float64
= {
1161 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
1164 qemu_get_timer(f
, v
);
1168 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
1171 qemu_put_timer(f
, v
);
1174 const VMStateInfo vmstate_info_timer
= {
1180 /* uint8_t buffers */
1182 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1185 qemu_get_buffer(f
, v
, size
);
1189 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1192 qemu_put_buffer(f
, v
, size
);
1195 const VMStateInfo vmstate_info_buffer
= {
1201 /* unused buffers: space that was used for some fields that are
1202 not useful anymore */
1204 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1210 block_len
= MIN(sizeof(buf
), size
);
1212 qemu_get_buffer(f
, buf
, block_len
);
1217 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
1219 static const uint8_t buf
[1024];
1223 block_len
= MIN(sizeof(buf
), size
);
1225 qemu_put_buffer(f
, buf
, block_len
);
1229 const VMStateInfo vmstate_info_unused_buffer
= {
1230 .name
= "unused_buffer",
1231 .get
= get_unused_buffer
,
1232 .put
= put_unused_buffer
,
1235 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1236 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1237 * bit words with the bits in big endian order. The in-memory format
1238 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1240 /* This is the number of 64 bit words sent over the wire */
1241 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1242 static int get_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1244 unsigned long *bmp
= pv
;
1246 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1247 uint64_t w
= qemu_get_be64(f
);
1249 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1250 bmp
[idx
++] = w
>> 32;
1256 static void put_bitmap(QEMUFile
*f
, void *pv
, size_t size
)
1258 unsigned long *bmp
= pv
;
1260 for (i
= 0; i
< BITS_TO_U64S(size
); i
++) {
1261 uint64_t w
= bmp
[idx
++];
1262 if (sizeof(unsigned long) == 4 && idx
< BITS_TO_LONGS(size
)) {
1263 w
|= ((uint64_t)bmp
[idx
++]) << 32;
1265 qemu_put_be64(f
, w
);
1269 const VMStateInfo vmstate_info_bitmap
= {
1275 typedef struct CompatEntry
{
1280 typedef struct SaveStateEntry
{
1281 QTAILQ_ENTRY(SaveStateEntry
) entry
;
1287 SaveVMHandlers
*ops
;
1288 const VMStateDescription
*vmsd
;
1290 CompatEntry
*compat
;
1296 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1297 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1298 static int global_section_id
;
1300 static int calculate_new_instance_id(const char *idstr
)
1303 int instance_id
= 0;
1305 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1306 if (strcmp(idstr
, se
->idstr
) == 0
1307 && instance_id
<= se
->instance_id
) {
1308 instance_id
= se
->instance_id
+ 1;
1314 static int calculate_compat_instance_id(const char *idstr
)
1317 int instance_id
= 0;
1319 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1323 if (strcmp(idstr
, se
->compat
->idstr
) == 0
1324 && instance_id
<= se
->compat
->instance_id
) {
1325 instance_id
= se
->compat
->instance_id
+ 1;
1331 /* TODO: Individual devices generally have very little idea about the rest
1332 of the system, so instance_id should be removed/replaced.
1333 Meanwhile pass -1 as instance_id if you do not already have a clearly
1334 distinguishing id for all instances of your device class. */
1335 int register_savevm_live(DeviceState
*dev
,
1339 SaveVMHandlers
*ops
,
1344 se
= g_malloc0(sizeof(SaveStateEntry
));
1345 se
->version_id
= version_id
;
1346 se
->section_id
= global_section_id
++;
1348 se
->opaque
= opaque
;
1351 /* if this is a live_savem then set is_ram */
1352 if (ops
->save_live_setup
!= NULL
) {
1357 char *id
= qdev_get_dev_path(dev
);
1359 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1360 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1363 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1364 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), idstr
);
1365 se
->compat
->instance_id
= instance_id
== -1 ?
1366 calculate_compat_instance_id(idstr
) : instance_id
;
1370 pstrcat(se
->idstr
, sizeof(se
->idstr
), idstr
);
1372 if (instance_id
== -1) {
1373 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1375 se
->instance_id
= instance_id
;
1377 assert(!se
->compat
|| se
->instance_id
== 0);
1378 /* add at the end of list */
1379 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1383 int register_savevm(DeviceState
*dev
,
1387 SaveStateHandler
*save_state
,
1388 LoadStateHandler
*load_state
,
1391 SaveVMHandlers
*ops
= g_malloc0(sizeof(SaveVMHandlers
));
1392 ops
->save_state
= save_state
;
1393 ops
->load_state
= load_state
;
1394 return register_savevm_live(dev
, idstr
, instance_id
, version_id
,
1398 void unregister_savevm(DeviceState
*dev
, const char *idstr
, void *opaque
)
1400 SaveStateEntry
*se
, *new_se
;
1404 char *path
= qdev_get_dev_path(dev
);
1406 pstrcpy(id
, sizeof(id
), path
);
1407 pstrcat(id
, sizeof(id
), "/");
1411 pstrcat(id
, sizeof(id
), idstr
);
1413 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1414 if (strcmp(se
->idstr
, id
) == 0 && se
->opaque
== opaque
) {
1415 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1425 int vmstate_register_with_alias_id(DeviceState
*dev
, int instance_id
,
1426 const VMStateDescription
*vmsd
,
1427 void *opaque
, int alias_id
,
1428 int required_for_version
)
1432 /* If this triggers, alias support can be dropped for the vmsd. */
1433 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1435 se
= g_malloc0(sizeof(SaveStateEntry
));
1436 se
->version_id
= vmsd
->version_id
;
1437 se
->section_id
= global_section_id
++;
1438 se
->opaque
= opaque
;
1440 se
->alias_id
= alias_id
;
1441 se
->no_migrate
= vmsd
->unmigratable
;
1444 char *id
= qdev_get_dev_path(dev
);
1446 pstrcpy(se
->idstr
, sizeof(se
->idstr
), id
);
1447 pstrcat(se
->idstr
, sizeof(se
->idstr
), "/");
1450 se
->compat
= g_malloc0(sizeof(CompatEntry
));
1451 pstrcpy(se
->compat
->idstr
, sizeof(se
->compat
->idstr
), vmsd
->name
);
1452 se
->compat
->instance_id
= instance_id
== -1 ?
1453 calculate_compat_instance_id(vmsd
->name
) : instance_id
;
1457 pstrcat(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1459 if (instance_id
== -1) {
1460 se
->instance_id
= calculate_new_instance_id(se
->idstr
);
1462 se
->instance_id
= instance_id
;
1464 assert(!se
->compat
|| se
->instance_id
== 0);
1465 /* add at the end of list */
1466 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1470 void vmstate_unregister(DeviceState
*dev
, const VMStateDescription
*vmsd
,
1473 SaveStateEntry
*se
, *new_se
;
1475 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1476 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1477 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1486 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1488 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1491 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1492 void *opaque
, int version_id
)
1494 VMStateField
*field
= vmsd
->fields
;
1497 if (version_id
> vmsd
->version_id
) {
1500 if (version_id
< vmsd
->minimum_version_id_old
) {
1503 if (version_id
< vmsd
->minimum_version_id
) {
1504 return vmsd
->load_state_old(f
, opaque
, version_id
);
1506 if (vmsd
->pre_load
) {
1507 int ret
= vmsd
->pre_load(opaque
);
1511 while(field
->name
) {
1512 if ((field
->field_exists
&&
1513 field
->field_exists(opaque
, version_id
)) ||
1514 (!field
->field_exists
&&
1515 field
->version_id
<= version_id
)) {
1516 void *base_addr
= opaque
+ field
->offset
;
1518 int size
= field
->size
;
1520 if (field
->flags
& VMS_VBUFFER
) {
1521 size
= *(int32_t *)(opaque
+field
->size_offset
);
1522 if (field
->flags
& VMS_MULTIPLY
) {
1523 size
*= field
->size
;
1526 if (field
->flags
& VMS_ARRAY
) {
1527 n_elems
= field
->num
;
1528 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1529 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1530 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1531 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1532 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1533 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1534 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1535 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1537 if (field
->flags
& VMS_POINTER
) {
1538 base_addr
= *(void **)base_addr
+ field
->start
;
1540 for (i
= 0; i
< n_elems
; i
++) {
1541 void *addr
= base_addr
+ size
* i
;
1543 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1544 addr
= *(void **)addr
;
1546 if (field
->flags
& VMS_STRUCT
) {
1547 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1549 ret
= field
->info
->get(f
, addr
, size
);
1559 ret
= vmstate_subsection_load(f
, vmsd
, opaque
);
1563 if (vmsd
->post_load
) {
1564 return vmsd
->post_load(opaque
, version_id
);
1569 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1572 VMStateField
*field
= vmsd
->fields
;
1574 if (vmsd
->pre_save
) {
1575 vmsd
->pre_save(opaque
);
1577 while(field
->name
) {
1578 if (!field
->field_exists
||
1579 field
->field_exists(opaque
, vmsd
->version_id
)) {
1580 void *base_addr
= opaque
+ field
->offset
;
1582 int size
= field
->size
;
1584 if (field
->flags
& VMS_VBUFFER
) {
1585 size
= *(int32_t *)(opaque
+field
->size_offset
);
1586 if (field
->flags
& VMS_MULTIPLY
) {
1587 size
*= field
->size
;
1590 if (field
->flags
& VMS_ARRAY
) {
1591 n_elems
= field
->num
;
1592 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1593 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1594 } else if (field
->flags
& VMS_VARRAY_UINT32
) {
1595 n_elems
= *(uint32_t *)(opaque
+field
->num_offset
);
1596 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1597 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1598 } else if (field
->flags
& VMS_VARRAY_UINT8
) {
1599 n_elems
= *(uint8_t *)(opaque
+field
->num_offset
);
1601 if (field
->flags
& VMS_POINTER
) {
1602 base_addr
= *(void **)base_addr
+ field
->start
;
1604 for (i
= 0; i
< n_elems
; i
++) {
1605 void *addr
= base_addr
+ size
* i
;
1607 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1608 addr
= *(void **)addr
;
1610 if (field
->flags
& VMS_STRUCT
) {
1611 vmstate_save_state(f
, field
->vmsd
, addr
);
1613 field
->info
->put(f
, addr
, size
);
1619 vmstate_subsection_save(f
, vmsd
, opaque
);
1622 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1624 if (!se
->vmsd
) { /* Old style */
1625 return se
->ops
->load_state(f
, se
->opaque
, version_id
);
1627 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1630 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1632 if (!se
->vmsd
) { /* Old style */
1633 se
->ops
->save_state(f
, se
->opaque
);
1636 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1639 #define QEMU_VM_FILE_MAGIC 0x5145564d
1640 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1641 #define QEMU_VM_FILE_VERSION 0x00000003
1643 #define QEMU_VM_EOF 0x00
1644 #define QEMU_VM_SECTION_START 0x01
1645 #define QEMU_VM_SECTION_PART 0x02
1646 #define QEMU_VM_SECTION_END 0x03
1647 #define QEMU_VM_SECTION_FULL 0x04
1648 #define QEMU_VM_SUBSECTION 0x05
1650 bool qemu_savevm_state_blocked(Error
**errp
)
1654 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1655 if (se
->no_migrate
) {
1656 error_set(errp
, QERR_MIGRATION_NOT_SUPPORTED
, se
->idstr
);
1663 void qemu_savevm_state_begin(QEMUFile
*f
,
1664 const MigrationParams
*params
)
1669 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1670 if (!se
->ops
|| !se
->ops
->set_params
) {
1673 se
->ops
->set_params(params
, se
->opaque
);
1676 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1677 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1679 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1682 if (!se
->ops
|| !se
->ops
->save_live_setup
) {
1685 if (se
->ops
&& se
->ops
->is_active
) {
1686 if (!se
->ops
->is_active(se
->opaque
)) {
1691 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1692 qemu_put_be32(f
, se
->section_id
);
1695 len
= strlen(se
->idstr
);
1696 qemu_put_byte(f
, len
);
1697 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1699 qemu_put_be32(f
, se
->instance_id
);
1700 qemu_put_be32(f
, se
->version_id
);
1702 ret
= se
->ops
->save_live_setup(f
, se
->opaque
);
1704 qemu_file_set_error(f
, ret
);
1711 * this function has three return values:
1712 * negative: there was one error, and we have -errno.
1713 * 0 : We haven't finished, caller have to go again
1714 * 1 : We have finished, we can go to complete phase
1716 int qemu_savevm_state_iterate(QEMUFile
*f
)
1721 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1722 if (!se
->ops
|| !se
->ops
->save_live_iterate
) {
1725 if (se
->ops
&& se
->ops
->is_active
) {
1726 if (!se
->ops
->is_active(se
->opaque
)) {
1730 if (qemu_file_rate_limit(f
)) {
1733 trace_savevm_section_start();
1735 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1736 qemu_put_be32(f
, se
->section_id
);
1738 ret
= se
->ops
->save_live_iterate(f
, se
->opaque
);
1739 trace_savevm_section_end(se
->section_id
);
1742 qemu_file_set_error(f
, ret
);
1745 /* Do not proceed to the next vmstate before this one reported
1746 completion of the current stage. This serializes the migration
1747 and reduces the probability that a faster changing state is
1748 synchronized over and over again. */
1755 void qemu_savevm_state_complete(QEMUFile
*f
)
1760 cpu_synchronize_all_states();
1762 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1763 if (!se
->ops
|| !se
->ops
->save_live_complete
) {
1766 if (se
->ops
&& se
->ops
->is_active
) {
1767 if (!se
->ops
->is_active(se
->opaque
)) {
1771 trace_savevm_section_start();
1773 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1774 qemu_put_be32(f
, se
->section_id
);
1776 ret
= se
->ops
->save_live_complete(f
, se
->opaque
);
1777 trace_savevm_section_end(se
->section_id
);
1779 qemu_file_set_error(f
, ret
);
1784 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1787 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1790 trace_savevm_section_start();
1792 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1793 qemu_put_be32(f
, se
->section_id
);
1796 len
= strlen(se
->idstr
);
1797 qemu_put_byte(f
, len
);
1798 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1800 qemu_put_be32(f
, se
->instance_id
);
1801 qemu_put_be32(f
, se
->version_id
);
1803 vmstate_save(f
, se
);
1804 trace_savevm_section_end(se
->section_id
);
1807 qemu_put_byte(f
, QEMU_VM_EOF
);
1811 uint64_t qemu_savevm_state_pending(QEMUFile
*f
, uint64_t max_size
)
1816 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1817 if (!se
->ops
|| !se
->ops
->save_live_pending
) {
1820 if (se
->ops
&& se
->ops
->is_active
) {
1821 if (!se
->ops
->is_active(se
->opaque
)) {
1825 ret
+= se
->ops
->save_live_pending(f
, se
->opaque
, max_size
);
1830 void qemu_savevm_state_cancel(void)
1834 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1835 if (se
->ops
&& se
->ops
->cancel
) {
1836 se
->ops
->cancel(se
->opaque
);
1841 static int qemu_savevm_state(QEMUFile
*f
)
1844 MigrationParams params
= {
1849 if (qemu_savevm_state_blocked(NULL
)) {
1853 qemu_mutex_unlock_iothread();
1854 qemu_savevm_state_begin(f
, ¶ms
);
1855 qemu_mutex_lock_iothread();
1857 while (qemu_file_get_error(f
) == 0) {
1858 if (qemu_savevm_state_iterate(f
) > 0) {
1863 ret
= qemu_file_get_error(f
);
1865 qemu_savevm_state_complete(f
);
1866 ret
= qemu_file_get_error(f
);
1869 qemu_savevm_state_cancel();
1874 static int qemu_save_device_state(QEMUFile
*f
)
1878 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1879 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1881 cpu_synchronize_all_states();
1883 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1889 if ((!se
->ops
|| !se
->ops
->save_state
) && !se
->vmsd
) {
1894 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1895 qemu_put_be32(f
, se
->section_id
);
1898 len
= strlen(se
->idstr
);
1899 qemu_put_byte(f
, len
);
1900 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1902 qemu_put_be32(f
, se
->instance_id
);
1903 qemu_put_be32(f
, se
->version_id
);
1905 vmstate_save(f
, se
);
1908 qemu_put_byte(f
, QEMU_VM_EOF
);
1910 return qemu_file_get_error(f
);
1913 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1917 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1918 if (!strcmp(se
->idstr
, idstr
) &&
1919 (instance_id
== se
->instance_id
||
1920 instance_id
== se
->alias_id
))
1922 /* Migrating from an older version? */
1923 if (strstr(se
->idstr
, idstr
) && se
->compat
) {
1924 if (!strcmp(se
->compat
->idstr
, idstr
) &&
1925 (instance_id
== se
->compat
->instance_id
||
1926 instance_id
== se
->alias_id
))
1933 static const VMStateDescription
*vmstate_get_subsection(const VMStateSubsection
*sub
, char *idstr
)
1935 while(sub
&& sub
->needed
) {
1936 if (strcmp(idstr
, sub
->vmsd
->name
) == 0) {
1944 static int vmstate_subsection_load(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1947 while (qemu_peek_byte(f
, 0) == QEMU_VM_SUBSECTION
) {
1950 uint8_t version_id
, len
, size
;
1951 const VMStateDescription
*sub_vmsd
;
1953 len
= qemu_peek_byte(f
, 1);
1954 if (len
< strlen(vmsd
->name
) + 1) {
1955 /* subsection name has be be "section_name/a" */
1958 size
= qemu_peek_buffer(f
, (uint8_t *)idstr
, len
, 2);
1964 if (strncmp(vmsd
->name
, idstr
, strlen(vmsd
->name
)) != 0) {
1965 /* it don't have a valid subsection name */
1968 sub_vmsd
= vmstate_get_subsection(vmsd
->subsections
, idstr
);
1969 if (sub_vmsd
== NULL
) {
1972 qemu_file_skip(f
, 1); /* subsection */
1973 qemu_file_skip(f
, 1); /* len */
1974 qemu_file_skip(f
, len
); /* idstr */
1975 version_id
= qemu_get_be32(f
);
1977 ret
= vmstate_load_state(f
, sub_vmsd
, opaque
, version_id
);
1985 static void vmstate_subsection_save(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1988 const VMStateSubsection
*sub
= vmsd
->subsections
;
1990 while (sub
&& sub
->needed
) {
1991 if (sub
->needed(opaque
)) {
1992 const VMStateDescription
*vmsd
= sub
->vmsd
;
1995 qemu_put_byte(f
, QEMU_VM_SUBSECTION
);
1996 len
= strlen(vmsd
->name
);
1997 qemu_put_byte(f
, len
);
1998 qemu_put_buffer(f
, (uint8_t *)vmsd
->name
, len
);
1999 qemu_put_be32(f
, vmsd
->version_id
);
2000 vmstate_save_state(f
, vmsd
, opaque
);
2006 typedef struct LoadStateEntry
{
2007 QLIST_ENTRY(LoadStateEntry
) entry
;
2013 int qemu_loadvm_state(QEMUFile
*f
)
2015 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
2016 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
2017 LoadStateEntry
*le
, *new_le
;
2018 uint8_t section_type
;
2022 if (qemu_savevm_state_blocked(NULL
)) {
2026 v
= qemu_get_be32(f
);
2027 if (v
!= QEMU_VM_FILE_MAGIC
)
2030 v
= qemu_get_be32(f
);
2031 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
2032 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
2035 if (v
!= QEMU_VM_FILE_VERSION
)
2038 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
2039 uint32_t instance_id
, version_id
, section_id
;
2044 switch (section_type
) {
2045 case QEMU_VM_SECTION_START
:
2046 case QEMU_VM_SECTION_FULL
:
2047 /* Read section start */
2048 section_id
= qemu_get_be32(f
);
2049 len
= qemu_get_byte(f
);
2050 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
2052 instance_id
= qemu_get_be32(f
);
2053 version_id
= qemu_get_be32(f
);
2055 /* Find savevm section */
2056 se
= find_se(idstr
, instance_id
);
2058 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
2063 /* Validate version */
2064 if (version_id
> se
->version_id
) {
2065 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
2066 version_id
, idstr
, se
->version_id
);
2072 le
= g_malloc0(sizeof(*le
));
2075 le
->section_id
= section_id
;
2076 le
->version_id
= version_id
;
2077 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
2079 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2081 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2082 instance_id
, idstr
);
2086 case QEMU_VM_SECTION_PART
:
2087 case QEMU_VM_SECTION_END
:
2088 section_id
= qemu_get_be32(f
);
2090 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
2091 if (le
->section_id
== section_id
) {
2096 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
2101 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
2103 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
2109 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
2115 cpu_synchronize_all_post_init();
2120 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
2121 QLIST_REMOVE(le
, entry
);
2126 ret
= qemu_file_get_error(f
);
2132 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
2135 QEMUSnapshotInfo
*sn_tab
, *sn
;
2139 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2142 for(i
= 0; i
< nb_sns
; i
++) {
2144 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
2155 * Deletes snapshots of a given name in all opened images.
2157 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
2159 BlockDriverState
*bs
;
2160 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
2164 while ((bs
= bdrv_next(bs
))) {
2165 if (bdrv_can_snapshot(bs
) &&
2166 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
2168 ret
= bdrv_snapshot_delete(bs
, name
);
2171 "Error while deleting snapshot on '%s'\n",
2172 bdrv_get_device_name(bs
));
2181 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
2183 BlockDriverState
*bs
, *bs1
;
2184 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
2187 int saved_vm_running
;
2188 uint64_t vm_state_size
;
2191 const char *name
= qdict_get_try_str(qdict
, "name");
2193 /* Verify if there is a device that doesn't support snapshots and is writable */
2195 while ((bs
= bdrv_next(bs
))) {
2197 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2201 if (!bdrv_can_snapshot(bs
)) {
2202 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
2203 bdrv_get_device_name(bs
));
2208 bs
= bdrv_snapshots();
2210 monitor_printf(mon
, "No block device can accept snapshots\n");
2214 saved_vm_running
= runstate_is_running();
2215 vm_stop(RUN_STATE_SAVE_VM
);
2217 memset(sn
, 0, sizeof(*sn
));
2219 /* fill auxiliary fields */
2220 qemu_gettimeofday(&tv
);
2221 sn
->date_sec
= tv
.tv_sec
;
2222 sn
->date_nsec
= tv
.tv_usec
* 1000;
2223 sn
->vm_clock_nsec
= qemu_get_clock_ns(vm_clock
);
2226 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
2228 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
2229 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
2231 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
2234 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2235 localtime_r((const time_t *)&tv
.tv_sec
, &tm
);
2236 strftime(sn
->name
, sizeof(sn
->name
), "vm-%Y%m%d%H%M%S", &tm
);
2239 /* Delete old snapshots of the same name */
2240 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
2244 /* save the VM state */
2245 f
= qemu_fopen_bdrv(bs
, 1);
2247 monitor_printf(mon
, "Could not open VM state file\n");
2250 ret
= qemu_savevm_state(f
);
2251 vm_state_size
= qemu_ftell(f
);
2254 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
2258 /* create the snapshots */
2261 while ((bs1
= bdrv_next(bs1
))) {
2262 if (bdrv_can_snapshot(bs1
)) {
2263 /* Write VM state size only to the image that contains the state */
2264 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
2265 ret
= bdrv_snapshot_create(bs1
, sn
);
2267 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
2268 bdrv_get_device_name(bs1
));
2274 if (saved_vm_running
)
2278 void qmp_xen_save_devices_state(const char *filename
, Error
**errp
)
2281 int saved_vm_running
;
2284 saved_vm_running
= runstate_is_running();
2285 vm_stop(RUN_STATE_SAVE_VM
);
2287 f
= qemu_fopen(filename
, "wb");
2289 error_set(errp
, QERR_OPEN_FILE_FAILED
, filename
);
2292 ret
= qemu_save_device_state(f
);
2295 error_set(errp
, QERR_IO_ERROR
);
2299 if (saved_vm_running
)
2303 int load_vmstate(const char *name
)
2305 BlockDriverState
*bs
, *bs_vm_state
;
2306 QEMUSnapshotInfo sn
;
2310 bs_vm_state
= bdrv_snapshots();
2312 error_report("No block device supports snapshots");
2316 /* Don't even try to load empty VM states */
2317 ret
= bdrv_snapshot_find(bs_vm_state
, &sn
, name
);
2320 } else if (sn
.vm_state_size
== 0) {
2321 error_report("This is a disk-only snapshot. Revert to it offline "
2326 /* Verify if there is any device that doesn't support snapshots and is
2327 writable and check if the requested snapshot is available too. */
2329 while ((bs
= bdrv_next(bs
))) {
2331 if (!bdrv_is_inserted(bs
) || bdrv_is_read_only(bs
)) {
2335 if (!bdrv_can_snapshot(bs
)) {
2336 error_report("Device '%s' is writable but does not support snapshots.",
2337 bdrv_get_device_name(bs
));
2341 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
2343 error_report("Device '%s' does not have the requested snapshot '%s'",
2344 bdrv_get_device_name(bs
), name
);
2349 /* Flush all IO requests so they don't interfere with the new state. */
2353 while ((bs
= bdrv_next(bs
))) {
2354 if (bdrv_can_snapshot(bs
)) {
2355 ret
= bdrv_snapshot_goto(bs
, name
);
2357 error_report("Error %d while activating snapshot '%s' on '%s'",
2358 ret
, name
, bdrv_get_device_name(bs
));
2364 /* restore the VM state */
2365 f
= qemu_fopen_bdrv(bs_vm_state
, 0);
2367 error_report("Could not open VM state file");
2371 qemu_system_reset(VMRESET_SILENT
);
2372 ret
= qemu_loadvm_state(f
);
2376 error_report("Error %d while loading VM state", ret
);
2383 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
2385 BlockDriverState
*bs
, *bs1
;
2387 const char *name
= qdict_get_str(qdict
, "name");
2389 bs
= bdrv_snapshots();
2391 monitor_printf(mon
, "No block device supports snapshots\n");
2396 while ((bs1
= bdrv_next(bs1
))) {
2397 if (bdrv_can_snapshot(bs1
)) {
2398 ret
= bdrv_snapshot_delete(bs1
, name
);
2400 if (ret
== -ENOTSUP
)
2402 "Snapshots not supported on device '%s'\n",
2403 bdrv_get_device_name(bs1
));
2405 monitor_printf(mon
, "Error %d while deleting snapshot on "
2406 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
2412 void do_info_snapshots(Monitor
*mon
, const QDict
*qdict
)
2414 BlockDriverState
*bs
, *bs1
;
2415 QEMUSnapshotInfo
*sn_tab
, *sn
, s
, *sn_info
= &s
;
2416 int nb_sns
, i
, ret
, available
;
2418 int *available_snapshots
;
2421 bs
= bdrv_snapshots();
2423 monitor_printf(mon
, "No available block device supports snapshots\n");
2427 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
2429 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
2434 monitor_printf(mon
, "There is no snapshot available.\n");
2438 available_snapshots
= g_malloc0(sizeof(int) * nb_sns
);
2440 for (i
= 0; i
< nb_sns
; i
++) {
2445 while ((bs1
= bdrv_next(bs1
))) {
2446 if (bdrv_can_snapshot(bs1
) && bs1
!= bs
) {
2447 ret
= bdrv_snapshot_find(bs1
, sn_info
, sn
->id_str
);
2456 available_snapshots
[total
] = i
;
2462 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
2463 for (i
= 0; i
< total
; i
++) {
2464 sn
= &sn_tab
[available_snapshots
[i
]];
2465 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));
2468 monitor_printf(mon
, "There is no suitable snapshot available\n");
2472 g_free(available_snapshots
);
2476 void vmstate_register_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2478 qemu_ram_set_idstr(memory_region_get_ram_addr(mr
) & TARGET_PAGE_MASK
,
2479 memory_region_name(mr
), dev
);
2482 void vmstate_unregister_ram(MemoryRegion
*mr
, DeviceState
*dev
)
2484 /* Nothing do to while the implementation is in RAMBlock */
2487 void vmstate_register_ram_global(MemoryRegion
*mr
)
2489 vmstate_register_ram(mr
, NULL
);