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