4 * Copyright (c) 2003-2008 Fabrice Bellard
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
36 #include <sys/times.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
45 #include <arpa/inet.h>
48 #include <sys/select.h>
51 #if defined(__FreeBSD__) || defined(__DragonFly__)
56 #elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
57 #include <freebsd/stdlib.h>
62 #include <linux/rtc.h>
70 #include <sys/timeb.h>
72 #define getopt_long_only getopt_long
73 #define memalign(align, size) malloc(size)
76 #include "qemu-common.h"
81 #include "qemu-timer.h"
82 #include "qemu-char.h"
84 #include "audio/audio.h"
85 #include "migration.h"
86 #include "qemu_socket.h"
87 #include "qemu-queue.h"
89 /* point to the block driver where the snapshots are managed */
90 static BlockDriverState *bs_snapshots;
92 #define SELF_ANNOUNCE_ROUNDS 5
95 #define ETH_P_RARP 0x0835
97 #define ARP_HTYPE_ETH 0x0001
98 #define ARP_PTYPE_IP 0x0800
99 #define ARP_OP_REQUEST_REV 0x3
101 static int announce_self_create(uint8_t *buf,
104 /* Ethernet header. */
105 memset(buf, 0xff, 6); /* destination MAC addr */
106 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
107 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
110 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
111 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
112 *(buf + 18) = 6; /* hardware addr length (ethernet) */
113 *(buf + 19) = 4; /* protocol addr length (IPv4) */
114 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
115 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
116 memset(buf + 28, 0x00, 4); /* source protocol addr */
117 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
118 memset(buf + 38, 0x00, 4); /* target protocol addr */
120 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
121 memset(buf + 42, 0x00, 18);
123 return 60; /* len (FCS will be added by hardware) */
126 static void qemu_announce_self_once(void *opaque)
132 static int count = SELF_ANNOUNCE_ROUNDS;
133 QEMUTimer *timer = *(QEMUTimer **)opaque;
135 for (i = 0; i < MAX_NICS; i++) {
136 if (!nd_table[i].used)
138 len = announce_self_create(buf, nd_table[i].macaddr);
139 vlan = nd_table[i].vlan;
140 QTAILQ_FOREACH(vc, &vlan->clients, next) {
141 qemu_send_packet_raw(vc, buf, len);
145 /* delay 50ms, 150ms, 250ms, ... */
146 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
147 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
149 qemu_del_timer(timer);
150 qemu_free_timer(timer);
154 void qemu_announce_self(void)
156 static QEMUTimer *timer;
157 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
158 qemu_announce_self_once(&timer);
161 /***********************************************************/
162 /* savevm/loadvm support */
164 #define IO_BUF_SIZE 32768
167 QEMUFilePutBufferFunc *put_buffer;
168 QEMUFileGetBufferFunc *get_buffer;
169 QEMUFileCloseFunc *close;
170 QEMUFileRateLimit *rate_limit;
171 QEMUFileSetRateLimit *set_rate_limit;
172 QEMUFileGetRateLimit *get_rate_limit;
176 int64_t buf_offset; /* start of buffer when writing, end of buffer
179 int buf_size; /* 0 when writing */
180 uint8_t buf[IO_BUF_SIZE];
185 typedef struct QEMUFileStdio
191 typedef struct QEMUFileSocket
197 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
199 QEMUFileSocket *s = opaque;
203 len = recv(s->fd, (void *)buf, size, 0);
204 } while (len == -1 && socket_error() == EINTR);
207 len = -socket_error();
212 static int socket_close(void *opaque)
214 QEMUFileSocket *s = opaque;
219 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
221 QEMUFileStdio *s = opaque;
222 return fwrite(buf, 1, size, s->stdio_file);
225 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
227 QEMUFileStdio *s = opaque;
228 FILE *fp = s->stdio_file;
233 bytes = fread(buf, 1, size, fp);
234 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
238 static int stdio_pclose(void *opaque)
240 QEMUFileStdio *s = opaque;
241 pclose(s->stdio_file);
246 static int stdio_fclose(void *opaque)
248 QEMUFileStdio *s = opaque;
249 fclose(s->stdio_file);
254 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
258 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
259 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
263 s = qemu_mallocz(sizeof(QEMUFileStdio));
265 s->stdio_file = stdio_file;
268 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
271 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
277 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
281 popen_file = popen(command, mode);
282 if(popen_file == NULL) {
286 return qemu_popen(popen_file, mode);
289 int qemu_stdio_fd(QEMUFile *f)
294 p = (QEMUFileStdio *)f->opaque;
295 fd = fileno(p->stdio_file);
300 QEMUFile *qemu_fdopen(int fd, const char *mode)
305 (mode[0] != 'r' && mode[0] != 'w') ||
306 mode[1] != 'b' || mode[2] != 0) {
307 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
311 s = qemu_mallocz(sizeof(QEMUFileStdio));
312 s->stdio_file = fdopen(fd, mode);
317 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
320 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
330 QEMUFile *qemu_fopen_socket(int fd)
332 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
335 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
340 static int file_put_buffer(void *opaque, const uint8_t *buf,
341 int64_t pos, int size)
343 QEMUFileStdio *s = opaque;
344 fseek(s->stdio_file, pos, SEEK_SET);
345 fwrite(buf, 1, size, s->stdio_file);
349 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
351 QEMUFileStdio *s = opaque;
352 fseek(s->stdio_file, pos, SEEK_SET);
353 return fread(buf, 1, size, s->stdio_file);
356 QEMUFile *qemu_fopen(const char *filename, const char *mode)
361 (mode[0] != 'r' && mode[0] != 'w') ||
362 mode[1] != 'b' || mode[2] != 0) {
363 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
367 s = qemu_mallocz(sizeof(QEMUFileStdio));
369 s->stdio_file = fopen(filename, mode);
374 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
377 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
386 static int block_put_buffer(void *opaque, const uint8_t *buf,
387 int64_t pos, int size)
389 bdrv_save_vmstate(opaque, buf, pos, size);
393 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
395 return bdrv_load_vmstate(opaque, buf, pos, size);
398 static int bdrv_fclose(void *opaque)
403 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
406 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
408 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
411 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
412 QEMUFileGetBufferFunc *get_buffer,
413 QEMUFileCloseFunc *close,
414 QEMUFileRateLimit *rate_limit,
415 QEMUFileSetRateLimit *set_rate_limit,
416 QEMUFileGetRateLimit *get_rate_limit)
420 f = qemu_mallocz(sizeof(QEMUFile));
423 f->put_buffer = put_buffer;
424 f->get_buffer = get_buffer;
426 f->rate_limit = rate_limit;
427 f->set_rate_limit = set_rate_limit;
428 f->get_rate_limit = get_rate_limit;
434 int qemu_file_has_error(QEMUFile *f)
439 void qemu_file_set_error(QEMUFile *f)
444 void qemu_fflush(QEMUFile *f)
449 if (f->is_write && f->buf_index > 0) {
452 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
454 f->buf_offset += f->buf_index;
461 static void qemu_fill_buffer(QEMUFile *f)
471 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
475 f->buf_offset += len;
476 } else if (len != -EAGAIN)
480 int qemu_fclose(QEMUFile *f)
485 ret = f->close(f->opaque);
490 void qemu_file_put_notify(QEMUFile *f)
492 f->put_buffer(f->opaque, NULL, 0, 0);
495 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
499 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
501 "Attempted to write to buffer while read buffer is not empty\n");
505 while (!f->has_error && size > 0) {
506 l = IO_BUF_SIZE - f->buf_index;
509 memcpy(f->buf + f->buf_index, buf, l);
514 if (f->buf_index >= IO_BUF_SIZE)
519 void qemu_put_byte(QEMUFile *f, int v)
521 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
523 "Attempted to write to buffer while read buffer is not empty\n");
527 f->buf[f->buf_index++] = v;
529 if (f->buf_index >= IO_BUF_SIZE)
533 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
542 l = f->buf_size - f->buf_index;
545 l = f->buf_size - f->buf_index;
551 memcpy(buf, f->buf + f->buf_index, l);
559 int qemu_get_byte(QEMUFile *f)
564 if (f->buf_index >= f->buf_size) {
566 if (f->buf_index >= f->buf_size)
569 return f->buf[f->buf_index++];
572 int64_t qemu_ftell(QEMUFile *f)
574 return f->buf_offset - f->buf_size + f->buf_index;
577 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
579 if (whence == SEEK_SET) {
581 } else if (whence == SEEK_CUR) {
582 pos += qemu_ftell(f);
584 /* SEEK_END not supported */
598 int qemu_file_rate_limit(QEMUFile *f)
601 return f->rate_limit(f->opaque);
606 size_t qemu_file_get_rate_limit(QEMUFile *f)
608 if (f->get_rate_limit)
609 return f->get_rate_limit(f->opaque);
614 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
616 /* any failed or completed migration keeps its state to allow probing of
617 * migration data, but has no associated file anymore */
618 if (f && f->set_rate_limit)
619 return f->set_rate_limit(f->opaque, new_rate);
624 void qemu_put_be16(QEMUFile *f, unsigned int v)
626 qemu_put_byte(f, v >> 8);
630 void qemu_put_be32(QEMUFile *f, unsigned int v)
632 qemu_put_byte(f, v >> 24);
633 qemu_put_byte(f, v >> 16);
634 qemu_put_byte(f, v >> 8);
638 void qemu_put_be64(QEMUFile *f, uint64_t v)
640 qemu_put_be32(f, v >> 32);
644 unsigned int qemu_get_be16(QEMUFile *f)
647 v = qemu_get_byte(f) << 8;
648 v |= qemu_get_byte(f);
652 unsigned int qemu_get_be32(QEMUFile *f)
655 v = qemu_get_byte(f) << 24;
656 v |= qemu_get_byte(f) << 16;
657 v |= qemu_get_byte(f) << 8;
658 v |= qemu_get_byte(f);
662 uint64_t qemu_get_be64(QEMUFile *f)
665 v = (uint64_t)qemu_get_be32(f) << 32;
666 v |= qemu_get_be32(f);
672 static int get_int8(QEMUFile *f, void *pv, size_t size)
679 static void put_int8(QEMUFile *f, void *pv, size_t size)
685 const VMStateInfo vmstate_info_int8 = {
693 static int get_int16(QEMUFile *f, void *pv, size_t size)
696 qemu_get_sbe16s(f, v);
700 static void put_int16(QEMUFile *f, void *pv, size_t size)
703 qemu_put_sbe16s(f, v);
706 const VMStateInfo vmstate_info_int16 = {
714 static int get_int32(QEMUFile *f, void *pv, size_t size)
717 qemu_get_sbe32s(f, v);
721 static void put_int32(QEMUFile *f, void *pv, size_t size)
724 qemu_put_sbe32s(f, v);
727 const VMStateInfo vmstate_info_int32 = {
733 /* 32 bit int. See that the received value is the same than the one
736 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
740 qemu_get_sbe32s(f, &v2);
747 const VMStateInfo vmstate_info_int32_equal = {
748 .name = "int32 equal",
749 .get = get_int32_equal,
753 /* 32 bit int. See that the received value is the less or the same
754 than the one in the field */
756 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
760 qemu_get_sbe32s(f, &new);
767 const VMStateInfo vmstate_info_int32_le = {
768 .name = "int32 equal",
775 static int get_int64(QEMUFile *f, void *pv, size_t size)
778 qemu_get_sbe64s(f, v);
782 static void put_int64(QEMUFile *f, void *pv, size_t size)
785 qemu_put_sbe64s(f, v);
788 const VMStateInfo vmstate_info_int64 = {
794 /* 8 bit unsigned int */
796 static int get_uint8(QEMUFile *f, void *pv, size_t size)
803 static void put_uint8(QEMUFile *f, void *pv, size_t size)
809 const VMStateInfo vmstate_info_uint8 = {
815 /* 16 bit unsigned int */
817 static int get_uint16(QEMUFile *f, void *pv, size_t size)
820 qemu_get_be16s(f, v);
824 static void put_uint16(QEMUFile *f, void *pv, size_t size)
827 qemu_put_be16s(f, v);
830 const VMStateInfo vmstate_info_uint16 = {
836 /* 32 bit unsigned int */
838 static int get_uint32(QEMUFile *f, void *pv, size_t size)
841 qemu_get_be32s(f, v);
845 static void put_uint32(QEMUFile *f, void *pv, size_t size)
848 qemu_put_be32s(f, v);
851 const VMStateInfo vmstate_info_uint32 = {
857 /* 64 bit unsigned int */
859 static int get_uint64(QEMUFile *f, void *pv, size_t size)
862 qemu_get_be64s(f, v);
866 static void put_uint64(QEMUFile *f, void *pv, size_t size)
869 qemu_put_be64s(f, v);
872 const VMStateInfo vmstate_info_uint64 = {
878 /* 8 bit int. See that the received value is the same than the one
881 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
892 const VMStateInfo vmstate_info_uint8_equal = {
893 .name = "uint8 equal",
894 .get = get_uint8_equal,
898 /* 16 bit unsigned int int. See that the received value is the same than the one
901 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
905 qemu_get_be16s(f, &v2);
912 const VMStateInfo vmstate_info_uint16_equal = {
913 .name = "uint16 equal",
914 .get = get_uint16_equal,
920 static int get_timer(QEMUFile *f, void *pv, size_t size)
923 qemu_get_timer(f, v);
927 static void put_timer(QEMUFile *f, void *pv, size_t size)
930 qemu_put_timer(f, v);
933 const VMStateInfo vmstate_info_timer = {
939 /* uint8_t buffers */
941 static int get_buffer(QEMUFile *f, void *pv, size_t size)
944 qemu_get_buffer(f, v, size);
948 static void put_buffer(QEMUFile *f, void *pv, size_t size)
951 qemu_put_buffer(f, v, size);
954 const VMStateInfo vmstate_info_buffer = {
960 /* unused buffers: space that was used for some fields that are
961 not usefull anymore */
963 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
965 qemu_fseek(f, size, SEEK_CUR);
969 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
971 qemu_fseek(f, size, SEEK_CUR);
974 const VMStateInfo vmstate_info_unused_buffer = {
975 .name = "unused_buffer",
976 .get = get_unused_buffer,
977 .put = put_unused_buffer,
980 typedef struct SaveStateEntry {
981 QTAILQ_ENTRY(SaveStateEntry) entry;
986 SaveSetParamsHandler *set_params;
987 SaveLiveStateHandler *save_live_state;
988 SaveStateHandler *save_state;
989 LoadStateHandler *load_state;
990 const VMStateDescription *vmsd;
995 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
996 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
997 static int global_section_id;
999 static int calculate_new_instance_id(const char *idstr)
1002 int instance_id = 0;
1004 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1005 if (strcmp(idstr, se->idstr) == 0
1006 && instance_id <= se->instance_id) {
1007 instance_id = se->instance_id + 1;
1013 /* TODO: Individual devices generally have very little idea about the rest
1014 of the system, so instance_id should be removed/replaced.
1015 Meanwhile pass -1 as instance_id if you do not already have a clearly
1016 distinguishing id for all instances of your device class. */
1017 int register_savevm_live(const char *idstr,
1020 SaveSetParamsHandler *set_params,
1021 SaveLiveStateHandler *save_live_state,
1022 SaveStateHandler *save_state,
1023 LoadStateHandler *load_state,
1028 se = qemu_mallocz(sizeof(SaveStateEntry));
1029 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1030 se->version_id = version_id;
1031 se->section_id = global_section_id++;
1032 se->set_params = set_params;
1033 se->save_live_state = save_live_state;
1034 se->save_state = save_state;
1035 se->load_state = load_state;
1036 se->opaque = opaque;
1039 if (instance_id == -1) {
1040 se->instance_id = calculate_new_instance_id(idstr);
1042 se->instance_id = instance_id;
1044 /* add at the end of list */
1045 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1049 int register_savevm(const char *idstr,
1052 SaveStateHandler *save_state,
1053 LoadStateHandler *load_state,
1056 return register_savevm_live(idstr, instance_id, version_id,
1057 NULL, NULL, save_state, load_state, opaque);
1060 void unregister_savevm(const char *idstr, void *opaque)
1062 SaveStateEntry *se, *new_se;
1064 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1065 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1066 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1072 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1077 se = qemu_mallocz(sizeof(SaveStateEntry));
1078 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1079 se->version_id = vmsd->version_id;
1080 se->section_id = global_section_id++;
1081 se->save_live_state = NULL;
1082 se->save_state = NULL;
1083 se->load_state = NULL;
1084 se->opaque = opaque;
1087 if (instance_id == -1) {
1088 se->instance_id = calculate_new_instance_id(vmsd->name);
1090 se->instance_id = instance_id;
1092 /* add at the end of list */
1093 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1097 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1099 SaveStateEntry *se, *new_se;
1101 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1102 if (se->vmsd == vmsd && se->opaque == opaque) {
1103 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1109 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1110 void *opaque, int version_id)
1112 VMStateField *field = vmsd->fields;
1114 if (version_id > vmsd->version_id) {
1117 if (version_id < vmsd->minimum_version_id_old) {
1120 if (version_id < vmsd->minimum_version_id) {
1121 return vmsd->load_state_old(f, opaque, version_id);
1123 if (vmsd->pre_load) {
1124 int ret = vmsd->pre_load(opaque);
1128 while(field->name) {
1129 if ((field->field_exists &&
1130 field->field_exists(opaque, version_id)) ||
1131 (!field->field_exists &&
1132 field->version_id <= version_id)) {
1133 void *base_addr = opaque + field->offset;
1134 int ret, i, n_elems = 1;
1136 if (field->flags & VMS_ARRAY) {
1137 n_elems = field->num;
1138 } else if (field->flags & VMS_VARRAY_INT32) {
1139 n_elems = *(int32_t *)(opaque+field->num_offset);
1140 } else if (field->flags & VMS_VARRAY_UINT16) {
1141 n_elems = *(uint16_t *)(opaque+field->num_offset);
1143 if (field->flags & VMS_POINTER) {
1144 base_addr = *(void **)base_addr;
1146 for (i = 0; i < n_elems; i++) {
1147 void *addr = base_addr + field->size * i;
1149 if (field->flags & VMS_ARRAY_OF_POINTER) {
1150 addr = *(void **)addr;
1152 if (field->flags & VMS_STRUCT) {
1153 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1155 ret = field->info->get(f, addr, field->size);
1165 if (vmsd->post_load) {
1166 return vmsd->post_load(opaque, version_id);
1171 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1174 VMStateField *field = vmsd->fields;
1176 if (vmsd->pre_save) {
1177 vmsd->pre_save(opaque);
1179 while(field->name) {
1180 if (!field->field_exists ||
1181 field->field_exists(opaque, vmsd->version_id)) {
1182 void *base_addr = opaque + field->offset;
1185 if (field->flags & VMS_ARRAY) {
1186 n_elems = field->num;
1187 } else if (field->flags & VMS_VARRAY_INT32) {
1188 n_elems = *(int32_t *)(opaque+field->num_offset);
1189 } else if (field->flags & VMS_VARRAY_UINT16) {
1190 n_elems = *(uint16_t *)(opaque+field->num_offset);
1192 if (field->flags & VMS_POINTER) {
1193 base_addr = *(void **)base_addr;
1195 for (i = 0; i < n_elems; i++) {
1196 void *addr = base_addr + field->size * i;
1198 if (field->flags & VMS_STRUCT) {
1199 vmstate_save_state(f, field->vmsd, addr);
1201 field->info->put(f, addr, field->size);
1207 if (vmsd->post_save) {
1208 vmsd->post_save(opaque);
1212 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1214 if (!se->vmsd) { /* Old style */
1215 return se->load_state(f, se->opaque, version_id);
1217 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1220 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1222 if (!se->vmsd) { /* Old style */
1223 se->save_state(f, se->opaque);
1226 vmstate_save_state(f,se->vmsd, se->opaque);
1229 #define QEMU_VM_FILE_MAGIC 0x5145564d
1230 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1231 #define QEMU_VM_FILE_VERSION 0x00000003
1233 #define QEMU_VM_EOF 0x00
1234 #define QEMU_VM_SECTION_START 0x01
1235 #define QEMU_VM_SECTION_PART 0x02
1236 #define QEMU_VM_SECTION_END 0x03
1237 #define QEMU_VM_SECTION_FULL 0x04
1239 int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared)
1243 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1244 if(se->set_params == NULL) {
1247 se->set_params(blk_enable, shared, se->opaque);
1250 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1251 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1253 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1256 if (se->save_live_state == NULL)
1260 qemu_put_byte(f, QEMU_VM_SECTION_START);
1261 qemu_put_be32(f, se->section_id);
1264 len = strlen(se->idstr);
1265 qemu_put_byte(f, len);
1266 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1268 qemu_put_be32(f, se->instance_id);
1269 qemu_put_be32(f, se->version_id);
1271 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1274 if (qemu_file_has_error(f))
1280 int qemu_savevm_state_iterate(QEMUFile *f)
1285 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1286 if (se->save_live_state == NULL)
1290 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1291 qemu_put_be32(f, se->section_id);
1293 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1299 if (qemu_file_has_error(f))
1305 int qemu_savevm_state_complete(QEMUFile *f)
1309 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1310 if (se->save_live_state == NULL)
1314 qemu_put_byte(f, QEMU_VM_SECTION_END);
1315 qemu_put_be32(f, se->section_id);
1317 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1320 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1323 if (se->save_state == NULL && se->vmsd == NULL)
1327 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1328 qemu_put_be32(f, se->section_id);
1331 len = strlen(se->idstr);
1332 qemu_put_byte(f, len);
1333 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1335 qemu_put_be32(f, se->instance_id);
1336 qemu_put_be32(f, se->version_id);
1338 vmstate_save(f, se);
1341 qemu_put_byte(f, QEMU_VM_EOF);
1343 if (qemu_file_has_error(f))
1349 int qemu_savevm_state(QEMUFile *f)
1351 int saved_vm_running;
1354 saved_vm_running = vm_running;
1359 ret = qemu_savevm_state_begin(f, 0, 0);
1364 ret = qemu_savevm_state_iterate(f);
1369 ret = qemu_savevm_state_complete(f);
1372 if (qemu_file_has_error(f))
1375 if (!ret && saved_vm_running)
1381 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1385 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1386 if (!strcmp(se->idstr, idstr) &&
1387 instance_id == se->instance_id)
1393 typedef struct LoadStateEntry {
1394 QLIST_ENTRY(LoadStateEntry) entry;
1400 int qemu_loadvm_state(QEMUFile *f)
1402 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1403 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1404 LoadStateEntry *le, *new_le;
1405 uint8_t section_type;
1409 v = qemu_get_be32(f);
1410 if (v != QEMU_VM_FILE_MAGIC)
1413 v = qemu_get_be32(f);
1414 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1415 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1418 if (v != QEMU_VM_FILE_VERSION)
1421 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1422 uint32_t instance_id, version_id, section_id;
1427 switch (section_type) {
1428 case QEMU_VM_SECTION_START:
1429 case QEMU_VM_SECTION_FULL:
1430 /* Read section start */
1431 section_id = qemu_get_be32(f);
1432 len = qemu_get_byte(f);
1433 qemu_get_buffer(f, (uint8_t *)idstr, len);
1435 instance_id = qemu_get_be32(f);
1436 version_id = qemu_get_be32(f);
1438 /* Find savevm section */
1439 se = find_se(idstr, instance_id);
1441 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1446 /* Validate version */
1447 if (version_id > se->version_id) {
1448 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1449 version_id, idstr, se->version_id);
1455 le = qemu_mallocz(sizeof(*le));
1458 le->section_id = section_id;
1459 le->version_id = version_id;
1460 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1462 ret = vmstate_load(f, le->se, le->version_id);
1464 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1465 instance_id, idstr);
1469 case QEMU_VM_SECTION_PART:
1470 case QEMU_VM_SECTION_END:
1471 section_id = qemu_get_be32(f);
1473 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1474 if (le->section_id == section_id) {
1479 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1484 ret = vmstate_load(f, le->se, le->version_id);
1486 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1492 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1501 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1502 QLIST_REMOVE(le, entry);
1506 if (qemu_file_has_error(f))
1512 /* device can contain snapshots */
1513 static int bdrv_can_snapshot(BlockDriverState *bs)
1516 !bdrv_is_removable(bs) &&
1517 !bdrv_is_read_only(bs));
1520 /* device must be snapshots in order to have a reliable snapshot */
1521 static int bdrv_has_snapshot(BlockDriverState *bs)
1524 !bdrv_is_removable(bs) &&
1525 !bdrv_is_read_only(bs));
1528 static BlockDriverState *get_bs_snapshots(void)
1530 BlockDriverState *bs;
1534 return bs_snapshots;
1535 QTAILQ_FOREACH(dinfo, &drives, next) {
1537 if (bdrv_can_snapshot(bs))
1546 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1549 QEMUSnapshotInfo *sn_tab, *sn;
1553 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1556 for(i = 0; i < nb_sns; i++) {
1558 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1569 * Deletes snapshots of a given name in all opened images.
1571 static int del_existing_snapshots(Monitor *mon, const char *name)
1573 BlockDriverState *bs;
1575 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1578 QTAILQ_FOREACH(dinfo, &drives, next) {
1580 if (bdrv_can_snapshot(bs) &&
1581 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1583 ret = bdrv_snapshot_delete(bs, name);
1586 "Error while deleting snapshot on '%s'\n",
1587 bdrv_get_device_name(bs));
1596 void do_savevm(Monitor *mon, const QDict *qdict)
1599 BlockDriverState *bs, *bs1;
1600 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1603 int saved_vm_running;
1604 uint32_t vm_state_size;
1610 const char *name = qdict_get_try_str(qdict, "name");
1612 bs = get_bs_snapshots();
1614 monitor_printf(mon, "No block device can accept snapshots\n");
1618 /* ??? Should this occur after vm_stop? */
1621 saved_vm_running = vm_running;
1624 memset(sn, 0, sizeof(*sn));
1626 ret = bdrv_snapshot_find(bs, old_sn, name);
1628 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1629 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1631 pstrcpy(sn->name, sizeof(sn->name), name);
1635 /* fill auxiliary fields */
1638 sn->date_sec = tb.time;
1639 sn->date_nsec = tb.millitm * 1000000;
1641 gettimeofday(&tv, NULL);
1642 sn->date_sec = tv.tv_sec;
1643 sn->date_nsec = tv.tv_usec * 1000;
1645 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1647 /* Delete old snapshots of the same name */
1648 if (del_existing_snapshots(mon, name) < 0) {
1652 /* save the VM state */
1653 f = qemu_fopen_bdrv(bs, 1);
1655 monitor_printf(mon, "Could not open VM state file\n");
1658 ret = qemu_savevm_state(f);
1659 vm_state_size = qemu_ftell(f);
1662 monitor_printf(mon, "Error %d while writing VM\n", ret);
1666 /* create the snapshots */
1668 QTAILQ_FOREACH(dinfo, &drives, next) {
1670 if (bdrv_has_snapshot(bs1)) {
1671 /* Write VM state size only to the image that contains the state */
1672 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1673 ret = bdrv_snapshot_create(bs1, sn);
1675 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1676 bdrv_get_device_name(bs1));
1682 if (saved_vm_running)
1686 int load_vmstate(Monitor *mon, const char *name)
1689 BlockDriverState *bs, *bs1;
1690 QEMUSnapshotInfo sn;
1694 bs = get_bs_snapshots();
1696 monitor_printf(mon, "No block device supports snapshots\n");
1700 /* Flush all IO requests so they don't interfere with the new state. */
1703 QTAILQ_FOREACH(dinfo, &drives, next) {
1705 if (bdrv_has_snapshot(bs1)) {
1706 ret = bdrv_snapshot_goto(bs1, name);
1709 monitor_printf(mon, "Warning: ");
1713 "Snapshots not supported on device '%s'\n",
1714 bdrv_get_device_name(bs1));
1717 monitor_printf(mon, "Could not find snapshot '%s' on "
1719 name, bdrv_get_device_name(bs1));
1722 monitor_printf(mon, "Error %d while activating snapshot on"
1723 " '%s'\n", ret, bdrv_get_device_name(bs1));
1726 /* fatal on snapshot block device */
1733 /* Don't even try to load empty VM states */
1734 ret = bdrv_snapshot_find(bs, &sn, name);
1735 if ((ret >= 0) && (sn.vm_state_size == 0))
1738 /* restore the VM state */
1739 f = qemu_fopen_bdrv(bs, 0);
1741 monitor_printf(mon, "Could not open VM state file\n");
1744 ret = qemu_loadvm_state(f);
1747 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1753 void do_delvm(Monitor *mon, const QDict *qdict)
1756 BlockDriverState *bs, *bs1;
1758 const char *name = qdict_get_str(qdict, "name");
1760 bs = get_bs_snapshots();
1762 monitor_printf(mon, "No block device supports snapshots\n");
1766 QTAILQ_FOREACH(dinfo, &drives, next) {
1768 if (bdrv_has_snapshot(bs1)) {
1769 ret = bdrv_snapshot_delete(bs1, name);
1771 if (ret == -ENOTSUP)
1773 "Snapshots not supported on device '%s'\n",
1774 bdrv_get_device_name(bs1));
1776 monitor_printf(mon, "Error %d while deleting snapshot on "
1777 "'%s'\n", ret, bdrv_get_device_name(bs1));
1783 void do_info_snapshots(Monitor *mon)
1786 BlockDriverState *bs, *bs1;
1787 QEMUSnapshotInfo *sn_tab, *sn;
1791 bs = get_bs_snapshots();
1793 monitor_printf(mon, "No available block device supports snapshots\n");
1796 monitor_printf(mon, "Snapshot devices:");
1797 QTAILQ_FOREACH(dinfo, &drives, next) {
1799 if (bdrv_has_snapshot(bs1)) {
1801 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1804 monitor_printf(mon, "\n");
1806 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1808 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1811 monitor_printf(mon, "Snapshot list (from %s):\n",
1812 bdrv_get_device_name(bs));
1813 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1814 for(i = 0; i < nb_sns; i++) {
1816 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));