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