blob: 66b8b5ec8bb5738c4fa2f0ed44a97a80793574e7 [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,
Eduardo Habkost5cecf412013-11-28 12:01:12 -020056 uint8_t *mac_addr)
aliguoria672b462008-11-11 21:33:36 +000057{
Nolan18995b92009-10-15 16:53:55 -070058 /* Ethernet header. */
59 memset(buf, 0xff, 6); /* destination MAC addr */
60 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
61 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000062
Nolan18995b92009-10-15 16:53:55 -070063 /* RARP header. */
64 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
65 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
66 *(buf + 18) = 6; /* hardware addr length (ethernet) */
67 *(buf + 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
69 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
70 memset(buf + 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
72 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000073
Nolan18995b92009-10-15 16:53:55 -070074 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000076
Nolan18995b92009-10-15 16:53:55 -070077 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000078}
79
Mark McLoughlinf401ca22009-11-25 18:49:32 +000080static void qemu_announce_self_iter(NICState *nic, void *opaque)
81{
82 uint8_t buf[60];
83 int len;
84
85 len = announce_self_create(buf, nic->conf->macaddr.a);
86
Jason Wangb356f762013-01-30 19:12:22 +080087 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000088}
89
90
Gleb Natapoved8b3302009-05-21 17:17:44 +030091static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000092{
Gleb Natapoved8b3302009-05-21 17:17:44 +030093 static int count = SELF_ANNOUNCE_ROUNDS;
94 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000095
Mark McLoughlinf401ca22009-11-25 18:49:32 +000096 qemu_foreach_nic(qemu_announce_self_iter, NULL);
97
Nolan18995b92009-10-15 16:53:55 -070098 if (--count) {
99 /* delay 50ms, 150ms, 250ms, ... */
Alex Blighbc72ad62013-08-21 16:03:08 +0100100 timer_mod(timer, qemu_clock_get_ms(QEMU_CLOCK_REALTIME) +
Nolan18995b92009-10-15 16:53:55 -0700101 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300102 } else {
Eduardo Habkost5cecf412013-11-28 12:01:12 -0200103 timer_del(timer);
104 timer_free(timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300105 }
106}
107
108void qemu_announce_self(void)
109{
Eduardo Habkost5cecf412013-11-28 12:01:12 -0200110 static QEMUTimer *timer;
111 timer = timer_new_ms(QEMU_CLOCK_REALTIME, qemu_announce_self_once, &timer);
112 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
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200140typedef struct QEMUFileStdio {
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200141 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000142 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200143} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000144
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200145typedef struct QEMUFileSocket {
aliguoria672b462008-11-11 21:33:36 +0000146 int fd;
147 QEMUFile *file;
148} QEMUFileSocket;
149
Kevin Wolf05fcc842013-04-05 21:27:54 +0200150static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
151 int64_t pos)
Orit Wasserman28085f72013-03-22 16:47:58 +0200152{
153 QEMUFileSocket *s = opaque;
154 ssize_t len;
155 ssize_t size = iov_size(iov, iovcnt);
156
157 len = iov_send(s->fd, iov, iovcnt, 0, size);
158 if (len < size) {
159 len = -socket_error();
160 }
161 return len;
162}
163
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200164static int socket_get_fd(void *opaque)
165{
166 QEMUFileSocket *s = opaque;
167
168 return s->fd;
169}
170
aliguoria672b462008-11-11 21:33:36 +0000171static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
172{
173 QEMUFileSocket *s = opaque;
174 ssize_t len;
175
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200176 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000177 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200178 if (len != -1) {
179 break;
180 }
181 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100182 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200183 } else if (socket_error() != EINTR) {
184 break;
185 }
186 }
aliguoria672b462008-11-11 21:33:36 +0000187
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200188 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000189 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200190 }
aliguoria672b462008-11-11 21:33:36 +0000191 return len;
192}
193
194static int socket_close(void *opaque)
195{
196 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200197 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500198 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000199 return 0;
200}
201
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200202static int stdio_get_fd(void *opaque)
203{
204 QEMUFileStdio *s = opaque;
205
206 return fileno(s->stdio_file);
207}
208
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200209static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos,
210 int size)
aliguoria672b462008-11-11 21:33:36 +0000211{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200212 QEMUFileStdio *s = opaque;
213 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000214}
215
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200216static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000217{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200218 QEMUFileStdio *s = opaque;
219 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300220 int bytes;
221
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200222 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300223 clearerr(fp);
224 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200225 if (bytes != 0 || !ferror(fp)) {
226 break;
227 }
228 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100229 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200230 } else if (errno != EINTR) {
231 break;
232 }
233 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300234 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000235}
236
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200237static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000238{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200239 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500240 int ret;
241 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200242 if (ret == -1) {
243 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100244 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
245 /* close succeeded, but non-zero exit code: */
246 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200247 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500248 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500249 return ret;
aliguoria672b462008-11-11 21:33:36 +0000250}
251
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200252static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000253{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200254 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200255 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100256
Orit Wassermancb88aa82013-03-22 16:48:01 +0200257 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100258 int fd = fileno(s->stdio_file);
259 struct stat st;
260
261 ret = fstat(fd, &st);
262 if (ret == 0 && S_ISREG(st.st_mode)) {
263 /*
264 * If the file handle is a regular file make sure the
265 * data is flushed to disk before signaling success.
266 */
267 ret = fsync(fd);
268 if (ret != 0) {
269 ret = -errno;
270 return ret;
271 }
272 }
273 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200274 if (fclose(s->stdio_file) == EOF) {
275 ret = -errno;
276 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500277 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200278 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200279}
aliguoria672b462008-11-11 21:33:36 +0000280
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200281static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200282 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200283 .get_buffer = stdio_get_buffer,
284 .close = stdio_pclose
285};
286
287static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200288 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200289 .put_buffer = stdio_put_buffer,
290 .close = stdio_pclose
291};
292
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100293QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200294{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100295 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200296 QEMUFileStdio *s;
297
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200298 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
299 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100300 return NULL;
301 }
302
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200303 stdio_file = popen(command, mode);
304 if (stdio_file == NULL) {
aliguoria672b462008-11-11 21:33:36 +0000305 return NULL;
306 }
307
Anthony Liguori7267c092011-08-20 22:09:37 -0500308 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000309
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200310 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000311
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200312 if (mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200313 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000314 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200315 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000316 }
aliguoria672b462008-11-11 21:33:36 +0000317 return s->file;
318}
319
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200320static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200321 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200322 .get_buffer = stdio_get_buffer,
323 .close = stdio_fclose
324};
325
326static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200327 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200328 .put_buffer = stdio_put_buffer,
329 .close = stdio_fclose
330};
331
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100332static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
333 int64_t pos)
334{
335 QEMUFileSocket *s = opaque;
336 ssize_t len, offset;
337 ssize_t size = iov_size(iov, iovcnt);
338 ssize_t total = 0;
339
340 assert(iovcnt > 0);
341 offset = 0;
342 while (size > 0) {
343 /* Find the next start position; skip all full-sized vector elements */
344 while (offset >= iov[0].iov_len) {
345 offset -= iov[0].iov_len;
346 iov++, iovcnt--;
347 }
348
349 /* skip `offset' bytes from the (now) first element, undo it on exit */
350 assert(iovcnt > 0);
351 iov[0].iov_base += offset;
352 iov[0].iov_len -= offset;
353
354 do {
355 len = writev(s->fd, iov, iovcnt);
356 } while (len == -1 && errno == EINTR);
357 if (len == -1) {
358 return -errno;
359 }
360
361 /* Undo the changes above */
362 iov[0].iov_base -= offset;
363 iov[0].iov_len += offset;
364
365 /* Prepare for the next iteration */
366 offset += len;
367 total += len;
368 size -= len;
369 }
370
371 return total;
372}
373
374static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
375{
376 QEMUFileSocket *s = opaque;
377 ssize_t len;
378
379 for (;;) {
380 len = read(s->fd, buf, size);
381 if (len != -1) {
382 break;
383 }
384 if (errno == EAGAIN) {
385 yield_until_fd_readable(s->fd);
386 } else if (errno != EINTR) {
387 break;
388 }
389 }
390
391 if (len == -1) {
392 len = -errno;
393 }
394 return len;
395}
396
397static int unix_close(void *opaque)
398{
399 QEMUFileSocket *s = opaque;
400 close(s->fd);
401 g_free(s);
402 return 0;
403}
404
405static const QEMUFileOps unix_read_ops = {
406 .get_fd = socket_get_fd,
407 .get_buffer = unix_get_buffer,
408 .close = unix_close
409};
410
411static const QEMUFileOps unix_write_ops = {
412 .get_fd = socket_get_fd,
413 .writev_buffer = unix_writev_buffer,
414 .close = unix_close
415};
416
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200417QEMUFile *qemu_fdopen(int fd, const char *mode)
418{
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100419 QEMUFileSocket *s;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200420
421 if (mode == NULL ||
Eduardo Habkost5cecf412013-11-28 12:01:12 -0200422 (mode[0] != 'r' && mode[0] != 'w') ||
423 mode[1] != 'b' || mode[2] != 0) {
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200424 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
425 return NULL;
426 }
427
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100428 s = g_malloc0(sizeof(QEMUFileSocket));
429 s->fd = fd;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200430
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200431 if (mode[0] == 'r') {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100432 s->file = qemu_fopen_ops(s, &unix_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200433 } else {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100434 s->file = qemu_fopen_ops(s, &unix_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200435 }
436 return s->file;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200437}
438
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200439static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200440 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200441 .get_buffer = socket_get_buffer,
442 .close = socket_close
443};
444
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100445static const QEMUFileOps socket_write_ops = {
446 .get_fd = socket_get_fd,
Orit Wasserman28085f72013-03-22 16:47:58 +0200447 .writev_buffer = socket_writev_buffer,
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100448 .close = socket_close
449};
450
Michael R. Hinesbc1256f2013-06-25 21:35:31 -0400451bool qemu_file_mode_is_not_valid(const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000452{
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100453 if (mode == NULL ||
454 (mode[0] != 'r' && mode[0] != 'w') ||
455 mode[1] != 'b' || mode[2] != 0) {
456 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Michael R. Hinesbc1256f2013-06-25 21:35:31 -0400457 return true;
458 }
459
460 return false;
461}
462
463QEMUFile *qemu_fopen_socket(int fd, const char *mode)
464{
465 QEMUFileSocket *s;
466
467 if (qemu_file_mode_is_not_valid(mode)) {
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100468 return NULL;
469 }
470
Stefan Weil4f080052013-06-16 13:33:05 +0200471 s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000472 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100473 if (mode[0] == 'w') {
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100474 qemu_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100475 s->file = qemu_fopen_ops(s, &socket_write_ops);
476 } else {
477 s->file = qemu_fopen_ops(s, &socket_read_ops);
478 }
aliguoria672b462008-11-11 21:33:36 +0000479 return s->file;
480}
481
aliguoria672b462008-11-11 21:33:36 +0000482QEMUFile *qemu_fopen(const char *filename, const char *mode)
483{
484 QEMUFileStdio *s;
485
Michael R. Hinesbc1256f2013-06-25 21:35:31 -0400486 if (qemu_file_mode_is_not_valid(mode)) {
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200487 return NULL;
488 }
489
Anthony Liguori7267c092011-08-20 22:09:37 -0500490 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000491
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200492 s->stdio_file = fopen(filename, mode);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200493 if (!s->stdio_file) {
aliguoria672b462008-11-11 21:33:36 +0000494 goto fail;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200495 }
496
497 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{
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200553 if (is_writable) {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200554 return qemu_fopen_ops(bs, &bdrv_write_ops);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200555 }
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200556 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000557}
558
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200559QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000560{
561 QEMUFile *f;
562
Anthony Liguori7267c092011-08-20 22:09:37 -0500563 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000564
565 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200566 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000567 return f;
568}
569
Lei Li675fd0a2013-09-04 17:02:34 +0800570/*
571 * Get last error for stream f
572 *
573 * Return negative error value if there has been an error on previous
574 * operations, return 0 if no error happened.
575 *
576 */
Juan Quintela624b9cc2011-10-05 01:02:52 +0200577int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000578{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200579 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000580}
581
Eduardo Habkostc9615142013-11-28 12:01:10 -0200582void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000583{
Juan Quintelaafe41932013-01-14 13:36:28 +0100584 if (f->last_error == 0) {
585 f->last_error = ret;
586 }
aliguori4dabe242009-04-05 19:30:51 +0000587}
588
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200589static inline bool qemu_file_is_writable(QEMUFile *f)
590{
591 return f->ops->writev_buffer || f->ops->put_buffer;
592}
593
Orit Wassermancb88aa82013-03-22 16:48:01 +0200594/**
595 * Flushes QEMUFile buffer
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200596 *
Orit Wassermancb88aa82013-03-22 16:48:01 +0200597 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
598 * put_buffer ops.
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200599 */
Michael R. Hinesbe903b22013-06-25 21:35:32 -0400600void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000601{
Orit Wassermancb88aa82013-03-22 16:48:01 +0200602 ssize_t ret = 0;
Juan Quintela7311bea2012-08-29 19:08:59 +0200603
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200604 if (!qemu_file_is_writable(f)) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100605 return;
606 }
Orit Wassermancb88aa82013-03-22 16:48:01 +0200607
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200608 if (f->ops->writev_buffer) {
609 if (f->iovcnt > 0) {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200610 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
Juan Quintela7311bea2012-08-29 19:08:59 +0200611 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200612 } else {
613 if (f->buf_index > 0) {
614 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200615 }
aliguoria672b462008-11-11 21:33:36 +0000616 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200617 if (ret >= 0) {
618 f->pos += ret;
619 }
620 f->buf_index = 0;
621 f->iovcnt = 0;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100622 if (ret < 0) {
623 qemu_file_set_error(f, ret);
624 }
aliguoria672b462008-11-11 21:33:36 +0000625}
626
Michael R. Hines43487c62013-06-25 21:35:35 -0400627void ram_control_before_iterate(QEMUFile *f, uint64_t flags)
628{
629 int ret = 0;
630
631 if (f->ops->before_ram_iterate) {
632 ret = f->ops->before_ram_iterate(f, f->opaque, flags);
633 if (ret < 0) {
634 qemu_file_set_error(f, ret);
635 }
636 }
637}
638
639void ram_control_after_iterate(QEMUFile *f, uint64_t flags)
640{
641 int ret = 0;
642
643 if (f->ops->after_ram_iterate) {
644 ret = f->ops->after_ram_iterate(f, f->opaque, flags);
645 if (ret < 0) {
646 qemu_file_set_error(f, ret);
647 }
648 }
649}
650
651void ram_control_load_hook(QEMUFile *f, uint64_t flags)
652{
Lei Lic77a5f22013-09-04 17:02:35 +0800653 int ret = -EINVAL;
Michael R. Hines43487c62013-06-25 21:35:35 -0400654
655 if (f->ops->hook_ram_load) {
656 ret = f->ops->hook_ram_load(f, f->opaque, flags);
657 if (ret < 0) {
658 qemu_file_set_error(f, ret);
659 }
660 } else {
661 qemu_file_set_error(f, ret);
662 }
663}
664
665size_t ram_control_save_page(QEMUFile *f, ram_addr_t block_offset,
666 ram_addr_t offset, size_t size, int *bytes_sent)
667{
668 if (f->ops->save_page) {
669 int ret = f->ops->save_page(f, f->opaque, block_offset,
670 offset, size, bytes_sent);
671
672 if (ret != RAM_SAVE_CONTROL_DELAYED) {
Michael R. Hinesde7b6852013-07-22 10:01:52 -0400673 if (bytes_sent && *bytes_sent > 0) {
Michael R. Hines43487c62013-06-25 21:35:35 -0400674 qemu_update_position(f, *bytes_sent);
675 } else if (ret < 0) {
676 qemu_file_set_error(f, ret);
677 }
678 }
679
680 return ret;
681 }
682
683 return RAM_SAVE_CONTROL_NOT_SUPP;
684}
685
aliguoria672b462008-11-11 21:33:36 +0000686static void qemu_fill_buffer(QEMUFile *f)
687{
688 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200689 int pending;
aliguoria672b462008-11-11 21:33:36 +0000690
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200691 assert(!qemu_file_is_writable(f));
aliguoria672b462008-11-11 21:33:36 +0000692
Juan Quintela0046c452011-09-30 19:28:45 +0200693 pending = f->buf_size - f->buf_index;
694 if (pending > 0) {
695 memmove(f->buf, f->buf + f->buf_index, pending);
696 }
697 f->buf_index = 0;
698 f->buf_size = pending;
699
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100700 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200701 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000702 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200703 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100704 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200705 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200706 qemu_file_set_error(f, -EIO);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200707 } else if (len != -EAGAIN) {
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200708 qemu_file_set_error(f, len);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200709 }
aliguoria672b462008-11-11 21:33:36 +0000710}
711
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200712int qemu_get_fd(QEMUFile *f)
713{
714 if (f->ops->get_fd) {
715 return f->ops->get_fd(f->opaque);
716 }
717 return -1;
718}
719
Michael R. Hines2b0ce072013-06-25 21:35:28 -0400720void qemu_update_position(QEMUFile *f, size_t size)
721{
722 f->pos += size;
723}
724
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200725/** Closes the file
726 *
727 * Returns negative error value if any error happened on previous operations or
728 * while closing the file. Returns 0 or positive number on success.
729 *
730 * The meaning of return value on success depends on the specific backend
731 * being used.
732 */
733int qemu_fclose(QEMUFile *f)
734{
Juan Quintela29eee862012-08-29 19:14:54 +0200735 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100736 qemu_fflush(f);
737 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200738
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200739 if (f->ops->close) {
740 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200741 if (ret >= 0) {
742 ret = ret2;
743 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200744 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200745 /* If any error was spotted before closing, we should report it
746 * instead of the close() return value.
747 */
748 if (f->last_error) {
749 ret = f->last_error;
750 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500751 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000752 return ret;
753}
754
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200755static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
756{
757 /* check for adjacent buffer and coalesce them */
758 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
759 f->iov[f->iovcnt - 1].iov_len) {
760 f->iov[f->iovcnt - 1].iov_len += size;
761 } else {
762 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
763 f->iov[f->iovcnt++].iov_len = size;
764 }
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200765
Paolo Bonzini4d117242013-04-08 13:29:57 +0200766 if (f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200767 qemu_fflush(f);
768 }
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200769}
770
Orit Wasserman6181ec22013-03-22 16:48:02 +0200771void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
772{
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200773 if (!f->ops->writev_buffer) {
774 qemu_put_buffer(f, buf, size);
775 return;
776 }
777
Orit Wasserman6181ec22013-03-22 16:48:02 +0200778 if (f->last_error) {
779 return;
780 }
781
Orit Wasserman6181ec22013-03-22 16:48:02 +0200782 f->bytes_xfer += size;
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200783 add_to_iovec(f, buf, size);
Orit Wasserman6181ec22013-03-22 16:48:02 +0200784}
785
aliguoria672b462008-11-11 21:33:36 +0000786void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
787{
788 int l;
789
Juan Quintelac10682c2012-08-29 19:43:39 +0200790 if (f->last_error) {
791 return;
792 }
793
Juan Quintelac10682c2012-08-29 19:43:39 +0200794 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000795 l = IO_BUF_SIZE - f->buf_index;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200796 if (l > size) {
aliguoria672b462008-11-11 21:33:36 +0000797 l = size;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -0200798 }
aliguoria672b462008-11-11 21:33:36 +0000799 memcpy(f->buf + f->buf_index, buf, l);
Wangting (Kathy)8e867292013-11-19 05:53:45 +0000800 f->bytes_xfer += l;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200801 if (f->ops->writev_buffer) {
802 add_to_iovec(f, f->buf + f->buf_index, l);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200803 }
804 f->buf_index += l;
805 if (f->buf_index == IO_BUF_SIZE) {
806 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200807 }
Orit Wasserman6181ec22013-03-22 16:48:02 +0200808 if (qemu_file_get_error(f)) {
809 break;
810 }
aliguoria672b462008-11-11 21:33:36 +0000811 buf += l;
812 size -= l;
aliguoria672b462008-11-11 21:33:36 +0000813 }
814}
815
816void qemu_put_byte(QEMUFile *f, int v)
817{
Juan Quintelac10682c2012-08-29 19:43:39 +0200818 if (f->last_error) {
819 return;
820 }
821
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200822 f->buf[f->buf_index] = v;
Orit Wasserman7d8a30b2013-03-22 16:47:59 +0200823 f->bytes_xfer++;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200824 if (f->ops->writev_buffer) {
825 add_to_iovec(f, f->buf + f->buf_index, 1);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200826 }
827 f->buf_index++;
828 if (f->buf_index == IO_BUF_SIZE) {
829 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200830 }
aliguoria672b462008-11-11 21:33:36 +0000831}
832
Eduardo Habkostc9615142013-11-28 12:01:10 -0200833void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000834{
Juan Quintelac6380722011-10-04 15:28:31 +0200835 if (f->buf_index + size <= f->buf_size) {
836 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000837 }
aliguoria672b462008-11-11 21:33:36 +0000838}
839
Eduardo Habkostc9615142013-11-28 12:01:10 -0200840int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200841{
Juan Quintelac6380722011-10-04 15:28:31 +0200842 int pending;
843 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200844
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200845 assert(!qemu_file_is_writable(f));
Juan Quintela811814b2010-07-26 21:38:43 +0200846
Juan Quintelac6380722011-10-04 15:28:31 +0200847 index = f->buf_index + offset;
848 pending = f->buf_size - index;
849 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200850 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200851 index = f->buf_index + offset;
852 pending = f->buf_size - index;
853 }
854
855 if (pending <= 0) {
856 return 0;
857 }
858 if (size > pending) {
859 size = pending;
860 }
861
862 memcpy(buf, f->buf + index, size);
863 return size;
864}
865
866int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
867{
868 int pending = size;
869 int done = 0;
870
871 while (pending > 0) {
872 int res;
873
874 res = qemu_peek_buffer(f, buf, pending, 0);
875 if (res == 0) {
876 return done;
877 }
878 qemu_file_skip(f, res);
879 buf += res;
880 pending -= res;
881 done += res;
882 }
883 return done;
884}
885
Eduardo Habkostc9615142013-11-28 12:01:10 -0200886int qemu_peek_byte(QEMUFile *f, int offset)
Juan Quintelac6380722011-10-04 15:28:31 +0200887{
888 int index = f->buf_index + offset;
889
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200890 assert(!qemu_file_is_writable(f));
Juan Quintelac6380722011-10-04 15:28:31 +0200891
892 if (index >= f->buf_size) {
893 qemu_fill_buffer(f);
894 index = f->buf_index + offset;
895 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200896 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200897 }
Juan Quintela811814b2010-07-26 21:38:43 +0200898 }
Juan Quintelac6380722011-10-04 15:28:31 +0200899 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200900}
901
aliguoria672b462008-11-11 21:33:36 +0000902int qemu_get_byte(QEMUFile *f)
903{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200904 int result;
aliguoria672b462008-11-11 21:33:36 +0000905
Juan Quintelac6380722011-10-04 15:28:31 +0200906 result = qemu_peek_byte(f, 0);
907 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200908 return result;
aliguoria672b462008-11-11 21:33:36 +0000909}
910
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100911int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000912{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100913 qemu_fflush(f);
914 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000915}
916
aliguoria672b462008-11-11 21:33:36 +0000917int qemu_file_rate_limit(QEMUFile *f)
918{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100919 if (qemu_file_get_error(f)) {
920 return 1;
921 }
922 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
923 return 1;
924 }
aliguoria672b462008-11-11 21:33:36 +0000925 return 0;
926}
927
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200928int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200929{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100930 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200931}
932
Paolo Bonzini1964a392013-02-22 17:36:45 +0100933void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400934{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100935 f->xfer_limit = limit;
936}
Glauber Costa19629532009-05-20 18:26:57 -0400937
Paolo Bonzini1964a392013-02-22 17:36:45 +0100938void qemu_file_reset_rate_limit(QEMUFile *f)
939{
940 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400941}
942
aliguoria672b462008-11-11 21:33:36 +0000943void qemu_put_be16(QEMUFile *f, unsigned int v)
944{
945 qemu_put_byte(f, v >> 8);
946 qemu_put_byte(f, v);
947}
948
949void qemu_put_be32(QEMUFile *f, unsigned int v)
950{
951 qemu_put_byte(f, v >> 24);
952 qemu_put_byte(f, v >> 16);
953 qemu_put_byte(f, v >> 8);
954 qemu_put_byte(f, v);
955}
956
957void qemu_put_be64(QEMUFile *f, uint64_t v)
958{
959 qemu_put_be32(f, v >> 32);
960 qemu_put_be32(f, v);
961}
962
963unsigned int qemu_get_be16(QEMUFile *f)
964{
965 unsigned int v;
966 v = qemu_get_byte(f) << 8;
967 v |= qemu_get_byte(f);
968 return v;
969}
970
971unsigned int qemu_get_be32(QEMUFile *f)
972{
973 unsigned int v;
974 v = qemu_get_byte(f) << 24;
975 v |= qemu_get_byte(f) << 16;
976 v |= qemu_get_byte(f) << 8;
977 v |= qemu_get_byte(f);
978 return v;
979}
980
981uint64_t qemu_get_be64(QEMUFile *f)
982{
983 uint64_t v;
984 v = (uint64_t)qemu_get_be32(f) << 32;
985 v |= qemu_get_be32(f);
986 return v;
987}
988
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200989
990/* timer */
991
Alex Bligh40daca52013-08-21 16:03:02 +0100992void timer_put(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200993{
994 uint64_t expire_time;
995
Alex Blighe93379b2013-08-21 16:02:39 +0100996 expire_time = timer_expire_time_ns(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200997 qemu_put_be64(f, expire_time);
998}
999
Alex Bligh40daca52013-08-21 16:03:02 +01001000void timer_get(QEMUFile *f, QEMUTimer *ts)
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02001001{
1002 uint64_t expire_time;
1003
1004 expire_time = qemu_get_be64(f);
1005 if (expire_time != -1) {
Alex Blighbc72ad62013-08-21 16:03:08 +01001006 timer_mod_ns(ts, expire_time);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02001007 } else {
Alex Blighbc72ad62013-08-21 16:03:08 +01001008 timer_del(ts);
Paolo Bonzini2ff68d02011-09-12 16:21:44 +02001009 }
1010}
1011
1012
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +01001013/* bool */
1014
1015static int get_bool(QEMUFile *f, void *pv, size_t size)
1016{
1017 bool *v = pv;
1018 *v = qemu_get_byte(f);
1019 return 0;
1020}
1021
1022static void put_bool(QEMUFile *f, void *pv, size_t size)
1023{
1024 bool *v = pv;
1025 qemu_put_byte(f, *v);
1026}
1027
1028const VMStateInfo vmstate_info_bool = {
1029 .name = "bool",
1030 .get = get_bool,
1031 .put = put_bool,
1032};
1033
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001034/* 8 bit int */
1035
1036static int get_int8(QEMUFile *f, void *pv, size_t size)
1037{
1038 int8_t *v = pv;
1039 qemu_get_s8s(f, v);
1040 return 0;
1041}
1042
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001043static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001044{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001045 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001046 qemu_put_s8s(f, v);
1047}
1048
1049const VMStateInfo vmstate_info_int8 = {
1050 .name = "int8",
1051 .get = get_int8,
1052 .put = put_int8,
1053};
1054
1055/* 16 bit int */
1056
1057static int get_int16(QEMUFile *f, void *pv, size_t size)
1058{
1059 int16_t *v = pv;
1060 qemu_get_sbe16s(f, v);
1061 return 0;
1062}
1063
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001064static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001065{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001066 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001067 qemu_put_sbe16s(f, v);
1068}
1069
1070const VMStateInfo vmstate_info_int16 = {
1071 .name = "int16",
1072 .get = get_int16,
1073 .put = put_int16,
1074};
1075
1076/* 32 bit int */
1077
1078static int get_int32(QEMUFile *f, void *pv, size_t size)
1079{
1080 int32_t *v = pv;
1081 qemu_get_sbe32s(f, v);
1082 return 0;
1083}
1084
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001085static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001086{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001087 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001088 qemu_put_sbe32s(f, v);
1089}
1090
1091const VMStateInfo vmstate_info_int32 = {
1092 .name = "int32",
1093 .get = get_int32,
1094 .put = put_int32,
1095};
1096
Juan Quintela82501662009-08-20 19:42:32 +02001097/* 32 bit int. See that the received value is the same than the one
1098 in the field */
1099
1100static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1101{
1102 int32_t *v = pv;
1103 int32_t v2;
1104 qemu_get_sbe32s(f, &v2);
1105
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001106 if (*v == v2) {
Juan Quintela82501662009-08-20 19:42:32 +02001107 return 0;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001108 }
Juan Quintela82501662009-08-20 19:42:32 +02001109 return -EINVAL;
1110}
1111
1112const VMStateInfo vmstate_info_int32_equal = {
1113 .name = "int32 equal",
1114 .get = get_int32_equal,
1115 .put = put_int32,
1116};
1117
Juan Quintela0a031e02009-08-20 19:42:37 +02001118/* 32 bit int. See that the received value is the less or the same
1119 than the one in the field */
1120
1121static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1122{
1123 int32_t *old = pv;
1124 int32_t new;
1125 qemu_get_sbe32s(f, &new);
1126
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001127 if (*old <= new) {
Juan Quintela0a031e02009-08-20 19:42:37 +02001128 return 0;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001129 }
Juan Quintela0a031e02009-08-20 19:42:37 +02001130 return -EINVAL;
1131}
1132
1133const VMStateInfo vmstate_info_int32_le = {
1134 .name = "int32 equal",
1135 .get = get_int32_le,
1136 .put = put_int32,
1137};
1138
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001139/* 64 bit int */
1140
1141static int get_int64(QEMUFile *f, void *pv, size_t size)
1142{
1143 int64_t *v = pv;
1144 qemu_get_sbe64s(f, v);
1145 return 0;
1146}
1147
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001148static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001149{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001150 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001151 qemu_put_sbe64s(f, v);
1152}
1153
1154const VMStateInfo vmstate_info_int64 = {
1155 .name = "int64",
1156 .get = get_int64,
1157 .put = put_int64,
1158};
1159
1160/* 8 bit unsigned int */
1161
1162static int get_uint8(QEMUFile *f, void *pv, size_t size)
1163{
1164 uint8_t *v = pv;
1165 qemu_get_8s(f, v);
1166 return 0;
1167}
1168
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001169static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001170{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001171 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001172 qemu_put_8s(f, v);
1173}
1174
1175const VMStateInfo vmstate_info_uint8 = {
1176 .name = "uint8",
1177 .get = get_uint8,
1178 .put = put_uint8,
1179};
1180
1181/* 16 bit unsigned int */
1182
1183static int get_uint16(QEMUFile *f, void *pv, size_t size)
1184{
1185 uint16_t *v = pv;
1186 qemu_get_be16s(f, v);
1187 return 0;
1188}
1189
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001190static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001191{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001192 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001193 qemu_put_be16s(f, v);
1194}
1195
1196const VMStateInfo vmstate_info_uint16 = {
1197 .name = "uint16",
1198 .get = get_uint16,
1199 .put = put_uint16,
1200};
1201
1202/* 32 bit unsigned int */
1203
1204static int get_uint32(QEMUFile *f, void *pv, size_t size)
1205{
1206 uint32_t *v = pv;
1207 qemu_get_be32s(f, v);
1208 return 0;
1209}
1210
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001211static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001212{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001213 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001214 qemu_put_be32s(f, v);
1215}
1216
1217const VMStateInfo vmstate_info_uint32 = {
1218 .name = "uint32",
1219 .get = get_uint32,
1220 .put = put_uint32,
1221};
1222
Juan Quintela9122a8f2011-03-10 12:33:48 +01001223/* 32 bit uint. See that the received value is the same than the one
1224 in the field */
1225
1226static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1227{
1228 uint32_t *v = pv;
1229 uint32_t v2;
1230 qemu_get_be32s(f, &v2);
1231
1232 if (*v == v2) {
1233 return 0;
1234 }
1235 return -EINVAL;
1236}
1237
1238const VMStateInfo vmstate_info_uint32_equal = {
1239 .name = "uint32 equal",
1240 .get = get_uint32_equal,
1241 .put = put_uint32,
1242};
1243
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001244/* 64 bit unsigned int */
1245
1246static int get_uint64(QEMUFile *f, void *pv, size_t size)
1247{
1248 uint64_t *v = pv;
1249 qemu_get_be64s(f, v);
1250 return 0;
1251}
1252
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001253static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001254{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001255 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001256 qemu_put_be64s(f, v);
1257}
1258
1259const VMStateInfo vmstate_info_uint64 = {
1260 .name = "uint64",
1261 .get = get_uint64,
1262 .put = put_uint64,
1263};
1264
David Gibsone344b8a2013-03-12 14:06:00 +11001265/* 64 bit unsigned int. See that the received value is the same than the one
1266 in the field */
1267
1268static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1269{
1270 uint64_t *v = pv;
1271 uint64_t v2;
1272 qemu_get_be64s(f, &v2);
1273
1274 if (*v == v2) {
1275 return 0;
1276 }
1277 return -EINVAL;
1278}
1279
1280const VMStateInfo vmstate_info_uint64_equal = {
1281 .name = "int64 equal",
1282 .get = get_uint64_equal,
1283 .put = put_uint64,
1284};
1285
Juan Quintela80cd83e2009-09-10 03:04:36 +02001286/* 8 bit int. See that the received value is the same than the one
1287 in the field */
1288
1289static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1290{
1291 uint8_t *v = pv;
1292 uint8_t v2;
1293 qemu_get_8s(f, &v2);
1294
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001295 if (*v == v2) {
Juan Quintela80cd83e2009-09-10 03:04:36 +02001296 return 0;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001297 }
Juan Quintela80cd83e2009-09-10 03:04:36 +02001298 return -EINVAL;
1299}
1300
1301const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001302 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001303 .get = get_uint8_equal,
1304 .put = put_uint8,
1305};
1306
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001307/* 16 bit unsigned int int. See that the received value is the same than the one
1308 in the field */
1309
1310static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1311{
1312 uint16_t *v = pv;
1313 uint16_t v2;
1314 qemu_get_be16s(f, &v2);
1315
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001316 if (*v == v2) {
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001317 return 0;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001318 }
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001319 return -EINVAL;
1320}
1321
1322const VMStateInfo vmstate_info_uint16_equal = {
1323 .name = "uint16 equal",
1324 .get = get_uint16_equal,
1325 .put = put_uint16,
1326};
1327
David Gibson213945e2013-03-12 14:06:02 +11001328/* floating point */
1329
1330static int get_float64(QEMUFile *f, void *pv, size_t size)
1331{
1332 float64 *v = pv;
1333
1334 *v = make_float64(qemu_get_be64(f));
1335 return 0;
1336}
1337
1338static void put_float64(QEMUFile *f, void *pv, size_t size)
1339{
1340 uint64_t *v = pv;
1341
1342 qemu_put_be64(f, float64_val(*v));
1343}
1344
1345const VMStateInfo vmstate_info_float64 = {
1346 .name = "float64",
1347 .get = get_float64,
1348 .put = put_float64,
1349};
1350
Juan Quinteladde04632009-08-20 19:42:26 +02001351/* timers */
1352
1353static int get_timer(QEMUFile *f, void *pv, size_t size)
1354{
1355 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +01001356 timer_get(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +02001357 return 0;
1358}
1359
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001360static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001361{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001362 QEMUTimer *v = pv;
Alex Bligh40daca52013-08-21 16:03:02 +01001363 timer_put(f, v);
Juan Quinteladde04632009-08-20 19:42:26 +02001364}
1365
1366const VMStateInfo vmstate_info_timer = {
1367 .name = "timer",
1368 .get = get_timer,
1369 .put = put_timer,
1370};
1371
Juan Quintela6f67c502009-08-20 19:42:35 +02001372/* uint8_t buffers */
1373
1374static int get_buffer(QEMUFile *f, void *pv, size_t size)
1375{
1376 uint8_t *v = pv;
1377 qemu_get_buffer(f, v, size);
1378 return 0;
1379}
1380
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001381static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001382{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001383 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001384 qemu_put_buffer(f, v, size);
1385}
1386
1387const VMStateInfo vmstate_info_buffer = {
1388 .name = "buffer",
1389 .get = get_buffer,
1390 .put = put_buffer,
1391};
1392
Juan Quintela76507c72009-10-19 15:46:28 +02001393/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001394 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001395
1396static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1397{
Jan Kiszka21174c32009-12-02 12:36:35 +01001398 uint8_t buf[1024];
1399 int block_len;
1400
1401 while (size > 0) {
1402 block_len = MIN(sizeof(buf), size);
1403 size -= block_len;
1404 qemu_get_buffer(f, buf, block_len);
1405 }
1406 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001407}
1408
1409static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1410{
Jan Kiszka21174c32009-12-02 12:36:35 +01001411 static const uint8_t buf[1024];
1412 int block_len;
1413
1414 while (size > 0) {
1415 block_len = MIN(sizeof(buf), size);
1416 size -= block_len;
1417 qemu_put_buffer(f, buf, block_len);
1418 }
Juan Quintela76507c72009-10-19 15:46:28 +02001419}
1420
1421const VMStateInfo vmstate_info_unused_buffer = {
1422 .name = "unused_buffer",
1423 .get = get_unused_buffer,
1424 .put = put_unused_buffer,
1425};
1426
Peter Maydell08e99e22012-10-30 07:45:12 +00001427/* bitmaps (as defined by bitmap.h). Note that size here is the size
1428 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1429 * bit words with the bits in big endian order. The in-memory format
1430 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1431 */
1432/* This is the number of 64 bit words sent over the wire */
1433#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1434static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1435{
1436 unsigned long *bmp = pv;
1437 int i, idx = 0;
1438 for (i = 0; i < BITS_TO_U64S(size); i++) {
1439 uint64_t w = qemu_get_be64(f);
1440 bmp[idx++] = w;
1441 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1442 bmp[idx++] = w >> 32;
1443 }
1444 }
1445 return 0;
1446}
1447
1448static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1449{
1450 unsigned long *bmp = pv;
1451 int i, idx = 0;
1452 for (i = 0; i < BITS_TO_U64S(size); i++) {
1453 uint64_t w = bmp[idx++];
1454 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1455 w |= ((uint64_t)bmp[idx++]) << 32;
1456 }
1457 qemu_put_be64(f, w);
1458 }
1459}
1460
1461const VMStateInfo vmstate_info_bitmap = {
1462 .name = "bitmap",
1463 .get = get_bitmap,
1464 .put = put_bitmap,
1465};
1466
Alex Williamson7685ee62010-06-25 11:09:14 -06001467typedef struct CompatEntry {
1468 char idstr[256];
1469 int instance_id;
1470} CompatEntry;
1471
aliguoria672b462008-11-11 21:33:36 +00001472typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001473 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001474 char idstr[256];
1475 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001476 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001477 int version_id;
1478 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001479 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001480 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001481 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001482 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001483 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001484 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001485} SaveStateEntry;
1486
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001487
Blue Swirl72cf2d42009-09-12 07:36:22 +00001488static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1489 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001490static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001491
Juan Quintela8718e992009-09-01 02:12:31 +02001492static int calculate_new_instance_id(const char *idstr)
1493{
1494 SaveStateEntry *se;
1495 int instance_id = 0;
1496
Blue Swirl72cf2d42009-09-12 07:36:22 +00001497 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001498 if (strcmp(idstr, se->idstr) == 0
1499 && instance_id <= se->instance_id) {
1500 instance_id = se->instance_id + 1;
1501 }
1502 }
1503 return instance_id;
1504}
1505
Alex Williamson7685ee62010-06-25 11:09:14 -06001506static int calculate_compat_instance_id(const char *idstr)
1507{
1508 SaveStateEntry *se;
1509 int instance_id = 0;
1510
1511 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001512 if (!se->compat) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001513 continue;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001514 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001515
1516 if (strcmp(idstr, se->compat->idstr) == 0
1517 && instance_id <= se->compat->instance_id) {
1518 instance_id = se->compat->instance_id + 1;
1519 }
1520 }
1521 return instance_id;
1522}
1523
aliguoria672b462008-11-11 21:33:36 +00001524/* TODO: Individual devices generally have very little idea about the rest
1525 of the system, so instance_id should be removed/replaced.
1526 Meanwhile pass -1 as instance_id if you do not already have a clearly
1527 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001528int register_savevm_live(DeviceState *dev,
1529 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001530 int instance_id,
1531 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001532 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001533 void *opaque)
1534{
Juan Quintela8718e992009-09-01 02:12:31 +02001535 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001536
Anthony Liguori7267c092011-08-20 22:09:37 -05001537 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001538 se->version_id = version_id;
1539 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001540 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001541 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001542 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001543 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001544 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001545 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001546 se->is_ram = 1;
1547 }
aliguoria672b462008-11-11 21:33:36 +00001548
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001549 if (dev) {
1550 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001551 if (id) {
1552 pstrcpy(se->idstr, sizeof(se->idstr), id);
1553 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001554 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001555
Anthony Liguori7267c092011-08-20 22:09:37 -05001556 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001557 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1558 se->compat->instance_id = instance_id == -1 ?
1559 calculate_compat_instance_id(idstr) : instance_id;
1560 instance_id = -1;
1561 }
1562 }
1563 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1564
Juan Quintela8718e992009-09-01 02:12:31 +02001565 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001566 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001567 } else {
1568 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001569 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001570 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001571 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001572 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001573 return 0;
1574}
1575
Alex Williamson0be71e32010-06-25 11:09:07 -06001576int register_savevm(DeviceState *dev,
1577 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001578 int instance_id,
1579 int version_id,
1580 SaveStateHandler *save_state,
1581 LoadStateHandler *load_state,
1582 void *opaque)
1583{
Juan Quintela7908c782012-06-26 18:46:10 +02001584 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1585 ops->save_state = save_state;
1586 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001587 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001588 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001589}
1590
Alex Williamson0be71e32010-06-25 11:09:07 -06001591void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001592{
Juan Quintela8718e992009-09-01 02:12:31 +02001593 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001594 char id[256] = "";
1595
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001596 if (dev) {
1597 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001598 if (path) {
1599 pstrcpy(id, sizeof(id), path);
1600 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001601 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001602 }
1603 }
1604 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001605
Blue Swirl72cf2d42009-09-12 07:36:22 +00001606 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001607 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001608 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001609 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001610 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001611 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001612 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001613 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001614 }
aliguori41bd13a2009-04-17 17:10:59 +00001615 }
1616}
1617
Alex Williamson0be71e32010-06-25 11:09:07 -06001618int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001619 const VMStateDescription *vmsd,
1620 void *opaque, int alias_id,
1621 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001622{
Juan Quintela8718e992009-09-01 02:12:31 +02001623 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001624
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001625 /* If this triggers, alias support can be dropped for the vmsd. */
1626 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1627
Anthony Liguori7267c092011-08-20 22:09:37 -05001628 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001629 se->version_id = vmsd->version_id;
1630 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001631 se->opaque = opaque;
1632 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001633 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001634 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001635
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001636 if (dev) {
1637 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001638 if (id) {
1639 pstrcpy(se->idstr, sizeof(se->idstr), id);
1640 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001641 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001642
Anthony Liguori7267c092011-08-20 22:09:37 -05001643 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001644 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1645 se->compat->instance_id = instance_id == -1 ?
1646 calculate_compat_instance_id(vmsd->name) : instance_id;
1647 instance_id = -1;
1648 }
1649 }
1650 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1651
Juan Quintela8718e992009-09-01 02:12:31 +02001652 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001653 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001654 } else {
1655 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001656 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001657 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001658 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001659 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001660 return 0;
1661}
1662
Alex Williamson0be71e32010-06-25 11:09:07 -06001663void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1664 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001665{
Juan Quintela1eb75382009-09-10 03:04:29 +02001666 SaveStateEntry *se, *new_se;
1667
Blue Swirl72cf2d42009-09-12 07:36:22 +00001668 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001669 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001670 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001671 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001672 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001673 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001674 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001675 }
1676 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001677}
1678
Juan Quintela811814b2010-07-26 21:38:43 +02001679static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1680 void *opaque);
1681static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1682 void *opaque);
1683
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001684int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1685 void *opaque, int version_id)
1686{
1687 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001688 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001689
1690 if (version_id > vmsd->version_id) {
1691 return -EINVAL;
1692 }
1693 if (version_id < vmsd->minimum_version_id_old) {
1694 return -EINVAL;
1695 }
1696 if (version_id < vmsd->minimum_version_id) {
1697 return vmsd->load_state_old(f, opaque, version_id);
1698 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001699 if (vmsd->pre_load) {
1700 int ret = vmsd->pre_load(opaque);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001701 if (ret) {
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001702 return ret;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001703 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001704 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001705 while (field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001706 if ((field->field_exists &&
1707 field->field_exists(opaque, version_id)) ||
1708 (!field->field_exists &&
1709 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001710 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001711 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001712 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001713
Juan Quintelae61a1e02009-12-02 12:36:38 +01001714 if (field->flags & VMS_VBUFFER) {
1715 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001716 if (field->flags & VMS_MULTIPLY) {
1717 size *= field->size;
1718 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001719 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001720 if (field->flags & VMS_ARRAY) {
1721 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001722 } else if (field->flags & VMS_VARRAY_INT32) {
1723 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001724 } else if (field->flags & VMS_VARRAY_UINT32) {
1725 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001726 } else if (field->flags & VMS_VARRAY_UINT16) {
1727 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001728 } else if (field->flags & VMS_VARRAY_UINT8) {
1729 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001730 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001731 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001732 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001733 }
1734 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001735 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001736
Juan Quintela19df4382009-09-29 22:48:41 +02001737 if (field->flags & VMS_ARRAY_OF_POINTER) {
1738 addr = *(void **)addr;
1739 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001740 if (field->flags & VMS_STRUCT) {
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001741 ret = vmstate_load_state(f, field->vmsd, addr,
1742 field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001743 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001744 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001745
1746 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001747 if (ret < 0) {
1748 return ret;
1749 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001750 }
1751 }
1752 field++;
1753 }
Juan Quintela811814b2010-07-26 21:38:43 +02001754 ret = vmstate_subsection_load(f, vmsd, opaque);
1755 if (ret != 0) {
1756 return ret;
1757 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001758 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001759 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001760 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001761 return 0;
1762}
1763
1764void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001765 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001766{
1767 VMStateField *field = vmsd->fields;
1768
Juan Quintela8fb07912009-09-10 03:04:32 +02001769 if (vmsd->pre_save) {
1770 vmsd->pre_save(opaque);
1771 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001772 while (field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001773 if (!field->field_exists ||
1774 field->field_exists(opaque, vmsd->version_id)) {
1775 void *base_addr = opaque + field->offset;
1776 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001777 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001778
Juan Quintelae61a1e02009-12-02 12:36:38 +01001779 if (field->flags & VMS_VBUFFER) {
1780 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001781 if (field->flags & VMS_MULTIPLY) {
1782 size *= field->size;
1783 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001784 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001785 if (field->flags & VMS_ARRAY) {
1786 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001787 } else if (field->flags & VMS_VARRAY_INT32) {
1788 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001789 } else if (field->flags & VMS_VARRAY_UINT32) {
1790 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001791 } else if (field->flags & VMS_VARRAY_UINT16) {
1792 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001793 } else if (field->flags & VMS_VARRAY_UINT8) {
1794 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001795 }
1796 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001797 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001798 }
1799 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001800 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001801
Juan Quintela85953872009-12-02 12:36:37 +01001802 if (field->flags & VMS_ARRAY_OF_POINTER) {
1803 addr = *(void **)addr;
1804 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001805 if (field->flags & VMS_STRUCT) {
1806 vmstate_save_state(f, field->vmsd, addr);
1807 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001808 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001809 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001810 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001811 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001812 field++;
1813 }
Juan Quintela811814b2010-07-26 21:38:43 +02001814 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001815}
1816
Juan Quintela4082be42009-08-20 19:42:24 +02001817static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1818{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001819 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001820 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001821 }
1822 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001823}
1824
Alex Williamsondc912122011-01-11 14:39:43 -07001825static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001826{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001827 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001828 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001829 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001830 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001831 vmstate_save_state(f, se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001832}
1833
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001834bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001835{
1836 SaveStateEntry *se;
1837
1838 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1839 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001840 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001841 return true;
1842 }
1843 }
1844 return false;
1845}
1846
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001847void qemu_savevm_state_begin(QEMUFile *f,
1848 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001849{
1850 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001851 int ret;
aliguoria672b462008-11-11 21:33:36 +00001852
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001853 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001854 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001855 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001856 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001857 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001858 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02001859
aliguoria672b462008-11-11 21:33:36 +00001860 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1861 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1862
Blue Swirl72cf2d42009-09-12 07:36:22 +00001863 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001864 int len;
1865
Juan Quintelad1315aa2012-06-28 15:11:57 +02001866 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001867 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001868 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001869 if (se->ops && se->ops->is_active) {
1870 if (!se->ops->is_active(se->opaque)) {
1871 continue;
1872 }
1873 }
aliguoria672b462008-11-11 21:33:36 +00001874 /* Section type */
1875 qemu_put_byte(f, QEMU_VM_SECTION_START);
1876 qemu_put_be32(f, se->section_id);
1877
1878 /* ID string */
1879 len = strlen(se->idstr);
1880 qemu_put_byte(f, len);
1881 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1882
1883 qemu_put_be32(f, se->instance_id);
1884 qemu_put_be32(f, se->version_id);
1885
Juan Quintelad1315aa2012-06-28 15:11:57 +02001886 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001887 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001888 qemu_file_set_error(f, ret);
1889 break;
Juan Quintela29757252011-10-19 15:22:18 +02001890 }
aliguoria672b462008-11-11 21:33:36 +00001891 }
aliguoria672b462008-11-11 21:33:36 +00001892}
1893
Juan Quintela39346382011-09-22 11:02:14 +02001894/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001895 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001896 * negative: there was one error, and we have -errno.
1897 * 0 : We haven't finished, caller have to go again
1898 * 1 : We have finished, we can go to complete phase
1899 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001900int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001901{
1902 SaveStateEntry *se;
1903 int ret = 1;
1904
Blue Swirl72cf2d42009-09-12 07:36:22 +00001905 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001906 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001907 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001908 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001909 if (se->ops && se->ops->is_active) {
1910 if (!se->ops->is_active(se->opaque)) {
1911 continue;
1912 }
1913 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001914 if (qemu_file_rate_limit(f)) {
1915 return 0;
1916 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001917 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001918 /* Section type */
1919 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1920 qemu_put_be32(f, se->section_id);
1921
Juan Quintela16310a32012-06-28 15:31:37 +02001922 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001923 trace_savevm_section_end(se->section_id);
1924
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001925 if (ret < 0) {
1926 qemu_file_set_error(f, ret);
1927 }
Juan Quintela29757252011-10-19 15:22:18 +02001928 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001929 /* Do not proceed to the next vmstate before this one reported
1930 completion of the current stage. This serializes the migration
1931 and reduces the probability that a faster changing state is
1932 synchronized over and over again. */
1933 break;
1934 }
aliguoria672b462008-11-11 21:33:36 +00001935 }
Juan Quintela39346382011-09-22 11:02:14 +02001936 return ret;
aliguoria672b462008-11-11 21:33:36 +00001937}
1938
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001939void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001940{
1941 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001942 int ret;
aliguoria672b462008-11-11 21:33:36 +00001943
Jan Kiszkaea375f92010-03-01 19:10:30 +01001944 cpu_synchronize_all_states();
1945
Blue Swirl72cf2d42009-09-12 07:36:22 +00001946 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001947 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001948 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001949 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001950 if (se->ops && se->ops->is_active) {
1951 if (!se->ops->is_active(se->opaque)) {
1952 continue;
1953 }
1954 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001955 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001956 /* Section type */
1957 qemu_put_byte(f, QEMU_VM_SECTION_END);
1958 qemu_put_be32(f, se->section_id);
1959
Juan Quintela16310a32012-06-28 15:31:37 +02001960 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001961 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001962 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001963 qemu_file_set_error(f, ret);
1964 return;
Juan Quintela29757252011-10-19 15:22:18 +02001965 }
aliguoria672b462008-11-11 21:33:36 +00001966 }
1967
Blue Swirl72cf2d42009-09-12 07:36:22 +00001968 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001969 int len;
1970
Juan Quintela22ea40f2012-06-26 17:19:10 +02001971 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Eduardo Habkost5cecf412013-11-28 12:01:12 -02001972 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001973 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001974 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001975 /* Section type */
1976 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1977 qemu_put_be32(f, se->section_id);
1978
1979 /* ID string */
1980 len = strlen(se->idstr);
1981 qemu_put_byte(f, len);
1982 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1983
1984 qemu_put_be32(f, se->instance_id);
1985 qemu_put_be32(f, se->version_id);
1986
Alex Williamsondc912122011-01-11 14:39:43 -07001987 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001988 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001989 }
1990
1991 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001992 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001993}
1994
Juan Quintelae4ed1542012-09-21 11:18:18 +02001995uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1996{
1997 SaveStateEntry *se;
1998 uint64_t ret = 0;
1999
2000 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2001 if (!se->ops || !se->ops->save_live_pending) {
2002 continue;
2003 }
2004 if (se->ops && se->ops->is_active) {
2005 if (!se->ops->is_active(se->opaque)) {
2006 continue;
2007 }
2008 }
2009 ret += se->ops->save_live_pending(f, se->opaque, max_size);
2010 }
2011 return ret;
2012}
2013
Juan Quintela65227732013-01-14 14:14:42 +01002014void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002015{
2016 SaveStateEntry *se;
2017
2018 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02002019 if (se->ops && se->ops->cancel) {
2020 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01002021 }
2022 }
2023}
2024
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002025static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00002026{
aliguoria672b462008-11-11 21:33:36 +00002027 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03002028 MigrationParams params = {
2029 .blk = 0,
2030 .shared = 0
2031 };
aliguoria672b462008-11-11 21:33:36 +00002032
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002033 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01002034 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07002035 }
2036
Paolo Bonzini9b095032013-02-22 17:36:28 +01002037 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002038 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01002039 qemu_mutex_lock_iothread();
2040
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002041 while (qemu_file_get_error(f) == 0) {
2042 if (qemu_savevm_state_iterate(f) > 0) {
2043 break;
2044 }
2045 }
aliguoria672b462008-11-11 21:33:36 +00002046
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002047 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02002048 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002049 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002050 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02002051 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01002052 if (ret != 0) {
2053 qemu_savevm_state_cancel();
2054 }
aliguoria672b462008-11-11 21:33:36 +00002055 return ret;
2056}
2057
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002058static int qemu_save_device_state(QEMUFile *f)
2059{
2060 SaveStateEntry *se;
2061
2062 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2063 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2064
2065 cpu_synchronize_all_states();
2066
2067 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2068 int len;
2069
2070 if (se->is_ram) {
2071 continue;
2072 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02002073 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002074 continue;
2075 }
2076
2077 /* Section type */
2078 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2079 qemu_put_be32(f, se->section_id);
2080
2081 /* ID string */
2082 len = strlen(se->idstr);
2083 qemu_put_byte(f, len);
2084 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2085
2086 qemu_put_be32(f, se->instance_id);
2087 qemu_put_be32(f, se->version_id);
2088
2089 vmstate_save(f, se);
2090 }
2091
2092 qemu_put_byte(f, QEMU_VM_EOF);
2093
2094 return qemu_file_get_error(f);
2095}
2096
aliguoria672b462008-11-11 21:33:36 +00002097static SaveStateEntry *find_se(const char *idstr, int instance_id)
2098{
2099 SaveStateEntry *se;
2100
Blue Swirl72cf2d42009-09-12 07:36:22 +00002101 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00002102 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02002103 (instance_id == se->instance_id ||
2104 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00002105 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06002106 /* Migrating from an older version? */
2107 if (strstr(se->idstr, idstr) && se->compat) {
2108 if (!strcmp(se->compat->idstr, idstr) &&
2109 (instance_id == se->compat->instance_id ||
2110 instance_id == se->alias_id))
2111 return se;
2112 }
aliguoria672b462008-11-11 21:33:36 +00002113 }
2114 return NULL;
2115}
2116
Juan Quintela811814b2010-07-26 21:38:43 +02002117static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2118{
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002119 while (sub && sub->needed) {
Juan Quintela811814b2010-07-26 21:38:43 +02002120 if (strcmp(idstr, sub->vmsd->name) == 0) {
2121 return sub->vmsd;
2122 }
2123 sub++;
2124 }
2125 return NULL;
2126}
2127
2128static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2129 void *opaque)
2130{
Juan Quintelac6380722011-10-04 15:28:31 +02002131 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02002132 char idstr[256];
2133 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02002134 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02002135 const VMStateDescription *sub_vmsd;
2136
Juan Quintelac6380722011-10-04 15:28:31 +02002137 len = qemu_peek_byte(f, 1);
2138 if (len < strlen(vmsd->name) + 1) {
2139 /* subsection name has be be "section_name/a" */
2140 return 0;
2141 }
2142 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2143 if (size != len) {
2144 return 0;
2145 }
2146 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02002147
Juan Quintelac6380722011-10-04 15:28:31 +02002148 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2149 /* it don't have a valid subsection name */
2150 return 0;
2151 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02002152 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02002153 if (sub_vmsd == NULL) {
2154 return -ENOENT;
2155 }
Juan Quintelac6380722011-10-04 15:28:31 +02002156 qemu_file_skip(f, 1); /* subsection */
2157 qemu_file_skip(f, 1); /* len */
2158 qemu_file_skip(f, len); /* idstr */
2159 version_id = qemu_get_be32(f);
2160
Juan Quintela811814b2010-07-26 21:38:43 +02002161 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2162 if (ret) {
2163 return ret;
2164 }
2165 }
2166 return 0;
2167}
2168
2169static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2170 void *opaque)
2171{
2172 const VMStateSubsection *sub = vmsd->subsections;
2173
2174 while (sub && sub->needed) {
2175 if (sub->needed(opaque)) {
2176 const VMStateDescription *vmsd = sub->vmsd;
2177 uint8_t len;
2178
2179 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2180 len = strlen(vmsd->name);
2181 qemu_put_byte(f, len);
2182 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2183 qemu_put_be32(f, vmsd->version_id);
2184 vmstate_save_state(f, vmsd, opaque);
2185 }
2186 sub++;
2187 }
2188}
2189
aliguoria672b462008-11-11 21:33:36 +00002190typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002191 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002192 SaveStateEntry *se;
2193 int section_id;
2194 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002195} LoadStateEntry;
2196
aliguoria672b462008-11-11 21:33:36 +00002197int qemu_loadvm_state(QEMUFile *f)
2198{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002199 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2200 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002201 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002202 uint8_t section_type;
2203 unsigned int v;
2204 int ret;
2205
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002206 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002207 return -EINVAL;
2208 }
2209
aliguoria672b462008-11-11 21:33:36 +00002210 v = qemu_get_be32(f);
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002211 if (v != QEMU_VM_FILE_MAGIC) {
aliguoria672b462008-11-11 21:33:36 +00002212 return -EINVAL;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002213 }
aliguoria672b462008-11-11 21:33:36 +00002214
2215 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002216 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2217 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2218 return -ENOTSUP;
2219 }
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002220 if (v != QEMU_VM_FILE_VERSION) {
aliguoria672b462008-11-11 21:33:36 +00002221 return -ENOTSUP;
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002222 }
aliguoria672b462008-11-11 21:33:36 +00002223
2224 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2225 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002226 SaveStateEntry *se;
2227 char idstr[257];
2228 int len;
2229
2230 switch (section_type) {
2231 case QEMU_VM_SECTION_START:
2232 case QEMU_VM_SECTION_FULL:
2233 /* Read section start */
2234 section_id = qemu_get_be32(f);
2235 len = qemu_get_byte(f);
2236 qemu_get_buffer(f, (uint8_t *)idstr, len);
2237 idstr[len] = 0;
2238 instance_id = qemu_get_be32(f);
2239 version_id = qemu_get_be32(f);
2240
2241 /* Find savevm section */
2242 se = find_se(idstr, instance_id);
2243 if (se == NULL) {
2244 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2245 ret = -EINVAL;
2246 goto out;
2247 }
2248
2249 /* Validate version */
2250 if (version_id > se->version_id) {
2251 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2252 version_id, idstr, se->version_id);
2253 ret = -EINVAL;
2254 goto out;
2255 }
2256
2257 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002258 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002259
2260 le->se = se;
2261 le->section_id = section_id;
2262 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002263 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002264
Juan Quintela4082be42009-08-20 19:42:24 +02002265 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002266 if (ret < 0) {
2267 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2268 instance_id, idstr);
2269 goto out;
2270 }
aliguoria672b462008-11-11 21:33:36 +00002271 break;
2272 case QEMU_VM_SECTION_PART:
2273 case QEMU_VM_SECTION_END:
2274 section_id = qemu_get_be32(f);
2275
Blue Swirl72cf2d42009-09-12 07:36:22 +00002276 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002277 if (le->section_id == section_id) {
2278 break;
2279 }
2280 }
aliguoria672b462008-11-11 21:33:36 +00002281 if (le == NULL) {
2282 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2283 ret = -EINVAL;
2284 goto out;
2285 }
2286
Juan Quintela4082be42009-08-20 19:42:24 +02002287 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002288 if (ret < 0) {
2289 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2290 section_id);
2291 goto out;
2292 }
aliguoria672b462008-11-11 21:33:36 +00002293 break;
2294 default:
2295 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2296 ret = -EINVAL;
2297 goto out;
2298 }
2299 }
2300
Jan Kiszkaea375f92010-03-01 19:10:30 +01002301 cpu_synchronize_all_post_init();
2302
aliguoria672b462008-11-11 21:33:36 +00002303 ret = 0;
2304
2305out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002306 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2307 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002308 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002309 }
2310
Juan Quintela42802d42011-10-05 01:14:46 +02002311 if (ret == 0) {
2312 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002313 }
aliguoria672b462008-11-11 21:33:36 +00002314
2315 return ret;
2316}
2317
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002318static BlockDriverState *find_vmstate_bs(void)
2319{
2320 BlockDriverState *bs = NULL;
2321 while ((bs = bdrv_next(bs))) {
2322 if (bdrv_can_snapshot(bs)) {
2323 return bs;
2324 }
2325 }
2326 return NULL;
2327}
2328
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002329/*
2330 * Deletes snapshots of a given name in all opened images.
2331 */
2332static int del_existing_snapshots(Monitor *mon, const char *name)
2333{
2334 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002335 QEMUSnapshotInfo sn1, *snapshot = &sn1;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002336 Error *err = NULL;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002337
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002338 bs = NULL;
2339 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002340 if (bdrv_can_snapshot(bs) &&
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002341 bdrv_snapshot_find(bs, snapshot, name) >= 0) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002342 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
2343 if (error_is_set(&err)) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002344 monitor_printf(mon,
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002345 "Error while deleting snapshot on device '%s':"
2346 " %s\n",
2347 bdrv_get_device_name(bs),
2348 error_get_pretty(err));
2349 error_free(err);
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002350 return -1;
2351 }
2352 }
2353 }
2354
2355 return 0;
2356}
2357
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002358void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002359{
2360 BlockDriverState *bs, *bs1;
2361 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002362 int ret;
aliguoria672b462008-11-11 21:33:36 +00002363 QEMUFile *f;
2364 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002365 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002366 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002367 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002368 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002369
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002370 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002371 bs = NULL;
2372 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002373
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002374 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002375 continue;
2376 }
2377
2378 if (!bdrv_can_snapshot(bs)) {
2379 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2380 bdrv_get_device_name(bs));
2381 return;
2382 }
2383 }
2384
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002385 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002386 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002387 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002388 return;
2389 }
aliguoria672b462008-11-11 21:33:36 +00002390
Luiz Capitulino13548692011-07-29 15:36:43 -03002391 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002392 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002393
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002394 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002395
2396 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002397 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002398 sn->date_sec = tv.tv_sec;
2399 sn->date_nsec = tv.tv_usec * 1000;
Alex Blighbc72ad62013-08-21 16:03:08 +01002400 sn->vm_clock_nsec = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL);
aliguoria672b462008-11-11 21:33:36 +00002401
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002402 if (name) {
2403 ret = bdrv_snapshot_find(bs, old_sn, name);
2404 if (ret >= 0) {
2405 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2406 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2407 } else {
2408 pstrcpy(sn->name, sizeof(sn->name), name);
2409 }
2410 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002411 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2412 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002413 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002414 }
2415
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002416 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002417 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002418 goto the_end;
2419 }
2420
aliguoria672b462008-11-11 21:33:36 +00002421 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002422 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002423 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002424 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002425 goto the_end;
2426 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002427 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002428 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002429 qemu_fclose(f);
2430 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002431 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002432 goto the_end;
2433 }
2434
2435 /* create the snapshots */
2436
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002437 bs1 = NULL;
2438 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002439 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002440 /* Write VM state size only to the image that contains the state */
2441 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002442 ret = bdrv_snapshot_create(bs1, sn);
2443 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002444 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2445 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002446 }
2447 }
2448 }
2449
2450 the_end:
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002451 if (saved_vm_running) {
aliguoria672b462008-11-11 21:33:36 +00002452 vm_start();
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002453 }
aliguoria672b462008-11-11 21:33:36 +00002454}
2455
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002456void qmp_xen_save_devices_state(const char *filename, Error **errp)
2457{
2458 QEMUFile *f;
2459 int saved_vm_running;
2460 int ret;
2461
2462 saved_vm_running = runstate_is_running();
2463 vm_stop(RUN_STATE_SAVE_VM);
2464
2465 f = qemu_fopen(filename, "wb");
2466 if (!f) {
Luiz Capitulino1befce92013-06-07 14:36:58 -04002467 error_setg_file_open(errp, errno, filename);
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002468 goto the_end;
2469 }
2470 ret = qemu_save_device_state(f);
2471 qemu_fclose(f);
2472 if (ret < 0) {
2473 error_set(errp, QERR_IO_ERROR);
2474 }
2475
2476 the_end:
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002477 if (saved_vm_running) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002478 vm_start();
Eduardo Habkost38ff78d2013-11-28 12:01:13 -02002479 }
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002480}
2481
Markus Armbruster03cd4652010-02-17 16:24:10 +01002482int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002483{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002484 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002485 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002486 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002487 int ret;
aliguoria672b462008-11-11 21:33:36 +00002488
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002489 bs_vm_state = find_vmstate_bs();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002490 if (!bs_vm_state) {
2491 error_report("No block device supports snapshots");
2492 return -ENOTSUP;
2493 }
2494
2495 /* Don't even try to load empty VM states */
2496 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2497 if (ret < 0) {
2498 return ret;
2499 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002500 error_report("This is a disk-only snapshot. Revert to it offline "
2501 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002502 return -EINVAL;
2503 }
2504
2505 /* Verify if there is any device that doesn't support snapshots and is
2506 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002507 bs = NULL;
2508 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002509
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002510 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002511 continue;
2512 }
2513
2514 if (!bdrv_can_snapshot(bs)) {
2515 error_report("Device '%s' is writable but does not support snapshots.",
2516 bdrv_get_device_name(bs));
2517 return -ENOTSUP;
2518 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002519
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002520 ret = bdrv_snapshot_find(bs, &sn, name);
2521 if (ret < 0) {
2522 error_report("Device '%s' does not have the requested snapshot '%s'",
2523 bdrv_get_device_name(bs), name);
2524 return ret;
2525 }
aliguoria672b462008-11-11 21:33:36 +00002526 }
2527
2528 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002529 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002530
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002531 bs = NULL;
2532 while ((bs = bdrv_next(bs))) {
2533 if (bdrv_can_snapshot(bs)) {
2534 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002535 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002536 error_report("Error %d while activating snapshot '%s' on '%s'",
2537 ret, name, bdrv_get_device_name(bs));
2538 return ret;
aliguoria672b462008-11-11 21:33:36 +00002539 }
2540 }
2541 }
2542
aliguoria672b462008-11-11 21:33:36 +00002543 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002544 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002545 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002546 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002547 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002548 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002549
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002550 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002551 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002552
aliguoria672b462008-11-11 21:33:36 +00002553 qemu_fclose(f);
2554 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002555 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002556 return ret;
aliguoria672b462008-11-11 21:33:36 +00002557 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002558
Juan Quintela05f24012009-08-20 19:42:22 +02002559 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002560}
2561
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002562void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002563{
2564 BlockDriverState *bs, *bs1;
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002565 Error *err = NULL;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002566 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002567
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002568 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002569 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002570 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002571 return;
2572 }
2573
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002574 bs1 = NULL;
2575 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002576 if (bdrv_can_snapshot(bs1)) {
Wenchao Xiaa89d89d2013-09-11 14:04:33 +08002577 bdrv_snapshot_delete_by_id_or_name(bs, name, &err);
2578 if (error_is_set(&err)) {
2579 monitor_printf(mon,
2580 "Error while deleting snapshot on device '%s':"
2581 " %s\n",
2582 bdrv_get_device_name(bs),
2583 error_get_pretty(err));
2584 error_free(err);
aliguoria672b462008-11-11 21:33:36 +00002585 }
2586 }
2587 }
2588}
2589
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002590void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002591{
2592 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002593 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2594 int nb_sns, i, ret, available;
2595 int total;
2596 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002597
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002598 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002599 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002600 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002601 return;
2602 }
aliguoria672b462008-11-11 21:33:36 +00002603
2604 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2605 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002606 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002607 return;
2608 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002609
2610 if (nb_sns == 0) {
2611 monitor_printf(mon, "There is no snapshot available.\n");
2612 return;
aliguoria672b462008-11-11 21:33:36 +00002613 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002614
Anthony Liguori7267c092011-08-20 22:09:37 -05002615 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002616 total = 0;
2617 for (i = 0; i < nb_sns; i++) {
2618 sn = &sn_tab[i];
2619 available = 1;
2620 bs1 = NULL;
2621
2622 while ((bs1 = bdrv_next(bs1))) {
2623 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2624 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2625 if (ret < 0) {
2626 available = 0;
2627 break;
2628 }
2629 }
2630 }
2631
2632 if (available) {
2633 available_snapshots[total] = i;
2634 total++;
2635 }
2636 }
2637
2638 if (total > 0) {
Wenchao Xia5b917042013-05-25 11:09:45 +08002639 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2640 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002641 for (i = 0; i < total; i++) {
2642 sn = &sn_tab[available_snapshots[i]];
Wenchao Xia5b917042013-05-25 11:09:45 +08002643 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2644 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002645 }
2646 } else {
2647 monitor_printf(mon, "There is no suitable snapshot available\n");
2648 }
2649
Anthony Liguori7267c092011-08-20 22:09:37 -05002650 g_free(sn_tab);
2651 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002652
aliguoria672b462008-11-11 21:33:36 +00002653}
Avi Kivityc5705a72011-12-20 15:59:12 +02002654
2655void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2656{
Avi Kivity1ddde082012-01-08 13:18:19 +02002657 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002658 memory_region_name(mr), dev);
2659}
2660
2661void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2662{
2663 /* Nothing do to while the implementation is in RAMBlock */
2664}
2665
2666void vmstate_register_ram_global(MemoryRegion *mr)
2667{
2668 vmstate_register_ram(mr, NULL);
2669}