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
)
130 static int count
= SELF_ANNOUNCE_ROUNDS
;
131 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
133 for (i
= 0; i
< MAX_NICS
; i
++) {
134 if (!nd_table
[i
].used
)
136 len
= announce_self_create(buf
, nd_table
[i
].macaddr
);
137 qemu_send_packet_raw(nd_table
[i
].vc
, buf
, len
);
140 /* delay 50ms, 150ms, 250ms, ... */
141 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) +
142 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
144 qemu_del_timer(timer
);
145 qemu_free_timer(timer
);
149 void qemu_announce_self(void)
151 static QEMUTimer
*timer
;
152 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
153 qemu_announce_self_once(&timer
);
156 /***********************************************************/
157 /* savevm/loadvm support */
159 #define IO_BUF_SIZE 32768
162 QEMUFilePutBufferFunc
*put_buffer
;
163 QEMUFileGetBufferFunc
*get_buffer
;
164 QEMUFileCloseFunc
*close
;
165 QEMUFileRateLimit
*rate_limit
;
166 QEMUFileSetRateLimit
*set_rate_limit
;
170 int64_t buf_offset
; /* start of buffer when writing, end of buffer
173 int buf_size
; /* 0 when writing */
174 uint8_t buf
[IO_BUF_SIZE
];
179 typedef struct QEMUFileStdio
185 typedef struct QEMUFileSocket
191 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
193 QEMUFileSocket
*s
= opaque
;
197 len
= recv(s
->fd
, (void *)buf
, size
, 0);
198 } while (len
== -1 && socket_error() == EINTR
);
201 len
= -socket_error();
206 static int socket_close(void *opaque
)
208 QEMUFileSocket
*s
= opaque
;
213 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
215 QEMUFileStdio
*s
= opaque
;
216 return fwrite(buf
, 1, size
, s
->stdio_file
);
219 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
221 QEMUFileStdio
*s
= opaque
;
222 FILE *fp
= s
->stdio_file
;
227 bytes
= fread(buf
, 1, size
, fp
);
228 } while ((bytes
== 0) && ferror(fp
) && (errno
== EINTR
));
232 static int stdio_pclose(void *opaque
)
234 QEMUFileStdio
*s
= opaque
;
235 pclose(s
->stdio_file
);
240 static int stdio_fclose(void *opaque
)
242 QEMUFileStdio
*s
= opaque
;
243 fclose(s
->stdio_file
);
248 QEMUFile
*qemu_popen(FILE *stdio_file
, const char *mode
)
252 if (stdio_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
253 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
257 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
259 s
->stdio_file
= stdio_file
;
262 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_pclose
, NULL
, NULL
);
264 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_pclose
, NULL
, NULL
);
269 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
273 popen_file
= popen(command
, mode
);
274 if(popen_file
== NULL
) {
278 return qemu_popen(popen_file
, mode
);
281 int qemu_stdio_fd(QEMUFile
*f
)
286 p
= (QEMUFileStdio
*)f
->opaque
;
287 fd
= fileno(p
->stdio_file
);
292 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
297 (mode
[0] != 'r' && mode
[0] != 'w') ||
298 mode
[1] != 'b' || mode
[2] != 0) {
299 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
303 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
304 s
->stdio_file
= fdopen(fd
, mode
);
309 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_fclose
, NULL
, NULL
);
311 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
320 QEMUFile
*qemu_fopen_socket(int fd
)
322 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
325 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
, NULL
, NULL
);
329 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
330 int64_t pos
, int size
)
332 QEMUFileStdio
*s
= opaque
;
333 fseek(s
->stdio_file
, pos
, SEEK_SET
);
334 fwrite(buf
, 1, size
, s
->stdio_file
);
338 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
340 QEMUFileStdio
*s
= opaque
;
341 fseek(s
->stdio_file
, pos
, SEEK_SET
);
342 return fread(buf
, 1, size
, s
->stdio_file
);
345 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
350 (mode
[0] != 'r' && mode
[0] != 'w') ||
351 mode
[1] != 'b' || mode
[2] != 0) {
352 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
356 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
358 s
->stdio_file
= fopen(filename
, mode
);
363 s
->file
= qemu_fopen_ops(s
, file_put_buffer
, NULL
, stdio_fclose
, NULL
, NULL
);
365 s
->file
= qemu_fopen_ops(s
, NULL
, file_get_buffer
, stdio_fclose
, NULL
, NULL
);
373 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
374 int64_t pos
, int size
)
376 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
380 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
382 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
385 static int bdrv_fclose(void *opaque
)
390 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
393 return qemu_fopen_ops(bs
, block_put_buffer
, NULL
, bdrv_fclose
, NULL
, NULL
);
394 return qemu_fopen_ops(bs
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
, NULL
);
397 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
398 QEMUFileGetBufferFunc
*get_buffer
,
399 QEMUFileCloseFunc
*close
,
400 QEMUFileRateLimit
*rate_limit
,
401 QEMUFileSetRateLimit
*set_rate_limit
)
405 f
= qemu_mallocz(sizeof(QEMUFile
));
408 f
->put_buffer
= put_buffer
;
409 f
->get_buffer
= get_buffer
;
411 f
->rate_limit
= rate_limit
;
412 f
->set_rate_limit
= set_rate_limit
;
418 int qemu_file_has_error(QEMUFile
*f
)
423 void qemu_file_set_error(QEMUFile
*f
)
428 void qemu_fflush(QEMUFile
*f
)
433 if (f
->is_write
&& f
->buf_index
> 0) {
436 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
438 f
->buf_offset
+= f
->buf_index
;
445 static void qemu_fill_buffer(QEMUFile
*f
)
455 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
459 f
->buf_offset
+= len
;
460 } else if (len
!= -EAGAIN
)
464 int qemu_fclose(QEMUFile
*f
)
469 ret
= f
->close(f
->opaque
);
474 void qemu_file_put_notify(QEMUFile
*f
)
476 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
479 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
483 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
485 "Attempted to write to buffer while read buffer is not empty\n");
489 while (!f
->has_error
&& size
> 0) {
490 l
= IO_BUF_SIZE
- f
->buf_index
;
493 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
498 if (f
->buf_index
>= IO_BUF_SIZE
)
503 void qemu_put_byte(QEMUFile
*f
, int v
)
505 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
507 "Attempted to write to buffer while read buffer is not empty\n");
511 f
->buf
[f
->buf_index
++] = v
;
513 if (f
->buf_index
>= IO_BUF_SIZE
)
517 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
526 l
= f
->buf_size
- f
->buf_index
;
529 l
= f
->buf_size
- f
->buf_index
;
535 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
543 int qemu_get_byte(QEMUFile
*f
)
548 if (f
->buf_index
>= f
->buf_size
) {
550 if (f
->buf_index
>= f
->buf_size
)
553 return f
->buf
[f
->buf_index
++];
556 int64_t qemu_ftell(QEMUFile
*f
)
558 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
561 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
563 if (whence
== SEEK_SET
) {
565 } else if (whence
== SEEK_CUR
) {
566 pos
+= qemu_ftell(f
);
568 /* SEEK_END not supported */
582 int qemu_file_rate_limit(QEMUFile
*f
)
585 return f
->rate_limit(f
->opaque
);
590 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
)
592 /* any failed or completed migration keeps its state to allow probing of
593 * migration data, but has no associated file anymore */
594 if (f
&& f
->set_rate_limit
)
595 return f
->set_rate_limit(f
->opaque
, new_rate
);
600 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
602 qemu_put_byte(f
, v
>> 8);
606 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
608 qemu_put_byte(f
, v
>> 24);
609 qemu_put_byte(f
, v
>> 16);
610 qemu_put_byte(f
, v
>> 8);
614 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
616 qemu_put_be32(f
, v
>> 32);
620 unsigned int qemu_get_be16(QEMUFile
*f
)
623 v
= qemu_get_byte(f
) << 8;
624 v
|= qemu_get_byte(f
);
628 unsigned int qemu_get_be32(QEMUFile
*f
)
631 v
= qemu_get_byte(f
) << 24;
632 v
|= qemu_get_byte(f
) << 16;
633 v
|= qemu_get_byte(f
) << 8;
634 v
|= qemu_get_byte(f
);
638 uint64_t qemu_get_be64(QEMUFile
*f
)
641 v
= (uint64_t)qemu_get_be32(f
) << 32;
642 v
|= qemu_get_be32(f
);
648 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
655 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
661 const VMStateInfo vmstate_info_int8
= {
669 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
672 qemu_get_sbe16s(f
, v
);
676 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
679 qemu_put_sbe16s(f
, v
);
682 const VMStateInfo vmstate_info_int16
= {
690 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
693 qemu_get_sbe32s(f
, v
);
697 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
700 qemu_put_sbe32s(f
, v
);
703 const VMStateInfo vmstate_info_int32
= {
709 /* 32 bit int. See that the received value is the same than the one
712 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
716 qemu_get_sbe32s(f
, &v2
);
723 const VMStateInfo vmstate_info_int32_equal
= {
724 .name
= "int32 equal",
725 .get
= get_int32_equal
,
729 /* 32 bit int. See that the received value is the less or the same
730 than the one in the field */
732 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
736 qemu_get_sbe32s(f
, &new);
743 const VMStateInfo vmstate_info_int32_le
= {
744 .name
= "int32 equal",
751 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
754 qemu_get_sbe64s(f
, v
);
758 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
761 qemu_put_sbe64s(f
, v
);
764 const VMStateInfo vmstate_info_int64
= {
770 /* 8 bit unsigned int */
772 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
779 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
785 const VMStateInfo vmstate_info_uint8
= {
791 /* 16 bit unsigned int */
793 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
796 qemu_get_be16s(f
, v
);
800 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
803 qemu_put_be16s(f
, v
);
806 const VMStateInfo vmstate_info_uint16
= {
812 /* 32 bit unsigned int */
814 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
817 qemu_get_be32s(f
, v
);
821 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
824 qemu_put_be32s(f
, v
);
827 const VMStateInfo vmstate_info_uint32
= {
833 /* 64 bit unsigned int */
835 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
838 qemu_get_be64s(f
, v
);
842 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
845 qemu_put_be64s(f
, v
);
848 const VMStateInfo vmstate_info_uint64
= {
854 /* 64 bit linux kernel unsigned int */
857 static int get_u64(QEMUFile
*f
, void *pv
, size_t size
)
860 qemu_get_be64s(f
, v
);
864 static void put_u64(QEMUFile
*f
, void *pv
, size_t size
)
867 qemu_put_be64s(f
, v
);
870 const VMStateInfo vmstate_info_u64
= {
875 #endif /* __linux__ */
877 /* 8 bit int. See that the received value is the same than the one
880 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
891 const VMStateInfo vmstate_info_uint8_equal
= {
892 .name
= "uint8 equal",
893 .get
= get_uint8_equal
,
897 /* 16 bit unsigned int int. See that the received value is the same than the one
900 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
904 qemu_get_be16s(f
, &v2
);
911 const VMStateInfo vmstate_info_uint16_equal
= {
912 .name
= "uint16 equal",
913 .get
= get_uint16_equal
,
919 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
922 qemu_get_timer(f
, v
);
926 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
929 qemu_put_timer(f
, v
);
932 const VMStateInfo vmstate_info_timer
= {
938 /* uint8_t buffers */
940 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
943 qemu_get_buffer(f
, v
, size
);
947 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
950 qemu_put_buffer(f
, v
, size
);
953 const VMStateInfo vmstate_info_buffer
= {
959 /* unused buffers: space that was used for some fields that are
960 not usefull anymore */
962 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
964 qemu_fseek(f
, size
, SEEK_CUR
);
968 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
970 qemu_fseek(f
, size
, SEEK_CUR
);
973 const VMStateInfo vmstate_info_unused_buffer
= {
974 .name
= "unused_buffer",
975 .get
= get_unused_buffer
,
976 .put
= put_unused_buffer
,
979 typedef struct SaveStateEntry
{
980 QTAILQ_ENTRY(SaveStateEntry
) entry
;
985 SaveLiveStateHandler
*save_live_state
;
986 SaveStateHandler
*save_state
;
987 LoadStateHandler
*load_state
;
988 const VMStateDescription
*vmsd
;
992 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
993 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
994 static int global_section_id
;
996 static int calculate_new_instance_id(const char *idstr
)
1001 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1002 if (strcmp(idstr
, se
->idstr
) == 0
1003 && instance_id
<= se
->instance_id
) {
1004 instance_id
= se
->instance_id
+ 1;
1010 /* TODO: Individual devices generally have very little idea about the rest
1011 of the system, so instance_id should be removed/replaced.
1012 Meanwhile pass -1 as instance_id if you do not already have a clearly
1013 distinguishing id for all instances of your device class. */
1014 int register_savevm_live(const char *idstr
,
1017 SaveLiveStateHandler
*save_live_state
,
1018 SaveStateHandler
*save_state
,
1019 LoadStateHandler
*load_state
,
1024 se
= qemu_malloc(sizeof(SaveStateEntry
));
1025 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1026 se
->version_id
= version_id
;
1027 se
->section_id
= global_section_id
++;
1028 se
->save_live_state
= save_live_state
;
1029 se
->save_state
= save_state
;
1030 se
->load_state
= load_state
;
1031 se
->opaque
= opaque
;
1034 if (instance_id
== -1) {
1035 se
->instance_id
= calculate_new_instance_id(idstr
);
1037 se
->instance_id
= instance_id
;
1039 /* add at the end of list */
1040 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1044 int register_savevm(const char *idstr
,
1047 SaveStateHandler
*save_state
,
1048 LoadStateHandler
*load_state
,
1051 return register_savevm_live(idstr
, instance_id
, version_id
,
1052 NULL
, save_state
, load_state
, opaque
);
1055 void unregister_savevm(const char *idstr
, void *opaque
)
1057 SaveStateEntry
*se
, *new_se
;
1059 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1060 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
1061 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1067 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1072 se
= qemu_malloc(sizeof(SaveStateEntry
));
1073 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1074 se
->version_id
= vmsd
->version_id
;
1075 se
->section_id
= global_section_id
++;
1076 se
->save_live_state
= NULL
;
1077 se
->save_state
= NULL
;
1078 se
->load_state
= NULL
;
1079 se
->opaque
= opaque
;
1082 if (instance_id
== -1) {
1083 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1085 se
->instance_id
= instance_id
;
1087 /* add at the end of list */
1088 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1092 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1094 SaveStateEntry
*se
, *new_se
;
1096 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1097 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1098 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1104 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1105 void *opaque
, int version_id
)
1107 VMStateField
*field
= vmsd
->fields
;
1109 if (version_id
> vmsd
->version_id
) {
1112 if (version_id
< vmsd
->minimum_version_id_old
) {
1115 if (version_id
< vmsd
->minimum_version_id
) {
1116 return vmsd
->load_state_old(f
, opaque
, version_id
);
1118 if (vmsd
->pre_load
) {
1119 int ret
= vmsd
->pre_load(opaque
);
1123 while(field
->name
) {
1124 if ((field
->field_exists
&&
1125 field
->field_exists(opaque
, version_id
)) ||
1126 (!field
->field_exists
&&
1127 field
->version_id
<= version_id
)) {
1128 void *base_addr
= opaque
+ field
->offset
;
1129 int ret
, i
, n_elems
= 1;
1131 if (field
->flags
& VMS_ARRAY
) {
1132 n_elems
= field
->num
;
1133 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1134 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1135 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1136 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1138 if (field
->flags
& VMS_POINTER
) {
1139 base_addr
= *(void **)base_addr
;
1141 for (i
= 0; i
< n_elems
; i
++) {
1142 void *addr
= base_addr
+ field
->size
* i
;
1144 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1145 addr
= *(void **)addr
;
1147 if (field
->flags
& VMS_STRUCT
) {
1148 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1150 ret
= field
->info
->get(f
, addr
, field
->size
);
1160 if (vmsd
->post_load
) {
1161 return vmsd
->post_load(opaque
, version_id
);
1166 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1169 VMStateField
*field
= vmsd
->fields
;
1171 if (vmsd
->pre_save
) {
1172 vmsd
->pre_save(opaque
);
1174 while(field
->name
) {
1175 if (!field
->field_exists
||
1176 field
->field_exists(opaque
, vmsd
->version_id
)) {
1177 void *base_addr
= opaque
+ field
->offset
;
1180 if (field
->flags
& VMS_ARRAY
) {
1181 n_elems
= field
->num
;
1182 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1183 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1184 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1185 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1187 if (field
->flags
& VMS_POINTER
) {
1188 base_addr
= *(void **)base_addr
;
1190 for (i
= 0; i
< n_elems
; i
++) {
1191 void *addr
= base_addr
+ field
->size
* i
;
1193 if (field
->flags
& VMS_STRUCT
) {
1194 vmstate_save_state(f
, field
->vmsd
, addr
);
1196 field
->info
->put(f
, addr
, field
->size
);
1202 if (vmsd
->post_save
) {
1203 vmsd
->post_save(opaque
);
1207 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1209 if (!se
->vmsd
) { /* Old style */
1210 return se
->load_state(f
, se
->opaque
, version_id
);
1212 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1215 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1217 if (!se
->vmsd
) { /* Old style */
1218 se
->save_state(f
, se
->opaque
);
1221 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1224 #define QEMU_VM_FILE_MAGIC 0x5145564d
1225 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1226 #define QEMU_VM_FILE_VERSION 0x00000003
1228 #define QEMU_VM_EOF 0x00
1229 #define QEMU_VM_SECTION_START 0x01
1230 #define QEMU_VM_SECTION_PART 0x02
1231 #define QEMU_VM_SECTION_END 0x03
1232 #define QEMU_VM_SECTION_FULL 0x04
1234 int qemu_savevm_state_begin(QEMUFile
*f
)
1238 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1239 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1241 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1244 if (se
->save_live_state
== NULL
)
1248 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1249 qemu_put_be32(f
, se
->section_id
);
1252 len
= strlen(se
->idstr
);
1253 qemu_put_byte(f
, len
);
1254 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1256 qemu_put_be32(f
, se
->instance_id
);
1257 qemu_put_be32(f
, se
->version_id
);
1259 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
1262 if (qemu_file_has_error(f
))
1268 int qemu_savevm_state_iterate(QEMUFile
*f
)
1273 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1274 if (se
->save_live_state
== NULL
)
1278 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1279 qemu_put_be32(f
, se
->section_id
);
1281 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1287 if (qemu_file_has_error(f
))
1293 int qemu_savevm_state_complete(QEMUFile
*f
)
1297 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1298 if (se
->save_live_state
== NULL
)
1302 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1303 qemu_put_be32(f
, se
->section_id
);
1305 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
1308 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1311 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1315 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1316 qemu_put_be32(f
, se
->section_id
);
1319 len
= strlen(se
->idstr
);
1320 qemu_put_byte(f
, len
);
1321 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1323 qemu_put_be32(f
, se
->instance_id
);
1324 qemu_put_be32(f
, se
->version_id
);
1326 vmstate_save(f
, se
);
1329 qemu_put_byte(f
, QEMU_VM_EOF
);
1331 if (qemu_file_has_error(f
))
1337 int qemu_savevm_state(QEMUFile
*f
)
1339 int saved_vm_running
;
1342 saved_vm_running
= vm_running
;
1347 ret
= qemu_savevm_state_begin(f
);
1352 ret
= qemu_savevm_state_iterate(f
);
1357 ret
= qemu_savevm_state_complete(f
);
1360 if (qemu_file_has_error(f
))
1363 if (!ret
&& saved_vm_running
)
1369 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1373 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1374 if (!strcmp(se
->idstr
, idstr
) &&
1375 instance_id
== se
->instance_id
)
1381 typedef struct LoadStateEntry
{
1382 QLIST_ENTRY(LoadStateEntry
) entry
;
1388 int qemu_loadvm_state(QEMUFile
*f
)
1390 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1391 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1392 LoadStateEntry
*le
, *new_le
;
1393 uint8_t section_type
;
1397 v
= qemu_get_be32(f
);
1398 if (v
!= QEMU_VM_FILE_MAGIC
)
1401 v
= qemu_get_be32(f
);
1402 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1403 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1406 if (v
!= QEMU_VM_FILE_VERSION
)
1409 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1410 uint32_t instance_id
, version_id
, section_id
;
1415 switch (section_type
) {
1416 case QEMU_VM_SECTION_START
:
1417 case QEMU_VM_SECTION_FULL
:
1418 /* Read section start */
1419 section_id
= qemu_get_be32(f
);
1420 len
= qemu_get_byte(f
);
1421 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1423 instance_id
= qemu_get_be32(f
);
1424 version_id
= qemu_get_be32(f
);
1426 /* Find savevm section */
1427 se
= find_se(idstr
, instance_id
);
1429 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1434 /* Validate version */
1435 if (version_id
> se
->version_id
) {
1436 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1437 version_id
, idstr
, se
->version_id
);
1443 le
= qemu_mallocz(sizeof(*le
));
1446 le
->section_id
= section_id
;
1447 le
->version_id
= version_id
;
1448 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1450 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1452 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1453 instance_id
, idstr
);
1457 case QEMU_VM_SECTION_PART
:
1458 case QEMU_VM_SECTION_END
:
1459 section_id
= qemu_get_be32(f
);
1461 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1462 if (le
->section_id
== section_id
) {
1467 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1472 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1474 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1480 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1489 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1490 QLIST_REMOVE(le
, entry
);
1494 if (qemu_file_has_error(f
))
1500 /* device can contain snapshots */
1501 static int bdrv_can_snapshot(BlockDriverState
*bs
)
1504 !bdrv_is_removable(bs
) &&
1505 !bdrv_is_read_only(bs
));
1508 /* device must be snapshots in order to have a reliable snapshot */
1509 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1512 !bdrv_is_removable(bs
) &&
1513 !bdrv_is_read_only(bs
));
1516 static BlockDriverState
*get_bs_snapshots(void)
1518 BlockDriverState
*bs
;
1522 return bs_snapshots
;
1523 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1525 if (bdrv_can_snapshot(bs
))
1534 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1537 QEMUSnapshotInfo
*sn_tab
, *sn
;
1541 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1544 for(i
= 0; i
< nb_sns
; i
++) {
1546 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1556 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1559 BlockDriverState
*bs
, *bs1
;
1560 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1561 int must_delete
, ret
;
1563 int saved_vm_running
;
1564 uint32_t vm_state_size
;
1570 const char *name
= qdict_get_try_str(qdict
, "name");
1572 bs
= get_bs_snapshots();
1574 monitor_printf(mon
, "No block device can accept snapshots\n");
1578 /* ??? Should this occur after vm_stop? */
1581 saved_vm_running
= vm_running
;
1586 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1591 memset(sn
, 0, sizeof(*sn
));
1593 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1594 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1597 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1600 /* fill auxiliary fields */
1603 sn
->date_sec
= tb
.time
;
1604 sn
->date_nsec
= tb
.millitm
* 1000000;
1606 gettimeofday(&tv
, NULL
);
1607 sn
->date_sec
= tv
.tv_sec
;
1608 sn
->date_nsec
= tv
.tv_usec
* 1000;
1610 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1612 /* save the VM state */
1613 f
= qemu_fopen_bdrv(bs
, 1);
1615 monitor_printf(mon
, "Could not open VM state file\n");
1618 ret
= qemu_savevm_state(f
);
1619 vm_state_size
= qemu_ftell(f
);
1622 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1626 /* create the snapshots */
1628 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1630 if (bdrv_has_snapshot(bs1
)) {
1632 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1635 "Error while deleting snapshot on '%s'\n",
1636 bdrv_get_device_name(bs1
));
1639 /* Write VM state size only to the image that contains the state */
1640 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1641 ret
= bdrv_snapshot_create(bs1
, sn
);
1643 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1644 bdrv_get_device_name(bs1
));
1650 if (saved_vm_running
)
1654 int load_vmstate(Monitor
*mon
, const char *name
)
1657 BlockDriverState
*bs
, *bs1
;
1658 QEMUSnapshotInfo sn
;
1662 bs
= get_bs_snapshots();
1664 monitor_printf(mon
, "No block device supports snapshots\n");
1668 /* Flush all IO requests so they don't interfere with the new state. */
1671 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1673 if (bdrv_has_snapshot(bs1
)) {
1674 ret
= bdrv_snapshot_goto(bs1
, name
);
1677 monitor_printf(mon
, "Warning: ");
1681 "Snapshots not supported on device '%s'\n",
1682 bdrv_get_device_name(bs1
));
1685 monitor_printf(mon
, "Could not find snapshot '%s' on "
1687 name
, bdrv_get_device_name(bs1
));
1690 monitor_printf(mon
, "Error %d while activating snapshot on"
1691 " '%s'\n", ret
, bdrv_get_device_name(bs1
));
1694 /* fatal on snapshot block device */
1701 /* Don't even try to load empty VM states */
1702 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1703 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1706 /* restore the VM state */
1707 f
= qemu_fopen_bdrv(bs
, 0);
1709 monitor_printf(mon
, "Could not open VM state file\n");
1712 ret
= qemu_loadvm_state(f
);
1715 monitor_printf(mon
, "Error %d while loading VM state\n", ret
);
1721 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1724 BlockDriverState
*bs
, *bs1
;
1726 const char *name
= qdict_get_str(qdict
, "name");
1728 bs
= get_bs_snapshots();
1730 monitor_printf(mon
, "No block device supports snapshots\n");
1734 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1736 if (bdrv_has_snapshot(bs1
)) {
1737 ret
= bdrv_snapshot_delete(bs1
, name
);
1739 if (ret
== -ENOTSUP
)
1741 "Snapshots not supported on device '%s'\n",
1742 bdrv_get_device_name(bs1
));
1744 monitor_printf(mon
, "Error %d while deleting snapshot on "
1745 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1751 void do_info_snapshots(Monitor
*mon
)
1754 BlockDriverState
*bs
, *bs1
;
1755 QEMUSnapshotInfo
*sn_tab
, *sn
;
1759 bs
= get_bs_snapshots();
1761 monitor_printf(mon
, "No available block device supports snapshots\n");
1764 monitor_printf(mon
, "Snapshot devices:");
1765 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1767 if (bdrv_has_snapshot(bs1
)) {
1769 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1772 monitor_printf(mon
, "\n");
1774 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1776 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1779 monitor_printf(mon
, "Snapshot list (from %s):\n",
1780 bdrv_get_device_name(bs
));
1781 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1782 for(i
= 0; i
< nb_sns
; i
++) {
1784 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));