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