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