]> git.proxmox.com Git - mirror_qemu.git/blame - savevm.c
vmstate: remove usused VMSTATE_STRUCT_ARRAY_SIZE_UINT8
[mirror_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>
26#include <signal.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <zlib.h>
31
71e72a19 32/* Needed early for CONFIG_BSD etc. */
d40cdb10
BS
33#include "config-host.h"
34
a672b469
AL
35#ifndef _WIN32
36#include <sys/times.h>
37#include <sys/wait.h>
38#include <termios.h>
39#include <sys/mman.h>
40#include <sys/ioctl.h>
41#include <sys/resource.h>
42#include <sys/socket.h>
43#include <netinet/in.h>
44#include <net/if.h>
a672b469
AL
45#include <arpa/inet.h>
46#include <dirent.h>
47#include <netdb.h>
48#include <sys/select.h>
71e72a19 49#ifdef CONFIG_BSD
a672b469 50#include <sys/stat.h>
a167ba50 51#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
a672b469
AL
52#include <libutil.h>
53#else
54#include <util.h>
55#endif
a672b469
AL
56#ifdef __linux__
57#include <pty.h>
58#include <malloc.h>
59#include <linux/rtc.h>
60#endif
61#endif
62#endif
63
64#ifdef _WIN32
49dc768d 65#include <windows.h>
a672b469
AL
66#include <malloc.h>
67#include <sys/timeb.h>
68#include <mmsystem.h>
69#define getopt_long_only getopt_long
70#define memalign(align, size) malloc(size)
71#endif
72
511d2b14
BS
73#include "qemu-common.h"
74#include "hw/hw.h"
75#include "net.h"
76#include "monitor.h"
77#include "sysemu.h"
78#include "qemu-timer.h"
79#include "qemu-char.h"
80#include "block.h"
81#include "audio/audio.h"
82#include "migration.h"
83#include "qemu_socket.h"
72cf2d4f 84#include "qemu-queue.h"
511d2b14 85
a672b469
AL
86/* point to the block driver where the snapshots are managed */
87static BlockDriverState *bs_snapshots;
88
89#define SELF_ANNOUNCE_ROUNDS 5
a672b469 90
18995b98
N
91#ifndef ETH_P_RARP
92#define ETH_P_RARP 0x0835
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, ... */
143 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
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;
154 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
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
179 int has_error;
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 {
c5b76b38 200 len = recv(s->fd, (void *)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;
212 qemu_free(s);
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;
238 pclose(s->stdio_file);
239 qemu_free(s);
240 return 0;
241}
242
243static int stdio_fclose(void *opaque)
a672b469 244{
7f79dd28
PB
245 QEMUFileStdio *s = opaque;
246 fclose(s->stdio_file);
a672b469
AL
247 qemu_free(s);
248 return 0;
249}
250
7f79dd28 251QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
a672b469 252{
7f79dd28 253 QEMUFileStdio *s;
a672b469 254
7f79dd28 255 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
a672b469
AL
256 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
257 return NULL;
258 }
259
7f79dd28 260 s = qemu_mallocz(sizeof(QEMUFileStdio));
a672b469 261
7f79dd28 262 s->stdio_file = stdio_file;
a672b469
AL
263
264 if(mode[0] == 'r') {
c163b5ca
LS
265 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
266 NULL, NULL, NULL);
a672b469 267 } else {
c163b5ca
LS
268 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
269 NULL, NULL, NULL);
a672b469 270 }
a672b469
AL
271 return s->file;
272}
273
274QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
275{
276 FILE *popen_file;
277
278 popen_file = popen(command, mode);
279 if(popen_file == NULL) {
280 return NULL;
281 }
282
283 return qemu_popen(popen_file, mode);
284}
285
7f79dd28 286int qemu_stdio_fd(QEMUFile *f)
8a43b1ea 287{
7f79dd28 288 QEMUFileStdio *p;
8a43b1ea
CL
289 int fd;
290
7f79dd28
PB
291 p = (QEMUFileStdio *)f->opaque;
292 fd = fileno(p->stdio_file);
8a43b1ea
CL
293
294 return fd;
295}
296
5ac1fad3
PB
297QEMUFile *qemu_fdopen(int fd, const char *mode)
298{
299 QEMUFileStdio *s;
300
301 if (mode == NULL ||
302 (mode[0] != 'r' && mode[0] != 'w') ||
303 mode[1] != 'b' || mode[2] != 0) {
304 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
305 return NULL;
306 }
307
308 s = qemu_mallocz(sizeof(QEMUFileStdio));
309 s->stdio_file = fdopen(fd, mode);
310 if (!s->stdio_file)
311 goto fail;
312
313 if(mode[0] == 'r') {
c163b5ca
LS
314 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
315 NULL, NULL, NULL);
5ac1fad3 316 } else {
c163b5ca
LS
317 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
318 NULL, NULL, NULL);
5ac1fad3
PB
319 }
320 return s->file;
321
322fail:
323 qemu_free(s);
324 return NULL;
325}
326
a672b469
AL
327QEMUFile *qemu_fopen_socket(int fd)
328{
329 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
330
a672b469 331 s->fd = fd;
c163b5ca
LS
332 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
333 NULL, NULL, NULL);
a672b469
AL
334 return s->file;
335}
336
a672b469
AL
337static int file_put_buffer(void *opaque, const uint8_t *buf,
338 int64_t pos, int size)
339{
340 QEMUFileStdio *s = opaque;
7f79dd28
PB
341 fseek(s->stdio_file, pos, SEEK_SET);
342 fwrite(buf, 1, size, s->stdio_file);
a672b469
AL
343 return size;
344}
345
346static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
347{
348 QEMUFileStdio *s = opaque;
7f79dd28
PB
349 fseek(s->stdio_file, pos, SEEK_SET);
350 return fread(buf, 1, size, s->stdio_file);
a672b469
AL
351}
352
353QEMUFile *qemu_fopen(const char *filename, const char *mode)
354{
355 QEMUFileStdio *s;
356
7f79dd28
PB
357 if (mode == NULL ||
358 (mode[0] != 'r' && mode[0] != 'w') ||
359 mode[1] != 'b' || mode[2] != 0) {
360 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
361 return NULL;
362 }
363
a672b469 364 s = qemu_mallocz(sizeof(QEMUFileStdio));
a672b469 365
7f79dd28
PB
366 s->stdio_file = fopen(filename, mode);
367 if (!s->stdio_file)
a672b469 368 goto fail;
c163b5ca 369
7f79dd28 370 if(mode[0] == 'w') {
c163b5ca
LS
371 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
372 NULL, NULL, NULL);
7f79dd28 373 } else {
c163b5ca
LS
374 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
375 NULL, NULL, NULL);
7f79dd28
PB
376 }
377 return s->file;
a672b469 378fail:
a672b469
AL
379 qemu_free(s);
380 return NULL;
381}
382
178e08a5 383static int block_put_buffer(void *opaque, const uint8_t *buf,
a672b469
AL
384 int64_t pos, int size)
385{
45566e9c 386 bdrv_save_vmstate(opaque, buf, pos, size);
a672b469
AL
387 return size;
388}
389
178e08a5 390static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
a672b469 391{
45566e9c 392 return bdrv_load_vmstate(opaque, buf, pos, size);
a672b469
AL
393}
394
395static int bdrv_fclose(void *opaque)
396{
a672b469
AL
397 return 0;
398}
399
45566e9c 400static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
a672b469 401{
a672b469 402 if (is_writable)
c163b5ca
LS
403 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
404 NULL, NULL, NULL);
405 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
a672b469
AL
406}
407
408QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
409 QEMUFileGetBufferFunc *get_buffer,
410 QEMUFileCloseFunc *close,
19629537 411 QEMUFileRateLimit *rate_limit,
c163b5ca
LS
412 QEMUFileSetRateLimit *set_rate_limit,
413 QEMUFileGetRateLimit *get_rate_limit)
a672b469
AL
414{
415 QEMUFile *f;
416
417 f = qemu_mallocz(sizeof(QEMUFile));
a672b469
AL
418
419 f->opaque = opaque;
420 f->put_buffer = put_buffer;
421 f->get_buffer = get_buffer;
422 f->close = close;
423 f->rate_limit = rate_limit;
19629537 424 f->set_rate_limit = set_rate_limit;
c163b5ca 425 f->get_rate_limit = get_rate_limit;
a672b469
AL
426 f->is_write = 0;
427
428 return f;
429}
430
431int qemu_file_has_error(QEMUFile *f)
432{
433 return f->has_error;
434}
435
4dabe248
AL
436void qemu_file_set_error(QEMUFile *f)
437{
438 f->has_error = 1;
439}
440
a672b469
AL
441void qemu_fflush(QEMUFile *f)
442{
443 if (!f->put_buffer)
444 return;
445
446 if (f->is_write && f->buf_index > 0) {
447 int len;
448
449 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
450 if (len > 0)
451 f->buf_offset += f->buf_index;
452 else
453 f->has_error = 1;
454 f->buf_index = 0;
455 }
456}
457
458static void qemu_fill_buffer(QEMUFile *f)
459{
460 int len;
461
462 if (!f->get_buffer)
463 return;
464
465 if (f->is_write)
466 abort();
467
468 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
469 if (len > 0) {
470 f->buf_index = 0;
471 f->buf_size = len;
472 f->buf_offset += len;
473 } else if (len != -EAGAIN)
474 f->has_error = 1;
475}
476
477int qemu_fclose(QEMUFile *f)
478{
479 int ret = 0;
480 qemu_fflush(f);
481 if (f->close)
482 ret = f->close(f->opaque);
483 qemu_free(f);
484 return ret;
485}
486
487void qemu_file_put_notify(QEMUFile *f)
488{
489 f->put_buffer(f->opaque, NULL, 0, 0);
490}
491
492void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
493{
494 int l;
495
496 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
497 fprintf(stderr,
498 "Attempted to write to buffer while read buffer is not empty\n");
499 abort();
500 }
501
502 while (!f->has_error && size > 0) {
503 l = IO_BUF_SIZE - f->buf_index;
504 if (l > size)
505 l = size;
506 memcpy(f->buf + f->buf_index, buf, l);
507 f->is_write = 1;
508 f->buf_index += l;
509 buf += l;
510 size -= l;
511 if (f->buf_index >= IO_BUF_SIZE)
512 qemu_fflush(f);
513 }
514}
515
516void qemu_put_byte(QEMUFile *f, int v)
517{
518 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
519 fprintf(stderr,
520 "Attempted to write to buffer while read buffer is not empty\n");
521 abort();
522 }
523
524 f->buf[f->buf_index++] = v;
525 f->is_write = 1;
526 if (f->buf_index >= IO_BUF_SIZE)
527 qemu_fflush(f);
528}
529
530int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
531{
532 int size, l;
533
534 if (f->is_write)
535 abort();
536
537 size = size1;
538 while (size > 0) {
539 l = f->buf_size - f->buf_index;
540 if (l == 0) {
541 qemu_fill_buffer(f);
542 l = f->buf_size - f->buf_index;
543 if (l == 0)
544 break;
545 }
546 if (l > size)
547 l = size;
548 memcpy(buf, f->buf + f->buf_index, l);
549 f->buf_index += l;
550 buf += l;
551 size -= l;
552 }
553 return size1 - size;
554}
555
556int qemu_get_byte(QEMUFile *f)
557{
558 if (f->is_write)
559 abort();
560
561 if (f->buf_index >= f->buf_size) {
562 qemu_fill_buffer(f);
563 if (f->buf_index >= f->buf_size)
564 return 0;
565 }
566 return f->buf[f->buf_index++];
567}
568
569int64_t qemu_ftell(QEMUFile *f)
570{
571 return f->buf_offset - f->buf_size + f->buf_index;
572}
573
574int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
575{
576 if (whence == SEEK_SET) {
577 /* nothing to do */
578 } else if (whence == SEEK_CUR) {
579 pos += qemu_ftell(f);
580 } else {
581 /* SEEK_END not supported */
582 return -1;
583 }
584 if (f->put_buffer) {
585 qemu_fflush(f);
586 f->buf_offset = pos;
587 } else {
588 f->buf_offset = pos;
589 f->buf_index = 0;
590 f->buf_size = 0;
591 }
592 return pos;
593}
594
595int qemu_file_rate_limit(QEMUFile *f)
596{
597 if (f->rate_limit)
598 return f->rate_limit(f->opaque);
599
600 return 0;
601}
602
c163b5ca
LS
603size_t qemu_file_get_rate_limit(QEMUFile *f)
604{
605 if (f->get_rate_limit)
606 return f->get_rate_limit(f->opaque);
607
608 return 0;
609}
610
19629537
GC
611size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
612{
0bb05eaf
GC
613 /* any failed or completed migration keeps its state to allow probing of
614 * migration data, but has no associated file anymore */
615 if (f && f->set_rate_limit)
19629537
GC
616 return f->set_rate_limit(f->opaque, new_rate);
617
618 return 0;
619}
620
a672b469
AL
621void qemu_put_be16(QEMUFile *f, unsigned int v)
622{
623 qemu_put_byte(f, v >> 8);
624 qemu_put_byte(f, v);
625}
626
627void qemu_put_be32(QEMUFile *f, unsigned int v)
628{
629 qemu_put_byte(f, v >> 24);
630 qemu_put_byte(f, v >> 16);
631 qemu_put_byte(f, v >> 8);
632 qemu_put_byte(f, v);
633}
634
635void qemu_put_be64(QEMUFile *f, uint64_t v)
636{
637 qemu_put_be32(f, v >> 32);
638 qemu_put_be32(f, v);
639}
640
641unsigned int qemu_get_be16(QEMUFile *f)
642{
643 unsigned int v;
644 v = qemu_get_byte(f) << 8;
645 v |= qemu_get_byte(f);
646 return v;
647}
648
649unsigned int qemu_get_be32(QEMUFile *f)
650{
651 unsigned int v;
652 v = qemu_get_byte(f) << 24;
653 v |= qemu_get_byte(f) << 16;
654 v |= qemu_get_byte(f) << 8;
655 v |= qemu_get_byte(f);
656 return v;
657}
658
659uint64_t qemu_get_be64(QEMUFile *f)
660{
661 uint64_t v;
662 v = (uint64_t)qemu_get_be32(f) << 32;
663 v |= qemu_get_be32(f);
664 return v;
665}
666
9ed7d6ae
JQ
667/* 8 bit int */
668
669static int get_int8(QEMUFile *f, void *pv, size_t size)
670{
671 int8_t *v = pv;
672 qemu_get_s8s(f, v);
673 return 0;
674}
675
84e2e3eb 676static void put_int8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 677{
84e2e3eb 678 int8_t *v = pv;
9ed7d6ae
JQ
679 qemu_put_s8s(f, v);
680}
681
682const VMStateInfo vmstate_info_int8 = {
683 .name = "int8",
684 .get = get_int8,
685 .put = put_int8,
686};
687
688/* 16 bit int */
689
690static int get_int16(QEMUFile *f, void *pv, size_t size)
691{
692 int16_t *v = pv;
693 qemu_get_sbe16s(f, v);
694 return 0;
695}
696
84e2e3eb 697static void put_int16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 698{
84e2e3eb 699 int16_t *v = pv;
9ed7d6ae
JQ
700 qemu_put_sbe16s(f, v);
701}
702
703const VMStateInfo vmstate_info_int16 = {
704 .name = "int16",
705 .get = get_int16,
706 .put = put_int16,
707};
708
709/* 32 bit int */
710
711static int get_int32(QEMUFile *f, void *pv, size_t size)
712{
713 int32_t *v = pv;
714 qemu_get_sbe32s(f, v);
715 return 0;
716}
717
84e2e3eb 718static void put_int32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 719{
84e2e3eb 720 int32_t *v = pv;
9ed7d6ae
JQ
721 qemu_put_sbe32s(f, v);
722}
723
724const VMStateInfo vmstate_info_int32 = {
725 .name = "int32",
726 .get = get_int32,
727 .put = put_int32,
728};
729
82501660
JQ
730/* 32 bit int. See that the received value is the same than the one
731 in the field */
732
733static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
734{
735 int32_t *v = pv;
736 int32_t v2;
737 qemu_get_sbe32s(f, &v2);
738
739 if (*v == v2)
740 return 0;
741 return -EINVAL;
742}
743
744const VMStateInfo vmstate_info_int32_equal = {
745 .name = "int32 equal",
746 .get = get_int32_equal,
747 .put = put_int32,
748};
749
0a031e0a
JQ
750/* 32 bit int. See that the received value is the less or the same
751 than the one in the field */
752
753static int get_int32_le(QEMUFile *f, void *pv, size_t size)
754{
755 int32_t *old = pv;
756 int32_t new;
757 qemu_get_sbe32s(f, &new);
758
759 if (*old <= new)
760 return 0;
761 return -EINVAL;
762}
763
764const VMStateInfo vmstate_info_int32_le = {
765 .name = "int32 equal",
766 .get = get_int32_le,
767 .put = put_int32,
768};
769
9ed7d6ae
JQ
770/* 64 bit int */
771
772static int get_int64(QEMUFile *f, void *pv, size_t size)
773{
774 int64_t *v = pv;
775 qemu_get_sbe64s(f, v);
776 return 0;
777}
778
84e2e3eb 779static void put_int64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 780{
84e2e3eb 781 int64_t *v = pv;
9ed7d6ae
JQ
782 qemu_put_sbe64s(f, v);
783}
784
785const VMStateInfo vmstate_info_int64 = {
786 .name = "int64",
787 .get = get_int64,
788 .put = put_int64,
789};
790
791/* 8 bit unsigned int */
792
793static int get_uint8(QEMUFile *f, void *pv, size_t size)
794{
795 uint8_t *v = pv;
796 qemu_get_8s(f, v);
797 return 0;
798}
799
84e2e3eb 800static void put_uint8(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 801{
84e2e3eb 802 uint8_t *v = pv;
9ed7d6ae
JQ
803 qemu_put_8s(f, v);
804}
805
806const VMStateInfo vmstate_info_uint8 = {
807 .name = "uint8",
808 .get = get_uint8,
809 .put = put_uint8,
810};
811
812/* 16 bit unsigned int */
813
814static int get_uint16(QEMUFile *f, void *pv, size_t size)
815{
816 uint16_t *v = pv;
817 qemu_get_be16s(f, v);
818 return 0;
819}
820
84e2e3eb 821static void put_uint16(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 822{
84e2e3eb 823 uint16_t *v = pv;
9ed7d6ae
JQ
824 qemu_put_be16s(f, v);
825}
826
827const VMStateInfo vmstate_info_uint16 = {
828 .name = "uint16",
829 .get = get_uint16,
830 .put = put_uint16,
831};
832
833/* 32 bit unsigned int */
834
835static int get_uint32(QEMUFile *f, void *pv, size_t size)
836{
837 uint32_t *v = pv;
838 qemu_get_be32s(f, v);
839 return 0;
840}
841
84e2e3eb 842static void put_uint32(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 843{
84e2e3eb 844 uint32_t *v = pv;
9ed7d6ae
JQ
845 qemu_put_be32s(f, v);
846}
847
848const VMStateInfo vmstate_info_uint32 = {
849 .name = "uint32",
850 .get = get_uint32,
851 .put = put_uint32,
852};
853
854/* 64 bit unsigned int */
855
856static int get_uint64(QEMUFile *f, void *pv, size_t size)
857{
858 uint64_t *v = pv;
859 qemu_get_be64s(f, v);
860 return 0;
861}
862
84e2e3eb 863static void put_uint64(QEMUFile *f, void *pv, size_t size)
9ed7d6ae 864{
84e2e3eb 865 uint64_t *v = pv;
9ed7d6ae
JQ
866 qemu_put_be64s(f, v);
867}
868
869const VMStateInfo vmstate_info_uint64 = {
870 .name = "uint64",
871 .get = get_uint64,
872 .put = put_uint64,
873};
874
80cd83e7
JQ
875/* 8 bit int. See that the received value is the same than the one
876 in the field */
877
878static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
879{
880 uint8_t *v = pv;
881 uint8_t v2;
882 qemu_get_8s(f, &v2);
883
884 if (*v == v2)
885 return 0;
886 return -EINVAL;
887}
888
889const VMStateInfo vmstate_info_uint8_equal = {
aa1cce69 890 .name = "uint8 equal",
80cd83e7
JQ
891 .get = get_uint8_equal,
892 .put = put_uint8,
893};
894
dc3b83a0
JQ
895/* 16 bit unsigned int int. See that the received value is the same than the one
896 in the field */
897
898static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
899{
900 uint16_t *v = pv;
901 uint16_t v2;
902 qemu_get_be16s(f, &v2);
903
904 if (*v == v2)
905 return 0;
906 return -EINVAL;
907}
908
909const VMStateInfo vmstate_info_uint16_equal = {
910 .name = "uint16 equal",
911 .get = get_uint16_equal,
912 .put = put_uint16,
913};
914
dde0463b
JQ
915/* timers */
916
917static int get_timer(QEMUFile *f, void *pv, size_t size)
918{
919 QEMUTimer *v = pv;
920 qemu_get_timer(f, v);
921 return 0;
922}
923
84e2e3eb 924static void put_timer(QEMUFile *f, void *pv, size_t size)
dde0463b 925{
84e2e3eb 926 QEMUTimer *v = pv;
dde0463b
JQ
927 qemu_put_timer(f, v);
928}
929
930const VMStateInfo vmstate_info_timer = {
931 .name = "timer",
932 .get = get_timer,
933 .put = put_timer,
934};
935
6f67c50f
JQ
936/* uint8_t buffers */
937
938static int get_buffer(QEMUFile *f, void *pv, size_t size)
939{
940 uint8_t *v = pv;
941 qemu_get_buffer(f, v, size);
942 return 0;
943}
944
84e2e3eb 945static void put_buffer(QEMUFile *f, void *pv, size_t size)
6f67c50f 946{
84e2e3eb 947 uint8_t *v = pv;
6f67c50f
JQ
948 qemu_put_buffer(f, v, size);
949}
950
951const VMStateInfo vmstate_info_buffer = {
952 .name = "buffer",
953 .get = get_buffer,
954 .put = put_buffer,
955};
956
76507c75
JQ
957/* unused buffers: space that was used for some fields that are
958 not usefull anymore */
959
960static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
961{
21174c34
JK
962 uint8_t buf[1024];
963 int block_len;
964
965 while (size > 0) {
966 block_len = MIN(sizeof(buf), size);
967 size -= block_len;
968 qemu_get_buffer(f, buf, block_len);
969 }
970 return 0;
76507c75
JQ
971}
972
973static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
974{
21174c34
JK
975 static const uint8_t buf[1024];
976 int block_len;
977
978 while (size > 0) {
979 block_len = MIN(sizeof(buf), size);
980 size -= block_len;
981 qemu_put_buffer(f, buf, block_len);
982 }
76507c75
JQ
983}
984
985const VMStateInfo vmstate_info_unused_buffer = {
986 .name = "unused_buffer",
987 .get = get_unused_buffer,
988 .put = put_unused_buffer,
989};
990
a672b469 991typedef struct SaveStateEntry {
72cf2d4f 992 QTAILQ_ENTRY(SaveStateEntry) entry;
a672b469
AL
993 char idstr[256];
994 int instance_id;
995 int version_id;
996 int section_id;
c163b5ca 997 SaveSetParamsHandler *set_params;
a672b469
AL
998 SaveLiveStateHandler *save_live_state;
999 SaveStateHandler *save_state;
1000 LoadStateHandler *load_state;
9ed7d6ae 1001 const VMStateDescription *vmsd;
a672b469 1002 void *opaque;
a672b469
AL
1003} SaveStateEntry;
1004
c163b5ca 1005
72cf2d4f
BS
1006static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1007 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
9ed7d6ae 1008static int global_section_id;
a672b469 1009
8718e999
JQ
1010static int calculate_new_instance_id(const char *idstr)
1011{
1012 SaveStateEntry *se;
1013 int instance_id = 0;
1014
72cf2d4f 1015 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
8718e999
JQ
1016 if (strcmp(idstr, se->idstr) == 0
1017 && instance_id <= se->instance_id) {
1018 instance_id = se->instance_id + 1;
1019 }
1020 }
1021 return instance_id;
1022}
1023
a672b469
AL
1024/* TODO: Individual devices generally have very little idea about the rest
1025 of the system, so instance_id should be removed/replaced.
1026 Meanwhile pass -1 as instance_id if you do not already have a clearly
1027 distinguishing id for all instances of your device class. */
1028int register_savevm_live(const char *idstr,
1029 int instance_id,
1030 int version_id,
c163b5ca 1031 SaveSetParamsHandler *set_params,
a672b469
AL
1032 SaveLiveStateHandler *save_live_state,
1033 SaveStateHandler *save_state,
1034 LoadStateHandler *load_state,
1035 void *opaque)
1036{
8718e999 1037 SaveStateEntry *se;
a672b469 1038
c163b5ca 1039 se = qemu_mallocz(sizeof(SaveStateEntry));
a672b469 1040 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
a672b469
AL
1041 se->version_id = version_id;
1042 se->section_id = global_section_id++;
c163b5ca 1043 se->set_params = set_params;
a672b469
AL
1044 se->save_live_state = save_live_state;
1045 se->save_state = save_state;
1046 se->load_state = load_state;
1047 se->opaque = opaque;
9ed7d6ae 1048 se->vmsd = NULL;
a672b469 1049
8718e999
JQ
1050 if (instance_id == -1) {
1051 se->instance_id = calculate_new_instance_id(idstr);
1052 } else {
1053 se->instance_id = instance_id;
a672b469 1054 }
8718e999 1055 /* add at the end of list */
72cf2d4f 1056 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
a672b469
AL
1057 return 0;
1058}
1059
1060int register_savevm(const char *idstr,
1061 int instance_id,
1062 int version_id,
1063 SaveStateHandler *save_state,
1064 LoadStateHandler *load_state,
1065 void *opaque)
1066{
1067 return register_savevm_live(idstr, instance_id, version_id,
c163b5ca 1068 NULL, NULL, save_state, load_state, opaque);
a672b469
AL
1069}
1070
41bd13af
AL
1071void unregister_savevm(const char *idstr, void *opaque)
1072{
8718e999 1073 SaveStateEntry *se, *new_se;
41bd13af 1074
72cf2d4f 1075 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
8718e999 1076 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
72cf2d4f 1077 QTAILQ_REMOVE(&savevm_handlers, se, entry);
8718e999 1078 qemu_free(se);
41bd13af 1079 }
41bd13af
AL
1080 }
1081}
1082
9ed7d6ae
JQ
1083int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1084 void *opaque)
1085{
8718e999 1086 SaveStateEntry *se;
9ed7d6ae 1087
c163b5ca 1088 se = qemu_mallocz(sizeof(SaveStateEntry));
9ed7d6ae 1089 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
9ed7d6ae
JQ
1090 se->version_id = vmsd->version_id;
1091 se->section_id = global_section_id++;
1092 se->save_live_state = NULL;
1093 se->save_state = NULL;
1094 se->load_state = NULL;
1095 se->opaque = opaque;
1096 se->vmsd = vmsd;
9ed7d6ae 1097
8718e999
JQ
1098 if (instance_id == -1) {
1099 se->instance_id = calculate_new_instance_id(vmsd->name);
1100 } else {
1101 se->instance_id = instance_id;
9ed7d6ae 1102 }
8718e999 1103 /* add at the end of list */
72cf2d4f 1104 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
9ed7d6ae
JQ
1105 return 0;
1106}
1107
1eb7538b 1108void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
9ed7d6ae 1109{
1eb7538b
JQ
1110 SaveStateEntry *se, *new_se;
1111
72cf2d4f 1112 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1eb7538b 1113 if (se->vmsd == vmsd && se->opaque == opaque) {
72cf2d4f 1114 QTAILQ_REMOVE(&savevm_handlers, se, entry);
1eb7538b
JQ
1115 qemu_free(se);
1116 }
1117 }
9ed7d6ae
JQ
1118}
1119
1120int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1121 void *opaque, int version_id)
1122{
1123 VMStateField *field = vmsd->fields;
1124
1125 if (version_id > vmsd->version_id) {
1126 return -EINVAL;
1127 }
1128 if (version_id < vmsd->minimum_version_id_old) {
1129 return -EINVAL;
1130 }
1131 if (version_id < vmsd->minimum_version_id) {
1132 return vmsd->load_state_old(f, opaque, version_id);
1133 }
fd4d52de
JQ
1134 if (vmsd->pre_load) {
1135 int ret = vmsd->pre_load(opaque);
1136 if (ret)
1137 return ret;
1138 }
9ed7d6ae 1139 while(field->name) {
f11f6a5f
JQ
1140 if ((field->field_exists &&
1141 field->field_exists(opaque, version_id)) ||
1142 (!field->field_exists &&
1143 field->version_id <= version_id)) {
f752a6aa
JQ
1144 void *base_addr = opaque + field->offset;
1145 int ret, i, n_elems = 1;
e61a1e0a 1146 int size = field->size;
9ed7d6ae 1147
e61a1e0a
JQ
1148 if (field->flags & VMS_VBUFFER) {
1149 size = *(int32_t *)(opaque+field->size_offset);
1150 }
f752a6aa
JQ
1151 if (field->flags & VMS_ARRAY) {
1152 n_elems = field->num;
d6698281
JQ
1153 } else if (field->flags & VMS_VARRAY_INT32) {
1154 n_elems = *(int32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1155 } else if (field->flags & VMS_VARRAY_UINT16) {
1156 n_elems = *(uint16_t *)(opaque+field->num_offset);
f752a6aa 1157 }
dde0463b 1158 if (field->flags & VMS_POINTER) {
e61a1e0a 1159 base_addr = *(void **)base_addr + field->start;
dde0463b 1160 }
f752a6aa 1161 for (i = 0; i < n_elems; i++) {
e61a1e0a 1162 void *addr = base_addr + size * i;
ec245e21 1163
19df438b
JQ
1164 if (field->flags & VMS_ARRAY_OF_POINTER) {
1165 addr = *(void **)addr;
1166 }
ec245e21 1167 if (field->flags & VMS_STRUCT) {
fa3aad24 1168 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
ec245e21 1169 } else {
e61a1e0a 1170 ret = field->info->get(f, addr, size);
ec245e21
JQ
1171
1172 }
f752a6aa
JQ
1173 if (ret < 0) {
1174 return ret;
1175 }
9ed7d6ae
JQ
1176 }
1177 }
1178 field++;
1179 }
752ff2fa 1180 if (vmsd->post_load) {
e59fb374 1181 return vmsd->post_load(opaque, version_id);
752ff2fa 1182 }
9ed7d6ae
JQ
1183 return 0;
1184}
1185
1186void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
84e2e3eb 1187 void *opaque)
9ed7d6ae
JQ
1188{
1189 VMStateField *field = vmsd->fields;
1190
8fb0791d
JQ
1191 if (vmsd->pre_save) {
1192 vmsd->pre_save(opaque);
1193 }
9ed7d6ae 1194 while(field->name) {
f11f6a5f
JQ
1195 if (!field->field_exists ||
1196 field->field_exists(opaque, vmsd->version_id)) {
1197 void *base_addr = opaque + field->offset;
1198 int i, n_elems = 1;
e61a1e0a 1199 int size = field->size;
dde0463b 1200
e61a1e0a
JQ
1201 if (field->flags & VMS_VBUFFER) {
1202 size = *(int32_t *)(opaque+field->size_offset);
1203 }
f11f6a5f
JQ
1204 if (field->flags & VMS_ARRAY) {
1205 n_elems = field->num;
d6698281
JQ
1206 } else if (field->flags & VMS_VARRAY_INT32) {
1207 n_elems = *(int32_t *)(opaque+field->num_offset);
bdb4941d
JQ
1208 } else if (field->flags & VMS_VARRAY_UINT16) {
1209 n_elems = *(uint16_t *)(opaque+field->num_offset);
f11f6a5f
JQ
1210 }
1211 if (field->flags & VMS_POINTER) {
e61a1e0a 1212 base_addr = *(void **)base_addr + field->start;
f11f6a5f
JQ
1213 }
1214 for (i = 0; i < n_elems; i++) {
e61a1e0a 1215 void *addr = base_addr + size * i;
ec245e21 1216
8595387e
JQ
1217 if (field->flags & VMS_ARRAY_OF_POINTER) {
1218 addr = *(void **)addr;
1219 }
f11f6a5f
JQ
1220 if (field->flags & VMS_STRUCT) {
1221 vmstate_save_state(f, field->vmsd, addr);
1222 } else {
e61a1e0a 1223 field->info->put(f, addr, size);
f11f6a5f 1224 }
ec245e21 1225 }
dde0463b 1226 }
9ed7d6ae
JQ
1227 field++;
1228 }
8fb0791d
JQ
1229 if (vmsd->post_save) {
1230 vmsd->post_save(opaque);
1231 }
9ed7d6ae
JQ
1232}
1233
4082be4d
JQ
1234static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1235{
9ed7d6ae
JQ
1236 if (!se->vmsd) { /* Old style */
1237 return se->load_state(f, se->opaque, version_id);
1238 }
1239 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
4082be4d
JQ
1240}
1241
1242static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1243{
9ed7d6ae
JQ
1244 if (!se->vmsd) { /* Old style */
1245 se->save_state(f, se->opaque);
1246 return;
1247 }
1248 vmstate_save_state(f,se->vmsd, se->opaque);
4082be4d
JQ
1249}
1250
a672b469
AL
1251#define QEMU_VM_FILE_MAGIC 0x5145564d
1252#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1253#define QEMU_VM_FILE_VERSION 0x00000003
1254
1255#define QEMU_VM_EOF 0x00
1256#define QEMU_VM_SECTION_START 0x01
1257#define QEMU_VM_SECTION_PART 0x02
1258#define QEMU_VM_SECTION_END 0x03
1259#define QEMU_VM_SECTION_FULL 0x04
1260
c163b5ca 1261int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared)
a672b469
AL
1262{
1263 SaveStateEntry *se;
1264
c163b5ca
LS
1265 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1266 if(se->set_params == NULL) {
1267 continue;
1268 }
1269 se->set_params(blk_enable, shared, se->opaque);
1270 }
1271
a672b469
AL
1272 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1273 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1274
72cf2d4f 1275 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1276 int len;
1277
1278 if (se->save_live_state == NULL)
1279 continue;
1280
1281 /* Section type */
1282 qemu_put_byte(f, QEMU_VM_SECTION_START);
1283 qemu_put_be32(f, se->section_id);
1284
1285 /* ID string */
1286 len = strlen(se->idstr);
1287 qemu_put_byte(f, len);
1288 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1289
1290 qemu_put_be32(f, se->instance_id);
1291 qemu_put_be32(f, se->version_id);
1292
1293 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1294 }
1295
1296 if (qemu_file_has_error(f))
1297 return -EIO;
1298
1299 return 0;
1300}
1301
1302int qemu_savevm_state_iterate(QEMUFile *f)
1303{
1304 SaveStateEntry *se;
1305 int ret = 1;
1306
72cf2d4f 1307 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1308 if (se->save_live_state == NULL)
1309 continue;
1310
1311 /* Section type */
1312 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1313 qemu_put_be32(f, se->section_id);
1314
1315 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1316 }
1317
1318 if (ret)
1319 return 1;
1320
1321 if (qemu_file_has_error(f))
1322 return -EIO;
1323
1324 return 0;
1325}
1326
1327int qemu_savevm_state_complete(QEMUFile *f)
1328{
1329 SaveStateEntry *se;
1330
72cf2d4f 1331 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1332 if (se->save_live_state == NULL)
1333 continue;
1334
1335 /* Section type */
1336 qemu_put_byte(f, QEMU_VM_SECTION_END);
1337 qemu_put_be32(f, se->section_id);
1338
1339 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1340 }
1341
72cf2d4f 1342 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1343 int len;
1344
9ed7d6ae 1345 if (se->save_state == NULL && se->vmsd == NULL)
a672b469
AL
1346 continue;
1347
1348 /* Section type */
1349 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1350 qemu_put_be32(f, se->section_id);
1351
1352 /* ID string */
1353 len = strlen(se->idstr);
1354 qemu_put_byte(f, len);
1355 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1356
1357 qemu_put_be32(f, se->instance_id);
1358 qemu_put_be32(f, se->version_id);
1359
4082be4d 1360 vmstate_save(f, se);
a672b469
AL
1361 }
1362
1363 qemu_put_byte(f, QEMU_VM_EOF);
1364
1365 if (qemu_file_has_error(f))
1366 return -EIO;
1367
1368 return 0;
1369}
1370
1371int qemu_savevm_state(QEMUFile *f)
1372{
1373 int saved_vm_running;
1374 int ret;
1375
1376 saved_vm_running = vm_running;
1377 vm_stop(0);
1378
1379 bdrv_flush_all();
1380
c163b5ca 1381 ret = qemu_savevm_state_begin(f, 0, 0);
a672b469
AL
1382 if (ret < 0)
1383 goto out;
1384
1385 do {
1386 ret = qemu_savevm_state_iterate(f);
1387 if (ret < 0)
1388 goto out;
1389 } while (ret == 0);
1390
1391 ret = qemu_savevm_state_complete(f);
1392
1393out:
1394 if (qemu_file_has_error(f))
1395 ret = -EIO;
1396
1397 if (!ret && saved_vm_running)
1398 vm_start();
1399
1400 return ret;
1401}
1402
1403static SaveStateEntry *find_se(const char *idstr, int instance_id)
1404{
1405 SaveStateEntry *se;
1406
72cf2d4f 1407 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
a672b469
AL
1408 if (!strcmp(se->idstr, idstr) &&
1409 instance_id == se->instance_id)
1410 return se;
1411 }
1412 return NULL;
1413}
1414
1415typedef struct LoadStateEntry {
72cf2d4f 1416 QLIST_ENTRY(LoadStateEntry) entry;
a672b469
AL
1417 SaveStateEntry *se;
1418 int section_id;
1419 int version_id;
a672b469
AL
1420} LoadStateEntry;
1421
a672b469
AL
1422int qemu_loadvm_state(QEMUFile *f)
1423{
72cf2d4f
BS
1424 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1425 QLIST_HEAD_INITIALIZER(loadvm_handlers);
f4dbb8dd 1426 LoadStateEntry *le, *new_le;
a672b469
AL
1427 uint8_t section_type;
1428 unsigned int v;
1429 int ret;
1430
1431 v = qemu_get_be32(f);
1432 if (v != QEMU_VM_FILE_MAGIC)
1433 return -EINVAL;
1434
1435 v = qemu_get_be32(f);
bbfe1408
JQ
1436 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1437 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1438 return -ENOTSUP;
1439 }
a672b469
AL
1440 if (v != QEMU_VM_FILE_VERSION)
1441 return -ENOTSUP;
1442
1443 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1444 uint32_t instance_id, version_id, section_id;
a672b469
AL
1445 SaveStateEntry *se;
1446 char idstr[257];
1447 int len;
1448
1449 switch (section_type) {
1450 case QEMU_VM_SECTION_START:
1451 case QEMU_VM_SECTION_FULL:
1452 /* Read section start */
1453 section_id = qemu_get_be32(f);
1454 len = qemu_get_byte(f);
1455 qemu_get_buffer(f, (uint8_t *)idstr, len);
1456 idstr[len] = 0;
1457 instance_id = qemu_get_be32(f);
1458 version_id = qemu_get_be32(f);
1459
1460 /* Find savevm section */
1461 se = find_se(idstr, instance_id);
1462 if (se == NULL) {
1463 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1464 ret = -EINVAL;
1465 goto out;
1466 }
1467
1468 /* Validate version */
1469 if (version_id > se->version_id) {
1470 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1471 version_id, idstr, se->version_id);
1472 ret = -EINVAL;
1473 goto out;
1474 }
1475
1476 /* Add entry */
1477 le = qemu_mallocz(sizeof(*le));
a672b469
AL
1478
1479 le->se = se;
1480 le->section_id = section_id;
1481 le->version_id = version_id;
72cf2d4f 1482 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
a672b469 1483
4082be4d 1484 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1485 if (ret < 0) {
1486 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1487 instance_id, idstr);
1488 goto out;
1489 }
a672b469
AL
1490 break;
1491 case QEMU_VM_SECTION_PART:
1492 case QEMU_VM_SECTION_END:
1493 section_id = qemu_get_be32(f);
1494
72cf2d4f 1495 QLIST_FOREACH(le, &loadvm_handlers, entry) {
f4dbb8dd
JQ
1496 if (le->section_id == section_id) {
1497 break;
1498 }
1499 }
a672b469
AL
1500 if (le == NULL) {
1501 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1502 ret = -EINVAL;
1503 goto out;
1504 }
1505
4082be4d 1506 ret = vmstate_load(f, le->se, le->version_id);
b5a22e4a
JQ
1507 if (ret < 0) {
1508 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1509 section_id);
1510 goto out;
1511 }
a672b469
AL
1512 break;
1513 default:
1514 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1515 ret = -EINVAL;
1516 goto out;
1517 }
1518 }
1519
1520 ret = 0;
1521
1522out:
72cf2d4f
BS
1523 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1524 QLIST_REMOVE(le, entry);
a672b469
AL
1525 qemu_free(le);
1526 }
1527
1528 if (qemu_file_has_error(f))
1529 ret = -EIO;
1530
1531 return ret;
1532}
1533
1534/* device can contain snapshots */
1535static int bdrv_can_snapshot(BlockDriverState *bs)
1536{
1537 return (bs &&
1538 !bdrv_is_removable(bs) &&
1539 !bdrv_is_read_only(bs));
1540}
1541
1542/* device must be snapshots in order to have a reliable snapshot */
1543static int bdrv_has_snapshot(BlockDriverState *bs)
1544{
1545 return (bs &&
1546 !bdrv_is_removable(bs) &&
1547 !bdrv_is_read_only(bs));
1548}
1549
1550static BlockDriverState *get_bs_snapshots(void)
1551{
1552 BlockDriverState *bs;
751c6a17 1553 DriveInfo *dinfo;
a672b469
AL
1554
1555 if (bs_snapshots)
1556 return bs_snapshots;
72cf2d4f 1557 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 1558 bs = dinfo->bdrv;
a672b469
AL
1559 if (bdrv_can_snapshot(bs))
1560 goto ok;
1561 }
1562 return NULL;
1563 ok:
1564 bs_snapshots = bs;
1565 return bs;
1566}
1567
1568static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1569 const char *name)
1570{
1571 QEMUSnapshotInfo *sn_tab, *sn;
1572 int nb_sns, i, ret;
1573
1574 ret = -ENOENT;
1575 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1576 if (nb_sns < 0)
1577 return ret;
1578 for(i = 0; i < nb_sns; i++) {
1579 sn = &sn_tab[i];
1580 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1581 *sn_info = *sn;
1582 ret = 0;
1583 break;
1584 }
1585 }
1586 qemu_free(sn_tab);
1587 return ret;
1588}
1589
cb499fb2
KW
1590/*
1591 * Deletes snapshots of a given name in all opened images.
1592 */
1593static int del_existing_snapshots(Monitor *mon, const char *name)
1594{
1595 BlockDriverState *bs;
1596 DriveInfo *dinfo;
1597 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1598 int ret;
1599
1600 QTAILQ_FOREACH(dinfo, &drives, next) {
1601 bs = dinfo->bdrv;
1602 if (bdrv_can_snapshot(bs) &&
1603 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1604 {
1605 ret = bdrv_snapshot_delete(bs, name);
1606 if (ret < 0) {
1607 monitor_printf(mon,
1608 "Error while deleting snapshot on '%s'\n",
1609 bdrv_get_device_name(bs));
1610 return -1;
1611 }
1612 }
1613 }
1614
1615 return 0;
1616}
1617
d54908a5 1618void do_savevm(Monitor *mon, const QDict *qdict)
a672b469 1619{
751c6a17 1620 DriveInfo *dinfo;
a672b469
AL
1621 BlockDriverState *bs, *bs1;
1622 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
cb499fb2 1623 int ret;
a672b469
AL
1624 QEMUFile *f;
1625 int saved_vm_running;
2d22b18f 1626 uint32_t vm_state_size;
a672b469
AL
1627#ifdef _WIN32
1628 struct _timeb tb;
1629#else
1630 struct timeval tv;
1631#endif
d54908a5 1632 const char *name = qdict_get_try_str(qdict, "name");
a672b469
AL
1633
1634 bs = get_bs_snapshots();
1635 if (!bs) {
376253ec 1636 monitor_printf(mon, "No block device can accept snapshots\n");
a672b469
AL
1637 return;
1638 }
1639
1640 /* ??? Should this occur after vm_stop? */
1641 qemu_aio_flush();
1642
1643 saved_vm_running = vm_running;
1644 vm_stop(0);
1645
cb499fb2 1646 memset(sn, 0, sizeof(*sn));
a672b469
AL
1647 if (name) {
1648 ret = bdrv_snapshot_find(bs, old_sn, name);
1649 if (ret >= 0) {
cb499fb2
KW
1650 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1651 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1652 } else {
a672b469 1653 pstrcpy(sn->name, sizeof(sn->name), name);
cb499fb2 1654 }
a672b469
AL
1655 }
1656
1657 /* fill auxiliary fields */
1658#ifdef _WIN32
1659 _ftime(&tb);
1660 sn->date_sec = tb.time;
1661 sn->date_nsec = tb.millitm * 1000000;
1662#else
1663 gettimeofday(&tv, NULL);
1664 sn->date_sec = tv.tv_sec;
1665 sn->date_nsec = tv.tv_usec * 1000;
1666#endif
1667 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1668
cb499fb2
KW
1669 /* Delete old snapshots of the same name */
1670 if (del_existing_snapshots(mon, name) < 0) {
1671 goto the_end;
1672 }
1673
a672b469 1674 /* save the VM state */
45566e9c 1675 f = qemu_fopen_bdrv(bs, 1);
a672b469 1676 if (!f) {
376253ec 1677 monitor_printf(mon, "Could not open VM state file\n");
a672b469
AL
1678 goto the_end;
1679 }
1680 ret = qemu_savevm_state(f);
2d22b18f 1681 vm_state_size = qemu_ftell(f);
a672b469
AL
1682 qemu_fclose(f);
1683 if (ret < 0) {
376253ec 1684 monitor_printf(mon, "Error %d while writing VM\n", ret);
a672b469
AL
1685 goto the_end;
1686 }
1687
1688 /* create the snapshots */
1689
72cf2d4f 1690 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 1691 bs1 = dinfo->bdrv;
a672b469 1692 if (bdrv_has_snapshot(bs1)) {
2d22b18f
AL
1693 /* Write VM state size only to the image that contains the state */
1694 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
a672b469
AL
1695 ret = bdrv_snapshot_create(bs1, sn);
1696 if (ret < 0) {
376253ec
AL
1697 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1698 bdrv_get_device_name(bs1));
a672b469
AL
1699 }
1700 }
1701 }
1702
1703 the_end:
1704 if (saved_vm_running)
1705 vm_start();
1706}
1707
05f2401e 1708int load_vmstate(Monitor *mon, const char *name)
a672b469 1709{
751c6a17 1710 DriveInfo *dinfo;
a672b469 1711 BlockDriverState *bs, *bs1;
2d22b18f 1712 QEMUSnapshotInfo sn;
a672b469 1713 QEMUFile *f;
751c6a17 1714 int ret;
a672b469
AL
1715
1716 bs = get_bs_snapshots();
1717 if (!bs) {
376253ec 1718 monitor_printf(mon, "No block device supports snapshots\n");
05f2401e 1719 return -EINVAL;
a672b469
AL
1720 }
1721
1722 /* Flush all IO requests so they don't interfere with the new state. */
1723 qemu_aio_flush();
1724
72cf2d4f 1725 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 1726 bs1 = dinfo->bdrv;
a672b469
AL
1727 if (bdrv_has_snapshot(bs1)) {
1728 ret = bdrv_snapshot_goto(bs1, name);
1729 if (ret < 0) {
1730 if (bs != bs1)
376253ec 1731 monitor_printf(mon, "Warning: ");
a672b469
AL
1732 switch(ret) {
1733 case -ENOTSUP:
376253ec
AL
1734 monitor_printf(mon,
1735 "Snapshots not supported on device '%s'\n",
1736 bdrv_get_device_name(bs1));
a672b469
AL
1737 break;
1738 case -ENOENT:
376253ec
AL
1739 monitor_printf(mon, "Could not find snapshot '%s' on "
1740 "device '%s'\n",
1741 name, bdrv_get_device_name(bs1));
a672b469
AL
1742 break;
1743 default:
376253ec
AL
1744 monitor_printf(mon, "Error %d while activating snapshot on"
1745 " '%s'\n", ret, bdrv_get_device_name(bs1));
a672b469
AL
1746 break;
1747 }
1748 /* fatal on snapshot block device */
1749 if (bs == bs1)
05f2401e 1750 return 0;
a672b469
AL
1751 }
1752 }
1753 }
1754
2d22b18f
AL
1755 /* Don't even try to load empty VM states */
1756 ret = bdrv_snapshot_find(bs, &sn, name);
1757 if ((ret >= 0) && (sn.vm_state_size == 0))
05f2401e 1758 return -EINVAL;
2d22b18f 1759
a672b469 1760 /* restore the VM state */
45566e9c 1761 f = qemu_fopen_bdrv(bs, 0);
a672b469 1762 if (!f) {
376253ec 1763 monitor_printf(mon, "Could not open VM state file\n");
05f2401e 1764 return -EINVAL;
a672b469
AL
1765 }
1766 ret = qemu_loadvm_state(f);
1767 qemu_fclose(f);
1768 if (ret < 0) {
376253ec 1769 monitor_printf(mon, "Error %d while loading VM state\n", ret);
05f2401e 1770 return ret;
a672b469 1771 }
05f2401e 1772 return 0;
7b630349
JQ
1773}
1774
d54908a5 1775void do_delvm(Monitor *mon, const QDict *qdict)
a672b469 1776{
751c6a17 1777 DriveInfo *dinfo;
a672b469 1778 BlockDriverState *bs, *bs1;
751c6a17 1779 int ret;
d54908a5 1780 const char *name = qdict_get_str(qdict, "name");
a672b469
AL
1781
1782 bs = get_bs_snapshots();
1783 if (!bs) {
376253ec 1784 monitor_printf(mon, "No block device supports snapshots\n");
a672b469
AL
1785 return;
1786 }
1787
72cf2d4f 1788 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 1789 bs1 = dinfo->bdrv;
a672b469
AL
1790 if (bdrv_has_snapshot(bs1)) {
1791 ret = bdrv_snapshot_delete(bs1, name);
1792 if (ret < 0) {
1793 if (ret == -ENOTSUP)
376253ec
AL
1794 monitor_printf(mon,
1795 "Snapshots not supported on device '%s'\n",
1796 bdrv_get_device_name(bs1));
a672b469 1797 else
376253ec
AL
1798 monitor_printf(mon, "Error %d while deleting snapshot on "
1799 "'%s'\n", ret, bdrv_get_device_name(bs1));
a672b469
AL
1800 }
1801 }
1802 }
1803}
1804
376253ec 1805void do_info_snapshots(Monitor *mon)
a672b469 1806{
751c6a17 1807 DriveInfo *dinfo;
a672b469
AL
1808 BlockDriverState *bs, *bs1;
1809 QEMUSnapshotInfo *sn_tab, *sn;
1810 int nb_sns, i;
1811 char buf[256];
1812
1813 bs = get_bs_snapshots();
1814 if (!bs) {
376253ec 1815 monitor_printf(mon, "No available block device supports snapshots\n");
a672b469
AL
1816 return;
1817 }
376253ec 1818 monitor_printf(mon, "Snapshot devices:");
72cf2d4f 1819 QTAILQ_FOREACH(dinfo, &drives, next) {
751c6a17 1820 bs1 = dinfo->bdrv;
a672b469
AL
1821 if (bdrv_has_snapshot(bs1)) {
1822 if (bs == bs1)
376253ec 1823 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
a672b469
AL
1824 }
1825 }
376253ec 1826 monitor_printf(mon, "\n");
a672b469
AL
1827
1828 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1829 if (nb_sns < 0) {
376253ec 1830 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
a672b469
AL
1831 return;
1832 }
376253ec
AL
1833 monitor_printf(mon, "Snapshot list (from %s):\n",
1834 bdrv_get_device_name(bs));
1835 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
a672b469
AL
1836 for(i = 0; i < nb_sns; i++) {
1837 sn = &sn_tab[i];
376253ec 1838 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
a672b469
AL
1839 }
1840 qemu_free(sn_tab);
1841}