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