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