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