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