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