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