Merge commit '92a16d7a9a904d5dd55d012b89ad798f0e4cd0bd' into upstream-merge
[qemu-kvm/amd-iommu.git] / savevm.c
blob3554239dfad3fd7316062a73b80ae4b8e46e9e6b
1 /*
2 * QEMU System Emulator
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
22 * THE SOFTWARE.
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <signal.h>
27 #include <time.h>
28 #include <errno.h>
29 #include <sys/time.h>
30 #include <zlib.h>
32 /* Needed early for CONFIG_BSD etc. */
33 #include "config-host.h"
35 #ifndef _WIN32
36 #include <sys/times.h>
37 #include <sys/wait.h>
38 #include <termios.h>
39 #include <sys/mman.h>
40 #include <sys/ioctl.h>
41 #include <sys/resource.h>
42 #include <sys/socket.h>
43 #include <netinet/in.h>
44 #include <net/if.h>
45 #include <arpa/inet.h>
46 #include <dirent.h>
47 #include <netdb.h>
48 #include <sys/select.h>
49 #ifdef CONFIG_BSD
50 #include <sys/stat.h>
51 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
52 #include <libutil.h>
53 #else
54 #include <util.h>
55 #endif
56 #ifdef __linux__
57 #include <pty.h>
58 #include <malloc.h>
59 #include <linux/rtc.h>
60 #endif
61 #endif
62 #endif
64 #ifdef _WIN32
65 #include <windows.h>
66 #include <malloc.h>
67 #include <sys/timeb.h>
68 #include <mmsystem.h>
69 #define getopt_long_only getopt_long
70 #define memalign(align, size) malloc(size)
71 #endif
73 #include "qemu-common.h"
74 #include "hw/hw.h"
75 #include "net.h"
76 #include "monitor.h"
77 #include "sysemu.h"
78 #include "qemu-timer.h"
79 #include "qemu-char.h"
80 #include "blockdev.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
91 #ifndef ETH_P_RARP
92 #define ETH_P_RARP 0x8035
93 #endif
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,
99 uint8_t *mac_addr)
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 */
106 /* RARP header. */
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)
125 uint8_t buf[60];
126 int len;
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);
141 if (--count) {
142 /* delay 50ms, 150ms, 250ms, ... */
143 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
144 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
145 } else {
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
163 struct QEMUFile {
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;
170 void *opaque;
171 int is_write;
173 int64_t buf_offset; /* start of buffer when writing, end of buffer
174 when reading */
175 int buf_index;
176 int buf_size; /* 0 when writing */
177 uint8_t buf[IO_BUF_SIZE];
179 int has_error;
182 typedef struct QEMUFileStdio
184 FILE *stdio_file;
185 QEMUFile *file;
186 } QEMUFileStdio;
188 typedef struct QEMUFileSocket
190 int fd;
191 QEMUFile *file;
192 } QEMUFileSocket;
194 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
196 QEMUFileSocket *s = opaque;
197 ssize_t len;
199 do {
200 len = recv(s->fd, (void *)buf, size, 0);
201 } while (len == -1 && socket_error() == EINTR);
203 if (len == -1)
204 len = -socket_error();
206 return len;
209 static int socket_close(void *opaque)
211 QEMUFileSocket *s = opaque;
212 qemu_free(s);
213 return 0;
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;
226 int bytes;
228 do {
229 clearerr(fp);
230 bytes = fread(buf, 1, size, fp);
231 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
232 return bytes;
235 static int stdio_pclose(void *opaque)
237 QEMUFileStdio *s = opaque;
238 int ret;
239 ret = pclose(s->stdio_file);
240 qemu_free(s);
241 return ret;
244 static int stdio_fclose(void *opaque)
246 QEMUFileStdio *s = opaque;
247 fclose(s->stdio_file);
248 qemu_free(s);
249 return 0;
252 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
254 QEMUFileStdio *s;
256 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
257 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
258 return NULL;
261 s = qemu_mallocz(sizeof(QEMUFileStdio));
263 s->stdio_file = stdio_file;
265 if(mode[0] == 'r') {
266 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
267 NULL, NULL, NULL);
268 } else {
269 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
270 NULL, NULL, NULL);
272 return s->file;
275 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
277 FILE *popen_file;
279 popen_file = popen(command, mode);
280 if(popen_file == NULL) {
281 return NULL;
284 return qemu_popen(popen_file, mode);
287 int qemu_stdio_fd(QEMUFile *f)
289 QEMUFileStdio *p;
290 int fd;
292 p = (QEMUFileStdio *)f->opaque;
293 fd = fileno(p->stdio_file);
295 return fd;
298 QEMUFile *qemu_fdopen(int fd, const char *mode)
300 QEMUFileStdio *s;
302 if (mode == NULL ||
303 (mode[0] != 'r' && mode[0] != 'w') ||
304 mode[1] != 'b' || mode[2] != 0) {
305 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
306 return NULL;
309 s = qemu_mallocz(sizeof(QEMUFileStdio));
310 s->stdio_file = fdopen(fd, mode);
311 if (!s->stdio_file)
312 goto fail;
314 if(mode[0] == 'r') {
315 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
316 NULL, NULL, NULL);
317 } else {
318 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
319 NULL, NULL, NULL);
321 return s->file;
323 fail:
324 qemu_free(s);
325 return NULL;
328 QEMUFile *qemu_fopen_socket(int fd)
330 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
332 s->fd = fd;
333 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
334 NULL, NULL, NULL);
335 return s->file;
338 static int file_put_buffer(void *opaque, const uint8_t *buf,
339 int64_t pos, int size)
341 QEMUFileStdio *s = opaque;
342 fseek(s->stdio_file, pos, SEEK_SET);
343 return fwrite(buf, 1, size, s->stdio_file);
346 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
348 QEMUFileStdio *s = opaque;
349 fseek(s->stdio_file, pos, SEEK_SET);
350 return fread(buf, 1, size, s->stdio_file);
353 QEMUFile *qemu_fopen(const char *filename, const char *mode)
355 QEMUFileStdio *s;
357 if (mode == NULL ||
358 (mode[0] != 'r' && mode[0] != 'w') ||
359 mode[1] != 'b' || mode[2] != 0) {
360 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
361 return NULL;
364 s = qemu_mallocz(sizeof(QEMUFileStdio));
366 s->stdio_file = fopen(filename, mode);
367 if (!s->stdio_file)
368 goto fail;
370 if(mode[0] == 'w') {
371 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
372 NULL, NULL, NULL);
373 } else {
374 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
375 NULL, NULL, NULL);
377 return s->file;
378 fail:
379 qemu_free(s);
380 return NULL;
383 static int block_put_buffer(void *opaque, const uint8_t *buf,
384 int64_t pos, int size)
386 bdrv_save_vmstate(opaque, buf, pos, size);
387 return size;
390 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
392 return bdrv_load_vmstate(opaque, buf, pos, size);
395 static int bdrv_fclose(void *opaque)
397 return 0;
400 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
402 if (is_writable)
403 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
404 NULL, NULL, NULL);
405 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
408 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
409 QEMUFileGetBufferFunc *get_buffer,
410 QEMUFileCloseFunc *close,
411 QEMUFileRateLimit *rate_limit,
412 QEMUFileSetRateLimit *set_rate_limit,
413 QEMUFileGetRateLimit *get_rate_limit)
415 QEMUFile *f;
417 f = qemu_mallocz(sizeof(QEMUFile));
419 f->opaque = opaque;
420 f->put_buffer = put_buffer;
421 f->get_buffer = get_buffer;
422 f->close = close;
423 f->rate_limit = rate_limit;
424 f->set_rate_limit = set_rate_limit;
425 f->get_rate_limit = get_rate_limit;
426 f->is_write = 0;
428 return f;
431 int qemu_file_has_error(QEMUFile *f)
433 return f->has_error;
436 void qemu_file_set_error(QEMUFile *f)
438 f->has_error = 1;
441 void qemu_fflush(QEMUFile *f)
443 if (!f->put_buffer)
444 return;
446 if (f->is_write && f->buf_index > 0) {
447 int len;
449 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
450 if (len > 0)
451 f->buf_offset += f->buf_index;
452 else
453 f->has_error = 1;
454 f->buf_index = 0;
458 static void qemu_fill_buffer(QEMUFile *f)
460 int len;
462 if (!f->get_buffer)
463 return;
465 if (f->is_write)
466 abort();
468 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
469 if (len > 0) {
470 f->buf_index = 0;
471 f->buf_size = len;
472 f->buf_offset += len;
473 } else if (len != -EAGAIN)
474 f->has_error = 1;
477 int qemu_fclose(QEMUFile *f)
479 int ret = 0;
480 qemu_fflush(f);
481 if (f->close)
482 ret = f->close(f->opaque);
483 qemu_free(f);
484 return ret;
487 void qemu_file_put_notify(QEMUFile *f)
489 f->put_buffer(f->opaque, NULL, 0, 0);
492 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
494 int l;
496 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
497 fprintf(stderr,
498 "Attempted to write to buffer while read buffer is not empty\n");
499 abort();
502 while (!f->has_error && size > 0) {
503 l = IO_BUF_SIZE - f->buf_index;
504 if (l > size)
505 l = size;
506 memcpy(f->buf + f->buf_index, buf, l);
507 f->is_write = 1;
508 f->buf_index += l;
509 buf += l;
510 size -= l;
511 if (f->buf_index >= IO_BUF_SIZE)
512 qemu_fflush(f);
516 void qemu_put_byte(QEMUFile *f, int v)
518 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
519 fprintf(stderr,
520 "Attempted to write to buffer while read buffer is not empty\n");
521 abort();
524 f->buf[f->buf_index++] = v;
525 f->is_write = 1;
526 if (f->buf_index >= IO_BUF_SIZE)
527 qemu_fflush(f);
530 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
532 int size, l;
534 if (f->is_write)
535 abort();
537 size = size1;
538 while (size > 0) {
539 l = f->buf_size - f->buf_index;
540 if (l == 0) {
541 qemu_fill_buffer(f);
542 l = f->buf_size - f->buf_index;
543 if (l == 0)
544 break;
546 if (l > size)
547 l = size;
548 memcpy(buf, f->buf + f->buf_index, l);
549 f->buf_index += l;
550 buf += l;
551 size -= l;
553 return size1 - size;
556 int qemu_get_byte(QEMUFile *f)
558 if (f->is_write)
559 abort();
561 if (f->buf_index >= f->buf_size) {
562 qemu_fill_buffer(f);
563 if (f->buf_index >= f->buf_size)
564 return 0;
566 return f->buf[f->buf_index++];
569 int64_t qemu_ftell(QEMUFile *f)
571 return f->buf_offset - f->buf_size + f->buf_index;
574 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
576 if (whence == SEEK_SET) {
577 /* nothing to do */
578 } else if (whence == SEEK_CUR) {
579 pos += qemu_ftell(f);
580 } else {
581 /* SEEK_END not supported */
582 return -1;
584 if (f->put_buffer) {
585 qemu_fflush(f);
586 f->buf_offset = pos;
587 } else {
588 f->buf_offset = pos;
589 f->buf_index = 0;
590 f->buf_size = 0;
592 return pos;
595 int qemu_file_rate_limit(QEMUFile *f)
597 if (f->rate_limit)
598 return f->rate_limit(f->opaque);
600 return 0;
603 size_t qemu_file_get_rate_limit(QEMUFile *f)
605 if (f->get_rate_limit)
606 return f->get_rate_limit(f->opaque);
608 return 0;
611 size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
613 /* any failed or completed migration keeps its state to allow probing of
614 * migration data, but has no associated file anymore */
615 if (f && f->set_rate_limit)
616 return f->set_rate_limit(f->opaque, new_rate);
618 return 0;
621 void qemu_put_be16(QEMUFile *f, unsigned int v)
623 qemu_put_byte(f, v >> 8);
624 qemu_put_byte(f, v);
627 void qemu_put_be32(QEMUFile *f, unsigned int v)
629 qemu_put_byte(f, v >> 24);
630 qemu_put_byte(f, v >> 16);
631 qemu_put_byte(f, v >> 8);
632 qemu_put_byte(f, v);
635 void qemu_put_be64(QEMUFile *f, uint64_t v)
637 qemu_put_be32(f, v >> 32);
638 qemu_put_be32(f, v);
641 unsigned int qemu_get_be16(QEMUFile *f)
643 unsigned int v;
644 v = qemu_get_byte(f) << 8;
645 v |= qemu_get_byte(f);
646 return v;
649 unsigned int qemu_get_be32(QEMUFile *f)
651 unsigned int v;
652 v = qemu_get_byte(f) << 24;
653 v |= qemu_get_byte(f) << 16;
654 v |= qemu_get_byte(f) << 8;
655 v |= qemu_get_byte(f);
656 return v;
659 uint64_t qemu_get_be64(QEMUFile *f)
661 uint64_t v;
662 v = (uint64_t)qemu_get_be32(f) << 32;
663 v |= qemu_get_be32(f);
664 return v;
667 /* 8 bit int */
669 static int get_int8(QEMUFile *f, void *pv, size_t size)
671 int8_t *v = pv;
672 qemu_get_s8s(f, v);
673 return 0;
676 static void put_int8(QEMUFile *f, void *pv, size_t size)
678 int8_t *v = pv;
679 qemu_put_s8s(f, v);
682 const VMStateInfo vmstate_info_int8 = {
683 .name = "int8",
684 .get = get_int8,
685 .put = put_int8,
688 /* 16 bit int */
690 static int get_int16(QEMUFile *f, void *pv, size_t size)
692 int16_t *v = pv;
693 qemu_get_sbe16s(f, v);
694 return 0;
697 static void put_int16(QEMUFile *f, void *pv, size_t size)
699 int16_t *v = pv;
700 qemu_put_sbe16s(f, v);
703 const VMStateInfo vmstate_info_int16 = {
704 .name = "int16",
705 .get = get_int16,
706 .put = put_int16,
709 /* 32 bit int */
711 static int get_int32(QEMUFile *f, void *pv, size_t size)
713 int32_t *v = pv;
714 qemu_get_sbe32s(f, v);
715 return 0;
718 static void put_int32(QEMUFile *f, void *pv, size_t size)
720 int32_t *v = pv;
721 qemu_put_sbe32s(f, v);
724 const VMStateInfo vmstate_info_int32 = {
725 .name = "int32",
726 .get = get_int32,
727 .put = put_int32,
730 /* 32 bit int. See that the received value is the same than the one
731 in the field */
733 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
735 int32_t *v = pv;
736 int32_t v2;
737 qemu_get_sbe32s(f, &v2);
739 if (*v == v2)
740 return 0;
741 return -EINVAL;
744 const VMStateInfo vmstate_info_int32_equal = {
745 .name = "int32 equal",
746 .get = get_int32_equal,
747 .put = put_int32,
750 /* 32 bit int. See that the received value is the less or the same
751 than the one in the field */
753 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
755 int32_t *old = pv;
756 int32_t new;
757 qemu_get_sbe32s(f, &new);
759 if (*old <= new)
760 return 0;
761 return -EINVAL;
764 const VMStateInfo vmstate_info_int32_le = {
765 .name = "int32 equal",
766 .get = get_int32_le,
767 .put = put_int32,
770 /* 64 bit int */
772 static int get_int64(QEMUFile *f, void *pv, size_t size)
774 int64_t *v = pv;
775 qemu_get_sbe64s(f, v);
776 return 0;
779 static void put_int64(QEMUFile *f, void *pv, size_t size)
781 int64_t *v = pv;
782 qemu_put_sbe64s(f, v);
785 const VMStateInfo vmstate_info_int64 = {
786 .name = "int64",
787 .get = get_int64,
788 .put = put_int64,
791 /* 8 bit unsigned int */
793 static int get_uint8(QEMUFile *f, void *pv, size_t size)
795 uint8_t *v = pv;
796 qemu_get_8s(f, v);
797 return 0;
800 static void put_uint8(QEMUFile *f, void *pv, size_t size)
802 uint8_t *v = pv;
803 qemu_put_8s(f, v);
806 const VMStateInfo vmstate_info_uint8 = {
807 .name = "uint8",
808 .get = get_uint8,
809 .put = put_uint8,
812 /* 16 bit unsigned int */
814 static int get_uint16(QEMUFile *f, void *pv, size_t size)
816 uint16_t *v = pv;
817 qemu_get_be16s(f, v);
818 return 0;
821 static void put_uint16(QEMUFile *f, void *pv, size_t size)
823 uint16_t *v = pv;
824 qemu_put_be16s(f, v);
827 const VMStateInfo vmstate_info_uint16 = {
828 .name = "uint16",
829 .get = get_uint16,
830 .put = put_uint16,
833 /* 32 bit unsigned int */
835 static int get_uint32(QEMUFile *f, void *pv, size_t size)
837 uint32_t *v = pv;
838 qemu_get_be32s(f, v);
839 return 0;
842 static void put_uint32(QEMUFile *f, void *pv, size_t size)
844 uint32_t *v = pv;
845 qemu_put_be32s(f, v);
848 const VMStateInfo vmstate_info_uint32 = {
849 .name = "uint32",
850 .get = get_uint32,
851 .put = put_uint32,
854 /* 64 bit unsigned int */
856 static int get_uint64(QEMUFile *f, void *pv, size_t size)
858 uint64_t *v = pv;
859 qemu_get_be64s(f, v);
860 return 0;
863 static void put_uint64(QEMUFile *f, void *pv, size_t size)
865 uint64_t *v = pv;
866 qemu_put_be64s(f, v);
869 const VMStateInfo vmstate_info_uint64 = {
870 .name = "uint64",
871 .get = get_uint64,
872 .put = put_uint64,
875 /* 64 bit linux kernel unsigned int */
877 #ifdef __linux__
878 static int get_u64(QEMUFile *f, void *pv, size_t size)
880 uint64_t *v = pv;
881 qemu_get_be64s(f, v);
882 return 0;
885 static void put_u64(QEMUFile *f, void *pv, size_t size)
887 uint64_t *v = pv;
888 qemu_put_be64s(f, v);
891 const VMStateInfo vmstate_info_u64 = {
892 .name = "__u64",
893 .get = get_u64,
894 .put = put_u64,
896 #endif /* __linux__ */
898 /* 8 bit int. See that the received value is the same than the one
899 in the field */
901 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
903 uint8_t *v = pv;
904 uint8_t v2;
905 qemu_get_8s(f, &v2);
907 if (*v == v2)
908 return 0;
909 return -EINVAL;
912 const VMStateInfo vmstate_info_uint8_equal = {
913 .name = "uint8 equal",
914 .get = get_uint8_equal,
915 .put = put_uint8,
918 /* 16 bit unsigned int int. See that the received value is the same than the one
919 in the field */
921 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
923 uint16_t *v = pv;
924 uint16_t v2;
925 qemu_get_be16s(f, &v2);
927 if (*v == v2)
928 return 0;
929 return -EINVAL;
932 const VMStateInfo vmstate_info_uint16_equal = {
933 .name = "uint16 equal",
934 .get = get_uint16_equal,
935 .put = put_uint16,
938 /* timers */
940 static int get_timer(QEMUFile *f, void *pv, size_t size)
942 QEMUTimer *v = pv;
943 qemu_get_timer(f, v);
944 return 0;
947 static void put_timer(QEMUFile *f, void *pv, size_t size)
949 QEMUTimer *v = pv;
950 qemu_put_timer(f, v);
953 const VMStateInfo vmstate_info_timer = {
954 .name = "timer",
955 .get = get_timer,
956 .put = put_timer,
959 /* uint8_t buffers */
961 static int get_buffer(QEMUFile *f, void *pv, size_t size)
963 uint8_t *v = pv;
964 qemu_get_buffer(f, v, size);
965 return 0;
968 static void put_buffer(QEMUFile *f, void *pv, size_t size)
970 uint8_t *v = pv;
971 qemu_put_buffer(f, v, size);
974 const VMStateInfo vmstate_info_buffer = {
975 .name = "buffer",
976 .get = get_buffer,
977 .put = put_buffer,
980 /* unused buffers: space that was used for some fields that are
981 not usefull anymore */
983 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
985 uint8_t buf[1024];
986 int block_len;
988 while (size > 0) {
989 block_len = MIN(sizeof(buf), size);
990 size -= block_len;
991 qemu_get_buffer(f, buf, block_len);
993 return 0;
996 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
998 static const uint8_t buf[1024];
999 int block_len;
1001 while (size > 0) {
1002 block_len = MIN(sizeof(buf), size);
1003 size -= block_len;
1004 qemu_put_buffer(f, buf, block_len);
1008 const VMStateInfo vmstate_info_unused_buffer = {
1009 .name = "unused_buffer",
1010 .get = get_unused_buffer,
1011 .put = put_unused_buffer,
1014 typedef struct SaveStateEntry {
1015 QTAILQ_ENTRY(SaveStateEntry) entry;
1016 char idstr[256];
1017 int instance_id;
1018 int alias_id;
1019 int version_id;
1020 int section_id;
1021 SaveSetParamsHandler *set_params;
1022 SaveLiveStateHandler *save_live_state;
1023 SaveStateHandler *save_state;
1024 LoadStateHandler *load_state;
1025 const VMStateDescription *vmsd;
1026 void *opaque;
1027 } SaveStateEntry;
1030 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1031 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1032 static int global_section_id;
1034 static int calculate_new_instance_id(const char *idstr)
1036 SaveStateEntry *se;
1037 int instance_id = 0;
1039 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1040 if (strcmp(idstr, se->idstr) == 0
1041 && instance_id <= se->instance_id) {
1042 instance_id = se->instance_id + 1;
1045 return instance_id;
1048 /* TODO: Individual devices generally have very little idea about the rest
1049 of the system, so instance_id should be removed/replaced.
1050 Meanwhile pass -1 as instance_id if you do not already have a clearly
1051 distinguishing id for all instances of your device class. */
1052 int register_savevm_live(const char *idstr,
1053 int instance_id,
1054 int version_id,
1055 SaveSetParamsHandler *set_params,
1056 SaveLiveStateHandler *save_live_state,
1057 SaveStateHandler *save_state,
1058 LoadStateHandler *load_state,
1059 void *opaque)
1061 SaveStateEntry *se;
1063 se = qemu_mallocz(sizeof(SaveStateEntry));
1064 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1065 se->version_id = version_id;
1066 se->section_id = global_section_id++;
1067 se->set_params = set_params;
1068 se->save_live_state = save_live_state;
1069 se->save_state = save_state;
1070 se->load_state = load_state;
1071 se->opaque = opaque;
1072 se->vmsd = NULL;
1074 if (instance_id == -1) {
1075 se->instance_id = calculate_new_instance_id(idstr);
1076 } else {
1077 se->instance_id = instance_id;
1079 /* add at the end of list */
1080 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1081 return 0;
1084 int register_savevm(const char *idstr,
1085 int instance_id,
1086 int version_id,
1087 SaveStateHandler *save_state,
1088 LoadStateHandler *load_state,
1089 void *opaque)
1091 return register_savevm_live(idstr, instance_id, version_id,
1092 NULL, NULL, save_state, load_state, opaque);
1095 void unregister_savevm(const char *idstr, void *opaque)
1097 SaveStateEntry *se, *new_se;
1099 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1100 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1101 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1102 qemu_free(se);
1107 int vmstate_register_with_alias_id(int instance_id,
1108 const VMStateDescription *vmsd,
1109 void *opaque, int alias_id,
1110 int required_for_version)
1112 SaveStateEntry *se;
1114 /* If this triggers, alias support can be dropped for the vmsd. */
1115 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1117 se = qemu_mallocz(sizeof(SaveStateEntry));
1118 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1119 se->version_id = vmsd->version_id;
1120 se->section_id = global_section_id++;
1121 se->save_live_state = NULL;
1122 se->save_state = NULL;
1123 se->load_state = NULL;
1124 se->opaque = opaque;
1125 se->vmsd = vmsd;
1126 se->alias_id = alias_id;
1128 if (instance_id == -1) {
1129 se->instance_id = calculate_new_instance_id(vmsd->name);
1130 } else {
1131 se->instance_id = instance_id;
1133 /* add at the end of list */
1134 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1135 return 0;
1138 int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1139 void *opaque)
1141 return vmstate_register_with_alias_id(instance_id, vmsd, opaque, -1, 0);
1144 void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1146 SaveStateEntry *se, *new_se;
1148 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1149 if (se->vmsd == vmsd && se->opaque == opaque) {
1150 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1151 qemu_free(se);
1156 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1157 void *opaque, int version_id)
1159 VMStateField *field = vmsd->fields;
1161 if (version_id > vmsd->version_id) {
1162 return -EINVAL;
1164 if (version_id < vmsd->minimum_version_id_old) {
1165 return -EINVAL;
1167 if (version_id < vmsd->minimum_version_id) {
1168 return vmsd->load_state_old(f, opaque, version_id);
1170 if (vmsd->pre_load) {
1171 int ret = vmsd->pre_load(opaque);
1172 if (ret)
1173 return ret;
1175 while(field->name) {
1176 if ((field->field_exists &&
1177 field->field_exists(opaque, version_id)) ||
1178 (!field->field_exists &&
1179 field->version_id <= version_id)) {
1180 void *base_addr = opaque + field->offset;
1181 int ret, i, n_elems = 1;
1182 int size = field->size;
1184 if (field->flags & VMS_VBUFFER) {
1185 size = *(int32_t *)(opaque+field->size_offset);
1186 if (field->flags & VMS_MULTIPLY) {
1187 size *= field->size;
1190 if (field->flags & VMS_ARRAY) {
1191 n_elems = field->num;
1192 } else if (field->flags & VMS_VARRAY_INT32) {
1193 n_elems = *(int32_t *)(opaque+field->num_offset);
1194 } else if (field->flags & VMS_VARRAY_UINT16) {
1195 n_elems = *(uint16_t *)(opaque+field->num_offset);
1197 if (field->flags & VMS_POINTER) {
1198 base_addr = *(void **)base_addr + field->start;
1200 for (i = 0; i < n_elems; i++) {
1201 void *addr = base_addr + size * i;
1203 if (field->flags & VMS_ARRAY_OF_POINTER) {
1204 addr = *(void **)addr;
1206 if (field->flags & VMS_STRUCT) {
1207 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1208 } else {
1209 ret = field->info->get(f, addr, size);
1212 if (ret < 0) {
1213 return ret;
1217 field++;
1219 if (vmsd->post_load) {
1220 return vmsd->post_load(opaque, version_id);
1222 return 0;
1225 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1226 void *opaque)
1228 VMStateField *field = vmsd->fields;
1230 if (vmsd->pre_save) {
1231 vmsd->pre_save(opaque);
1233 while(field->name) {
1234 if (!field->field_exists ||
1235 field->field_exists(opaque, vmsd->version_id)) {
1236 void *base_addr = opaque + field->offset;
1237 int i, n_elems = 1;
1238 int size = field->size;
1240 if (field->flags & VMS_VBUFFER) {
1241 size = *(int32_t *)(opaque+field->size_offset);
1242 if (field->flags & VMS_MULTIPLY) {
1243 size *= field->size;
1246 if (field->flags & VMS_ARRAY) {
1247 n_elems = field->num;
1248 } else if (field->flags & VMS_VARRAY_INT32) {
1249 n_elems = *(int32_t *)(opaque+field->num_offset);
1250 } else if (field->flags & VMS_VARRAY_UINT16) {
1251 n_elems = *(uint16_t *)(opaque+field->num_offset);
1253 if (field->flags & VMS_POINTER) {
1254 base_addr = *(void **)base_addr + field->start;
1256 for (i = 0; i < n_elems; i++) {
1257 void *addr = base_addr + size * i;
1259 if (field->flags & VMS_ARRAY_OF_POINTER) {
1260 addr = *(void **)addr;
1262 if (field->flags & VMS_STRUCT) {
1263 vmstate_save_state(f, field->vmsd, addr);
1264 } else {
1265 field->info->put(f, addr, size);
1269 field++;
1273 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1275 if (!se->vmsd) { /* Old style */
1276 return se->load_state(f, se->opaque, version_id);
1278 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1281 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1283 if (!se->vmsd) { /* Old style */
1284 se->save_state(f, se->opaque);
1285 return;
1287 vmstate_save_state(f,se->vmsd, se->opaque);
1290 #define QEMU_VM_FILE_MAGIC 0x5145564d
1291 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1292 #define QEMU_VM_FILE_VERSION 0x00000003
1294 #define QEMU_VM_EOF 0x00
1295 #define QEMU_VM_SECTION_START 0x01
1296 #define QEMU_VM_SECTION_PART 0x02
1297 #define QEMU_VM_SECTION_END 0x03
1298 #define QEMU_VM_SECTION_FULL 0x04
1300 int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1301 int shared)
1303 SaveStateEntry *se;
1305 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1306 if(se->set_params == NULL) {
1307 continue;
1309 se->set_params(blk_enable, shared, se->opaque);
1312 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1313 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1315 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1316 int len;
1318 if (se->save_live_state == NULL)
1319 continue;
1321 /* Section type */
1322 qemu_put_byte(f, QEMU_VM_SECTION_START);
1323 qemu_put_be32(f, se->section_id);
1325 /* ID string */
1326 len = strlen(se->idstr);
1327 qemu_put_byte(f, len);
1328 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1330 qemu_put_be32(f, se->instance_id);
1331 qemu_put_be32(f, se->version_id);
1333 se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
1336 if (qemu_file_has_error(f)) {
1337 qemu_savevm_state_cancel(mon, f);
1338 return -EIO;
1341 return 0;
1344 int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
1346 SaveStateEntry *se;
1347 int ret = 1;
1349 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1350 if (se->save_live_state == NULL)
1351 continue;
1353 /* Section type */
1354 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1355 qemu_put_be32(f, se->section_id);
1357 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
1358 if (!ret) {
1359 /* Do not proceed to the next vmstate before this one reported
1360 completion of the current stage. This serializes the migration
1361 and reduces the probability that a faster changing state is
1362 synchronized over and over again. */
1363 break;
1367 if (ret)
1368 return 1;
1370 if (qemu_file_has_error(f)) {
1371 qemu_savevm_state_cancel(mon, f);
1372 return -EIO;
1375 return 0;
1378 int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
1380 SaveStateEntry *se;
1382 cpu_synchronize_all_states();
1384 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1385 if (se->save_live_state == NULL)
1386 continue;
1388 /* Section type */
1389 qemu_put_byte(f, QEMU_VM_SECTION_END);
1390 qemu_put_be32(f, se->section_id);
1392 se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
1395 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1396 int len;
1398 if (se->save_state == NULL && se->vmsd == NULL)
1399 continue;
1401 /* Section type */
1402 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1403 qemu_put_be32(f, se->section_id);
1405 /* ID string */
1406 len = strlen(se->idstr);
1407 qemu_put_byte(f, len);
1408 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1410 qemu_put_be32(f, se->instance_id);
1411 qemu_put_be32(f, se->version_id);
1413 vmstate_save(f, se);
1416 qemu_put_byte(f, QEMU_VM_EOF);
1418 if (qemu_file_has_error(f))
1419 return -EIO;
1421 return 0;
1424 void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
1426 SaveStateEntry *se;
1428 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1429 if (se->save_live_state) {
1430 se->save_live_state(mon, f, -1, se->opaque);
1435 static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
1437 int saved_vm_running;
1438 int ret;
1440 saved_vm_running = vm_running;
1441 vm_stop(0);
1443 bdrv_flush_all();
1445 ret = qemu_savevm_state_begin(mon, f, 0, 0);
1446 if (ret < 0)
1447 goto out;
1449 do {
1450 ret = qemu_savevm_state_iterate(mon, f);
1451 if (ret < 0)
1452 goto out;
1453 } while (ret == 0);
1455 ret = qemu_savevm_state_complete(mon, f);
1457 out:
1458 if (qemu_file_has_error(f))
1459 ret = -EIO;
1461 if (!ret && saved_vm_running)
1462 vm_start();
1464 return ret;
1467 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1469 SaveStateEntry *se;
1471 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1472 if (!strcmp(se->idstr, idstr) &&
1473 (instance_id == se->instance_id ||
1474 instance_id == se->alias_id))
1475 return se;
1477 return NULL;
1480 typedef struct LoadStateEntry {
1481 QLIST_ENTRY(LoadStateEntry) entry;
1482 SaveStateEntry *se;
1483 int section_id;
1484 int version_id;
1485 } LoadStateEntry;
1487 int qemu_loadvm_state(QEMUFile *f)
1489 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1490 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1491 LoadStateEntry *le, *new_le;
1492 uint8_t section_type;
1493 unsigned int v;
1494 int ret;
1496 v = qemu_get_be32(f);
1497 if (v != QEMU_VM_FILE_MAGIC)
1498 return -EINVAL;
1500 v = qemu_get_be32(f);
1501 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1502 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1503 return -ENOTSUP;
1505 if (v != QEMU_VM_FILE_VERSION)
1506 return -ENOTSUP;
1508 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1509 uint32_t instance_id, version_id, section_id;
1510 SaveStateEntry *se;
1511 char idstr[257];
1512 int len;
1514 switch (section_type) {
1515 case QEMU_VM_SECTION_START:
1516 case QEMU_VM_SECTION_FULL:
1517 /* Read section start */
1518 section_id = qemu_get_be32(f);
1519 len = qemu_get_byte(f);
1520 qemu_get_buffer(f, (uint8_t *)idstr, len);
1521 idstr[len] = 0;
1522 instance_id = qemu_get_be32(f);
1523 version_id = qemu_get_be32(f);
1525 /* Find savevm section */
1526 se = find_se(idstr, instance_id);
1527 if (se == NULL) {
1528 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1529 ret = -EINVAL;
1530 goto out;
1533 /* Validate version */
1534 if (version_id > se->version_id) {
1535 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1536 version_id, idstr, se->version_id);
1537 ret = -EINVAL;
1538 goto out;
1541 /* Add entry */
1542 le = qemu_mallocz(sizeof(*le));
1544 le->se = se;
1545 le->section_id = section_id;
1546 le->version_id = version_id;
1547 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1549 ret = vmstate_load(f, le->se, le->version_id);
1550 if (ret < 0) {
1551 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1552 instance_id, idstr);
1553 goto out;
1555 break;
1556 case QEMU_VM_SECTION_PART:
1557 case QEMU_VM_SECTION_END:
1558 section_id = qemu_get_be32(f);
1560 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1561 if (le->section_id == section_id) {
1562 break;
1565 if (le == NULL) {
1566 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1567 ret = -EINVAL;
1568 goto out;
1571 ret = vmstate_load(f, le->se, le->version_id);
1572 if (ret < 0) {
1573 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1574 section_id);
1575 goto out;
1577 break;
1578 default:
1579 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1580 ret = -EINVAL;
1581 goto out;
1585 cpu_synchronize_all_post_init();
1587 ret = 0;
1589 out:
1590 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1591 QLIST_REMOVE(le, entry);
1592 qemu_free(le);
1595 if (qemu_file_has_error(f))
1596 ret = -EIO;
1598 return ret;
1601 static BlockDriverState *get_bs_snapshots(void)
1603 BlockDriverState *bs;
1605 if (bs_snapshots)
1606 return bs_snapshots;
1607 /* FIXME what if bs_snapshots gets hot-unplugged? */
1609 bs = NULL;
1610 while ((bs = bdrv_next(bs))) {
1611 if (bdrv_can_snapshot(bs)) {
1612 goto ok;
1615 return NULL;
1617 bs_snapshots = bs;
1618 return bs;
1621 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1622 const char *name)
1624 QEMUSnapshotInfo *sn_tab, *sn;
1625 int nb_sns, i, ret;
1627 ret = -ENOENT;
1628 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1629 if (nb_sns < 0)
1630 return ret;
1631 for(i = 0; i < nb_sns; i++) {
1632 sn = &sn_tab[i];
1633 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1634 *sn_info = *sn;
1635 ret = 0;
1636 break;
1639 qemu_free(sn_tab);
1640 return ret;
1644 * Deletes snapshots of a given name in all opened images.
1646 static int del_existing_snapshots(Monitor *mon, const char *name)
1648 BlockDriverState *bs;
1649 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1650 int ret;
1652 bs = NULL;
1653 while ((bs = bdrv_next(bs))) {
1654 if (bdrv_can_snapshot(bs) &&
1655 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1657 ret = bdrv_snapshot_delete(bs, name);
1658 if (ret < 0) {
1659 monitor_printf(mon,
1660 "Error while deleting snapshot on '%s'\n",
1661 bdrv_get_device_name(bs));
1662 return -1;
1667 return 0;
1670 void do_savevm(Monitor *mon, const QDict *qdict)
1672 BlockDriverState *bs, *bs1;
1673 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1674 int ret;
1675 QEMUFile *f;
1676 int saved_vm_running;
1677 uint32_t vm_state_size;
1678 #ifdef _WIN32
1679 struct _timeb tb;
1680 #else
1681 struct timeval tv;
1682 #endif
1683 const char *name = qdict_get_try_str(qdict, "name");
1685 /* Verify if there is a device that doesn't support snapshots and is writable */
1686 bs = NULL;
1687 while ((bs = bdrv_next(bs))) {
1689 if (bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1690 continue;
1693 if (!bdrv_can_snapshot(bs)) {
1694 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
1695 bdrv_get_device_name(bs));
1696 return;
1700 bs = get_bs_snapshots();
1701 if (!bs) {
1702 monitor_printf(mon, "No block device can accept snapshots\n");
1703 return;
1705 /* ??? Should this occur after vm_stop? */
1706 qemu_aio_flush();
1708 saved_vm_running = vm_running;
1709 vm_stop(0);
1711 memset(sn, 0, sizeof(*sn));
1712 if (name) {
1713 ret = bdrv_snapshot_find(bs, old_sn, name);
1714 if (ret >= 0) {
1715 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1716 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1717 } else {
1718 pstrcpy(sn->name, sizeof(sn->name), name);
1722 /* fill auxiliary fields */
1723 #ifdef _WIN32
1724 _ftime(&tb);
1725 sn->date_sec = tb.time;
1726 sn->date_nsec = tb.millitm * 1000000;
1727 #else
1728 gettimeofday(&tv, NULL);
1729 sn->date_sec = tv.tv_sec;
1730 sn->date_nsec = tv.tv_usec * 1000;
1731 #endif
1732 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1734 /* Delete old snapshots of the same name */
1735 if (name && del_existing_snapshots(mon, name) < 0) {
1736 goto the_end;
1739 /* save the VM state */
1740 f = qemu_fopen_bdrv(bs, 1);
1741 if (!f) {
1742 monitor_printf(mon, "Could not open VM state file\n");
1743 goto the_end;
1745 ret = qemu_savevm_state(mon, f);
1746 vm_state_size = qemu_ftell(f);
1747 qemu_fclose(f);
1748 if (ret < 0) {
1749 monitor_printf(mon, "Error %d while writing VM\n", ret);
1750 goto the_end;
1753 /* create the snapshots */
1755 bs1 = NULL;
1756 while ((bs1 = bdrv_next(bs1))) {
1757 if (bdrv_can_snapshot(bs1)) {
1758 /* Write VM state size only to the image that contains the state */
1759 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1760 ret = bdrv_snapshot_create(bs1, sn);
1761 if (ret < 0) {
1762 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1763 bdrv_get_device_name(bs1));
1768 the_end:
1769 if (saved_vm_running)
1770 vm_start();
1773 int load_vmstate(const char *name)
1775 BlockDriverState *bs, *bs1;
1776 QEMUSnapshotInfo sn;
1777 QEMUFile *f;
1778 int ret;
1780 /* Verify if there is a device that doesn't support snapshots and is writable */
1781 bs = NULL;
1782 while ((bs = bdrv_next(bs))) {
1784 if (bdrv_is_removable(bs) || bdrv_is_read_only(bs)) {
1785 continue;
1788 if (!bdrv_can_snapshot(bs)) {
1789 error_report("Device '%s' is writable but does not support snapshots.",
1790 bdrv_get_device_name(bs));
1791 return -ENOTSUP;
1795 bs = get_bs_snapshots();
1796 if (!bs) {
1797 error_report("No block device supports snapshots");
1798 return -EINVAL;
1801 /* Flush all IO requests so they don't interfere with the new state. */
1802 qemu_aio_flush();
1804 bs1 = NULL;
1805 while ((bs1 = bdrv_next(bs1))) {
1806 if (bdrv_can_snapshot(bs1)) {
1807 ret = bdrv_snapshot_goto(bs1, name);
1808 if (ret < 0) {
1809 switch(ret) {
1810 case -ENOTSUP:
1811 error_report("%sSnapshots not supported on device '%s'",
1812 bs != bs1 ? "Warning: " : "",
1813 bdrv_get_device_name(bs1));
1814 break;
1815 case -ENOENT:
1816 error_report("%sCould not find snapshot '%s' on device '%s'",
1817 bs != bs1 ? "Warning: " : "",
1818 name, bdrv_get_device_name(bs1));
1819 break;
1820 default:
1821 error_report("%sError %d while activating snapshot on '%s'",
1822 bs != bs1 ? "Warning: " : "",
1823 ret, bdrv_get_device_name(bs1));
1824 break;
1826 /* fatal on snapshot block device */
1827 if (bs == bs1)
1828 return 0;
1833 /* Don't even try to load empty VM states */
1834 ret = bdrv_snapshot_find(bs, &sn, name);
1835 if ((ret >= 0) && (sn.vm_state_size == 0))
1836 return -EINVAL;
1838 /* restore the VM state */
1839 f = qemu_fopen_bdrv(bs, 0);
1840 if (!f) {
1841 error_report("Could not open VM state file");
1842 return -EINVAL;
1844 ret = qemu_loadvm_state(f);
1845 qemu_fclose(f);
1846 if (ret < 0) {
1847 error_report("Error %d while loading VM state", ret);
1848 return ret;
1850 return 0;
1853 void do_delvm(Monitor *mon, const QDict *qdict)
1855 BlockDriverState *bs, *bs1;
1856 int ret;
1857 const char *name = qdict_get_str(qdict, "name");
1859 bs = get_bs_snapshots();
1860 if (!bs) {
1861 monitor_printf(mon, "No block device supports snapshots\n");
1862 return;
1865 bs1 = NULL;
1866 while ((bs1 = bdrv_next(bs1))) {
1867 if (bdrv_can_snapshot(bs1)) {
1868 ret = bdrv_snapshot_delete(bs1, name);
1869 if (ret < 0) {
1870 if (ret == -ENOTSUP)
1871 monitor_printf(mon,
1872 "Snapshots not supported on device '%s'\n",
1873 bdrv_get_device_name(bs1));
1874 else
1875 monitor_printf(mon, "Error %d while deleting snapshot on "
1876 "'%s'\n", ret, bdrv_get_device_name(bs1));
1882 void do_info_snapshots(Monitor *mon)
1884 BlockDriverState *bs, *bs1;
1885 QEMUSnapshotInfo *sn_tab, *sn;
1886 int nb_sns, i;
1887 char buf[256];
1889 bs = get_bs_snapshots();
1890 if (!bs) {
1891 monitor_printf(mon, "No available block device supports snapshots\n");
1892 return;
1894 monitor_printf(mon, "Snapshot devices:");
1895 bs1 = NULL;
1896 while ((bs1 = bdrv_next(bs1))) {
1897 if (bdrv_can_snapshot(bs1)) {
1898 if (bs == bs1)
1899 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1902 monitor_printf(mon, "\n");
1904 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1905 if (nb_sns < 0) {
1906 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1907 return;
1909 monitor_printf(mon, "Snapshot list (from %s):\n",
1910 bdrv_get_device_name(bs));
1911 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1912 for(i = 0; i < nb_sns; i++) {
1913 sn = &sn_tab[i];
1914 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1916 qemu_free(sn_tab);