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