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 /* 8 bit int. See that the received value is the same than the one
857 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
868 const VMStateInfo vmstate_info_uint8_equal
= {
869 .name
= "uint8 equal",
870 .get
= get_uint8_equal
,
874 /* 16 bit unsigned int int. See that the received value is the same than the one
877 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
881 qemu_get_be16s(f
, &v2
);
888 const VMStateInfo vmstate_info_uint16_equal
= {
889 .name
= "uint16 equal",
890 .get
= get_uint16_equal
,
896 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
899 qemu_get_timer(f
, v
);
903 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
906 qemu_put_timer(f
, v
);
909 const VMStateInfo vmstate_info_timer
= {
915 /* uint8_t buffers */
917 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
920 qemu_get_buffer(f
, v
, size
);
924 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
927 qemu_put_buffer(f
, v
, size
);
930 const VMStateInfo vmstate_info_buffer
= {
936 /* unused buffers: space that was used for some fields that are
937 not usefull anymore */
939 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
941 qemu_fseek(f
, size
, SEEK_CUR
);
945 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
947 qemu_fseek(f
, size
, SEEK_CUR
);
950 const VMStateInfo vmstate_info_unused_buffer
= {
951 .name
= "unused_buffer",
952 .get
= get_unused_buffer
,
953 .put
= put_unused_buffer
,
956 typedef struct SaveStateEntry
{
957 QTAILQ_ENTRY(SaveStateEntry
) entry
;
962 SaveLiveStateHandler
*save_live_state
;
963 SaveStateHandler
*save_state
;
964 LoadStateHandler
*load_state
;
965 const VMStateDescription
*vmsd
;
969 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
970 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
971 static int global_section_id
;
973 static int calculate_new_instance_id(const char *idstr
)
978 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
979 if (strcmp(idstr
, se
->idstr
) == 0
980 && instance_id
<= se
->instance_id
) {
981 instance_id
= se
->instance_id
+ 1;
987 /* TODO: Individual devices generally have very little idea about the rest
988 of the system, so instance_id should be removed/replaced.
989 Meanwhile pass -1 as instance_id if you do not already have a clearly
990 distinguishing id for all instances of your device class. */
991 int register_savevm_live(const char *idstr
,
994 SaveLiveStateHandler
*save_live_state
,
995 SaveStateHandler
*save_state
,
996 LoadStateHandler
*load_state
,
1001 se
= qemu_malloc(sizeof(SaveStateEntry
));
1002 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1003 se
->version_id
= version_id
;
1004 se
->section_id
= global_section_id
++;
1005 se
->save_live_state
= save_live_state
;
1006 se
->save_state
= save_state
;
1007 se
->load_state
= load_state
;
1008 se
->opaque
= opaque
;
1011 if (instance_id
== -1) {
1012 se
->instance_id
= calculate_new_instance_id(idstr
);
1014 se
->instance_id
= instance_id
;
1016 /* add at the end of list */
1017 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1021 int register_savevm(const char *idstr
,
1024 SaveStateHandler
*save_state
,
1025 LoadStateHandler
*load_state
,
1028 return register_savevm_live(idstr
, instance_id
, version_id
,
1029 NULL
, save_state
, load_state
, opaque
);
1032 void unregister_savevm(const char *idstr
, void *opaque
)
1034 SaveStateEntry
*se
, *new_se
;
1036 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1037 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
1038 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1044 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1049 se
= qemu_malloc(sizeof(SaveStateEntry
));
1050 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1051 se
->version_id
= vmsd
->version_id
;
1052 se
->section_id
= global_section_id
++;
1053 se
->save_live_state
= NULL
;
1054 se
->save_state
= NULL
;
1055 se
->load_state
= NULL
;
1056 se
->opaque
= opaque
;
1059 if (instance_id
== -1) {
1060 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1062 se
->instance_id
= instance_id
;
1064 /* add at the end of list */
1065 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1069 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1071 SaveStateEntry
*se
, *new_se
;
1073 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1074 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1075 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1081 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1082 void *opaque
, int version_id
)
1084 VMStateField
*field
= vmsd
->fields
;
1086 if (version_id
> vmsd
->version_id
) {
1089 if (version_id
< vmsd
->minimum_version_id_old
) {
1092 if (version_id
< vmsd
->minimum_version_id
) {
1093 return vmsd
->load_state_old(f
, opaque
, version_id
);
1095 if (vmsd
->pre_load
) {
1096 int ret
= vmsd
->pre_load(opaque
);
1100 while(field
->name
) {
1101 if ((field
->field_exists
&&
1102 field
->field_exists(opaque
, version_id
)) ||
1103 (!field
->field_exists
&&
1104 field
->version_id
<= version_id
)) {
1105 void *base_addr
= opaque
+ field
->offset
;
1106 int ret
, i
, n_elems
= 1;
1108 if (field
->flags
& VMS_ARRAY
) {
1109 n_elems
= field
->num
;
1110 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1111 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1112 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1113 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1115 if (field
->flags
& VMS_POINTER
) {
1116 base_addr
= *(void **)base_addr
;
1118 for (i
= 0; i
< n_elems
; i
++) {
1119 void *addr
= base_addr
+ field
->size
* i
;
1121 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1122 addr
= *(void **)addr
;
1124 if (field
->flags
& VMS_STRUCT
) {
1125 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1127 ret
= field
->info
->get(f
, addr
, field
->size
);
1137 if (vmsd
->post_load
) {
1138 return vmsd
->post_load(opaque
, version_id
);
1143 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1146 VMStateField
*field
= vmsd
->fields
;
1148 if (vmsd
->pre_save
) {
1149 vmsd
->pre_save(opaque
);
1151 while(field
->name
) {
1152 if (!field
->field_exists
||
1153 field
->field_exists(opaque
, vmsd
->version_id
)) {
1154 void *base_addr
= opaque
+ field
->offset
;
1157 if (field
->flags
& VMS_ARRAY
) {
1158 n_elems
= field
->num
;
1159 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1160 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1161 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1162 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1164 if (field
->flags
& VMS_POINTER
) {
1165 base_addr
= *(void **)base_addr
;
1167 for (i
= 0; i
< n_elems
; i
++) {
1168 void *addr
= base_addr
+ field
->size
* i
;
1170 if (field
->flags
& VMS_STRUCT
) {
1171 vmstate_save_state(f
, field
->vmsd
, addr
);
1173 field
->info
->put(f
, addr
, field
->size
);
1179 if (vmsd
->post_save
) {
1180 vmsd
->post_save(opaque
);
1184 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1186 if (!se
->vmsd
) { /* Old style */
1187 return se
->load_state(f
, se
->opaque
, version_id
);
1189 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1192 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1194 if (!se
->vmsd
) { /* Old style */
1195 se
->save_state(f
, se
->opaque
);
1198 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1201 #define QEMU_VM_FILE_MAGIC 0x5145564d
1202 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1203 #define QEMU_VM_FILE_VERSION 0x00000003
1205 #define QEMU_VM_EOF 0x00
1206 #define QEMU_VM_SECTION_START 0x01
1207 #define QEMU_VM_SECTION_PART 0x02
1208 #define QEMU_VM_SECTION_END 0x03
1209 #define QEMU_VM_SECTION_FULL 0x04
1211 int qemu_savevm_state_begin(QEMUFile
*f
)
1215 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1216 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1218 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1221 if (se
->save_live_state
== NULL
)
1225 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1226 qemu_put_be32(f
, se
->section_id
);
1229 len
= strlen(se
->idstr
);
1230 qemu_put_byte(f
, len
);
1231 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1233 qemu_put_be32(f
, se
->instance_id
);
1234 qemu_put_be32(f
, se
->version_id
);
1236 se
->save_live_state(f
, QEMU_VM_SECTION_START
, se
->opaque
);
1239 if (qemu_file_has_error(f
))
1245 int qemu_savevm_state_iterate(QEMUFile
*f
)
1250 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1251 if (se
->save_live_state
== NULL
)
1255 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1256 qemu_put_be32(f
, se
->section_id
);
1258 ret
&= !!se
->save_live_state(f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1264 if (qemu_file_has_error(f
))
1270 int qemu_savevm_state_complete(QEMUFile
*f
)
1274 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1275 if (se
->save_live_state
== NULL
)
1279 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1280 qemu_put_be32(f
, se
->section_id
);
1282 se
->save_live_state(f
, QEMU_VM_SECTION_END
, se
->opaque
);
1285 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1288 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1292 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1293 qemu_put_be32(f
, se
->section_id
);
1296 len
= strlen(se
->idstr
);
1297 qemu_put_byte(f
, len
);
1298 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1300 qemu_put_be32(f
, se
->instance_id
);
1301 qemu_put_be32(f
, se
->version_id
);
1303 vmstate_save(f
, se
);
1306 qemu_put_byte(f
, QEMU_VM_EOF
);
1308 if (qemu_file_has_error(f
))
1314 int qemu_savevm_state(QEMUFile
*f
)
1316 int saved_vm_running
;
1319 saved_vm_running
= vm_running
;
1324 ret
= qemu_savevm_state_begin(f
);
1329 ret
= qemu_savevm_state_iterate(f
);
1334 ret
= qemu_savevm_state_complete(f
);
1337 if (qemu_file_has_error(f
))
1340 if (!ret
&& saved_vm_running
)
1346 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1350 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1351 if (!strcmp(se
->idstr
, idstr
) &&
1352 instance_id
== se
->instance_id
)
1358 typedef struct LoadStateEntry
{
1359 QLIST_ENTRY(LoadStateEntry
) entry
;
1365 int qemu_loadvm_state(QEMUFile
*f
)
1367 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1368 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1369 LoadStateEntry
*le
, *new_le
;
1370 uint8_t section_type
;
1374 v
= qemu_get_be32(f
);
1375 if (v
!= QEMU_VM_FILE_MAGIC
)
1378 v
= qemu_get_be32(f
);
1379 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1380 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1383 if (v
!= QEMU_VM_FILE_VERSION
)
1386 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1387 uint32_t instance_id
, version_id
, section_id
;
1392 switch (section_type
) {
1393 case QEMU_VM_SECTION_START
:
1394 case QEMU_VM_SECTION_FULL
:
1395 /* Read section start */
1396 section_id
= qemu_get_be32(f
);
1397 len
= qemu_get_byte(f
);
1398 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1400 instance_id
= qemu_get_be32(f
);
1401 version_id
= qemu_get_be32(f
);
1403 /* Find savevm section */
1404 se
= find_se(idstr
, instance_id
);
1406 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1411 /* Validate version */
1412 if (version_id
> se
->version_id
) {
1413 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1414 version_id
, idstr
, se
->version_id
);
1420 le
= qemu_mallocz(sizeof(*le
));
1423 le
->section_id
= section_id
;
1424 le
->version_id
= version_id
;
1425 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1427 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1429 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1430 instance_id
, idstr
);
1434 case QEMU_VM_SECTION_PART
:
1435 case QEMU_VM_SECTION_END
:
1436 section_id
= qemu_get_be32(f
);
1438 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1439 if (le
->section_id
== section_id
) {
1444 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1449 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1451 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1457 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1466 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1467 QLIST_REMOVE(le
, entry
);
1471 if (qemu_file_has_error(f
))
1477 /* device can contain snapshots */
1478 static int bdrv_can_snapshot(BlockDriverState
*bs
)
1481 !bdrv_is_removable(bs
) &&
1482 !bdrv_is_read_only(bs
));
1485 /* device must be snapshots in order to have a reliable snapshot */
1486 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1489 !bdrv_is_removable(bs
) &&
1490 !bdrv_is_read_only(bs
));
1493 static BlockDriverState
*get_bs_snapshots(void)
1495 BlockDriverState
*bs
;
1499 return bs_snapshots
;
1500 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1502 if (bdrv_can_snapshot(bs
))
1511 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1514 QEMUSnapshotInfo
*sn_tab
, *sn
;
1518 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1521 for(i
= 0; i
< nb_sns
; i
++) {
1523 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1533 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1536 BlockDriverState
*bs
, *bs1
;
1537 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1538 int must_delete
, ret
;
1540 int saved_vm_running
;
1541 uint32_t vm_state_size
;
1547 const char *name
= qdict_get_try_str(qdict
, "name");
1549 bs
= get_bs_snapshots();
1551 monitor_printf(mon
, "No block device can accept snapshots\n");
1555 /* ??? Should this occur after vm_stop? */
1558 saved_vm_running
= vm_running
;
1563 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1568 memset(sn
, 0, sizeof(*sn
));
1570 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1571 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1574 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1577 /* fill auxiliary fields */
1580 sn
->date_sec
= tb
.time
;
1581 sn
->date_nsec
= tb
.millitm
* 1000000;
1583 gettimeofday(&tv
, NULL
);
1584 sn
->date_sec
= tv
.tv_sec
;
1585 sn
->date_nsec
= tv
.tv_usec
* 1000;
1587 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1589 /* save the VM state */
1590 f
= qemu_fopen_bdrv(bs
, 1);
1592 monitor_printf(mon
, "Could not open VM state file\n");
1595 ret
= qemu_savevm_state(f
);
1596 vm_state_size
= qemu_ftell(f
);
1599 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1603 /* create the snapshots */
1605 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1607 if (bdrv_has_snapshot(bs1
)) {
1609 ret
= bdrv_snapshot_delete(bs1
, old_sn
->id_str
);
1612 "Error while deleting snapshot on '%s'\n",
1613 bdrv_get_device_name(bs1
));
1616 /* Write VM state size only to the image that contains the state */
1617 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1618 ret
= bdrv_snapshot_create(bs1
, sn
);
1620 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1621 bdrv_get_device_name(bs1
));
1627 if (saved_vm_running
)
1631 int load_vmstate(Monitor
*mon
, const char *name
)
1634 BlockDriverState
*bs
, *bs1
;
1635 QEMUSnapshotInfo sn
;
1639 bs
= get_bs_snapshots();
1641 monitor_printf(mon
, "No block device supports snapshots\n");
1645 /* Flush all IO requests so they don't interfere with the new state. */
1648 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1650 if (bdrv_has_snapshot(bs1
)) {
1651 ret
= bdrv_snapshot_goto(bs1
, name
);
1654 monitor_printf(mon
, "Warning: ");
1658 "Snapshots not supported on device '%s'\n",
1659 bdrv_get_device_name(bs1
));
1662 monitor_printf(mon
, "Could not find snapshot '%s' on "
1664 name
, bdrv_get_device_name(bs1
));
1667 monitor_printf(mon
, "Error %d while activating snapshot on"
1668 " '%s'\n", ret
, bdrv_get_device_name(bs1
));
1671 /* fatal on snapshot block device */
1678 /* Don't even try to load empty VM states */
1679 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1680 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1683 /* restore the VM state */
1684 f
= qemu_fopen_bdrv(bs
, 0);
1686 monitor_printf(mon
, "Could not open VM state file\n");
1689 ret
= qemu_loadvm_state(f
);
1692 monitor_printf(mon
, "Error %d while loading VM state\n", ret
);
1698 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1701 BlockDriverState
*bs
, *bs1
;
1703 const char *name
= qdict_get_str(qdict
, "name");
1705 bs
= get_bs_snapshots();
1707 monitor_printf(mon
, "No block device supports snapshots\n");
1711 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1713 if (bdrv_has_snapshot(bs1
)) {
1714 ret
= bdrv_snapshot_delete(bs1
, name
);
1716 if (ret
== -ENOTSUP
)
1718 "Snapshots not supported on device '%s'\n",
1719 bdrv_get_device_name(bs1
));
1721 monitor_printf(mon
, "Error %d while deleting snapshot on "
1722 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1728 void do_info_snapshots(Monitor
*mon
)
1731 BlockDriverState
*bs
, *bs1
;
1732 QEMUSnapshotInfo
*sn_tab
, *sn
;
1736 bs
= get_bs_snapshots();
1738 monitor_printf(mon
, "No available block device supports snapshots\n");
1741 monitor_printf(mon
, "Snapshot devices:");
1742 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1744 if (bdrv_has_snapshot(bs1
)) {
1746 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1749 monitor_printf(mon
, "\n");
1751 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1753 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1756 monitor_printf(mon
, "Snapshot list (from %s):\n",
1757 bdrv_get_device_name(bs
));
1758 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1759 for(i
= 0; i
< nb_sns
; i
++) {
1761 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));