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