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