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