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