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