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