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