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