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