]> git.proxmox.com Git - qemu.git/blob - savevm.c
migration: consolidate QEMUFile methods in a single QEMUFileOps struct
[qemu.git] / savevm.c
1 /*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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.
23 */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <time.h>
27 #include <errno.h>
28 #include <sys/time.h>
29 #include <zlib.h>
30
31 /* Needed early for CONFIG_BSD etc. */
32 #include "config-host.h"
33
34 #ifndef _WIN32
35 #include <sys/times.h>
36 #include <sys/wait.h>
37 #include <termios.h>
38 #include <sys/mman.h>
39 #include <sys/ioctl.h>
40 #include <sys/resource.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <net/if.h>
44 #include <arpa/inet.h>
45 #include <dirent.h>
46 #include <netdb.h>
47 #include <sys/select.h>
48 #ifdef CONFIG_BSD
49 #include <sys/stat.h>
50 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
51 #include <libutil.h>
52 #else
53 #include <util.h>
54 #endif
55 #ifdef __linux__
56 #include <pty.h>
57 #include <malloc.h>
58 #include <linux/rtc.h>
59 #endif
60 #endif
61 #endif
62
63 #ifdef _WIN32
64 #include <windows.h>
65 #include <malloc.h>
66 #include <sys/timeb.h>
67 #include <mmsystem.h>
68 #define getopt_long_only getopt_long
69 #define memalign(align, size) malloc(size)
70 #endif
71
72 #include "qemu-common.h"
73 #include "hw/hw.h"
74 #include "hw/qdev.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 "audio/audio.h"
81 #include "migration.h"
82 #include "qemu_socket.h"
83 #include "qemu-queue.h"
84 #include "qemu-timer.h"
85 #include "cpus.h"
86 #include "memory.h"
87 #include "qmp-commands.h"
88 #include "trace.h"
89 #include "bitops.h"
90
91 #define SELF_ANNOUNCE_ROUNDS 5
92
93 #ifndef ETH_P_RARP
94 #define ETH_P_RARP 0x8035
95 #endif
96 #define ARP_HTYPE_ETH 0x0001
97 #define ARP_PTYPE_IP 0x0800
98 #define ARP_OP_REQUEST_REV 0x3
99
100 static int announce_self_create(uint8_t *buf,
101 uint8_t *mac_addr)
102 {
103 /* Ethernet header. */
104 memset(buf, 0xff, 6); /* destination MAC addr */
105 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
106 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
107
108 /* RARP header. */
109 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
110 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
111 *(buf + 18) = 6; /* hardware addr length (ethernet) */
112 *(buf + 19) = 4; /* protocol addr length (IPv4) */
113 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
114 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
115 memset(buf + 28, 0x00, 4); /* source protocol addr */
116 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
117 memset(buf + 38, 0x00, 4); /* target protocol addr */
118
119 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
120 memset(buf + 42, 0x00, 18);
121
122 return 60; /* len (FCS will be added by hardware) */
123 }
124
125 static void qemu_announce_self_iter(NICState *nic, void *opaque)
126 {
127 uint8_t buf[60];
128 int len;
129
130 len = announce_self_create(buf, nic->conf->macaddr.a);
131
132 qemu_send_packet_raw(&nic->nc, buf, len);
133 }
134
135
136 static void qemu_announce_self_once(void *opaque)
137 {
138 static int count = SELF_ANNOUNCE_ROUNDS;
139 QEMUTimer *timer = *(QEMUTimer **)opaque;
140
141 qemu_foreach_nic(qemu_announce_self_iter, NULL);
142
143 if (--count) {
144 /* delay 50ms, 150ms, 250ms, ... */
145 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
146 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
147 } else {
148 qemu_del_timer(timer);
149 qemu_free_timer(timer);
150 }
151 }
152
153 void qemu_announce_self(void)
154 {
155 static QEMUTimer *timer;
156 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
157 qemu_announce_self_once(&timer);
158 }
159
160 /***********************************************************/
161 /* savevm/loadvm support */
162
163 #define IO_BUF_SIZE 32768
164
165 struct QEMUFile {
166 const QEMUFileOps *ops;
167 void *opaque;
168 int is_write;
169
170 int64_t buf_offset; /* start of buffer when writing, end of buffer
171 when reading */
172 int buf_index;
173 int buf_size; /* 0 when writing */
174 uint8_t buf[IO_BUF_SIZE];
175
176 int last_error;
177 };
178
179 typedef struct QEMUFileStdio
180 {
181 FILE *stdio_file;
182 QEMUFile *file;
183 } QEMUFileStdio;
184
185 typedef struct QEMUFileSocket
186 {
187 int fd;
188 QEMUFile *file;
189 } QEMUFileSocket;
190
191 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
192 {
193 QEMUFileSocket *s = opaque;
194 ssize_t len;
195
196 do {
197 len = qemu_recv(s->fd, buf, size, 0);
198 } while (len == -1 && socket_error() == EINTR);
199
200 if (len == -1)
201 len = -socket_error();
202
203 return len;
204 }
205
206 static int socket_close(void *opaque)
207 {
208 QEMUFileSocket *s = opaque;
209 g_free(s);
210 return 0;
211 }
212
213 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
214 {
215 QEMUFileStdio *s = opaque;
216 return fwrite(buf, 1, size, s->stdio_file);
217 }
218
219 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
220 {
221 QEMUFileStdio *s = opaque;
222 FILE *fp = s->stdio_file;
223 int bytes;
224
225 do {
226 clearerr(fp);
227 bytes = fread(buf, 1, size, fp);
228 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
229 return bytes;
230 }
231
232 static int stdio_pclose(void *opaque)
233 {
234 QEMUFileStdio *s = opaque;
235 int ret;
236 ret = pclose(s->stdio_file);
237 if (ret == -1) {
238 ret = -errno;
239 }
240 g_free(s);
241 return ret;
242 }
243
244 static int stdio_fclose(void *opaque)
245 {
246 QEMUFileStdio *s = opaque;
247 int ret = 0;
248 if (fclose(s->stdio_file) == EOF) {
249 ret = -errno;
250 }
251 g_free(s);
252 return ret;
253 }
254
255 static const QEMUFileOps stdio_pipe_read_ops = {
256 .get_buffer = stdio_get_buffer,
257 .close = stdio_pclose
258 };
259
260 static const QEMUFileOps stdio_pipe_write_ops = {
261 .put_buffer = stdio_put_buffer,
262 .close = stdio_pclose
263 };
264
265 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
266 {
267 QEMUFileStdio *s;
268
269 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
270 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
271 return NULL;
272 }
273
274 s = g_malloc0(sizeof(QEMUFileStdio));
275
276 s->stdio_file = stdio_file;
277
278 if(mode[0] == 'r') {
279 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
280 } else {
281 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
282 }
283 return s->file;
284 }
285
286 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
287 {
288 FILE *popen_file;
289
290 popen_file = popen(command, mode);
291 if(popen_file == NULL) {
292 return NULL;
293 }
294
295 return qemu_popen(popen_file, mode);
296 }
297
298 int qemu_stdio_fd(QEMUFile *f)
299 {
300 QEMUFileStdio *p;
301 int fd;
302
303 p = (QEMUFileStdio *)f->opaque;
304 fd = fileno(p->stdio_file);
305
306 return fd;
307 }
308
309 static const QEMUFileOps stdio_file_read_ops = {
310 .get_buffer = stdio_get_buffer,
311 .close = stdio_fclose
312 };
313
314 static const QEMUFileOps stdio_file_write_ops = {
315 .put_buffer = stdio_put_buffer,
316 .close = stdio_fclose
317 };
318
319 QEMUFile *qemu_fdopen(int fd, const char *mode)
320 {
321 QEMUFileStdio *s;
322
323 if (mode == NULL ||
324 (mode[0] != 'r' && mode[0] != 'w') ||
325 mode[1] != 'b' || mode[2] != 0) {
326 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
327 return NULL;
328 }
329
330 s = g_malloc0(sizeof(QEMUFileStdio));
331 s->stdio_file = fdopen(fd, mode);
332 if (!s->stdio_file)
333 goto fail;
334
335 if(mode[0] == 'r') {
336 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
337 } else {
338 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
339 }
340 return s->file;
341
342 fail:
343 g_free(s);
344 return NULL;
345 }
346
347 static const QEMUFileOps socket_read_ops = {
348 .get_buffer = socket_get_buffer,
349 .close = socket_close
350 };
351
352 QEMUFile *qemu_fopen_socket(int fd)
353 {
354 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
355
356 s->fd = fd;
357 s->file = qemu_fopen_ops(s, &socket_read_ops);
358 return s->file;
359 }
360
361 QEMUFile *qemu_fopen(const char *filename, const char *mode)
362 {
363 QEMUFileStdio *s;
364
365 if (mode == NULL ||
366 (mode[0] != 'r' && mode[0] != 'w') ||
367 mode[1] != 'b' || mode[2] != 0) {
368 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
369 return NULL;
370 }
371
372 s = g_malloc0(sizeof(QEMUFileStdio));
373
374 s->stdio_file = fopen(filename, mode);
375 if (!s->stdio_file)
376 goto fail;
377
378 if(mode[0] == 'w') {
379 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
380 } else {
381 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
382 }
383 return s->file;
384 fail:
385 g_free(s);
386 return NULL;
387 }
388
389 static int block_put_buffer(void *opaque, const uint8_t *buf,
390 int64_t pos, int size)
391 {
392 bdrv_save_vmstate(opaque, buf, pos, size);
393 return size;
394 }
395
396 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
397 {
398 return bdrv_load_vmstate(opaque, buf, pos, size);
399 }
400
401 static int bdrv_fclose(void *opaque)
402 {
403 return bdrv_flush(opaque);
404 }
405
406 static const QEMUFileOps bdrv_read_ops = {
407 .get_buffer = block_get_buffer,
408 .close = bdrv_fclose
409 };
410
411 static const QEMUFileOps bdrv_write_ops = {
412 .put_buffer = block_put_buffer,
413 .close = bdrv_fclose
414 };
415
416 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
417 {
418 if (is_writable)
419 return qemu_fopen_ops(bs, &bdrv_write_ops);
420 return qemu_fopen_ops(bs, &bdrv_read_ops);
421 }
422
423 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
424 {
425 QEMUFile *f;
426
427 f = g_malloc0(sizeof(QEMUFile));
428
429 f->opaque = opaque;
430 f->ops = ops;
431 f->is_write = 0;
432
433 return f;
434 }
435
436 int qemu_file_get_error(QEMUFile *f)
437 {
438 return f->last_error;
439 }
440
441 static void qemu_file_set_error(QEMUFile *f, int ret)
442 {
443 f->last_error = ret;
444 }
445
446 /** Flushes QEMUFile buffer
447 *
448 */
449 static int qemu_fflush(QEMUFile *f)
450 {
451 int ret = 0;
452
453 if (!f->ops->put_buffer)
454 return 0;
455
456 if (f->is_write && f->buf_index > 0) {
457 ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
458 if (ret >= 0) {
459 f->buf_offset += f->buf_index;
460 }
461 f->buf_index = 0;
462 }
463 return ret;
464 }
465
466 static void qemu_fill_buffer(QEMUFile *f)
467 {
468 int len;
469 int pending;
470
471 if (!f->ops->get_buffer)
472 return;
473
474 if (f->is_write)
475 abort();
476
477 pending = f->buf_size - f->buf_index;
478 if (pending > 0) {
479 memmove(f->buf, f->buf + f->buf_index, pending);
480 }
481 f->buf_index = 0;
482 f->buf_size = pending;
483
484 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
485 IO_BUF_SIZE - pending);
486 if (len > 0) {
487 f->buf_size += len;
488 f->buf_offset += len;
489 } else if (len == 0) {
490 qemu_file_set_error(f, -EIO);
491 } else if (len != -EAGAIN)
492 qemu_file_set_error(f, len);
493 }
494
495 /** Closes the file
496 *
497 * Returns negative error value if any error happened on previous operations or
498 * while closing the file. Returns 0 or positive number on success.
499 *
500 * The meaning of return value on success depends on the specific backend
501 * being used.
502 */
503 int qemu_fclose(QEMUFile *f)
504 {
505 int ret;
506 ret = qemu_fflush(f);
507
508 if (f->ops->close) {
509 int ret2 = f->ops->close(f->opaque);
510 if (ret >= 0) {
511 ret = ret2;
512 }
513 }
514 /* If any error was spotted before closing, we should report it
515 * instead of the close() return value.
516 */
517 if (f->last_error) {
518 ret = f->last_error;
519 }
520 g_free(f);
521 return ret;
522 }
523
524 int qemu_file_put_notify(QEMUFile *f)
525 {
526 return f->ops->put_buffer(f->opaque, NULL, 0, 0);
527 }
528
529 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
530 {
531 int l;
532
533 if (f->last_error) {
534 return;
535 }
536
537 if (f->is_write == 0 && f->buf_index > 0) {
538 fprintf(stderr,
539 "Attempted to write to buffer while read buffer is not empty\n");
540 abort();
541 }
542
543 while (size > 0) {
544 l = IO_BUF_SIZE - f->buf_index;
545 if (l > size)
546 l = size;
547 memcpy(f->buf + f->buf_index, buf, l);
548 f->is_write = 1;
549 f->buf_index += l;
550 buf += l;
551 size -= l;
552 if (f->buf_index >= IO_BUF_SIZE) {
553 int ret = qemu_fflush(f);
554 if (ret < 0) {
555 qemu_file_set_error(f, ret);
556 break;
557 }
558 }
559 }
560 }
561
562 void qemu_put_byte(QEMUFile *f, int v)
563 {
564 if (f->last_error) {
565 return;
566 }
567
568 if (f->is_write == 0 && f->buf_index > 0) {
569 fprintf(stderr,
570 "Attempted to write to buffer while read buffer is not empty\n");
571 abort();
572 }
573
574 f->buf[f->buf_index++] = v;
575 f->is_write = 1;
576 if (f->buf_index >= IO_BUF_SIZE) {
577 int ret = qemu_fflush(f);
578 if (ret < 0) {
579 qemu_file_set_error(f, ret);
580 }
581 }
582 }
583
584 static void qemu_file_skip(QEMUFile *f, int size)
585 {
586 if (f->buf_index + size <= f->buf_size) {
587 f->buf_index += size;
588 }
589 }
590
591 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
592 {
593 int pending;
594 int index;
595
596 if (f->is_write) {
597 abort();
598 }
599
600 index = f->buf_index + offset;
601 pending = f->buf_size - index;
602 if (pending < size) {
603 qemu_fill_buffer(f);
604 index = f->buf_index + offset;
605 pending = f->buf_size - index;
606 }
607
608 if (pending <= 0) {
609 return 0;
610 }
611 if (size > pending) {
612 size = pending;
613 }
614
615 memcpy(buf, f->buf + index, size);
616 return size;
617 }
618
619 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
620 {
621 int pending = size;
622 int done = 0;
623
624 while (pending > 0) {
625 int res;
626
627 res = qemu_peek_buffer(f, buf, pending, 0);
628 if (res == 0) {
629 return done;
630 }
631 qemu_file_skip(f, res);
632 buf += res;
633 pending -= res;
634 done += res;
635 }
636 return done;
637 }
638
639 static int qemu_peek_byte(QEMUFile *f, int offset)
640 {
641 int index = f->buf_index + offset;
642
643 if (f->is_write) {
644 abort();
645 }
646
647 if (index >= f->buf_size) {
648 qemu_fill_buffer(f);
649 index = f->buf_index + offset;
650 if (index >= f->buf_size) {
651 return 0;
652 }
653 }
654 return f->buf[index];
655 }
656
657 int qemu_get_byte(QEMUFile *f)
658 {
659 int result;
660
661 result = qemu_peek_byte(f, 0);
662 qemu_file_skip(f, 1);
663 return result;
664 }
665
666 static int64_t qemu_ftell(QEMUFile *f)
667 {
668 return f->buf_offset - f->buf_size + f->buf_index;
669 }
670
671 int qemu_file_rate_limit(QEMUFile *f)
672 {
673 if (f->ops->rate_limit)
674 return f->ops->rate_limit(f->opaque);
675
676 return 0;
677 }
678
679 int64_t qemu_file_get_rate_limit(QEMUFile *f)
680 {
681 if (f->ops->get_rate_limit)
682 return f->ops->get_rate_limit(f->opaque);
683
684 return 0;
685 }
686
687 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
688 {
689 /* any failed or completed migration keeps its state to allow probing of
690 * migration data, but has no associated file anymore */
691 if (f && f->ops->set_rate_limit)
692 return f->ops->set_rate_limit(f->opaque, new_rate);
693
694 return 0;
695 }
696
697 void qemu_put_be16(QEMUFile *f, unsigned int v)
698 {
699 qemu_put_byte(f, v >> 8);
700 qemu_put_byte(f, v);
701 }
702
703 void qemu_put_be32(QEMUFile *f, unsigned int v)
704 {
705 qemu_put_byte(f, v >> 24);
706 qemu_put_byte(f, v >> 16);
707 qemu_put_byte(f, v >> 8);
708 qemu_put_byte(f, v);
709 }
710
711 void qemu_put_be64(QEMUFile *f, uint64_t v)
712 {
713 qemu_put_be32(f, v >> 32);
714 qemu_put_be32(f, v);
715 }
716
717 unsigned int qemu_get_be16(QEMUFile *f)
718 {
719 unsigned int v;
720 v = qemu_get_byte(f) << 8;
721 v |= qemu_get_byte(f);
722 return v;
723 }
724
725 unsigned int qemu_get_be32(QEMUFile *f)
726 {
727 unsigned int v;
728 v = qemu_get_byte(f) << 24;
729 v |= qemu_get_byte(f) << 16;
730 v |= qemu_get_byte(f) << 8;
731 v |= qemu_get_byte(f);
732 return v;
733 }
734
735 uint64_t qemu_get_be64(QEMUFile *f)
736 {
737 uint64_t v;
738 v = (uint64_t)qemu_get_be32(f) << 32;
739 v |= qemu_get_be32(f);
740 return v;
741 }
742
743
744 /* timer */
745
746 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
747 {
748 uint64_t expire_time;
749
750 expire_time = qemu_timer_expire_time_ns(ts);
751 qemu_put_be64(f, expire_time);
752 }
753
754 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
755 {
756 uint64_t expire_time;
757
758 expire_time = qemu_get_be64(f);
759 if (expire_time != -1) {
760 qemu_mod_timer_ns(ts, expire_time);
761 } else {
762 qemu_del_timer(ts);
763 }
764 }
765
766
767 /* bool */
768
769 static int get_bool(QEMUFile *f, void *pv, size_t size)
770 {
771 bool *v = pv;
772 *v = qemu_get_byte(f);
773 return 0;
774 }
775
776 static void put_bool(QEMUFile *f, void *pv, size_t size)
777 {
778 bool *v = pv;
779 qemu_put_byte(f, *v);
780 }
781
782 const VMStateInfo vmstate_info_bool = {
783 .name = "bool",
784 .get = get_bool,
785 .put = put_bool,
786 };
787
788 /* 8 bit int */
789
790 static int get_int8(QEMUFile *f, void *pv, size_t size)
791 {
792 int8_t *v = pv;
793 qemu_get_s8s(f, v);
794 return 0;
795 }
796
797 static void put_int8(QEMUFile *f, void *pv, size_t size)
798 {
799 int8_t *v = pv;
800 qemu_put_s8s(f, v);
801 }
802
803 const VMStateInfo vmstate_info_int8 = {
804 .name = "int8",
805 .get = get_int8,
806 .put = put_int8,
807 };
808
809 /* 16 bit int */
810
811 static int get_int16(QEMUFile *f, void *pv, size_t size)
812 {
813 int16_t *v = pv;
814 qemu_get_sbe16s(f, v);
815 return 0;
816 }
817
818 static void put_int16(QEMUFile *f, void *pv, size_t size)
819 {
820 int16_t *v = pv;
821 qemu_put_sbe16s(f, v);
822 }
823
824 const VMStateInfo vmstate_info_int16 = {
825 .name = "int16",
826 .get = get_int16,
827 .put = put_int16,
828 };
829
830 /* 32 bit int */
831
832 static int get_int32(QEMUFile *f, void *pv, size_t size)
833 {
834 int32_t *v = pv;
835 qemu_get_sbe32s(f, v);
836 return 0;
837 }
838
839 static void put_int32(QEMUFile *f, void *pv, size_t size)
840 {
841 int32_t *v = pv;
842 qemu_put_sbe32s(f, v);
843 }
844
845 const VMStateInfo vmstate_info_int32 = {
846 .name = "int32",
847 .get = get_int32,
848 .put = put_int32,
849 };
850
851 /* 32 bit int. See that the received value is the same than the one
852 in the field */
853
854 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
855 {
856 int32_t *v = pv;
857 int32_t v2;
858 qemu_get_sbe32s(f, &v2);
859
860 if (*v == v2)
861 return 0;
862 return -EINVAL;
863 }
864
865 const VMStateInfo vmstate_info_int32_equal = {
866 .name = "int32 equal",
867 .get = get_int32_equal,
868 .put = put_int32,
869 };
870
871 /* 32 bit int. See that the received value is the less or the same
872 than the one in the field */
873
874 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
875 {
876 int32_t *old = pv;
877 int32_t new;
878 qemu_get_sbe32s(f, &new);
879
880 if (*old <= new)
881 return 0;
882 return -EINVAL;
883 }
884
885 const VMStateInfo vmstate_info_int32_le = {
886 .name = "int32 equal",
887 .get = get_int32_le,
888 .put = put_int32,
889 };
890
891 /* 64 bit int */
892
893 static int get_int64(QEMUFile *f, void *pv, size_t size)
894 {
895 int64_t *v = pv;
896 qemu_get_sbe64s(f, v);
897 return 0;
898 }
899
900 static void put_int64(QEMUFile *f, void *pv, size_t size)
901 {
902 int64_t *v = pv;
903 qemu_put_sbe64s(f, v);
904 }
905
906 const VMStateInfo vmstate_info_int64 = {
907 .name = "int64",
908 .get = get_int64,
909 .put = put_int64,
910 };
911
912 /* 8 bit unsigned int */
913
914 static int get_uint8(QEMUFile *f, void *pv, size_t size)
915 {
916 uint8_t *v = pv;
917 qemu_get_8s(f, v);
918 return 0;
919 }
920
921 static void put_uint8(QEMUFile *f, void *pv, size_t size)
922 {
923 uint8_t *v = pv;
924 qemu_put_8s(f, v);
925 }
926
927 const VMStateInfo vmstate_info_uint8 = {
928 .name = "uint8",
929 .get = get_uint8,
930 .put = put_uint8,
931 };
932
933 /* 16 bit unsigned int */
934
935 static int get_uint16(QEMUFile *f, void *pv, size_t size)
936 {
937 uint16_t *v = pv;
938 qemu_get_be16s(f, v);
939 return 0;
940 }
941
942 static void put_uint16(QEMUFile *f, void *pv, size_t size)
943 {
944 uint16_t *v = pv;
945 qemu_put_be16s(f, v);
946 }
947
948 const VMStateInfo vmstate_info_uint16 = {
949 .name = "uint16",
950 .get = get_uint16,
951 .put = put_uint16,
952 };
953
954 /* 32 bit unsigned int */
955
956 static int get_uint32(QEMUFile *f, void *pv, size_t size)
957 {
958 uint32_t *v = pv;
959 qemu_get_be32s(f, v);
960 return 0;
961 }
962
963 static void put_uint32(QEMUFile *f, void *pv, size_t size)
964 {
965 uint32_t *v = pv;
966 qemu_put_be32s(f, v);
967 }
968
969 const VMStateInfo vmstate_info_uint32 = {
970 .name = "uint32",
971 .get = get_uint32,
972 .put = put_uint32,
973 };
974
975 /* 32 bit uint. See that the received value is the same than the one
976 in the field */
977
978 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
979 {
980 uint32_t *v = pv;
981 uint32_t v2;
982 qemu_get_be32s(f, &v2);
983
984 if (*v == v2) {
985 return 0;
986 }
987 return -EINVAL;
988 }
989
990 const VMStateInfo vmstate_info_uint32_equal = {
991 .name = "uint32 equal",
992 .get = get_uint32_equal,
993 .put = put_uint32,
994 };
995
996 /* 64 bit unsigned int */
997
998 static int get_uint64(QEMUFile *f, void *pv, size_t size)
999 {
1000 uint64_t *v = pv;
1001 qemu_get_be64s(f, v);
1002 return 0;
1003 }
1004
1005 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1006 {
1007 uint64_t *v = pv;
1008 qemu_put_be64s(f, v);
1009 }
1010
1011 const VMStateInfo vmstate_info_uint64 = {
1012 .name = "uint64",
1013 .get = get_uint64,
1014 .put = put_uint64,
1015 };
1016
1017 /* 8 bit int. See that the received value is the same than the one
1018 in the field */
1019
1020 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1021 {
1022 uint8_t *v = pv;
1023 uint8_t v2;
1024 qemu_get_8s(f, &v2);
1025
1026 if (*v == v2)
1027 return 0;
1028 return -EINVAL;
1029 }
1030
1031 const VMStateInfo vmstate_info_uint8_equal = {
1032 .name = "uint8 equal",
1033 .get = get_uint8_equal,
1034 .put = put_uint8,
1035 };
1036
1037 /* 16 bit unsigned int int. See that the received value is the same than the one
1038 in the field */
1039
1040 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1041 {
1042 uint16_t *v = pv;
1043 uint16_t v2;
1044 qemu_get_be16s(f, &v2);
1045
1046 if (*v == v2)
1047 return 0;
1048 return -EINVAL;
1049 }
1050
1051 const VMStateInfo vmstate_info_uint16_equal = {
1052 .name = "uint16 equal",
1053 .get = get_uint16_equal,
1054 .put = put_uint16,
1055 };
1056
1057 /* timers */
1058
1059 static int get_timer(QEMUFile *f, void *pv, size_t size)
1060 {
1061 QEMUTimer *v = pv;
1062 qemu_get_timer(f, v);
1063 return 0;
1064 }
1065
1066 static void put_timer(QEMUFile *f, void *pv, size_t size)
1067 {
1068 QEMUTimer *v = pv;
1069 qemu_put_timer(f, v);
1070 }
1071
1072 const VMStateInfo vmstate_info_timer = {
1073 .name = "timer",
1074 .get = get_timer,
1075 .put = put_timer,
1076 };
1077
1078 /* uint8_t buffers */
1079
1080 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1081 {
1082 uint8_t *v = pv;
1083 qemu_get_buffer(f, v, size);
1084 return 0;
1085 }
1086
1087 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1088 {
1089 uint8_t *v = pv;
1090 qemu_put_buffer(f, v, size);
1091 }
1092
1093 const VMStateInfo vmstate_info_buffer = {
1094 .name = "buffer",
1095 .get = get_buffer,
1096 .put = put_buffer,
1097 };
1098
1099 /* unused buffers: space that was used for some fields that are
1100 not useful anymore */
1101
1102 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1103 {
1104 uint8_t buf[1024];
1105 int block_len;
1106
1107 while (size > 0) {
1108 block_len = MIN(sizeof(buf), size);
1109 size -= block_len;
1110 qemu_get_buffer(f, buf, block_len);
1111 }
1112 return 0;
1113 }
1114
1115 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1116 {
1117 static const uint8_t buf[1024];
1118 int block_len;
1119
1120 while (size > 0) {
1121 block_len = MIN(sizeof(buf), size);
1122 size -= block_len;
1123 qemu_put_buffer(f, buf, block_len);
1124 }
1125 }
1126
1127 const VMStateInfo vmstate_info_unused_buffer = {
1128 .name = "unused_buffer",
1129 .get = get_unused_buffer,
1130 .put = put_unused_buffer,
1131 };
1132
1133 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1134 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1135 * bit words with the bits in big endian order. The in-memory format
1136 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1137 */
1138 /* This is the number of 64 bit words sent over the wire */
1139 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1140 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1141 {
1142 unsigned long *bmp = pv;
1143 int i, idx = 0;
1144 for (i = 0; i < BITS_TO_U64S(size); i++) {
1145 uint64_t w = qemu_get_be64(f);
1146 bmp[idx++] = w;
1147 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1148 bmp[idx++] = w >> 32;
1149 }
1150 }
1151 return 0;
1152 }
1153
1154 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1155 {
1156 unsigned long *bmp = pv;
1157 int i, idx = 0;
1158 for (i = 0; i < BITS_TO_U64S(size); i++) {
1159 uint64_t w = bmp[idx++];
1160 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1161 w |= ((uint64_t)bmp[idx++]) << 32;
1162 }
1163 qemu_put_be64(f, w);
1164 }
1165 }
1166
1167 const VMStateInfo vmstate_info_bitmap = {
1168 .name = "bitmap",
1169 .get = get_bitmap,
1170 .put = put_bitmap,
1171 };
1172
1173 typedef struct CompatEntry {
1174 char idstr[256];
1175 int instance_id;
1176 } CompatEntry;
1177
1178 typedef struct SaveStateEntry {
1179 QTAILQ_ENTRY(SaveStateEntry) entry;
1180 char idstr[256];
1181 int instance_id;
1182 int alias_id;
1183 int version_id;
1184 int section_id;
1185 SaveVMHandlers *ops;
1186 const VMStateDescription *vmsd;
1187 void *opaque;
1188 CompatEntry *compat;
1189 int no_migrate;
1190 int is_ram;
1191 } SaveStateEntry;
1192
1193
1194 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1195 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1196 static int global_section_id;
1197
1198 static int calculate_new_instance_id(const char *idstr)
1199 {
1200 SaveStateEntry *se;
1201 int instance_id = 0;
1202
1203 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1204 if (strcmp(idstr, se->idstr) == 0
1205 && instance_id <= se->instance_id) {
1206 instance_id = se->instance_id + 1;
1207 }
1208 }
1209 return instance_id;
1210 }
1211
1212 static int calculate_compat_instance_id(const char *idstr)
1213 {
1214 SaveStateEntry *se;
1215 int instance_id = 0;
1216
1217 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1218 if (!se->compat)
1219 continue;
1220
1221 if (strcmp(idstr, se->compat->idstr) == 0
1222 && instance_id <= se->compat->instance_id) {
1223 instance_id = se->compat->instance_id + 1;
1224 }
1225 }
1226 return instance_id;
1227 }
1228
1229 /* TODO: Individual devices generally have very little idea about the rest
1230 of the system, so instance_id should be removed/replaced.
1231 Meanwhile pass -1 as instance_id if you do not already have a clearly
1232 distinguishing id for all instances of your device class. */
1233 int register_savevm_live(DeviceState *dev,
1234 const char *idstr,
1235 int instance_id,
1236 int version_id,
1237 SaveVMHandlers *ops,
1238 void *opaque)
1239 {
1240 SaveStateEntry *se;
1241
1242 se = g_malloc0(sizeof(SaveStateEntry));
1243 se->version_id = version_id;
1244 se->section_id = global_section_id++;
1245 se->ops = ops;
1246 se->opaque = opaque;
1247 se->vmsd = NULL;
1248 se->no_migrate = 0;
1249 /* if this is a live_savem then set is_ram */
1250 if (ops->save_live_setup != NULL) {
1251 se->is_ram = 1;
1252 }
1253
1254 if (dev) {
1255 char *id = qdev_get_dev_path(dev);
1256 if (id) {
1257 pstrcpy(se->idstr, sizeof(se->idstr), id);
1258 pstrcat(se->idstr, sizeof(se->idstr), "/");
1259 g_free(id);
1260
1261 se->compat = g_malloc0(sizeof(CompatEntry));
1262 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1263 se->compat->instance_id = instance_id == -1 ?
1264 calculate_compat_instance_id(idstr) : instance_id;
1265 instance_id = -1;
1266 }
1267 }
1268 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1269
1270 if (instance_id == -1) {
1271 se->instance_id = calculate_new_instance_id(se->idstr);
1272 } else {
1273 se->instance_id = instance_id;
1274 }
1275 assert(!se->compat || se->instance_id == 0);
1276 /* add at the end of list */
1277 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1278 return 0;
1279 }
1280
1281 int register_savevm(DeviceState *dev,
1282 const char *idstr,
1283 int instance_id,
1284 int version_id,
1285 SaveStateHandler *save_state,
1286 LoadStateHandler *load_state,
1287 void *opaque)
1288 {
1289 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1290 ops->save_state = save_state;
1291 ops->load_state = load_state;
1292 return register_savevm_live(dev, idstr, instance_id, version_id,
1293 ops, opaque);
1294 }
1295
1296 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1297 {
1298 SaveStateEntry *se, *new_se;
1299 char id[256] = "";
1300
1301 if (dev) {
1302 char *path = qdev_get_dev_path(dev);
1303 if (path) {
1304 pstrcpy(id, sizeof(id), path);
1305 pstrcat(id, sizeof(id), "/");
1306 g_free(path);
1307 }
1308 }
1309 pstrcat(id, sizeof(id), idstr);
1310
1311 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1312 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1313 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1314 if (se->compat) {
1315 g_free(se->compat);
1316 }
1317 g_free(se->ops);
1318 g_free(se);
1319 }
1320 }
1321 }
1322
1323 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1324 const VMStateDescription *vmsd,
1325 void *opaque, int alias_id,
1326 int required_for_version)
1327 {
1328 SaveStateEntry *se;
1329
1330 /* If this triggers, alias support can be dropped for the vmsd. */
1331 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1332
1333 se = g_malloc0(sizeof(SaveStateEntry));
1334 se->version_id = vmsd->version_id;
1335 se->section_id = global_section_id++;
1336 se->opaque = opaque;
1337 se->vmsd = vmsd;
1338 se->alias_id = alias_id;
1339 se->no_migrate = vmsd->unmigratable;
1340
1341 if (dev) {
1342 char *id = qdev_get_dev_path(dev);
1343 if (id) {
1344 pstrcpy(se->idstr, sizeof(se->idstr), id);
1345 pstrcat(se->idstr, sizeof(se->idstr), "/");
1346 g_free(id);
1347
1348 se->compat = g_malloc0(sizeof(CompatEntry));
1349 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1350 se->compat->instance_id = instance_id == -1 ?
1351 calculate_compat_instance_id(vmsd->name) : instance_id;
1352 instance_id = -1;
1353 }
1354 }
1355 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1356
1357 if (instance_id == -1) {
1358 se->instance_id = calculate_new_instance_id(se->idstr);
1359 } else {
1360 se->instance_id = instance_id;
1361 }
1362 assert(!se->compat || se->instance_id == 0);
1363 /* add at the end of list */
1364 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1365 return 0;
1366 }
1367
1368 int vmstate_register(DeviceState *dev, int instance_id,
1369 const VMStateDescription *vmsd, void *opaque)
1370 {
1371 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1372 opaque, -1, 0);
1373 }
1374
1375 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1376 void *opaque)
1377 {
1378 SaveStateEntry *se, *new_se;
1379
1380 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1381 if (se->vmsd == vmsd && se->opaque == opaque) {
1382 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1383 if (se->compat) {
1384 g_free(se->compat);
1385 }
1386 g_free(se);
1387 }
1388 }
1389 }
1390
1391 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1392 void *opaque);
1393 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1394 void *opaque);
1395
1396 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1397 void *opaque, int version_id)
1398 {
1399 VMStateField *field = vmsd->fields;
1400 int ret;
1401
1402 if (version_id > vmsd->version_id) {
1403 return -EINVAL;
1404 }
1405 if (version_id < vmsd->minimum_version_id_old) {
1406 return -EINVAL;
1407 }
1408 if (version_id < vmsd->minimum_version_id) {
1409 return vmsd->load_state_old(f, opaque, version_id);
1410 }
1411 if (vmsd->pre_load) {
1412 int ret = vmsd->pre_load(opaque);
1413 if (ret)
1414 return ret;
1415 }
1416 while(field->name) {
1417 if ((field->field_exists &&
1418 field->field_exists(opaque, version_id)) ||
1419 (!field->field_exists &&
1420 field->version_id <= version_id)) {
1421 void *base_addr = opaque + field->offset;
1422 int i, n_elems = 1;
1423 int size = field->size;
1424
1425 if (field->flags & VMS_VBUFFER) {
1426 size = *(int32_t *)(opaque+field->size_offset);
1427 if (field->flags & VMS_MULTIPLY) {
1428 size *= field->size;
1429 }
1430 }
1431 if (field->flags & VMS_ARRAY) {
1432 n_elems = field->num;
1433 } else if (field->flags & VMS_VARRAY_INT32) {
1434 n_elems = *(int32_t *)(opaque+field->num_offset);
1435 } else if (field->flags & VMS_VARRAY_UINT32) {
1436 n_elems = *(uint32_t *)(opaque+field->num_offset);
1437 } else if (field->flags & VMS_VARRAY_UINT16) {
1438 n_elems = *(uint16_t *)(opaque+field->num_offset);
1439 } else if (field->flags & VMS_VARRAY_UINT8) {
1440 n_elems = *(uint8_t *)(opaque+field->num_offset);
1441 }
1442 if (field->flags & VMS_POINTER) {
1443 base_addr = *(void **)base_addr + field->start;
1444 }
1445 for (i = 0; i < n_elems; i++) {
1446 void *addr = base_addr + size * i;
1447
1448 if (field->flags & VMS_ARRAY_OF_POINTER) {
1449 addr = *(void **)addr;
1450 }
1451 if (field->flags & VMS_STRUCT) {
1452 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1453 } else {
1454 ret = field->info->get(f, addr, size);
1455
1456 }
1457 if (ret < 0) {
1458 return ret;
1459 }
1460 }
1461 }
1462 field++;
1463 }
1464 ret = vmstate_subsection_load(f, vmsd, opaque);
1465 if (ret != 0) {
1466 return ret;
1467 }
1468 if (vmsd->post_load) {
1469 return vmsd->post_load(opaque, version_id);
1470 }
1471 return 0;
1472 }
1473
1474 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1475 void *opaque)
1476 {
1477 VMStateField *field = vmsd->fields;
1478
1479 if (vmsd->pre_save) {
1480 vmsd->pre_save(opaque);
1481 }
1482 while(field->name) {
1483 if (!field->field_exists ||
1484 field->field_exists(opaque, vmsd->version_id)) {
1485 void *base_addr = opaque + field->offset;
1486 int i, n_elems = 1;
1487 int size = field->size;
1488
1489 if (field->flags & VMS_VBUFFER) {
1490 size = *(int32_t *)(opaque+field->size_offset);
1491 if (field->flags & VMS_MULTIPLY) {
1492 size *= field->size;
1493 }
1494 }
1495 if (field->flags & VMS_ARRAY) {
1496 n_elems = field->num;
1497 } else if (field->flags & VMS_VARRAY_INT32) {
1498 n_elems = *(int32_t *)(opaque+field->num_offset);
1499 } else if (field->flags & VMS_VARRAY_UINT32) {
1500 n_elems = *(uint32_t *)(opaque+field->num_offset);
1501 } else if (field->flags & VMS_VARRAY_UINT16) {
1502 n_elems = *(uint16_t *)(opaque+field->num_offset);
1503 } else if (field->flags & VMS_VARRAY_UINT8) {
1504 n_elems = *(uint8_t *)(opaque+field->num_offset);
1505 }
1506 if (field->flags & VMS_POINTER) {
1507 base_addr = *(void **)base_addr + field->start;
1508 }
1509 for (i = 0; i < n_elems; i++) {
1510 void *addr = base_addr + size * i;
1511
1512 if (field->flags & VMS_ARRAY_OF_POINTER) {
1513 addr = *(void **)addr;
1514 }
1515 if (field->flags & VMS_STRUCT) {
1516 vmstate_save_state(f, field->vmsd, addr);
1517 } else {
1518 field->info->put(f, addr, size);
1519 }
1520 }
1521 }
1522 field++;
1523 }
1524 vmstate_subsection_save(f, vmsd, opaque);
1525 }
1526
1527 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1528 {
1529 if (!se->vmsd) { /* Old style */
1530 return se->ops->load_state(f, se->opaque, version_id);
1531 }
1532 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1533 }
1534
1535 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1536 {
1537 if (!se->vmsd) { /* Old style */
1538 se->ops->save_state(f, se->opaque);
1539 return;
1540 }
1541 vmstate_save_state(f,se->vmsd, se->opaque);
1542 }
1543
1544 #define QEMU_VM_FILE_MAGIC 0x5145564d
1545 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1546 #define QEMU_VM_FILE_VERSION 0x00000003
1547
1548 #define QEMU_VM_EOF 0x00
1549 #define QEMU_VM_SECTION_START 0x01
1550 #define QEMU_VM_SECTION_PART 0x02
1551 #define QEMU_VM_SECTION_END 0x03
1552 #define QEMU_VM_SECTION_FULL 0x04
1553 #define QEMU_VM_SUBSECTION 0x05
1554
1555 bool qemu_savevm_state_blocked(Error **errp)
1556 {
1557 SaveStateEntry *se;
1558
1559 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1560 if (se->no_migrate) {
1561 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1562 return true;
1563 }
1564 }
1565 return false;
1566 }
1567
1568 int qemu_savevm_state_begin(QEMUFile *f,
1569 const MigrationParams *params)
1570 {
1571 SaveStateEntry *se;
1572 int ret;
1573
1574 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1575 if (!se->ops || !se->ops->set_params) {
1576 continue;
1577 }
1578 se->ops->set_params(params, se->opaque);
1579 }
1580
1581 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1582 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1583
1584 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1585 int len;
1586
1587 if (!se->ops || !se->ops->save_live_setup) {
1588 continue;
1589 }
1590 if (se->ops && se->ops->is_active) {
1591 if (!se->ops->is_active(se->opaque)) {
1592 continue;
1593 }
1594 }
1595 /* Section type */
1596 qemu_put_byte(f, QEMU_VM_SECTION_START);
1597 qemu_put_be32(f, se->section_id);
1598
1599 /* ID string */
1600 len = strlen(se->idstr);
1601 qemu_put_byte(f, len);
1602 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1603
1604 qemu_put_be32(f, se->instance_id);
1605 qemu_put_be32(f, se->version_id);
1606
1607 ret = se->ops->save_live_setup(f, se->opaque);
1608 if (ret < 0) {
1609 qemu_savevm_state_cancel(f);
1610 return ret;
1611 }
1612 }
1613 ret = qemu_file_get_error(f);
1614 if (ret != 0) {
1615 qemu_savevm_state_cancel(f);
1616 }
1617
1618 return ret;
1619
1620 }
1621
1622 /*
1623 * this function has three return values:
1624 * negative: there was one error, and we have -errno.
1625 * 0 : We haven't finished, caller have to go again
1626 * 1 : We have finished, we can go to complete phase
1627 */
1628 int qemu_savevm_state_iterate(QEMUFile *f)
1629 {
1630 SaveStateEntry *se;
1631 int ret = 1;
1632
1633 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1634 if (!se->ops || !se->ops->save_live_iterate) {
1635 continue;
1636 }
1637 if (se->ops && se->ops->is_active) {
1638 if (!se->ops->is_active(se->opaque)) {
1639 continue;
1640 }
1641 }
1642 if (qemu_file_rate_limit(f)) {
1643 return 0;
1644 }
1645 trace_savevm_section_start();
1646 /* Section type */
1647 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1648 qemu_put_be32(f, se->section_id);
1649
1650 ret = se->ops->save_live_iterate(f, se->opaque);
1651 trace_savevm_section_end(se->section_id);
1652
1653 if (ret <= 0) {
1654 /* Do not proceed to the next vmstate before this one reported
1655 completion of the current stage. This serializes the migration
1656 and reduces the probability that a faster changing state is
1657 synchronized over and over again. */
1658 break;
1659 }
1660 }
1661 if (ret != 0) {
1662 return ret;
1663 }
1664 ret = qemu_file_get_error(f);
1665 if (ret != 0) {
1666 qemu_savevm_state_cancel(f);
1667 }
1668 return ret;
1669 }
1670
1671 int qemu_savevm_state_complete(QEMUFile *f)
1672 {
1673 SaveStateEntry *se;
1674 int ret;
1675
1676 cpu_synchronize_all_states();
1677
1678 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1679 if (!se->ops || !se->ops->save_live_complete) {
1680 continue;
1681 }
1682 if (se->ops && se->ops->is_active) {
1683 if (!se->ops->is_active(se->opaque)) {
1684 continue;
1685 }
1686 }
1687 trace_savevm_section_start();
1688 /* Section type */
1689 qemu_put_byte(f, QEMU_VM_SECTION_END);
1690 qemu_put_be32(f, se->section_id);
1691
1692 ret = se->ops->save_live_complete(f, se->opaque);
1693 trace_savevm_section_end(se->section_id);
1694 if (ret < 0) {
1695 return ret;
1696 }
1697 }
1698
1699 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1700 int len;
1701
1702 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1703 continue;
1704 }
1705 trace_savevm_section_start();
1706 /* Section type */
1707 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1708 qemu_put_be32(f, se->section_id);
1709
1710 /* ID string */
1711 len = strlen(se->idstr);
1712 qemu_put_byte(f, len);
1713 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1714
1715 qemu_put_be32(f, se->instance_id);
1716 qemu_put_be32(f, se->version_id);
1717
1718 vmstate_save(f, se);
1719 trace_savevm_section_end(se->section_id);
1720 }
1721
1722 qemu_put_byte(f, QEMU_VM_EOF);
1723
1724 return qemu_file_get_error(f);
1725 }
1726
1727 void qemu_savevm_state_cancel(QEMUFile *f)
1728 {
1729 SaveStateEntry *se;
1730
1731 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1732 if (se->ops && se->ops->cancel) {
1733 se->ops->cancel(se->opaque);
1734 }
1735 }
1736 }
1737
1738 static int qemu_savevm_state(QEMUFile *f)
1739 {
1740 int ret;
1741 MigrationParams params = {
1742 .blk = 0,
1743 .shared = 0
1744 };
1745
1746 if (qemu_savevm_state_blocked(NULL)) {
1747 ret = -EINVAL;
1748 goto out;
1749 }
1750
1751 ret = qemu_savevm_state_begin(f, &params);
1752 if (ret < 0)
1753 goto out;
1754
1755 do {
1756 ret = qemu_savevm_state_iterate(f);
1757 if (ret < 0)
1758 goto out;
1759 } while (ret == 0);
1760
1761 ret = qemu_savevm_state_complete(f);
1762
1763 out:
1764 if (ret == 0) {
1765 ret = qemu_file_get_error(f);
1766 }
1767
1768 return ret;
1769 }
1770
1771 static int qemu_save_device_state(QEMUFile *f)
1772 {
1773 SaveStateEntry *se;
1774
1775 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1776 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1777
1778 cpu_synchronize_all_states();
1779
1780 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1781 int len;
1782
1783 if (se->is_ram) {
1784 continue;
1785 }
1786 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1787 continue;
1788 }
1789
1790 /* Section type */
1791 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1792 qemu_put_be32(f, se->section_id);
1793
1794 /* ID string */
1795 len = strlen(se->idstr);
1796 qemu_put_byte(f, len);
1797 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1798
1799 qemu_put_be32(f, se->instance_id);
1800 qemu_put_be32(f, se->version_id);
1801
1802 vmstate_save(f, se);
1803 }
1804
1805 qemu_put_byte(f, QEMU_VM_EOF);
1806
1807 return qemu_file_get_error(f);
1808 }
1809
1810 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1811 {
1812 SaveStateEntry *se;
1813
1814 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1815 if (!strcmp(se->idstr, idstr) &&
1816 (instance_id == se->instance_id ||
1817 instance_id == se->alias_id))
1818 return se;
1819 /* Migrating from an older version? */
1820 if (strstr(se->idstr, idstr) && se->compat) {
1821 if (!strcmp(se->compat->idstr, idstr) &&
1822 (instance_id == se->compat->instance_id ||
1823 instance_id == se->alias_id))
1824 return se;
1825 }
1826 }
1827 return NULL;
1828 }
1829
1830 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1831 {
1832 while(sub && sub->needed) {
1833 if (strcmp(idstr, sub->vmsd->name) == 0) {
1834 return sub->vmsd;
1835 }
1836 sub++;
1837 }
1838 return NULL;
1839 }
1840
1841 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1842 void *opaque)
1843 {
1844 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
1845 char idstr[256];
1846 int ret;
1847 uint8_t version_id, len, size;
1848 const VMStateDescription *sub_vmsd;
1849
1850 len = qemu_peek_byte(f, 1);
1851 if (len < strlen(vmsd->name) + 1) {
1852 /* subsection name has be be "section_name/a" */
1853 return 0;
1854 }
1855 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1856 if (size != len) {
1857 return 0;
1858 }
1859 idstr[size] = 0;
1860
1861 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1862 /* it don't have a valid subsection name */
1863 return 0;
1864 }
1865 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
1866 if (sub_vmsd == NULL) {
1867 return -ENOENT;
1868 }
1869 qemu_file_skip(f, 1); /* subsection */
1870 qemu_file_skip(f, 1); /* len */
1871 qemu_file_skip(f, len); /* idstr */
1872 version_id = qemu_get_be32(f);
1873
1874 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1875 if (ret) {
1876 return ret;
1877 }
1878 }
1879 return 0;
1880 }
1881
1882 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1883 void *opaque)
1884 {
1885 const VMStateSubsection *sub = vmsd->subsections;
1886
1887 while (sub && sub->needed) {
1888 if (sub->needed(opaque)) {
1889 const VMStateDescription *vmsd = sub->vmsd;
1890 uint8_t len;
1891
1892 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1893 len = strlen(vmsd->name);
1894 qemu_put_byte(f, len);
1895 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1896 qemu_put_be32(f, vmsd->version_id);
1897 vmstate_save_state(f, vmsd, opaque);
1898 }
1899 sub++;
1900 }
1901 }
1902
1903 typedef struct LoadStateEntry {
1904 QLIST_ENTRY(LoadStateEntry) entry;
1905 SaveStateEntry *se;
1906 int section_id;
1907 int version_id;
1908 } LoadStateEntry;
1909
1910 int qemu_loadvm_state(QEMUFile *f)
1911 {
1912 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1913 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1914 LoadStateEntry *le, *new_le;
1915 uint8_t section_type;
1916 unsigned int v;
1917 int ret;
1918
1919 if (qemu_savevm_state_blocked(NULL)) {
1920 return -EINVAL;
1921 }
1922
1923 v = qemu_get_be32(f);
1924 if (v != QEMU_VM_FILE_MAGIC)
1925 return -EINVAL;
1926
1927 v = qemu_get_be32(f);
1928 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1929 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1930 return -ENOTSUP;
1931 }
1932 if (v != QEMU_VM_FILE_VERSION)
1933 return -ENOTSUP;
1934
1935 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1936 uint32_t instance_id, version_id, section_id;
1937 SaveStateEntry *se;
1938 char idstr[257];
1939 int len;
1940
1941 switch (section_type) {
1942 case QEMU_VM_SECTION_START:
1943 case QEMU_VM_SECTION_FULL:
1944 /* Read section start */
1945 section_id = qemu_get_be32(f);
1946 len = qemu_get_byte(f);
1947 qemu_get_buffer(f, (uint8_t *)idstr, len);
1948 idstr[len] = 0;
1949 instance_id = qemu_get_be32(f);
1950 version_id = qemu_get_be32(f);
1951
1952 /* Find savevm section */
1953 se = find_se(idstr, instance_id);
1954 if (se == NULL) {
1955 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1956 ret = -EINVAL;
1957 goto out;
1958 }
1959
1960 /* Validate version */
1961 if (version_id > se->version_id) {
1962 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1963 version_id, idstr, se->version_id);
1964 ret = -EINVAL;
1965 goto out;
1966 }
1967
1968 /* Add entry */
1969 le = g_malloc0(sizeof(*le));
1970
1971 le->se = se;
1972 le->section_id = section_id;
1973 le->version_id = version_id;
1974 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1975
1976 ret = vmstate_load(f, le->se, le->version_id);
1977 if (ret < 0) {
1978 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1979 instance_id, idstr);
1980 goto out;
1981 }
1982 break;
1983 case QEMU_VM_SECTION_PART:
1984 case QEMU_VM_SECTION_END:
1985 section_id = qemu_get_be32(f);
1986
1987 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1988 if (le->section_id == section_id) {
1989 break;
1990 }
1991 }
1992 if (le == NULL) {
1993 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1994 ret = -EINVAL;
1995 goto out;
1996 }
1997
1998 ret = vmstate_load(f, le->se, le->version_id);
1999 if (ret < 0) {
2000 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2001 section_id);
2002 goto out;
2003 }
2004 break;
2005 default:
2006 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2007 ret = -EINVAL;
2008 goto out;
2009 }
2010 }
2011
2012 cpu_synchronize_all_post_init();
2013
2014 ret = 0;
2015
2016 out:
2017 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2018 QLIST_REMOVE(le, entry);
2019 g_free(le);
2020 }
2021
2022 if (ret == 0) {
2023 ret = qemu_file_get_error(f);
2024 }
2025
2026 return ret;
2027 }
2028
2029 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2030 const char *name)
2031 {
2032 QEMUSnapshotInfo *sn_tab, *sn;
2033 int nb_sns, i, ret;
2034
2035 ret = -ENOENT;
2036 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2037 if (nb_sns < 0)
2038 return ret;
2039 for(i = 0; i < nb_sns; i++) {
2040 sn = &sn_tab[i];
2041 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2042 *sn_info = *sn;
2043 ret = 0;
2044 break;
2045 }
2046 }
2047 g_free(sn_tab);
2048 return ret;
2049 }
2050
2051 /*
2052 * Deletes snapshots of a given name in all opened images.
2053 */
2054 static int del_existing_snapshots(Monitor *mon, const char *name)
2055 {
2056 BlockDriverState *bs;
2057 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2058 int ret;
2059
2060 bs = NULL;
2061 while ((bs = bdrv_next(bs))) {
2062 if (bdrv_can_snapshot(bs) &&
2063 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2064 {
2065 ret = bdrv_snapshot_delete(bs, name);
2066 if (ret < 0) {
2067 monitor_printf(mon,
2068 "Error while deleting snapshot on '%s'\n",
2069 bdrv_get_device_name(bs));
2070 return -1;
2071 }
2072 }
2073 }
2074
2075 return 0;
2076 }
2077
2078 void do_savevm(Monitor *mon, const QDict *qdict)
2079 {
2080 BlockDriverState *bs, *bs1;
2081 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2082 int ret;
2083 QEMUFile *f;
2084 int saved_vm_running;
2085 uint64_t vm_state_size;
2086 #ifdef _WIN32
2087 struct _timeb tb;
2088 struct tm *ptm;
2089 #else
2090 struct timeval tv;
2091 struct tm tm;
2092 #endif
2093 const char *name = qdict_get_try_str(qdict, "name");
2094
2095 /* Verify if there is a device that doesn't support snapshots and is writable */
2096 bs = NULL;
2097 while ((bs = bdrv_next(bs))) {
2098
2099 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2100 continue;
2101 }
2102
2103 if (!bdrv_can_snapshot(bs)) {
2104 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2105 bdrv_get_device_name(bs));
2106 return;
2107 }
2108 }
2109
2110 bs = bdrv_snapshots();
2111 if (!bs) {
2112 monitor_printf(mon, "No block device can accept snapshots\n");
2113 return;
2114 }
2115
2116 saved_vm_running = runstate_is_running();
2117 vm_stop(RUN_STATE_SAVE_VM);
2118
2119 memset(sn, 0, sizeof(*sn));
2120
2121 /* fill auxiliary fields */
2122 #ifdef _WIN32
2123 _ftime(&tb);
2124 sn->date_sec = tb.time;
2125 sn->date_nsec = tb.millitm * 1000000;
2126 #else
2127 gettimeofday(&tv, NULL);
2128 sn->date_sec = tv.tv_sec;
2129 sn->date_nsec = tv.tv_usec * 1000;
2130 #endif
2131 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2132
2133 if (name) {
2134 ret = bdrv_snapshot_find(bs, old_sn, name);
2135 if (ret >= 0) {
2136 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2137 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2138 } else {
2139 pstrcpy(sn->name, sizeof(sn->name), name);
2140 }
2141 } else {
2142 #ifdef _WIN32
2143 time_t t = tb.time;
2144 ptm = localtime(&t);
2145 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2146 #else
2147 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2148 localtime_r((const time_t *)&tv.tv_sec, &tm);
2149 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2150 #endif
2151 }
2152
2153 /* Delete old snapshots of the same name */
2154 if (name && del_existing_snapshots(mon, name) < 0) {
2155 goto the_end;
2156 }
2157
2158 /* save the VM state */
2159 f = qemu_fopen_bdrv(bs, 1);
2160 if (!f) {
2161 monitor_printf(mon, "Could not open VM state file\n");
2162 goto the_end;
2163 }
2164 ret = qemu_savevm_state(f);
2165 vm_state_size = qemu_ftell(f);
2166 qemu_fclose(f);
2167 if (ret < 0) {
2168 monitor_printf(mon, "Error %d while writing VM\n", ret);
2169 goto the_end;
2170 }
2171
2172 /* create the snapshots */
2173
2174 bs1 = NULL;
2175 while ((bs1 = bdrv_next(bs1))) {
2176 if (bdrv_can_snapshot(bs1)) {
2177 /* Write VM state size only to the image that contains the state */
2178 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2179 ret = bdrv_snapshot_create(bs1, sn);
2180 if (ret < 0) {
2181 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2182 bdrv_get_device_name(bs1));
2183 }
2184 }
2185 }
2186
2187 the_end:
2188 if (saved_vm_running)
2189 vm_start();
2190 }
2191
2192 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2193 {
2194 QEMUFile *f;
2195 int saved_vm_running;
2196 int ret;
2197
2198 saved_vm_running = runstate_is_running();
2199 vm_stop(RUN_STATE_SAVE_VM);
2200
2201 f = qemu_fopen(filename, "wb");
2202 if (!f) {
2203 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2204 goto the_end;
2205 }
2206 ret = qemu_save_device_state(f);
2207 qemu_fclose(f);
2208 if (ret < 0) {
2209 error_set(errp, QERR_IO_ERROR);
2210 }
2211
2212 the_end:
2213 if (saved_vm_running)
2214 vm_start();
2215 }
2216
2217 int load_vmstate(const char *name)
2218 {
2219 BlockDriverState *bs, *bs_vm_state;
2220 QEMUSnapshotInfo sn;
2221 QEMUFile *f;
2222 int ret;
2223
2224 bs_vm_state = bdrv_snapshots();
2225 if (!bs_vm_state) {
2226 error_report("No block device supports snapshots");
2227 return -ENOTSUP;
2228 }
2229
2230 /* Don't even try to load empty VM states */
2231 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2232 if (ret < 0) {
2233 return ret;
2234 } else if (sn.vm_state_size == 0) {
2235 error_report("This is a disk-only snapshot. Revert to it offline "
2236 "using qemu-img.");
2237 return -EINVAL;
2238 }
2239
2240 /* Verify if there is any device that doesn't support snapshots and is
2241 writable and check if the requested snapshot is available too. */
2242 bs = NULL;
2243 while ((bs = bdrv_next(bs))) {
2244
2245 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2246 continue;
2247 }
2248
2249 if (!bdrv_can_snapshot(bs)) {
2250 error_report("Device '%s' is writable but does not support snapshots.",
2251 bdrv_get_device_name(bs));
2252 return -ENOTSUP;
2253 }
2254
2255 ret = bdrv_snapshot_find(bs, &sn, name);
2256 if (ret < 0) {
2257 error_report("Device '%s' does not have the requested snapshot '%s'",
2258 bdrv_get_device_name(bs), name);
2259 return ret;
2260 }
2261 }
2262
2263 /* Flush all IO requests so they don't interfere with the new state. */
2264 bdrv_drain_all();
2265
2266 bs = NULL;
2267 while ((bs = bdrv_next(bs))) {
2268 if (bdrv_can_snapshot(bs)) {
2269 ret = bdrv_snapshot_goto(bs, name);
2270 if (ret < 0) {
2271 error_report("Error %d while activating snapshot '%s' on '%s'",
2272 ret, name, bdrv_get_device_name(bs));
2273 return ret;
2274 }
2275 }
2276 }
2277
2278 /* restore the VM state */
2279 f = qemu_fopen_bdrv(bs_vm_state, 0);
2280 if (!f) {
2281 error_report("Could not open VM state file");
2282 return -EINVAL;
2283 }
2284
2285 qemu_system_reset(VMRESET_SILENT);
2286 ret = qemu_loadvm_state(f);
2287
2288 qemu_fclose(f);
2289 if (ret < 0) {
2290 error_report("Error %d while loading VM state", ret);
2291 return ret;
2292 }
2293
2294 return 0;
2295 }
2296
2297 void do_delvm(Monitor *mon, const QDict *qdict)
2298 {
2299 BlockDriverState *bs, *bs1;
2300 int ret;
2301 const char *name = qdict_get_str(qdict, "name");
2302
2303 bs = bdrv_snapshots();
2304 if (!bs) {
2305 monitor_printf(mon, "No block device supports snapshots\n");
2306 return;
2307 }
2308
2309 bs1 = NULL;
2310 while ((bs1 = bdrv_next(bs1))) {
2311 if (bdrv_can_snapshot(bs1)) {
2312 ret = bdrv_snapshot_delete(bs1, name);
2313 if (ret < 0) {
2314 if (ret == -ENOTSUP)
2315 monitor_printf(mon,
2316 "Snapshots not supported on device '%s'\n",
2317 bdrv_get_device_name(bs1));
2318 else
2319 monitor_printf(mon, "Error %d while deleting snapshot on "
2320 "'%s'\n", ret, bdrv_get_device_name(bs1));
2321 }
2322 }
2323 }
2324 }
2325
2326 void do_info_snapshots(Monitor *mon)
2327 {
2328 BlockDriverState *bs, *bs1;
2329 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2330 int nb_sns, i, ret, available;
2331 int total;
2332 int *available_snapshots;
2333 char buf[256];
2334
2335 bs = bdrv_snapshots();
2336 if (!bs) {
2337 monitor_printf(mon, "No available block device supports snapshots\n");
2338 return;
2339 }
2340
2341 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2342 if (nb_sns < 0) {
2343 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2344 return;
2345 }
2346
2347 if (nb_sns == 0) {
2348 monitor_printf(mon, "There is no snapshot available.\n");
2349 return;
2350 }
2351
2352 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2353 total = 0;
2354 for (i = 0; i < nb_sns; i++) {
2355 sn = &sn_tab[i];
2356 available = 1;
2357 bs1 = NULL;
2358
2359 while ((bs1 = bdrv_next(bs1))) {
2360 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2361 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2362 if (ret < 0) {
2363 available = 0;
2364 break;
2365 }
2366 }
2367 }
2368
2369 if (available) {
2370 available_snapshots[total] = i;
2371 total++;
2372 }
2373 }
2374
2375 if (total > 0) {
2376 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2377 for (i = 0; i < total; i++) {
2378 sn = &sn_tab[available_snapshots[i]];
2379 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2380 }
2381 } else {
2382 monitor_printf(mon, "There is no suitable snapshot available\n");
2383 }
2384
2385 g_free(sn_tab);
2386 g_free(available_snapshots);
2387
2388 }
2389
2390 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2391 {
2392 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2393 memory_region_name(mr), dev);
2394 }
2395
2396 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2397 {
2398 /* Nothing do to while the implementation is in RAMBlock */
2399 }
2400
2401 void vmstate_register_ram_global(MemoryRegion *mr)
2402 {
2403 vmstate_register_ram(mr, NULL);
2404 }
2405
2406 /*
2407 page = zrun nzrun
2408 | zrun nzrun page
2409
2410 zrun = length
2411
2412 nzrun = length byte...
2413
2414 length = uleb128 encoded integer
2415 */
2416 int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2417 uint8_t *dst, int dlen)
2418 {
2419 uint32_t zrun_len = 0, nzrun_len = 0;
2420 int d = 0, i = 0;
2421 long res, xor;
2422 uint8_t *nzrun_start = NULL;
2423
2424 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2425 sizeof(long)));
2426
2427 while (i < slen) {
2428 /* overflow */
2429 if (d + 2 > dlen) {
2430 return -1;
2431 }
2432
2433 /* not aligned to sizeof(long) */
2434 res = (slen - i) % sizeof(long);
2435 while (res && old_buf[i] == new_buf[i]) {
2436 zrun_len++;
2437 i++;
2438 res--;
2439 }
2440
2441 /* word at a time for speed */
2442 if (!res) {
2443 while (i < slen &&
2444 (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2445 i += sizeof(long);
2446 zrun_len += sizeof(long);
2447 }
2448
2449 /* go over the rest */
2450 while (i < slen && old_buf[i] == new_buf[i]) {
2451 zrun_len++;
2452 i++;
2453 }
2454 }
2455
2456 /* buffer unchanged */
2457 if (zrun_len == slen) {
2458 return 0;
2459 }
2460
2461 /* skip last zero run */
2462 if (i == slen) {
2463 return d;
2464 }
2465
2466 d += uleb128_encode_small(dst + d, zrun_len);
2467
2468 zrun_len = 0;
2469 nzrun_start = new_buf + i;
2470
2471 /* overflow */
2472 if (d + 2 > dlen) {
2473 return -1;
2474 }
2475 /* not aligned to sizeof(long) */
2476 res = (slen - i) % sizeof(long);
2477 while (res && old_buf[i] != new_buf[i]) {
2478 i++;
2479 nzrun_len++;
2480 res--;
2481 }
2482
2483 /* word at a time for speed, use of 32-bit long okay */
2484 if (!res) {
2485 /* truncation to 32-bit long okay */
2486 long mask = (long)0x0101010101010101ULL;
2487 while (i < slen) {
2488 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2489 if ((xor - mask) & ~xor & (mask << 7)) {
2490 /* found the end of an nzrun within the current long */
2491 while (old_buf[i] != new_buf[i]) {
2492 nzrun_len++;
2493 i++;
2494 }
2495 break;
2496 } else {
2497 i += sizeof(long);
2498 nzrun_len += sizeof(long);
2499 }
2500 }
2501 }
2502
2503 d += uleb128_encode_small(dst + d, nzrun_len);
2504 /* overflow */
2505 if (d + nzrun_len > dlen) {
2506 return -1;
2507 }
2508 memcpy(dst + d, nzrun_start, nzrun_len);
2509 d += nzrun_len;
2510 nzrun_len = 0;
2511 }
2512
2513 return d;
2514 }
2515
2516 int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2517 {
2518 int i = 0, d = 0;
2519 int ret;
2520 uint32_t count = 0;
2521
2522 while (i < slen) {
2523
2524 /* zrun */
2525 if ((slen - i) < 2) {
2526 return -1;
2527 }
2528
2529 ret = uleb128_decode_small(src + i, &count);
2530 if (ret < 0 || (i && !count)) {
2531 return -1;
2532 }
2533 i += ret;
2534 d += count;
2535
2536 /* overflow */
2537 if (d > dlen) {
2538 return -1;
2539 }
2540
2541 /* nzrun */
2542 if ((slen - i) < 2) {
2543 return -1;
2544 }
2545
2546 ret = uleb128_decode_small(src + i, &count);
2547 if (ret < 0 || !count) {
2548 return -1;
2549 }
2550 i += ret;
2551
2552 /* overflow */
2553 if (d + count > dlen || i + count > slen) {
2554 return -1;
2555 }
2556
2557 memcpy(dst + d, src + i, count);
2558 d += count;
2559 i += count;
2560 }
2561
2562 return d;
2563 }