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 #if defined(__NetBSD__)
46 #include <net/if_tap.h>
49 #include <linux/if_tun.h>
51 #include <arpa/inet.h>
54 #include <sys/select.h>
57 #if defined(__FreeBSD__) || defined(__DragonFly__)
62 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63 #include <freebsd/stdlib.h>
68 #include <linux/rtc.h>
76 #include <sys/timeb.h>
78 #define getopt_long_only getopt_long
79 #define memalign(align, size) malloc(size)
82 #include "qemu-common.h"
87 #include "qemu-timer.h"
88 #include "qemu-char.h"
90 #include "audio/audio.h"
91 #include "migration.h"
92 #include "qemu_socket.h"
93 #include "qemu-queue.h"
95 /* point to the block driver where the snapshots are managed */
96 static BlockDriverState
*bs_snapshots
;
98 #define SELF_ANNOUNCE_ROUNDS 5
99 #define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
100 //#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
101 #define EXPERIMENTAL_MAGIC 0xf1f23f4f
103 static int announce_self_create(uint8_t *buf
,
106 uint32_t magic
= EXPERIMENTAL_MAGIC
;
107 uint16_t proto
= htons(ETH_P_EXPERIMENTAL
);
109 /* FIXME: should we send a different packet (arp/rarp/ping)? */
112 memset(buf
, 0xff, 6); /* h_dst */
113 memcpy(buf
+ 6, mac_addr
, 6); /* h_src */
114 memcpy(buf
+ 12, &proto
, 2); /* h_proto */
115 memcpy(buf
+ 14, &magic
, 4); /* magic */
120 static void qemu_announce_self_once(void *opaque
)
126 static int count
= SELF_ANNOUNCE_ROUNDS
;
127 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
129 for (i
= 0; i
< MAX_NICS
; i
++) {
130 if (!nd_table
[i
].used
)
132 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
133 vlan
= nd_table
[i
].vlan
;
134 for(vc
= vlan
->first_client
; vc
!= NULL
; vc
= vc
->next
) {
135 vc
->receive(vc
, buf
, len
);
139 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) + 100);
141 qemu_del_timer(timer
);
142 qemu_free_timer(timer
);
146 void qemu_announce_self(void)
148 static QEMUTimer
*timer
;
149 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
150 qemu_announce_self_once(&timer
);
153 /***********************************************************/
154 /* savevm/loadvm support */
156 #define IO_BUF_SIZE 32768
159 QEMUFilePutBufferFunc
*put_buffer
;
160 QEMUFileGetBufferFunc
*get_buffer
;
161 QEMUFileCloseFunc
*close
;
162 QEMUFileRateLimit
*rate_limit
;
163 QEMUFileSetRateLimit
*set_rate_limit
;
167 int64_t buf_offset
; /* start of buffer when writing, end of buffer
170 int buf_size
; /* 0 when writing */
171 uint8_t buf
[IO_BUF_SIZE
];
176 typedef struct QEMUFileStdio
182 typedef struct QEMUFileSocket
188 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
190 QEMUFileSocket
*s
= opaque
;
194 len
= recv(s
->fd
, (void *)buf
, size
, 0);
195 } while (len
== -1 && socket_error() == EINTR
);
198 len
= -socket_error();
203 static int socket_close(void *opaque
)
205 QEMUFileSocket
*s
= opaque
;
210 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
212 QEMUFileStdio
*s
= opaque
;
213 return fwrite(buf
, 1, size
, s
->stdio_file
);
216 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
218 QEMUFileStdio
*s
= opaque
;
219 FILE *fp
= s
->stdio_file
;
224 bytes
= fread(buf
, 1, size
, fp
);
225 } while ((bytes
== 0) && ferror(fp
) && (errno
== EINTR
));
229 static int stdio_pclose(void *opaque
)
231 QEMUFileStdio
*s
= opaque
;
232 pclose(s
->stdio_file
);
237 static int stdio_fclose(void *opaque
)
239 QEMUFileStdio
*s
= opaque
;
240 fclose(s
->stdio_file
);
245 QEMUFile
*qemu_popen(FILE *stdio_file
, const char *mode
)
249 if (stdio_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
250 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
254 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
256 s
->stdio_file
= stdio_file
;
259 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_pclose
, NULL
, NULL
);
261 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_pclose
, NULL
, NULL
);
266 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
270 popen_file
= popen(command
, mode
);
271 if(popen_file
== NULL
) {
275 return qemu_popen(popen_file
, mode
);
278 int qemu_stdio_fd(QEMUFile
*f
)
283 p
= (QEMUFileStdio
*)f
->opaque
;
284 fd
= fileno(p
->stdio_file
);
289 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
294 (mode
[0] != 'r' && mode
[0] != 'w') ||
295 mode
[1] != 'b' || mode
[2] != 0) {
296 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
300 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
301 s
->stdio_file
= fdopen(fd
, mode
);
306 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_fclose
, NULL
, NULL
);
308 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
317 QEMUFile
*qemu_fopen_socket(int fd
)
319 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
322 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
, NULL
);
326 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
327 int64_t pos
, int size
)
329 QEMUFileStdio
*s
= opaque
;
330 fseek(s
->stdio_file
, pos
, SEEK_SET
);
331 fwrite(buf
, 1, size
, s
->stdio_file
);
335 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
337 QEMUFileStdio
*s
= opaque
;
338 fseek(s
->stdio_file
, pos
, SEEK_SET
);
339 return fread(buf
, 1, size
, s
->stdio_file
);
342 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
347 (mode
[0] != 'r' && mode
[0] != 'w') ||
348 mode
[1] != 'b' || mode
[2] != 0) {
349 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
353 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
355 s
->stdio_file
= fopen(filename
, mode
);
360 s
->file
= qemu_fopen_ops(s
, file_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
362 s
->file
= qemu_fopen_ops(s
, NULL
, file_get_buffer
, stdio_fclose
, NULL
, NULL
);
370 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
371 int64_t pos
, int size
)
373 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
377 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
379 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
382 static int bdrv_fclose(void *opaque
)
387 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
390 return qemu_fopen_ops(bs
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
, NULL
);
391 return qemu_fopen_ops(bs
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
, NULL
);
394 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
395 QEMUFileGetBufferFunc
*get_buffer
,
396 QEMUFileCloseFunc
*close
,
397 QEMUFileRateLimit
*rate_limit
,
398 QEMUFileSetRateLimit
*set_rate_limit
)
402 f
= qemu_mallocz(sizeof(QEMUFile
));
405 f
->put_buffer
= put_buffer
;
406 f
->get_buffer
= get_buffer
;
408 f
->rate_limit
= rate_limit
;
409 f
->set_rate_limit
= set_rate_limit
;
415 int qemu_file_has_error(QEMUFile
*f
)
420 void qemu_file_set_error(QEMUFile
*f
)
425 void qemu_fflush(QEMUFile
*f
)
430 if (f
->is_write
&& f
->buf_index
> 0) {
433 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
435 f
->buf_offset
+= f
->buf_index
;
442 static void qemu_fill_buffer(QEMUFile
*f
)
452 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
456 f
->buf_offset
+= len
;
457 } else if (len
!= -EAGAIN
)
461 int qemu_fclose(QEMUFile
*f
)
466 ret
= f
->close(f
->opaque
);
471 void qemu_file_put_notify(QEMUFile
*f
)
473 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
476 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
480 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
482 "Attempted to write to buffer while read buffer is not empty\n");
486 while (!f
->has_error
&& size
> 0) {
487 l
= IO_BUF_SIZE
- f
->buf_index
;
490 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
495 if (f
->buf_index
>= IO_BUF_SIZE
)
500 void qemu_put_byte(QEMUFile
*f
, int v
)
502 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
504 "Attempted to write to buffer while read buffer is not empty\n");
508 f
->buf
[f
->buf_index
++] = v
;
510 if (f
->buf_index
>= IO_BUF_SIZE
)
514 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
523 l
= f
->buf_size
- f
->buf_index
;
526 l
= f
->buf_size
- f
->buf_index
;
532 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
540 int qemu_get_byte(QEMUFile
*f
)
545 if (f
->buf_index
>= f
->buf_size
) {
547 if (f
->buf_index
>= f
->buf_size
)
550 return f
->buf
[f
->buf_index
++];
553 int64_t qemu_ftell(QEMUFile
*f
)
555 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
558 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
560 if (whence
== SEEK_SET
) {
562 } else if (whence
== SEEK_CUR
) {
563 pos
+= qemu_ftell(f
);
565 /* SEEK_END not supported */
579 int qemu_file_rate_limit(QEMUFile
*f
)
582 return f
->rate_limit(f
->opaque
);
587 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
)
589 /* any failed or completed migration keeps its state to allow probing of
590 * migration data, but has no associated file anymore */
591 if (f
&& f
->set_rate_limit
)
592 return f
->set_rate_limit(f
->opaque
, new_rate
);
597 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
599 qemu_put_byte(f
, v
>> 8);
603 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
605 qemu_put_byte(f
, v
>> 24);
606 qemu_put_byte(f
, v
>> 16);
607 qemu_put_byte(f
, v
>> 8);
611 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
613 qemu_put_be32(f
, v
>> 32);
617 unsigned int qemu_get_be16(QEMUFile
*f
)
620 v
= qemu_get_byte(f
) << 8;
621 v
|= qemu_get_byte(f
);
625 unsigned int qemu_get_be32(QEMUFile
*f
)
628 v
= qemu_get_byte(f
) << 24;
629 v
|= qemu_get_byte(f
) << 16;
630 v
|= qemu_get_byte(f
) << 8;
631 v
|= qemu_get_byte(f
);
635 uint64_t qemu_get_be64(QEMUFile
*f
)
638 v
= (uint64_t)qemu_get_be32(f
) << 32;
639 v
|= qemu_get_be32(f
);
645 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
652 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
658 const VMStateInfo vmstate_info_int8
= {
666 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
669 qemu_get_sbe16s(f
, v
);
673 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
676 qemu_put_sbe16s(f
, v
);
679 const VMStateInfo vmstate_info_int16
= {
687 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
690 qemu_get_sbe32s(f
, v
);
694 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
697 qemu_put_sbe32s(f
, v
);
700 const VMStateInfo vmstate_info_int32
= {
706 /* 32 bit int. See that the received value is the same than the one
709 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
713 qemu_get_sbe32s(f
, &v2
);
720 const VMStateInfo vmstate_info_int32_equal
= {
721 .name
= "int32 equal",
722 .get
= get_int32_equal
,
726 /* 32 bit int. See that the received value is the less or the same
727 than the one in the field */
729 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
733 qemu_get_sbe32s(f
, &new);
740 const VMStateInfo vmstate_info_int32_le
= {
741 .name
= "int32 equal",
748 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
751 qemu_get_sbe64s(f
, v
);
755 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
758 qemu_put_sbe64s(f
, v
);
761 const VMStateInfo vmstate_info_int64
= {
767 /* 8 bit unsigned int */
769 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
776 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
782 const VMStateInfo vmstate_info_uint8
= {
788 /* 16 bit unsigned int */
790 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
793 qemu_get_be16s(f
, v
);
797 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
800 qemu_put_be16s(f
, v
);
803 const VMStateInfo vmstate_info_uint16
= {
809 /* 32 bit unsigned int */
811 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
814 qemu_get_be32s(f
, v
);
818 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
821 qemu_put_be32s(f
, v
);
824 const VMStateInfo vmstate_info_uint32
= {
830 /* 64 bit unsigned int */
832 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
835 qemu_get_be64s(f
, v
);
839 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
842 qemu_put_be64s(f
, v
);
845 const VMStateInfo vmstate_info_uint64
= {
851 /* 8 bit int. See that the received value is the same than the one
854 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
865 const VMStateInfo vmstate_info_uint8_equal
= {
866 .name
= "int32 equal",
867 .get
= get_uint8_equal
,
873 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
876 qemu_get_timer(f
, v
);
880 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
883 qemu_put_timer(f
, v
);
886 const VMStateInfo vmstate_info_timer
= {
892 /* uint8_t buffers */
894 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
897 qemu_get_buffer(f
, v
, size
);
901 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
904 qemu_put_buffer(f
, v
, size
);
907 const VMStateInfo vmstate_info_buffer
= {
913 typedef struct SaveStateEntry
{
914 QTAILQ_ENTRY(SaveStateEntry
) entry
;
919 SaveLiveStateHandler
*save_live_state
;
920 SaveStateHandler
*save_state
;
921 LoadStateHandler
*load_state
;
922 const VMStateDescription
*vmsd
;
926 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
927 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
928 static int global_section_id
;
930 static int calculate_new_instance_id(const char *idstr
)
935 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
936 if (strcmp(idstr
, se
->idstr
) == 0
937 && instance_id
<= se
->instance_id
) {
938 instance_id
= se
->instance_id
+ 1;
944 /* TODO: Individual devices generally have very little idea about the rest
945 of the system, so instance_id should be removed/replaced.
946 Meanwhile pass -1 as instance_id if you do not already have a clearly
947 distinguishing id for all instances of your device class. */
948 int register_savevm_live(const char *idstr
,
951 SaveLiveStateHandler
*save_live_state
,
952 SaveStateHandler
*save_state
,
953 LoadStateHandler
*load_state
,
958 se
= qemu_malloc(sizeof(SaveStateEntry
));
959 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
960 se
->version_id
= version_id
;
961 se
->section_id
= global_section_id
++;
962 se
->save_live_state
= save_live_state
;
963 se
->save_state
= save_state
;
964 se
->load_state
= load_state
;
968 if (instance_id
== -1) {
969 se
->instance_id
= calculate_new_instance_id(idstr
);
971 se
->instance_id
= instance_id
;
973 /* add at the end of list */
974 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
978 int register_savevm(const char *idstr
,
981 SaveStateHandler
*save_state
,
982 LoadStateHandler
*load_state
,
985 return register_savevm_live(idstr
, instance_id
, version_id
,
986 NULL
, save_state
, load_state
, opaque
);
989 void unregister_savevm(const char *idstr
, void *opaque
)
991 SaveStateEntry
*se
, *new_se
;
993 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
994 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
995 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1001 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1006 se
= qemu_malloc(sizeof(SaveStateEntry
));
1007 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1008 se
->version_id
= vmsd
->version_id
;
1009 se
->section_id
= global_section_id
++;
1010 se
->save_live_state
= NULL
;
1011 se
->save_state
= NULL
;
1012 se
->load_state
= NULL
;
1013 se
->opaque
= opaque
;
1016 if (instance_id
== -1) {
1017 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1019 se
->instance_id
= instance_id
;
1021 /* add at the end of list */
1022 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1026 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1028 SaveStateEntry
*se
, *new_se
;
1030 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1031 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1032 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1038 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1039 void *opaque
, int version_id
)
1041 VMStateField
*field
= vmsd
->fields
;
1043 if (version_id
> vmsd
->version_id
) {
1046 if (version_id
< vmsd
->minimum_version_id_old
) {
1049 if (version_id
< vmsd
->minimum_version_id
) {
1050 return vmsd
->load_state_old(f
, opaque
, version_id
);
1052 if (vmsd
->pre_load
) {
1053 int ret
= vmsd
->pre_load(opaque
);
1057 while(field
->name
) {
1058 if ((field
->field_exists
&&
1059 field
->field_exists(opaque
, version_id
)) ||
1060 (!field
->field_exists
&&
1061 field
->version_id
<= version_id
)) {
1062 void *base_addr
= opaque
+ field
->offset
;
1063 int ret
, i
, n_elems
= 1;
1065 if (field
->flags
& VMS_ARRAY
) {
1066 n_elems
= field
->num
;
1067 } else if (field
->flags
& VMS_VARRAY
) {
1068 n_elems
= *(size_t *)(opaque
+field
->num_offset
);
1070 if (field
->flags
& VMS_POINTER
) {
1071 base_addr
= *(void **)base_addr
;
1073 for (i
= 0; i
< n_elems
; i
++) {
1074 void *addr
= base_addr
+ field
->size
* i
;
1076 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1077 addr
= *(void **)addr
;
1079 if (field
->flags
& VMS_STRUCT
) {
1080 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1082 ret
= field
->info
->get(f
, addr
, field
->size
);
1092 if (vmsd
->post_load
) {
1093 return vmsd
->post_load(opaque
, version_id
);
1098 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1101 VMStateField
*field
= vmsd
->fields
;
1103 if (vmsd
->pre_save
) {
1104 vmsd
->pre_save(opaque
);
1106 while(field
->name
) {
1107 if (!field
->field_exists
||
1108 field
->field_exists(opaque
, vmsd
->version_id
)) {
1109 void *base_addr
= opaque
+ field
->offset
;
1112 if (field
->flags
& VMS_ARRAY
) {
1113 n_elems
= field
->num
;
1114 } else if (field
->flags
& VMS_VARRAY
) {
1115 n_elems
= *(size_t *)(opaque
+field
->num_offset
);
1117 if (field
->flags
& VMS_POINTER
) {
1118 base_addr
= *(void **)base_addr
;
1120 for (i
= 0; i
< n_elems
; i
++) {
1121 void *addr
= base_addr
+ field
->size
* i
;
1123 if (field
->flags
& VMS_STRUCT
) {
1124 vmstate_save_state(f
, field
->vmsd
, addr
);
1126 field
->info
->put(f
, addr
, field
->size
);
1132 if (vmsd
->post_save
) {
1133 vmsd
->post_save(opaque
);
1137 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1139 if (!se
->vmsd
) { /* Old style */
1140 return se
->load_state(f
, se
->opaque
, version_id
);
1142 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1145 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1147 if (!se
->vmsd
) { /* Old style */
1148 se
->save_state(f
, se
->opaque
);
1151 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1154 #define QEMU_VM_FILE_MAGIC 0x5145564d
1155 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1156 #define QEMU_VM_FILE_VERSION 0x00000003
1158 #define QEMU_VM_EOF 0x00
1159 #define QEMU_VM_SECTION_START 0x01
1160 #define QEMU_VM_SECTION_PART 0x02
1161 #define QEMU_VM_SECTION_END 0x03
1162 #define QEMU_VM_SECTION_FULL 0x04
1164 int qemu_savevm_state_begin(QEMUFile
*f
)
1168 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1169 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1171 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1174 if (se
->save_live_state
== NULL
)
1178 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1179 qemu_put_be32(f
, se
->section_id
);
1182 len
= strlen(se
->idstr
);
1183 qemu_put_byte(f
, len
);
1184 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1186 qemu_put_be32(f
, se
->instance_id
);
1187 qemu_put_be32(f
, se
->version_id
);
1189 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
1192 if (qemu_file_has_error(f
))
1198 int qemu_savevm_state_iterate(QEMUFile
*f
)
1203 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1204 if (se
->save_live_state
== NULL
)
1208 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1209 qemu_put_be32(f
, se
->section_id
);
1211 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1217 if (qemu_file_has_error(f
))
1223 int qemu_savevm_state_complete(QEMUFile
*f
)
1227 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1228 if (se
->save_live_state
== NULL
)
1232 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1233 qemu_put_be32(f
, se
->section_id
);
1235 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
1238 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1241 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1245 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1246 qemu_put_be32(f
, se
->section_id
);
1249 len
= strlen(se
->idstr
);
1250 qemu_put_byte(f
, len
);
1251 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1253 qemu_put_be32(f
, se
->instance_id
);
1254 qemu_put_be32(f
, se
->version_id
);
1256 vmstate_save(f
, se
);
1259 qemu_put_byte(f
, QEMU_VM_EOF
);
1261 if (qemu_file_has_error(f
))
1267 int qemu_savevm_state(QEMUFile
*f
)
1269 int saved_vm_running
;
1272 saved_vm_running
= vm_running
;
1277 ret
= qemu_savevm_state_begin(f
);
1282 ret
= qemu_savevm_state_iterate(f
);
1287 ret
= qemu_savevm_state_complete(f
);
1290 if (qemu_file_has_error(f
))
1293 if (!ret
&& saved_vm_running
)
1299 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1303 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1304 if (!strcmp(se
->idstr
, idstr
) &&
1305 instance_id
== se
->instance_id
)
1311 typedef struct LoadStateEntry
{
1312 QLIST_ENTRY(LoadStateEntry
) entry
;
1318 int qemu_loadvm_state(QEMUFile
*f
)
1320 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1321 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1322 LoadStateEntry
*le
, *new_le
;
1323 uint8_t section_type
;
1327 v
= qemu_get_be32(f
);
1328 if (v
!= QEMU_VM_FILE_MAGIC
)
1331 v
= qemu_get_be32(f
);
1332 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1333 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1336 if (v
!= QEMU_VM_FILE_VERSION
)
1339 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1340 uint32_t instance_id
, version_id
, section_id
;
1345 switch (section_type
) {
1346 case QEMU_VM_SECTION_START
:
1347 case QEMU_VM_SECTION_FULL
:
1348 /* Read section start */
1349 section_id
= qemu_get_be32(f
);
1350 len
= qemu_get_byte(f
);
1351 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1353 instance_id
= qemu_get_be32(f
);
1354 version_id
= qemu_get_be32(f
);
1356 /* Find savevm section */
1357 se
= find_se(idstr
, instance_id
);
1359 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1364 /* Validate version */
1365 if (version_id
> se
->version_id
) {
1366 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1367 version_id
, idstr
, se
->version_id
);
1373 le
= qemu_mallocz(sizeof(*le
));
1376 le
->section_id
= section_id
;
1377 le
->version_id
= version_id
;
1378 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1380 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1382 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1383 instance_id
, idstr
);
1387 case QEMU_VM_SECTION_PART
:
1388 case QEMU_VM_SECTION_END
:
1389 section_id
= qemu_get_be32(f
);
1391 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1392 if (le
->section_id
== section_id
) {
1397 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1402 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1404 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1410 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1419 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1420 QLIST_REMOVE(le
, entry
);
1424 if (qemu_file_has_error(f
))
1430 /* device can contain snapshots */
1431 static int bdrv_can_snapshot(BlockDriverState
*bs
)
1434 !bdrv_is_removable(bs
) &&
1435 !bdrv_is_read_only(bs
));
1438 /* device must be snapshots in order to have a reliable snapshot */
1439 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1442 !bdrv_is_removable(bs
) &&
1443 !bdrv_is_read_only(bs
));
1446 static BlockDriverState
*get_bs_snapshots(void)
1448 BlockDriverState
*bs
;
1452 return bs_snapshots
;
1453 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1455 if (bdrv_can_snapshot(bs
))
1464 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1467 QEMUSnapshotInfo
*sn_tab
, *sn
;
1471 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1474 for(i
= 0; i
< nb_sns
; i
++) {
1476 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1486 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1489 BlockDriverState
*bs
, *bs1
;
1490 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1491 int must_delete
, ret
;
1493 int saved_vm_running
;
1494 uint32_t vm_state_size
;
1500 const char *name
= qdict_get_try_str(qdict
, "name");
1502 bs
= get_bs_snapshots();
1504 monitor_printf(mon
, "No block device can accept snapshots\n");
1508 /* ??? Should this occur after vm_stop? */
1511 saved_vm_running
= vm_running
;
1516 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1521 memset(sn
, 0, sizeof(*sn
));
1523 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1524 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1527 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1530 /* fill auxiliary fields */
1533 sn
->date_sec
= tb
.time
;
1534 sn
->date_nsec
= tb
.millitm
* 1000000;
1536 gettimeofday(&tv
, NULL
);
1537 sn
->date_sec
= tv
.tv_sec
;
1538 sn
->date_nsec
= tv
.tv_usec
* 1000;
1540 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1542 /* save the VM state */
1543 f
= qemu_fopen_bdrv(bs
, 1);
1545 monitor_printf(mon
, "Could not open VM state file\n");
1548 ret
= qemu_savevm_state(f
);
1549 vm_state_size
= qemu_ftell(f
);
1552 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1556 /* create the snapshots */
1558 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1560 if (bdrv_has_snapshot(bs1
)) {
1562 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1565 "Error while deleting snapshot on '%s'\n",
1566 bdrv_get_device_name(bs1
));
1569 /* Write VM state size only to the image that contains the state */
1570 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1571 ret
= bdrv_snapshot_create(bs1
, sn
);
1573 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1574 bdrv_get_device_name(bs1
));
1580 if (saved_vm_running
)
1584 int load_vmstate(Monitor
*mon
, const char *name
)
1587 BlockDriverState
*bs
, *bs1
;
1588 QEMUSnapshotInfo sn
;
1592 bs
= get_bs_snapshots();
1594 monitor_printf(mon
, "No block device supports snapshots\n");
1598 /* Flush all IO requests so they don't interfere with the new state. */
1601 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1603 if (bdrv_has_snapshot(bs1
)) {
1604 ret
= bdrv_snapshot_goto(bs1
, name
);
1607 monitor_printf(mon
, "Warning: ");
1611 "Snapshots not supported on device '%s'\n",
1612 bdrv_get_device_name(bs1
));
1615 monitor_printf(mon
, "Could not find snapshot '%s' on "
1617 name
, bdrv_get_device_name(bs1
));
1620 monitor_printf(mon
, "Error %d while activating snapshot on"
1621 " '%s'\n", ret
, bdrv_get_device_name(bs1
));
1624 /* fatal on snapshot block device */
1631 /* Don't even try to load empty VM states */
1632 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1633 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1636 /* restore the VM state */
1637 f
= qemu_fopen_bdrv(bs
, 0);
1639 monitor_printf(mon
, "Could not open VM state file\n");
1642 ret
= qemu_loadvm_state(f
);
1645 monitor_printf(mon
, "Error %d while loading VM state\n", ret
);
1651 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1654 BlockDriverState
*bs
, *bs1
;
1656 const char *name
= qdict_get_str(qdict
, "name");
1658 bs
= get_bs_snapshots();
1660 monitor_printf(mon
, "No block device supports snapshots\n");
1664 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1666 if (bdrv_has_snapshot(bs1
)) {
1667 ret
= bdrv_snapshot_delete(bs1
, name
);
1669 if (ret
== -ENOTSUP
)
1671 "Snapshots not supported on device '%s'\n",
1672 bdrv_get_device_name(bs1
));
1674 monitor_printf(mon
, "Error %d while deleting snapshot on "
1675 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1681 void do_info_snapshots(Monitor
*mon
)
1684 BlockDriverState
*bs
, *bs1
;
1685 QEMUSnapshotInfo
*sn_tab
, *sn
;
1689 bs
= get_bs_snapshots();
1691 monitor_printf(mon
, "No available block device supports snapshots\n");
1694 monitor_printf(mon
, "Snapshot devices:");
1695 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1697 if (bdrv_has_snapshot(bs1
)) {
1699 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1702 monitor_printf(mon
, "\n");
1704 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1706 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1709 monitor_printf(mon
, "Snapshot list (from %s):\n",
1710 bdrv_get_device_name(bs
));
1711 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1712 for(i
= 0; i
< nb_sns
; i
++) {
1714 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));