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