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