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