blob: b11dbf6210588d0dc22d8c37acb3a9c822e303ec [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"
blueswir1511d2b12009-03-07 15:32:56 +000043
aliguoria672b462008-11-11 21:33:36 +000044#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000045
Nolan18995b92009-10-15 16:53:55 -070046#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040047#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070048#endif
49#define ARP_HTYPE_ETH 0x0001
50#define ARP_PTYPE_IP 0x0800
51#define ARP_OP_REQUEST_REV 0x3
52
53static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000054 uint8_t *mac_addr)
55{
Nolan18995b92009-10-15 16:53:55 -070056 /* Ethernet header. */
57 memset(buf, 0xff, 6); /* destination MAC addr */
58 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
59 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000060
Nolan18995b92009-10-15 16:53:55 -070061 /* RARP header. */
62 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
63 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
64 *(buf + 18) = 6; /* hardware addr length (ethernet) */
65 *(buf + 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
67 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
68 memset(buf + 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
70 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000071
Nolan18995b92009-10-15 16:53:55 -070072 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000074
Nolan18995b92009-10-15 16:53:55 -070075 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000076}
77
Mark McLoughlinf401ca22009-11-25 18:49:32 +000078static void qemu_announce_self_iter(NICState *nic, void *opaque)
79{
80 uint8_t buf[60];
81 int len;
82
83 len = announce_self_create(buf, nic->conf->macaddr.a);
84
Jason Wangb356f762013-01-30 19:12:22 +080085 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000086}
87
88
Gleb Natapoved8b3302009-05-21 17:17:44 +030089static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000090{
Gleb Natapoved8b3302009-05-21 17:17:44 +030091 static int count = SELF_ANNOUNCE_ROUNDS;
92 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000093
Mark McLoughlinf401ca22009-11-25 18:49:32 +000094 qemu_foreach_nic(qemu_announce_self_iter, NULL);
95
Nolan18995b92009-10-15 16:53:55 -070096 if (--count) {
97 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +010098 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -070099 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300100 } else {
101 qemu_del_timer(timer);
102 qemu_free_timer(timer);
103 }
104}
105
106void qemu_announce_self(void)
107{
108 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100109 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300110 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000111}
112
113/***********************************************************/
114/* savevm/loadvm support */
115
116#define IO_BUF_SIZE 32768
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200117#define MAX_IOV_SIZE MIN(IOV_MAX, 64)
aliguoria672b462008-11-11 21:33:36 +0000118
119struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200120 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000121 void *opaque;
aliguoria672b462008-11-11 21:33:36 +0000122
Paolo Bonzini1964a392013-02-22 17:36:45 +0100123 int64_t bytes_xfer;
124 int64_t xfer_limit;
125
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100126 int64_t pos; /* start of buffer when writing, end of buffer
127 when reading */
aliguoria672b462008-11-11 21:33:36 +0000128 int buf_index;
129 int buf_size; /* 0 when writing */
130 uint8_t buf[IO_BUF_SIZE];
131
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200132 struct iovec iov[MAX_IOV_SIZE];
133 unsigned int iovcnt;
134
Juan Quintela3961b4d2011-10-05 01:05:21 +0200135 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000136};
137
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200138typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000139{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200140 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000141 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200142} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000143
144typedef struct QEMUFileSocket
145{
146 int fd;
147 QEMUFile *file;
148} QEMUFileSocket;
149
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100150typedef struct {
151 Coroutine *co;
152 int fd;
153} FDYieldUntilData;
154
155static void fd_coroutine_enter(void *opaque)
156{
157 FDYieldUntilData *data = opaque;
158 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
159 qemu_coroutine_enter(data->co, NULL);
160}
161
162/**
163 * Yield until a file descriptor becomes readable
164 *
165 * Note that this function clobbers the handlers for the file descriptor.
166 */
167static void coroutine_fn yield_until_fd_readable(int fd)
168{
169 FDYieldUntilData data;
170
171 assert(qemu_in_coroutine());
172 data.co = qemu_coroutine_self();
173 data.fd = fd;
174 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
175 qemu_coroutine_yield();
176}
177
Kevin Wolf05fcc842013-04-05 21:27:54 +0200178static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
179 int64_t pos)
Orit Wasserman28085f72013-03-22 16:47:58 +0200180{
181 QEMUFileSocket *s = opaque;
182 ssize_t len;
183 ssize_t size = iov_size(iov, iovcnt);
184
185 len = iov_send(s->fd, iov, iovcnt, 0, size);
186 if (len < size) {
187 len = -socket_error();
188 }
189 return len;
190}
191
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200192static int socket_get_fd(void *opaque)
193{
194 QEMUFileSocket *s = opaque;
195
196 return s->fd;
197}
198
aliguoria672b462008-11-11 21:33:36 +0000199static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
200{
201 QEMUFileSocket *s = opaque;
202 ssize_t len;
203
Paolo Bonzini595ab642012-08-07 11:07:59 +0200204 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000205 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab642012-08-07 11:07:59 +0200206 if (len != -1) {
207 break;
208 }
209 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100210 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab642012-08-07 11:07:59 +0200211 } else if (socket_error() != EINTR) {
212 break;
213 }
214 }
aliguoria672b462008-11-11 21:33:36 +0000215
Paolo Bonzini595ab642012-08-07 11:07:59 +0200216 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000217 len = -socket_error();
Paolo Bonzini595ab642012-08-07 11:07:59 +0200218 }
aliguoria672b462008-11-11 21:33:36 +0000219 return len;
220}
221
222static int socket_close(void *opaque)
223{
224 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200225 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500226 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000227 return 0;
228}
229
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200230static int stdio_get_fd(void *opaque)
231{
232 QEMUFileStdio *s = opaque;
233
234 return fileno(s->stdio_file);
235}
236
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200237static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000238{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200239 QEMUFileStdio *s = opaque;
240 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000241}
242
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200243static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000244{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200245 QEMUFileStdio *s = opaque;
246 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300247 int bytes;
248
Paolo Bonzini595ab642012-08-07 11:07:59 +0200249 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300250 clearerr(fp);
251 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab642012-08-07 11:07:59 +0200252 if (bytes != 0 || !ferror(fp)) {
253 break;
254 }
255 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100256 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab642012-08-07 11:07:59 +0200257 } else if (errno != EINTR) {
258 break;
259 }
260 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300261 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000262}
263
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200264static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000265{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200266 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500267 int ret;
268 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200269 if (ret == -1) {
270 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100271 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
272 /* close succeeded, but non-zero exit code: */
273 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200274 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500275 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500276 return ret;
aliguoria672b462008-11-11 21:33:36 +0000277}
278
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200279static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000280{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200281 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200282 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100283
Orit Wassermancb88aa82013-03-22 16:48:01 +0200284 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100285 int fd = fileno(s->stdio_file);
286 struct stat st;
287
288 ret = fstat(fd, &st);
289 if (ret == 0 && S_ISREG(st.st_mode)) {
290 /*
291 * If the file handle is a regular file make sure the
292 * data is flushed to disk before signaling success.
293 */
294 ret = fsync(fd);
295 if (ret != 0) {
296 ret = -errno;
297 return ret;
298 }
299 }
300 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200301 if (fclose(s->stdio_file) == EOF) {
302 ret = -errno;
303 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500304 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200305 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200306}
aliguoria672b462008-11-11 21:33:36 +0000307
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200308static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200309 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200310 .get_buffer = stdio_get_buffer,
311 .close = stdio_pclose
312};
313
314static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200315 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200316 .put_buffer = stdio_put_buffer,
317 .close = stdio_pclose
318};
319
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100320QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200321{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100322 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200323 QEMUFileStdio *s;
324
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200325 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
326 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100327 return NULL;
328 }
329
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200330 stdio_file = popen(command, mode);
331 if (stdio_file == NULL) {
aliguoria672b462008-11-11 21:33:36 +0000332 return NULL;
333 }
334
Anthony Liguori7267c092011-08-20 22:09:37 -0500335 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000336
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200337 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000338
339 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200340 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000341 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200342 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000343 }
aliguoria672b462008-11-11 21:33:36 +0000344 return s->file;
345}
346
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200347static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200348 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200349 .get_buffer = stdio_get_buffer,
350 .close = stdio_fclose
351};
352
353static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200354 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200355 .put_buffer = stdio_put_buffer,
356 .close = stdio_fclose
357};
358
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100359static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
360 int64_t pos)
361{
362 QEMUFileSocket *s = opaque;
363 ssize_t len, offset;
364 ssize_t size = iov_size(iov, iovcnt);
365 ssize_t total = 0;
366
367 assert(iovcnt > 0);
368 offset = 0;
369 while (size > 0) {
370 /* Find the next start position; skip all full-sized vector elements */
371 while (offset >= iov[0].iov_len) {
372 offset -= iov[0].iov_len;
373 iov++, iovcnt--;
374 }
375
376 /* skip `offset' bytes from the (now) first element, undo it on exit */
377 assert(iovcnt > 0);
378 iov[0].iov_base += offset;
379 iov[0].iov_len -= offset;
380
381 do {
382 len = writev(s->fd, iov, iovcnt);
383 } while (len == -1 && errno == EINTR);
384 if (len == -1) {
385 return -errno;
386 }
387
388 /* Undo the changes above */
389 iov[0].iov_base -= offset;
390 iov[0].iov_len += offset;
391
392 /* Prepare for the next iteration */
393 offset += len;
394 total += len;
395 size -= len;
396 }
397
398 return total;
399}
400
401static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
402{
403 QEMUFileSocket *s = opaque;
404 ssize_t len;
405
406 for (;;) {
407 len = read(s->fd, buf, size);
408 if (len != -1) {
409 break;
410 }
411 if (errno == EAGAIN) {
412 yield_until_fd_readable(s->fd);
413 } else if (errno != EINTR) {
414 break;
415 }
416 }
417
418 if (len == -1) {
419 len = -errno;
420 }
421 return len;
422}
423
424static int unix_close(void *opaque)
425{
426 QEMUFileSocket *s = opaque;
427 close(s->fd);
428 g_free(s);
429 return 0;
430}
431
432static const QEMUFileOps unix_read_ops = {
433 .get_fd = socket_get_fd,
434 .get_buffer = unix_get_buffer,
435 .close = unix_close
436};
437
438static const QEMUFileOps unix_write_ops = {
439 .get_fd = socket_get_fd,
440 .writev_buffer = unix_writev_buffer,
441 .close = unix_close
442};
443
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200444QEMUFile *qemu_fdopen(int fd, const char *mode)
445{
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100446 QEMUFileSocket *s;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200447
448 if (mode == NULL ||
449 (mode[0] != 'r' && mode[0] != 'w') ||
450 mode[1] != 'b' || mode[2] != 0) {
451 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
452 return NULL;
453 }
454
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100455 s = g_malloc0(sizeof(QEMUFileSocket));
456 s->fd = fd;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200457
458 if(mode[0] == 'r') {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100459 s->file = qemu_fopen_ops(s, &unix_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200460 } else {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100461 s->file = qemu_fopen_ops(s, &unix_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200462 }
463 return s->file;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200464}
465
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200466static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200467 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200468 .get_buffer = socket_get_buffer,
469 .close = socket_close
470};
471
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100472static const QEMUFileOps socket_write_ops = {
473 .get_fd = socket_get_fd,
Orit Wasserman28085f72013-03-22 16:47:58 +0200474 .writev_buffer = socket_writev_buffer,
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100475 .close = socket_close
476};
477
478QEMUFile *qemu_fopen_socket(int fd, const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000479{
Anthony Liguori7267c092011-08-20 22:09:37 -0500480 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000481
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100482 if (mode == NULL ||
483 (mode[0] != 'r' && mode[0] != 'w') ||
484 mode[1] != 'b' || mode[2] != 0) {
485 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
486 return NULL;
487 }
488
aliguoria672b462008-11-11 21:33:36 +0000489 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100490 if (mode[0] == 'w') {
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100491 qemu_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100492 s->file = qemu_fopen_ops(s, &socket_write_ops);
493 } else {
494 s->file = qemu_fopen_ops(s, &socket_read_ops);
495 }
aliguoria672b462008-11-11 21:33:36 +0000496 return s->file;
497}
498
aliguoria672b462008-11-11 21:33:36 +0000499QEMUFile *qemu_fopen(const char *filename, const char *mode)
500{
501 QEMUFileStdio *s;
502
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200503 if (mode == NULL ||
504 (mode[0] != 'r' && mode[0] != 'w') ||
505 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000506 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200507 return NULL;
508 }
509
Anthony Liguori7267c092011-08-20 22:09:37 -0500510 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000511
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200512 s->stdio_file = fopen(filename, mode);
513 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000514 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200515
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200516 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200517 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200518 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200519 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200520 }
521 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000522fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500523 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000524 return NULL;
525}
526
Kevin Wolf05fcc842013-04-05 21:27:54 +0200527static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
528 int64_t pos)
529{
530 int ret;
531 QEMUIOVector qiov;
532
533 qemu_iovec_init_external(&qiov, iov, iovcnt);
534 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
535 if (ret < 0) {
536 return ret;
537 }
538
539 return qiov.size;
540}
541
aliguori178e08a2009-04-05 19:10:55 +0000542static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000543 int64_t pos, int size)
544{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200545 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000546 return size;
547}
548
aliguori178e08a2009-04-05 19:10:55 +0000549static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000550{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200551 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000552}
553
554static int bdrv_fclose(void *opaque)
555{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200556 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000557}
558
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200559static const QEMUFileOps bdrv_read_ops = {
560 .get_buffer = block_get_buffer,
561 .close = bdrv_fclose
562};
563
564static const QEMUFileOps bdrv_write_ops = {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200565 .put_buffer = block_put_buffer,
566 .writev_buffer = block_writev_buffer,
567 .close = bdrv_fclose
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200568};
569
Christoph Hellwig45566e92009-07-10 23:11:57 +0200570static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000571{
aliguoria672b462008-11-11 21:33:36 +0000572 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200573 return qemu_fopen_ops(bs, &bdrv_write_ops);
574 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000575}
576
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200577QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000578{
579 QEMUFile *f;
580
Anthony Liguori7267c092011-08-20 22:09:37 -0500581 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000582
583 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200584 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000585 return f;
586}
587
Juan Quintela624b9cc2011-10-05 01:02:52 +0200588int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000589{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200590 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000591}
592
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100593static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000594{
Juan Quintelaafe41932013-01-14 13:36:28 +0100595 if (f->last_error == 0) {
596 f->last_error = ret;
597 }
aliguori4dabe242009-04-05 19:30:51 +0000598}
599
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200600static inline bool qemu_file_is_writable(QEMUFile *f)
601{
602 return f->ops->writev_buffer || f->ops->put_buffer;
603}
604
Orit Wassermancb88aa82013-03-22 16:48:01 +0200605/**
606 * Flushes QEMUFile buffer
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200607 *
Orit Wassermancb88aa82013-03-22 16:48:01 +0200608 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
609 * put_buffer ops.
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200610 */
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100611static void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000612{
Orit Wassermancb88aa82013-03-22 16:48:01 +0200613 ssize_t ret = 0;
Juan Quintela7311bea2012-08-29 19:08:59 +0200614
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200615 if (!qemu_file_is_writable(f)) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100616 return;
617 }
Orit Wassermancb88aa82013-03-22 16:48:01 +0200618
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200619 if (f->ops->writev_buffer) {
620 if (f->iovcnt > 0) {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200621 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
Juan Quintela7311bea2012-08-29 19:08:59 +0200622 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200623 } else {
624 if (f->buf_index > 0) {
625 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200626 }
aliguoria672b462008-11-11 21:33:36 +0000627 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200628 if (ret >= 0) {
629 f->pos += ret;
630 }
631 f->buf_index = 0;
632 f->iovcnt = 0;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100633 if (ret < 0) {
634 qemu_file_set_error(f, ret);
635 }
aliguoria672b462008-11-11 21:33:36 +0000636}
637
638static void qemu_fill_buffer(QEMUFile *f)
639{
640 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200641 int pending;
aliguoria672b462008-11-11 21:33:36 +0000642
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200643 assert(!qemu_file_is_writable(f));
aliguoria672b462008-11-11 21:33:36 +0000644
Juan Quintela0046c452011-09-30 19:28:45 +0200645 pending = f->buf_size - f->buf_index;
646 if (pending > 0) {
647 memmove(f->buf, f->buf + f->buf_index, pending);
648 }
649 f->buf_index = 0;
650 f->buf_size = pending;
651
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100652 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200653 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000654 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200655 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100656 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200657 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200658 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000659 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200660 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000661}
662
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200663int qemu_get_fd(QEMUFile *f)
664{
665 if (f->ops->get_fd) {
666 return f->ops->get_fd(f->opaque);
667 }
668 return -1;
669}
670
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200671/** Closes the file
672 *
673 * Returns negative error value if any error happened on previous operations or
674 * while closing the file. Returns 0 or positive number on success.
675 *
676 * The meaning of return value on success depends on the specific backend
677 * being used.
678 */
679int qemu_fclose(QEMUFile *f)
680{
Juan Quintela29eee862012-08-29 19:14:54 +0200681 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100682 qemu_fflush(f);
683 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200684
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200685 if (f->ops->close) {
686 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200687 if (ret >= 0) {
688 ret = ret2;
689 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200690 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200691 /* If any error was spotted before closing, we should report it
692 * instead of the close() return value.
693 */
694 if (f->last_error) {
695 ret = f->last_error;
696 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500697 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000698 return ret;
699}
700
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200701static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
702{
703 /* check for adjacent buffer and coalesce them */
704 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
705 f->iov[f->iovcnt - 1].iov_len) {
706 f->iov[f->iovcnt - 1].iov_len += size;
707 } else {
708 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
709 f->iov[f->iovcnt++].iov_len = size;
710 }
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200711
Paolo Bonzini4d117242013-04-08 13:29:57 +0200712 if (f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200713 qemu_fflush(f);
714 }
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200715}
716
Orit Wasserman6181ec22013-03-22 16:48:02 +0200717void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
718{
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200719 if (!f->ops->writev_buffer) {
720 qemu_put_buffer(f, buf, size);
721 return;
722 }
723
Orit Wasserman6181ec22013-03-22 16:48:02 +0200724 if (f->last_error) {
725 return;
726 }
727
Orit Wasserman6181ec22013-03-22 16:48:02 +0200728 f->bytes_xfer += size;
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200729 add_to_iovec(f, buf, size);
Orit Wasserman6181ec22013-03-22 16:48:02 +0200730}
731
aliguoria672b462008-11-11 21:33:36 +0000732void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
733{
734 int l;
735
Juan Quintelac10682c2012-08-29 19:43:39 +0200736 if (f->last_error) {
737 return;
738 }
739
Juan Quintelac10682c2012-08-29 19:43:39 +0200740 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000741 l = IO_BUF_SIZE - f->buf_index;
742 if (l > size)
743 l = size;
744 memcpy(f->buf + f->buf_index, buf, l);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200745 f->bytes_xfer += size;
746 if (f->ops->writev_buffer) {
747 add_to_iovec(f, f->buf + f->buf_index, l);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200748 }
749 f->buf_index += l;
750 if (f->buf_index == IO_BUF_SIZE) {
751 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200752 }
Orit Wasserman6181ec22013-03-22 16:48:02 +0200753 if (qemu_file_get_error(f)) {
754 break;
755 }
aliguoria672b462008-11-11 21:33:36 +0000756 buf += l;
757 size -= l;
aliguoria672b462008-11-11 21:33:36 +0000758 }
759}
760
761void qemu_put_byte(QEMUFile *f, int v)
762{
Juan Quintelac10682c2012-08-29 19:43:39 +0200763 if (f->last_error) {
764 return;
765 }
766
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200767 f->buf[f->buf_index] = v;
Orit Wasserman7d8a30bb2013-03-22 16:47:59 +0200768 f->bytes_xfer++;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200769 if (f->ops->writev_buffer) {
770 add_to_iovec(f, f->buf + f->buf_index, 1);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200771 }
772 f->buf_index++;
773 if (f->buf_index == IO_BUF_SIZE) {
774 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200775 }
aliguoria672b462008-11-11 21:33:36 +0000776}
777
Juan Quintelac6380722011-10-04 15:28:31 +0200778static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000779{
Juan Quintelac6380722011-10-04 15:28:31 +0200780 if (f->buf_index + size <= f->buf_size) {
781 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000782 }
aliguoria672b462008-11-11 21:33:36 +0000783}
784
Juan Quintelac6380722011-10-04 15:28:31 +0200785static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200786{
Juan Quintelac6380722011-10-04 15:28:31 +0200787 int pending;
788 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200789
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200790 assert(!qemu_file_is_writable(f));
Juan Quintela811814b2010-07-26 21:38:43 +0200791
Juan Quintelac6380722011-10-04 15:28:31 +0200792 index = f->buf_index + offset;
793 pending = f->buf_size - index;
794 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200795 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200796 index = f->buf_index + offset;
797 pending = f->buf_size - index;
798 }
799
800 if (pending <= 0) {
801 return 0;
802 }
803 if (size > pending) {
804 size = pending;
805 }
806
807 memcpy(buf, f->buf + index, size);
808 return size;
809}
810
811int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
812{
813 int pending = size;
814 int done = 0;
815
816 while (pending > 0) {
817 int res;
818
819 res = qemu_peek_buffer(f, buf, pending, 0);
820 if (res == 0) {
821 return done;
822 }
823 qemu_file_skip(f, res);
824 buf += res;
825 pending -= res;
826 done += res;
827 }
828 return done;
829}
830
831static int qemu_peek_byte(QEMUFile *f, int offset)
832{
833 int index = f->buf_index + offset;
834
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200835 assert(!qemu_file_is_writable(f));
Juan Quintelac6380722011-10-04 15:28:31 +0200836
837 if (index >= f->buf_size) {
838 qemu_fill_buffer(f);
839 index = f->buf_index + offset;
840 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200841 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200842 }
Juan Quintela811814b2010-07-26 21:38:43 +0200843 }
Juan Quintelac6380722011-10-04 15:28:31 +0200844 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200845}
846
aliguoria672b462008-11-11 21:33:36 +0000847int qemu_get_byte(QEMUFile *f)
848{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200849 int result;
aliguoria672b462008-11-11 21:33:36 +0000850
Juan Quintelac6380722011-10-04 15:28:31 +0200851 result = qemu_peek_byte(f, 0);
852 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200853 return result;
aliguoria672b462008-11-11 21:33:36 +0000854}
855
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100856int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000857{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100858 qemu_fflush(f);
859 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000860}
861
aliguoria672b462008-11-11 21:33:36 +0000862int qemu_file_rate_limit(QEMUFile *f)
863{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100864 if (qemu_file_get_error(f)) {
865 return 1;
866 }
867 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
868 return 1;
869 }
aliguoria672b462008-11-11 21:33:36 +0000870 return 0;
871}
872
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200873int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200874{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100875 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200876}
877
Paolo Bonzini1964a392013-02-22 17:36:45 +0100878void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400879{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100880 f->xfer_limit = limit;
881}
Glauber Costa19629532009-05-20 18:26:57 -0400882
Paolo Bonzini1964a392013-02-22 17:36:45 +0100883void qemu_file_reset_rate_limit(QEMUFile *f)
884{
885 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400886}
887
aliguoria672b462008-11-11 21:33:36 +0000888void qemu_put_be16(QEMUFile *f, unsigned int v)
889{
890 qemu_put_byte(f, v >> 8);
891 qemu_put_byte(f, v);
892}
893
894void qemu_put_be32(QEMUFile *f, unsigned int v)
895{
896 qemu_put_byte(f, v >> 24);
897 qemu_put_byte(f, v >> 16);
898 qemu_put_byte(f, v >> 8);
899 qemu_put_byte(f, v);
900}
901
902void qemu_put_be64(QEMUFile *f, uint64_t v)
903{
904 qemu_put_be32(f, v >> 32);
905 qemu_put_be32(f, v);
906}
907
908unsigned int qemu_get_be16(QEMUFile *f)
909{
910 unsigned int v;
911 v = qemu_get_byte(f) << 8;
912 v |= qemu_get_byte(f);
913 return v;
914}
915
916unsigned int qemu_get_be32(QEMUFile *f)
917{
918 unsigned int v;
919 v = qemu_get_byte(f) << 24;
920 v |= qemu_get_byte(f) << 16;
921 v |= qemu_get_byte(f) << 8;
922 v |= qemu_get_byte(f);
923 return v;
924}
925
926uint64_t qemu_get_be64(QEMUFile *f)
927{
928 uint64_t v;
929 v = (uint64_t)qemu_get_be32(f) << 32;
930 v |= qemu_get_be32(f);
931 return v;
932}
933
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200934
935/* timer */
936
937void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
938{
939 uint64_t expire_time;
940
941 expire_time = qemu_timer_expire_time_ns(ts);
942 qemu_put_be64(f, expire_time);
943}
944
945void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
946{
947 uint64_t expire_time;
948
949 expire_time = qemu_get_be64(f);
950 if (expire_time != -1) {
951 qemu_mod_timer_ns(ts, expire_time);
952 } else {
953 qemu_del_timer(ts);
954 }
955}
956
957
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100958/* bool */
959
960static int get_bool(QEMUFile *f, void *pv, size_t size)
961{
962 bool *v = pv;
963 *v = qemu_get_byte(f);
964 return 0;
965}
966
967static void put_bool(QEMUFile *f, void *pv, size_t size)
968{
969 bool *v = pv;
970 qemu_put_byte(f, *v);
971}
972
973const VMStateInfo vmstate_info_bool = {
974 .name = "bool",
975 .get = get_bool,
976 .put = put_bool,
977};
978
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200979/* 8 bit int */
980
981static int get_int8(QEMUFile *f, void *pv, size_t size)
982{
983 int8_t *v = pv;
984 qemu_get_s8s(f, v);
985 return 0;
986}
987
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200988static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200989{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200990 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200991 qemu_put_s8s(f, v);
992}
993
994const VMStateInfo vmstate_info_int8 = {
995 .name = "int8",
996 .get = get_int8,
997 .put = put_int8,
998};
999
1000/* 16 bit int */
1001
1002static int get_int16(QEMUFile *f, void *pv, size_t size)
1003{
1004 int16_t *v = pv;
1005 qemu_get_sbe16s(f, v);
1006 return 0;
1007}
1008
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001009static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001010{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001011 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001012 qemu_put_sbe16s(f, v);
1013}
1014
1015const VMStateInfo vmstate_info_int16 = {
1016 .name = "int16",
1017 .get = get_int16,
1018 .put = put_int16,
1019};
1020
1021/* 32 bit int */
1022
1023static int get_int32(QEMUFile *f, void *pv, size_t size)
1024{
1025 int32_t *v = pv;
1026 qemu_get_sbe32s(f, v);
1027 return 0;
1028}
1029
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001030static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001031{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001032 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001033 qemu_put_sbe32s(f, v);
1034}
1035
1036const VMStateInfo vmstate_info_int32 = {
1037 .name = "int32",
1038 .get = get_int32,
1039 .put = put_int32,
1040};
1041
Juan Quintela82501662009-08-20 19:42:32 +02001042/* 32 bit int. See that the received value is the same than the one
1043 in the field */
1044
1045static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1046{
1047 int32_t *v = pv;
1048 int32_t v2;
1049 qemu_get_sbe32s(f, &v2);
1050
1051 if (*v == v2)
1052 return 0;
1053 return -EINVAL;
1054}
1055
1056const VMStateInfo vmstate_info_int32_equal = {
1057 .name = "int32 equal",
1058 .get = get_int32_equal,
1059 .put = put_int32,
1060};
1061
Juan Quintela0a031e02009-08-20 19:42:37 +02001062/* 32 bit int. See that the received value is the less or the same
1063 than the one in the field */
1064
1065static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1066{
1067 int32_t *old = pv;
1068 int32_t new;
1069 qemu_get_sbe32s(f, &new);
1070
1071 if (*old <= new)
1072 return 0;
1073 return -EINVAL;
1074}
1075
1076const VMStateInfo vmstate_info_int32_le = {
1077 .name = "int32 equal",
1078 .get = get_int32_le,
1079 .put = put_int32,
1080};
1081
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001082/* 64 bit int */
1083
1084static int get_int64(QEMUFile *f, void *pv, size_t size)
1085{
1086 int64_t *v = pv;
1087 qemu_get_sbe64s(f, v);
1088 return 0;
1089}
1090
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001091static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001092{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001093 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001094 qemu_put_sbe64s(f, v);
1095}
1096
1097const VMStateInfo vmstate_info_int64 = {
1098 .name = "int64",
1099 .get = get_int64,
1100 .put = put_int64,
1101};
1102
1103/* 8 bit unsigned int */
1104
1105static int get_uint8(QEMUFile *f, void *pv, size_t size)
1106{
1107 uint8_t *v = pv;
1108 qemu_get_8s(f, v);
1109 return 0;
1110}
1111
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001112static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001113{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001114 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001115 qemu_put_8s(f, v);
1116}
1117
1118const VMStateInfo vmstate_info_uint8 = {
1119 .name = "uint8",
1120 .get = get_uint8,
1121 .put = put_uint8,
1122};
1123
1124/* 16 bit unsigned int */
1125
1126static int get_uint16(QEMUFile *f, void *pv, size_t size)
1127{
1128 uint16_t *v = pv;
1129 qemu_get_be16s(f, v);
1130 return 0;
1131}
1132
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001133static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001134{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001135 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001136 qemu_put_be16s(f, v);
1137}
1138
1139const VMStateInfo vmstate_info_uint16 = {
1140 .name = "uint16",
1141 .get = get_uint16,
1142 .put = put_uint16,
1143};
1144
1145/* 32 bit unsigned int */
1146
1147static int get_uint32(QEMUFile *f, void *pv, size_t size)
1148{
1149 uint32_t *v = pv;
1150 qemu_get_be32s(f, v);
1151 return 0;
1152}
1153
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001154static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001155{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001156 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001157 qemu_put_be32s(f, v);
1158}
1159
1160const VMStateInfo vmstate_info_uint32 = {
1161 .name = "uint32",
1162 .get = get_uint32,
1163 .put = put_uint32,
1164};
1165
Juan Quintela9122a8f2011-03-10 12:33:48 +01001166/* 32 bit uint. See that the received value is the same than the one
1167 in the field */
1168
1169static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1170{
1171 uint32_t *v = pv;
1172 uint32_t v2;
1173 qemu_get_be32s(f, &v2);
1174
1175 if (*v == v2) {
1176 return 0;
1177 }
1178 return -EINVAL;
1179}
1180
1181const VMStateInfo vmstate_info_uint32_equal = {
1182 .name = "uint32 equal",
1183 .get = get_uint32_equal,
1184 .put = put_uint32,
1185};
1186
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001187/* 64 bit unsigned int */
1188
1189static int get_uint64(QEMUFile *f, void *pv, size_t size)
1190{
1191 uint64_t *v = pv;
1192 qemu_get_be64s(f, v);
1193 return 0;
1194}
1195
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001196static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001197{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001198 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001199 qemu_put_be64s(f, v);
1200}
1201
1202const VMStateInfo vmstate_info_uint64 = {
1203 .name = "uint64",
1204 .get = get_uint64,
1205 .put = put_uint64,
1206};
1207
David Gibsone344b8a2013-03-12 14:06:00 +11001208/* 64 bit unsigned int. See that the received value is the same than the one
1209 in the field */
1210
1211static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1212{
1213 uint64_t *v = pv;
1214 uint64_t v2;
1215 qemu_get_be64s(f, &v2);
1216
1217 if (*v == v2) {
1218 return 0;
1219 }
1220 return -EINVAL;
1221}
1222
1223const VMStateInfo vmstate_info_uint64_equal = {
1224 .name = "int64 equal",
1225 .get = get_uint64_equal,
1226 .put = put_uint64,
1227};
1228
Juan Quintela80cd83e2009-09-10 03:04:36 +02001229/* 8 bit int. See that the received value is the same than the one
1230 in the field */
1231
1232static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1233{
1234 uint8_t *v = pv;
1235 uint8_t v2;
1236 qemu_get_8s(f, &v2);
1237
1238 if (*v == v2)
1239 return 0;
1240 return -EINVAL;
1241}
1242
1243const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001244 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001245 .get = get_uint8_equal,
1246 .put = put_uint8,
1247};
1248
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001249/* 16 bit unsigned int int. See that the received value is the same than the one
1250 in the field */
1251
1252static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1253{
1254 uint16_t *v = pv;
1255 uint16_t v2;
1256 qemu_get_be16s(f, &v2);
1257
1258 if (*v == v2)
1259 return 0;
1260 return -EINVAL;
1261}
1262
1263const VMStateInfo vmstate_info_uint16_equal = {
1264 .name = "uint16 equal",
1265 .get = get_uint16_equal,
1266 .put = put_uint16,
1267};
1268
David Gibson213945e2013-03-12 14:06:02 +11001269/* floating point */
1270
1271static int get_float64(QEMUFile *f, void *pv, size_t size)
1272{
1273 float64 *v = pv;
1274
1275 *v = make_float64(qemu_get_be64(f));
1276 return 0;
1277}
1278
1279static void put_float64(QEMUFile *f, void *pv, size_t size)
1280{
1281 uint64_t *v = pv;
1282
1283 qemu_put_be64(f, float64_val(*v));
1284}
1285
1286const VMStateInfo vmstate_info_float64 = {
1287 .name = "float64",
1288 .get = get_float64,
1289 .put = put_float64,
1290};
1291
Juan Quinteladde04632009-08-20 19:42:26 +02001292/* timers */
1293
1294static int get_timer(QEMUFile *f, void *pv, size_t size)
1295{
1296 QEMUTimer *v = pv;
1297 qemu_get_timer(f, v);
1298 return 0;
1299}
1300
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001301static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001302{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001303 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001304 qemu_put_timer(f, v);
1305}
1306
1307const VMStateInfo vmstate_info_timer = {
1308 .name = "timer",
1309 .get = get_timer,
1310 .put = put_timer,
1311};
1312
Juan Quintela6f67c502009-08-20 19:42:35 +02001313/* uint8_t buffers */
1314
1315static int get_buffer(QEMUFile *f, void *pv, size_t size)
1316{
1317 uint8_t *v = pv;
1318 qemu_get_buffer(f, v, size);
1319 return 0;
1320}
1321
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001322static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001323{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001324 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001325 qemu_put_buffer(f, v, size);
1326}
1327
1328const VMStateInfo vmstate_info_buffer = {
1329 .name = "buffer",
1330 .get = get_buffer,
1331 .put = put_buffer,
1332};
1333
Juan Quintela76507c72009-10-19 15:46:28 +02001334/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001335 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001336
1337static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1338{
Jan Kiszka21174c32009-12-02 12:36:35 +01001339 uint8_t buf[1024];
1340 int block_len;
1341
1342 while (size > 0) {
1343 block_len = MIN(sizeof(buf), size);
1344 size -= block_len;
1345 qemu_get_buffer(f, buf, block_len);
1346 }
1347 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001348}
1349
1350static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1351{
Jan Kiszka21174c32009-12-02 12:36:35 +01001352 static const uint8_t buf[1024];
1353 int block_len;
1354
1355 while (size > 0) {
1356 block_len = MIN(sizeof(buf), size);
1357 size -= block_len;
1358 qemu_put_buffer(f, buf, block_len);
1359 }
Juan Quintela76507c72009-10-19 15:46:28 +02001360}
1361
1362const VMStateInfo vmstate_info_unused_buffer = {
1363 .name = "unused_buffer",
1364 .get = get_unused_buffer,
1365 .put = put_unused_buffer,
1366};
1367
Peter Maydell08e99e22012-10-30 07:45:12 +00001368/* bitmaps (as defined by bitmap.h). Note that size here is the size
1369 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1370 * bit words with the bits in big endian order. The in-memory format
1371 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1372 */
1373/* This is the number of 64 bit words sent over the wire */
1374#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1375static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1376{
1377 unsigned long *bmp = pv;
1378 int i, idx = 0;
1379 for (i = 0; i < BITS_TO_U64S(size); i++) {
1380 uint64_t w = qemu_get_be64(f);
1381 bmp[idx++] = w;
1382 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1383 bmp[idx++] = w >> 32;
1384 }
1385 }
1386 return 0;
1387}
1388
1389static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1390{
1391 unsigned long *bmp = pv;
1392 int i, idx = 0;
1393 for (i = 0; i < BITS_TO_U64S(size); i++) {
1394 uint64_t w = bmp[idx++];
1395 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1396 w |= ((uint64_t)bmp[idx++]) << 32;
1397 }
1398 qemu_put_be64(f, w);
1399 }
1400}
1401
1402const VMStateInfo vmstate_info_bitmap = {
1403 .name = "bitmap",
1404 .get = get_bitmap,
1405 .put = put_bitmap,
1406};
1407
Alex Williamson7685ee62010-06-25 11:09:14 -06001408typedef struct CompatEntry {
1409 char idstr[256];
1410 int instance_id;
1411} CompatEntry;
1412
aliguoria672b462008-11-11 21:33:36 +00001413typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001414 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001415 char idstr[256];
1416 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001417 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001418 int version_id;
1419 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001420 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001421 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001422 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001423 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001424 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001425 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001426} SaveStateEntry;
1427
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001428
Blue Swirl72cf2d42009-09-12 07:36:22 +00001429static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1430 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001431static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001432
Juan Quintela8718e992009-09-01 02:12:31 +02001433static int calculate_new_instance_id(const char *idstr)
1434{
1435 SaveStateEntry *se;
1436 int instance_id = 0;
1437
Blue Swirl72cf2d42009-09-12 07:36:22 +00001438 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001439 if (strcmp(idstr, se->idstr) == 0
1440 && instance_id <= se->instance_id) {
1441 instance_id = se->instance_id + 1;
1442 }
1443 }
1444 return instance_id;
1445}
1446
Alex Williamson7685ee62010-06-25 11:09:14 -06001447static int calculate_compat_instance_id(const char *idstr)
1448{
1449 SaveStateEntry *se;
1450 int instance_id = 0;
1451
1452 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1453 if (!se->compat)
1454 continue;
1455
1456 if (strcmp(idstr, se->compat->idstr) == 0
1457 && instance_id <= se->compat->instance_id) {
1458 instance_id = se->compat->instance_id + 1;
1459 }
1460 }
1461 return instance_id;
1462}
1463
aliguoria672b462008-11-11 21:33:36 +00001464/* TODO: Individual devices generally have very little idea about the rest
1465 of the system, so instance_id should be removed/replaced.
1466 Meanwhile pass -1 as instance_id if you do not already have a clearly
1467 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001468int register_savevm_live(DeviceState *dev,
1469 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001470 int instance_id,
1471 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001472 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001473 void *opaque)
1474{
Juan Quintela8718e992009-09-01 02:12:31 +02001475 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001476
Anthony Liguori7267c092011-08-20 22:09:37 -05001477 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001478 se->version_id = version_id;
1479 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001480 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001481 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001482 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001483 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001484 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001485 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001486 se->is_ram = 1;
1487 }
aliguoria672b462008-11-11 21:33:36 +00001488
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001489 if (dev) {
1490 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001491 if (id) {
1492 pstrcpy(se->idstr, sizeof(se->idstr), id);
1493 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001494 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001495
Anthony Liguori7267c092011-08-20 22:09:37 -05001496 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001497 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1498 se->compat->instance_id = instance_id == -1 ?
1499 calculate_compat_instance_id(idstr) : instance_id;
1500 instance_id = -1;
1501 }
1502 }
1503 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1504
Juan Quintela8718e992009-09-01 02:12:31 +02001505 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001506 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001507 } else {
1508 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001509 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001510 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001511 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001512 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001513 return 0;
1514}
1515
Alex Williamson0be71e32010-06-25 11:09:07 -06001516int register_savevm(DeviceState *dev,
1517 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001518 int instance_id,
1519 int version_id,
1520 SaveStateHandler *save_state,
1521 LoadStateHandler *load_state,
1522 void *opaque)
1523{
Juan Quintela7908c782012-06-26 18:46:10 +02001524 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1525 ops->save_state = save_state;
1526 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001527 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001528 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001529}
1530
Alex Williamson0be71e32010-06-25 11:09:07 -06001531void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001532{
Juan Quintela8718e992009-09-01 02:12:31 +02001533 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001534 char id[256] = "";
1535
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001536 if (dev) {
1537 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001538 if (path) {
1539 pstrcpy(id, sizeof(id), path);
1540 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001541 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001542 }
1543 }
1544 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001545
Blue Swirl72cf2d42009-09-12 07:36:22 +00001546 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001547 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001548 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001549 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001550 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001551 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001552 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001553 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001554 }
aliguori41bd13a2009-04-17 17:10:59 +00001555 }
1556}
1557
Alex Williamson0be71e32010-06-25 11:09:07 -06001558int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001559 const VMStateDescription *vmsd,
1560 void *opaque, int alias_id,
1561 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001562{
Juan Quintela8718e992009-09-01 02:12:31 +02001563 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001564
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001565 /* If this triggers, alias support can be dropped for the vmsd. */
1566 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1567
Anthony Liguori7267c092011-08-20 22:09:37 -05001568 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001569 se->version_id = vmsd->version_id;
1570 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001571 se->opaque = opaque;
1572 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001573 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001574 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001575
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001576 if (dev) {
1577 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001578 if (id) {
1579 pstrcpy(se->idstr, sizeof(se->idstr), id);
1580 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001581 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001582
Anthony Liguori7267c092011-08-20 22:09:37 -05001583 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001584 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1585 se->compat->instance_id = instance_id == -1 ?
1586 calculate_compat_instance_id(vmsd->name) : instance_id;
1587 instance_id = -1;
1588 }
1589 }
1590 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1591
Juan Quintela8718e992009-09-01 02:12:31 +02001592 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001593 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001594 } else {
1595 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001596 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001597 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001598 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001599 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001600 return 0;
1601}
1602
Alex Williamson0be71e32010-06-25 11:09:07 -06001603void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1604 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001605{
Juan Quintela1eb75382009-09-10 03:04:29 +02001606 SaveStateEntry *se, *new_se;
1607
Blue Swirl72cf2d42009-09-12 07:36:22 +00001608 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001609 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001610 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001611 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001612 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001613 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001614 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001615 }
1616 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001617}
1618
Juan Quintela811814b2010-07-26 21:38:43 +02001619static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1620 void *opaque);
1621static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1622 void *opaque);
1623
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001624int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1625 void *opaque, int version_id)
1626{
1627 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001628 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001629
1630 if (version_id > vmsd->version_id) {
1631 return -EINVAL;
1632 }
1633 if (version_id < vmsd->minimum_version_id_old) {
1634 return -EINVAL;
1635 }
1636 if (version_id < vmsd->minimum_version_id) {
1637 return vmsd->load_state_old(f, opaque, version_id);
1638 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001639 if (vmsd->pre_load) {
1640 int ret = vmsd->pre_load(opaque);
1641 if (ret)
1642 return ret;
1643 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001644 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001645 if ((field->field_exists &&
1646 field->field_exists(opaque, version_id)) ||
1647 (!field->field_exists &&
1648 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001649 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001650 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001651 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001652
Juan Quintelae61a1e02009-12-02 12:36:38 +01001653 if (field->flags & VMS_VBUFFER) {
1654 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001655 if (field->flags & VMS_MULTIPLY) {
1656 size *= field->size;
1657 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001658 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001659 if (field->flags & VMS_ARRAY) {
1660 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001661 } else if (field->flags & VMS_VARRAY_INT32) {
1662 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001663 } else if (field->flags & VMS_VARRAY_UINT32) {
1664 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001665 } else if (field->flags & VMS_VARRAY_UINT16) {
1666 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001667 } else if (field->flags & VMS_VARRAY_UINT8) {
1668 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001669 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001670 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001671 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001672 }
1673 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001674 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001675
Juan Quintela19df4382009-09-29 22:48:41 +02001676 if (field->flags & VMS_ARRAY_OF_POINTER) {
1677 addr = *(void **)addr;
1678 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001679 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001680 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001681 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001682 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001683
1684 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001685 if (ret < 0) {
1686 return ret;
1687 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001688 }
1689 }
1690 field++;
1691 }
Juan Quintela811814b2010-07-26 21:38:43 +02001692 ret = vmstate_subsection_load(f, vmsd, opaque);
1693 if (ret != 0) {
1694 return ret;
1695 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001696 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001697 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001698 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001699 return 0;
1700}
1701
1702void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001703 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001704{
1705 VMStateField *field = vmsd->fields;
1706
Juan Quintela8fb07912009-09-10 03:04:32 +02001707 if (vmsd->pre_save) {
1708 vmsd->pre_save(opaque);
1709 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001710 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001711 if (!field->field_exists ||
1712 field->field_exists(opaque, vmsd->version_id)) {
1713 void *base_addr = opaque + field->offset;
1714 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001715 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001716
Juan Quintelae61a1e02009-12-02 12:36:38 +01001717 if (field->flags & VMS_VBUFFER) {
1718 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001719 if (field->flags & VMS_MULTIPLY) {
1720 size *= field->size;
1721 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001722 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001723 if (field->flags & VMS_ARRAY) {
1724 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001725 } else if (field->flags & VMS_VARRAY_INT32) {
1726 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001727 } else if (field->flags & VMS_VARRAY_UINT32) {
1728 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001729 } else if (field->flags & VMS_VARRAY_UINT16) {
1730 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001731 } else if (field->flags & VMS_VARRAY_UINT8) {
1732 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001733 }
1734 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001735 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001736 }
1737 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001738 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001739
Juan Quintela85953872009-12-02 12:36:37 +01001740 if (field->flags & VMS_ARRAY_OF_POINTER) {
1741 addr = *(void **)addr;
1742 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001743 if (field->flags & VMS_STRUCT) {
1744 vmstate_save_state(f, field->vmsd, addr);
1745 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001746 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001747 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001748 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001749 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001750 field++;
1751 }
Juan Quintela811814b2010-07-26 21:38:43 +02001752 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001753}
1754
Juan Quintela4082be42009-08-20 19:42:24 +02001755static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1756{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001757 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001758 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001759 }
1760 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001761}
1762
Alex Williamsondc912122011-01-11 14:39:43 -07001763static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001764{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001765 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001766 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001767 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001768 }
1769 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001770}
1771
aliguoria672b462008-11-11 21:33:36 +00001772#define QEMU_VM_FILE_MAGIC 0x5145564d
1773#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1774#define QEMU_VM_FILE_VERSION 0x00000003
1775
1776#define QEMU_VM_EOF 0x00
1777#define QEMU_VM_SECTION_START 0x01
1778#define QEMU_VM_SECTION_PART 0x02
1779#define QEMU_VM_SECTION_END 0x03
1780#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001781#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001782
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001783bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001784{
1785 SaveStateEntry *se;
1786
1787 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1788 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001789 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001790 return true;
1791 }
1792 }
1793 return false;
1794}
1795
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001796void qemu_savevm_state_begin(QEMUFile *f,
1797 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001798{
1799 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001800 int ret;
aliguoria672b462008-11-11 21:33:36 +00001801
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001802 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001803 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001804 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001805 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001806 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001807 }
1808
aliguoria672b462008-11-11 21:33:36 +00001809 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1810 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1811
Blue Swirl72cf2d42009-09-12 07:36:22 +00001812 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001813 int len;
1814
Juan Quintelad1315aa2012-06-28 15:11:57 +02001815 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001816 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001817 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001818 if (se->ops && se->ops->is_active) {
1819 if (!se->ops->is_active(se->opaque)) {
1820 continue;
1821 }
1822 }
aliguoria672b462008-11-11 21:33:36 +00001823 /* Section type */
1824 qemu_put_byte(f, QEMU_VM_SECTION_START);
1825 qemu_put_be32(f, se->section_id);
1826
1827 /* ID string */
1828 len = strlen(se->idstr);
1829 qemu_put_byte(f, len);
1830 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1831
1832 qemu_put_be32(f, se->instance_id);
1833 qemu_put_be32(f, se->version_id);
1834
Juan Quintelad1315aa2012-06-28 15:11:57 +02001835 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001836 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001837 qemu_file_set_error(f, ret);
1838 break;
Juan Quintela29757252011-10-19 15:22:18 +02001839 }
aliguoria672b462008-11-11 21:33:36 +00001840 }
aliguoria672b462008-11-11 21:33:36 +00001841}
1842
Juan Quintela39346382011-09-22 11:02:14 +02001843/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001844 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001845 * negative: there was one error, and we have -errno.
1846 * 0 : We haven't finished, caller have to go again
1847 * 1 : We have finished, we can go to complete phase
1848 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001849int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001850{
1851 SaveStateEntry *se;
1852 int ret = 1;
1853
Blue Swirl72cf2d42009-09-12 07:36:22 +00001854 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001855 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001856 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001857 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001858 if (se->ops && se->ops->is_active) {
1859 if (!se->ops->is_active(se->opaque)) {
1860 continue;
1861 }
1862 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001863 if (qemu_file_rate_limit(f)) {
1864 return 0;
1865 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001866 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001867 /* Section type */
1868 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1869 qemu_put_be32(f, se->section_id);
1870
Juan Quintela16310a32012-06-28 15:31:37 +02001871 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001872 trace_savevm_section_end(se->section_id);
1873
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001874 if (ret < 0) {
1875 qemu_file_set_error(f, ret);
1876 }
Juan Quintela29757252011-10-19 15:22:18 +02001877 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001878 /* Do not proceed to the next vmstate before this one reported
1879 completion of the current stage. This serializes the migration
1880 and reduces the probability that a faster changing state is
1881 synchronized over and over again. */
1882 break;
1883 }
aliguoria672b462008-11-11 21:33:36 +00001884 }
Juan Quintela39346382011-09-22 11:02:14 +02001885 return ret;
aliguoria672b462008-11-11 21:33:36 +00001886}
1887
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001888void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001889{
1890 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001891 int ret;
aliguoria672b462008-11-11 21:33:36 +00001892
Jan Kiszkaea375f92010-03-01 19:10:30 +01001893 cpu_synchronize_all_states();
1894
Blue Swirl72cf2d42009-09-12 07:36:22 +00001895 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001896 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001897 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001898 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001899 if (se->ops && se->ops->is_active) {
1900 if (!se->ops->is_active(se->opaque)) {
1901 continue;
1902 }
1903 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001904 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001905 /* Section type */
1906 qemu_put_byte(f, QEMU_VM_SECTION_END);
1907 qemu_put_be32(f, se->section_id);
1908
Juan Quintela16310a32012-06-28 15:31:37 +02001909 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001910 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001911 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001912 qemu_file_set_error(f, ret);
1913 return;
Juan Quintela29757252011-10-19 15:22:18 +02001914 }
aliguoria672b462008-11-11 21:33:36 +00001915 }
1916
Blue Swirl72cf2d42009-09-12 07:36:22 +00001917 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001918 int len;
1919
Juan Quintela22ea40f2012-06-26 17:19:10 +02001920 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001921 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001922 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001923 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001924 /* Section type */
1925 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1926 qemu_put_be32(f, se->section_id);
1927
1928 /* ID string */
1929 len = strlen(se->idstr);
1930 qemu_put_byte(f, len);
1931 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1932
1933 qemu_put_be32(f, se->instance_id);
1934 qemu_put_be32(f, se->version_id);
1935
Alex Williamsondc912122011-01-11 14:39:43 -07001936 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001937 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001938 }
1939
1940 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001941 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001942}
1943
Juan Quintelae4ed1542012-09-21 11:18:18 +02001944uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1945{
1946 SaveStateEntry *se;
1947 uint64_t ret = 0;
1948
1949 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1950 if (!se->ops || !se->ops->save_live_pending) {
1951 continue;
1952 }
1953 if (se->ops && se->ops->is_active) {
1954 if (!se->ops->is_active(se->opaque)) {
1955 continue;
1956 }
1957 }
1958 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1959 }
1960 return ret;
1961}
1962
Juan Quintela65227732013-01-14 14:14:42 +01001963void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001964{
1965 SaveStateEntry *se;
1966
1967 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001968 if (se->ops && se->ops->cancel) {
1969 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001970 }
1971 }
1972}
1973
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001974static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001975{
aliguoria672b462008-11-11 21:33:36 +00001976 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001977 MigrationParams params = {
1978 .blk = 0,
1979 .shared = 0
1980 };
aliguoria672b462008-11-11 21:33:36 +00001981
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001982 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001983 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07001984 }
1985
Paolo Bonzini9b095032013-02-22 17:36:28 +01001986 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001987 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001988 qemu_mutex_lock_iothread();
1989
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001990 while (qemu_file_get_error(f) == 0) {
1991 if (qemu_savevm_state_iterate(f) > 0) {
1992 break;
1993 }
1994 }
aliguoria672b462008-11-11 21:33:36 +00001995
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001996 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001997 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001998 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001999 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02002000 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01002001 if (ret != 0) {
2002 qemu_savevm_state_cancel();
2003 }
aliguoria672b462008-11-11 21:33:36 +00002004 return ret;
2005}
2006
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002007static int qemu_save_device_state(QEMUFile *f)
2008{
2009 SaveStateEntry *se;
2010
2011 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2012 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2013
2014 cpu_synchronize_all_states();
2015
2016 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2017 int len;
2018
2019 if (se->is_ram) {
2020 continue;
2021 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02002022 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002023 continue;
2024 }
2025
2026 /* Section type */
2027 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2028 qemu_put_be32(f, se->section_id);
2029
2030 /* ID string */
2031 len = strlen(se->idstr);
2032 qemu_put_byte(f, len);
2033 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2034
2035 qemu_put_be32(f, se->instance_id);
2036 qemu_put_be32(f, se->version_id);
2037
2038 vmstate_save(f, se);
2039 }
2040
2041 qemu_put_byte(f, QEMU_VM_EOF);
2042
2043 return qemu_file_get_error(f);
2044}
2045
aliguoria672b462008-11-11 21:33:36 +00002046static SaveStateEntry *find_se(const char *idstr, int instance_id)
2047{
2048 SaveStateEntry *se;
2049
Blue Swirl72cf2d42009-09-12 07:36:22 +00002050 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00002051 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02002052 (instance_id == se->instance_id ||
2053 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00002054 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06002055 /* Migrating from an older version? */
2056 if (strstr(se->idstr, idstr) && se->compat) {
2057 if (!strcmp(se->compat->idstr, idstr) &&
2058 (instance_id == se->compat->instance_id ||
2059 instance_id == se->alias_id))
2060 return se;
2061 }
aliguoria672b462008-11-11 21:33:36 +00002062 }
2063 return NULL;
2064}
2065
Juan Quintela811814b2010-07-26 21:38:43 +02002066static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2067{
2068 while(sub && sub->needed) {
2069 if (strcmp(idstr, sub->vmsd->name) == 0) {
2070 return sub->vmsd;
2071 }
2072 sub++;
2073 }
2074 return NULL;
2075}
2076
2077static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2078 void *opaque)
2079{
Juan Quintelac6380722011-10-04 15:28:31 +02002080 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02002081 char idstr[256];
2082 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02002083 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02002084 const VMStateDescription *sub_vmsd;
2085
Juan Quintelac6380722011-10-04 15:28:31 +02002086 len = qemu_peek_byte(f, 1);
2087 if (len < strlen(vmsd->name) + 1) {
2088 /* subsection name has be be "section_name/a" */
2089 return 0;
2090 }
2091 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2092 if (size != len) {
2093 return 0;
2094 }
2095 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02002096
Juan Quintelac6380722011-10-04 15:28:31 +02002097 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2098 /* it don't have a valid subsection name */
2099 return 0;
2100 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02002101 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02002102 if (sub_vmsd == NULL) {
2103 return -ENOENT;
2104 }
Juan Quintelac6380722011-10-04 15:28:31 +02002105 qemu_file_skip(f, 1); /* subsection */
2106 qemu_file_skip(f, 1); /* len */
2107 qemu_file_skip(f, len); /* idstr */
2108 version_id = qemu_get_be32(f);
2109
Juan Quintela811814b2010-07-26 21:38:43 +02002110 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2111 if (ret) {
2112 return ret;
2113 }
2114 }
2115 return 0;
2116}
2117
2118static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2119 void *opaque)
2120{
2121 const VMStateSubsection *sub = vmsd->subsections;
2122
2123 while (sub && sub->needed) {
2124 if (sub->needed(opaque)) {
2125 const VMStateDescription *vmsd = sub->vmsd;
2126 uint8_t len;
2127
2128 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2129 len = strlen(vmsd->name);
2130 qemu_put_byte(f, len);
2131 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2132 qemu_put_be32(f, vmsd->version_id);
2133 vmstate_save_state(f, vmsd, opaque);
2134 }
2135 sub++;
2136 }
2137}
2138
aliguoria672b462008-11-11 21:33:36 +00002139typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002140 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002141 SaveStateEntry *se;
2142 int section_id;
2143 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002144} LoadStateEntry;
2145
aliguoria672b462008-11-11 21:33:36 +00002146int qemu_loadvm_state(QEMUFile *f)
2147{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002148 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2149 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002150 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002151 uint8_t section_type;
2152 unsigned int v;
2153 int ret;
2154
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002155 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002156 return -EINVAL;
2157 }
2158
aliguoria672b462008-11-11 21:33:36 +00002159 v = qemu_get_be32(f);
2160 if (v != QEMU_VM_FILE_MAGIC)
2161 return -EINVAL;
2162
2163 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002164 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2165 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2166 return -ENOTSUP;
2167 }
aliguoria672b462008-11-11 21:33:36 +00002168 if (v != QEMU_VM_FILE_VERSION)
2169 return -ENOTSUP;
2170
2171 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2172 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002173 SaveStateEntry *se;
2174 char idstr[257];
2175 int len;
2176
2177 switch (section_type) {
2178 case QEMU_VM_SECTION_START:
2179 case QEMU_VM_SECTION_FULL:
2180 /* Read section start */
2181 section_id = qemu_get_be32(f);
2182 len = qemu_get_byte(f);
2183 qemu_get_buffer(f, (uint8_t *)idstr, len);
2184 idstr[len] = 0;
2185 instance_id = qemu_get_be32(f);
2186 version_id = qemu_get_be32(f);
2187
2188 /* Find savevm section */
2189 se = find_se(idstr, instance_id);
2190 if (se == NULL) {
2191 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2192 ret = -EINVAL;
2193 goto out;
2194 }
2195
2196 /* Validate version */
2197 if (version_id > se->version_id) {
2198 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2199 version_id, idstr, se->version_id);
2200 ret = -EINVAL;
2201 goto out;
2202 }
2203
2204 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002205 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002206
2207 le->se = se;
2208 le->section_id = section_id;
2209 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002210 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002211
Juan Quintela4082be42009-08-20 19:42:24 +02002212 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002213 if (ret < 0) {
2214 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2215 instance_id, idstr);
2216 goto out;
2217 }
aliguoria672b462008-11-11 21:33:36 +00002218 break;
2219 case QEMU_VM_SECTION_PART:
2220 case QEMU_VM_SECTION_END:
2221 section_id = qemu_get_be32(f);
2222
Blue Swirl72cf2d42009-09-12 07:36:22 +00002223 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002224 if (le->section_id == section_id) {
2225 break;
2226 }
2227 }
aliguoria672b462008-11-11 21:33:36 +00002228 if (le == NULL) {
2229 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2230 ret = -EINVAL;
2231 goto out;
2232 }
2233
Juan Quintela4082be42009-08-20 19:42:24 +02002234 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002235 if (ret < 0) {
2236 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2237 section_id);
2238 goto out;
2239 }
aliguoria672b462008-11-11 21:33:36 +00002240 break;
2241 default:
2242 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2243 ret = -EINVAL;
2244 goto out;
2245 }
2246 }
2247
Jan Kiszkaea375f92010-03-01 19:10:30 +01002248 cpu_synchronize_all_post_init();
2249
aliguoria672b462008-11-11 21:33:36 +00002250 ret = 0;
2251
2252out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002253 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2254 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002255 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002256 }
2257
Juan Quintela42802d42011-10-05 01:14:46 +02002258 if (ret == 0) {
2259 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002260 }
aliguoria672b462008-11-11 21:33:36 +00002261
2262 return ret;
2263}
2264
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002265static BlockDriverState *find_vmstate_bs(void)
2266{
2267 BlockDriverState *bs = NULL;
2268 while ((bs = bdrv_next(bs))) {
2269 if (bdrv_can_snapshot(bs)) {
2270 return bs;
2271 }
2272 }
2273 return NULL;
2274}
2275
aliguoria672b462008-11-11 21:33:36 +00002276static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2277 const char *name)
2278{
2279 QEMUSnapshotInfo *sn_tab, *sn;
2280 int nb_sns, i, ret;
2281
2282 ret = -ENOENT;
2283 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2284 if (nb_sns < 0)
2285 return ret;
2286 for(i = 0; i < nb_sns; i++) {
2287 sn = &sn_tab[i];
2288 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2289 *sn_info = *sn;
2290 ret = 0;
2291 break;
2292 }
2293 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002294 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002295 return ret;
2296}
2297
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002298/*
2299 * Deletes snapshots of a given name in all opened images.
2300 */
2301static int del_existing_snapshots(Monitor *mon, const char *name)
2302{
2303 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002304 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2305 int ret;
2306
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002307 bs = NULL;
2308 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002309 if (bdrv_can_snapshot(bs) &&
2310 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2311 {
2312 ret = bdrv_snapshot_delete(bs, name);
2313 if (ret < 0) {
2314 monitor_printf(mon,
2315 "Error while deleting snapshot on '%s'\n",
2316 bdrv_get_device_name(bs));
2317 return -1;
2318 }
2319 }
2320 }
2321
2322 return 0;
2323}
2324
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002325void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002326{
2327 BlockDriverState *bs, *bs1;
2328 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002329 int ret;
aliguoria672b462008-11-11 21:33:36 +00002330 QEMUFile *f;
2331 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002332 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002333 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002334 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002335 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002336
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002337 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002338 bs = NULL;
2339 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002340
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002341 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002342 continue;
2343 }
2344
2345 if (!bdrv_can_snapshot(bs)) {
2346 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2347 bdrv_get_device_name(bs));
2348 return;
2349 }
2350 }
2351
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002352 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002353 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002354 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002355 return;
2356 }
aliguoria672b462008-11-11 21:33:36 +00002357
Luiz Capitulino13548692011-07-29 15:36:43 -03002358 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002359 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002360
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002361 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002362
2363 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002364 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002365 sn->date_sec = tv.tv_sec;
2366 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002367 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002368
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002369 if (name) {
2370 ret = bdrv_snapshot_find(bs, old_sn, name);
2371 if (ret >= 0) {
2372 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2373 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2374 } else {
2375 pstrcpy(sn->name, sizeof(sn->name), name);
2376 }
2377 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002378 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2379 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002380 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002381 }
2382
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002383 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002384 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002385 goto the_end;
2386 }
2387
aliguoria672b462008-11-11 21:33:36 +00002388 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002389 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002390 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002391 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002392 goto the_end;
2393 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002394 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002395 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002396 qemu_fclose(f);
2397 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002398 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002399 goto the_end;
2400 }
2401
2402 /* create the snapshots */
2403
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002404 bs1 = NULL;
2405 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002406 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002407 /* Write VM state size only to the image that contains the state */
2408 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002409 ret = bdrv_snapshot_create(bs1, sn);
2410 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002411 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2412 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002413 }
2414 }
2415 }
2416
2417 the_end:
2418 if (saved_vm_running)
2419 vm_start();
2420}
2421
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002422void qmp_xen_save_devices_state(const char *filename, Error **errp)
2423{
2424 QEMUFile *f;
2425 int saved_vm_running;
2426 int ret;
2427
2428 saved_vm_running = runstate_is_running();
2429 vm_stop(RUN_STATE_SAVE_VM);
2430
2431 f = qemu_fopen(filename, "wb");
2432 if (!f) {
2433 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2434 goto the_end;
2435 }
2436 ret = qemu_save_device_state(f);
2437 qemu_fclose(f);
2438 if (ret < 0) {
2439 error_set(errp, QERR_IO_ERROR);
2440 }
2441
2442 the_end:
2443 if (saved_vm_running)
2444 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002445}
2446
Markus Armbruster03cd4652010-02-17 16:24:10 +01002447int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002448{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002449 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002450 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002451 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002452 int ret;
aliguoria672b462008-11-11 21:33:36 +00002453
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002454 bs_vm_state = find_vmstate_bs();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002455 if (!bs_vm_state) {
2456 error_report("No block device supports snapshots");
2457 return -ENOTSUP;
2458 }
2459
2460 /* Don't even try to load empty VM states */
2461 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2462 if (ret < 0) {
2463 return ret;
2464 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002465 error_report("This is a disk-only snapshot. Revert to it offline "
2466 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002467 return -EINVAL;
2468 }
2469
2470 /* Verify if there is any device that doesn't support snapshots and is
2471 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002472 bs = NULL;
2473 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002474
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002475 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002476 continue;
2477 }
2478
2479 if (!bdrv_can_snapshot(bs)) {
2480 error_report("Device '%s' is writable but does not support snapshots.",
2481 bdrv_get_device_name(bs));
2482 return -ENOTSUP;
2483 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002484
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002485 ret = bdrv_snapshot_find(bs, &sn, name);
2486 if (ret < 0) {
2487 error_report("Device '%s' does not have the requested snapshot '%s'",
2488 bdrv_get_device_name(bs), name);
2489 return ret;
2490 }
aliguoria672b462008-11-11 21:33:36 +00002491 }
2492
2493 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002494 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002495
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002496 bs = NULL;
2497 while ((bs = bdrv_next(bs))) {
2498 if (bdrv_can_snapshot(bs)) {
2499 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002500 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002501 error_report("Error %d while activating snapshot '%s' on '%s'",
2502 ret, name, bdrv_get_device_name(bs));
2503 return ret;
aliguoria672b462008-11-11 21:33:36 +00002504 }
2505 }
2506 }
2507
aliguoria672b462008-11-11 21:33:36 +00002508 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002509 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002510 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002511 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002512 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002513 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002514
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002515 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002516 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002517
aliguoria672b462008-11-11 21:33:36 +00002518 qemu_fclose(f);
2519 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002520 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002521 return ret;
aliguoria672b462008-11-11 21:33:36 +00002522 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002523
Juan Quintela05f24012009-08-20 19:42:22 +02002524 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002525}
2526
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002527void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002528{
2529 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002530 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002531 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002532
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002533 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002534 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002535 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002536 return;
2537 }
2538
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002539 bs1 = NULL;
2540 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002541 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002542 ret = bdrv_snapshot_delete(bs1, name);
2543 if (ret < 0) {
2544 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002545 monitor_printf(mon,
2546 "Snapshots not supported on device '%s'\n",
2547 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002548 else
aliguori376253e2009-03-05 23:01:23 +00002549 monitor_printf(mon, "Error %d while deleting snapshot on "
2550 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002551 }
2552 }
2553 }
2554}
2555
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002556void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002557{
2558 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002559 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2560 int nb_sns, i, ret, available;
2561 int total;
2562 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002563 char buf[256];
2564
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002565 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002566 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002567 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002568 return;
2569 }
aliguoria672b462008-11-11 21:33:36 +00002570
2571 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2572 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002573 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002574 return;
2575 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002576
2577 if (nb_sns == 0) {
2578 monitor_printf(mon, "There is no snapshot available.\n");
2579 return;
aliguoria672b462008-11-11 21:33:36 +00002580 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002581
Anthony Liguori7267c092011-08-20 22:09:37 -05002582 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002583 total = 0;
2584 for (i = 0; i < nb_sns; i++) {
2585 sn = &sn_tab[i];
2586 available = 1;
2587 bs1 = NULL;
2588
2589 while ((bs1 = bdrv_next(bs1))) {
2590 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2591 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2592 if (ret < 0) {
2593 available = 0;
2594 break;
2595 }
2596 }
2597 }
2598
2599 if (available) {
2600 available_snapshots[total] = i;
2601 total++;
2602 }
2603 }
2604
2605 if (total > 0) {
2606 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2607 for (i = 0; i < total; i++) {
2608 sn = &sn_tab[available_snapshots[i]];
2609 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2610 }
2611 } else {
2612 monitor_printf(mon, "There is no suitable snapshot available\n");
2613 }
2614
Anthony Liguori7267c092011-08-20 22:09:37 -05002615 g_free(sn_tab);
2616 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002617
aliguoria672b462008-11-11 21:33:36 +00002618}
Avi Kivityc5705a72011-12-20 15:59:12 +02002619
2620void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2621{
Avi Kivity1ddde082012-01-08 13:18:19 +02002622 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002623 memory_region_name(mr), dev);
2624}
2625
2626void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2627{
2628 /* Nothing do to while the implementation is in RAMBlock */
2629}
2630
2631void vmstate_register_ram_global(MemoryRegion *mr)
2632{
2633 vmstate_register_ram(mr, NULL);
2634}