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