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