blob: 3f912ddcf9919ca9060a6fb385d9ee398139f24a [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
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 */
aliguoria672b462008-11-11 21:33:36 +000024
blueswir1d40cdb12009-03-07 16:52:02 +000025#include "config-host.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include "qemu-common.h"
27#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060028#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/net.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/timer.h"
blueswir1511d2b12009-03-07 15:32:56 +000033#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010034#include "migration/migration.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
36#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000039#include "qmp-commands.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020040#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/bitops.h"
Orit Wasserman28085f72013-03-22 16:47:58 +020042#include "qemu/iov.h"
Wenchao Xiade08c602013-05-25 11:09:43 +080043#include "block/snapshot.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080044#include "block/qapi.h"
blueswir1511d2b12009-03-07 15:32:56 +000045
aliguoria672b462008-11-11 21:33:36 +000046#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000047
Nolan18995b92009-10-15 16:53:55 -070048#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040049#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070050#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,
aliguoria672b462008-11-11 21:33:36 +000056 uint8_t *mac_addr)
57{
Nolan18995b92009-10-15 16:53:55 -070058 /* 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 */
aliguoria672b462008-11-11 21:33:36 +000062
Nolan18995b92009-10-15 16:53:55 -070063 /* 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 */
aliguoria672b462008-11-11 21:33:36 +000073
Nolan18995b92009-10-15 16:53:55 -070074 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000076
Nolan18995b92009-10-15 16:53:55 -070077 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000078}
79
Mark McLoughlinf401ca22009-11-25 18:49:32 +000080static void qemu_announce_self_iter(NICState *nic, void *opaque)
81{
82 uint8_t buf[60];
83 int len;
84
85 len = announce_self_create(buf, nic->conf->macaddr.a);
86
Jason Wangb356f762013-01-30 19:12:22 +080087 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000088}
89
90
Gleb Natapoved8b3302009-05-21 17:17:44 +030091static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000092{
Gleb Natapoved8b3302009-05-21 17:17:44 +030093 static int count = SELF_ANNOUNCE_ROUNDS;
94 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000095
Mark McLoughlinf401ca22009-11-25 18:49:32 +000096 qemu_foreach_nic(qemu_announce_self_iter, NULL);
97
Nolan18995b92009-10-15 16:53:55 -070098 if (--count) {
99 /* delay 50ms, 150ms, 250ms, ... */
Alex Blighbc72ad62013-08-21 16:03:08 +0100100 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
Nolan18995b92009-10-15 16:53:55 -0700101 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300102 } else {
Alex Blighbc72ad62013-08-21 16:03:08 +0100103 timer_del(timer);
104 timer_free(timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300105 }
106}
107
108void qemu_announce_self(void)
109{
110 static QEMUTimer *timer;
Alex Blighbc72ad62013-08-21 16:03:08 +0100111 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300112 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000113}
114
115/***********************************************************/
116/* savevm/loadvm support */
117
118#define IO_BUF_SIZE 32768
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200119#define MAX_IOV_SIZE MIN(IOV_MAX, 64)
aliguoria672b462008-11-11 21:33:36 +0000120
121struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200122 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000123 void *opaque;
aliguoria672b462008-11-11 21:33:36 +0000124
Paolo Bonzini1964a392013-02-22 17:36:45 +0100125 int64_t bytes_xfer;
126 int64_t xfer_limit;
127
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100128 int64_t pos; /* start of buffer when writing, end of buffer
129 when reading */
aliguoria672b462008-11-11 21:33:36 +0000130 int buf_index;
131 int buf_size; /* 0 when writing */
132 uint8_t buf[IO_BUF_SIZE];
133
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200134 struct iovec iov[MAX_IOV_SIZE];
135 unsigned int iovcnt;
136
Juan Quintela3961b4d2011-10-05 01:05:21 +0200137 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000138};
139
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200140typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000141{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200142 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000143 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200144} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000145
146typedef struct QEMUFileSocket
147{
148 int fd;
149 QEMUFile *file;
150} QEMUFileSocket;
151
Kevin Wolf05fcc842013-04-05 21:27:54 +0200152static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
153 int64_t pos)
Orit Wasserman28085f72013-03-22 16:47:58 +0200154{
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
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200166static int socket_get_fd(void *opaque)
167{
168 QEMUFileSocket *s = opaque;
169
170 return s->fd;
171}
172
aliguoria672b462008-11-11 21:33:36 +0000173static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
174{
175 QEMUFileSocket *s = opaque;
176 ssize_t len;
177
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200178 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000179 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200180 if (len != -1) {
181 break;
182 }
183 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100184 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200185 } else if (socket_error() != EINTR) {
186 break;
187 }
188 }
aliguoria672b462008-11-11 21:33:36 +0000189
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200190 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000191 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200192 }
aliguoria672b462008-11-11 21:33:36 +0000193 return len;
194}
195
196static int socket_close(void *opaque)
197{
198 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200199 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500200 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000201 return 0;
202}
203
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200204static int stdio_get_fd(void *opaque)
205{
206 QEMUFileStdio *s = opaque;
207
208 return fileno(s->stdio_file);
209}
210
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200211static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000212{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200213 QEMUFileStdio *s = opaque;
214 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000215}
216
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200217static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000218{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200219 QEMUFileStdio *s = opaque;
220 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300221 int bytes;
222
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200223 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300224 clearerr(fp);
225 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200226 if (bytes != 0 || !ferror(fp)) {
227 break;
228 }
229 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100230 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200231 } else if (errno != EINTR) {
232 break;
233 }
234 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300235 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000236}
237
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200238static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000239{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200240 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500241 int ret;
242 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200243 if (ret == -1) {
244 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100245 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
246 /* close succeeded, but non-zero exit code: */
247 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200248 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500249 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500250 return ret;
aliguoria672b462008-11-11 21:33:36 +0000251}
252
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200253static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000254{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200255 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200256 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100257
Orit Wassermancb88aa82013-03-22 16:48:01 +0200258 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100259 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 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200275 if (fclose(s->stdio_file) == EOF) {
276 ret = -errno;
277 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500278 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200279 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200280}
aliguoria672b462008-11-11 21:33:36 +0000281
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200282static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200283 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200284 .get_buffer = stdio_get_buffer,
285 .close = stdio_pclose
286};
287
288static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200289 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200290 .put_buffer = stdio_put_buffer,
291 .close = stdio_pclose
292};
293
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100294QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200295{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100296 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200297 QEMUFileStdio *s;
298
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200299 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
300 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100301 return NULL;
302 }
303
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200304 stdio_file = popen(command, mode);
305 if (stdio_file == NULL) {
aliguoria672b462008-11-11 21:33:36 +0000306 return NULL;
307 }
308
Anthony Liguori7267c092011-08-20 22:09:37 -0500309 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000310
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200311 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000312
313 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200314 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000315 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200316 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000317 }
aliguoria672b462008-11-11 21:33:36 +0000318 return s->file;
319}
320
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200321static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200322 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200323 .get_buffer = stdio_get_buffer,
324 .close = stdio_fclose
325};
326
327static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200328 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200329 .put_buffer = stdio_put_buffer,
330 .close = stdio_fclose
331};
332
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100333static 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
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200418QEMUFile *qemu_fdopen(int fd, const char *mode)
419{
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100420 QEMUFileSocket *s;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200421
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
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100429 s = g_malloc0(sizeof(QEMUFileSocket));
430 s->fd = fd;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200431
432 if(mode[0] == 'r') {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100433 s->file = qemu_fopen_ops(s, &unix_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200434 } else {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100435 s->file = qemu_fopen_ops(s, &unix_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200436 }
437 return s->file;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200438}
439
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200440static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200441 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200442 .get_buffer = socket_get_buffer,
443 .close = socket_close
444};
445
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100446static const QEMUFileOps socket_write_ops = {
447 .get_fd = socket_get_fd,
Orit Wasserman28085f72013-03-22 16:47:58 +0200448 .writev_buffer = socket_writev_buffer,
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100449 .close = socket_close
450};
451
Michael R. Hinesbc1256f2013-06-25 21:35:31 -0400452bool qemu_file_mode_is_not_valid(const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000453{
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100454 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");
Michael R. Hinesbc1256f2013-06-25 21:35:31 -0400458 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)) {
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100469 return NULL;
470 }
471
Stefan Weil4f080052013-06-16 13:33:05 +0200472 s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000473 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100474 if (mode[0] == 'w') {
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100475 qemu_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100476 s->file = qemu_fopen_ops(s, &socket_write_ops);
477 } else {
478 s->file = qemu_fopen_ops(s, &socket_read_ops);
479 }
aliguoria672b462008-11-11 21:33:36 +0000480 return s->file;
481}
482
aliguoria672b462008-11-11 21:33:36 +0000483QEMUFile *qemu_fopen(const char *filename, const char *mode)
484{
485 QEMUFileStdio *s;
486
Michael R. Hinesbc1256f2013-06-25 21:35:31 -0400487 if (qemu_file_mode_is_not_valid(mode)) {
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200488 return NULL;
489 }
490
Anthony Liguori7267c092011-08-20 22:09:37 -0500491 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000492
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200493 s->stdio_file = fopen(filename, mode);
494 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000495 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200496
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200497 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200498 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200499 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200500 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200501 }
502 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000503fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500504 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000505 return NULL;
506}
507
Kevin Wolf05fcc842013-04-05 21:27:54 +0200508static 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
aliguori178e08a2009-04-05 19:10:55 +0000523static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000524 int64_t pos, int size)
525{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200526 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000527 return size;
528}
529
aliguori178e08a2009-04-05 19:10:55 +0000530static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000531{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200532 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000533}
534
535static int bdrv_fclose(void *opaque)
536{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200537 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000538}
539
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200540static const QEMUFileOps bdrv_read_ops = {
541 .get_buffer = block_get_buffer,
542 .close = bdrv_fclose
543};
544
545static const QEMUFileOps bdrv_write_ops = {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200546 .put_buffer = block_put_buffer,
547 .writev_buffer = block_writev_buffer,
548 .close = bdrv_fclose
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200549};
550
Christoph Hellwig45566e92009-07-10 23:11:57 +0200551static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000552{
aliguoria672b462008-11-11 21:33:36 +0000553 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200554 return qemu_fopen_ops(bs, &bdrv_write_ops);
555 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000556}
557
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200558QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000559{
560 QEMUFile *f;
561
Anthony Liguori7267c092011-08-20 22:09:37 -0500562 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000563
564 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200565 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000566 return f;
567}
568
Lei Li675fd0a2013-09-04 17:02:34 +0800569/*
570 * Get last error for stream f
571 *
572 * Return negative error value if there has been an error on previous
573 * operations, return 0 if no error happened.
574 *
575 */
Juan Quintela624b9cc2011-10-05 01:02:52 +0200576int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000577{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200578 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000579}
580
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100581static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000582{
Juan Quintelaafe41932013-01-14 13:36:28 +0100583 if (f->last_error == 0) {
584 f->last_error = ret;
585 }
aliguori4dabe242009-04-05 19:30:51 +0000586}
587
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200588static inline bool qemu_file_is_writable(QEMUFile *f)
589{
590 return f->ops->writev_buffer || f->ops->put_buffer;
591}
592
Orit Wassermancb88aa82013-03-22 16:48:01 +0200593/**
594 * Flushes QEMUFile buffer
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200595 *
Orit Wassermancb88aa82013-03-22 16:48:01 +0200596 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
597 * put_buffer ops.
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200598 */
Michael R. Hinesbe903b22013-06-25 21:35:32 -0400599void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000600{
Orit Wassermancb88aa82013-03-22 16:48:01 +0200601 ssize_t ret = 0;
Juan Quintela7311bea2012-08-29 19:08:59 +0200602
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200603 if (!qemu_file_is_writable(f)) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100604 return;
605 }
Orit Wassermancb88aa82013-03-22 16:48:01 +0200606
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200607 if (f->ops->writev_buffer) {
608 if (f->iovcnt > 0) {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200609 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
Juan Quintela7311bea2012-08-29 19:08:59 +0200610 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200611 } else {
612 if (f->buf_index > 0) {
613 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200614 }
aliguoria672b462008-11-11 21:33:36 +0000615 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200616 if (ret >= 0) {
617 f->pos += ret;
618 }
619 f->buf_index = 0;
620 f->iovcnt = 0;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100621 if (ret < 0) {
622 qemu_file_set_error(f, ret);
623 }
aliguoria672b462008-11-11 21:33:36 +0000624}
625
Michael R. Hines43487c62013-06-25 21:35:35 -0400626void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
627{
628 int ret = 0;
629
630 if (f->ops->before_ram_iterate) {
631 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
632 if (ret < 0) {
633 qemu_file_set_error(f, ret);
634 }
635 }
636}
637
638void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
639{
640 int ret = 0;
641
642 if (f->ops->after_ram_iterate) {
643 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
644 if (ret < 0) {
645 qemu_file_set_error(f, ret);
646 }
647 }
648}
649
650void ram_control_load_hook(QEMUFile *f, uint64_t flags)
651{
Lei Lic77a5f22013-09-04 17:02:35 +0800652 int ret = -EINVAL;
Michael R. Hines43487c62013-06-25 21:35:35 -0400653
654 if (f->ops->hook_ram_load) {
655 ret = f->ops->hook_ram_load(f, f->opaque, flags);
656 if (ret < 0) {
657 qemu_file_set_error(f, ret);
658 }
659 } else {
660 qemu_file_set_error(f, ret);
661 }
662}
663
664size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
665 ram_addr_t offset, size_t size, int *bytes_sent)
666{
667 if (f->ops->save_page) {
668 int ret = f->ops->save_page(f, f->opaque, block_offset,
669 offset, size, bytes_sent);
670
671 if (ret != RAM_SAVE_CONTROL_DELAYED) {
Michael R. Hinesde7b6852013-07-22 10:01:52 -0400672 if (bytes_sent && *bytes_sent > 0) {
Michael R. Hines43487c62013-06-25 21:35:35 -0400673 qemu_update_position(f, *bytes_sent);
674 } else if (ret < 0) {
675 qemu_file_set_error(f, ret);
676 }
677 }
678
679 return ret;
680 }
681
682 return RAM_SAVE_CONTROL_NOT_SUPP;
683}
684
aliguoria672b462008-11-11 21:33:36 +0000685static void qemu_fill_buffer(QEMUFile *f)
686{
687 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200688 int pending;
aliguoria672b462008-11-11 21:33:36 +0000689
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200690 assert(!qemu_file_is_writable(f));
aliguoria672b462008-11-11 21:33:36 +0000691
Juan Quintela0046c452011-09-30 19:28:45 +0200692 pending = f->buf_size - f->buf_index;
693 if (pending > 0) {
694 memmove(f->buf, f->buf + f->buf_index, pending);
695 }
696 f->buf_index = 0;
697 f->buf_size = pending;
698
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100699 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200700 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000701 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200702 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100703 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200704 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200705 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000706 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200707 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000708}
709
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200710int qemu_get_fd(QEMUFile *f)
711{
712 if (f->ops->get_fd) {
713 return f->ops->get_fd(f->opaque);
714 }
715 return -1;
716}
717
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400718void qemu_update_position(QEMUFile *f, size_t size)
719{
720 f->pos += size;
721}
722
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200723/** Closes the file
724 *
725 * Returns negative error value if any error happened on previous operations or
726 * while closing the file. Returns 0 or positive number on success.
727 *
728 * The meaning of return value on success depends on the specific backend
729 * being used.
730 */
731int qemu_fclose(QEMUFile *f)
732{
Juan Quintela29eee862012-08-29 19:14:54 +0200733 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100734 qemu_fflush(f);
735 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200736
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200737 if (f->ops->close) {
738 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200739 if (ret >= 0) {
740 ret = ret2;
741 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200742 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200743 /* If any error was spotted before closing, we should report it
744 * instead of the close() return value.
745 */
746 if (f->last_error) {
747 ret = f->last_error;
748 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500749 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000750 return ret;
751}
752
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200753static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
754{
755 /* check for adjacent buffer and coalesce them */
756 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
757 f->iov[f->iovcnt - 1].iov_len) {
758 f->iov[f->iovcnt - 1].iov_len += size;
759 } else {
760 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
761 f->iov[f->iovcnt++].iov_len = size;
762 }
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200763
Paolo Bonzini4d117242013-04-08 13:29:57 +0200764 if (f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200765 qemu_fflush(f);
766 }
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200767}
768
Orit Wasserman6181ec22013-03-22 16:48:02 +0200769void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
770{
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200771 if (!f->ops->writev_buffer) {
772 qemu_put_buffer(f, buf, size);
773 return;
774 }
775
Orit Wasserman6181ec22013-03-22 16:48:02 +0200776 if (f->last_error) {
777 return;
778 }
779
Orit Wasserman6181ec22013-03-22 16:48:02 +0200780 f->bytes_xfer += size;
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200781 add_to_iovec(f, buf, size);
Orit Wasserman6181ec22013-03-22 16:48:02 +0200782}
783
aliguoria672b462008-11-11 21:33:36 +0000784void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
785{
786 int l;
787
Juan Quintelac10682c2012-08-29 19:43:39 +0200788 if (f->last_error) {
789 return;
790 }
791
Juan Quintelac10682c2012-08-29 19:43:39 +0200792 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000793 l = IO_BUF_SIZE - f->buf_index;
794 if (l > size)
795 l = size;
796 memcpy(f->buf + f->buf_index, buf, l);
Wangting (Kathy)8e867292013-11-19 05:53:45 +0000797 f->bytes_xfer += l;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200798 if (f->ops->writev_buffer) {
799 add_to_iovec(f, f->buf + f->buf_index, l);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200800 }
801 f->buf_index += l;
802 if (f->buf_index == IO_BUF_SIZE) {
803 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200804 }
Orit Wasserman6181ec22013-03-22 16:48:02 +0200805 if (qemu_file_get_error(f)) {
806 break;
807 }
aliguoria672b462008-11-11 21:33:36 +0000808 buf += l;
809 size -= l;
aliguoria672b462008-11-11 21:33:36 +0000810 }
811}
812
813void qemu_put_byte(QEMUFile *f, int v)
814{
Juan Quintelac10682c2012-08-29 19:43:39 +0200815 if (f->last_error) {
816 return;
817 }
818
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200819 f->buf[f->buf_index] = v;
Orit Wasserman7d8a30b2013-03-22 16:47:59 +0200820 f->bytes_xfer++;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200821 if (f->ops->writev_buffer) {
822 add_to_iovec(f, f->buf + f->buf_index, 1);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200823 }
824 f->buf_index++;
825 if (f->buf_index == IO_BUF_SIZE) {
826 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200827 }
aliguoria672b462008-11-11 21:33:36 +0000828}
829
Juan Quintelac6380722011-10-04 15:28:31 +0200830static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000831{
Juan Quintelac6380722011-10-04 15:28:31 +0200832 if (f->buf_index + size <= f->buf_size) {
833 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000834 }
aliguoria672b462008-11-11 21:33:36 +0000835}
836
Juan Quintelac6380722011-10-04 15:28:31 +0200837static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200838{
Juan Quintelac6380722011-10-04 15:28:31 +0200839 int pending;
840 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200841
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200842 assert(!qemu_file_is_writable(f));
Juan Quintela811814b2010-07-26 21:38:43 +0200843
Juan Quintelac6380722011-10-04 15:28:31 +0200844 index = f->buf_index + offset;
845 pending = f->buf_size - index;
846 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200847 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200848 index = f->buf_index + offset;
849 pending = f->buf_size - index;
850 }
851
852 if (pending <= 0) {
853 return 0;
854 }
855 if (size > pending) {
856 size = pending;
857 }
858
859 memcpy(buf, f->buf + index, size);
860 return size;
861}
862
863int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
864{
865 int pending = size;
866 int done = 0;
867
868 while (pending > 0) {
869 int res;
870
871 res = qemu_peek_buffer(f, buf, pending, 0);
872 if (res == 0) {
873 return done;
874 }
875 qemu_file_skip(f, res);
876 buf += res;
877 pending -= res;
878 done += res;
879 }
880 return done;
881}
882
883static int qemu_peek_byte(QEMUFile *f, int offset)
884{
885 int index = f->buf_index + offset;
886
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200887 assert(!qemu_file_is_writable(f));
Juan Quintelac6380722011-10-04 15:28:31 +0200888
889 if (index >= f->buf_size) {
890 qemu_fill_buffer(f);
891 index = f->buf_index + offset;
892 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200893 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200894 }
Juan Quintela811814b2010-07-26 21:38:43 +0200895 }
Juan Quintelac6380722011-10-04 15:28:31 +0200896 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200897}
898
aliguoria672b462008-11-11 21:33:36 +0000899int qemu_get_byte(QEMUFile *f)
900{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200901 int result;
aliguoria672b462008-11-11 21:33:36 +0000902
Juan Quintelac6380722011-10-04 15:28:31 +0200903 result = qemu_peek_byte(f, 0);
904 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200905 return result;
aliguoria672b462008-11-11 21:33:36 +0000906}
907
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100908int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000909{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100910 qemu_fflush(f);
911 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000912}
913
aliguoria672b462008-11-11 21:33:36 +0000914int qemu_file_rate_limit(QEMUFile *f)
915{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100916 if (qemu_file_get_error(f)) {
917 return 1;
918 }
919 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
920 return 1;
921 }
aliguoria672b462008-11-11 21:33:36 +0000922 return 0;
923}
924
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200925int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200926{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100927 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200928}
929
Paolo Bonzini1964a392013-02-22 17:36:45 +0100930void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400931{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100932 f->xfer_limit = limit;
933}
Glauber Costa19629532009-05-20 18:26:57 -0400934
Paolo Bonzini1964a392013-02-22 17:36:45 +0100935void qemu_file_reset_rate_limit(QEMUFile *f)
936{
937 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400938}
939
aliguoria672b462008-11-11 21:33:36 +0000940void qemu_put_be16(QEMUFile *f, unsigned int v)
941{
942 qemu_put_byte(f, v >> 8);
943 qemu_put_byte(f, v);
944}
945
946void qemu_put_be32(QEMUFile *f, unsigned int v)
947{
948 qemu_put_byte(f, v >> 24);
949 qemu_put_byte(f, v >> 16);
950 qemu_put_byte(f, v >> 8);
951 qemu_put_byte(f, v);
952}
953
954void qemu_put_be64(QEMUFile *f, uint64_t v)
955{
956 qemu_put_be32(f, v >> 32);
957 qemu_put_be32(f, v);
958}
959
960unsigned int qemu_get_be16(QEMUFile *f)
961{
962 unsigned int v;
963 v = qemu_get_byte(f) << 8;
964 v |= qemu_get_byte(f);
965 return v;
966}
967
968unsigned int qemu_get_be32(QEMUFile *f)
969{
970 unsigned int v;
971 v = qemu_get_byte(f) << 24;
972 v |= qemu_get_byte(f) << 16;
973 v |= qemu_get_byte(f) << 8;
974 v |= qemu_get_byte(f);
975 return v;
976}
977
978uint64_t qemu_get_be64(QEMUFile *f)
979{
980 uint64_t v;
981 v = (uint64_t)qemu_get_be32(f) << 32;
982 v |= qemu_get_be32(f);
983 return v;
984}
985
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200986
987/* timer */
988
Alex Bligh40daca52013-08-21 16:03:02 +0100989void timer_put(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200990{
991 uint64_t expire_time;
992
Alex Blighe93379b2013-08-21 16:02:39 +0100993 expire_time = timer_expire_time_ns(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200994 qemu_put_be64(f, expire_time);
995}
996
Alex Bligh40daca52013-08-21 16:03:02 +0100997void timer_get(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200998{
999 uint64_t expire_time;
1000
1001 expire_time = qemu_get_be64(f);
1002 if (expire_time != -1) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001003 timer_mod_ns(ts, expire_time);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02001004 } else {
Alex Blighbc72ad62013-08-21 16:03:08 +01001005 timer_del(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02001006 }
1007}
1008
1009
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +01001010/* bool */
1011
1012static int get_bool(QEMUFile *f, void *pv, size_t size)
1013{
1014 bool *v = pv;
1015 *v = qemu_get_byte(f);
1016 return 0;
1017}
1018
1019static void put_bool(QEMUFile *f, void *pv, size_t size)
1020{
1021 bool *v = pv;
1022 qemu_put_byte(f, *v);
1023}
1024
1025const VMStateInfo vmstate_info_bool = {
1026 .name = "bool",
1027 .get = get_bool,
1028 .put = put_bool,
1029};
1030
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001031/* 8 bit int */
1032
1033static int get_int8(QEMUFile *f, void *pv, size_t size)
1034{
1035 int8_t *v = pv;
1036 qemu_get_s8s(f, v);
1037 return 0;
1038}
1039
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001040static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001041{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001042 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001043 qemu_put_s8s(f, v);
1044}
1045
1046const VMStateInfo vmstate_info_int8 = {
1047 .name = "int8",
1048 .get = get_int8,
1049 .put = put_int8,
1050};
1051
1052/* 16 bit int */
1053
1054static int get_int16(QEMUFile *f, void *pv, size_t size)
1055{
1056 int16_t *v = pv;
1057 qemu_get_sbe16s(f, v);
1058 return 0;
1059}
1060
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001061static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001062{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001063 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001064 qemu_put_sbe16s(f, v);
1065}
1066
1067const VMStateInfo vmstate_info_int16 = {
1068 .name = "int16",
1069 .get = get_int16,
1070 .put = put_int16,
1071};
1072
1073/* 32 bit int */
1074
1075static int get_int32(QEMUFile *f, void *pv, size_t size)
1076{
1077 int32_t *v = pv;
1078 qemu_get_sbe32s(f, v);
1079 return 0;
1080}
1081
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001082static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001083{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001084 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001085 qemu_put_sbe32s(f, v);
1086}
1087
1088const VMStateInfo vmstate_info_int32 = {
1089 .name = "int32",
1090 .get = get_int32,
1091 .put = put_int32,
1092};
1093
Juan Quintela82501662009-08-20 19:42:32 +02001094/* 32 bit int. See that the received value is the same than the one
1095 in the field */
1096
1097static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1098{
1099 int32_t *v = pv;
1100 int32_t v2;
1101 qemu_get_sbe32s(f, &v2);
1102
1103 if (*v == v2)
1104 return 0;
1105 return -EINVAL;
1106}
1107
1108const VMStateInfo vmstate_info_int32_equal = {
1109 .name = "int32 equal",
1110 .get = get_int32_equal,
1111 .put = put_int32,
1112};
1113
Juan Quintela0a031e02009-08-20 19:42:37 +02001114/* 32 bit int. See that the received value is the less or the same
1115 than the one in the field */
1116
1117static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1118{
1119 int32_t *old = pv;
1120 int32_t new;
1121 qemu_get_sbe32s(f, &new);
1122
1123 if (*old <= new)
1124 return 0;
1125 return -EINVAL;
1126}
1127
1128const VMStateInfo vmstate_info_int32_le = {
1129 .name = "int32 equal",
1130 .get = get_int32_le,
1131 .put = put_int32,
1132};
1133
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001134/* 64 bit int */
1135
1136static int get_int64(QEMUFile *f, void *pv, size_t size)
1137{
1138 int64_t *v = pv;
1139 qemu_get_sbe64s(f, v);
1140 return 0;
1141}
1142
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001143static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001144{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001145 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001146 qemu_put_sbe64s(f, v);
1147}
1148
1149const VMStateInfo vmstate_info_int64 = {
1150 .name = "int64",
1151 .get = get_int64,
1152 .put = put_int64,
1153};
1154
1155/* 8 bit unsigned int */
1156
1157static int get_uint8(QEMUFile *f, void *pv, size_t size)
1158{
1159 uint8_t *v = pv;
1160 qemu_get_8s(f, v);
1161 return 0;
1162}
1163
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001164static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001165{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001166 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001167 qemu_put_8s(f, v);
1168}
1169
1170const VMStateInfo vmstate_info_uint8 = {
1171 .name = "uint8",
1172 .get = get_uint8,
1173 .put = put_uint8,
1174};
1175
1176/* 16 bit unsigned int */
1177
1178static int get_uint16(QEMUFile *f, void *pv, size_t size)
1179{
1180 uint16_t *v = pv;
1181 qemu_get_be16s(f, v);
1182 return 0;
1183}
1184
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001185static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001186{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001187 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001188 qemu_put_be16s(f, v);
1189}
1190
1191const VMStateInfo vmstate_info_uint16 = {
1192 .name = "uint16",
1193 .get = get_uint16,
1194 .put = put_uint16,
1195};
1196
1197/* 32 bit unsigned int */
1198
1199static int get_uint32(QEMUFile *f, void *pv, size_t size)
1200{
1201 uint32_t *v = pv;
1202 qemu_get_be32s(f, v);
1203 return 0;
1204}
1205
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001206static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001207{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001208 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001209 qemu_put_be32s(f, v);
1210}
1211
1212const VMStateInfo vmstate_info_uint32 = {
1213 .name = "uint32",
1214 .get = get_uint32,
1215 .put = put_uint32,
1216};
1217
Juan Quintela9122a8f2011-03-10 12:33:48 +01001218/* 32 bit uint. See that the received value is the same than the one
1219 in the field */
1220
1221static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1222{
1223 uint32_t *v = pv;
1224 uint32_t v2;
1225 qemu_get_be32s(f, &v2);
1226
1227 if (*v == v2) {
1228 return 0;
1229 }
1230 return -EINVAL;
1231}
1232
1233const VMStateInfo vmstate_info_uint32_equal = {
1234 .name = "uint32 equal",
1235 .get = get_uint32_equal,
1236 .put = put_uint32,
1237};
1238
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001239/* 64 bit unsigned int */
1240
1241static int get_uint64(QEMUFile *f, void *pv, size_t size)
1242{
1243 uint64_t *v = pv;
1244 qemu_get_be64s(f, v);
1245 return 0;
1246}
1247
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001248static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001249{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001250 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001251 qemu_put_be64s(f, v);
1252}
1253
1254const VMStateInfo vmstate_info_uint64 = {
1255 .name = "uint64",
1256 .get = get_uint64,
1257 .put = put_uint64,
1258};
1259
David Gibsone344b8a2013-03-12 14:06:00 +11001260/* 64 bit unsigned int. See that the received value is the same than the one
1261 in the field */
1262
1263static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1264{
1265 uint64_t *v = pv;
1266 uint64_t v2;
1267 qemu_get_be64s(f, &v2);
1268
1269 if (*v == v2) {
1270 return 0;
1271 }
1272 return -EINVAL;
1273}
1274
1275const VMStateInfo vmstate_info_uint64_equal = {
1276 .name = "int64 equal",
1277 .get = get_uint64_equal,
1278 .put = put_uint64,
1279};
1280
Juan Quintela80cd83e2009-09-10 03:04:36 +02001281/* 8 bit int. See that the received value is the same than the one
1282 in the field */
1283
1284static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1285{
1286 uint8_t *v = pv;
1287 uint8_t v2;
1288 qemu_get_8s(f, &v2);
1289
1290 if (*v == v2)
1291 return 0;
1292 return -EINVAL;
1293}
1294
1295const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001296 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001297 .get = get_uint8_equal,
1298 .put = put_uint8,
1299};
1300
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001301/* 16 bit unsigned int int. See that the received value is the same than the one
1302 in the field */
1303
1304static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1305{
1306 uint16_t *v = pv;
1307 uint16_t v2;
1308 qemu_get_be16s(f, &v2);
1309
1310 if (*v == v2)
1311 return 0;
1312 return -EINVAL;
1313}
1314
1315const VMStateInfo vmstate_info_uint16_equal = {
1316 .name = "uint16 equal",
1317 .get = get_uint16_equal,
1318 .put = put_uint16,
1319};
1320
David Gibson213945e2013-03-12 14:06:02 +11001321/* floating point */
1322
1323static int get_float64(QEMUFile *f, void *pv, size_t size)
1324{
1325 float64 *v = pv;
1326
1327 *v = make_float64(qemu_get_be64(f));
1328 return 0;
1329}
1330
1331static void put_float64(QEMUFile *f, void *pv, size_t size)
1332{
1333 uint64_t *v = pv;
1334
1335 qemu_put_be64(f, float64_val(*v));
1336}
1337
1338const VMStateInfo vmstate_info_float64 = {
1339 .name = "float64",
1340 .get = get_float64,
1341 .put = put_float64,
1342};
1343
Juan Quinteladde04632009-08-20 19:42:26 +02001344/* timers */
1345
1346static int get_timer(QEMUFile *f, void *pv, size_t size)
1347{
1348 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +01001349 timer_get(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +02001350 return 0;
1351}
1352
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001353static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001354{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001355 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +01001356 timer_put(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +02001357}
1358
1359const VMStateInfo vmstate_info_timer = {
1360 .name = "timer",
1361 .get = get_timer,
1362 .put = put_timer,
1363};
1364
Juan Quintela6f67c502009-08-20 19:42:35 +02001365/* uint8_t buffers */
1366
1367static int get_buffer(QEMUFile *f, void *pv, size_t size)
1368{
1369 uint8_t *v = pv;
1370 qemu_get_buffer(f, v, size);
1371 return 0;
1372}
1373
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001374static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001375{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001376 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001377 qemu_put_buffer(f, v, size);
1378}
1379
1380const VMStateInfo vmstate_info_buffer = {
1381 .name = "buffer",
1382 .get = get_buffer,
1383 .put = put_buffer,
1384};
1385
Juan Quintela76507c72009-10-19 15:46:28 +02001386/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001387 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001388
1389static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1390{
Jan Kiszka21174c32009-12-02 12:36:35 +01001391 uint8_t buf[1024];
1392 int block_len;
1393
1394 while (size > 0) {
1395 block_len = MIN(sizeof(buf), size);
1396 size -= block_len;
1397 qemu_get_buffer(f, buf, block_len);
1398 }
1399 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001400}
1401
1402static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1403{
Jan Kiszka21174c32009-12-02 12:36:35 +01001404 static const uint8_t buf[1024];
1405 int block_len;
1406
1407 while (size > 0) {
1408 block_len = MIN(sizeof(buf), size);
1409 size -= block_len;
1410 qemu_put_buffer(f, buf, block_len);
1411 }
Juan Quintela76507c72009-10-19 15:46:28 +02001412}
1413
1414const VMStateInfo vmstate_info_unused_buffer = {
1415 .name = "unused_buffer",
1416 .get = get_unused_buffer,
1417 .put = put_unused_buffer,
1418};
1419
Peter Maydell08e99e22012-10-30 07:45:12 +00001420/* bitmaps (as defined by bitmap.h). Note that size here is the size
1421 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1422 * bit words with the bits in big endian order. The in-memory format
1423 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1424 */
1425/* This is the number of 64 bit words sent over the wire */
1426#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1427static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1428{
1429 unsigned long *bmp = pv;
1430 int i, idx = 0;
1431 for (i = 0; i < BITS_TO_U64S(size); i++) {
1432 uint64_t w = qemu_get_be64(f);
1433 bmp[idx++] = w;
1434 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1435 bmp[idx++] = w >> 32;
1436 }
1437 }
1438 return 0;
1439}
1440
1441static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1442{
1443 unsigned long *bmp = pv;
1444 int i, idx = 0;
1445 for (i = 0; i < BITS_TO_U64S(size); i++) {
1446 uint64_t w = bmp[idx++];
1447 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1448 w |= ((uint64_t)bmp[idx++]) << 32;
1449 }
1450 qemu_put_be64(f, w);
1451 }
1452}
1453
1454const VMStateInfo vmstate_info_bitmap = {
1455 .name = "bitmap",
1456 .get = get_bitmap,
1457 .put = put_bitmap,
1458};
1459
Alex Williamson7685ee62010-06-25 11:09:14 -06001460typedef struct CompatEntry {
1461 char idstr[256];
1462 int instance_id;
1463} CompatEntry;
1464
aliguoria672b462008-11-11 21:33:36 +00001465typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001466 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001467 char idstr[256];
1468 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001469 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001470 int version_id;
1471 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001472 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001473 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001474 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001475 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001476 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001477 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001478} SaveStateEntry;
1479
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001480
Blue Swirl72cf2d42009-09-12 07:36:22 +00001481static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1482 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001483static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001484
Juan Quintela8718e992009-09-01 02:12:31 +02001485static int calculate_new_instance_id(const char *idstr)
1486{
1487 SaveStateEntry *se;
1488 int instance_id = 0;
1489
Blue Swirl72cf2d42009-09-12 07:36:22 +00001490 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001491 if (strcmp(idstr, se->idstr) == 0
1492 && instance_id <= se->instance_id) {
1493 instance_id = se->instance_id + 1;
1494 }
1495 }
1496 return instance_id;
1497}
1498
Alex Williamson7685ee62010-06-25 11:09:14 -06001499static int calculate_compat_instance_id(const char *idstr)
1500{
1501 SaveStateEntry *se;
1502 int instance_id = 0;
1503
1504 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1505 if (!se->compat)
1506 continue;
1507
1508 if (strcmp(idstr, se->compat->idstr) == 0
1509 && instance_id <= se->compat->instance_id) {
1510 instance_id = se->compat->instance_id + 1;
1511 }
1512 }
1513 return instance_id;
1514}
1515
aliguoria672b462008-11-11 21:33:36 +00001516/* TODO: Individual devices generally have very little idea about the rest
1517 of the system, so instance_id should be removed/replaced.
1518 Meanwhile pass -1 as instance_id if you do not already have a clearly
1519 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001520int register_savevm_live(DeviceState *dev,
1521 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001522 int instance_id,
1523 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001524 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001525 void *opaque)
1526{
Juan Quintela8718e992009-09-01 02:12:31 +02001527 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001528
Anthony Liguori7267c092011-08-20 22:09:37 -05001529 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001530 se->version_id = version_id;
1531 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001532 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001533 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001534 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001535 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001536 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001537 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001538 se->is_ram = 1;
1539 }
aliguoria672b462008-11-11 21:33:36 +00001540
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001541 if (dev) {
1542 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001543 if (id) {
1544 pstrcpy(se->idstr, sizeof(se->idstr), id);
1545 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001546 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001547
Anthony Liguori7267c092011-08-20 22:09:37 -05001548 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001549 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1550 se->compat->instance_id = instance_id == -1 ?
1551 calculate_compat_instance_id(idstr) : instance_id;
1552 instance_id = -1;
1553 }
1554 }
1555 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1556
Juan Quintela8718e992009-09-01 02:12:31 +02001557 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001558 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001559 } else {
1560 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001561 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001562 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001563 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001564 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001565 return 0;
1566}
1567
Alex Williamson0be71e32010-06-25 11:09:07 -06001568int register_savevm(DeviceState *dev,
1569 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001570 int instance_id,
1571 int version_id,
1572 SaveStateHandler *save_state,
1573 LoadStateHandler *load_state,
1574 void *opaque)
1575{
Juan Quintela7908c782012-06-26 18:46:10 +02001576 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1577 ops->save_state = save_state;
1578 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001579 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001580 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001581}
1582
Alex Williamson0be71e32010-06-25 11:09:07 -06001583void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001584{
Juan Quintela8718e992009-09-01 02:12:31 +02001585 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001586 char id[256] = "";
1587
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001588 if (dev) {
1589 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001590 if (path) {
1591 pstrcpy(id, sizeof(id), path);
1592 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001593 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001594 }
1595 }
1596 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001597
Blue Swirl72cf2d42009-09-12 07:36:22 +00001598 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001599 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001600 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001601 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001602 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001603 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001604 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001605 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001606 }
aliguori41bd13a2009-04-17 17:10:59 +00001607 }
1608}
1609
Alex Williamson0be71e32010-06-25 11:09:07 -06001610int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001611 const VMStateDescription *vmsd,
1612 void *opaque, int alias_id,
1613 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001614{
Juan Quintela8718e992009-09-01 02:12:31 +02001615 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001616
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001617 /* If this triggers, alias support can be dropped for the vmsd. */
1618 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1619
Anthony Liguori7267c092011-08-20 22:09:37 -05001620 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001621 se->version_id = vmsd->version_id;
1622 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001623 se->opaque = opaque;
1624 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001625 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001626 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001627
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001628 if (dev) {
1629 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001630 if (id) {
1631 pstrcpy(se->idstr, sizeof(se->idstr), id);
1632 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001633 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001634
Anthony Liguori7267c092011-08-20 22:09:37 -05001635 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001636 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1637 se->compat->instance_id = instance_id == -1 ?
1638 calculate_compat_instance_id(vmsd->name) : instance_id;
1639 instance_id = -1;
1640 }
1641 }
1642 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1643
Juan Quintela8718e992009-09-01 02:12:31 +02001644 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001645 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001646 } else {
1647 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001648 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001649 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001650 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001651 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001652 return 0;
1653}
1654
Alex Williamson0be71e32010-06-25 11:09:07 -06001655void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1656 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001657{
Juan Quintela1eb75382009-09-10 03:04:29 +02001658 SaveStateEntry *se, *new_se;
1659
Blue Swirl72cf2d42009-09-12 07:36:22 +00001660 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001661 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001662 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001663 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001664 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001665 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001666 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001667 }
1668 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001669}
1670
Juan Quintela811814b2010-07-26 21:38:43 +02001671static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1672 void *opaque);
1673static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1674 void *opaque);
1675
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001676int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1677 void *opaque, int version_id)
1678{
1679 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001680 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001681
1682 if (version_id > vmsd->version_id) {
1683 return -EINVAL;
1684 }
1685 if (version_id < vmsd->minimum_version_id_old) {
1686 return -EINVAL;
1687 }
1688 if (version_id < vmsd->minimum_version_id) {
1689 return vmsd->load_state_old(f, opaque, version_id);
1690 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001691 if (vmsd->pre_load) {
1692 int ret = vmsd->pre_load(opaque);
1693 if (ret)
1694 return ret;
1695 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001696 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001697 if ((field->field_exists &&
1698 field->field_exists(opaque, version_id)) ||
1699 (!field->field_exists &&
1700 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001701 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001702 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001703 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001704
Juan Quintelae61a1e02009-12-02 12:36:38 +01001705 if (field->flags & VMS_VBUFFER) {
1706 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001707 if (field->flags & VMS_MULTIPLY) {
1708 size *= field->size;
1709 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001710 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001711 if (field->flags & VMS_ARRAY) {
1712 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001713 } else if (field->flags & VMS_VARRAY_INT32) {
1714 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001715 } else if (field->flags & VMS_VARRAY_UINT32) {
1716 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001717 } else if (field->flags & VMS_VARRAY_UINT16) {
1718 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001719 } else if (field->flags & VMS_VARRAY_UINT8) {
1720 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001721 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001722 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001723 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001724 }
1725 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001726 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001727
Juan Quintela19df4382009-09-29 22:48:41 +02001728 if (field->flags & VMS_ARRAY_OF_POINTER) {
1729 addr = *(void **)addr;
1730 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001731 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001732 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001733 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001734 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001735
1736 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001737 if (ret < 0) {
1738 return ret;
1739 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001740 }
1741 }
1742 field++;
1743 }
Juan Quintela811814b2010-07-26 21:38:43 +02001744 ret = vmstate_subsection_load(f, vmsd, opaque);
1745 if (ret != 0) {
1746 return ret;
1747 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001748 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001749 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001750 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001751 return 0;
1752}
1753
1754void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001755 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001756{
1757 VMStateField *field = vmsd->fields;
1758
Juan Quintela8fb07912009-09-10 03:04:32 +02001759 if (vmsd->pre_save) {
1760 vmsd->pre_save(opaque);
1761 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001762 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001763 if (!field->field_exists ||
1764 field->field_exists(opaque, vmsd->version_id)) {
1765 void *base_addr = opaque + field->offset;
1766 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001767 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001768
Juan Quintelae61a1e02009-12-02 12:36:38 +01001769 if (field->flags & VMS_VBUFFER) {
1770 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001771 if (field->flags & VMS_MULTIPLY) {
1772 size *= field->size;
1773 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001774 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001775 if (field->flags & VMS_ARRAY) {
1776 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001777 } else if (field->flags & VMS_VARRAY_INT32) {
1778 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001779 } else if (field->flags & VMS_VARRAY_UINT32) {
1780 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001781 } else if (field->flags & VMS_VARRAY_UINT16) {
1782 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001783 } else if (field->flags & VMS_VARRAY_UINT8) {
1784 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001785 }
1786 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001787 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001788 }
1789 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001790 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001791
Juan Quintela85953872009-12-02 12:36:37 +01001792 if (field->flags & VMS_ARRAY_OF_POINTER) {
1793 addr = *(void **)addr;
1794 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001795 if (field->flags & VMS_STRUCT) {
1796 vmstate_save_state(f, field->vmsd, addr);
1797 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001798 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001799 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001800 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001801 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001802 field++;
1803 }
Juan Quintela811814b2010-07-26 21:38:43 +02001804 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001805}
1806
Juan Quintela4082be42009-08-20 19:42:24 +02001807static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1808{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001809 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001810 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001811 }
1812 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001813}
1814
Alex Williamsondc912122011-01-11 14:39:43 -07001815static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001816{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001817 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001818 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001819 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001820 }
1821 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001822}
1823
aliguoria672b462008-11-11 21:33:36 +00001824#define QEMU_VM_FILE_MAGIC 0x5145564d
1825#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1826#define QEMU_VM_FILE_VERSION 0x00000003
1827
1828#define QEMU_VM_EOF 0x00
1829#define QEMU_VM_SECTION_START 0x01
1830#define QEMU_VM_SECTION_PART 0x02
1831#define QEMU_VM_SECTION_END 0x03
1832#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001833#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001834
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001835bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001836{
1837 SaveStateEntry *se;
1838
1839 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1840 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001841 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001842 return true;
1843 }
1844 }
1845 return false;
1846}
1847
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001848void qemu_savevm_state_begin(QEMUFile *f,
1849 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001850{
1851 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001852 int ret;
aliguoria672b462008-11-11 21:33:36 +00001853
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001854 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001855 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001856 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001857 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001858 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001859 }
1860
aliguoria672b462008-11-11 21:33:36 +00001861 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1862 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1863
Blue Swirl72cf2d42009-09-12 07:36:22 +00001864 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001865 int len;
1866
Juan Quintelad1315aa2012-06-28 15:11:57 +02001867 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001868 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001869 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001870 if (se->ops && se->ops->is_active) {
1871 if (!se->ops->is_active(se->opaque)) {
1872 continue;
1873 }
1874 }
aliguoria672b462008-11-11 21:33:36 +00001875 /* Section type */
1876 qemu_put_byte(f, QEMU_VM_SECTION_START);
1877 qemu_put_be32(f, se->section_id);
1878
1879 /* ID string */
1880 len = strlen(se->idstr);
1881 qemu_put_byte(f, len);
1882 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1883
1884 qemu_put_be32(f, se->instance_id);
1885 qemu_put_be32(f, se->version_id);
1886
Juan Quintelad1315aa2012-06-28 15:11:57 +02001887 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001888 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001889 qemu_file_set_error(f, ret);
1890 break;
Juan Quintela29757252011-10-19 15:22:18 +02001891 }
aliguoria672b462008-11-11 21:33:36 +00001892 }
aliguoria672b462008-11-11 21:33:36 +00001893}
1894
Juan Quintela39346382011-09-22 11:02:14 +02001895/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001896 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001897 * negative: there was one error, and we have -errno.
1898 * 0 : We haven't finished, caller have to go again
1899 * 1 : We have finished, we can go to complete phase
1900 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001901int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001902{
1903 SaveStateEntry *se;
1904 int ret = 1;
1905
Blue Swirl72cf2d42009-09-12 07:36:22 +00001906 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001907 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001908 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001909 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001910 if (se->ops && se->ops->is_active) {
1911 if (!se->ops->is_active(se->opaque)) {
1912 continue;
1913 }
1914 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001915 if (qemu_file_rate_limit(f)) {
1916 return 0;
1917 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001918 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001919 /* Section type */
1920 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1921 qemu_put_be32(f, se->section_id);
1922
Juan Quintela16310a32012-06-28 15:31:37 +02001923 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001924 trace_savevm_section_end(se->section_id);
1925
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001926 if (ret < 0) {
1927 qemu_file_set_error(f, ret);
1928 }
Juan Quintela29757252011-10-19 15:22:18 +02001929 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001930 /* Do not proceed to the next vmstate before this one reported
1931 completion of the current stage. This serializes the migration
1932 and reduces the probability that a faster changing state is
1933 synchronized over and over again. */
1934 break;
1935 }
aliguoria672b462008-11-11 21:33:36 +00001936 }
Juan Quintela39346382011-09-22 11:02:14 +02001937 return ret;
aliguoria672b462008-11-11 21:33:36 +00001938}
1939
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001940void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001941{
1942 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001943 int ret;
aliguoria672b462008-11-11 21:33:36 +00001944
Jan Kiszkaea375f92010-03-01 19:10:30 +01001945 cpu_synchronize_all_states();
1946
Blue Swirl72cf2d42009-09-12 07:36:22 +00001947 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001948 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001949 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001950 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001951 if (se->ops && se->ops->is_active) {
1952 if (!se->ops->is_active(se->opaque)) {
1953 continue;
1954 }
1955 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001956 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001957 /* Section type */
1958 qemu_put_byte(f, QEMU_VM_SECTION_END);
1959 qemu_put_be32(f, se->section_id);
1960
Juan Quintela16310a32012-06-28 15:31:37 +02001961 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001962 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001963 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001964 qemu_file_set_error(f, ret);
1965 return;
Juan Quintela29757252011-10-19 15:22:18 +02001966 }
aliguoria672b462008-11-11 21:33:36 +00001967 }
1968
Blue Swirl72cf2d42009-09-12 07:36:22 +00001969 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001970 int len;
1971
Juan Quintela22ea40f2012-06-26 17:19:10 +02001972 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001973 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001974 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001975 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001976 /* Section type */
1977 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1978 qemu_put_be32(f, se->section_id);
1979
1980 /* ID string */
1981 len = strlen(se->idstr);
1982 qemu_put_byte(f, len);
1983 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1984
1985 qemu_put_be32(f, se->instance_id);
1986 qemu_put_be32(f, se->version_id);
1987
Alex Williamsondc912122011-01-11 14:39:43 -07001988 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001989 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001990 }
1991
1992 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001993 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001994}
1995
Juan Quintelae4ed1542012-09-21 11:18:18 +02001996uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1997{
1998 SaveStateEntry *se;
1999 uint64_t ret = 0;
2000
2001 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2002 if (!se->ops || !se->ops->save_live_pending) {
2003 continue;
2004 }
2005 if (se->ops && se->ops->is_active) {
2006 if (!se->ops->is_active(se->opaque)) {
2007 continue;
2008 }
2009 }
2010 ret += se->ops->save_live_pending(f, se->opaque, max_size);
2011 }
2012 return ret;
2013}
2014
Juan Quintela65227732013-01-14 14:14:42 +01002015void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002016{
2017 SaveStateEntry *se;
2018
2019 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02002020 if (se->ops && se->ops->cancel) {
2021 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002022 }
2023 }
2024}
2025
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002026static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00002027{
aliguoria672b462008-11-11 21:33:36 +00002028 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03002029 MigrationParams params = {
2030 .blk = 0,
2031 .shared = 0
2032 };
aliguoria672b462008-11-11 21:33:36 +00002033
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002034 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01002035 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07002036 }
2037
Paolo Bonzini9b095032013-02-22 17:36:28 +01002038 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002039 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01002040 qemu_mutex_lock_iothread();
2041
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002042 while (qemu_file_get_error(f) == 0) {
2043 if (qemu_savevm_state_iterate(f) > 0) {
2044 break;
2045 }
2046 }
aliguoria672b462008-11-11 21:33:36 +00002047
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002048 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02002049 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002050 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002051 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02002052 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01002053 if (ret != 0) {
2054 qemu_savevm_state_cancel();
2055 }
aliguoria672b462008-11-11 21:33:36 +00002056 return ret;
2057}
2058
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002059static int qemu_save_device_state(QEMUFile *f)
2060{
2061 SaveStateEntry *se;
2062
2063 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2064 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2065
2066 cpu_synchronize_all_states();
2067
2068 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2069 int len;
2070
2071 if (se->is_ram) {
2072 continue;
2073 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02002074 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002075 continue;
2076 }
2077
2078 /* Section type */
2079 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2080 qemu_put_be32(f, se->section_id);
2081
2082 /* ID string */
2083 len = strlen(se->idstr);
2084 qemu_put_byte(f, len);
2085 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2086
2087 qemu_put_be32(f, se->instance_id);
2088 qemu_put_be32(f, se->version_id);
2089
2090 vmstate_save(f, se);
2091 }
2092
2093 qemu_put_byte(f, QEMU_VM_EOF);
2094
2095 return qemu_file_get_error(f);
2096}
2097
aliguoria672b462008-11-11 21:33:36 +00002098static SaveStateEntry *find_se(const char *idstr, int instance_id)
2099{
2100 SaveStateEntry *se;
2101
Blue Swirl72cf2d42009-09-12 07:36:22 +00002102 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00002103 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02002104 (instance_id == se->instance_id ||
2105 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00002106 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06002107 /* Migrating from an older version? */
2108 if (strstr(se->idstr, idstr) && se->compat) {
2109 if (!strcmp(se->compat->idstr, idstr) &&
2110 (instance_id == se->compat->instance_id ||
2111 instance_id == se->alias_id))
2112 return se;
2113 }
aliguoria672b462008-11-11 21:33:36 +00002114 }
2115 return NULL;
2116}
2117
Juan Quintela811814b2010-07-26 21:38:43 +02002118static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2119{
2120 while(sub && sub->needed) {
2121 if (strcmp(idstr, sub->vmsd->name) == 0) {
2122 return sub->vmsd;
2123 }
2124 sub++;
2125 }
2126 return NULL;
2127}
2128
2129static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2130 void *opaque)
2131{
Juan Quintelac6380722011-10-04 15:28:31 +02002132 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02002133 char idstr[256];
2134 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02002135 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02002136 const VMStateDescription *sub_vmsd;
2137
Juan Quintelac6380722011-10-04 15:28:31 +02002138 len = qemu_peek_byte(f, 1);
2139 if (len < strlen(vmsd->name) + 1) {
2140 /* subsection name has be be "section_name/a" */
2141 return 0;
2142 }
2143 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2144 if (size != len) {
2145 return 0;
2146 }
2147 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02002148
Juan Quintelac6380722011-10-04 15:28:31 +02002149 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2150 /* it don't have a valid subsection name */
2151 return 0;
2152 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02002153 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02002154 if (sub_vmsd == NULL) {
2155 return -ENOENT;
2156 }
Juan Quintelac6380722011-10-04 15:28:31 +02002157 qemu_file_skip(f, 1); /* subsection */
2158 qemu_file_skip(f, 1); /* len */
2159 qemu_file_skip(f, len); /* idstr */
2160 version_id = qemu_get_be32(f);
2161
Juan Quintela811814b2010-07-26 21:38:43 +02002162 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2163 if (ret) {
2164 return ret;
2165 }
2166 }
2167 return 0;
2168}
2169
2170static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2171 void *opaque)
2172{
2173 const VMStateSubsection *sub = vmsd->subsections;
2174
2175 while (sub && sub->needed) {
2176 if (sub->needed(opaque)) {
2177 const VMStateDescription *vmsd = sub->vmsd;
2178 uint8_t len;
2179
2180 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2181 len = strlen(vmsd->name);
2182 qemu_put_byte(f, len);
2183 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2184 qemu_put_be32(f, vmsd->version_id);
2185 vmstate_save_state(f, vmsd, opaque);
2186 }
2187 sub++;
2188 }
2189}
2190
aliguoria672b462008-11-11 21:33:36 +00002191typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002192 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002193 SaveStateEntry *se;
2194 int section_id;
2195 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002196} LoadStateEntry;
2197
aliguoria672b462008-11-11 21:33:36 +00002198int qemu_loadvm_state(QEMUFile *f)
2199{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002200 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2201 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002202 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002203 uint8_t section_type;
2204 unsigned int v;
2205 int ret;
2206
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002207 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002208 return -EINVAL;
2209 }
2210
aliguoria672b462008-11-11 21:33:36 +00002211 v = qemu_get_be32(f);
2212 if (v != QEMU_VM_FILE_MAGIC)
2213 return -EINVAL;
2214
2215 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002216 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2217 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2218 return -ENOTSUP;
2219 }
aliguoria672b462008-11-11 21:33:36 +00002220 if (v != QEMU_VM_FILE_VERSION)
2221 return -ENOTSUP;
2222
2223 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2224 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002225 SaveStateEntry *se;
2226 char idstr[257];
2227 int len;
2228
2229 switch (section_type) {
2230 case QEMU_VM_SECTION_START:
2231 case QEMU_VM_SECTION_FULL:
2232 /* Read section start */
2233 section_id = qemu_get_be32(f);
2234 len = qemu_get_byte(f);
2235 qemu_get_buffer(f, (uint8_t *)idstr, len);
2236 idstr[len] = 0;
2237 instance_id = qemu_get_be32(f);
2238 version_id = qemu_get_be32(f);
2239
2240 /* Find savevm section */
2241 se = find_se(idstr, instance_id);
2242 if (se == NULL) {
2243 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2244 ret = -EINVAL;
2245 goto out;
2246 }
2247
2248 /* Validate version */
2249 if (version_id > se->version_id) {
2250 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2251 version_id, idstr, se->version_id);
2252 ret = -EINVAL;
2253 goto out;
2254 }
2255
2256 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002257 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002258
2259 le->se = se;
2260 le->section_id = section_id;
2261 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002262 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002263
Juan Quintela4082be42009-08-20 19:42:24 +02002264 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002265 if (ret < 0) {
2266 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2267 instance_id, idstr);
2268 goto out;
2269 }
aliguoria672b462008-11-11 21:33:36 +00002270 break;
2271 case QEMU_VM_SECTION_PART:
2272 case QEMU_VM_SECTION_END:
2273 section_id = qemu_get_be32(f);
2274
Blue Swirl72cf2d42009-09-12 07:36:22 +00002275 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002276 if (le->section_id == section_id) {
2277 break;
2278 }
2279 }
aliguoria672b462008-11-11 21:33:36 +00002280 if (le == NULL) {
2281 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2282 ret = -EINVAL;
2283 goto out;
2284 }
2285
Juan Quintela4082be42009-08-20 19:42:24 +02002286 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002287 if (ret < 0) {
2288 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2289 section_id);
2290 goto out;
2291 }
aliguoria672b462008-11-11 21:33:36 +00002292 break;
2293 default:
2294 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2295 ret = -EINVAL;
2296 goto out;
2297 }
2298 }
2299
Jan Kiszkaea375f92010-03-01 19:10:30 +01002300 cpu_synchronize_all_post_init();
2301
aliguoria672b462008-11-11 21:33:36 +00002302 ret = 0;
2303
2304out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002305 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2306 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002307 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002308 }
2309
Juan Quintela42802d42011-10-05 01:14:46 +02002310 if (ret == 0) {
2311 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002312 }
aliguoria672b462008-11-11 21:33:36 +00002313
2314 return ret;
2315}
2316
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002317static BlockDriverState *find_vmstate_bs(void)
2318{
2319 BlockDriverState *bs = NULL;
2320 while ((bs = bdrv_next(bs))) {
2321 if (bdrv_can_snapshot(bs)) {
2322 return bs;
2323 }
2324 }
2325 return NULL;
2326}
2327
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002328/*
2329 * Deletes snapshots of a given name in all opened images.
2330 */
2331static int del_existing_snapshots(Monitor *mon, const char *name)
2332{
2333 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002334 QEMUSnapshotInfo sn1, *snapshot = &sn1;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002335 Error *err = NULL;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002336
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002337 bs = NULL;
2338 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002339 if (bdrv_can_snapshot(bs) &&
2340 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2341 {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002342 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
2343 if (error_is_set(&err)) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002344 monitor_printf(mon,
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002345 "Error while deleting snapshot on device '%s':"
2346 " %s\n",
2347 bdrv_get_device_name(bs),
2348 error_get_pretty(err));
2349 error_free(err);
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002350 return -1;
2351 }
2352 }
2353 }
2354
2355 return 0;
2356}
2357
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002358void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002359{
2360 BlockDriverState *bs, *bs1;
2361 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002362 int ret;
aliguoria672b462008-11-11 21:33:36 +00002363 QEMUFile *f;
2364 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002365 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002366 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002367 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002368 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002369
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002370 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002371 bs = NULL;
2372 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002373
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002374 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002375 continue;
2376 }
2377
2378 if (!bdrv_can_snapshot(bs)) {
2379 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2380 bdrv_get_device_name(bs));
2381 return;
2382 }
2383 }
2384
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002385 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002386 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002387 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002388 return;
2389 }
aliguoria672b462008-11-11 21:33:36 +00002390
Luiz Capitulino13548692011-07-29 15:36:43 -03002391 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002392 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002393
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002394 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002395
2396 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002397 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002398 sn->date_sec = tv.tv_sec;
2399 sn->date_nsec = tv.tv_usec * 1000;
Alex Blighbc72ad62013-08-21 16:03:08 +01002400 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
aliguoria672b462008-11-11 21:33:36 +00002401
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002402 if (name) {
2403 ret = bdrv_snapshot_find(bs, old_sn, name);
2404 if (ret >= 0) {
2405 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2406 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2407 } else {
2408 pstrcpy(sn->name, sizeof(sn->name), name);
2409 }
2410 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002411 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2412 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002413 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002414 }
2415
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002416 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002417 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002418 goto the_end;
2419 }
2420
aliguoria672b462008-11-11 21:33:36 +00002421 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002422 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002423 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002424 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002425 goto the_end;
2426 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002427 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002428 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002429 qemu_fclose(f);
2430 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002431 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002432 goto the_end;
2433 }
2434
2435 /* create the snapshots */
2436
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002437 bs1 = NULL;
2438 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002439 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002440 /* Write VM state size only to the image that contains the state */
2441 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002442 ret = bdrv_snapshot_create(bs1, sn);
2443 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002444 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2445 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002446 }
2447 }
2448 }
2449
2450 the_end:
2451 if (saved_vm_running)
2452 vm_start();
2453}
2454
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002455void qmp_xen_save_devices_state(const char *filename, Error **errp)
2456{
2457 QEMUFile *f;
2458 int saved_vm_running;
2459 int ret;
2460
2461 saved_vm_running = runstate_is_running();
2462 vm_stop(RUN_STATE_SAVE_VM);
2463
2464 f = qemu_fopen(filename, "wb");
2465 if (!f) {
Luiz Capitulino1befce92013-06-07 14:36:58 -04002466 error_setg_file_open(errp, errno, filename);
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002467 goto the_end;
2468 }
2469 ret = qemu_save_device_state(f);
2470 qemu_fclose(f);
2471 if (ret < 0) {
2472 error_set(errp, QERR_IO_ERROR);
2473 }
2474
2475 the_end:
2476 if (saved_vm_running)
2477 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002478}
2479
Markus Armbruster03cd4652010-02-17 16:24:10 +01002480int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002481{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002482 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002483 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002484 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002485 int ret;
aliguoria672b462008-11-11 21:33:36 +00002486
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002487 bs_vm_state = find_vmstate_bs();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002488 if (!bs_vm_state) {
2489 error_report("No block device supports snapshots");
2490 return -ENOTSUP;
2491 }
2492
2493 /* Don't even try to load empty VM states */
2494 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2495 if (ret < 0) {
2496 return ret;
2497 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002498 error_report("This is a disk-only snapshot. Revert to it offline "
2499 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002500 return -EINVAL;
2501 }
2502
2503 /* Verify if there is any device that doesn't support snapshots and is
2504 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002505 bs = NULL;
2506 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002507
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002508 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002509 continue;
2510 }
2511
2512 if (!bdrv_can_snapshot(bs)) {
2513 error_report("Device '%s' is writable but does not support snapshots.",
2514 bdrv_get_device_name(bs));
2515 return -ENOTSUP;
2516 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002517
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002518 ret = bdrv_snapshot_find(bs, &sn, name);
2519 if (ret < 0) {
2520 error_report("Device '%s' does not have the requested snapshot '%s'",
2521 bdrv_get_device_name(bs), name);
2522 return ret;
2523 }
aliguoria672b462008-11-11 21:33:36 +00002524 }
2525
2526 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002527 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002528
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002529 bs = NULL;
2530 while ((bs = bdrv_next(bs))) {
2531 if (bdrv_can_snapshot(bs)) {
2532 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002533 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002534 error_report("Error %d while activating snapshot '%s' on '%s'",
2535 ret, name, bdrv_get_device_name(bs));
2536 return ret;
aliguoria672b462008-11-11 21:33:36 +00002537 }
2538 }
2539 }
2540
aliguoria672b462008-11-11 21:33:36 +00002541 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002542 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002543 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002544 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002545 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002546 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002547
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002548 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002549 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002550
aliguoria672b462008-11-11 21:33:36 +00002551 qemu_fclose(f);
2552 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002553 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002554 return ret;
aliguoria672b462008-11-11 21:33:36 +00002555 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002556
Juan Quintela05f24012009-08-20 19:42:22 +02002557 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002558}
2559
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002560void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002561{
2562 BlockDriverState *bs, *bs1;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002563 Error *err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002564 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002565
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002566 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002567 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002568 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002569 return;
2570 }
2571
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002572 bs1 = NULL;
2573 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002574 if (bdrv_can_snapshot(bs1)) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002575 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
2576 if (error_is_set(&err)) {
2577 monitor_printf(mon,
2578 "Error while deleting snapshot on device '%s':"
2579 " %s\n",
2580 bdrv_get_device_name(bs),
2581 error_get_pretty(err));
2582 error_free(err);
aliguoria672b462008-11-11 21:33:36 +00002583 }
2584 }
2585 }
2586}
2587
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002588void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002589{
2590 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002591 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2592 int nb_sns, i, ret, available;
2593 int total;
2594 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002595
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002596 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002597 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002598 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002599 return;
2600 }
aliguoria672b462008-11-11 21:33:36 +00002601
2602 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2603 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002604 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002605 return;
2606 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002607
2608 if (nb_sns == 0) {
2609 monitor_printf(mon, "There is no snapshot available.\n");
2610 return;
aliguoria672b462008-11-11 21:33:36 +00002611 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002612
Anthony Liguori7267c092011-08-20 22:09:37 -05002613 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002614 total = 0;
2615 for (i = 0; i < nb_sns; i++) {
2616 sn = &sn_tab[i];
2617 available = 1;
2618 bs1 = NULL;
2619
2620 while ((bs1 = bdrv_next(bs1))) {
2621 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2622 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2623 if (ret < 0) {
2624 available = 0;
2625 break;
2626 }
2627 }
2628 }
2629
2630 if (available) {
2631 available_snapshots[total] = i;
2632 total++;
2633 }
2634 }
2635
2636 if (total > 0) {
Wenchao Xia5b917042013-05-25 11:09:45 +08002637 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2638 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002639 for (i = 0; i < total; i++) {
2640 sn = &sn_tab[available_snapshots[i]];
Wenchao Xia5b917042013-05-25 11:09:45 +08002641 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2642 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002643 }
2644 } else {
2645 monitor_printf(mon, "There is no suitable snapshot available\n");
2646 }
2647
Anthony Liguori7267c092011-08-20 22:09:37 -05002648 g_free(sn_tab);
2649 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002650
aliguoria672b462008-11-11 21:33:36 +00002651}
Avi Kivityc5705a72011-12-20 15:59:12 +02002652
2653void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2654{
Avi Kivity1ddde082012-01-08 13:18:19 +02002655 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002656 memory_region_name(mr), dev);
2657}
2658
2659void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2660{
2661 /* Nothing do to while the implementation is in RAMBlock */
2662}
2663
2664void vmstate_register_ram_global(MemoryRegion *mr)
2665{
2666 vmstate_register_ram(mr, NULL);
2667}