blob: 67ddf06af0c485ec9bc553ef17e5c8d2d5a72120 [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, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100100 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700101 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300102 } else {
103 qemu_del_timer(timer);
104 qemu_free_timer(timer);
105 }
106}
107
108void qemu_announce_self(void)
109{
110 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100111 timer = qemu_new_timer_ms(rt_clock, 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
Juan Quintela624b9cc2011-10-05 01:02:52 +0200569int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000570{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200571 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000572}
573
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100574static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000575{
Juan Quintelaafe41932013-01-14 13:36:28 +0100576 if (f->last_error == 0) {
577 f->last_error = ret;
578 }
aliguori4dabe242009-04-05 19:30:51 +0000579}
580
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200581static inline bool qemu_file_is_writable(QEMUFile *f)
582{
583 return f->ops->writev_buffer || f->ops->put_buffer;
584}
585
Orit Wassermancb88aa82013-03-22 16:48:01 +0200586/**
587 * Flushes QEMUFile buffer
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200588 *
Orit Wassermancb88aa82013-03-22 16:48:01 +0200589 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
590 * put_buffer ops.
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200591 */
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100592static void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000593{
Orit Wassermancb88aa82013-03-22 16:48:01 +0200594 ssize_t ret = 0;
Juan Quintela7311bea2012-08-29 19:08:59 +0200595
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200596 if (!qemu_file_is_writable(f)) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100597 return;
598 }
Orit Wassermancb88aa82013-03-22 16:48:01 +0200599
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200600 if (f->ops->writev_buffer) {
601 if (f->iovcnt > 0) {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200602 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
Juan Quintela7311bea2012-08-29 19:08:59 +0200603 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200604 } else {
605 if (f->buf_index > 0) {
606 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200607 }
aliguoria672b462008-11-11 21:33:36 +0000608 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200609 if (ret >= 0) {
610 f->pos += ret;
611 }
612 f->buf_index = 0;
613 f->iovcnt = 0;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100614 if (ret < 0) {
615 qemu_file_set_error(f, ret);
616 }
aliguoria672b462008-11-11 21:33:36 +0000617}
618
619static void qemu_fill_buffer(QEMUFile *f)
620{
621 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200622 int pending;
aliguoria672b462008-11-11 21:33:36 +0000623
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200624 assert(!qemu_file_is_writable(f));
aliguoria672b462008-11-11 21:33:36 +0000625
Juan Quintela0046c452011-09-30 19:28:45 +0200626 pending = f->buf_size - f->buf_index;
627 if (pending > 0) {
628 memmove(f->buf, f->buf + f->buf_index, pending);
629 }
630 f->buf_index = 0;
631 f->buf_size = pending;
632
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100633 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200634 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000635 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200636 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100637 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200638 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200639 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000640 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200641 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000642}
643
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200644int qemu_get_fd(QEMUFile *f)
645{
646 if (f->ops->get_fd) {
647 return f->ops->get_fd(f->opaque);
648 }
649 return -1;
650}
651
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400652void qemu_update_position(QEMUFile *f, size_t size)
653{
654 f->pos += size;
655}
656
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200657/** Closes the file
658 *
659 * Returns negative error value if any error happened on previous operations or
660 * while closing the file. Returns 0 or positive number on success.
661 *
662 * The meaning of return value on success depends on the specific backend
663 * being used.
664 */
665int qemu_fclose(QEMUFile *f)
666{
Juan Quintela29eee862012-08-29 19:14:54 +0200667 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100668 qemu_fflush(f);
669 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200670
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200671 if (f->ops->close) {
672 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200673 if (ret >= 0) {
674 ret = ret2;
675 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200676 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200677 /* If any error was spotted before closing, we should report it
678 * instead of the close() return value.
679 */
680 if (f->last_error) {
681 ret = f->last_error;
682 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500683 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000684 return ret;
685}
686
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200687static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
688{
689 /* check for adjacent buffer and coalesce them */
690 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
691 f->iov[f->iovcnt - 1].iov_len) {
692 f->iov[f->iovcnt - 1].iov_len += size;
693 } else {
694 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
695 f->iov[f->iovcnt++].iov_len = size;
696 }
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200697
Paolo Bonzini4d117242013-04-08 13:29:57 +0200698 if (f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200699 qemu_fflush(f);
700 }
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200701}
702
Orit Wasserman6181ec22013-03-22 16:48:02 +0200703void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
704{
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200705 if (!f->ops->writev_buffer) {
706 qemu_put_buffer(f, buf, size);
707 return;
708 }
709
Orit Wasserman6181ec22013-03-22 16:48:02 +0200710 if (f->last_error) {
711 return;
712 }
713
Orit Wasserman6181ec22013-03-22 16:48:02 +0200714 f->bytes_xfer += size;
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200715 add_to_iovec(f, buf, size);
Orit Wasserman6181ec22013-03-22 16:48:02 +0200716}
717
aliguoria672b462008-11-11 21:33:36 +0000718void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
719{
720 int l;
721
Juan Quintelac10682c2012-08-29 19:43:39 +0200722 if (f->last_error) {
723 return;
724 }
725
Juan Quintelac10682c2012-08-29 19:43:39 +0200726 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000727 l = IO_BUF_SIZE - f->buf_index;
728 if (l > size)
729 l = size;
730 memcpy(f->buf + f->buf_index, buf, l);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200731 f->bytes_xfer += size;
732 if (f->ops->writev_buffer) {
733 add_to_iovec(f, f->buf + f->buf_index, l);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200734 }
735 f->buf_index += l;
736 if (f->buf_index == IO_BUF_SIZE) {
737 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200738 }
Orit Wasserman6181ec22013-03-22 16:48:02 +0200739 if (qemu_file_get_error(f)) {
740 break;
741 }
aliguoria672b462008-11-11 21:33:36 +0000742 buf += l;
743 size -= l;
aliguoria672b462008-11-11 21:33:36 +0000744 }
745}
746
747void qemu_put_byte(QEMUFile *f, int v)
748{
Juan Quintelac10682c2012-08-29 19:43:39 +0200749 if (f->last_error) {
750 return;
751 }
752
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200753 f->buf[f->buf_index] = v;
Orit Wasserman7d8a30b2013-03-22 16:47:59 +0200754 f->bytes_xfer++;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200755 if (f->ops->writev_buffer) {
756 add_to_iovec(f, f->buf + f->buf_index, 1);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200757 }
758 f->buf_index++;
759 if (f->buf_index == IO_BUF_SIZE) {
760 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200761 }
aliguoria672b462008-11-11 21:33:36 +0000762}
763
Juan Quintelac6380722011-10-04 15:28:31 +0200764static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000765{
Juan Quintelac6380722011-10-04 15:28:31 +0200766 if (f->buf_index + size <= f->buf_size) {
767 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000768 }
aliguoria672b462008-11-11 21:33:36 +0000769}
770
Juan Quintelac6380722011-10-04 15:28:31 +0200771static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200772{
Juan Quintelac6380722011-10-04 15:28:31 +0200773 int pending;
774 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200775
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200776 assert(!qemu_file_is_writable(f));
Juan Quintela811814b2010-07-26 21:38:43 +0200777
Juan Quintelac6380722011-10-04 15:28:31 +0200778 index = f->buf_index + offset;
779 pending = f->buf_size - index;
780 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200781 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200782 index = f->buf_index + offset;
783 pending = f->buf_size - index;
784 }
785
786 if (pending <= 0) {
787 return 0;
788 }
789 if (size > pending) {
790 size = pending;
791 }
792
793 memcpy(buf, f->buf + index, size);
794 return size;
795}
796
797int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
798{
799 int pending = size;
800 int done = 0;
801
802 while (pending > 0) {
803 int res;
804
805 res = qemu_peek_buffer(f, buf, pending, 0);
806 if (res == 0) {
807 return done;
808 }
809 qemu_file_skip(f, res);
810 buf += res;
811 pending -= res;
812 done += res;
813 }
814 return done;
815}
816
817static int qemu_peek_byte(QEMUFile *f, int offset)
818{
819 int index = f->buf_index + offset;
820
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200821 assert(!qemu_file_is_writable(f));
Juan Quintelac6380722011-10-04 15:28:31 +0200822
823 if (index >= f->buf_size) {
824 qemu_fill_buffer(f);
825 index = f->buf_index + offset;
826 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200827 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200828 }
Juan Quintela811814b2010-07-26 21:38:43 +0200829 }
Juan Quintelac6380722011-10-04 15:28:31 +0200830 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200831}
832
aliguoria672b462008-11-11 21:33:36 +0000833int qemu_get_byte(QEMUFile *f)
834{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200835 int result;
aliguoria672b462008-11-11 21:33:36 +0000836
Juan Quintelac6380722011-10-04 15:28:31 +0200837 result = qemu_peek_byte(f, 0);
838 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200839 return result;
aliguoria672b462008-11-11 21:33:36 +0000840}
841
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100842int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000843{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100844 qemu_fflush(f);
845 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000846}
847
aliguoria672b462008-11-11 21:33:36 +0000848int qemu_file_rate_limit(QEMUFile *f)
849{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100850 if (qemu_file_get_error(f)) {
851 return 1;
852 }
853 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
854 return 1;
855 }
aliguoria672b462008-11-11 21:33:36 +0000856 return 0;
857}
858
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200859int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200860{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100861 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200862}
863
Paolo Bonzini1964a392013-02-22 17:36:45 +0100864void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400865{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100866 f->xfer_limit = limit;
867}
Glauber Costa19629532009-05-20 18:26:57 -0400868
Paolo Bonzini1964a392013-02-22 17:36:45 +0100869void qemu_file_reset_rate_limit(QEMUFile *f)
870{
871 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400872}
873
aliguoria672b462008-11-11 21:33:36 +0000874void qemu_put_be16(QEMUFile *f, unsigned int v)
875{
876 qemu_put_byte(f, v >> 8);
877 qemu_put_byte(f, v);
878}
879
880void qemu_put_be32(QEMUFile *f, unsigned int v)
881{
882 qemu_put_byte(f, v >> 24);
883 qemu_put_byte(f, v >> 16);
884 qemu_put_byte(f, v >> 8);
885 qemu_put_byte(f, v);
886}
887
888void qemu_put_be64(QEMUFile *f, uint64_t v)
889{
890 qemu_put_be32(f, v >> 32);
891 qemu_put_be32(f, v);
892}
893
894unsigned int qemu_get_be16(QEMUFile *f)
895{
896 unsigned int v;
897 v = qemu_get_byte(f) << 8;
898 v |= qemu_get_byte(f);
899 return v;
900}
901
902unsigned int qemu_get_be32(QEMUFile *f)
903{
904 unsigned int v;
905 v = qemu_get_byte(f) << 24;
906 v |= qemu_get_byte(f) << 16;
907 v |= qemu_get_byte(f) << 8;
908 v |= qemu_get_byte(f);
909 return v;
910}
911
912uint64_t qemu_get_be64(QEMUFile *f)
913{
914 uint64_t v;
915 v = (uint64_t)qemu_get_be32(f) << 32;
916 v |= qemu_get_be32(f);
917 return v;
918}
919
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200920
921/* timer */
922
923void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
924{
925 uint64_t expire_time;
926
927 expire_time = qemu_timer_expire_time_ns(ts);
928 qemu_put_be64(f, expire_time);
929}
930
931void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
932{
933 uint64_t expire_time;
934
935 expire_time = qemu_get_be64(f);
936 if (expire_time != -1) {
937 qemu_mod_timer_ns(ts, expire_time);
938 } else {
939 qemu_del_timer(ts);
940 }
941}
942
943
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100944/* bool */
945
946static int get_bool(QEMUFile *f, void *pv, size_t size)
947{
948 bool *v = pv;
949 *v = qemu_get_byte(f);
950 return 0;
951}
952
953static void put_bool(QEMUFile *f, void *pv, size_t size)
954{
955 bool *v = pv;
956 qemu_put_byte(f, *v);
957}
958
959const VMStateInfo vmstate_info_bool = {
960 .name = "bool",
961 .get = get_bool,
962 .put = put_bool,
963};
964
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200965/* 8 bit int */
966
967static int get_int8(QEMUFile *f, void *pv, size_t size)
968{
969 int8_t *v = pv;
970 qemu_get_s8s(f, v);
971 return 0;
972}
973
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200974static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200975{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200976 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200977 qemu_put_s8s(f, v);
978}
979
980const VMStateInfo vmstate_info_int8 = {
981 .name = "int8",
982 .get = get_int8,
983 .put = put_int8,
984};
985
986/* 16 bit int */
987
988static int get_int16(QEMUFile *f, void *pv, size_t size)
989{
990 int16_t *v = pv;
991 qemu_get_sbe16s(f, v);
992 return 0;
993}
994
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200995static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200996{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200997 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200998 qemu_put_sbe16s(f, v);
999}
1000
1001const VMStateInfo vmstate_info_int16 = {
1002 .name = "int16",
1003 .get = get_int16,
1004 .put = put_int16,
1005};
1006
1007/* 32 bit int */
1008
1009static int get_int32(QEMUFile *f, void *pv, size_t size)
1010{
1011 int32_t *v = pv;
1012 qemu_get_sbe32s(f, v);
1013 return 0;
1014}
1015
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001016static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001017{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001018 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001019 qemu_put_sbe32s(f, v);
1020}
1021
1022const VMStateInfo vmstate_info_int32 = {
1023 .name = "int32",
1024 .get = get_int32,
1025 .put = put_int32,
1026};
1027
Juan Quintela82501662009-08-20 19:42:32 +02001028/* 32 bit int. See that the received value is the same than the one
1029 in the field */
1030
1031static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1032{
1033 int32_t *v = pv;
1034 int32_t v2;
1035 qemu_get_sbe32s(f, &v2);
1036
1037 if (*v == v2)
1038 return 0;
1039 return -EINVAL;
1040}
1041
1042const VMStateInfo vmstate_info_int32_equal = {
1043 .name = "int32 equal",
1044 .get = get_int32_equal,
1045 .put = put_int32,
1046};
1047
Juan Quintela0a031e02009-08-20 19:42:37 +02001048/* 32 bit int. See that the received value is the less or the same
1049 than the one in the field */
1050
1051static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1052{
1053 int32_t *old = pv;
1054 int32_t new;
1055 qemu_get_sbe32s(f, &new);
1056
1057 if (*old <= new)
1058 return 0;
1059 return -EINVAL;
1060}
1061
1062const VMStateInfo vmstate_info_int32_le = {
1063 .name = "int32 equal",
1064 .get = get_int32_le,
1065 .put = put_int32,
1066};
1067
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001068/* 64 bit int */
1069
1070static int get_int64(QEMUFile *f, void *pv, size_t size)
1071{
1072 int64_t *v = pv;
1073 qemu_get_sbe64s(f, v);
1074 return 0;
1075}
1076
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001077static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001078{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001079 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001080 qemu_put_sbe64s(f, v);
1081}
1082
1083const VMStateInfo vmstate_info_int64 = {
1084 .name = "int64",
1085 .get = get_int64,
1086 .put = put_int64,
1087};
1088
1089/* 8 bit unsigned int */
1090
1091static int get_uint8(QEMUFile *f, void *pv, size_t size)
1092{
1093 uint8_t *v = pv;
1094 qemu_get_8s(f, v);
1095 return 0;
1096}
1097
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001098static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001099{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001100 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001101 qemu_put_8s(f, v);
1102}
1103
1104const VMStateInfo vmstate_info_uint8 = {
1105 .name = "uint8",
1106 .get = get_uint8,
1107 .put = put_uint8,
1108};
1109
1110/* 16 bit unsigned int */
1111
1112static int get_uint16(QEMUFile *f, void *pv, size_t size)
1113{
1114 uint16_t *v = pv;
1115 qemu_get_be16s(f, v);
1116 return 0;
1117}
1118
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001119static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001120{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001121 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001122 qemu_put_be16s(f, v);
1123}
1124
1125const VMStateInfo vmstate_info_uint16 = {
1126 .name = "uint16",
1127 .get = get_uint16,
1128 .put = put_uint16,
1129};
1130
1131/* 32 bit unsigned int */
1132
1133static int get_uint32(QEMUFile *f, void *pv, size_t size)
1134{
1135 uint32_t *v = pv;
1136 qemu_get_be32s(f, v);
1137 return 0;
1138}
1139
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001140static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001141{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001142 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001143 qemu_put_be32s(f, v);
1144}
1145
1146const VMStateInfo vmstate_info_uint32 = {
1147 .name = "uint32",
1148 .get = get_uint32,
1149 .put = put_uint32,
1150};
1151
Juan Quintela9122a8f2011-03-10 12:33:48 +01001152/* 32 bit uint. See that the received value is the same than the one
1153 in the field */
1154
1155static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1156{
1157 uint32_t *v = pv;
1158 uint32_t v2;
1159 qemu_get_be32s(f, &v2);
1160
1161 if (*v == v2) {
1162 return 0;
1163 }
1164 return -EINVAL;
1165}
1166
1167const VMStateInfo vmstate_info_uint32_equal = {
1168 .name = "uint32 equal",
1169 .get = get_uint32_equal,
1170 .put = put_uint32,
1171};
1172
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001173/* 64 bit unsigned int */
1174
1175static int get_uint64(QEMUFile *f, void *pv, size_t size)
1176{
1177 uint64_t *v = pv;
1178 qemu_get_be64s(f, v);
1179 return 0;
1180}
1181
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001182static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001183{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001184 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001185 qemu_put_be64s(f, v);
1186}
1187
1188const VMStateInfo vmstate_info_uint64 = {
1189 .name = "uint64",
1190 .get = get_uint64,
1191 .put = put_uint64,
1192};
1193
David Gibsone344b8a2013-03-12 14:06:00 +11001194/* 64 bit unsigned int. See that the received value is the same than the one
1195 in the field */
1196
1197static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1198{
1199 uint64_t *v = pv;
1200 uint64_t v2;
1201 qemu_get_be64s(f, &v2);
1202
1203 if (*v == v2) {
1204 return 0;
1205 }
1206 return -EINVAL;
1207}
1208
1209const VMStateInfo vmstate_info_uint64_equal = {
1210 .name = "int64 equal",
1211 .get = get_uint64_equal,
1212 .put = put_uint64,
1213};
1214
Juan Quintela80cd83e2009-09-10 03:04:36 +02001215/* 8 bit int. See that the received value is the same than the one
1216 in the field */
1217
1218static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1219{
1220 uint8_t *v = pv;
1221 uint8_t v2;
1222 qemu_get_8s(f, &v2);
1223
1224 if (*v == v2)
1225 return 0;
1226 return -EINVAL;
1227}
1228
1229const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001230 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001231 .get = get_uint8_equal,
1232 .put = put_uint8,
1233};
1234
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001235/* 16 bit unsigned int int. See that the received value is the same than the one
1236 in the field */
1237
1238static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1239{
1240 uint16_t *v = pv;
1241 uint16_t v2;
1242 qemu_get_be16s(f, &v2);
1243
1244 if (*v == v2)
1245 return 0;
1246 return -EINVAL;
1247}
1248
1249const VMStateInfo vmstate_info_uint16_equal = {
1250 .name = "uint16 equal",
1251 .get = get_uint16_equal,
1252 .put = put_uint16,
1253};
1254
David Gibson213945e2013-03-12 14:06:02 +11001255/* floating point */
1256
1257static int get_float64(QEMUFile *f, void *pv, size_t size)
1258{
1259 float64 *v = pv;
1260
1261 *v = make_float64(qemu_get_be64(f));
1262 return 0;
1263}
1264
1265static void put_float64(QEMUFile *f, void *pv, size_t size)
1266{
1267 uint64_t *v = pv;
1268
1269 qemu_put_be64(f, float64_val(*v));
1270}
1271
1272const VMStateInfo vmstate_info_float64 = {
1273 .name = "float64",
1274 .get = get_float64,
1275 .put = put_float64,
1276};
1277
Juan Quinteladde04632009-08-20 19:42:26 +02001278/* timers */
1279
1280static int get_timer(QEMUFile *f, void *pv, size_t size)
1281{
1282 QEMUTimer *v = pv;
1283 qemu_get_timer(f, v);
1284 return 0;
1285}
1286
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001287static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001288{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001289 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001290 qemu_put_timer(f, v);
1291}
1292
1293const VMStateInfo vmstate_info_timer = {
1294 .name = "timer",
1295 .get = get_timer,
1296 .put = put_timer,
1297};
1298
Juan Quintela6f67c502009-08-20 19:42:35 +02001299/* uint8_t buffers */
1300
1301static int get_buffer(QEMUFile *f, void *pv, size_t size)
1302{
1303 uint8_t *v = pv;
1304 qemu_get_buffer(f, v, size);
1305 return 0;
1306}
1307
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001308static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001309{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001310 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001311 qemu_put_buffer(f, v, size);
1312}
1313
1314const VMStateInfo vmstate_info_buffer = {
1315 .name = "buffer",
1316 .get = get_buffer,
1317 .put = put_buffer,
1318};
1319
Juan Quintela76507c72009-10-19 15:46:28 +02001320/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001321 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001322
1323static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1324{
Jan Kiszka21174c32009-12-02 12:36:35 +01001325 uint8_t buf[1024];
1326 int block_len;
1327
1328 while (size > 0) {
1329 block_len = MIN(sizeof(buf), size);
1330 size -= block_len;
1331 qemu_get_buffer(f, buf, block_len);
1332 }
1333 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001334}
1335
1336static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1337{
Jan Kiszka21174c32009-12-02 12:36:35 +01001338 static const uint8_t buf[1024];
1339 int block_len;
1340
1341 while (size > 0) {
1342 block_len = MIN(sizeof(buf), size);
1343 size -= block_len;
1344 qemu_put_buffer(f, buf, block_len);
1345 }
Juan Quintela76507c72009-10-19 15:46:28 +02001346}
1347
1348const VMStateInfo vmstate_info_unused_buffer = {
1349 .name = "unused_buffer",
1350 .get = get_unused_buffer,
1351 .put = put_unused_buffer,
1352};
1353
Peter Maydell08e99e22012-10-30 07:45:12 +00001354/* bitmaps (as defined by bitmap.h). Note that size here is the size
1355 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1356 * bit words with the bits in big endian order. The in-memory format
1357 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1358 */
1359/* This is the number of 64 bit words sent over the wire */
1360#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1361static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1362{
1363 unsigned long *bmp = pv;
1364 int i, idx = 0;
1365 for (i = 0; i < BITS_TO_U64S(size); i++) {
1366 uint64_t w = qemu_get_be64(f);
1367 bmp[idx++] = w;
1368 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1369 bmp[idx++] = w >> 32;
1370 }
1371 }
1372 return 0;
1373}
1374
1375static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1376{
1377 unsigned long *bmp = pv;
1378 int i, idx = 0;
1379 for (i = 0; i < BITS_TO_U64S(size); i++) {
1380 uint64_t w = bmp[idx++];
1381 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1382 w |= ((uint64_t)bmp[idx++]) << 32;
1383 }
1384 qemu_put_be64(f, w);
1385 }
1386}
1387
1388const VMStateInfo vmstate_info_bitmap = {
1389 .name = "bitmap",
1390 .get = get_bitmap,
1391 .put = put_bitmap,
1392};
1393
Alex Williamson7685ee62010-06-25 11:09:14 -06001394typedef struct CompatEntry {
1395 char idstr[256];
1396 int instance_id;
1397} CompatEntry;
1398
aliguoria672b462008-11-11 21:33:36 +00001399typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001400 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001401 char idstr[256];
1402 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001403 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001404 int version_id;
1405 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001406 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001407 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001408 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001409 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001410 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001411 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001412} SaveStateEntry;
1413
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001414
Blue Swirl72cf2d42009-09-12 07:36:22 +00001415static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1416 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001417static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001418
Juan Quintela8718e992009-09-01 02:12:31 +02001419static int calculate_new_instance_id(const char *idstr)
1420{
1421 SaveStateEntry *se;
1422 int instance_id = 0;
1423
Blue Swirl72cf2d42009-09-12 07:36:22 +00001424 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001425 if (strcmp(idstr, se->idstr) == 0
1426 && instance_id <= se->instance_id) {
1427 instance_id = se->instance_id + 1;
1428 }
1429 }
1430 return instance_id;
1431}
1432
Alex Williamson7685ee62010-06-25 11:09:14 -06001433static int calculate_compat_instance_id(const char *idstr)
1434{
1435 SaveStateEntry *se;
1436 int instance_id = 0;
1437
1438 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1439 if (!se->compat)
1440 continue;
1441
1442 if (strcmp(idstr, se->compat->idstr) == 0
1443 && instance_id <= se->compat->instance_id) {
1444 instance_id = se->compat->instance_id + 1;
1445 }
1446 }
1447 return instance_id;
1448}
1449
aliguoria672b462008-11-11 21:33:36 +00001450/* TODO: Individual devices generally have very little idea about the rest
1451 of the system, so instance_id should be removed/replaced.
1452 Meanwhile pass -1 as instance_id if you do not already have a clearly
1453 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001454int register_savevm_live(DeviceState *dev,
1455 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001456 int instance_id,
1457 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001458 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001459 void *opaque)
1460{
Juan Quintela8718e992009-09-01 02:12:31 +02001461 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001462
Anthony Liguori7267c092011-08-20 22:09:37 -05001463 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001464 se->version_id = version_id;
1465 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001466 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001467 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001468 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001469 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001470 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001471 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001472 se->is_ram = 1;
1473 }
aliguoria672b462008-11-11 21:33:36 +00001474
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001475 if (dev) {
1476 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001477 if (id) {
1478 pstrcpy(se->idstr, sizeof(se->idstr), id);
1479 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001480 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001481
Anthony Liguori7267c092011-08-20 22:09:37 -05001482 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001483 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1484 se->compat->instance_id = instance_id == -1 ?
1485 calculate_compat_instance_id(idstr) : instance_id;
1486 instance_id = -1;
1487 }
1488 }
1489 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1490
Juan Quintela8718e992009-09-01 02:12:31 +02001491 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001492 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001493 } else {
1494 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001495 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001496 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001497 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001498 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001499 return 0;
1500}
1501
Alex Williamson0be71e32010-06-25 11:09:07 -06001502int register_savevm(DeviceState *dev,
1503 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001504 int instance_id,
1505 int version_id,
1506 SaveStateHandler *save_state,
1507 LoadStateHandler *load_state,
1508 void *opaque)
1509{
Juan Quintela7908c782012-06-26 18:46:10 +02001510 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1511 ops->save_state = save_state;
1512 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001513 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001514 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001515}
1516
Alex Williamson0be71e32010-06-25 11:09:07 -06001517void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001518{
Juan Quintela8718e992009-09-01 02:12:31 +02001519 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001520 char id[256] = "";
1521
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001522 if (dev) {
1523 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001524 if (path) {
1525 pstrcpy(id, sizeof(id), path);
1526 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001527 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001528 }
1529 }
1530 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001531
Blue Swirl72cf2d42009-09-12 07:36:22 +00001532 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001533 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001534 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001535 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001536 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001537 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001538 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001539 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001540 }
aliguori41bd13a2009-04-17 17:10:59 +00001541 }
1542}
1543
Alex Williamson0be71e32010-06-25 11:09:07 -06001544int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001545 const VMStateDescription *vmsd,
1546 void *opaque, int alias_id,
1547 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001548{
Juan Quintela8718e992009-09-01 02:12:31 +02001549 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001550
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001551 /* If this triggers, alias support can be dropped for the vmsd. */
1552 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1553
Anthony Liguori7267c092011-08-20 22:09:37 -05001554 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001555 se->version_id = vmsd->version_id;
1556 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001557 se->opaque = opaque;
1558 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001559 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001560 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001561
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001562 if (dev) {
1563 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001564 if (id) {
1565 pstrcpy(se->idstr, sizeof(se->idstr), id);
1566 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001567 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001568
Anthony Liguori7267c092011-08-20 22:09:37 -05001569 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001570 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1571 se->compat->instance_id = instance_id == -1 ?
1572 calculate_compat_instance_id(vmsd->name) : instance_id;
1573 instance_id = -1;
1574 }
1575 }
1576 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1577
Juan Quintela8718e992009-09-01 02:12:31 +02001578 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001579 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001580 } else {
1581 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001582 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001583 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001584 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001585 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001586 return 0;
1587}
1588
Alex Williamson0be71e32010-06-25 11:09:07 -06001589void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1590 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001591{
Juan Quintela1eb75382009-09-10 03:04:29 +02001592 SaveStateEntry *se, *new_se;
1593
Blue Swirl72cf2d42009-09-12 07:36:22 +00001594 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001595 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001596 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001597 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001598 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001599 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001600 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001601 }
1602 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001603}
1604
Juan Quintela811814b2010-07-26 21:38:43 +02001605static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1606 void *opaque);
1607static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1608 void *opaque);
1609
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001610int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1611 void *opaque, int version_id)
1612{
1613 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001614 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001615
1616 if (version_id > vmsd->version_id) {
1617 return -EINVAL;
1618 }
1619 if (version_id < vmsd->minimum_version_id_old) {
1620 return -EINVAL;
1621 }
1622 if (version_id < vmsd->minimum_version_id) {
1623 return vmsd->load_state_old(f, opaque, version_id);
1624 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001625 if (vmsd->pre_load) {
1626 int ret = vmsd->pre_load(opaque);
1627 if (ret)
1628 return ret;
1629 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001630 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001631 if ((field->field_exists &&
1632 field->field_exists(opaque, version_id)) ||
1633 (!field->field_exists &&
1634 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001635 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001636 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001637 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001638
Juan Quintelae61a1e02009-12-02 12:36:38 +01001639 if (field->flags & VMS_VBUFFER) {
1640 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001641 if (field->flags & VMS_MULTIPLY) {
1642 size *= field->size;
1643 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001644 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001645 if (field->flags & VMS_ARRAY) {
1646 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001647 } else if (field->flags & VMS_VARRAY_INT32) {
1648 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001649 } else if (field->flags & VMS_VARRAY_UINT32) {
1650 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001651 } else if (field->flags & VMS_VARRAY_UINT16) {
1652 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001653 } else if (field->flags & VMS_VARRAY_UINT8) {
1654 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001655 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001656 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001657 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001658 }
1659 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001660 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001661
Juan Quintela19df4382009-09-29 22:48:41 +02001662 if (field->flags & VMS_ARRAY_OF_POINTER) {
1663 addr = *(void **)addr;
1664 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001665 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001666 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001667 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001668 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001669
1670 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001671 if (ret < 0) {
1672 return ret;
1673 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001674 }
1675 }
1676 field++;
1677 }
Juan Quintela811814b2010-07-26 21:38:43 +02001678 ret = vmstate_subsection_load(f, vmsd, opaque);
1679 if (ret != 0) {
1680 return ret;
1681 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001682 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001683 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001684 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001685 return 0;
1686}
1687
1688void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001689 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001690{
1691 VMStateField *field = vmsd->fields;
1692
Juan Quintela8fb07912009-09-10 03:04:32 +02001693 if (vmsd->pre_save) {
1694 vmsd->pre_save(opaque);
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, vmsd->version_id)) {
1699 void *base_addr = opaque + field->offset;
1700 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001701 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001702
Juan Quintelae61a1e02009-12-02 12:36:38 +01001703 if (field->flags & VMS_VBUFFER) {
1704 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001705 if (field->flags & VMS_MULTIPLY) {
1706 size *= field->size;
1707 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001708 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001709 if (field->flags & VMS_ARRAY) {
1710 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001711 } else if (field->flags & VMS_VARRAY_INT32) {
1712 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001713 } else if (field->flags & VMS_VARRAY_UINT32) {
1714 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001715 } else if (field->flags & VMS_VARRAY_UINT16) {
1716 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001717 } else if (field->flags & VMS_VARRAY_UINT8) {
1718 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001719 }
1720 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001721 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001722 }
1723 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001724 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001725
Juan Quintela85953872009-12-02 12:36:37 +01001726 if (field->flags & VMS_ARRAY_OF_POINTER) {
1727 addr = *(void **)addr;
1728 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001729 if (field->flags & VMS_STRUCT) {
1730 vmstate_save_state(f, field->vmsd, addr);
1731 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001732 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001733 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001734 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001735 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001736 field++;
1737 }
Juan Quintela811814b2010-07-26 21:38:43 +02001738 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001739}
1740
Juan Quintela4082be42009-08-20 19:42:24 +02001741static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1742{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001743 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001744 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001745 }
1746 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001747}
1748
Alex Williamsondc912122011-01-11 14:39:43 -07001749static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001750{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001751 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001752 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001753 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001754 }
1755 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001756}
1757
aliguoria672b462008-11-11 21:33:36 +00001758#define QEMU_VM_FILE_MAGIC 0x5145564d
1759#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1760#define QEMU_VM_FILE_VERSION 0x00000003
1761
1762#define QEMU_VM_EOF 0x00
1763#define QEMU_VM_SECTION_START 0x01
1764#define QEMU_VM_SECTION_PART 0x02
1765#define QEMU_VM_SECTION_END 0x03
1766#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001767#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001768
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001769bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001770{
1771 SaveStateEntry *se;
1772
1773 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1774 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001775 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001776 return true;
1777 }
1778 }
1779 return false;
1780}
1781
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001782void qemu_savevm_state_begin(QEMUFile *f,
1783 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001784{
1785 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001786 int ret;
aliguoria672b462008-11-11 21:33:36 +00001787
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001788 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001789 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001790 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001791 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001792 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001793 }
1794
aliguoria672b462008-11-11 21:33:36 +00001795 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1796 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1797
Blue Swirl72cf2d42009-09-12 07:36:22 +00001798 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001799 int len;
1800
Juan Quintelad1315aa2012-06-28 15:11:57 +02001801 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001802 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001803 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001804 if (se->ops && se->ops->is_active) {
1805 if (!se->ops->is_active(se->opaque)) {
1806 continue;
1807 }
1808 }
aliguoria672b462008-11-11 21:33:36 +00001809 /* Section type */
1810 qemu_put_byte(f, QEMU_VM_SECTION_START);
1811 qemu_put_be32(f, se->section_id);
1812
1813 /* ID string */
1814 len = strlen(se->idstr);
1815 qemu_put_byte(f, len);
1816 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1817
1818 qemu_put_be32(f, se->instance_id);
1819 qemu_put_be32(f, se->version_id);
1820
Juan Quintelad1315aa2012-06-28 15:11:57 +02001821 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001822 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001823 qemu_file_set_error(f, ret);
1824 break;
Juan Quintela29757252011-10-19 15:22:18 +02001825 }
aliguoria672b462008-11-11 21:33:36 +00001826 }
aliguoria672b462008-11-11 21:33:36 +00001827}
1828
Juan Quintela39346382011-09-22 11:02:14 +02001829/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001830 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001831 * negative: there was one error, and we have -errno.
1832 * 0 : We haven't finished, caller have to go again
1833 * 1 : We have finished, we can go to complete phase
1834 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001835int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001836{
1837 SaveStateEntry *se;
1838 int ret = 1;
1839
Blue Swirl72cf2d42009-09-12 07:36:22 +00001840 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001841 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001842 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001843 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001844 if (se->ops && se->ops->is_active) {
1845 if (!se->ops->is_active(se->opaque)) {
1846 continue;
1847 }
1848 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001849 if (qemu_file_rate_limit(f)) {
1850 return 0;
1851 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001852 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001853 /* Section type */
1854 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1855 qemu_put_be32(f, se->section_id);
1856
Juan Quintela16310a32012-06-28 15:31:37 +02001857 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001858 trace_savevm_section_end(se->section_id);
1859
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001860 if (ret < 0) {
1861 qemu_file_set_error(f, ret);
1862 }
Juan Quintela29757252011-10-19 15:22:18 +02001863 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001864 /* Do not proceed to the next vmstate before this one reported
1865 completion of the current stage. This serializes the migration
1866 and reduces the probability that a faster changing state is
1867 synchronized over and over again. */
1868 break;
1869 }
aliguoria672b462008-11-11 21:33:36 +00001870 }
Juan Quintela39346382011-09-22 11:02:14 +02001871 return ret;
aliguoria672b462008-11-11 21:33:36 +00001872}
1873
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001874void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001875{
1876 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001877 int ret;
aliguoria672b462008-11-11 21:33:36 +00001878
Jan Kiszkaea375f92010-03-01 19:10:30 +01001879 cpu_synchronize_all_states();
1880
Blue Swirl72cf2d42009-09-12 07:36:22 +00001881 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001882 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001883 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001884 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001885 if (se->ops && se->ops->is_active) {
1886 if (!se->ops->is_active(se->opaque)) {
1887 continue;
1888 }
1889 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001890 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001891 /* Section type */
1892 qemu_put_byte(f, QEMU_VM_SECTION_END);
1893 qemu_put_be32(f, se->section_id);
1894
Juan Quintela16310a32012-06-28 15:31:37 +02001895 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001896 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001897 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001898 qemu_file_set_error(f, ret);
1899 return;
Juan Quintela29757252011-10-19 15:22:18 +02001900 }
aliguoria672b462008-11-11 21:33:36 +00001901 }
1902
Blue Swirl72cf2d42009-09-12 07:36:22 +00001903 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001904 int len;
1905
Juan Quintela22ea40f2012-06-26 17:19:10 +02001906 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001907 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001908 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001909 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001910 /* Section type */
1911 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1912 qemu_put_be32(f, se->section_id);
1913
1914 /* ID string */
1915 len = strlen(se->idstr);
1916 qemu_put_byte(f, len);
1917 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1918
1919 qemu_put_be32(f, se->instance_id);
1920 qemu_put_be32(f, se->version_id);
1921
Alex Williamsondc912122011-01-11 14:39:43 -07001922 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001923 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001924 }
1925
1926 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001927 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001928}
1929
Juan Quintelae4ed1542012-09-21 11:18:18 +02001930uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1931{
1932 SaveStateEntry *se;
1933 uint64_t ret = 0;
1934
1935 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1936 if (!se->ops || !se->ops->save_live_pending) {
1937 continue;
1938 }
1939 if (se->ops && se->ops->is_active) {
1940 if (!se->ops->is_active(se->opaque)) {
1941 continue;
1942 }
1943 }
1944 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1945 }
1946 return ret;
1947}
1948
Juan Quintela65227732013-01-14 14:14:42 +01001949void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001950{
1951 SaveStateEntry *se;
1952
1953 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001954 if (se->ops && se->ops->cancel) {
1955 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001956 }
1957 }
1958}
1959
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001960static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001961{
aliguoria672b462008-11-11 21:33:36 +00001962 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001963 MigrationParams params = {
1964 .blk = 0,
1965 .shared = 0
1966 };
aliguoria672b462008-11-11 21:33:36 +00001967
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001968 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001969 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07001970 }
1971
Paolo Bonzini9b095032013-02-22 17:36:28 +01001972 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001973 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001974 qemu_mutex_lock_iothread();
1975
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001976 while (qemu_file_get_error(f) == 0) {
1977 if (qemu_savevm_state_iterate(f) > 0) {
1978 break;
1979 }
1980 }
aliguoria672b462008-11-11 21:33:36 +00001981
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001982 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001983 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001984 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001985 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001986 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001987 if (ret != 0) {
1988 qemu_savevm_state_cancel();
1989 }
aliguoria672b462008-11-11 21:33:36 +00001990 return ret;
1991}
1992
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001993static int qemu_save_device_state(QEMUFile *f)
1994{
1995 SaveStateEntry *se;
1996
1997 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1998 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1999
2000 cpu_synchronize_all_states();
2001
2002 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2003 int len;
2004
2005 if (se->is_ram) {
2006 continue;
2007 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02002008 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002009 continue;
2010 }
2011
2012 /* Section type */
2013 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2014 qemu_put_be32(f, se->section_id);
2015
2016 /* ID string */
2017 len = strlen(se->idstr);
2018 qemu_put_byte(f, len);
2019 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2020
2021 qemu_put_be32(f, se->instance_id);
2022 qemu_put_be32(f, se->version_id);
2023
2024 vmstate_save(f, se);
2025 }
2026
2027 qemu_put_byte(f, QEMU_VM_EOF);
2028
2029 return qemu_file_get_error(f);
2030}
2031
aliguoria672b462008-11-11 21:33:36 +00002032static SaveStateEntry *find_se(const char *idstr, int instance_id)
2033{
2034 SaveStateEntry *se;
2035
Blue Swirl72cf2d42009-09-12 07:36:22 +00002036 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00002037 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02002038 (instance_id == se->instance_id ||
2039 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00002040 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06002041 /* Migrating from an older version? */
2042 if (strstr(se->idstr, idstr) && se->compat) {
2043 if (!strcmp(se->compat->idstr, idstr) &&
2044 (instance_id == se->compat->instance_id ||
2045 instance_id == se->alias_id))
2046 return se;
2047 }
aliguoria672b462008-11-11 21:33:36 +00002048 }
2049 return NULL;
2050}
2051
Juan Quintela811814b2010-07-26 21:38:43 +02002052static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2053{
2054 while(sub && sub->needed) {
2055 if (strcmp(idstr, sub->vmsd->name) == 0) {
2056 return sub->vmsd;
2057 }
2058 sub++;
2059 }
2060 return NULL;
2061}
2062
2063static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2064 void *opaque)
2065{
Juan Quintelac6380722011-10-04 15:28:31 +02002066 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02002067 char idstr[256];
2068 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02002069 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02002070 const VMStateDescription *sub_vmsd;
2071
Juan Quintelac6380722011-10-04 15:28:31 +02002072 len = qemu_peek_byte(f, 1);
2073 if (len < strlen(vmsd->name) + 1) {
2074 /* subsection name has be be "section_name/a" */
2075 return 0;
2076 }
2077 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2078 if (size != len) {
2079 return 0;
2080 }
2081 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02002082
Juan Quintelac6380722011-10-04 15:28:31 +02002083 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2084 /* it don't have a valid subsection name */
2085 return 0;
2086 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02002087 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02002088 if (sub_vmsd == NULL) {
2089 return -ENOENT;
2090 }
Juan Quintelac6380722011-10-04 15:28:31 +02002091 qemu_file_skip(f, 1); /* subsection */
2092 qemu_file_skip(f, 1); /* len */
2093 qemu_file_skip(f, len); /* idstr */
2094 version_id = qemu_get_be32(f);
2095
Juan Quintela811814b2010-07-26 21:38:43 +02002096 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2097 if (ret) {
2098 return ret;
2099 }
2100 }
2101 return 0;
2102}
2103
2104static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2105 void *opaque)
2106{
2107 const VMStateSubsection *sub = vmsd->subsections;
2108
2109 while (sub && sub->needed) {
2110 if (sub->needed(opaque)) {
2111 const VMStateDescription *vmsd = sub->vmsd;
2112 uint8_t len;
2113
2114 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2115 len = strlen(vmsd->name);
2116 qemu_put_byte(f, len);
2117 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2118 qemu_put_be32(f, vmsd->version_id);
2119 vmstate_save_state(f, vmsd, opaque);
2120 }
2121 sub++;
2122 }
2123}
2124
aliguoria672b462008-11-11 21:33:36 +00002125typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002126 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002127 SaveStateEntry *se;
2128 int section_id;
2129 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002130} LoadStateEntry;
2131
aliguoria672b462008-11-11 21:33:36 +00002132int qemu_loadvm_state(QEMUFile *f)
2133{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002134 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2135 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002136 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002137 uint8_t section_type;
2138 unsigned int v;
2139 int ret;
2140
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002141 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002142 return -EINVAL;
2143 }
2144
aliguoria672b462008-11-11 21:33:36 +00002145 v = qemu_get_be32(f);
2146 if (v != QEMU_VM_FILE_MAGIC)
2147 return -EINVAL;
2148
2149 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002150 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2151 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2152 return -ENOTSUP;
2153 }
aliguoria672b462008-11-11 21:33:36 +00002154 if (v != QEMU_VM_FILE_VERSION)
2155 return -ENOTSUP;
2156
2157 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2158 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002159 SaveStateEntry *se;
2160 char idstr[257];
2161 int len;
2162
2163 switch (section_type) {
2164 case QEMU_VM_SECTION_START:
2165 case QEMU_VM_SECTION_FULL:
2166 /* Read section start */
2167 section_id = qemu_get_be32(f);
2168 len = qemu_get_byte(f);
2169 qemu_get_buffer(f, (uint8_t *)idstr, len);
2170 idstr[len] = 0;
2171 instance_id = qemu_get_be32(f);
2172 version_id = qemu_get_be32(f);
2173
2174 /* Find savevm section */
2175 se = find_se(idstr, instance_id);
2176 if (se == NULL) {
2177 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2178 ret = -EINVAL;
2179 goto out;
2180 }
2181
2182 /* Validate version */
2183 if (version_id > se->version_id) {
2184 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2185 version_id, idstr, se->version_id);
2186 ret = -EINVAL;
2187 goto out;
2188 }
2189
2190 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002191 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002192
2193 le->se = se;
2194 le->section_id = section_id;
2195 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002196 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002197
Juan Quintela4082be42009-08-20 19:42:24 +02002198 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002199 if (ret < 0) {
2200 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2201 instance_id, idstr);
2202 goto out;
2203 }
aliguoria672b462008-11-11 21:33:36 +00002204 break;
2205 case QEMU_VM_SECTION_PART:
2206 case QEMU_VM_SECTION_END:
2207 section_id = qemu_get_be32(f);
2208
Blue Swirl72cf2d42009-09-12 07:36:22 +00002209 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002210 if (le->section_id == section_id) {
2211 break;
2212 }
2213 }
aliguoria672b462008-11-11 21:33:36 +00002214 if (le == NULL) {
2215 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2216 ret = -EINVAL;
2217 goto out;
2218 }
2219
Juan Quintela4082be42009-08-20 19:42:24 +02002220 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002221 if (ret < 0) {
2222 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2223 section_id);
2224 goto out;
2225 }
aliguoria672b462008-11-11 21:33:36 +00002226 break;
2227 default:
2228 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2229 ret = -EINVAL;
2230 goto out;
2231 }
2232 }
2233
Jan Kiszkaea375f92010-03-01 19:10:30 +01002234 cpu_synchronize_all_post_init();
2235
aliguoria672b462008-11-11 21:33:36 +00002236 ret = 0;
2237
2238out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002239 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2240 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002241 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002242 }
2243
Juan Quintela42802d42011-10-05 01:14:46 +02002244 if (ret == 0) {
2245 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002246 }
aliguoria672b462008-11-11 21:33:36 +00002247
2248 return ret;
2249}
2250
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002251static BlockDriverState *find_vmstate_bs(void)
2252{
2253 BlockDriverState *bs = NULL;
2254 while ((bs = bdrv_next(bs))) {
2255 if (bdrv_can_snapshot(bs)) {
2256 return bs;
2257 }
2258 }
2259 return NULL;
2260}
2261
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002262/*
2263 * Deletes snapshots of a given name in all opened images.
2264 */
2265static int del_existing_snapshots(Monitor *mon, const char *name)
2266{
2267 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002268 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2269 int ret;
2270
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002271 bs = NULL;
2272 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002273 if (bdrv_can_snapshot(bs) &&
2274 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2275 {
2276 ret = bdrv_snapshot_delete(bs, name);
2277 if (ret < 0) {
2278 monitor_printf(mon,
2279 "Error while deleting snapshot on '%s'\n",
2280 bdrv_get_device_name(bs));
2281 return -1;
2282 }
2283 }
2284 }
2285
2286 return 0;
2287}
2288
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002289void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002290{
2291 BlockDriverState *bs, *bs1;
2292 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002293 int ret;
aliguoria672b462008-11-11 21:33:36 +00002294 QEMUFile *f;
2295 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002296 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002297 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002298 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002299 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002300
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002301 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002302 bs = NULL;
2303 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002304
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002305 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002306 continue;
2307 }
2308
2309 if (!bdrv_can_snapshot(bs)) {
2310 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2311 bdrv_get_device_name(bs));
2312 return;
2313 }
2314 }
2315
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002316 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002317 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002318 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002319 return;
2320 }
aliguoria672b462008-11-11 21:33:36 +00002321
Luiz Capitulino13548692011-07-29 15:36:43 -03002322 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002323 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002324
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002325 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002326
2327 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002328 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002329 sn->date_sec = tv.tv_sec;
2330 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002331 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002332
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002333 if (name) {
2334 ret = bdrv_snapshot_find(bs, old_sn, name);
2335 if (ret >= 0) {
2336 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2337 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2338 } else {
2339 pstrcpy(sn->name, sizeof(sn->name), name);
2340 }
2341 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002342 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2343 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002344 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002345 }
2346
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002347 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002348 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002349 goto the_end;
2350 }
2351
aliguoria672b462008-11-11 21:33:36 +00002352 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002353 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002354 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002355 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002356 goto the_end;
2357 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002358 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002359 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002360 qemu_fclose(f);
2361 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002362 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002363 goto the_end;
2364 }
2365
2366 /* create the snapshots */
2367
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002368 bs1 = NULL;
2369 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002370 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002371 /* Write VM state size only to the image that contains the state */
2372 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002373 ret = bdrv_snapshot_create(bs1, sn);
2374 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002375 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2376 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002377 }
2378 }
2379 }
2380
2381 the_end:
2382 if (saved_vm_running)
2383 vm_start();
2384}
2385
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002386void qmp_xen_save_devices_state(const char *filename, Error **errp)
2387{
2388 QEMUFile *f;
2389 int saved_vm_running;
2390 int ret;
2391
2392 saved_vm_running = runstate_is_running();
2393 vm_stop(RUN_STATE_SAVE_VM);
2394
2395 f = qemu_fopen(filename, "wb");
2396 if (!f) {
Luiz Capitulino1befce92013-06-07 14:36:58 -04002397 error_setg_file_open(errp, errno, filename);
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002398 goto the_end;
2399 }
2400 ret = qemu_save_device_state(f);
2401 qemu_fclose(f);
2402 if (ret < 0) {
2403 error_set(errp, QERR_IO_ERROR);
2404 }
2405
2406 the_end:
2407 if (saved_vm_running)
2408 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002409}
2410
Markus Armbruster03cd4652010-02-17 16:24:10 +01002411int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002412{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002413 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002414 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002415 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002416 int ret;
aliguoria672b462008-11-11 21:33:36 +00002417
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002418 bs_vm_state = find_vmstate_bs();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002419 if (!bs_vm_state) {
2420 error_report("No block device supports snapshots");
2421 return -ENOTSUP;
2422 }
2423
2424 /* Don't even try to load empty VM states */
2425 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2426 if (ret < 0) {
2427 return ret;
2428 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002429 error_report("This is a disk-only snapshot. Revert to it offline "
2430 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002431 return -EINVAL;
2432 }
2433
2434 /* Verify if there is any device that doesn't support snapshots and is
2435 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002436 bs = NULL;
2437 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002438
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002439 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002440 continue;
2441 }
2442
2443 if (!bdrv_can_snapshot(bs)) {
2444 error_report("Device '%s' is writable but does not support snapshots.",
2445 bdrv_get_device_name(bs));
2446 return -ENOTSUP;
2447 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002448
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002449 ret = bdrv_snapshot_find(bs, &sn, name);
2450 if (ret < 0) {
2451 error_report("Device '%s' does not have the requested snapshot '%s'",
2452 bdrv_get_device_name(bs), name);
2453 return ret;
2454 }
aliguoria672b462008-11-11 21:33:36 +00002455 }
2456
2457 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002458 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002459
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002460 bs = NULL;
2461 while ((bs = bdrv_next(bs))) {
2462 if (bdrv_can_snapshot(bs)) {
2463 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002464 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002465 error_report("Error %d while activating snapshot '%s' on '%s'",
2466 ret, name, bdrv_get_device_name(bs));
2467 return ret;
aliguoria672b462008-11-11 21:33:36 +00002468 }
2469 }
2470 }
2471
aliguoria672b462008-11-11 21:33:36 +00002472 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002473 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002474 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002475 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002476 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002477 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002478
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002479 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002480 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002481
aliguoria672b462008-11-11 21:33:36 +00002482 qemu_fclose(f);
2483 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002484 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002485 return ret;
aliguoria672b462008-11-11 21:33:36 +00002486 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002487
Juan Quintela05f24012009-08-20 19:42:22 +02002488 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002489}
2490
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002491void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002492{
2493 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002494 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002495 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002496
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002497 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002498 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002499 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002500 return;
2501 }
2502
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002503 bs1 = NULL;
2504 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002505 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002506 ret = bdrv_snapshot_delete(bs1, name);
2507 if (ret < 0) {
2508 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002509 monitor_printf(mon,
2510 "Snapshots not supported on device '%s'\n",
2511 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002512 else
aliguori376253e2009-03-05 23:01:23 +00002513 monitor_printf(mon, "Error %d while deleting snapshot on "
2514 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002515 }
2516 }
2517 }
2518}
2519
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002520void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002521{
2522 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002523 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2524 int nb_sns, i, ret, available;
2525 int total;
2526 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002527
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002528 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002529 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002530 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002531 return;
2532 }
aliguoria672b462008-11-11 21:33:36 +00002533
2534 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2535 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002536 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002537 return;
2538 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002539
2540 if (nb_sns == 0) {
2541 monitor_printf(mon, "There is no snapshot available.\n");
2542 return;
aliguoria672b462008-11-11 21:33:36 +00002543 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002544
Anthony Liguori7267c092011-08-20 22:09:37 -05002545 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002546 total = 0;
2547 for (i = 0; i < nb_sns; i++) {
2548 sn = &sn_tab[i];
2549 available = 1;
2550 bs1 = NULL;
2551
2552 while ((bs1 = bdrv_next(bs1))) {
2553 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2554 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2555 if (ret < 0) {
2556 available = 0;
2557 break;
2558 }
2559 }
2560 }
2561
2562 if (available) {
2563 available_snapshots[total] = i;
2564 total++;
2565 }
2566 }
2567
2568 if (total > 0) {
Wenchao Xia5b917042013-05-25 11:09:45 +08002569 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2570 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002571 for (i = 0; i < total; i++) {
2572 sn = &sn_tab[available_snapshots[i]];
Wenchao Xia5b917042013-05-25 11:09:45 +08002573 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2574 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002575 }
2576 } else {
2577 monitor_printf(mon, "There is no suitable snapshot available\n");
2578 }
2579
Anthony Liguori7267c092011-08-20 22:09:37 -05002580 g_free(sn_tab);
2581 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002582
aliguoria672b462008-11-11 21:33:36 +00002583}
Avi Kivityc5705a72011-12-20 15:59:12 +02002584
2585void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2586{
Avi Kivity1ddde082012-01-08 13:18:19 +02002587 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002588 memory_region_name(mr), dev);
2589}
2590
2591void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2592{
2593 /* Nothing do to while the implementation is in RAMBlock */
2594}
2595
2596void vmstate_register_ram_global(MemoryRegion *mr)
2597{
2598 vmstate_register_ram(mr, NULL);
2599}