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
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <sys/select.h>
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
59 #include <linux/rtc.h>
67 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
73 #include "qemu-common.h"
78 #include "qemu-timer.h"
79 #include "qemu-char.h"
81 #include "audio/audio.h"
82 #include "migration.h"
83 #include "qemu_socket.h"
84 #include "qemu-queue.h"
86 /* point to the block driver where the snapshots are managed */
87 static BlockDriverState
*bs_snapshots
;
89 #define SELF_ANNOUNCE_ROUNDS 5
92 #define ETH_P_RARP 0x8035
94 #define ARP_HTYPE_ETH 0x0001
95 #define ARP_PTYPE_IP 0x0800
96 #define ARP_OP_REQUEST_REV 0x3
98 static int announce_self_create(uint8_t *buf
,
101 /* Ethernet header. */
102 memset(buf
, 0xff, 6); /* destination MAC addr */
103 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
104 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
107 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
108 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
109 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
110 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
111 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
112 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
113 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
114 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
115 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
117 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
118 memset(buf
+ 42, 0x00, 18);
120 return 60; /* len (FCS will be added by hardware) */
123 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
128 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
130 qemu_send_packet_raw(&nic
->nc
, buf
, len
);
134 static void qemu_announce_self_once(void *opaque
)
136 static int count
= SELF_ANNOUNCE_ROUNDS
;
137 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
139 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
142 /* delay 50ms, 150ms, 250ms, ... */
143 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) +
144 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
146 qemu_del_timer(timer
);
147 qemu_free_timer(timer
);
151 void qemu_announce_self(void)
153 static QEMUTimer
*timer
;
154 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
155 qemu_announce_self_once(&timer
);
158 /***********************************************************/
159 /* savevm/loadvm support */
161 #define IO_BUF_SIZE 32768
164 QEMUFilePutBufferFunc
*put_buffer
;
165 QEMUFileGetBufferFunc
*get_buffer
;
166 QEMUFileCloseFunc
*close
;
167 QEMUFileRateLimit
*rate_limit
;
168 QEMUFileSetRateLimit
*set_rate_limit
;
169 QEMUFileGetRateLimit
*get_rate_limit
;
173 int64_t buf_offset
; /* start of buffer when writing, end of buffer
176 int buf_size
; /* 0 when writing */
177 uint8_t buf
[IO_BUF_SIZE
];
182 typedef struct QEMUFileStdio
188 typedef struct QEMUFileSocket
194 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
196 QEMUFileSocket
*s
= opaque
;
200 len
= recv(s
->fd
, (void *)buf
, size
, 0);
201 } while (len
== -1 && socket_error() == EINTR
);
204 len
= -socket_error();
209 static int socket_close(void *opaque
)
211 QEMUFileSocket
*s
= opaque
;
216 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
218 QEMUFileStdio
*s
= opaque
;
219 return fwrite(buf
, 1, size
, s
->stdio_file
);
222 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
224 QEMUFileStdio
*s
= opaque
;
225 FILE *fp
= s
->stdio_file
;
230 bytes
= fread(buf
, 1, size
, fp
);
231 } while ((bytes
== 0) && ferror(fp
) && (errno
== EINTR
));
235 static int stdio_pclose(void *opaque
)
237 QEMUFileStdio
*s
= opaque
;
239 ret
= pclose(s
->stdio_file
);
244 static int stdio_fclose(void *opaque
)
246 QEMUFileStdio
*s
= opaque
;
247 fclose(s
->stdio_file
);
252 QEMUFile
*qemu_popen(FILE *stdio_file
, const char *mode
)
256 if (stdio_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
257 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
261 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
263 s
->stdio_file
= stdio_file
;
266 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_pclose
,
269 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_pclose
,
275 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
279 popen_file
= popen(command
, mode
);
280 if(popen_file
== NULL
) {
284 return qemu_popen(popen_file
, mode
);
287 int qemu_stdio_fd(QEMUFile
*f
)
292 p
= (QEMUFileStdio
*)f
->opaque
;
293 fd
= fileno(p
->stdio_file
);
298 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
303 (mode
[0] != 'r' && mode
[0] != 'w') ||
304 mode
[1] != 'b' || mode
[2] != 0) {
305 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
309 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
310 s
->stdio_file
= fdopen(fd
, mode
);
315 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_fclose
,
318 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_fclose
,
328 QEMUFile
*qemu_fopen_socket(int fd
)
330 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
333 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
,
338 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
339 int64_t pos
, int size
)
341 QEMUFileStdio
*s
= opaque
;
342 fseek(s
->stdio_file
, pos
, SEEK_SET
);
343 return fwrite(buf
, 1, size
, s
->stdio_file
);
346 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
348 QEMUFileStdio
*s
= opaque
;
349 fseek(s
->stdio_file
, pos
, SEEK_SET
);
350 return fread(buf
, 1, size
, s
->stdio_file
);
353 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
358 (mode
[0] != 'r' && mode
[0] != 'w') ||
359 mode
[1] != 'b' || mode
[2] != 0) {
360 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
364 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
366 s
->stdio_file
= fopen(filename
, mode
);
371 s
->file
= qemu_fopen_ops(s
, file_put_buffer
, NULL
, stdio_fclose
,
374 s
->file
= qemu_fopen_ops(s
, NULL
, file_get_buffer
, stdio_fclose
,
383 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
384 int64_t pos
, int size
)
386 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
390 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
392 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
395 static int bdrv_fclose(void *opaque
)
400 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
403 return qemu_fopen_ops(bs
, block_put_buffer
, NULL
, bdrv_fclose
,
405 return qemu_fopen_ops(bs
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
, NULL
, NULL
);
408 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
409 QEMUFileGetBufferFunc
*get_buffer
,
410 QEMUFileCloseFunc
*close
,
411 QEMUFileRateLimit
*rate_limit
,
412 QEMUFileSetRateLimit
*set_rate_limit
,
413 QEMUFileGetRateLimit
*get_rate_limit
)
417 f
= qemu_mallocz(sizeof(QEMUFile
));
420 f
->put_buffer
= put_buffer
;
421 f
->get_buffer
= get_buffer
;
423 f
->rate_limit
= rate_limit
;
424 f
->set_rate_limit
= set_rate_limit
;
425 f
->get_rate_limit
= get_rate_limit
;
431 int qemu_file_has_error(QEMUFile
*f
)
436 void qemu_file_set_error(QEMUFile
*f
)
441 void qemu_fflush(QEMUFile
*f
)
446 if (f
->is_write
&& f
->buf_index
> 0) {
449 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
451 f
->buf_offset
+= f
->buf_index
;
458 static void qemu_fill_buffer(QEMUFile
*f
)
468 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
472 f
->buf_offset
+= len
;
473 } else if (len
!= -EAGAIN
)
477 int qemu_fclose(QEMUFile
*f
)
482 ret
= f
->close(f
->opaque
);
487 void qemu_file_put_notify(QEMUFile
*f
)
489 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
492 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
496 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
498 "Attempted to write to buffer while read buffer is not empty\n");
502 while (!f
->has_error
&& size
> 0) {
503 l
= IO_BUF_SIZE
- f
->buf_index
;
506 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
511 if (f
->buf_index
>= IO_BUF_SIZE
)
516 void qemu_put_byte(QEMUFile
*f
, int v
)
518 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
520 "Attempted to write to buffer while read buffer is not empty\n");
524 f
->buf
[f
->buf_index
++] = v
;
526 if (f
->buf_index
>= IO_BUF_SIZE
)
530 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
539 l
= f
->buf_size
- f
->buf_index
;
542 l
= f
->buf_size
- f
->buf_index
;
548 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
556 int qemu_get_byte(QEMUFile
*f
)
561 if (f
->buf_index
>= f
->buf_size
) {
563 if (f
->buf_index
>= f
->buf_size
)
566 return f
->buf
[f
->buf_index
++];
569 int64_t qemu_ftell(QEMUFile
*f
)
571 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
574 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
576 if (whence
== SEEK_SET
) {
578 } else if (whence
== SEEK_CUR
) {
579 pos
+= qemu_ftell(f
);
581 /* SEEK_END not supported */
595 int qemu_file_rate_limit(QEMUFile
*f
)
598 return f
->rate_limit(f
->opaque
);
603 size_t qemu_file_get_rate_limit(QEMUFile
*f
)
605 if (f
->get_rate_limit
)
606 return f
->get_rate_limit(f
->opaque
);
611 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
)
613 /* any failed or completed migration keeps its state to allow probing of
614 * migration data, but has no associated file anymore */
615 if (f
&& f
->set_rate_limit
)
616 return f
->set_rate_limit(f
->opaque
, new_rate
);
621 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
623 qemu_put_byte(f
, v
>> 8);
627 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
629 qemu_put_byte(f
, v
>> 24);
630 qemu_put_byte(f
, v
>> 16);
631 qemu_put_byte(f
, v
>> 8);
635 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
637 qemu_put_be32(f
, v
>> 32);
641 unsigned int qemu_get_be16(QEMUFile
*f
)
644 v
= qemu_get_byte(f
) << 8;
645 v
|= qemu_get_byte(f
);
649 unsigned int qemu_get_be32(QEMUFile
*f
)
652 v
= qemu_get_byte(f
) << 24;
653 v
|= qemu_get_byte(f
) << 16;
654 v
|= qemu_get_byte(f
) << 8;
655 v
|= qemu_get_byte(f
);
659 uint64_t qemu_get_be64(QEMUFile
*f
)
662 v
= (uint64_t)qemu_get_be32(f
) << 32;
663 v
|= qemu_get_be32(f
);
669 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
676 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
682 const VMStateInfo vmstate_info_int8
= {
690 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
693 qemu_get_sbe16s(f
, v
);
697 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
700 qemu_put_sbe16s(f
, v
);
703 const VMStateInfo vmstate_info_int16
= {
711 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
714 qemu_get_sbe32s(f
, v
);
718 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
721 qemu_put_sbe32s(f
, v
);
724 const VMStateInfo vmstate_info_int32
= {
730 /* 32 bit int. See that the received value is the same than the one
733 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
737 qemu_get_sbe32s(f
, &v2
);
744 const VMStateInfo vmstate_info_int32_equal
= {
745 .name
= "int32 equal",
746 .get
= get_int32_equal
,
750 /* 32 bit int. See that the received value is the less or the same
751 than the one in the field */
753 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
757 qemu_get_sbe32s(f
, &new);
764 const VMStateInfo vmstate_info_int32_le
= {
765 .name
= "int32 equal",
772 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
775 qemu_get_sbe64s(f
, v
);
779 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
782 qemu_put_sbe64s(f
, v
);
785 const VMStateInfo vmstate_info_int64
= {
791 /* 8 bit unsigned int */
793 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
800 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
806 const VMStateInfo vmstate_info_uint8
= {
812 /* 16 bit unsigned int */
814 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
817 qemu_get_be16s(f
, v
);
821 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
824 qemu_put_be16s(f
, v
);
827 const VMStateInfo vmstate_info_uint16
= {
833 /* 32 bit unsigned int */
835 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
838 qemu_get_be32s(f
, v
);
842 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
845 qemu_put_be32s(f
, v
);
848 const VMStateInfo vmstate_info_uint32
= {
854 /* 64 bit unsigned int */
856 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
859 qemu_get_be64s(f
, v
);
863 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
866 qemu_put_be64s(f
, v
);
869 const VMStateInfo vmstate_info_uint64
= {
875 /* 8 bit int. See that the received value is the same than the one
878 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
889 const VMStateInfo vmstate_info_uint8_equal
= {
890 .name
= "uint8 equal",
891 .get
= get_uint8_equal
,
895 /* 16 bit unsigned int int. See that the received value is the same than the one
898 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
902 qemu_get_be16s(f
, &v2
);
909 const VMStateInfo vmstate_info_uint16_equal
= {
910 .name
= "uint16 equal",
911 .get
= get_uint16_equal
,
917 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
920 qemu_get_timer(f
, v
);
924 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
927 qemu_put_timer(f
, v
);
930 const VMStateInfo vmstate_info_timer
= {
936 /* uint8_t buffers */
938 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
941 qemu_get_buffer(f
, v
, size
);
945 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
948 qemu_put_buffer(f
, v
, size
);
951 const VMStateInfo vmstate_info_buffer
= {
957 /* unused buffers: space that was used for some fields that are
958 not usefull anymore */
960 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
966 block_len
= MIN(sizeof(buf
), size
);
968 qemu_get_buffer(f
, buf
, block_len
);
973 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
975 static const uint8_t buf
[1024];
979 block_len
= MIN(sizeof(buf
), size
);
981 qemu_put_buffer(f
, buf
, block_len
);
985 const VMStateInfo vmstate_info_unused_buffer
= {
986 .name
= "unused_buffer",
987 .get
= get_unused_buffer
,
988 .put
= put_unused_buffer
,
991 typedef struct SaveStateEntry
{
992 QTAILQ_ENTRY(SaveStateEntry
) entry
;
998 SaveSetParamsHandler
*set_params
;
999 SaveLiveStateHandler
*save_live_state
;
1000 SaveStateHandler
*save_state
;
1001 LoadStateHandler
*load_state
;
1002 const VMStateDescription
*vmsd
;
1007 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1008 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1009 static int global_section_id
;
1011 static int calculate_new_instance_id(const char *idstr
)
1014 int instance_id
= 0;
1016 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1017 if (strcmp(idstr
, se
->idstr
) == 0
1018 && instance_id
<= se
->instance_id
) {
1019 instance_id
= se
->instance_id
+ 1;
1025 /* TODO: Individual devices generally have very little idea about the rest
1026 of the system, so instance_id should be removed/replaced.
1027 Meanwhile pass -1 as instance_id if you do not already have a clearly
1028 distinguishing id for all instances of your device class. */
1029 int register_savevm_live(const char *idstr
,
1032 SaveSetParamsHandler
*set_params
,
1033 SaveLiveStateHandler
*save_live_state
,
1034 SaveStateHandler
*save_state
,
1035 LoadStateHandler
*load_state
,
1040 se
= qemu_mallocz(sizeof(SaveStateEntry
));
1041 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1042 se
->version_id
= version_id
;
1043 se
->section_id
= global_section_id
++;
1044 se
->set_params
= set_params
;
1045 se
->save_live_state
= save_live_state
;
1046 se
->save_state
= save_state
;
1047 se
->load_state
= load_state
;
1048 se
->opaque
= opaque
;
1051 if (instance_id
== -1) {
1052 se
->instance_id
= calculate_new_instance_id(idstr
);
1054 se
->instance_id
= instance_id
;
1056 /* add at the end of list */
1057 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1061 int register_savevm(const char *idstr
,
1064 SaveStateHandler
*save_state
,
1065 LoadStateHandler
*load_state
,
1068 return register_savevm_live(idstr
, instance_id
, version_id
,
1069 NULL
, NULL
, save_state
, load_state
, opaque
);
1072 void unregister_savevm(const char *idstr
, void *opaque
)
1074 SaveStateEntry
*se
, *new_se
;
1076 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1077 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
1078 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1084 int vmstate_register_with_alias_id(int instance_id
,
1085 const VMStateDescription
*vmsd
,
1086 void *opaque
, int alias_id
,
1087 int required_for_version
)
1091 /* If this triggers, alias support can be dropped for the vmsd. */
1092 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1094 se
= qemu_mallocz(sizeof(SaveStateEntry
));
1095 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1096 se
->version_id
= vmsd
->version_id
;
1097 se
->section_id
= global_section_id
++;
1098 se
->save_live_state
= NULL
;
1099 se
->save_state
= NULL
;
1100 se
->load_state
= NULL
;
1101 se
->opaque
= opaque
;
1103 se
->alias_id
= alias_id
;
1105 if (instance_id
== -1) {
1106 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1108 se
->instance_id
= instance_id
;
1110 /* add at the end of list */
1111 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1115 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1118 return vmstate_register_with_alias_id(instance_id
, vmsd
, opaque
, -1, 0);
1121 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1123 SaveStateEntry
*se
, *new_se
;
1125 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1126 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1127 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1133 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1134 void *opaque
, int version_id
)
1136 VMStateField
*field
= vmsd
->fields
;
1138 if (version_id
> vmsd
->version_id
) {
1141 if (version_id
< vmsd
->minimum_version_id_old
) {
1144 if (version_id
< vmsd
->minimum_version_id
) {
1145 return vmsd
->load_state_old(f
, opaque
, version_id
);
1147 if (vmsd
->pre_load
) {
1148 int ret
= vmsd
->pre_load(opaque
);
1152 while(field
->name
) {
1153 if ((field
->field_exists
&&
1154 field
->field_exists(opaque
, version_id
)) ||
1155 (!field
->field_exists
&&
1156 field
->version_id
<= version_id
)) {
1157 void *base_addr
= opaque
+ field
->offset
;
1158 int ret
, i
, n_elems
= 1;
1159 int size
= field
->size
;
1161 if (field
->flags
& VMS_VBUFFER
) {
1162 size
= *(int32_t *)(opaque
+field
->size_offset
);
1163 if (field
->flags
& VMS_MULTIPLY
) {
1164 size
*= field
->size
;
1167 if (field
->flags
& VMS_ARRAY
) {
1168 n_elems
= field
->num
;
1169 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1170 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1171 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1172 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1174 if (field
->flags
& VMS_POINTER
) {
1175 base_addr
= *(void **)base_addr
+ field
->start
;
1177 for (i
= 0; i
< n_elems
; i
++) {
1178 void *addr
= base_addr
+ size
* i
;
1180 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1181 addr
= *(void **)addr
;
1183 if (field
->flags
& VMS_STRUCT
) {
1184 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1186 ret
= field
->info
->get(f
, addr
, size
);
1196 if (vmsd
->post_load
) {
1197 return vmsd
->post_load(opaque
, version_id
);
1202 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1205 VMStateField
*field
= vmsd
->fields
;
1207 if (vmsd
->pre_save
) {
1208 vmsd
->pre_save(opaque
);
1210 while(field
->name
) {
1211 if (!field
->field_exists
||
1212 field
->field_exists(opaque
, vmsd
->version_id
)) {
1213 void *base_addr
= opaque
+ field
->offset
;
1215 int size
= field
->size
;
1217 if (field
->flags
& VMS_VBUFFER
) {
1218 size
= *(int32_t *)(opaque
+field
->size_offset
);
1219 if (field
->flags
& VMS_MULTIPLY
) {
1220 size
*= field
->size
;
1223 if (field
->flags
& VMS_ARRAY
) {
1224 n_elems
= field
->num
;
1225 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1226 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1227 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1228 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1230 if (field
->flags
& VMS_POINTER
) {
1231 base_addr
= *(void **)base_addr
+ field
->start
;
1233 for (i
= 0; i
< n_elems
; i
++) {
1234 void *addr
= base_addr
+ size
* i
;
1236 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1237 addr
= *(void **)addr
;
1239 if (field
->flags
& VMS_STRUCT
) {
1240 vmstate_save_state(f
, field
->vmsd
, addr
);
1242 field
->info
->put(f
, addr
, size
);
1250 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1252 if (!se
->vmsd
) { /* Old style */
1253 return se
->load_state(f
, se
->opaque
, version_id
);
1255 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1258 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1260 if (!se
->vmsd
) { /* Old style */
1261 se
->save_state(f
, se
->opaque
);
1264 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1267 #define QEMU_VM_FILE_MAGIC 0x5145564d
1268 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1269 #define QEMU_VM_FILE_VERSION 0x00000003
1271 #define QEMU_VM_EOF 0x00
1272 #define QEMU_VM_SECTION_START 0x01
1273 #define QEMU_VM_SECTION_PART 0x02
1274 #define QEMU_VM_SECTION_END 0x03
1275 #define QEMU_VM_SECTION_FULL 0x04
1277 int qemu_savevm_state_begin(Monitor
*mon
, QEMUFile
*f
, int blk_enable
,
1282 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1283 if(se
->set_params
== NULL
) {
1286 se
->set_params(blk_enable
, shared
, se
->opaque
);
1289 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1290 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1292 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1295 if (se
->save_live_state
== NULL
)
1299 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1300 qemu_put_be32(f
, se
->section_id
);
1303 len
= strlen(se
->idstr
);
1304 qemu_put_byte(f
, len
);
1305 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1307 qemu_put_be32(f
, se
->instance_id
);
1308 qemu_put_be32(f
, se
->version_id
);
1310 se
->save_live_state(mon
, f
, QEMU_VM_SECTION_START
, se
->opaque
);
1313 if (qemu_file_has_error(f
)) {
1314 qemu_savevm_state_cancel(mon
, f
);
1321 int qemu_savevm_state_iterate(Monitor
*mon
, QEMUFile
*f
)
1326 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1327 if (se
->save_live_state
== NULL
)
1331 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1332 qemu_put_be32(f
, se
->section_id
);
1334 ret
= se
->save_live_state(mon
, f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1336 /* Do not proceed to the next vmstate before this one reported
1337 completion of the current stage. This serializes the migration
1338 and reduces the probability that a faster changing state is
1339 synchronized over and over again. */
1347 if (qemu_file_has_error(f
)) {
1348 qemu_savevm_state_cancel(mon
, f
);
1355 int qemu_savevm_state_complete(Monitor
*mon
, QEMUFile
*f
)
1359 cpu_synchronize_all_states();
1361 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1362 if (se
->save_live_state
== NULL
)
1366 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1367 qemu_put_be32(f
, se
->section_id
);
1369 se
->save_live_state(mon
, f
, QEMU_VM_SECTION_END
, se
->opaque
);
1372 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1375 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1379 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1380 qemu_put_be32(f
, se
->section_id
);
1383 len
= strlen(se
->idstr
);
1384 qemu_put_byte(f
, len
);
1385 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1387 qemu_put_be32(f
, se
->instance_id
);
1388 qemu_put_be32(f
, se
->version_id
);
1390 vmstate_save(f
, se
);
1393 qemu_put_byte(f
, QEMU_VM_EOF
);
1395 if (qemu_file_has_error(f
))
1401 void qemu_savevm_state_cancel(Monitor
*mon
, QEMUFile
*f
)
1405 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1406 if (se
->save_live_state
) {
1407 se
->save_live_state(mon
, f
, -1, se
->opaque
);
1412 static int qemu_savevm_state(Monitor
*mon
, QEMUFile
*f
)
1414 int saved_vm_running
;
1417 saved_vm_running
= vm_running
;
1422 ret
= qemu_savevm_state_begin(mon
, f
, 0, 0);
1427 ret
= qemu_savevm_state_iterate(mon
, f
);
1432 ret
= qemu_savevm_state_complete(mon
, f
);
1435 if (qemu_file_has_error(f
))
1438 if (!ret
&& saved_vm_running
)
1444 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1448 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1449 if (!strcmp(se
->idstr
, idstr
) &&
1450 (instance_id
== se
->instance_id
||
1451 instance_id
== se
->alias_id
))
1457 typedef struct LoadStateEntry
{
1458 QLIST_ENTRY(LoadStateEntry
) entry
;
1464 int qemu_loadvm_state(QEMUFile
*f
)
1466 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1467 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1468 LoadStateEntry
*le
, *new_le
;
1469 uint8_t section_type
;
1473 v
= qemu_get_be32(f
);
1474 if (v
!= QEMU_VM_FILE_MAGIC
)
1477 v
= qemu_get_be32(f
);
1478 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1479 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1482 if (v
!= QEMU_VM_FILE_VERSION
)
1485 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1486 uint32_t instance_id
, version_id
, section_id
;
1491 switch (section_type
) {
1492 case QEMU_VM_SECTION_START
:
1493 case QEMU_VM_SECTION_FULL
:
1494 /* Read section start */
1495 section_id
= qemu_get_be32(f
);
1496 len
= qemu_get_byte(f
);
1497 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1499 instance_id
= qemu_get_be32(f
);
1500 version_id
= qemu_get_be32(f
);
1502 /* Find savevm section */
1503 se
= find_se(idstr
, instance_id
);
1505 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1510 /* Validate version */
1511 if (version_id
> se
->version_id
) {
1512 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1513 version_id
, idstr
, se
->version_id
);
1519 le
= qemu_mallocz(sizeof(*le
));
1522 le
->section_id
= section_id
;
1523 le
->version_id
= version_id
;
1524 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1526 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1528 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1529 instance_id
, idstr
);
1533 case QEMU_VM_SECTION_PART
:
1534 case QEMU_VM_SECTION_END
:
1535 section_id
= qemu_get_be32(f
);
1537 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1538 if (le
->section_id
== section_id
) {
1543 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1548 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1550 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1556 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1562 cpu_synchronize_all_post_init();
1567 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1568 QLIST_REMOVE(le
, entry
);
1572 if (qemu_file_has_error(f
))
1578 static BlockDriverState
*get_bs_snapshots(void)
1580 BlockDriverState
*bs
;
1583 return bs_snapshots
;
1584 /* FIXME what if bs_snapshots gets hot-unplugged? */
1587 while ((bs
= bdrv_next(bs
))) {
1588 if (bdrv_can_snapshot(bs
)) {
1598 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1601 QEMUSnapshotInfo
*sn_tab
, *sn
;
1605 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1608 for(i
= 0; i
< nb_sns
; i
++) {
1610 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1621 * Deletes snapshots of a given name in all opened images.
1623 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1625 BlockDriverState
*bs
;
1626 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1630 while ((bs
= bdrv_next(bs
))) {
1631 if (bdrv_can_snapshot(bs
) &&
1632 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
1634 ret
= bdrv_snapshot_delete(bs
, name
);
1637 "Error while deleting snapshot on '%s'\n",
1638 bdrv_get_device_name(bs
));
1647 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1649 BlockDriverState
*bs
, *bs1
;
1650 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1653 int saved_vm_running
;
1654 uint32_t vm_state_size
;
1660 const char *name
= qdict_get_try_str(qdict
, "name");
1662 /* Verify if there is a device that doesn't support snapshots and is writable */
1664 while ((bs
= bdrv_next(bs
))) {
1666 if (bdrv_is_removable(bs
) || bdrv_is_read_only(bs
)) {
1670 if (!bdrv_can_snapshot(bs
)) {
1671 monitor_printf(mon
, "Device '%s' is writable but does not support snapshots.\n",
1672 bdrv_get_device_name(bs
));
1677 bs
= get_bs_snapshots();
1679 monitor_printf(mon
, "No block device can accept snapshots\n");
1682 /* ??? Should this occur after vm_stop? */
1685 saved_vm_running
= vm_running
;
1688 memset(sn
, 0, sizeof(*sn
));
1690 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1692 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1693 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1695 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1699 /* fill auxiliary fields */
1702 sn
->date_sec
= tb
.time
;
1703 sn
->date_nsec
= tb
.millitm
* 1000000;
1705 gettimeofday(&tv
, NULL
);
1706 sn
->date_sec
= tv
.tv_sec
;
1707 sn
->date_nsec
= tv
.tv_usec
* 1000;
1709 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1711 /* Delete old snapshots of the same name */
1712 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1716 /* save the VM state */
1717 f
= qemu_fopen_bdrv(bs
, 1);
1719 monitor_printf(mon
, "Could not open VM state file\n");
1722 ret
= qemu_savevm_state(mon
, f
);
1723 vm_state_size
= qemu_ftell(f
);
1726 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1730 /* create the snapshots */
1733 while ((bs1
= bdrv_next(bs1
))) {
1734 if (bdrv_can_snapshot(bs1
)) {
1735 /* Write VM state size only to the image that contains the state */
1736 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1737 ret
= bdrv_snapshot_create(bs1
, sn
);
1739 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1740 bdrv_get_device_name(bs1
));
1746 if (saved_vm_running
)
1750 int load_vmstate(const char *name
)
1752 BlockDriverState
*bs
, *bs1
;
1753 QEMUSnapshotInfo sn
;
1757 /* Verify if there is a device that doesn't support snapshots and is writable */
1759 while ((bs
= bdrv_next(bs
))) {
1761 if (bdrv_is_removable(bs
) || bdrv_is_read_only(bs
)) {
1765 if (!bdrv_can_snapshot(bs
)) {
1766 error_report("Device '%s' is writable but does not support snapshots.",
1767 bdrv_get_device_name(bs
));
1772 bs
= get_bs_snapshots();
1774 error_report("No block device supports snapshots");
1778 /* Flush all IO requests so they don't interfere with the new state. */
1782 while ((bs1
= bdrv_next(bs1
))) {
1783 if (bdrv_can_snapshot(bs1
)) {
1784 ret
= bdrv_snapshot_goto(bs1
, name
);
1788 error_report("%sSnapshots not supported on device '%s'",
1789 bs
!= bs1
? "Warning: " : "",
1790 bdrv_get_device_name(bs1
));
1793 error_report("%sCould not find snapshot '%s' on device '%s'",
1794 bs
!= bs1
? "Warning: " : "",
1795 name
, bdrv_get_device_name(bs1
));
1798 error_report("%sError %d while activating snapshot on '%s'",
1799 bs
!= bs1
? "Warning: " : "",
1800 ret
, bdrv_get_device_name(bs1
));
1803 /* fatal on snapshot block device */
1810 /* Don't even try to load empty VM states */
1811 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1812 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1815 /* restore the VM state */
1816 f
= qemu_fopen_bdrv(bs
, 0);
1818 error_report("Could not open VM state file");
1821 ret
= qemu_loadvm_state(f
);
1824 error_report("Error %d while loading VM state", ret
);
1830 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1832 BlockDriverState
*bs
, *bs1
;
1834 const char *name
= qdict_get_str(qdict
, "name");
1836 bs
= get_bs_snapshots();
1838 monitor_printf(mon
, "No block device supports snapshots\n");
1843 while ((bs1
= bdrv_next(bs1
))) {
1844 if (bdrv_can_snapshot(bs1
)) {
1845 ret
= bdrv_snapshot_delete(bs1
, name
);
1847 if (ret
== -ENOTSUP
)
1849 "Snapshots not supported on device '%s'\n",
1850 bdrv_get_device_name(bs1
));
1852 monitor_printf(mon
, "Error %d while deleting snapshot on "
1853 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1859 void do_info_snapshots(Monitor
*mon
)
1861 BlockDriverState
*bs
, *bs1
;
1862 QEMUSnapshotInfo
*sn_tab
, *sn
;
1866 bs
= get_bs_snapshots();
1868 monitor_printf(mon
, "No available block device supports snapshots\n");
1871 monitor_printf(mon
, "Snapshot devices:");
1873 while ((bs1
= bdrv_next(bs1
))) {
1874 if (bdrv_can_snapshot(bs1
)) {
1876 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1879 monitor_printf(mon
, "\n");
1881 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1883 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1886 monitor_printf(mon
, "Snapshot list (from %s):\n",
1887 bdrv_get_device_name(bs
));
1888 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1889 for(i
= 0; i
< nb_sns
; i
++) {
1891 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));