]> git.proxmox.com Git - qemu.git/blame - savevm.c
update VERSION for 1.1.2
[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{
a672b469
AL
403 return 0;
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
7685ee6a
AW
1251 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1252 char *id = dev->parent_bus->info->get_dev_path(dev);
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
1295 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1296 char *path = dev->parent_bus->info->get_dev_path(dev);
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
7685ee6a
AW
1337 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1338 char *id = dev->parent_bus->info->get_dev_path(dev);
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
539de124 1564int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared)
a672b469
AL
1565{
1566 SaveStateEntry *se;
39346385 1567 int ret;
a672b469 1568
c163b5ca 1569 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1570 if(se->set_params == NULL) {
1571 continue;
1572 }
1573 se->set_params(blk_enable, shared, se->opaque);
1574 }
1575
a672b469
AL
1576 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1577 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1578
72cf2d4f 1579 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1580 int len;
1581
1582 if (se->save_live_state == NULL)
1583 continue;
1584
1585 /* Section type */
1586 qemu_put_byte(f, QEMU_VM_SECTION_START);
1587 qemu_put_be32(f, se->section_id);
1588
1589 /* ID string */
1590 len = strlen(se->idstr);
1591 qemu_put_byte(f, len);
1592 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1593
1594 qemu_put_be32(f, se->instance_id);
1595 qemu_put_be32(f, se->version_id);
1596
539de124 1597 ret = se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
2975725f 1598 if (ret < 0) {
539de124 1599 qemu_savevm_state_cancel(f);
2975725f
JQ
1600 return ret;
1601 }
a672b469 1602 }
624b9cc2 1603 ret = qemu_file_get_error(f);
39346385 1604 if (ret != 0) {
539de124 1605 qemu_savevm_state_cancel(f);
4ec7fcc7 1606 }
a672b469 1607
39346385
JQ
1608 return ret;
1609
a672b469
AL
1610}
1611
39346385 1612/*
07f35073 1613 * this function has three return values:
39346385
JQ
1614 * negative: there was one error, and we have -errno.
1615 * 0 : We haven't finished, caller have to go again
1616 * 1 : We have finished, we can go to complete phase
1617 */
539de124 1618int qemu_savevm_state_iterate(QEMUFile *f)
a672b469
AL
1619{
1620 SaveStateEntry *se;
1621 int ret = 1;
1622
72cf2d4f 1623 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1624 if (se->save_live_state == NULL)
1625 continue;
1626
1627 /* Section type */
1628 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1629 qemu_put_be32(f, se->section_id);
1630
539de124 1631 ret = se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
2975725f 1632 if (ret <= 0) {
90697be8
JK
1633 /* Do not proceed to the next vmstate before this one reported
1634 completion of the current stage. This serializes the migration
1635 and reduces the probability that a faster changing state is
1636 synchronized over and over again. */
1637 break;
1638 }
a672b469 1639 }
39346385
JQ
1640 if (ret != 0) {
1641 return ret;
1642 }
624b9cc2 1643 ret = qemu_file_get_error(f);
39346385 1644 if (ret != 0) {
539de124 1645 qemu_savevm_state_cancel(f);
4ec7fcc7 1646 }
39346385 1647 return ret;
a672b469
AL
1648}
1649
539de124 1650int qemu_savevm_state_complete(QEMUFile *f)
a672b469
AL
1651{
1652 SaveStateEntry *se;
2975725f 1653 int ret;
a672b469 1654
ea375f9a
JK
1655 cpu_synchronize_all_states();
1656
72cf2d4f 1657 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1658 if (se->save_live_state == NULL)
1659 continue;
1660
1661 /* Section type */
1662 qemu_put_byte(f, QEMU_VM_SECTION_END);
1663 qemu_put_be32(f, se->section_id);
1664
539de124 1665 ret = se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
2975725f
JQ
1666 if (ret < 0) {
1667 return ret;
1668 }
a672b469
AL
1669 }
1670
72cf2d4f 1671 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1672 int len;
1673
9ed7d6ae 1674 if (se->save_state == NULL && se->vmsd == NULL)
a672b469
AL
1675 continue;
1676
1677 /* Section type */
1678 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1679 qemu_put_be32(f, se->section_id);
1680
1681 /* ID string */
1682 len = strlen(se->idstr);
1683 qemu_put_byte(f, len);
1684 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1685
1686 qemu_put_be32(f, se->instance_id);
1687 qemu_put_be32(f, se->version_id);
1688
dc912121 1689 vmstate_save(f, se);
a672b469
AL
1690 }
1691
1692 qemu_put_byte(f, QEMU_VM_EOF);
1693
624b9cc2 1694 return qemu_file_get_error(f);
a672b469
AL
1695}
1696
539de124 1697void qemu_savevm_state_cancel(QEMUFile *f)
4ec7fcc7
JK
1698{
1699 SaveStateEntry *se;
1700
1701 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1702 if (se->save_live_state) {
539de124 1703 se->save_live_state(f, -1, se->opaque);
4ec7fcc7
JK
1704 }
1705 }
1706}
1707
e1c37d0e 1708static int qemu_savevm_state(QEMUFile *f)
a672b469 1709{
a672b469
AL
1710 int ret;
1711
e1c37d0e 1712 if (qemu_savevm_state_blocked(NULL)) {
dc912121
AW
1713 ret = -EINVAL;
1714 goto out;
1715 }
1716
539de124 1717 ret = qemu_savevm_state_begin(f, 0, 0);
a672b469
AL
1718 if (ret < 0)
1719 goto out;
1720
1721 do {
539de124 1722 ret = qemu_savevm_state_iterate(f);
a672b469
AL
1723 if (ret < 0)
1724 goto out;
1725 } while (ret == 0);
1726
539de124 1727 ret = qemu_savevm_state_complete(f);
a672b469
AL
1728
1729out:
39346385 1730 if (ret == 0) {
624b9cc2 1731 ret = qemu_file_get_error(f);
39346385 1732 }
a672b469 1733
a672b469
AL
1734 return ret;
1735}
1736
a7ae8355
SS
1737static int qemu_save_device_state(QEMUFile *f)
1738{
1739 SaveStateEntry *se;
1740
1741 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1742 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1743
1744 cpu_synchronize_all_states();
1745
1746 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1747 int len;
1748
1749 if (se->is_ram) {
1750 continue;
1751 }
1752 if (se->save_state == NULL && se->vmsd == NULL) {
1753 continue;
1754 }
1755
1756 /* Section type */
1757 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1758 qemu_put_be32(f, se->section_id);
1759
1760 /* ID string */
1761 len = strlen(se->idstr);
1762 qemu_put_byte(f, len);
1763 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1764
1765 qemu_put_be32(f, se->instance_id);
1766 qemu_put_be32(f, se->version_id);
1767
1768 vmstate_save(f, se);
1769 }
1770
1771 qemu_put_byte(f, QEMU_VM_EOF);
1772
1773 return qemu_file_get_error(f);
1774}
1775
a672b469
AL
1776static SaveStateEntry *find_se(const char *idstr, int instance_id)
1777{
1778 SaveStateEntry *se;
1779
72cf2d4f 1780 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469 1781 if (!strcmp(se->idstr, idstr) &&
4d2ffa08
JK
1782 (instance_id == se->instance_id ||
1783 instance_id == se->alias_id))
a672b469 1784 return se;
7685ee6a
AW
1785 /* Migrating from an older version? */
1786 if (strstr(se->idstr, idstr) && se->compat) {
1787 if (!strcmp(se->compat->idstr, idstr) &&
1788 (instance_id == se->compat->instance_id ||
1789 instance_id == se->alias_id))
1790 return se;
1791 }
a672b469
AL
1792 }
1793 return NULL;
1794}
1795
811814bd
JQ
1796static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1797{
1798 while(sub && sub->needed) {
1799 if (strcmp(idstr, sub->vmsd->name) == 0) {
1800 return sub->vmsd;
1801 }
1802 sub++;
1803 }
1804 return NULL;
1805}
1806
1807static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1808 void *opaque)
1809{
c6380724 1810 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
811814bd
JQ
1811 char idstr[256];
1812 int ret;
c6380724 1813 uint8_t version_id, len, size;
811814bd
JQ
1814 const VMStateDescription *sub_vmsd;
1815
c6380724
JQ
1816 len = qemu_peek_byte(f, 1);
1817 if (len < strlen(vmsd->name) + 1) {
1818 /* subsection name has be be "section_name/a" */
1819 return 0;
1820 }
1821 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1822 if (size != len) {
1823 return 0;
1824 }
1825 idstr[size] = 0;
811814bd 1826
c6380724
JQ
1827 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1828 /* it don't have a valid subsection name */
1829 return 0;
1830 }
3da9eebd 1831 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
811814bd
JQ
1832 if (sub_vmsd == NULL) {
1833 return -ENOENT;
1834 }
c6380724
JQ
1835 qemu_file_skip(f, 1); /* subsection */
1836 qemu_file_skip(f, 1); /* len */
1837 qemu_file_skip(f, len); /* idstr */
1838 version_id = qemu_get_be32(f);
1839
811814bd
JQ
1840 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1841 if (ret) {
1842 return ret;
1843 }
1844 }
1845 return 0;
1846}
1847
1848static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1849 void *opaque)
1850{
1851 const VMStateSubsection *sub = vmsd->subsections;
1852
1853 while (sub && sub->needed) {
1854 if (sub->needed(opaque)) {
1855 const VMStateDescription *vmsd = sub->vmsd;
1856 uint8_t len;
1857
1858 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1859 len = strlen(vmsd->name);
1860 qemu_put_byte(f, len);
1861 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1862 qemu_put_be32(f, vmsd->version_id);
1863 vmstate_save_state(f, vmsd, opaque);
1864 }
1865 sub++;
1866 }
1867}
1868
a672b469 1869typedef struct LoadStateEntry {
72cf2d4f 1870 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1871 SaveStateEntry *se;
1872 int section_id;
1873 int version_id;
a672b469
AL
1874} LoadStateEntry;
1875
a672b469
AL
1876int qemu_loadvm_state(QEMUFile *f)
1877{
72cf2d4f
BS
1878 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1879 QLIST_HEAD_INITIALIZER(loadvm_handlers);
f4dbb8dd 1880 LoadStateEntry *le, *new_le;
a672b469
AL
1881 uint8_t section_type;
1882 unsigned int v;
1883 int ret;
1884
e1c37d0e 1885 if (qemu_savevm_state_blocked(NULL)) {
dc912121
AW
1886 return -EINVAL;
1887 }
1888
a672b469
AL
1889 v = qemu_get_be32(f);
1890 if (v != QEMU_VM_FILE_MAGIC)
1891 return -EINVAL;
1892
1893 v = qemu_get_be32(f);
bbfe1408
JQ
1894 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1895 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1896 return -ENOTSUP;
1897 }
a672b469
AL
1898 if (v != QEMU_VM_FILE_VERSION)
1899 return -ENOTSUP;
1900
1901 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1902 uint32_t instance_id, version_id, section_id;
a672b469
AL
1903 SaveStateEntry *se;
1904 char idstr[257];
1905 int len;
1906
1907 switch (section_type) {
1908 case QEMU_VM_SECTION_START:
1909 case QEMU_VM_SECTION_FULL:
1910 /* Read section start */
1911 section_id = qemu_get_be32(f);
1912 len = qemu_get_byte(f);
1913 qemu_get_buffer(f, (uint8_t *)idstr, len);
1914 idstr[len] = 0;
1915 instance_id = qemu_get_be32(f);
1916 version_id = qemu_get_be32(f);
1917
1918 /* Find savevm section */
1919 se = find_se(idstr, instance_id);
1920 if (se == NULL) {
1921 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1922 ret = -EINVAL;
1923 goto out;
1924 }
1925
1926 /* Validate version */
1927 if (version_id > se->version_id) {
1928 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1929 version_id, idstr, se->version_id);
1930 ret = -EINVAL;
1931 goto out;
1932 }
1933
1934 /* Add entry */
7267c094 1935 le = g_malloc0(sizeof(*le));
a672b469
AL
1936
1937 le->se = se;
1938 le->section_id = section_id;
1939 le->version_id = version_id;
72cf2d4f 1940 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
a672b469 1941
4082be4d 1942 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1943 if (ret < 0) {
1944 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1945 instance_id, idstr);
1946 goto out;
1947 }
a672b469
AL
1948 break;
1949 case QEMU_VM_SECTION_PART:
1950 case QEMU_VM_SECTION_END:
1951 section_id = qemu_get_be32(f);
1952
72cf2d4f 1953 QLIST_FOREACH(le, &loadvm_handlers, entry) {
f4dbb8dd
JQ
1954 if (le->section_id == section_id) {
1955 break;
1956 }
1957 }
a672b469
AL
1958 if (le == NULL) {
1959 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1960 ret = -EINVAL;
1961 goto out;
1962 }
1963
4082be4d 1964 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1965 if (ret < 0) {
1966 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1967 section_id);
1968 goto out;
1969 }
a672b469
AL
1970 break;
1971 default:
1972 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1973 ret = -EINVAL;
1974 goto out;
1975 }
1976 }
1977
ea375f9a
JK
1978 cpu_synchronize_all_post_init();
1979
a672b469
AL
1980 ret = 0;
1981
1982out:
72cf2d4f
BS
1983 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1984 QLIST_REMOVE(le, entry);
7267c094 1985 g_free(le);
a672b469
AL
1986 }
1987
42802d47
JQ
1988 if (ret == 0) {
1989 ret = qemu_file_get_error(f);
624b9cc2 1990 }
a672b469
AL
1991
1992 return ret;
1993}
1994
a672b469
AL
1995static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1996 const char *name)
1997{
1998 QEMUSnapshotInfo *sn_tab, *sn;
1999 int nb_sns, i, ret;
2000
2001 ret = -ENOENT;
2002 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2003 if (nb_sns < 0)
2004 return ret;
2005 for(i = 0; i < nb_sns; i++) {
2006 sn = &sn_tab[i];
2007 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2008 *sn_info = *sn;
2009 ret = 0;
2010 break;
2011 }
2012 }
7267c094 2013 g_free(sn_tab);
a672b469
AL
2014 return ret;
2015}
2016
cb499fb2
KW
2017/*
2018 * Deletes snapshots of a given name in all opened images.
2019 */
2020static int del_existing_snapshots(Monitor *mon, const char *name)
2021{
2022 BlockDriverState *bs;
cb499fb2
KW
2023 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2024 int ret;
2025
dbc13590
MA
2026 bs = NULL;
2027 while ((bs = bdrv_next(bs))) {
cb499fb2
KW
2028 if (bdrv_can_snapshot(bs) &&
2029 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2030 {
2031 ret = bdrv_snapshot_delete(bs, name);
2032 if (ret < 0) {
2033 monitor_printf(mon,
2034 "Error while deleting snapshot on '%s'\n",
2035 bdrv_get_device_name(bs));
2036 return -1;
2037 }
2038 }
2039 }
2040
2041 return 0;
2042}
2043
d54908a5 2044void do_savevm(Monitor *mon, const QDict *qdict)
a672b469
AL
2045{
2046 BlockDriverState *bs, *bs1;
2047 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 2048 int ret;
a672b469
AL
2049 QEMUFile *f;
2050 int saved_vm_running;
c2c9a466 2051 uint64_t vm_state_size;
a672b469
AL
2052#ifdef _WIN32
2053 struct _timeb tb;
7d631a11 2054 struct tm *ptm;
a672b469
AL
2055#else
2056 struct timeval tv;
7d631a11 2057 struct tm tm;
a672b469 2058#endif
d54908a5 2059 const char *name = qdict_get_try_str(qdict, "name");
a672b469 2060
feeee5ac 2061 /* Verify if there is a device that doesn't support snapshots and is writable */
dbc13590
MA
2062 bs = NULL;
2063 while ((bs = bdrv_next(bs))) {
feeee5ac 2064
07b70bfb 2065 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2066 continue;
2067 }
2068
2069 if (!bdrv_can_snapshot(bs)) {
2070 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2071 bdrv_get_device_name(bs));
2072 return;
2073 }
2074 }
2075
f9092b10 2076 bs = bdrv_snapshots();
a672b469 2077 if (!bs) {
376253ec 2078 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
2079 return;
2080 }
a672b469 2081
1354869c 2082 saved_vm_running = runstate_is_running();
0461d5a6 2083 vm_stop(RUN_STATE_SAVE_VM);
a672b469 2084
cb499fb2 2085 memset(sn, 0, sizeof(*sn));
a672b469
AL
2086
2087 /* fill auxiliary fields */
2088#ifdef _WIN32
2089 _ftime(&tb);
2090 sn->date_sec = tb.time;
2091 sn->date_nsec = tb.millitm * 1000000;
2092#else
2093 gettimeofday(&tv, NULL);
2094 sn->date_sec = tv.tv_sec;
2095 sn->date_nsec = tv.tv_usec * 1000;
2096#endif
74475455 2097 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
a672b469 2098
7d631a11
MDCF
2099 if (name) {
2100 ret = bdrv_snapshot_find(bs, old_sn, name);
2101 if (ret >= 0) {
2102 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2103 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2104 } else {
2105 pstrcpy(sn->name, sizeof(sn->name), name);
2106 }
2107 } else {
2108#ifdef _WIN32
55dd9ffa
SW
2109 time_t t = tb.time;
2110 ptm = localtime(&t);
7d631a11
MDCF
2111 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2112#else
d7d9b528
BS
2113 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2114 localtime_r((const time_t *)&tv.tv_sec, &tm);
7d631a11
MDCF
2115 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2116#endif
2117 }
2118
cb499fb2 2119 /* Delete old snapshots of the same name */
f139a412 2120 if (name && del_existing_snapshots(mon, name) < 0) {
cb499fb2
KW
2121 goto the_end;
2122 }
2123
a672b469 2124 /* save the VM state */
45566e9c 2125 f = qemu_fopen_bdrv(bs, 1);
a672b469 2126 if (!f) {
376253ec 2127 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
2128 goto the_end;
2129 }
e1c37d0e 2130 ret = qemu_savevm_state(f);
2d22b18f 2131 vm_state_size = qemu_ftell(f);
a672b469
AL
2132 qemu_fclose(f);
2133 if (ret < 0) {
376253ec 2134 monitor_printf(mon, "Error %d while writing VM\n", ret);
a672b469
AL
2135 goto the_end;
2136 }
2137
2138 /* create the snapshots */
2139
dbc13590
MA
2140 bs1 = NULL;
2141 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2142 if (bdrv_can_snapshot(bs1)) {
2d22b18f
AL
2143 /* Write VM state size only to the image that contains the state */
2144 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
2145 ret = bdrv_snapshot_create(bs1, sn);
2146 if (ret < 0) {
376253ec
AL
2147 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2148 bdrv_get_device_name(bs1));
a672b469
AL
2149 }
2150 }
2151 }
2152
2153 the_end:
2154 if (saved_vm_running)
2155 vm_start();
2156}
2157
a7ae8355
SS
2158void qmp_xen_save_devices_state(const char *filename, Error **errp)
2159{
2160 QEMUFile *f;
2161 int saved_vm_running;
2162 int ret;
2163
2164 saved_vm_running = runstate_is_running();
2165 vm_stop(RUN_STATE_SAVE_VM);
2166
2167 f = qemu_fopen(filename, "wb");
2168 if (!f) {
2169 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2170 goto the_end;
2171 }
2172 ret = qemu_save_device_state(f);
2173 qemu_fclose(f);
2174 if (ret < 0) {
2175 error_set(errp, QERR_IO_ERROR);
2176 }
2177
2178 the_end:
2179 if (saved_vm_running)
2180 vm_start();
2181 return;
2182}
2183
03cd4655 2184int load_vmstate(const char *name)
a672b469 2185{
f0aa7a8b 2186 BlockDriverState *bs, *bs_vm_state;
2d22b18f 2187 QEMUSnapshotInfo sn;
a672b469 2188 QEMUFile *f;
751c6a17 2189 int ret;
a672b469 2190
f0aa7a8b
MDCF
2191 bs_vm_state = bdrv_snapshots();
2192 if (!bs_vm_state) {
2193 error_report("No block device supports snapshots");
2194 return -ENOTSUP;
2195 }
2196
2197 /* Don't even try to load empty VM states */
2198 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2199 if (ret < 0) {
2200 return ret;
2201 } else if (sn.vm_state_size == 0) {
e11480db
KW
2202 error_report("This is a disk-only snapshot. Revert to it offline "
2203 "using qemu-img.");
f0aa7a8b
MDCF
2204 return -EINVAL;
2205 }
2206
2207 /* Verify if there is any device that doesn't support snapshots and is
2208 writable and check if the requested snapshot is available too. */
dbc13590
MA
2209 bs = NULL;
2210 while ((bs = bdrv_next(bs))) {
feeee5ac 2211
07b70bfb 2212 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
feeee5ac
MDCF
2213 continue;
2214 }
2215
2216 if (!bdrv_can_snapshot(bs)) {
2217 error_report("Device '%s' is writable but does not support snapshots.",
2218 bdrv_get_device_name(bs));
2219 return -ENOTSUP;
2220 }
feeee5ac 2221
f0aa7a8b
MDCF
2222 ret = bdrv_snapshot_find(bs, &sn, name);
2223 if (ret < 0) {
2224 error_report("Device '%s' does not have the requested snapshot '%s'",
2225 bdrv_get_device_name(bs), name);
2226 return ret;
2227 }
a672b469
AL
2228 }
2229
2230 /* Flush all IO requests so they don't interfere with the new state. */
922453bc 2231 bdrv_drain_all();
a672b469 2232
f0aa7a8b
MDCF
2233 bs = NULL;
2234 while ((bs = bdrv_next(bs))) {
2235 if (bdrv_can_snapshot(bs)) {
2236 ret = bdrv_snapshot_goto(bs, name);
a672b469 2237 if (ret < 0) {
f0aa7a8b
MDCF
2238 error_report("Error %d while activating snapshot '%s' on '%s'",
2239 ret, name, bdrv_get_device_name(bs));
2240 return ret;
a672b469
AL
2241 }
2242 }
2243 }
2244
a672b469 2245 /* restore the VM state */
f0aa7a8b 2246 f = qemu_fopen_bdrv(bs_vm_state, 0);
a672b469 2247 if (!f) {
1ecda02b 2248 error_report("Could not open VM state file");
05f2401e 2249 return -EINVAL;
a672b469 2250 }
f0aa7a8b 2251
5a8a49d7 2252 qemu_system_reset(VMRESET_SILENT);
a672b469 2253 ret = qemu_loadvm_state(f);
f0aa7a8b 2254
a672b469
AL
2255 qemu_fclose(f);
2256 if (ret < 0) {
1ecda02b 2257 error_report("Error %d while loading VM state", ret);
05f2401e 2258 return ret;
a672b469 2259 }
f0aa7a8b 2260
05f2401e 2261 return 0;
7b630349
JQ
2262}
2263
d54908a5 2264void do_delvm(Monitor *mon, const QDict *qdict)
a672b469
AL
2265{
2266 BlockDriverState *bs, *bs1;
751c6a17 2267 int ret;
d54908a5 2268 const char *name = qdict_get_str(qdict, "name");
a672b469 2269
f9092b10 2270 bs = bdrv_snapshots();
a672b469 2271 if (!bs) {
376253ec 2272 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
2273 return;
2274 }
2275
dbc13590
MA
2276 bs1 = NULL;
2277 while ((bs1 = bdrv_next(bs1))) {
feeee5ac 2278 if (bdrv_can_snapshot(bs1)) {
a672b469
AL
2279 ret = bdrv_snapshot_delete(bs1, name);
2280 if (ret < 0) {
2281 if (ret == -ENOTSUP)
376253ec
AL
2282 monitor_printf(mon,
2283 "Snapshots not supported on device '%s'\n",
2284 bdrv_get_device_name(bs1));
a672b469 2285 else
376253ec
AL
2286 monitor_printf(mon, "Error %d while deleting snapshot on "
2287 "'%s'\n", ret, bdrv_get_device_name(bs1));
a672b469
AL
2288 }
2289 }
2290 }
2291}
2292
376253ec 2293void do_info_snapshots(Monitor *mon)
a672b469
AL
2294{
2295 BlockDriverState *bs, *bs1;
f9209915
MDCF
2296 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2297 int nb_sns, i, ret, available;
2298 int total;
2299 int *available_snapshots;
a672b469
AL
2300 char buf[256];
2301
f9092b10 2302 bs = bdrv_snapshots();
a672b469 2303 if (!bs) {
376253ec 2304 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
2305 return;
2306 }
a672b469
AL
2307
2308 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2309 if (nb_sns < 0) {
376253ec 2310 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
2311 return;
2312 }
f9209915
MDCF
2313
2314 if (nb_sns == 0) {
2315 monitor_printf(mon, "There is no snapshot available.\n");
2316 return;
2317 }
2318
7267c094 2319 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
f9209915
MDCF
2320 total = 0;
2321 for (i = 0; i < nb_sns; i++) {
a672b469 2322 sn = &sn_tab[i];
f9209915
MDCF
2323 available = 1;
2324 bs1 = NULL;
2325
2326 while ((bs1 = bdrv_next(bs1))) {
2327 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2328 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2329 if (ret < 0) {
2330 available = 0;
2331 break;
2332 }
2333 }
2334 }
2335
2336 if (available) {
2337 available_snapshots[total] = i;
2338 total++;
2339 }
a672b469 2340 }
f9209915
MDCF
2341
2342 if (total > 0) {
2343 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2344 for (i = 0; i < total; i++) {
2345 sn = &sn_tab[available_snapshots[i]];
2346 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2347 }
2348 } else {
2349 monitor_printf(mon, "There is no suitable snapshot available\n");
2350 }
2351
7267c094
AL
2352 g_free(sn_tab);
2353 g_free(available_snapshots);
f9209915 2354
a672b469 2355}
c5705a77
AK
2356
2357void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2358{
1ddde087 2359 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
c5705a77
AK
2360 memory_region_name(mr), dev);
2361}
2362
2363void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2364{
2365 /* Nothing do to while the implementation is in RAMBlock */
2366}
2367
2368void vmstate_register_ram_global(MemoryRegion *mr)
2369{
2370 vmstate_register_ram(mr, NULL);
2371}