Merge remote-tracking branch 'kraxel/testdev.1' into staging
[qemu/ar7.git] / savevm.c
blob529d60ec1fb18be4f227522aebde03a402637b34
1 /*
2 * QEMU System Emulator
4 * Copyright (c) 2003-2008 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
25 #include "config-host.h"
27 #ifndef _WIN32
28 #include <arpa/inet.h>
29 #endif
31 #ifdef _WIN32
32 #include <windows.h>
33 #endif
35 #include "qemu-common.h"
36 #include "hw/hw.h"
37 #include "hw/qdev.h"
38 #include "net/net.h"
39 #include "monitor/monitor.h"
40 #include "sysemu/sysemu.h"
41 #include "qemu/timer.h"
42 #include "audio/audio.h"
43 #include "migration/migration.h"
44 #include "qemu/sockets.h"
45 #include "qemu/queue.h"
46 #include "sysemu/cpus.h"
47 #include "exec/memory.h"
48 #include "qmp-commands.h"
49 #include "trace.h"
50 #include "qemu/bitops.h"
52 #define SELF_ANNOUNCE_ROUNDS 5
54 #ifndef ETH_P_RARP
55 #define ETH_P_RARP 0x8035
56 #endif
57 #define ARP_HTYPE_ETH 0x0001
58 #define ARP_PTYPE_IP 0x0800
59 #define ARP_OP_REQUEST_REV 0x3
61 static int announce_self_create(uint8_t *buf,
62 uint8_t *mac_addr)
64 /* Ethernet header. */
65 memset(buf, 0xff, 6); /* destination MAC addr */
66 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
67 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
69 /* RARP header. */
70 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
71 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
72 *(buf + 18) = 6; /* hardware addr length (ethernet) */
73 *(buf + 19) = 4; /* protocol addr length (IPv4) */
74 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
75 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
76 memset(buf + 28, 0x00, 4); /* source protocol addr */
77 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
78 memset(buf + 38, 0x00, 4); /* target protocol addr */
80 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
81 memset(buf + 42, 0x00, 18);
83 return 60; /* len (FCS will be added by hardware) */
86 static void qemu_announce_self_iter(NICState *nic, void *opaque)
88 uint8_t buf[60];
89 int len;
91 len = announce_self_create(buf, nic->conf->macaddr.a);
93 qemu_send_packet_raw(&nic->nc, buf, len);
97 static void qemu_announce_self_once(void *opaque)
99 static int count = SELF_ANNOUNCE_ROUNDS;
100 QEMUTimer *timer = *(QEMUTimer **)opaque;
102 qemu_foreach_nic(qemu_announce_self_iter, NULL);
104 if (--count) {
105 /* delay 50ms, 150ms, 250ms, ... */
106 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
107 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
108 } else {
109 qemu_del_timer(timer);
110 qemu_free_timer(timer);
114 void qemu_announce_self(void)
116 static QEMUTimer *timer;
117 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
118 qemu_announce_self_once(&timer);
121 /***********************************************************/
122 /* savevm/loadvm support */
124 #define IO_BUF_SIZE 32768
126 struct QEMUFile {
127 const QEMUFileOps *ops;
128 void *opaque;
129 int is_write;
131 int64_t buf_offset; /* start of buffer when writing, end of buffer
132 when reading */
133 int buf_index;
134 int buf_size; /* 0 when writing */
135 uint8_t buf[IO_BUF_SIZE];
137 int last_error;
140 typedef struct QEMUFileStdio
142 FILE *stdio_file;
143 QEMUFile *file;
144 } QEMUFileStdio;
146 typedef struct QEMUFileSocket
148 int fd;
149 QEMUFile *file;
150 } QEMUFileSocket;
152 static int socket_get_fd(void *opaque)
154 QEMUFileSocket *s = opaque;
156 return s->fd;
159 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
161 QEMUFileSocket *s = opaque;
162 ssize_t len;
164 for (;;) {
165 len = qemu_recv(s->fd, buf, size, 0);
166 if (len != -1) {
167 break;
169 if (socket_error() == EAGAIN) {
170 assert(qemu_in_coroutine());
171 qemu_coroutine_yield();
172 } else if (socket_error() != EINTR) {
173 break;
177 if (len == -1) {
178 len = -socket_error();
180 return len;
183 static int socket_close(void *opaque)
185 QEMUFileSocket *s = opaque;
186 closesocket(s->fd);
187 g_free(s);
188 return 0;
191 static int stdio_get_fd(void *opaque)
193 QEMUFileStdio *s = opaque;
195 return fileno(s->stdio_file);
198 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
200 QEMUFileStdio *s = opaque;
201 return fwrite(buf, 1, size, s->stdio_file);
204 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
206 QEMUFileStdio *s = opaque;
207 FILE *fp = s->stdio_file;
208 int bytes;
210 for (;;) {
211 clearerr(fp);
212 bytes = fread(buf, 1, size, fp);
213 if (bytes != 0 || !ferror(fp)) {
214 break;
216 if (errno == EAGAIN) {
217 assert(qemu_in_coroutine());
218 qemu_coroutine_yield();
219 } else if (errno != EINTR) {
220 break;
223 return bytes;
226 static int stdio_pclose(void *opaque)
228 QEMUFileStdio *s = opaque;
229 int ret;
230 ret = pclose(s->stdio_file);
231 if (ret == -1) {
232 ret = -errno;
234 g_free(s);
235 return ret;
238 static int stdio_fclose(void *opaque)
240 QEMUFileStdio *s = opaque;
241 int ret = 0;
242 if (fclose(s->stdio_file) == EOF) {
243 ret = -errno;
245 g_free(s);
246 return ret;
249 static const QEMUFileOps stdio_pipe_read_ops = {
250 .get_fd = stdio_get_fd,
251 .get_buffer = stdio_get_buffer,
252 .close = stdio_pclose
255 static const QEMUFileOps stdio_pipe_write_ops = {
256 .get_fd = stdio_get_fd,
257 .put_buffer = stdio_put_buffer,
258 .close = stdio_pclose
261 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
263 QEMUFileStdio *s;
265 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
266 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
267 return NULL;
270 s = g_malloc0(sizeof(QEMUFileStdio));
272 s->stdio_file = stdio_file;
274 if(mode[0] == 'r') {
275 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
276 } else {
277 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
279 return s->file;
282 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
284 FILE *popen_file;
286 popen_file = popen(command, mode);
287 if(popen_file == NULL) {
288 return NULL;
291 return qemu_popen(popen_file, mode);
294 static const QEMUFileOps stdio_file_read_ops = {
295 .get_fd = stdio_get_fd,
296 .get_buffer = stdio_get_buffer,
297 .close = stdio_fclose
300 static const QEMUFileOps stdio_file_write_ops = {
301 .get_fd = stdio_get_fd,
302 .put_buffer = stdio_put_buffer,
303 .close = stdio_fclose
306 QEMUFile *qemu_fdopen(int fd, const char *mode)
308 QEMUFileStdio *s;
310 if (mode == NULL ||
311 (mode[0] != 'r' && mode[0] != 'w') ||
312 mode[1] != 'b' || mode[2] != 0) {
313 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
314 return NULL;
317 s = g_malloc0(sizeof(QEMUFileStdio));
318 s->stdio_file = fdopen(fd, mode);
319 if (!s->stdio_file)
320 goto fail;
322 if(mode[0] == 'r') {
323 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
324 } else {
325 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
327 return s->file;
329 fail:
330 g_free(s);
331 return NULL;
334 static const QEMUFileOps socket_read_ops = {
335 .get_fd = socket_get_fd,
336 .get_buffer = socket_get_buffer,
337 .close = socket_close
340 QEMUFile *qemu_fopen_socket(int fd)
342 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
344 s->fd = fd;
345 s->file = qemu_fopen_ops(s, &socket_read_ops);
346 return s->file;
349 QEMUFile *qemu_fopen(const char *filename, const char *mode)
351 QEMUFileStdio *s;
353 if (mode == NULL ||
354 (mode[0] != 'r' && mode[0] != 'w') ||
355 mode[1] != 'b' || mode[2] != 0) {
356 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
357 return NULL;
360 s = g_malloc0(sizeof(QEMUFileStdio));
362 s->stdio_file = fopen(filename, mode);
363 if (!s->stdio_file)
364 goto fail;
366 if(mode[0] == 'w') {
367 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
368 } else {
369 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
371 return s->file;
372 fail:
373 g_free(s);
374 return NULL;
377 static int block_put_buffer(void *opaque, const uint8_t *buf,
378 int64_t pos, int size)
380 bdrv_save_vmstate(opaque, buf, pos, size);
381 return size;
384 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
386 return bdrv_load_vmstate(opaque, buf, pos, size);
389 static int bdrv_fclose(void *opaque)
391 return bdrv_flush(opaque);
394 static const QEMUFileOps bdrv_read_ops = {
395 .get_buffer = block_get_buffer,
396 .close = bdrv_fclose
399 static const QEMUFileOps bdrv_write_ops = {
400 .put_buffer = block_put_buffer,
401 .close = bdrv_fclose
404 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
406 if (is_writable)
407 return qemu_fopen_ops(bs, &bdrv_write_ops);
408 return qemu_fopen_ops(bs, &bdrv_read_ops);
411 QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
413 QEMUFile *f;
415 f = g_malloc0(sizeof(QEMUFile));
417 f->opaque = opaque;
418 f->ops = ops;
419 f->is_write = 0;
421 return f;
424 int qemu_file_get_error(QEMUFile *f)
426 return f->last_error;
429 static void qemu_file_set_error(QEMUFile *f, int ret)
431 f->last_error = ret;
434 /** Flushes QEMUFile buffer
437 static int qemu_fflush(QEMUFile *f)
439 int ret = 0;
441 if (!f->ops->put_buffer)
442 return 0;
444 if (f->is_write && f->buf_index > 0) {
445 ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
446 if (ret >= 0) {
447 f->buf_offset += f->buf_index;
449 f->buf_index = 0;
451 return ret;
454 static void qemu_fill_buffer(QEMUFile *f)
456 int len;
457 int pending;
459 if (!f->ops->get_buffer)
460 return;
462 if (f->is_write)
463 abort();
465 pending = f->buf_size - f->buf_index;
466 if (pending > 0) {
467 memmove(f->buf, f->buf + f->buf_index, pending);
469 f->buf_index = 0;
470 f->buf_size = pending;
472 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
473 IO_BUF_SIZE - pending);
474 if (len > 0) {
475 f->buf_size += len;
476 f->buf_offset += len;
477 } else if (len == 0) {
478 qemu_file_set_error(f, -EIO);
479 } else if (len != -EAGAIN)
480 qemu_file_set_error(f, len);
483 int qemu_get_fd(QEMUFile *f)
485 if (f->ops->get_fd) {
486 return f->ops->get_fd(f->opaque);
488 return -1;
491 /** Closes the file
493 * Returns negative error value if any error happened on previous operations or
494 * while closing the file. Returns 0 or positive number on success.
496 * The meaning of return value on success depends on the specific backend
497 * being used.
499 int qemu_fclose(QEMUFile *f)
501 int ret;
502 ret = qemu_fflush(f);
504 if (f->ops->close) {
505 int ret2 = f->ops->close(f->opaque);
506 if (ret >= 0) {
507 ret = ret2;
510 /* If any error was spotted before closing, we should report it
511 * instead of the close() return value.
513 if (f->last_error) {
514 ret = f->last_error;
516 g_free(f);
517 return ret;
520 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
522 int l;
524 if (f->last_error) {
525 return;
528 if (f->is_write == 0 && f->buf_index > 0) {
529 fprintf(stderr,
530 "Attempted to write to buffer while read buffer is not empty\n");
531 abort();
534 while (size > 0) {
535 l = IO_BUF_SIZE - f->buf_index;
536 if (l > size)
537 l = size;
538 memcpy(f->buf + f->buf_index, buf, l);
539 f->is_write = 1;
540 f->buf_index += l;
541 buf += l;
542 size -= l;
543 if (f->buf_index >= IO_BUF_SIZE) {
544 int ret = qemu_fflush(f);
545 if (ret < 0) {
546 qemu_file_set_error(f, ret);
547 break;
553 void qemu_put_byte(QEMUFile *f, int v)
555 if (f->last_error) {
556 return;
559 if (f->is_write == 0 && f->buf_index > 0) {
560 fprintf(stderr,
561 "Attempted to write to buffer while read buffer is not empty\n");
562 abort();
565 f->buf[f->buf_index++] = v;
566 f->is_write = 1;
567 if (f->buf_index >= IO_BUF_SIZE) {
568 int ret = qemu_fflush(f);
569 if (ret < 0) {
570 qemu_file_set_error(f, ret);
575 static void qemu_file_skip(QEMUFile *f, int size)
577 if (f->buf_index + size <= f->buf_size) {
578 f->buf_index += size;
582 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
584 int pending;
585 int index;
587 if (f->is_write) {
588 abort();
591 index = f->buf_index + offset;
592 pending = f->buf_size - index;
593 if (pending < size) {
594 qemu_fill_buffer(f);
595 index = f->buf_index + offset;
596 pending = f->buf_size - index;
599 if (pending <= 0) {
600 return 0;
602 if (size > pending) {
603 size = pending;
606 memcpy(buf, f->buf + index, size);
607 return size;
610 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
612 int pending = size;
613 int done = 0;
615 while (pending > 0) {
616 int res;
618 res = qemu_peek_buffer(f, buf, pending, 0);
619 if (res == 0) {
620 return done;
622 qemu_file_skip(f, res);
623 buf += res;
624 pending -= res;
625 done += res;
627 return done;
630 static int qemu_peek_byte(QEMUFile *f, int offset)
632 int index = f->buf_index + offset;
634 if (f->is_write) {
635 abort();
638 if (index >= f->buf_size) {
639 qemu_fill_buffer(f);
640 index = f->buf_index + offset;
641 if (index >= f->buf_size) {
642 return 0;
645 return f->buf[index];
648 int qemu_get_byte(QEMUFile *f)
650 int result;
652 result = qemu_peek_byte(f, 0);
653 qemu_file_skip(f, 1);
654 return result;
657 static int64_t qemu_ftell(QEMUFile *f)
659 return f->buf_offset - f->buf_size + f->buf_index;
662 int qemu_file_rate_limit(QEMUFile *f)
664 if (f->ops->rate_limit)
665 return f->ops->rate_limit(f->opaque);
667 return 0;
670 int64_t qemu_file_get_rate_limit(QEMUFile *f)
672 if (f->ops->get_rate_limit)
673 return f->ops->get_rate_limit(f->opaque);
675 return 0;
678 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
680 /* any failed or completed migration keeps its state to allow probing of
681 * migration data, but has no associated file anymore */
682 if (f && f->ops->set_rate_limit)
683 return f->ops->set_rate_limit(f->opaque, new_rate);
685 return 0;
688 void qemu_put_be16(QEMUFile *f, unsigned int v)
690 qemu_put_byte(f, v >> 8);
691 qemu_put_byte(f, v);
694 void qemu_put_be32(QEMUFile *f, unsigned int v)
696 qemu_put_byte(f, v >> 24);
697 qemu_put_byte(f, v >> 16);
698 qemu_put_byte(f, v >> 8);
699 qemu_put_byte(f, v);
702 void qemu_put_be64(QEMUFile *f, uint64_t v)
704 qemu_put_be32(f, v >> 32);
705 qemu_put_be32(f, v);
708 unsigned int qemu_get_be16(QEMUFile *f)
710 unsigned int v;
711 v = qemu_get_byte(f) << 8;
712 v |= qemu_get_byte(f);
713 return v;
716 unsigned int qemu_get_be32(QEMUFile *f)
718 unsigned int v;
719 v = qemu_get_byte(f) << 24;
720 v |= qemu_get_byte(f) << 16;
721 v |= qemu_get_byte(f) << 8;
722 v |= qemu_get_byte(f);
723 return v;
726 uint64_t qemu_get_be64(QEMUFile *f)
728 uint64_t v;
729 v = (uint64_t)qemu_get_be32(f) << 32;
730 v |= qemu_get_be32(f);
731 return v;
735 /* timer */
737 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
739 uint64_t expire_time;
741 expire_time = qemu_timer_expire_time_ns(ts);
742 qemu_put_be64(f, expire_time);
745 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
747 uint64_t expire_time;
749 expire_time = qemu_get_be64(f);
750 if (expire_time != -1) {
751 qemu_mod_timer_ns(ts, expire_time);
752 } else {
753 qemu_del_timer(ts);
758 /* bool */
760 static int get_bool(QEMUFile *f, void *pv, size_t size)
762 bool *v = pv;
763 *v = qemu_get_byte(f);
764 return 0;
767 static void put_bool(QEMUFile *f, void *pv, size_t size)
769 bool *v = pv;
770 qemu_put_byte(f, *v);
773 const VMStateInfo vmstate_info_bool = {
774 .name = "bool",
775 .get = get_bool,
776 .put = put_bool,
779 /* 8 bit int */
781 static int get_int8(QEMUFile *f, void *pv, size_t size)
783 int8_t *v = pv;
784 qemu_get_s8s(f, v);
785 return 0;
788 static void put_int8(QEMUFile *f, void *pv, size_t size)
790 int8_t *v = pv;
791 qemu_put_s8s(f, v);
794 const VMStateInfo vmstate_info_int8 = {
795 .name = "int8",
796 .get = get_int8,
797 .put = put_int8,
800 /* 16 bit int */
802 static int get_int16(QEMUFile *f, void *pv, size_t size)
804 int16_t *v = pv;
805 qemu_get_sbe16s(f, v);
806 return 0;
809 static void put_int16(QEMUFile *f, void *pv, size_t size)
811 int16_t *v = pv;
812 qemu_put_sbe16s(f, v);
815 const VMStateInfo vmstate_info_int16 = {
816 .name = "int16",
817 .get = get_int16,
818 .put = put_int16,
821 /* 32 bit int */
823 static int get_int32(QEMUFile *f, void *pv, size_t size)
825 int32_t *v = pv;
826 qemu_get_sbe32s(f, v);
827 return 0;
830 static void put_int32(QEMUFile *f, void *pv, size_t size)
832 int32_t *v = pv;
833 qemu_put_sbe32s(f, v);
836 const VMStateInfo vmstate_info_int32 = {
837 .name = "int32",
838 .get = get_int32,
839 .put = put_int32,
842 /* 32 bit int. See that the received value is the same than the one
843 in the field */
845 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
847 int32_t *v = pv;
848 int32_t v2;
849 qemu_get_sbe32s(f, &v2);
851 if (*v == v2)
852 return 0;
853 return -EINVAL;
856 const VMStateInfo vmstate_info_int32_equal = {
857 .name = "int32 equal",
858 .get = get_int32_equal,
859 .put = put_int32,
862 /* 32 bit int. See that the received value is the less or the same
863 than the one in the field */
865 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
867 int32_t *old = pv;
868 int32_t new;
869 qemu_get_sbe32s(f, &new);
871 if (*old <= new)
872 return 0;
873 return -EINVAL;
876 const VMStateInfo vmstate_info_int32_le = {
877 .name = "int32 equal",
878 .get = get_int32_le,
879 .put = put_int32,
882 /* 64 bit int */
884 static int get_int64(QEMUFile *f, void *pv, size_t size)
886 int64_t *v = pv;
887 qemu_get_sbe64s(f, v);
888 return 0;
891 static void put_int64(QEMUFile *f, void *pv, size_t size)
893 int64_t *v = pv;
894 qemu_put_sbe64s(f, v);
897 const VMStateInfo vmstate_info_int64 = {
898 .name = "int64",
899 .get = get_int64,
900 .put = put_int64,
903 /* 8 bit unsigned int */
905 static int get_uint8(QEMUFile *f, void *pv, size_t size)
907 uint8_t *v = pv;
908 qemu_get_8s(f, v);
909 return 0;
912 static void put_uint8(QEMUFile *f, void *pv, size_t size)
914 uint8_t *v = pv;
915 qemu_put_8s(f, v);
918 const VMStateInfo vmstate_info_uint8 = {
919 .name = "uint8",
920 .get = get_uint8,
921 .put = put_uint8,
924 /* 16 bit unsigned int */
926 static int get_uint16(QEMUFile *f, void *pv, size_t size)
928 uint16_t *v = pv;
929 qemu_get_be16s(f, v);
930 return 0;
933 static void put_uint16(QEMUFile *f, void *pv, size_t size)
935 uint16_t *v = pv;
936 qemu_put_be16s(f, v);
939 const VMStateInfo vmstate_info_uint16 = {
940 .name = "uint16",
941 .get = get_uint16,
942 .put = put_uint16,
945 /* 32 bit unsigned int */
947 static int get_uint32(QEMUFile *f, void *pv, size_t size)
949 uint32_t *v = pv;
950 qemu_get_be32s(f, v);
951 return 0;
954 static void put_uint32(QEMUFile *f, void *pv, size_t size)
956 uint32_t *v = pv;
957 qemu_put_be32s(f, v);
960 const VMStateInfo vmstate_info_uint32 = {
961 .name = "uint32",
962 .get = get_uint32,
963 .put = put_uint32,
966 /* 32 bit uint. See that the received value is the same than the one
967 in the field */
969 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
971 uint32_t *v = pv;
972 uint32_t v2;
973 qemu_get_be32s(f, &v2);
975 if (*v == v2) {
976 return 0;
978 return -EINVAL;
981 const VMStateInfo vmstate_info_uint32_equal = {
982 .name = "uint32 equal",
983 .get = get_uint32_equal,
984 .put = put_uint32,
987 /* 64 bit unsigned int */
989 static int get_uint64(QEMUFile *f, void *pv, size_t size)
991 uint64_t *v = pv;
992 qemu_get_be64s(f, v);
993 return 0;
996 static void put_uint64(QEMUFile *f, void *pv, size_t size)
998 uint64_t *v = pv;
999 qemu_put_be64s(f, v);
1002 const VMStateInfo vmstate_info_uint64 = {
1003 .name = "uint64",
1004 .get = get_uint64,
1005 .put = put_uint64,
1008 /* 8 bit int. See that the received value is the same than the one
1009 in the field */
1011 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1013 uint8_t *v = pv;
1014 uint8_t v2;
1015 qemu_get_8s(f, &v2);
1017 if (*v == v2)
1018 return 0;
1019 return -EINVAL;
1022 const VMStateInfo vmstate_info_uint8_equal = {
1023 .name = "uint8 equal",
1024 .get = get_uint8_equal,
1025 .put = put_uint8,
1028 /* 16 bit unsigned int int. See that the received value is the same than the one
1029 in the field */
1031 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1033 uint16_t *v = pv;
1034 uint16_t v2;
1035 qemu_get_be16s(f, &v2);
1037 if (*v == v2)
1038 return 0;
1039 return -EINVAL;
1042 const VMStateInfo vmstate_info_uint16_equal = {
1043 .name = "uint16 equal",
1044 .get = get_uint16_equal,
1045 .put = put_uint16,
1048 /* timers */
1050 static int get_timer(QEMUFile *f, void *pv, size_t size)
1052 QEMUTimer *v = pv;
1053 qemu_get_timer(f, v);
1054 return 0;
1057 static void put_timer(QEMUFile *f, void *pv, size_t size)
1059 QEMUTimer *v = pv;
1060 qemu_put_timer(f, v);
1063 const VMStateInfo vmstate_info_timer = {
1064 .name = "timer",
1065 .get = get_timer,
1066 .put = put_timer,
1069 /* uint8_t buffers */
1071 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1073 uint8_t *v = pv;
1074 qemu_get_buffer(f, v, size);
1075 return 0;
1078 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1080 uint8_t *v = pv;
1081 qemu_put_buffer(f, v, size);
1084 const VMStateInfo vmstate_info_buffer = {
1085 .name = "buffer",
1086 .get = get_buffer,
1087 .put = put_buffer,
1090 /* unused buffers: space that was used for some fields that are
1091 not useful anymore */
1093 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1095 uint8_t buf[1024];
1096 int block_len;
1098 while (size > 0) {
1099 block_len = MIN(sizeof(buf), size);
1100 size -= block_len;
1101 qemu_get_buffer(f, buf, block_len);
1103 return 0;
1106 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1108 static const uint8_t buf[1024];
1109 int block_len;
1111 while (size > 0) {
1112 block_len = MIN(sizeof(buf), size);
1113 size -= block_len;
1114 qemu_put_buffer(f, buf, block_len);
1118 const VMStateInfo vmstate_info_unused_buffer = {
1119 .name = "unused_buffer",
1120 .get = get_unused_buffer,
1121 .put = put_unused_buffer,
1124 /* bitmaps (as defined by bitmap.h). Note that size here is the size
1125 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1126 * bit words with the bits in big endian order. The in-memory format
1127 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1129 /* This is the number of 64 bit words sent over the wire */
1130 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1131 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1133 unsigned long *bmp = pv;
1134 int i, idx = 0;
1135 for (i = 0; i < BITS_TO_U64S(size); i++) {
1136 uint64_t w = qemu_get_be64(f);
1137 bmp[idx++] = w;
1138 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1139 bmp[idx++] = w >> 32;
1142 return 0;
1145 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1147 unsigned long *bmp = pv;
1148 int i, idx = 0;
1149 for (i = 0; i < BITS_TO_U64S(size); i++) {
1150 uint64_t w = bmp[idx++];
1151 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1152 w |= ((uint64_t)bmp[idx++]) << 32;
1154 qemu_put_be64(f, w);
1158 const VMStateInfo vmstate_info_bitmap = {
1159 .name = "bitmap",
1160 .get = get_bitmap,
1161 .put = put_bitmap,
1164 typedef struct CompatEntry {
1165 char idstr[256];
1166 int instance_id;
1167 } CompatEntry;
1169 typedef struct SaveStateEntry {
1170 QTAILQ_ENTRY(SaveStateEntry) entry;
1171 char idstr[256];
1172 int instance_id;
1173 int alias_id;
1174 int version_id;
1175 int section_id;
1176 SaveVMHandlers *ops;
1177 const VMStateDescription *vmsd;
1178 void *opaque;
1179 CompatEntry *compat;
1180 int no_migrate;
1181 int is_ram;
1182 } SaveStateEntry;
1185 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1186 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1187 static int global_section_id;
1189 static int calculate_new_instance_id(const char *idstr)
1191 SaveStateEntry *se;
1192 int instance_id = 0;
1194 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1195 if (strcmp(idstr, se->idstr) == 0
1196 && instance_id <= se->instance_id) {
1197 instance_id = se->instance_id + 1;
1200 return instance_id;
1203 static int calculate_compat_instance_id(const char *idstr)
1205 SaveStateEntry *se;
1206 int instance_id = 0;
1208 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1209 if (!se->compat)
1210 continue;
1212 if (strcmp(idstr, se->compat->idstr) == 0
1213 && instance_id <= se->compat->instance_id) {
1214 instance_id = se->compat->instance_id + 1;
1217 return instance_id;
1220 /* TODO: Individual devices generally have very little idea about the rest
1221 of the system, so instance_id should be removed/replaced.
1222 Meanwhile pass -1 as instance_id if you do not already have a clearly
1223 distinguishing id for all instances of your device class. */
1224 int register_savevm_live(DeviceState *dev,
1225 const char *idstr,
1226 int instance_id,
1227 int version_id,
1228 SaveVMHandlers *ops,
1229 void *opaque)
1231 SaveStateEntry *se;
1233 se = g_malloc0(sizeof(SaveStateEntry));
1234 se->version_id = version_id;
1235 se->section_id = global_section_id++;
1236 se->ops = ops;
1237 se->opaque = opaque;
1238 se->vmsd = NULL;
1239 se->no_migrate = 0;
1240 /* if this is a live_savem then set is_ram */
1241 if (ops->save_live_setup != NULL) {
1242 se->is_ram = 1;
1245 if (dev) {
1246 char *id = qdev_get_dev_path(dev);
1247 if (id) {
1248 pstrcpy(se->idstr, sizeof(se->idstr), id);
1249 pstrcat(se->idstr, sizeof(se->idstr), "/");
1250 g_free(id);
1252 se->compat = g_malloc0(sizeof(CompatEntry));
1253 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1254 se->compat->instance_id = instance_id == -1 ?
1255 calculate_compat_instance_id(idstr) : instance_id;
1256 instance_id = -1;
1259 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1261 if (instance_id == -1) {
1262 se->instance_id = calculate_new_instance_id(se->idstr);
1263 } else {
1264 se->instance_id = instance_id;
1266 assert(!se->compat || se->instance_id == 0);
1267 /* add at the end of list */
1268 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1269 return 0;
1272 int register_savevm(DeviceState *dev,
1273 const char *idstr,
1274 int instance_id,
1275 int version_id,
1276 SaveStateHandler *save_state,
1277 LoadStateHandler *load_state,
1278 void *opaque)
1280 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1281 ops->save_state = save_state;
1282 ops->load_state = load_state;
1283 return register_savevm_live(dev, idstr, instance_id, version_id,
1284 ops, opaque);
1287 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1289 SaveStateEntry *se, *new_se;
1290 char id[256] = "";
1292 if (dev) {
1293 char *path = qdev_get_dev_path(dev);
1294 if (path) {
1295 pstrcpy(id, sizeof(id), path);
1296 pstrcat(id, sizeof(id), "/");
1297 g_free(path);
1300 pstrcat(id, sizeof(id), idstr);
1302 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1303 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1304 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1305 if (se->compat) {
1306 g_free(se->compat);
1308 g_free(se->ops);
1309 g_free(se);
1314 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1315 const VMStateDescription *vmsd,
1316 void *opaque, int alias_id,
1317 int required_for_version)
1319 SaveStateEntry *se;
1321 /* If this triggers, alias support can be dropped for the vmsd. */
1322 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1324 se = g_malloc0(sizeof(SaveStateEntry));
1325 se->version_id = vmsd->version_id;
1326 se->section_id = global_section_id++;
1327 se->opaque = opaque;
1328 se->vmsd = vmsd;
1329 se->alias_id = alias_id;
1330 se->no_migrate = vmsd->unmigratable;
1332 if (dev) {
1333 char *id = qdev_get_dev_path(dev);
1334 if (id) {
1335 pstrcpy(se->idstr, sizeof(se->idstr), id);
1336 pstrcat(se->idstr, sizeof(se->idstr), "/");
1337 g_free(id);
1339 se->compat = g_malloc0(sizeof(CompatEntry));
1340 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1341 se->compat->instance_id = instance_id == -1 ?
1342 calculate_compat_instance_id(vmsd->name) : instance_id;
1343 instance_id = -1;
1346 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1348 if (instance_id == -1) {
1349 se->instance_id = calculate_new_instance_id(se->idstr);
1350 } else {
1351 se->instance_id = instance_id;
1353 assert(!se->compat || se->instance_id == 0);
1354 /* add at the end of list */
1355 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1356 return 0;
1359 int vmstate_register(DeviceState *dev, int instance_id,
1360 const VMStateDescription *vmsd, void *opaque)
1362 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1363 opaque, -1, 0);
1366 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1367 void *opaque)
1369 SaveStateEntry *se, *new_se;
1371 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1372 if (se->vmsd == vmsd && se->opaque == opaque) {
1373 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1374 if (se->compat) {
1375 g_free(se->compat);
1377 g_free(se);
1382 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1383 void *opaque);
1384 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1385 void *opaque);
1387 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1388 void *opaque, int version_id)
1390 VMStateField *field = vmsd->fields;
1391 int ret;
1393 if (version_id > vmsd->version_id) {
1394 return -EINVAL;
1396 if (version_id < vmsd->minimum_version_id_old) {
1397 return -EINVAL;
1399 if (version_id < vmsd->minimum_version_id) {
1400 return vmsd->load_state_old(f, opaque, version_id);
1402 if (vmsd->pre_load) {
1403 int ret = vmsd->pre_load(opaque);
1404 if (ret)
1405 return ret;
1407 while(field->name) {
1408 if ((field->field_exists &&
1409 field->field_exists(opaque, version_id)) ||
1410 (!field->field_exists &&
1411 field->version_id <= version_id)) {
1412 void *base_addr = opaque + field->offset;
1413 int i, n_elems = 1;
1414 int size = field->size;
1416 if (field->flags & VMS_VBUFFER) {
1417 size = *(int32_t *)(opaque+field->size_offset);
1418 if (field->flags & VMS_MULTIPLY) {
1419 size *= field->size;
1422 if (field->flags & VMS_ARRAY) {
1423 n_elems = field->num;
1424 } else if (field->flags & VMS_VARRAY_INT32) {
1425 n_elems = *(int32_t *)(opaque+field->num_offset);
1426 } else if (field->flags & VMS_VARRAY_UINT32) {
1427 n_elems = *(uint32_t *)(opaque+field->num_offset);
1428 } else if (field->flags & VMS_VARRAY_UINT16) {
1429 n_elems = *(uint16_t *)(opaque+field->num_offset);
1430 } else if (field->flags & VMS_VARRAY_UINT8) {
1431 n_elems = *(uint8_t *)(opaque+field->num_offset);
1433 if (field->flags & VMS_POINTER) {
1434 base_addr = *(void **)base_addr + field->start;
1436 for (i = 0; i < n_elems; i++) {
1437 void *addr = base_addr + size * i;
1439 if (field->flags & VMS_ARRAY_OF_POINTER) {
1440 addr = *(void **)addr;
1442 if (field->flags & VMS_STRUCT) {
1443 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1444 } else {
1445 ret = field->info->get(f, addr, size);
1448 if (ret < 0) {
1449 return ret;
1453 field++;
1455 ret = vmstate_subsection_load(f, vmsd, opaque);
1456 if (ret != 0) {
1457 return ret;
1459 if (vmsd->post_load) {
1460 return vmsd->post_load(opaque, version_id);
1462 return 0;
1465 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1466 void *opaque)
1468 VMStateField *field = vmsd->fields;
1470 if (vmsd->pre_save) {
1471 vmsd->pre_save(opaque);
1473 while(field->name) {
1474 if (!field->field_exists ||
1475 field->field_exists(opaque, vmsd->version_id)) {
1476 void *base_addr = opaque + field->offset;
1477 int i, n_elems = 1;
1478 int size = field->size;
1480 if (field->flags & VMS_VBUFFER) {
1481 size = *(int32_t *)(opaque+field->size_offset);
1482 if (field->flags & VMS_MULTIPLY) {
1483 size *= field->size;
1486 if (field->flags & VMS_ARRAY) {
1487 n_elems = field->num;
1488 } else if (field->flags & VMS_VARRAY_INT32) {
1489 n_elems = *(int32_t *)(opaque+field->num_offset);
1490 } else if (field->flags & VMS_VARRAY_UINT32) {
1491 n_elems = *(uint32_t *)(opaque+field->num_offset);
1492 } else if (field->flags & VMS_VARRAY_UINT16) {
1493 n_elems = *(uint16_t *)(opaque+field->num_offset);
1494 } else if (field->flags & VMS_VARRAY_UINT8) {
1495 n_elems = *(uint8_t *)(opaque+field->num_offset);
1497 if (field->flags & VMS_POINTER) {
1498 base_addr = *(void **)base_addr + field->start;
1500 for (i = 0; i < n_elems; i++) {
1501 void *addr = base_addr + size * i;
1503 if (field->flags & VMS_ARRAY_OF_POINTER) {
1504 addr = *(void **)addr;
1506 if (field->flags & VMS_STRUCT) {
1507 vmstate_save_state(f, field->vmsd, addr);
1508 } else {
1509 field->info->put(f, addr, size);
1513 field++;
1515 vmstate_subsection_save(f, vmsd, opaque);
1518 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1520 if (!se->vmsd) { /* Old style */
1521 return se->ops->load_state(f, se->opaque, version_id);
1523 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1526 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1528 if (!se->vmsd) { /* Old style */
1529 se->ops->save_state(f, se->opaque);
1530 return;
1532 vmstate_save_state(f,se->vmsd, se->opaque);
1535 #define QEMU_VM_FILE_MAGIC 0x5145564d
1536 #define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1537 #define QEMU_VM_FILE_VERSION 0x00000003
1539 #define QEMU_VM_EOF 0x00
1540 #define QEMU_VM_SECTION_START 0x01
1541 #define QEMU_VM_SECTION_PART 0x02
1542 #define QEMU_VM_SECTION_END 0x03
1543 #define QEMU_VM_SECTION_FULL 0x04
1544 #define QEMU_VM_SUBSECTION 0x05
1546 bool qemu_savevm_state_blocked(Error **errp)
1548 SaveStateEntry *se;
1550 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1551 if (se->no_migrate) {
1552 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1553 return true;
1556 return false;
1559 int qemu_savevm_state_begin(QEMUFile *f,
1560 const MigrationParams *params)
1562 SaveStateEntry *se;
1563 int ret;
1565 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1566 if (!se->ops || !se->ops->set_params) {
1567 continue;
1569 se->ops->set_params(params, se->opaque);
1572 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1573 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1575 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1576 int len;
1578 if (!se->ops || !se->ops->save_live_setup) {
1579 continue;
1581 if (se->ops && se->ops->is_active) {
1582 if (!se->ops->is_active(se->opaque)) {
1583 continue;
1586 /* Section type */
1587 qemu_put_byte(f, QEMU_VM_SECTION_START);
1588 qemu_put_be32(f, se->section_id);
1590 /* ID string */
1591 len = strlen(se->idstr);
1592 qemu_put_byte(f, len);
1593 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1595 qemu_put_be32(f, se->instance_id);
1596 qemu_put_be32(f, se->version_id);
1598 ret = se->ops->save_live_setup(f, se->opaque);
1599 if (ret < 0) {
1600 qemu_savevm_state_cancel(f);
1601 return ret;
1604 ret = qemu_file_get_error(f);
1605 if (ret != 0) {
1606 qemu_savevm_state_cancel(f);
1609 return ret;
1614 * this function has three return values:
1615 * negative: there was one error, and we have -errno.
1616 * 0 : We haven't finished, caller have to go again
1617 * 1 : We have finished, we can go to complete phase
1619 int qemu_savevm_state_iterate(QEMUFile *f)
1621 SaveStateEntry *se;
1622 int ret = 1;
1624 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1625 if (!se->ops || !se->ops->save_live_iterate) {
1626 continue;
1628 if (se->ops && se->ops->is_active) {
1629 if (!se->ops->is_active(se->opaque)) {
1630 continue;
1633 if (qemu_file_rate_limit(f)) {
1634 return 0;
1636 trace_savevm_section_start();
1637 /* Section type */
1638 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1639 qemu_put_be32(f, se->section_id);
1641 ret = se->ops->save_live_iterate(f, se->opaque);
1642 trace_savevm_section_end(se->section_id);
1644 if (ret <= 0) {
1645 /* Do not proceed to the next vmstate before this one reported
1646 completion of the current stage. This serializes the migration
1647 and reduces the probability that a faster changing state is
1648 synchronized over and over again. */
1649 break;
1652 if (ret != 0) {
1653 return ret;
1655 ret = qemu_file_get_error(f);
1656 if (ret != 0) {
1657 qemu_savevm_state_cancel(f);
1659 return ret;
1662 int qemu_savevm_state_complete(QEMUFile *f)
1664 SaveStateEntry *se;
1665 int ret;
1667 cpu_synchronize_all_states();
1669 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1670 if (!se->ops || !se->ops->save_live_complete) {
1671 continue;
1673 if (se->ops && se->ops->is_active) {
1674 if (!se->ops->is_active(se->opaque)) {
1675 continue;
1678 trace_savevm_section_start();
1679 /* Section type */
1680 qemu_put_byte(f, QEMU_VM_SECTION_END);
1681 qemu_put_be32(f, se->section_id);
1683 ret = se->ops->save_live_complete(f, se->opaque);
1684 trace_savevm_section_end(se->section_id);
1685 if (ret < 0) {
1686 return ret;
1690 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1691 int len;
1693 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1694 continue;
1696 trace_savevm_section_start();
1697 /* Section type */
1698 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1699 qemu_put_be32(f, se->section_id);
1701 /* ID string */
1702 len = strlen(se->idstr);
1703 qemu_put_byte(f, len);
1704 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1706 qemu_put_be32(f, se->instance_id);
1707 qemu_put_be32(f, se->version_id);
1709 vmstate_save(f, se);
1710 trace_savevm_section_end(se->section_id);
1713 qemu_put_byte(f, QEMU_VM_EOF);
1715 return qemu_file_get_error(f);
1718 uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1720 SaveStateEntry *se;
1721 uint64_t ret = 0;
1723 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1724 if (!se->ops || !se->ops->save_live_pending) {
1725 continue;
1727 if (se->ops && se->ops->is_active) {
1728 if (!se->ops->is_active(se->opaque)) {
1729 continue;
1732 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1734 return ret;
1737 void qemu_savevm_state_cancel(QEMUFile *f)
1739 SaveStateEntry *se;
1741 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1742 if (se->ops && se->ops->cancel) {
1743 se->ops->cancel(se->opaque);
1748 static int qemu_savevm_state(QEMUFile *f)
1750 int ret;
1751 MigrationParams params = {
1752 .blk = 0,
1753 .shared = 0
1756 if (qemu_savevm_state_blocked(NULL)) {
1757 ret = -EINVAL;
1758 goto out;
1761 ret = qemu_savevm_state_begin(f, &params);
1762 if (ret < 0)
1763 goto out;
1765 do {
1766 ret = qemu_savevm_state_iterate(f);
1767 if (ret < 0)
1768 goto out;
1769 } while (ret == 0);
1771 ret = qemu_savevm_state_complete(f);
1773 out:
1774 if (ret == 0) {
1775 ret = qemu_file_get_error(f);
1778 return ret;
1781 static int qemu_save_device_state(QEMUFile *f)
1783 SaveStateEntry *se;
1785 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1786 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1788 cpu_synchronize_all_states();
1790 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1791 int len;
1793 if (se->is_ram) {
1794 continue;
1796 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1797 continue;
1800 /* Section type */
1801 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1802 qemu_put_be32(f, se->section_id);
1804 /* ID string */
1805 len = strlen(se->idstr);
1806 qemu_put_byte(f, len);
1807 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1809 qemu_put_be32(f, se->instance_id);
1810 qemu_put_be32(f, se->version_id);
1812 vmstate_save(f, se);
1815 qemu_put_byte(f, QEMU_VM_EOF);
1817 return qemu_file_get_error(f);
1820 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1822 SaveStateEntry *se;
1824 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1825 if (!strcmp(se->idstr, idstr) &&
1826 (instance_id == se->instance_id ||
1827 instance_id == se->alias_id))
1828 return se;
1829 /* Migrating from an older version? */
1830 if (strstr(se->idstr, idstr) && se->compat) {
1831 if (!strcmp(se->compat->idstr, idstr) &&
1832 (instance_id == se->compat->instance_id ||
1833 instance_id == se->alias_id))
1834 return se;
1837 return NULL;
1840 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1842 while(sub && sub->needed) {
1843 if (strcmp(idstr, sub->vmsd->name) == 0) {
1844 return sub->vmsd;
1846 sub++;
1848 return NULL;
1851 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1852 void *opaque)
1854 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
1855 char idstr[256];
1856 int ret;
1857 uint8_t version_id, len, size;
1858 const VMStateDescription *sub_vmsd;
1860 len = qemu_peek_byte(f, 1);
1861 if (len < strlen(vmsd->name) + 1) {
1862 /* subsection name has be be "section_name/a" */
1863 return 0;
1865 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1866 if (size != len) {
1867 return 0;
1869 idstr[size] = 0;
1871 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1872 /* it don't have a valid subsection name */
1873 return 0;
1875 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
1876 if (sub_vmsd == NULL) {
1877 return -ENOENT;
1879 qemu_file_skip(f, 1); /* subsection */
1880 qemu_file_skip(f, 1); /* len */
1881 qemu_file_skip(f, len); /* idstr */
1882 version_id = qemu_get_be32(f);
1884 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1885 if (ret) {
1886 return ret;
1889 return 0;
1892 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1893 void *opaque)
1895 const VMStateSubsection *sub = vmsd->subsections;
1897 while (sub && sub->needed) {
1898 if (sub->needed(opaque)) {
1899 const VMStateDescription *vmsd = sub->vmsd;
1900 uint8_t len;
1902 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1903 len = strlen(vmsd->name);
1904 qemu_put_byte(f, len);
1905 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1906 qemu_put_be32(f, vmsd->version_id);
1907 vmstate_save_state(f, vmsd, opaque);
1909 sub++;
1913 typedef struct LoadStateEntry {
1914 QLIST_ENTRY(LoadStateEntry) entry;
1915 SaveStateEntry *se;
1916 int section_id;
1917 int version_id;
1918 } LoadStateEntry;
1920 int qemu_loadvm_state(QEMUFile *f)
1922 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1923 QLIST_HEAD_INITIALIZER(loadvm_handlers);
1924 LoadStateEntry *le, *new_le;
1925 uint8_t section_type;
1926 unsigned int v;
1927 int ret;
1929 if (qemu_savevm_state_blocked(NULL)) {
1930 return -EINVAL;
1933 v = qemu_get_be32(f);
1934 if (v != QEMU_VM_FILE_MAGIC)
1935 return -EINVAL;
1937 v = qemu_get_be32(f);
1938 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1939 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1940 return -ENOTSUP;
1942 if (v != QEMU_VM_FILE_VERSION)
1943 return -ENOTSUP;
1945 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1946 uint32_t instance_id, version_id, section_id;
1947 SaveStateEntry *se;
1948 char idstr[257];
1949 int len;
1951 switch (section_type) {
1952 case QEMU_VM_SECTION_START:
1953 case QEMU_VM_SECTION_FULL:
1954 /* Read section start */
1955 section_id = qemu_get_be32(f);
1956 len = qemu_get_byte(f);
1957 qemu_get_buffer(f, (uint8_t *)idstr, len);
1958 idstr[len] = 0;
1959 instance_id = qemu_get_be32(f);
1960 version_id = qemu_get_be32(f);
1962 /* Find savevm section */
1963 se = find_se(idstr, instance_id);
1964 if (se == NULL) {
1965 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1966 ret = -EINVAL;
1967 goto out;
1970 /* Validate version */
1971 if (version_id > se->version_id) {
1972 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1973 version_id, idstr, se->version_id);
1974 ret = -EINVAL;
1975 goto out;
1978 /* Add entry */
1979 le = g_malloc0(sizeof(*le));
1981 le->se = se;
1982 le->section_id = section_id;
1983 le->version_id = version_id;
1984 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1986 ret = vmstate_load(f, le->se, le->version_id);
1987 if (ret < 0) {
1988 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1989 instance_id, idstr);
1990 goto out;
1992 break;
1993 case QEMU_VM_SECTION_PART:
1994 case QEMU_VM_SECTION_END:
1995 section_id = qemu_get_be32(f);
1997 QLIST_FOREACH(le, &loadvm_handlers, entry) {
1998 if (le->section_id == section_id) {
1999 break;
2002 if (le == NULL) {
2003 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2004 ret = -EINVAL;
2005 goto out;
2008 ret = vmstate_load(f, le->se, le->version_id);
2009 if (ret < 0) {
2010 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2011 section_id);
2012 goto out;
2014 break;
2015 default:
2016 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2017 ret = -EINVAL;
2018 goto out;
2022 cpu_synchronize_all_post_init();
2024 ret = 0;
2026 out:
2027 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2028 QLIST_REMOVE(le, entry);
2029 g_free(le);
2032 if (ret == 0) {
2033 ret = qemu_file_get_error(f);
2036 return ret;
2039 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2040 const char *name)
2042 QEMUSnapshotInfo *sn_tab, *sn;
2043 int nb_sns, i, ret;
2045 ret = -ENOENT;
2046 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2047 if (nb_sns < 0)
2048 return ret;
2049 for(i = 0; i < nb_sns; i++) {
2050 sn = &sn_tab[i];
2051 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2052 *sn_info = *sn;
2053 ret = 0;
2054 break;
2057 g_free(sn_tab);
2058 return ret;
2062 * Deletes snapshots of a given name in all opened images.
2064 static int del_existing_snapshots(Monitor *mon, const char *name)
2066 BlockDriverState *bs;
2067 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2068 int ret;
2070 bs = NULL;
2071 while ((bs = bdrv_next(bs))) {
2072 if (bdrv_can_snapshot(bs) &&
2073 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2075 ret = bdrv_snapshot_delete(bs, name);
2076 if (ret < 0) {
2077 monitor_printf(mon,
2078 "Error while deleting snapshot on '%s'\n",
2079 bdrv_get_device_name(bs));
2080 return -1;
2085 return 0;
2088 void do_savevm(Monitor *mon, const QDict *qdict)
2090 BlockDriverState *bs, *bs1;
2091 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2092 int ret;
2093 QEMUFile *f;
2094 int saved_vm_running;
2095 uint64_t vm_state_size;
2096 #ifdef _WIN32
2097 struct _timeb tb;
2098 struct tm *ptm;
2099 #else
2100 struct timeval tv;
2101 struct tm tm;
2102 #endif
2103 const char *name = qdict_get_try_str(qdict, "name");
2105 /* Verify if there is a device that doesn't support snapshots and is writable */
2106 bs = NULL;
2107 while ((bs = bdrv_next(bs))) {
2109 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2110 continue;
2113 if (!bdrv_can_snapshot(bs)) {
2114 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2115 bdrv_get_device_name(bs));
2116 return;
2120 bs = bdrv_snapshots();
2121 if (!bs) {
2122 monitor_printf(mon, "No block device can accept snapshots\n");
2123 return;
2126 saved_vm_running = runstate_is_running();
2127 vm_stop(RUN_STATE_SAVE_VM);
2129 memset(sn, 0, sizeof(*sn));
2131 /* fill auxiliary fields */
2132 #ifdef _WIN32
2133 _ftime(&tb);
2134 sn->date_sec = tb.time;
2135 sn->date_nsec = tb.millitm * 1000000;
2136 #else
2137 gettimeofday(&tv, NULL);
2138 sn->date_sec = tv.tv_sec;
2139 sn->date_nsec = tv.tv_usec * 1000;
2140 #endif
2141 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2143 if (name) {
2144 ret = bdrv_snapshot_find(bs, old_sn, name);
2145 if (ret >= 0) {
2146 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2147 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2148 } else {
2149 pstrcpy(sn->name, sizeof(sn->name), name);
2151 } else {
2152 #ifdef _WIN32
2153 time_t t = tb.time;
2154 ptm = localtime(&t);
2155 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2156 #else
2157 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2158 localtime_r((const time_t *)&tv.tv_sec, &tm);
2159 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2160 #endif
2163 /* Delete old snapshots of the same name */
2164 if (name && del_existing_snapshots(mon, name) < 0) {
2165 goto the_end;
2168 /* save the VM state */
2169 f = qemu_fopen_bdrv(bs, 1);
2170 if (!f) {
2171 monitor_printf(mon, "Could not open VM state file\n");
2172 goto the_end;
2174 ret = qemu_savevm_state(f);
2175 vm_state_size = qemu_ftell(f);
2176 qemu_fclose(f);
2177 if (ret < 0) {
2178 monitor_printf(mon, "Error %d while writing VM\n", ret);
2179 goto the_end;
2182 /* create the snapshots */
2184 bs1 = NULL;
2185 while ((bs1 = bdrv_next(bs1))) {
2186 if (bdrv_can_snapshot(bs1)) {
2187 /* Write VM state size only to the image that contains the state */
2188 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2189 ret = bdrv_snapshot_create(bs1, sn);
2190 if (ret < 0) {
2191 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2192 bdrv_get_device_name(bs1));
2197 the_end:
2198 if (saved_vm_running)
2199 vm_start();
2202 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2204 QEMUFile *f;
2205 int saved_vm_running;
2206 int ret;
2208 saved_vm_running = runstate_is_running();
2209 vm_stop(RUN_STATE_SAVE_VM);
2211 f = qemu_fopen(filename, "wb");
2212 if (!f) {
2213 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2214 goto the_end;
2216 ret = qemu_save_device_state(f);
2217 qemu_fclose(f);
2218 if (ret < 0) {
2219 error_set(errp, QERR_IO_ERROR);
2222 the_end:
2223 if (saved_vm_running)
2224 vm_start();
2227 int load_vmstate(const char *name)
2229 BlockDriverState *bs, *bs_vm_state;
2230 QEMUSnapshotInfo sn;
2231 QEMUFile *f;
2232 int ret;
2234 bs_vm_state = bdrv_snapshots();
2235 if (!bs_vm_state) {
2236 error_report("No block device supports snapshots");
2237 return -ENOTSUP;
2240 /* Don't even try to load empty VM states */
2241 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2242 if (ret < 0) {
2243 return ret;
2244 } else if (sn.vm_state_size == 0) {
2245 error_report("This is a disk-only snapshot. Revert to it offline "
2246 "using qemu-img.");
2247 return -EINVAL;
2250 /* Verify if there is any device that doesn't support snapshots and is
2251 writable and check if the requested snapshot is available too. */
2252 bs = NULL;
2253 while ((bs = bdrv_next(bs))) {
2255 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2256 continue;
2259 if (!bdrv_can_snapshot(bs)) {
2260 error_report("Device '%s' is writable but does not support snapshots.",
2261 bdrv_get_device_name(bs));
2262 return -ENOTSUP;
2265 ret = bdrv_snapshot_find(bs, &sn, name);
2266 if (ret < 0) {
2267 error_report("Device '%s' does not have the requested snapshot '%s'",
2268 bdrv_get_device_name(bs), name);
2269 return ret;
2273 /* Flush all IO requests so they don't interfere with the new state. */
2274 bdrv_drain_all();
2276 bs = NULL;
2277 while ((bs = bdrv_next(bs))) {
2278 if (bdrv_can_snapshot(bs)) {
2279 ret = bdrv_snapshot_goto(bs, name);
2280 if (ret < 0) {
2281 error_report("Error %d while activating snapshot '%s' on '%s'",
2282 ret, name, bdrv_get_device_name(bs));
2283 return ret;
2288 /* restore the VM state */
2289 f = qemu_fopen_bdrv(bs_vm_state, 0);
2290 if (!f) {
2291 error_report("Could not open VM state file");
2292 return -EINVAL;
2295 qemu_system_reset(VMRESET_SILENT);
2296 ret = qemu_loadvm_state(f);
2298 qemu_fclose(f);
2299 if (ret < 0) {
2300 error_report("Error %d while loading VM state", ret);
2301 return ret;
2304 return 0;
2307 void do_delvm(Monitor *mon, const QDict *qdict)
2309 BlockDriverState *bs, *bs1;
2310 int ret;
2311 const char *name = qdict_get_str(qdict, "name");
2313 bs = bdrv_snapshots();
2314 if (!bs) {
2315 monitor_printf(mon, "No block device supports snapshots\n");
2316 return;
2319 bs1 = NULL;
2320 while ((bs1 = bdrv_next(bs1))) {
2321 if (bdrv_can_snapshot(bs1)) {
2322 ret = bdrv_snapshot_delete(bs1, name);
2323 if (ret < 0) {
2324 if (ret == -ENOTSUP)
2325 monitor_printf(mon,
2326 "Snapshots not supported on device '%s'\n",
2327 bdrv_get_device_name(bs1));
2328 else
2329 monitor_printf(mon, "Error %d while deleting snapshot on "
2330 "'%s'\n", ret, bdrv_get_device_name(bs1));
2336 void do_info_snapshots(Monitor *mon)
2338 BlockDriverState *bs, *bs1;
2339 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2340 int nb_sns, i, ret, available;
2341 int total;
2342 int *available_snapshots;
2343 char buf[256];
2345 bs = bdrv_snapshots();
2346 if (!bs) {
2347 monitor_printf(mon, "No available block device supports snapshots\n");
2348 return;
2351 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2352 if (nb_sns < 0) {
2353 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2354 return;
2357 if (nb_sns == 0) {
2358 monitor_printf(mon, "There is no snapshot available.\n");
2359 return;
2362 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2363 total = 0;
2364 for (i = 0; i < nb_sns; i++) {
2365 sn = &sn_tab[i];
2366 available = 1;
2367 bs1 = NULL;
2369 while ((bs1 = bdrv_next(bs1))) {
2370 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2371 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2372 if (ret < 0) {
2373 available = 0;
2374 break;
2379 if (available) {
2380 available_snapshots[total] = i;
2381 total++;
2385 if (total > 0) {
2386 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2387 for (i = 0; i < total; i++) {
2388 sn = &sn_tab[available_snapshots[i]];
2389 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2391 } else {
2392 monitor_printf(mon, "There is no suitable snapshot available\n");
2395 g_free(sn_tab);
2396 g_free(available_snapshots);
2400 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2402 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2403 memory_region_name(mr), dev);
2406 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2408 /* Nothing do to while the implementation is in RAMBlock */
2411 void vmstate_register_ram_global(MemoryRegion *mr)
2413 vmstate_register_ram(mr, NULL);
2417 page = zrun nzrun
2418 | zrun nzrun page
2420 zrun = length
2422 nzrun = length byte...
2424 length = uleb128 encoded integer
2426 int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2427 uint8_t *dst, int dlen)
2429 uint32_t zrun_len = 0, nzrun_len = 0;
2430 int d = 0, i = 0;
2431 long res, xor;
2432 uint8_t *nzrun_start = NULL;
2434 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2435 sizeof(long)));
2437 while (i < slen) {
2438 /* overflow */
2439 if (d + 2 > dlen) {
2440 return -1;
2443 /* not aligned to sizeof(long) */
2444 res = (slen - i) % sizeof(long);
2445 while (res && old_buf[i] == new_buf[i]) {
2446 zrun_len++;
2447 i++;
2448 res--;
2451 /* word at a time for speed */
2452 if (!res) {
2453 while (i < slen &&
2454 (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2455 i += sizeof(long);
2456 zrun_len += sizeof(long);
2459 /* go over the rest */
2460 while (i < slen && old_buf[i] == new_buf[i]) {
2461 zrun_len++;
2462 i++;
2466 /* buffer unchanged */
2467 if (zrun_len == slen) {
2468 return 0;
2471 /* skip last zero run */
2472 if (i == slen) {
2473 return d;
2476 d += uleb128_encode_small(dst + d, zrun_len);
2478 zrun_len = 0;
2479 nzrun_start = new_buf + i;
2481 /* overflow */
2482 if (d + 2 > dlen) {
2483 return -1;
2485 /* not aligned to sizeof(long) */
2486 res = (slen - i) % sizeof(long);
2487 while (res && old_buf[i] != new_buf[i]) {
2488 i++;
2489 nzrun_len++;
2490 res--;
2493 /* word at a time for speed, use of 32-bit long okay */
2494 if (!res) {
2495 /* truncation to 32-bit long okay */
2496 long mask = (long)0x0101010101010101ULL;
2497 while (i < slen) {
2498 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2499 if ((xor - mask) & ~xor & (mask << 7)) {
2500 /* found the end of an nzrun within the current long */
2501 while (old_buf[i] != new_buf[i]) {
2502 nzrun_len++;
2503 i++;
2505 break;
2506 } else {
2507 i += sizeof(long);
2508 nzrun_len += sizeof(long);
2513 d += uleb128_encode_small(dst + d, nzrun_len);
2514 /* overflow */
2515 if (d + nzrun_len > dlen) {
2516 return -1;
2518 memcpy(dst + d, nzrun_start, nzrun_len);
2519 d += nzrun_len;
2520 nzrun_len = 0;
2523 return d;
2526 int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2528 int i = 0, d = 0;
2529 int ret;
2530 uint32_t count = 0;
2532 while (i < slen) {
2534 /* zrun */
2535 if ((slen - i) < 2) {
2536 return -1;
2539 ret = uleb128_decode_small(src + i, &count);
2540 if (ret < 0 || (i && !count)) {
2541 return -1;
2543 i += ret;
2544 d += count;
2546 /* overflow */
2547 if (d > dlen) {
2548 return -1;
2551 /* nzrun */
2552 if ((slen - i) < 2) {
2553 return -1;
2556 ret = uleb128_decode_small(src + i, &count);
2557 if (ret < 0 || !count) {
2558 return -1;
2560 i += ret;
2562 /* overflow */
2563 if (d + count > dlen || i + count > slen) {
2564 return -1;
2567 memcpy(dst + d, src + i, count);
2568 d += count;
2569 i += count;
2572 return d;