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(__DragonFly__)
56 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
57 #include <freebsd/stdlib.h>
62 #include <linux/rtc.h>
70 #include <sys/timeb.h>
72 #define getopt_long_only getopt_long
73 #define memalign(align, size) malloc(size)
76 #include "qemu-common.h"
81 #include "qemu-timer.h"
82 #include "qemu-char.h"
84 #include "audio/audio.h"
85 #include "migration.h"
86 #include "qemu_socket.h"
87 #include "qemu-queue.h"
89 /* point to the block driver where the snapshots are managed */
90 static BlockDriverState
*bs_snapshots
;
92 #define SELF_ANNOUNCE_ROUNDS 5
95 #define ETH_P_RARP 0x0835
97 #define ARP_HTYPE_ETH 0x0001
98 #define ARP_PTYPE_IP 0x0800
99 #define ARP_OP_REQUEST_REV 0x3
101 static int announce_self_create(uint8_t *buf
,
104 /* Ethernet header. */
105 memset(buf
, 0xff, 6); /* destination MAC addr */
106 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
107 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
110 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
111 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
112 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
113 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
114 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
115 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
116 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
117 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
118 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
120 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
121 memset(buf
+ 42, 0x00, 18);
123 return 60; /* len (FCS will be added by hardware) */
126 static void qemu_announce_self_once(void *opaque
)
132 static int count
= SELF_ANNOUNCE_ROUNDS
;
133 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
135 for (i
= 0; i
< MAX_NICS
; i
++) {
136 if (!nd_table
[i
].used
)
138 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
139 vlan
= nd_table
[i
].vlan
;
140 QTAILQ_FOREACH(vc
, &vlan
->clients
, next
) {
141 qemu_send_packet_raw(vc
, buf
, len
);
145 /* delay 50ms, 150ms, 250ms, ... */
146 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) +
147 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
149 qemu_del_timer(timer
);
150 qemu_free_timer(timer
);
154 void qemu_announce_self(void)
156 static QEMUTimer
*timer
;
157 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
158 qemu_announce_self_once(&timer
);
161 /***********************************************************/
162 /* savevm/loadvm support */
164 #define IO_BUF_SIZE 32768
167 QEMUFilePutBufferFunc
*put_buffer
;
168 QEMUFileGetBufferFunc
*get_buffer
;
169 QEMUFileCloseFunc
*close
;
170 QEMUFileRateLimit
*rate_limit
;
171 QEMUFileSetRateLimit
*set_rate_limit
;
175 int64_t buf_offset
; /* start of buffer when writing, end of buffer
178 int buf_size
; /* 0 when writing */
179 uint8_t buf
[IO_BUF_SIZE
];
184 typedef struct QEMUFileStdio
190 typedef struct QEMUFileSocket
196 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
198 QEMUFileSocket
*s
= opaque
;
202 len
= recv(s
->fd
, (void *)buf
, size
, 0);
203 } while (len
== -1 && socket_error() == EINTR
);
206 len
= -socket_error();
211 static int socket_close(void *opaque
)
213 QEMUFileSocket
*s
= opaque
;
218 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
220 QEMUFileStdio
*s
= opaque
;
221 return fwrite(buf
, 1, size
, s
->stdio_file
);
224 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
226 QEMUFileStdio
*s
= opaque
;
227 FILE *fp
= s
->stdio_file
;
232 bytes
= fread(buf
, 1, size
, fp
);
233 } while ((bytes
== 0) && ferror(fp
) && (errno
== EINTR
));
237 static int stdio_pclose(void *opaque
)
239 QEMUFileStdio
*s
= opaque
;
240 pclose(s
->stdio_file
);
245 static int stdio_fclose(void *opaque
)
247 QEMUFileStdio
*s
= opaque
;
248 fclose(s
->stdio_file
);
253 QEMUFile
*qemu_popen(FILE *stdio_file
, const char *mode
)
257 if (stdio_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
258 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
262 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
264 s
->stdio_file
= stdio_file
;
267 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_pclose
, NULL
, NULL
);
269 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_pclose
, NULL
, NULL
);
274 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
278 popen_file
= popen(command
, mode
);
279 if(popen_file
== NULL
) {
283 return qemu_popen(popen_file
, mode
);
286 int qemu_stdio_fd(QEMUFile
*f
)
291 p
= (QEMUFileStdio
*)f
->opaque
;
292 fd
= fileno(p
->stdio_file
);
297 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
302 (mode
[0] != 'r' && mode
[0] != 'w') ||
303 mode
[1] != 'b' || mode
[2] != 0) {
304 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
308 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
309 s
->stdio_file
= fdopen(fd
, mode
);
314 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_fclose
, NULL
, NULL
);
316 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
325 QEMUFile
*qemu_fopen_socket(int fd
)
327 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
330 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
, NULL
);
334 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
335 int64_t pos
, int size
)
337 QEMUFileStdio
*s
= opaque
;
338 fseek(s
->stdio_file
, pos
, SEEK_SET
);
339 fwrite(buf
, 1, size
, s
->stdio_file
);
343 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
345 QEMUFileStdio
*s
= opaque
;
346 fseek(s
->stdio_file
, pos
, SEEK_SET
);
347 return fread(buf
, 1, size
, s
->stdio_file
);
350 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
355 (mode
[0] != 'r' && mode
[0] != 'w') ||
356 mode
[1] != 'b' || mode
[2] != 0) {
357 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
361 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
363 s
->stdio_file
= fopen(filename
, mode
);
368 s
->file
= qemu_fopen_ops(s
, file_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
370 s
->file
= qemu_fopen_ops(s
, NULL
, file_get_buffer
, stdio_fclose
, NULL
, NULL
);
378 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
379 int64_t pos
, int size
)
381 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
385 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
387 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
390 static int bdrv_fclose(void *opaque
)
395 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
398 return qemu_fopen_ops(bs
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
, NULL
);
399 return qemu_fopen_ops(bs
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
, NULL
);
402 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
403 QEMUFileGetBufferFunc
*get_buffer
,
404 QEMUFileCloseFunc
*close
,
405 QEMUFileRateLimit
*rate_limit
,
406 QEMUFileSetRateLimit
*set_rate_limit
)
410 f
= qemu_mallocz(sizeof(QEMUFile
));
413 f
->put_buffer
= put_buffer
;
414 f
->get_buffer
= get_buffer
;
416 f
->rate_limit
= rate_limit
;
417 f
->set_rate_limit
= set_rate_limit
;
423 int qemu_file_has_error(QEMUFile
*f
)
428 void qemu_file_set_error(QEMUFile
*f
)
433 void qemu_fflush(QEMUFile
*f
)
438 if (f
->is_write
&& f
->buf_index
> 0) {
441 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
443 f
->buf_offset
+= f
->buf_index
;
450 static void qemu_fill_buffer(QEMUFile
*f
)
460 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
464 f
->buf_offset
+= len
;
465 } else if (len
!= -EAGAIN
)
469 int qemu_fclose(QEMUFile
*f
)
474 ret
= f
->close(f
->opaque
);
479 void qemu_file_put_notify(QEMUFile
*f
)
481 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
484 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
488 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
490 "Attempted to write to buffer while read buffer is not empty\n");
494 while (!f
->has_error
&& size
> 0) {
495 l
= IO_BUF_SIZE
- f
->buf_index
;
498 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
503 if (f
->buf_index
>= IO_BUF_SIZE
)
508 void qemu_put_byte(QEMUFile
*f
, int v
)
510 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
512 "Attempted to write to buffer while read buffer is not empty\n");
516 f
->buf
[f
->buf_index
++] = v
;
518 if (f
->buf_index
>= IO_BUF_SIZE
)
522 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
531 l
= f
->buf_size
- f
->buf_index
;
534 l
= f
->buf_size
- f
->buf_index
;
540 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
548 int qemu_get_byte(QEMUFile
*f
)
553 if (f
->buf_index
>= f
->buf_size
) {
555 if (f
->buf_index
>= f
->buf_size
)
558 return f
->buf
[f
->buf_index
++];
561 int64_t qemu_ftell(QEMUFile
*f
)
563 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
566 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
568 if (whence
== SEEK_SET
) {
570 } else if (whence
== SEEK_CUR
) {
571 pos
+= qemu_ftell(f
);
573 /* SEEK_END not supported */
587 int qemu_file_rate_limit(QEMUFile
*f
)
590 return f
->rate_limit(f
->opaque
);
595 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
)
597 /* any failed or completed migration keeps its state to allow probing of
598 * migration data, but has no associated file anymore */
599 if (f
&& f
->set_rate_limit
)
600 return f
->set_rate_limit(f
->opaque
, new_rate
);
605 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
607 qemu_put_byte(f
, v
>> 8);
611 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
613 qemu_put_byte(f
, v
>> 24);
614 qemu_put_byte(f
, v
>> 16);
615 qemu_put_byte(f
, v
>> 8);
619 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
621 qemu_put_be32(f
, v
>> 32);
625 unsigned int qemu_get_be16(QEMUFile
*f
)
628 v
= qemu_get_byte(f
) << 8;
629 v
|= qemu_get_byte(f
);
633 unsigned int qemu_get_be32(QEMUFile
*f
)
636 v
= qemu_get_byte(f
) << 24;
637 v
|= qemu_get_byte(f
) << 16;
638 v
|= qemu_get_byte(f
) << 8;
639 v
|= qemu_get_byte(f
);
643 uint64_t qemu_get_be64(QEMUFile
*f
)
646 v
= (uint64_t)qemu_get_be32(f
) << 32;
647 v
|= qemu_get_be32(f
);
653 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
660 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
666 const VMStateInfo vmstate_info_int8
= {
674 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
677 qemu_get_sbe16s(f
, v
);
681 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
684 qemu_put_sbe16s(f
, v
);
687 const VMStateInfo vmstate_info_int16
= {
695 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
698 qemu_get_sbe32s(f
, v
);
702 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
705 qemu_put_sbe32s(f
, v
);
708 const VMStateInfo vmstate_info_int32
= {
714 /* 32 bit int. See that the received value is the same than the one
717 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
721 qemu_get_sbe32s(f
, &v2
);
728 const VMStateInfo vmstate_info_int32_equal
= {
729 .name
= "int32 equal",
730 .get
= get_int32_equal
,
734 /* 32 bit int. See that the received value is the less or the same
735 than the one in the field */
737 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
741 qemu_get_sbe32s(f
, &new);
748 const VMStateInfo vmstate_info_int32_le
= {
749 .name
= "int32 equal",
756 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
759 qemu_get_sbe64s(f
, v
);
763 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
766 qemu_put_sbe64s(f
, v
);
769 const VMStateInfo vmstate_info_int64
= {
775 /* 8 bit unsigned int */
777 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
784 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
790 const VMStateInfo vmstate_info_uint8
= {
796 /* 16 bit unsigned int */
798 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
801 qemu_get_be16s(f
, v
);
805 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
808 qemu_put_be16s(f
, v
);
811 const VMStateInfo vmstate_info_uint16
= {
817 /* 32 bit unsigned int */
819 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
822 qemu_get_be32s(f
, v
);
826 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
829 qemu_put_be32s(f
, v
);
832 const VMStateInfo vmstate_info_uint32
= {
838 /* 64 bit unsigned int */
840 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
843 qemu_get_be64s(f
, v
);
847 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
850 qemu_put_be64s(f
, v
);
853 const VMStateInfo vmstate_info_uint64
= {
859 /* 64 bit linux kernel unsigned int */
862 static int get_u64(QEMUFile
*f
, void *pv
, size_t size
)
865 qemu_get_be64s(f
, v
);
869 static void put_u64(QEMUFile
*f
, void *pv
, size_t size
)
872 qemu_put_be64s(f
, v
);
875 const VMStateInfo vmstate_info_u64
= {
880 #endif /* __linux__ */
882 /* 8 bit int. See that the received value is the same than the one
885 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
896 const VMStateInfo vmstate_info_uint8_equal
= {
897 .name
= "uint8 equal",
898 .get
= get_uint8_equal
,
902 /* 16 bit unsigned int int. See that the received value is the same than the one
905 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
909 qemu_get_be16s(f
, &v2
);
916 const VMStateInfo vmstate_info_uint16_equal
= {
917 .name
= "uint16 equal",
918 .get
= get_uint16_equal
,
924 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
927 qemu_get_timer(f
, v
);
931 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
934 qemu_put_timer(f
, v
);
937 const VMStateInfo vmstate_info_timer
= {
943 /* uint8_t buffers */
945 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
948 qemu_get_buffer(f
, v
, size
);
952 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
955 qemu_put_buffer(f
, v
, size
);
958 const VMStateInfo vmstate_info_buffer
= {
964 /* unused buffers: space that was used for some fields that are
965 not usefull anymore */
967 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
969 qemu_fseek(f
, size
, SEEK_CUR
);
973 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
975 qemu_fseek(f
, size
, SEEK_CUR
);
978 const VMStateInfo vmstate_info_unused_buffer
= {
979 .name
= "unused_buffer",
980 .get
= get_unused_buffer
,
981 .put
= put_unused_buffer
,
984 typedef struct SaveStateEntry
{
985 QTAILQ_ENTRY(SaveStateEntry
) entry
;
990 SaveLiveStateHandler
*save_live_state
;
991 SaveStateHandler
*save_state
;
992 LoadStateHandler
*load_state
;
993 const VMStateDescription
*vmsd
;
997 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
998 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
999 static int global_section_id
;
1001 static int calculate_new_instance_id(const char *idstr
)
1004 int instance_id
= 0;
1006 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1007 if (strcmp(idstr
, se
->idstr
) == 0
1008 && instance_id
<= se
->instance_id
) {
1009 instance_id
= se
->instance_id
+ 1;
1015 /* TODO: Individual devices generally have very little idea about the rest
1016 of the system, so instance_id should be removed/replaced.
1017 Meanwhile pass -1 as instance_id if you do not already have a clearly
1018 distinguishing id for all instances of your device class. */
1019 int register_savevm_live(const char *idstr
,
1022 SaveLiveStateHandler
*save_live_state
,
1023 SaveStateHandler
*save_state
,
1024 LoadStateHandler
*load_state
,
1029 se
= qemu_malloc(sizeof(SaveStateEntry
));
1030 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1031 se
->version_id
= version_id
;
1032 se
->section_id
= global_section_id
++;
1033 se
->save_live_state
= save_live_state
;
1034 se
->save_state
= save_state
;
1035 se
->load_state
= load_state
;
1036 se
->opaque
= opaque
;
1039 if (instance_id
== -1) {
1040 se
->instance_id
= calculate_new_instance_id(idstr
);
1042 se
->instance_id
= instance_id
;
1044 /* add at the end of list */
1045 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1049 int register_savevm(const char *idstr
,
1052 SaveStateHandler
*save_state
,
1053 LoadStateHandler
*load_state
,
1056 return register_savevm_live(idstr
, instance_id
, version_id
,
1057 NULL
, save_state
, load_state
, opaque
);
1060 void unregister_savevm(const char *idstr
, void *opaque
)
1062 SaveStateEntry
*se
, *new_se
;
1064 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1065 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
1066 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1072 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1077 se
= qemu_malloc(sizeof(SaveStateEntry
));
1078 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1079 se
->version_id
= vmsd
->version_id
;
1080 se
->section_id
= global_section_id
++;
1081 se
->save_live_state
= NULL
;
1082 se
->save_state
= NULL
;
1083 se
->load_state
= NULL
;
1084 se
->opaque
= opaque
;
1087 if (instance_id
== -1) {
1088 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1090 se
->instance_id
= instance_id
;
1092 /* add at the end of list */
1093 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1097 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1099 SaveStateEntry
*se
, *new_se
;
1101 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1102 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1103 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1109 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1110 void *opaque
, int version_id
)
1112 VMStateField
*field
= vmsd
->fields
;
1114 if (version_id
> vmsd
->version_id
) {
1117 if (version_id
< vmsd
->minimum_version_id_old
) {
1120 if (version_id
< vmsd
->minimum_version_id
) {
1121 return vmsd
->load_state_old(f
, opaque
, version_id
);
1123 if (vmsd
->pre_load
) {
1124 int ret
= vmsd
->pre_load(opaque
);
1128 while(field
->name
) {
1129 if ((field
->field_exists
&&
1130 field
->field_exists(opaque
, version_id
)) ||
1131 (!field
->field_exists
&&
1132 field
->version_id
<= version_id
)) {
1133 void *base_addr
= opaque
+ field
->offset
;
1134 int ret
, i
, n_elems
= 1;
1136 if (field
->flags
& VMS_ARRAY
) {
1137 n_elems
= field
->num
;
1138 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1139 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1140 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1141 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1143 if (field
->flags
& VMS_POINTER
) {
1144 base_addr
= *(void **)base_addr
;
1146 for (i
= 0; i
< n_elems
; i
++) {
1147 void *addr
= base_addr
+ field
->size
* i
;
1149 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1150 addr
= *(void **)addr
;
1152 if (field
->flags
& VMS_STRUCT
) {
1153 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1155 ret
= field
->info
->get(f
, addr
, field
->size
);
1165 if (vmsd
->post_load
) {
1166 return vmsd
->post_load(opaque
, version_id
);
1171 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1174 VMStateField
*field
= vmsd
->fields
;
1176 if (vmsd
->pre_save
) {
1177 vmsd
->pre_save(opaque
);
1179 while(field
->name
) {
1180 if (!field
->field_exists
||
1181 field
->field_exists(opaque
, vmsd
->version_id
)) {
1182 void *base_addr
= opaque
+ field
->offset
;
1185 if (field
->flags
& VMS_ARRAY
) {
1186 n_elems
= field
->num
;
1187 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1188 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1189 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1190 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1192 if (field
->flags
& VMS_POINTER
) {
1193 base_addr
= *(void **)base_addr
;
1195 for (i
= 0; i
< n_elems
; i
++) {
1196 void *addr
= base_addr
+ field
->size
* i
;
1198 if (field
->flags
& VMS_STRUCT
) {
1199 vmstate_save_state(f
, field
->vmsd
, addr
);
1201 field
->info
->put(f
, addr
, field
->size
);
1207 if (vmsd
->post_save
) {
1208 vmsd
->post_save(opaque
);
1212 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1214 if (!se
->vmsd
) { /* Old style */
1215 return se
->load_state(f
, se
->opaque
, version_id
);
1217 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1220 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1222 if (!se
->vmsd
) { /* Old style */
1223 se
->save_state(f
, se
->opaque
);
1226 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1229 #define QEMU_VM_FILE_MAGIC 0x5145564d
1230 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1231 #define QEMU_VM_FILE_VERSION 0x00000003
1233 #define QEMU_VM_EOF 0x00
1234 #define QEMU_VM_SECTION_START 0x01
1235 #define QEMU_VM_SECTION_PART 0x02
1236 #define QEMU_VM_SECTION_END 0x03
1237 #define QEMU_VM_SECTION_FULL 0x04
1239 int qemu_savevm_state_begin(QEMUFile
*f
)
1243 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1244 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1246 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1249 if (se
->save_live_state
== NULL
)
1253 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1254 qemu_put_be32(f
, se
->section_id
);
1257 len
= strlen(se
->idstr
);
1258 qemu_put_byte(f
, len
);
1259 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1261 qemu_put_be32(f
, se
->instance_id
);
1262 qemu_put_be32(f
, se
->version_id
);
1264 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
1267 if (qemu_file_has_error(f
))
1273 int qemu_savevm_state_iterate(QEMUFile
*f
)
1278 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1279 if (se
->save_live_state
== NULL
)
1283 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1284 qemu_put_be32(f
, se
->section_id
);
1286 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1292 if (qemu_file_has_error(f
))
1298 int qemu_savevm_state_complete(QEMUFile
*f
)
1302 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1303 if (se
->save_live_state
== NULL
)
1307 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1308 qemu_put_be32(f
, se
->section_id
);
1310 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
1313 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1316 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1320 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1321 qemu_put_be32(f
, se
->section_id
);
1324 len
= strlen(se
->idstr
);
1325 qemu_put_byte(f
, len
);
1326 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1328 qemu_put_be32(f
, se
->instance_id
);
1329 qemu_put_be32(f
, se
->version_id
);
1331 vmstate_save(f
, se
);
1334 qemu_put_byte(f
, QEMU_VM_EOF
);
1336 if (qemu_file_has_error(f
))
1342 int qemu_savevm_state(QEMUFile
*f
)
1344 int saved_vm_running
;
1347 saved_vm_running
= vm_running
;
1352 ret
= qemu_savevm_state_begin(f
);
1357 ret
= qemu_savevm_state_iterate(f
);
1362 ret
= qemu_savevm_state_complete(f
);
1365 if (qemu_file_has_error(f
))
1368 if (!ret
&& saved_vm_running
)
1374 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1378 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1379 if (!strcmp(se
->idstr
, idstr
) &&
1380 instance_id
== se
->instance_id
)
1386 typedef struct LoadStateEntry
{
1387 QLIST_ENTRY(LoadStateEntry
) entry
;
1393 int qemu_loadvm_state(QEMUFile
*f
)
1395 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1396 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1397 LoadStateEntry
*le
, *new_le
;
1398 uint8_t section_type
;
1402 v
= qemu_get_be32(f
);
1403 if (v
!= QEMU_VM_FILE_MAGIC
)
1406 v
= qemu_get_be32(f
);
1407 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1408 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1411 if (v
!= QEMU_VM_FILE_VERSION
)
1414 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1415 uint32_t instance_id
, version_id
, section_id
;
1420 switch (section_type
) {
1421 case QEMU_VM_SECTION_START
:
1422 case QEMU_VM_SECTION_FULL
:
1423 /* Read section start */
1424 section_id
= qemu_get_be32(f
);
1425 len
= qemu_get_byte(f
);
1426 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1428 instance_id
= qemu_get_be32(f
);
1429 version_id
= qemu_get_be32(f
);
1431 /* Find savevm section */
1432 se
= find_se(idstr
, instance_id
);
1434 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1439 /* Validate version */
1440 if (version_id
> se
->version_id
) {
1441 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1442 version_id
, idstr
, se
->version_id
);
1448 le
= qemu_mallocz(sizeof(*le
));
1451 le
->section_id
= section_id
;
1452 le
->version_id
= version_id
;
1453 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1455 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1457 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1458 instance_id
, idstr
);
1462 case QEMU_VM_SECTION_PART
:
1463 case QEMU_VM_SECTION_END
:
1464 section_id
= qemu_get_be32(f
);
1466 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1467 if (le
->section_id
== section_id
) {
1472 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1477 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1479 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1485 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1494 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1495 QLIST_REMOVE(le
, entry
);
1499 if (qemu_file_has_error(f
))
1505 /* device can contain snapshots */
1506 static int bdrv_can_snapshot(BlockDriverState
*bs
)
1509 !bdrv_is_removable(bs
) &&
1510 !bdrv_is_read_only(bs
));
1513 /* device must be snapshots in order to have a reliable snapshot */
1514 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1517 !bdrv_is_removable(bs
) &&
1518 !bdrv_is_read_only(bs
));
1521 static BlockDriverState
*get_bs_snapshots(void)
1523 BlockDriverState
*bs
;
1527 return bs_snapshots
;
1528 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1530 if (bdrv_can_snapshot(bs
))
1539 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1542 QEMUSnapshotInfo
*sn_tab
, *sn
;
1546 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1549 for(i
= 0; i
< nb_sns
; i
++) {
1551 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1561 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1564 BlockDriverState
*bs
, *bs1
;
1565 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1566 int must_delete
, ret
;
1568 int saved_vm_running
;
1569 uint32_t vm_state_size
;
1575 const char *name
= qdict_get_try_str(qdict
, "name");
1577 bs
= get_bs_snapshots();
1579 monitor_printf(mon
, "No block device can accept snapshots\n");
1583 /* ??? Should this occur after vm_stop? */
1586 saved_vm_running
= vm_running
;
1591 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1596 memset(sn
, 0, sizeof(*sn
));
1598 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1599 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1602 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1605 /* fill auxiliary fields */
1608 sn
->date_sec
= tb
.time
;
1609 sn
->date_nsec
= tb
.millitm
* 1000000;
1611 gettimeofday(&tv
, NULL
);
1612 sn
->date_sec
= tv
.tv_sec
;
1613 sn
->date_nsec
= tv
.tv_usec
* 1000;
1615 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1617 /* save the VM state */
1618 f
= qemu_fopen_bdrv(bs
, 1);
1620 monitor_printf(mon
, "Could not open VM state file\n");
1623 ret
= qemu_savevm_state(f
);
1624 vm_state_size
= qemu_ftell(f
);
1627 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1631 /* create the snapshots */
1633 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1635 if (bdrv_has_snapshot(bs1
)) {
1637 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1640 "Error while deleting snapshot on '%s'\n",
1641 bdrv_get_device_name(bs1
));
1644 /* Write VM state size only to the image that contains the state */
1645 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1646 ret
= bdrv_snapshot_create(bs1
, sn
);
1648 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1649 bdrv_get_device_name(bs1
));
1655 if (saved_vm_running
)
1659 int load_vmstate(Monitor
*mon
, const char *name
)
1662 BlockDriverState
*bs
, *bs1
;
1663 QEMUSnapshotInfo sn
;
1667 bs
= get_bs_snapshots();
1669 monitor_printf(mon
, "No block device supports snapshots\n");
1673 /* Flush all IO requests so they don't interfere with the new state. */
1676 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1678 if (bdrv_has_snapshot(bs1
)) {
1679 ret
= bdrv_snapshot_goto(bs1
, name
);
1682 monitor_printf(mon
, "Warning: ");
1686 "Snapshots not supported on device '%s'\n",
1687 bdrv_get_device_name(bs1
));
1690 monitor_printf(mon
, "Could not find snapshot '%s' on "
1692 name
, bdrv_get_device_name(bs1
));
1695 monitor_printf(mon
, "Error %d while activating snapshot on"
1696 " '%s'\n", ret
, bdrv_get_device_name(bs1
));
1699 /* fatal on snapshot block device */
1706 /* Don't even try to load empty VM states */
1707 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1708 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1711 /* restore the VM state */
1712 f
= qemu_fopen_bdrv(bs
, 0);
1714 monitor_printf(mon
, "Could not open VM state file\n");
1717 ret
= qemu_loadvm_state(f
);
1720 monitor_printf(mon
, "Error %d while loading VM state\n", ret
);
1726 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1729 BlockDriverState
*bs
, *bs1
;
1731 const char *name
= qdict_get_str(qdict
, "name");
1733 bs
= get_bs_snapshots();
1735 monitor_printf(mon
, "No block device supports snapshots\n");
1739 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1741 if (bdrv_has_snapshot(bs1
)) {
1742 ret
= bdrv_snapshot_delete(bs1
, name
);
1744 if (ret
== -ENOTSUP
)
1746 "Snapshots not supported on device '%s'\n",
1747 bdrv_get_device_name(bs1
));
1749 monitor_printf(mon
, "Error %d while deleting snapshot on "
1750 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1756 void do_info_snapshots(Monitor
*mon
)
1759 BlockDriverState
*bs
, *bs1
;
1760 QEMUSnapshotInfo
*sn_tab
, *sn
;
1764 bs
= get_bs_snapshots();
1766 monitor_printf(mon
, "No available block device supports snapshots\n");
1769 monitor_printf(mon
, "Snapshot devices:");
1770 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1772 if (bdrv_has_snapshot(bs1
)) {
1774 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1777 monitor_printf(mon
, "\n");
1779 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1781 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1784 monitor_printf(mon
, "Snapshot list (from %s):\n",
1785 bdrv_get_device_name(bs
));
1786 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1787 for(i
= 0; i
< nb_sns
; i
++) {
1789 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));