4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <sys/select.h>
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
59 #include <linux/rtc.h>
67 #include <sys/timeb.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
73 #include "qemu-common.h"
78 #include "qemu-timer.h"
79 #include "qemu-char.h"
81 #include "audio/audio.h"
82 #include "migration.h"
83 #include "qemu_socket.h"
84 #include "qemu-queue.h"
86 /* point to the block driver where the snapshots are managed */
87 static BlockDriverState
*bs_snapshots
;
89 #define SELF_ANNOUNCE_ROUNDS 5
92 #define ETH_P_RARP 0x8035
94 #define ARP_HTYPE_ETH 0x0001
95 #define ARP_PTYPE_IP 0x0800
96 #define ARP_OP_REQUEST_REV 0x3
98 static int announce_self_create(uint8_t *buf
,
101 /* Ethernet header. */
102 memset(buf
, 0xff, 6); /* destination MAC addr */
103 memcpy(buf
+ 6, mac_addr
, 6); /* source MAC addr */
104 *(uint16_t *)(buf
+ 12) = htons(ETH_P_RARP
); /* ethertype */
107 *(uint16_t *)(buf
+ 14) = htons(ARP_HTYPE_ETH
); /* hardware addr space */
108 *(uint16_t *)(buf
+ 16) = htons(ARP_PTYPE_IP
); /* protocol addr space */
109 *(buf
+ 18) = 6; /* hardware addr length (ethernet) */
110 *(buf
+ 19) = 4; /* protocol addr length (IPv4) */
111 *(uint16_t *)(buf
+ 20) = htons(ARP_OP_REQUEST_REV
); /* opcode */
112 memcpy(buf
+ 22, mac_addr
, 6); /* source hw addr */
113 memset(buf
+ 28, 0x00, 4); /* source protocol addr */
114 memcpy(buf
+ 32, mac_addr
, 6); /* target hw addr */
115 memset(buf
+ 38, 0x00, 4); /* target protocol addr */
117 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
118 memset(buf
+ 42, 0x00, 18);
120 return 60; /* len (FCS will be added by hardware) */
123 static void qemu_announce_self_iter(NICState
*nic
, void *opaque
)
128 len
= announce_self_create(buf
, nic
->conf
->macaddr
.a
);
130 qemu_send_packet_raw(&nic
->nc
, buf
, len
);
134 static void qemu_announce_self_once(void *opaque
)
136 static int count
= SELF_ANNOUNCE_ROUNDS
;
137 QEMUTimer
*timer
= *(QEMUTimer
**)opaque
;
139 qemu_foreach_nic(qemu_announce_self_iter
, NULL
);
142 /* delay 50ms, 150ms, 250ms, ... */
143 qemu_mod_timer(timer
, qemu_get_clock(rt_clock
) +
144 50 + (SELF_ANNOUNCE_ROUNDS
- count
- 1) * 100);
146 qemu_del_timer(timer
);
147 qemu_free_timer(timer
);
151 void qemu_announce_self(void)
153 static QEMUTimer
*timer
;
154 timer
= qemu_new_timer(rt_clock
, qemu_announce_self_once
, &timer
);
155 qemu_announce_self_once(&timer
);
158 /***********************************************************/
159 /* savevm/loadvm support */
161 #define IO_BUF_SIZE 32768
164 QEMUFilePutBufferFunc
*put_buffer
;
165 QEMUFileGetBufferFunc
*get_buffer
;
166 QEMUFileCloseFunc
*close
;
167 QEMUFileRateLimit
*rate_limit
;
168 QEMUFileSetRateLimit
*set_rate_limit
;
169 QEMUFileGetRateLimit
*get_rate_limit
;
173 int64_t buf_offset
; /* start of buffer when writing, end of buffer
176 int buf_size
; /* 0 when writing */
177 uint8_t buf
[IO_BUF_SIZE
];
182 typedef struct QEMUFileStdio
188 typedef struct QEMUFileSocket
194 static int socket_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
196 QEMUFileSocket
*s
= opaque
;
200 len
= recv(s
->fd
, (void *)buf
, size
, 0);
201 } while (len
== -1 && socket_error() == EINTR
);
204 len
= -socket_error();
209 static int socket_close(void *opaque
)
211 QEMUFileSocket
*s
= opaque
;
216 static int stdio_put_buffer(void *opaque
, const uint8_t *buf
, int64_t pos
, int size
)
218 QEMUFileStdio
*s
= opaque
;
219 return fwrite(buf
, 1, size
, s
->stdio_file
);
222 static int stdio_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
224 QEMUFileStdio
*s
= opaque
;
225 FILE *fp
= s
->stdio_file
;
230 bytes
= fread(buf
, 1, size
, fp
);
231 } while ((bytes
== 0) && ferror(fp
) && (errno
== EINTR
));
235 static int stdio_pclose(void *opaque
)
237 QEMUFileStdio
*s
= opaque
;
238 pclose(s
->stdio_file
);
243 static int stdio_fclose(void *opaque
)
245 QEMUFileStdio
*s
= opaque
;
246 fclose(s
->stdio_file
);
251 QEMUFile
*qemu_popen(FILE *stdio_file
, const char *mode
)
255 if (stdio_file
== NULL
|| mode
== NULL
|| (mode
[0] != 'r' && mode
[0] != 'w') || mode
[1] != 0) {
256 fprintf(stderr
, "qemu_popen: Argument validity check failed\n");
260 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
262 s
->stdio_file
= stdio_file
;
265 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_pclose
,
268 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_pclose
,
274 QEMUFile
*qemu_popen_cmd(const char *command
, const char *mode
)
278 popen_file
= popen(command
, mode
);
279 if(popen_file
== NULL
) {
283 return qemu_popen(popen_file
, mode
);
286 int qemu_stdio_fd(QEMUFile
*f
)
291 p
= (QEMUFileStdio
*)f
->opaque
;
292 fd
= fileno(p
->stdio_file
);
297 QEMUFile
*qemu_fdopen(int fd
, const char *mode
)
302 (mode
[0] != 'r' && mode
[0] != 'w') ||
303 mode
[1] != 'b' || mode
[2] != 0) {
304 fprintf(stderr
, "qemu_fdopen: Argument validity check failed\n");
308 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
309 s
->stdio_file
= fdopen(fd
, mode
);
314 s
->file
= qemu_fopen_ops(s
, NULL
, stdio_get_buffer
, stdio_fclose
,
317 s
->file
= qemu_fopen_ops(s
, stdio_put_buffer
, NULL
, stdio_fclose
,
327 QEMUFile
*qemu_fopen_socket(int fd
)
329 QEMUFileSocket
*s
= qemu_mallocz(sizeof(QEMUFileSocket
));
332 s
->file
= qemu_fopen_ops(s
, NULL
, socket_get_buffer
, socket_close
,
337 static int file_put_buffer(void *opaque
, const uint8_t *buf
,
338 int64_t pos
, int size
)
340 QEMUFileStdio
*s
= opaque
;
341 fseek(s
->stdio_file
, pos
, SEEK_SET
);
342 return fwrite(buf
, 1, size
, s
->stdio_file
);
345 static int file_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
347 QEMUFileStdio
*s
= opaque
;
348 fseek(s
->stdio_file
, pos
, SEEK_SET
);
349 return fread(buf
, 1, size
, s
->stdio_file
);
352 QEMUFile
*qemu_fopen(const char *filename
, const char *mode
)
357 (mode
[0] != 'r' && mode
[0] != 'w') ||
358 mode
[1] != 'b' || mode
[2] != 0) {
359 fprintf(stderr
, "qemu_fopen: Argument validity check failed\n");
363 s
= qemu_mallocz(sizeof(QEMUFileStdio
));
365 s
->stdio_file
= fopen(filename
, mode
);
370 s
->file
= qemu_fopen_ops(s
, file_put_buffer
, NULL
, stdio_fclose
,
373 s
->file
= qemu_fopen_ops(s
, NULL
, file_get_buffer
, stdio_fclose
,
382 static int block_put_buffer(void *opaque
, const uint8_t *buf
,
383 int64_t pos
, int size
)
385 bdrv_save_vmstate(opaque
, buf
, pos
, size
);
389 static int block_get_buffer(void *opaque
, uint8_t *buf
, int64_t pos
, int size
)
391 return bdrv_load_vmstate(opaque
, buf
, pos
, size
);
394 static int bdrv_fclose(void *opaque
)
399 static QEMUFile
*qemu_fopen_bdrv(BlockDriverState
*bs
, int is_writable
)
402 return qemu_fopen_ops(bs
, block_put_buffer
, NULL
, bdrv_fclose
,
404 return qemu_fopen_ops(bs
, NULL
, block_get_buffer
, bdrv_fclose
, NULL
, NULL
, NULL
);
407 QEMUFile
*qemu_fopen_ops(void *opaque
, QEMUFilePutBufferFunc
*put_buffer
,
408 QEMUFileGetBufferFunc
*get_buffer
,
409 QEMUFileCloseFunc
*close
,
410 QEMUFileRateLimit
*rate_limit
,
411 QEMUFileSetRateLimit
*set_rate_limit
,
412 QEMUFileGetRateLimit
*get_rate_limit
)
416 f
= qemu_mallocz(sizeof(QEMUFile
));
419 f
->put_buffer
= put_buffer
;
420 f
->get_buffer
= get_buffer
;
422 f
->rate_limit
= rate_limit
;
423 f
->set_rate_limit
= set_rate_limit
;
424 f
->get_rate_limit
= get_rate_limit
;
430 int qemu_file_has_error(QEMUFile
*f
)
435 void qemu_file_set_error(QEMUFile
*f
)
440 void qemu_fflush(QEMUFile
*f
)
445 if (f
->is_write
&& f
->buf_index
> 0) {
448 len
= f
->put_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, f
->buf_index
);
450 f
->buf_offset
+= f
->buf_index
;
457 static void qemu_fill_buffer(QEMUFile
*f
)
467 len
= f
->get_buffer(f
->opaque
, f
->buf
, f
->buf_offset
, IO_BUF_SIZE
);
471 f
->buf_offset
+= len
;
472 } else if (len
!= -EAGAIN
)
476 int qemu_fclose(QEMUFile
*f
)
481 ret
= f
->close(f
->opaque
);
486 void qemu_file_put_notify(QEMUFile
*f
)
488 f
->put_buffer(f
->opaque
, NULL
, 0, 0);
491 void qemu_put_buffer(QEMUFile
*f
, const uint8_t *buf
, int size
)
495 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
497 "Attempted to write to buffer while read buffer is not empty\n");
501 while (!f
->has_error
&& size
> 0) {
502 l
= IO_BUF_SIZE
- f
->buf_index
;
505 memcpy(f
->buf
+ f
->buf_index
, buf
, l
);
510 if (f
->buf_index
>= IO_BUF_SIZE
)
515 void qemu_put_byte(QEMUFile
*f
, int v
)
517 if (!f
->has_error
&& f
->is_write
== 0 && f
->buf_index
> 0) {
519 "Attempted to write to buffer while read buffer is not empty\n");
523 f
->buf
[f
->buf_index
++] = v
;
525 if (f
->buf_index
>= IO_BUF_SIZE
)
529 int qemu_get_buffer(QEMUFile
*f
, uint8_t *buf
, int size1
)
538 l
= f
->buf_size
- f
->buf_index
;
541 l
= f
->buf_size
- f
->buf_index
;
547 memcpy(buf
, f
->buf
+ f
->buf_index
, l
);
555 int qemu_get_byte(QEMUFile
*f
)
560 if (f
->buf_index
>= f
->buf_size
) {
562 if (f
->buf_index
>= f
->buf_size
)
565 return f
->buf
[f
->buf_index
++];
568 int64_t qemu_ftell(QEMUFile
*f
)
570 return f
->buf_offset
- f
->buf_size
+ f
->buf_index
;
573 int64_t qemu_fseek(QEMUFile
*f
, int64_t pos
, int whence
)
575 if (whence
== SEEK_SET
) {
577 } else if (whence
== SEEK_CUR
) {
578 pos
+= qemu_ftell(f
);
580 /* SEEK_END not supported */
594 int qemu_file_rate_limit(QEMUFile
*f
)
597 return f
->rate_limit(f
->opaque
);
602 size_t qemu_file_get_rate_limit(QEMUFile
*f
)
604 if (f
->get_rate_limit
)
605 return f
->get_rate_limit(f
->opaque
);
610 size_t qemu_file_set_rate_limit(QEMUFile
*f
, size_t new_rate
)
612 /* any failed or completed migration keeps its state to allow probing of
613 * migration data, but has no associated file anymore */
614 if (f
&& f
->set_rate_limit
)
615 return f
->set_rate_limit(f
->opaque
, new_rate
);
620 void qemu_put_be16(QEMUFile
*f
, unsigned int v
)
622 qemu_put_byte(f
, v
>> 8);
626 void qemu_put_be32(QEMUFile
*f
, unsigned int v
)
628 qemu_put_byte(f
, v
>> 24);
629 qemu_put_byte(f
, v
>> 16);
630 qemu_put_byte(f
, v
>> 8);
634 void qemu_put_be64(QEMUFile
*f
, uint64_t v
)
636 qemu_put_be32(f
, v
>> 32);
640 unsigned int qemu_get_be16(QEMUFile
*f
)
643 v
= qemu_get_byte(f
) << 8;
644 v
|= qemu_get_byte(f
);
648 unsigned int qemu_get_be32(QEMUFile
*f
)
651 v
= qemu_get_byte(f
) << 24;
652 v
|= qemu_get_byte(f
) << 16;
653 v
|= qemu_get_byte(f
) << 8;
654 v
|= qemu_get_byte(f
);
658 uint64_t qemu_get_be64(QEMUFile
*f
)
661 v
= (uint64_t)qemu_get_be32(f
) << 32;
662 v
|= qemu_get_be32(f
);
668 static int get_int8(QEMUFile
*f
, void *pv
, size_t size
)
675 static void put_int8(QEMUFile
*f
, void *pv
, size_t size
)
681 const VMStateInfo vmstate_info_int8
= {
689 static int get_int16(QEMUFile
*f
, void *pv
, size_t size
)
692 qemu_get_sbe16s(f
, v
);
696 static void put_int16(QEMUFile
*f
, void *pv
, size_t size
)
699 qemu_put_sbe16s(f
, v
);
702 const VMStateInfo vmstate_info_int16
= {
710 static int get_int32(QEMUFile
*f
, void *pv
, size_t size
)
713 qemu_get_sbe32s(f
, v
);
717 static void put_int32(QEMUFile
*f
, void *pv
, size_t size
)
720 qemu_put_sbe32s(f
, v
);
723 const VMStateInfo vmstate_info_int32
= {
729 /* 32 bit int. See that the received value is the same than the one
732 static int get_int32_equal(QEMUFile
*f
, void *pv
, size_t size
)
736 qemu_get_sbe32s(f
, &v2
);
743 const VMStateInfo vmstate_info_int32_equal
= {
744 .name
= "int32 equal",
745 .get
= get_int32_equal
,
749 /* 32 bit int. See that the received value is the less or the same
750 than the one in the field */
752 static int get_int32_le(QEMUFile
*f
, void *pv
, size_t size
)
756 qemu_get_sbe32s(f
, &new);
763 const VMStateInfo vmstate_info_int32_le
= {
764 .name
= "int32 equal",
771 static int get_int64(QEMUFile
*f
, void *pv
, size_t size
)
774 qemu_get_sbe64s(f
, v
);
778 static void put_int64(QEMUFile
*f
, void *pv
, size_t size
)
781 qemu_put_sbe64s(f
, v
);
784 const VMStateInfo vmstate_info_int64
= {
790 /* 8 bit unsigned int */
792 static int get_uint8(QEMUFile
*f
, void *pv
, size_t size
)
799 static void put_uint8(QEMUFile
*f
, void *pv
, size_t size
)
805 const VMStateInfo vmstate_info_uint8
= {
811 /* 16 bit unsigned int */
813 static int get_uint16(QEMUFile
*f
, void *pv
, size_t size
)
816 qemu_get_be16s(f
, v
);
820 static void put_uint16(QEMUFile
*f
, void *pv
, size_t size
)
823 qemu_put_be16s(f
, v
);
826 const VMStateInfo vmstate_info_uint16
= {
832 /* 32 bit unsigned int */
834 static int get_uint32(QEMUFile
*f
, void *pv
, size_t size
)
837 qemu_get_be32s(f
, v
);
841 static void put_uint32(QEMUFile
*f
, void *pv
, size_t size
)
844 qemu_put_be32s(f
, v
);
847 const VMStateInfo vmstate_info_uint32
= {
853 /* 64 bit unsigned int */
855 static int get_uint64(QEMUFile
*f
, void *pv
, size_t size
)
858 qemu_get_be64s(f
, v
);
862 static void put_uint64(QEMUFile
*f
, void *pv
, size_t size
)
865 qemu_put_be64s(f
, v
);
868 const VMStateInfo vmstate_info_uint64
= {
874 /* 8 bit int. See that the received value is the same than the one
877 static int get_uint8_equal(QEMUFile
*f
, void *pv
, size_t size
)
888 const VMStateInfo vmstate_info_uint8_equal
= {
889 .name
= "uint8 equal",
890 .get
= get_uint8_equal
,
894 /* 16 bit unsigned int int. See that the received value is the same than the one
897 static int get_uint16_equal(QEMUFile
*f
, void *pv
, size_t size
)
901 qemu_get_be16s(f
, &v2
);
908 const VMStateInfo vmstate_info_uint16_equal
= {
909 .name
= "uint16 equal",
910 .get
= get_uint16_equal
,
916 static int get_timer(QEMUFile
*f
, void *pv
, size_t size
)
919 qemu_get_timer(f
, v
);
923 static void put_timer(QEMUFile
*f
, void *pv
, size_t size
)
926 qemu_put_timer(f
, v
);
929 const VMStateInfo vmstate_info_timer
= {
935 /* uint8_t buffers */
937 static int get_buffer(QEMUFile
*f
, void *pv
, size_t size
)
940 qemu_get_buffer(f
, v
, size
);
944 static void put_buffer(QEMUFile
*f
, void *pv
, size_t size
)
947 qemu_put_buffer(f
, v
, size
);
950 const VMStateInfo vmstate_info_buffer
= {
956 /* unused buffers: space that was used for some fields that are
957 not usefull anymore */
959 static int get_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
965 block_len
= MIN(sizeof(buf
), size
);
967 qemu_get_buffer(f
, buf
, block_len
);
972 static void put_unused_buffer(QEMUFile
*f
, void *pv
, size_t size
)
974 static const uint8_t buf
[1024];
978 block_len
= MIN(sizeof(buf
), size
);
980 qemu_put_buffer(f
, buf
, block_len
);
984 const VMStateInfo vmstate_info_unused_buffer
= {
985 .name
= "unused_buffer",
986 .get
= get_unused_buffer
,
987 .put
= put_unused_buffer
,
990 typedef struct SaveStateEntry
{
991 QTAILQ_ENTRY(SaveStateEntry
) entry
;
997 SaveSetParamsHandler
*set_params
;
998 SaveLiveStateHandler
*save_live_state
;
999 SaveStateHandler
*save_state
;
1000 LoadStateHandler
*load_state
;
1001 const VMStateDescription
*vmsd
;
1006 static QTAILQ_HEAD(savevm_handlers
, SaveStateEntry
) savevm_handlers
=
1007 QTAILQ_HEAD_INITIALIZER(savevm_handlers
);
1008 static int global_section_id
;
1010 static int calculate_new_instance_id(const char *idstr
)
1013 int instance_id
= 0;
1015 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1016 if (strcmp(idstr
, se
->idstr
) == 0
1017 && instance_id
<= se
->instance_id
) {
1018 instance_id
= se
->instance_id
+ 1;
1024 /* TODO: Individual devices generally have very little idea about the rest
1025 of the system, so instance_id should be removed/replaced.
1026 Meanwhile pass -1 as instance_id if you do not already have a clearly
1027 distinguishing id for all instances of your device class. */
1028 int register_savevm_live(const char *idstr
,
1031 SaveSetParamsHandler
*set_params
,
1032 SaveLiveStateHandler
*save_live_state
,
1033 SaveStateHandler
*save_state
,
1034 LoadStateHandler
*load_state
,
1039 se
= qemu_mallocz(sizeof(SaveStateEntry
));
1040 pstrcpy(se
->idstr
, sizeof(se
->idstr
), idstr
);
1041 se
->version_id
= version_id
;
1042 se
->section_id
= global_section_id
++;
1043 se
->set_params
= set_params
;
1044 se
->save_live_state
= save_live_state
;
1045 se
->save_state
= save_state
;
1046 se
->load_state
= load_state
;
1047 se
->opaque
= opaque
;
1050 if (instance_id
== -1) {
1051 se
->instance_id
= calculate_new_instance_id(idstr
);
1053 se
->instance_id
= instance_id
;
1055 /* add at the end of list */
1056 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1060 int register_savevm(const char *idstr
,
1063 SaveStateHandler
*save_state
,
1064 LoadStateHandler
*load_state
,
1067 return register_savevm_live(idstr
, instance_id
, version_id
,
1068 NULL
, NULL
, save_state
, load_state
, opaque
);
1071 void unregister_savevm(const char *idstr
, void *opaque
)
1073 SaveStateEntry
*se
, *new_se
;
1075 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1076 if (strcmp(se
->idstr
, idstr
) == 0 && se
->opaque
== opaque
) {
1077 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1083 int vmstate_register_with_alias_id(int instance_id
,
1084 const VMStateDescription
*vmsd
,
1085 void *opaque
, int alias_id
,
1086 int required_for_version
)
1090 /* If this triggers, alias support can be dropped for the vmsd. */
1091 assert(alias_id
== -1 || required_for_version
>= vmsd
->minimum_version_id
);
1093 se
= qemu_mallocz(sizeof(SaveStateEntry
));
1094 pstrcpy(se
->idstr
, sizeof(se
->idstr
), vmsd
->name
);
1095 se
->version_id
= vmsd
->version_id
;
1096 se
->section_id
= global_section_id
++;
1097 se
->save_live_state
= NULL
;
1098 se
->save_state
= NULL
;
1099 se
->load_state
= NULL
;
1100 se
->opaque
= opaque
;
1102 se
->alias_id
= alias_id
;
1104 if (instance_id
== -1) {
1105 se
->instance_id
= calculate_new_instance_id(vmsd
->name
);
1107 se
->instance_id
= instance_id
;
1109 /* add at the end of list */
1110 QTAILQ_INSERT_TAIL(&savevm_handlers
, se
, entry
);
1114 int vmstate_register(int instance_id
, const VMStateDescription
*vmsd
,
1117 return vmstate_register_with_alias_id(instance_id
, vmsd
, opaque
, -1, 0);
1120 void vmstate_unregister(const VMStateDescription
*vmsd
, void *opaque
)
1122 SaveStateEntry
*se
, *new_se
;
1124 QTAILQ_FOREACH_SAFE(se
, &savevm_handlers
, entry
, new_se
) {
1125 if (se
->vmsd
== vmsd
&& se
->opaque
== opaque
) {
1126 QTAILQ_REMOVE(&savevm_handlers
, se
, entry
);
1132 int vmstate_load_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1133 void *opaque
, int version_id
)
1135 VMStateField
*field
= vmsd
->fields
;
1137 if (version_id
> vmsd
->version_id
) {
1140 if (version_id
< vmsd
->minimum_version_id_old
) {
1143 if (version_id
< vmsd
->minimum_version_id
) {
1144 return vmsd
->load_state_old(f
, opaque
, version_id
);
1146 if (vmsd
->pre_load
) {
1147 int ret
= vmsd
->pre_load(opaque
);
1151 while(field
->name
) {
1152 if ((field
->field_exists
&&
1153 field
->field_exists(opaque
, version_id
)) ||
1154 (!field
->field_exists
&&
1155 field
->version_id
<= version_id
)) {
1156 void *base_addr
= opaque
+ field
->offset
;
1157 int ret
, i
, n_elems
= 1;
1158 int size
= field
->size
;
1160 if (field
->flags
& VMS_VBUFFER
) {
1161 size
= *(int32_t *)(opaque
+field
->size_offset
);
1162 if (field
->flags
& VMS_MULTIPLY
) {
1163 size
*= field
->size
;
1166 if (field
->flags
& VMS_ARRAY
) {
1167 n_elems
= field
->num
;
1168 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1169 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1170 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1171 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1173 if (field
->flags
& VMS_POINTER
) {
1174 base_addr
= *(void **)base_addr
+ field
->start
;
1176 for (i
= 0; i
< n_elems
; i
++) {
1177 void *addr
= base_addr
+ size
* i
;
1179 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1180 addr
= *(void **)addr
;
1182 if (field
->flags
& VMS_STRUCT
) {
1183 ret
= vmstate_load_state(f
, field
->vmsd
, addr
, field
->vmsd
->version_id
);
1185 ret
= field
->info
->get(f
, addr
, size
);
1195 if (vmsd
->post_load
) {
1196 return vmsd
->post_load(opaque
, version_id
);
1201 void vmstate_save_state(QEMUFile
*f
, const VMStateDescription
*vmsd
,
1204 VMStateField
*field
= vmsd
->fields
;
1206 if (vmsd
->pre_save
) {
1207 vmsd
->pre_save(opaque
);
1209 while(field
->name
) {
1210 if (!field
->field_exists
||
1211 field
->field_exists(opaque
, vmsd
->version_id
)) {
1212 void *base_addr
= opaque
+ field
->offset
;
1214 int size
= field
->size
;
1216 if (field
->flags
& VMS_VBUFFER
) {
1217 size
= *(int32_t *)(opaque
+field
->size_offset
);
1218 if (field
->flags
& VMS_MULTIPLY
) {
1219 size
*= field
->size
;
1222 if (field
->flags
& VMS_ARRAY
) {
1223 n_elems
= field
->num
;
1224 } else if (field
->flags
& VMS_VARRAY_INT32
) {
1225 n_elems
= *(int32_t *)(opaque
+field
->num_offset
);
1226 } else if (field
->flags
& VMS_VARRAY_UINT16
) {
1227 n_elems
= *(uint16_t *)(opaque
+field
->num_offset
);
1229 if (field
->flags
& VMS_POINTER
) {
1230 base_addr
= *(void **)base_addr
+ field
->start
;
1232 for (i
= 0; i
< n_elems
; i
++) {
1233 void *addr
= base_addr
+ size
* i
;
1235 if (field
->flags
& VMS_ARRAY_OF_POINTER
) {
1236 addr
= *(void **)addr
;
1238 if (field
->flags
& VMS_STRUCT
) {
1239 vmstate_save_state(f
, field
->vmsd
, addr
);
1241 field
->info
->put(f
, addr
, size
);
1249 static int vmstate_load(QEMUFile
*f
, SaveStateEntry
*se
, int version_id
)
1251 if (!se
->vmsd
) { /* Old style */
1252 return se
->load_state(f
, se
->opaque
, version_id
);
1254 return vmstate_load_state(f
, se
->vmsd
, se
->opaque
, version_id
);
1257 static void vmstate_save(QEMUFile
*f
, SaveStateEntry
*se
)
1259 if (!se
->vmsd
) { /* Old style */
1260 se
->save_state(f
, se
->opaque
);
1263 vmstate_save_state(f
,se
->vmsd
, se
->opaque
);
1266 #define QEMU_VM_FILE_MAGIC 0x5145564d
1267 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1268 #define QEMU_VM_FILE_VERSION 0x00000003
1270 #define QEMU_VM_EOF 0x00
1271 #define QEMU_VM_SECTION_START 0x01
1272 #define QEMU_VM_SECTION_PART 0x02
1273 #define QEMU_VM_SECTION_END 0x03
1274 #define QEMU_VM_SECTION_FULL 0x04
1276 int qemu_savevm_state_begin(Monitor
*mon
, QEMUFile
*f
, int blk_enable
,
1281 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1282 if(se
->set_params
== NULL
) {
1285 se
->set_params(blk_enable
, shared
, se
->opaque
);
1288 qemu_put_be32(f
, QEMU_VM_FILE_MAGIC
);
1289 qemu_put_be32(f
, QEMU_VM_FILE_VERSION
);
1291 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1294 if (se
->save_live_state
== NULL
)
1298 qemu_put_byte(f
, QEMU_VM_SECTION_START
);
1299 qemu_put_be32(f
, se
->section_id
);
1302 len
= strlen(se
->idstr
);
1303 qemu_put_byte(f
, len
);
1304 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1306 qemu_put_be32(f
, se
->instance_id
);
1307 qemu_put_be32(f
, se
->version_id
);
1309 se
->save_live_state(mon
, f
, QEMU_VM_SECTION_START
, se
->opaque
);
1312 if (qemu_file_has_error(f
)) {
1313 qemu_savevm_state_cancel(mon
, f
);
1320 int qemu_savevm_state_iterate(Monitor
*mon
, QEMUFile
*f
)
1325 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1326 if (se
->save_live_state
== NULL
)
1330 qemu_put_byte(f
, QEMU_VM_SECTION_PART
);
1331 qemu_put_be32(f
, se
->section_id
);
1333 ret
= se
->save_live_state(mon
, f
, QEMU_VM_SECTION_PART
, se
->opaque
);
1335 /* Do not proceed to the next vmstate before this one reported
1336 completion of the current stage. This serializes the migration
1337 and reduces the probability that a faster changing state is
1338 synchronized over and over again. */
1346 if (qemu_file_has_error(f
)) {
1347 qemu_savevm_state_cancel(mon
, f
);
1354 int qemu_savevm_state_complete(Monitor
*mon
, QEMUFile
*f
)
1358 cpu_synchronize_all_states();
1360 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1361 if (se
->save_live_state
== NULL
)
1365 qemu_put_byte(f
, QEMU_VM_SECTION_END
);
1366 qemu_put_be32(f
, se
->section_id
);
1368 se
->save_live_state(mon
, f
, QEMU_VM_SECTION_END
, se
->opaque
);
1371 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1374 if (se
->save_state
== NULL
&& se
->vmsd
== NULL
)
1378 qemu_put_byte(f
, QEMU_VM_SECTION_FULL
);
1379 qemu_put_be32(f
, se
->section_id
);
1382 len
= strlen(se
->idstr
);
1383 qemu_put_byte(f
, len
);
1384 qemu_put_buffer(f
, (uint8_t *)se
->idstr
, len
);
1386 qemu_put_be32(f
, se
->instance_id
);
1387 qemu_put_be32(f
, se
->version_id
);
1389 vmstate_save(f
, se
);
1392 qemu_put_byte(f
, QEMU_VM_EOF
);
1394 if (qemu_file_has_error(f
))
1400 void qemu_savevm_state_cancel(Monitor
*mon
, QEMUFile
*f
)
1404 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1405 if (se
->save_live_state
) {
1406 se
->save_live_state(mon
, f
, -1, se
->opaque
);
1411 static int qemu_savevm_state(Monitor
*mon
, QEMUFile
*f
)
1413 int saved_vm_running
;
1416 saved_vm_running
= vm_running
;
1421 ret
= qemu_savevm_state_begin(mon
, f
, 0, 0);
1426 ret
= qemu_savevm_state_iterate(mon
, f
);
1431 ret
= qemu_savevm_state_complete(mon
, f
);
1434 if (qemu_file_has_error(f
))
1437 if (!ret
&& saved_vm_running
)
1443 static SaveStateEntry
*find_se(const char *idstr
, int instance_id
)
1447 QTAILQ_FOREACH(se
, &savevm_handlers
, entry
) {
1448 if (!strcmp(se
->idstr
, idstr
) &&
1449 (instance_id
== se
->instance_id
||
1450 instance_id
== se
->alias_id
))
1456 typedef struct LoadStateEntry
{
1457 QLIST_ENTRY(LoadStateEntry
) entry
;
1463 int qemu_loadvm_state(QEMUFile
*f
)
1465 QLIST_HEAD(, LoadStateEntry
) loadvm_handlers
=
1466 QLIST_HEAD_INITIALIZER(loadvm_handlers
);
1467 LoadStateEntry
*le
, *new_le
;
1468 uint8_t section_type
;
1472 v
= qemu_get_be32(f
);
1473 if (v
!= QEMU_VM_FILE_MAGIC
)
1476 v
= qemu_get_be32(f
);
1477 if (v
== QEMU_VM_FILE_VERSION_COMPAT
) {
1478 fprintf(stderr
, "SaveVM v2 format is obsolete and don't work anymore\n");
1481 if (v
!= QEMU_VM_FILE_VERSION
)
1484 while ((section_type
= qemu_get_byte(f
)) != QEMU_VM_EOF
) {
1485 uint32_t instance_id
, version_id
, section_id
;
1490 switch (section_type
) {
1491 case QEMU_VM_SECTION_START
:
1492 case QEMU_VM_SECTION_FULL
:
1493 /* Read section start */
1494 section_id
= qemu_get_be32(f
);
1495 len
= qemu_get_byte(f
);
1496 qemu_get_buffer(f
, (uint8_t *)idstr
, len
);
1498 instance_id
= qemu_get_be32(f
);
1499 version_id
= qemu_get_be32(f
);
1501 /* Find savevm section */
1502 se
= find_se(idstr
, instance_id
);
1504 fprintf(stderr
, "Unknown savevm section or instance '%s' %d\n", idstr
, instance_id
);
1509 /* Validate version */
1510 if (version_id
> se
->version_id
) {
1511 fprintf(stderr
, "savevm: unsupported version %d for '%s' v%d\n",
1512 version_id
, idstr
, se
->version_id
);
1518 le
= qemu_mallocz(sizeof(*le
));
1521 le
->section_id
= section_id
;
1522 le
->version_id
= version_id
;
1523 QLIST_INSERT_HEAD(&loadvm_handlers
, le
, entry
);
1525 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1527 fprintf(stderr
, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1528 instance_id
, idstr
);
1532 case QEMU_VM_SECTION_PART
:
1533 case QEMU_VM_SECTION_END
:
1534 section_id
= qemu_get_be32(f
);
1536 QLIST_FOREACH(le
, &loadvm_handlers
, entry
) {
1537 if (le
->section_id
== section_id
) {
1542 fprintf(stderr
, "Unknown savevm section %d\n", section_id
);
1547 ret
= vmstate_load(f
, le
->se
, le
->version_id
);
1549 fprintf(stderr
, "qemu: warning: error while loading state section id %d\n",
1555 fprintf(stderr
, "Unknown savevm section type %d\n", section_type
);
1561 cpu_synchronize_all_post_init();
1566 QLIST_FOREACH_SAFE(le
, &loadvm_handlers
, entry
, new_le
) {
1567 QLIST_REMOVE(le
, entry
);
1571 if (qemu_file_has_error(f
))
1577 /* device can contain snapshots */
1578 static int bdrv_can_snapshot(BlockDriverState
*bs
)
1581 !bdrv_is_removable(bs
) &&
1582 !bdrv_is_read_only(bs
));
1585 /* device must be snapshots in order to have a reliable snapshot */
1586 static int bdrv_has_snapshot(BlockDriverState
*bs
)
1589 !bdrv_is_removable(bs
) &&
1590 !bdrv_is_read_only(bs
));
1593 static BlockDriverState
*get_bs_snapshots(void)
1595 BlockDriverState
*bs
;
1599 return bs_snapshots
;
1600 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1602 if (bdrv_can_snapshot(bs
))
1611 static int bdrv_snapshot_find(BlockDriverState
*bs
, QEMUSnapshotInfo
*sn_info
,
1614 QEMUSnapshotInfo
*sn_tab
, *sn
;
1618 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1621 for(i
= 0; i
< nb_sns
; i
++) {
1623 if (!strcmp(sn
->id_str
, name
) || !strcmp(sn
->name
, name
)) {
1634 * Deletes snapshots of a given name in all opened images.
1636 static int del_existing_snapshots(Monitor
*mon
, const char *name
)
1638 BlockDriverState
*bs
;
1640 QEMUSnapshotInfo sn1
, *snapshot
= &sn1
;
1643 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1645 if (bdrv_can_snapshot(bs
) &&
1646 bdrv_snapshot_find(bs
, snapshot
, name
) >= 0)
1648 ret
= bdrv_snapshot_delete(bs
, name
);
1651 "Error while deleting snapshot on '%s'\n",
1652 bdrv_get_device_name(bs
));
1661 void do_savevm(Monitor
*mon
, const QDict
*qdict
)
1664 BlockDriverState
*bs
, *bs1
;
1665 QEMUSnapshotInfo sn1
, *sn
= &sn1
, old_sn1
, *old_sn
= &old_sn1
;
1668 int saved_vm_running
;
1669 uint32_t vm_state_size
;
1675 const char *name
= qdict_get_try_str(qdict
, "name");
1677 bs
= get_bs_snapshots();
1679 monitor_printf(mon
, "No block device can accept snapshots\n");
1683 /* ??? Should this occur after vm_stop? */
1686 saved_vm_running
= vm_running
;
1689 memset(sn
, 0, sizeof(*sn
));
1691 ret
= bdrv_snapshot_find(bs
, old_sn
, name
);
1693 pstrcpy(sn
->name
, sizeof(sn
->name
), old_sn
->name
);
1694 pstrcpy(sn
->id_str
, sizeof(sn
->id_str
), old_sn
->id_str
);
1696 pstrcpy(sn
->name
, sizeof(sn
->name
), name
);
1700 /* fill auxiliary fields */
1703 sn
->date_sec
= tb
.time
;
1704 sn
->date_nsec
= tb
.millitm
* 1000000;
1706 gettimeofday(&tv
, NULL
);
1707 sn
->date_sec
= tv
.tv_sec
;
1708 sn
->date_nsec
= tv
.tv_usec
* 1000;
1710 sn
->vm_clock_nsec
= qemu_get_clock(vm_clock
);
1712 /* Delete old snapshots of the same name */
1713 if (name
&& del_existing_snapshots(mon
, name
) < 0) {
1717 /* save the VM state */
1718 f
= qemu_fopen_bdrv(bs
, 1);
1720 monitor_printf(mon
, "Could not open VM state file\n");
1723 ret
= qemu_savevm_state(mon
, f
);
1724 vm_state_size
= qemu_ftell(f
);
1727 monitor_printf(mon
, "Error %d while writing VM\n", ret
);
1731 /* create the snapshots */
1733 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1735 if (bdrv_has_snapshot(bs1
)) {
1736 /* Write VM state size only to the image that contains the state */
1737 sn
->vm_state_size
= (bs
== bs1
? vm_state_size
: 0);
1738 ret
= bdrv_snapshot_create(bs1
, sn
);
1740 monitor_printf(mon
, "Error while creating snapshot on '%s'\n",
1741 bdrv_get_device_name(bs1
));
1747 if (saved_vm_running
)
1751 int load_vmstate(const char *name
)
1754 BlockDriverState
*bs
, *bs1
;
1755 QEMUSnapshotInfo sn
;
1759 bs
= get_bs_snapshots();
1761 error_report("No block device supports snapshots");
1765 /* Flush all IO requests so they don't interfere with the new state. */
1768 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1770 if (bdrv_has_snapshot(bs1
)) {
1771 ret
= bdrv_snapshot_goto(bs1
, name
);
1775 error_report("%sSnapshots not supported on device '%s'",
1776 bs
!= bs1
? "Warning: " : "",
1777 bdrv_get_device_name(bs1
));
1780 error_report("%sCould not find snapshot '%s' on device '%s'",
1781 bs
!= bs1
? "Warning: " : "",
1782 name
, bdrv_get_device_name(bs1
));
1785 error_report("%sError %d while activating snapshot on '%s'",
1786 bs
!= bs1
? "Warning: " : "",
1787 ret
, bdrv_get_device_name(bs1
));
1790 /* fatal on snapshot block device */
1797 /* Don't even try to load empty VM states */
1798 ret
= bdrv_snapshot_find(bs
, &sn
, name
);
1799 if ((ret
>= 0) && (sn
.vm_state_size
== 0))
1802 /* restore the VM state */
1803 f
= qemu_fopen_bdrv(bs
, 0);
1805 error_report("Could not open VM state file");
1808 ret
= qemu_loadvm_state(f
);
1811 error_report("Error %d while loading VM state", ret
);
1817 void do_delvm(Monitor
*mon
, const QDict
*qdict
)
1820 BlockDriverState
*bs
, *bs1
;
1822 const char *name
= qdict_get_str(qdict
, "name");
1824 bs
= get_bs_snapshots();
1826 monitor_printf(mon
, "No block device supports snapshots\n");
1830 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1832 if (bdrv_has_snapshot(bs1
)) {
1833 ret
= bdrv_snapshot_delete(bs1
, name
);
1835 if (ret
== -ENOTSUP
)
1837 "Snapshots not supported on device '%s'\n",
1838 bdrv_get_device_name(bs1
));
1840 monitor_printf(mon
, "Error %d while deleting snapshot on "
1841 "'%s'\n", ret
, bdrv_get_device_name(bs1
));
1847 void do_info_snapshots(Monitor
*mon
)
1850 BlockDriverState
*bs
, *bs1
;
1851 QEMUSnapshotInfo
*sn_tab
, *sn
;
1855 bs
= get_bs_snapshots();
1857 monitor_printf(mon
, "No available block device supports snapshots\n");
1860 monitor_printf(mon
, "Snapshot devices:");
1861 QTAILQ_FOREACH(dinfo
, &drives
, next
) {
1863 if (bdrv_has_snapshot(bs1
)) {
1865 monitor_printf(mon
, " %s", bdrv_get_device_name(bs1
));
1868 monitor_printf(mon
, "\n");
1870 nb_sns
= bdrv_snapshot_list(bs
, &sn_tab
);
1872 monitor_printf(mon
, "bdrv_snapshot_list: error %d\n", nb_sns
);
1875 monitor_printf(mon
, "Snapshot list (from %s):\n",
1876 bdrv_get_device_name(bs
));
1877 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), NULL
));
1878 for(i
= 0; i
< nb_sns
; i
++) {
1880 monitor_printf(mon
, "%s\n", bdrv_snapshot_dump(buf
, sizeof(buf
), sn
));