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