blob: ff5ece651adfd981174561ca46a565c80a9f2d43 [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
aliguoria672b462008-11-11 21:33:36 +000024
blueswir1d40cdb12009-03-07 16:52:02 +000025#include "config-host.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include "qemu-common.h"
27#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060028#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/net.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/timer.h"
blueswir1511d2b12009-03-07 15:32:56 +000033#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010034#include "migration/migration.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
36#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000039#include "qmp-commands.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020040#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/bitops.h"
Orit Wasserman28085f72013-03-22 16:47:58 +020042#include "qemu/iov.h"
Wenchao Xiade08c602013-05-25 11:09:43 +080043#include "block/snapshot.h"
Wenchao Xiaf364ec62013-05-25 11:09:44 +080044#include "block/qapi.h"
blueswir1511d2b12009-03-07 15:32:56 +000045
aliguoria672b462008-11-11 21:33:36 +000046#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000047
Nolan18995b92009-10-15 16:53:55 -070048#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040049#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070050#endif
51#define ARP_HTYPE_ETH 0x0001
52#define ARP_PTYPE_IP 0x0800
53#define ARP_OP_REQUEST_REV 0x3
54
55static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000056 uint8_t *mac_addr)
57{
Nolan18995b92009-10-15 16:53:55 -070058 /* Ethernet header. */
59 memset(buf, 0xff, 6); /* destination MAC addr */
60 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
61 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000062
Nolan18995b92009-10-15 16:53:55 -070063 /* RARP header. */
64 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
65 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
66 *(buf + 18) = 6; /* hardware addr length (ethernet) */
67 *(buf + 19) = 4; /* protocol addr length (IPv4) */
68 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
69 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
70 memset(buf + 28, 0x00, 4); /* source protocol addr */
71 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
72 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000073
Nolan18995b92009-10-15 16:53:55 -070074 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
75 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000076
Nolan18995b92009-10-15 16:53:55 -070077 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000078}
79
Mark McLoughlinf401ca22009-11-25 18:49:32 +000080static void qemu_announce_self_iter(NICState *nic, void *opaque)
81{
82 uint8_t buf[60];
83 int len;
84
85 len = announce_self_create(buf, nic->conf->macaddr.a);
86
Jason Wangb356f762013-01-30 19:12:22 +080087 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000088}
89
90
Gleb Natapoved8b3302009-05-21 17:17:44 +030091static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000092{
Gleb Natapoved8b3302009-05-21 17:17:44 +030093 static int count = SELF_ANNOUNCE_ROUNDS;
94 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000095
Mark McLoughlinf401ca22009-11-25 18:49:32 +000096 qemu_foreach_nic(qemu_announce_self_iter, NULL);
97
Nolan18995b92009-10-15 16:53:55 -070098 if (--count) {
99 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100100 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700101 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300102 } else {
103 qemu_del_timer(timer);
104 qemu_free_timer(timer);
105 }
106}
107
108void qemu_announce_self(void)
109{
110 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100111 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300112 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000113}
114
115/***********************************************************/
116/* savevm/loadvm support */
117
118#define IO_BUF_SIZE 32768
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200119#define MAX_IOV_SIZE MIN(IOV_MAX, 64)
aliguoria672b462008-11-11 21:33:36 +0000120
121struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200122 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000123 void *opaque;
aliguoria672b462008-11-11 21:33:36 +0000124
Paolo Bonzini1964a392013-02-22 17:36:45 +0100125 int64_t bytes_xfer;
126 int64_t xfer_limit;
127
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100128 int64_t pos; /* start of buffer when writing, end of buffer
129 when reading */
aliguoria672b462008-11-11 21:33:36 +0000130 int buf_index;
131 int buf_size; /* 0 when writing */
132 uint8_t buf[IO_BUF_SIZE];
133
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200134 struct iovec iov[MAX_IOV_SIZE];
135 unsigned int iovcnt;
136
Juan Quintela3961b4d2011-10-05 01:05:21 +0200137 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000138};
139
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200140typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000141{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200142 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000143 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200144} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000145
146typedef struct QEMUFileSocket
147{
148 int fd;
149 QEMUFile *file;
150} QEMUFileSocket;
151
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100152typedef struct {
153 Coroutine *co;
154 int fd;
155} FDYieldUntilData;
156
157static void fd_coroutine_enter(void *opaque)
158{
159 FDYieldUntilData *data = opaque;
160 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
161 qemu_coroutine_enter(data->co, NULL);
162}
163
164/**
165 * Yield until a file descriptor becomes readable
166 *
167 * Note that this function clobbers the handlers for the file descriptor.
168 */
169static void coroutine_fn yield_until_fd_readable(int fd)
170{
171 FDYieldUntilData data;
172
173 assert(qemu_in_coroutine());
174 data.co = qemu_coroutine_self();
175 data.fd = fd;
176 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
177 qemu_coroutine_yield();
178}
179
Kevin Wolf05fcc842013-04-05 21:27:54 +0200180static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
181 int64_t pos)
Orit Wasserman28085f72013-03-22 16:47:58 +0200182{
183 QEMUFileSocket *s = opaque;
184 ssize_t len;
185 ssize_t size = iov_size(iov, iovcnt);
186
187 len = iov_send(s->fd, iov, iovcnt, 0, size);
188 if (len < size) {
189 len = -socket_error();
190 }
191 return len;
192}
193
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200194static int socket_get_fd(void *opaque)
195{
196 QEMUFileSocket *s = opaque;
197
198 return s->fd;
199}
200
aliguoria672b462008-11-11 21:33:36 +0000201static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
202{
203 QEMUFileSocket *s = opaque;
204 ssize_t len;
205
Paolo Bonzini595ab642012-08-07 11:07:59 +0200206 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000207 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab642012-08-07 11:07:59 +0200208 if (len != -1) {
209 break;
210 }
211 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100212 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab642012-08-07 11:07:59 +0200213 } else if (socket_error() != EINTR) {
214 break;
215 }
216 }
aliguoria672b462008-11-11 21:33:36 +0000217
Paolo Bonzini595ab642012-08-07 11:07:59 +0200218 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000219 len = -socket_error();
Paolo Bonzini595ab642012-08-07 11:07:59 +0200220 }
aliguoria672b462008-11-11 21:33:36 +0000221 return len;
222}
223
224static int socket_close(void *opaque)
225{
226 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200227 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500228 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000229 return 0;
230}
231
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200232static int stdio_get_fd(void *opaque)
233{
234 QEMUFileStdio *s = opaque;
235
236 return fileno(s->stdio_file);
237}
238
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200239static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000240{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200241 QEMUFileStdio *s = opaque;
242 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000243}
244
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200245static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000246{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200247 QEMUFileStdio *s = opaque;
248 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300249 int bytes;
250
Paolo Bonzini595ab642012-08-07 11:07:59 +0200251 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300252 clearerr(fp);
253 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab642012-08-07 11:07:59 +0200254 if (bytes != 0 || !ferror(fp)) {
255 break;
256 }
257 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100258 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab642012-08-07 11:07:59 +0200259 } else if (errno != EINTR) {
260 break;
261 }
262 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300263 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000264}
265
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200266static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000267{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200268 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500269 int ret;
270 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200271 if (ret == -1) {
272 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100273 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
274 /* close succeeded, but non-zero exit code: */
275 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200276 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500277 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500278 return ret;
aliguoria672b462008-11-11 21:33:36 +0000279}
280
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200281static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000282{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200283 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200284 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100285
Orit Wassermancb88aa82013-03-22 16:48:01 +0200286 if (s->file->ops->put_buffer || s->file->ops->writev_buffer) {
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100287 int fd = fileno(s->stdio_file);
288 struct stat st;
289
290 ret = fstat(fd, &st);
291 if (ret == 0 && S_ISREG(st.st_mode)) {
292 /*
293 * If the file handle is a regular file make sure the
294 * data is flushed to disk before signaling success.
295 */
296 ret = fsync(fd);
297 if (ret != 0) {
298 ret = -errno;
299 return ret;
300 }
301 }
302 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200303 if (fclose(s->stdio_file) == EOF) {
304 ret = -errno;
305 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500306 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200307 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200308}
aliguoria672b462008-11-11 21:33:36 +0000309
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200310static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200311 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200312 .get_buffer = stdio_get_buffer,
313 .close = stdio_pclose
314};
315
316static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200317 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200318 .put_buffer = stdio_put_buffer,
319 .close = stdio_pclose
320};
321
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100322QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200323{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100324 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200325 QEMUFileStdio *s;
326
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200327 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
328 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100329 return NULL;
330 }
331
Paolo Bonzinia4cc73d2013-05-31 14:00:27 +0200332 stdio_file = popen(command, mode);
333 if (stdio_file == NULL) {
aliguoria672b462008-11-11 21:33:36 +0000334 return NULL;
335 }
336
Anthony Liguori7267c092011-08-20 22:09:37 -0500337 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000338
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200339 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000340
341 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200342 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000343 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200344 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000345 }
aliguoria672b462008-11-11 21:33:36 +0000346 return s->file;
347}
348
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200349static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200350 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200351 .get_buffer = stdio_get_buffer,
352 .close = stdio_fclose
353};
354
355static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200356 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200357 .put_buffer = stdio_put_buffer,
358 .close = stdio_fclose
359};
360
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100361static ssize_t unix_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
362 int64_t pos)
363{
364 QEMUFileSocket *s = opaque;
365 ssize_t len, offset;
366 ssize_t size = iov_size(iov, iovcnt);
367 ssize_t total = 0;
368
369 assert(iovcnt > 0);
370 offset = 0;
371 while (size > 0) {
372 /* Find the next start position; skip all full-sized vector elements */
373 while (offset >= iov[0].iov_len) {
374 offset -= iov[0].iov_len;
375 iov++, iovcnt--;
376 }
377
378 /* skip `offset' bytes from the (now) first element, undo it on exit */
379 assert(iovcnt > 0);
380 iov[0].iov_base += offset;
381 iov[0].iov_len -= offset;
382
383 do {
384 len = writev(s->fd, iov, iovcnt);
385 } while (len == -1 && errno == EINTR);
386 if (len == -1) {
387 return -errno;
388 }
389
390 /* Undo the changes above */
391 iov[0].iov_base -= offset;
392 iov[0].iov_len += offset;
393
394 /* Prepare for the next iteration */
395 offset += len;
396 total += len;
397 size -= len;
398 }
399
400 return total;
401}
402
403static int unix_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
404{
405 QEMUFileSocket *s = opaque;
406 ssize_t len;
407
408 for (;;) {
409 len = read(s->fd, buf, size);
410 if (len != -1) {
411 break;
412 }
413 if (errno == EAGAIN) {
414 yield_until_fd_readable(s->fd);
415 } else if (errno != EINTR) {
416 break;
417 }
418 }
419
420 if (len == -1) {
421 len = -errno;
422 }
423 return len;
424}
425
426static int unix_close(void *opaque)
427{
428 QEMUFileSocket *s = opaque;
429 close(s->fd);
430 g_free(s);
431 return 0;
432}
433
434static const QEMUFileOps unix_read_ops = {
435 .get_fd = socket_get_fd,
436 .get_buffer = unix_get_buffer,
437 .close = unix_close
438};
439
440static const QEMUFileOps unix_write_ops = {
441 .get_fd = socket_get_fd,
442 .writev_buffer = unix_writev_buffer,
443 .close = unix_close
444};
445
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200446QEMUFile *qemu_fdopen(int fd, const char *mode)
447{
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100448 QEMUFileSocket *s;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200449
450 if (mode == NULL ||
451 (mode[0] != 'r' && mode[0] != 'w') ||
452 mode[1] != 'b' || mode[2] != 0) {
453 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
454 return NULL;
455 }
456
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100457 s = g_malloc0(sizeof(QEMUFileSocket));
458 s->fd = fd;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200459
460 if(mode[0] == 'r') {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100461 s->file = qemu_fopen_ops(s, &unix_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200462 } else {
Paolo Bonzinie9d8fbf2013-03-27 17:36:32 +0100463 s->file = qemu_fopen_ops(s, &unix_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200464 }
465 return s->file;
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200466}
467
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200468static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200469 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200470 .get_buffer = socket_get_buffer,
471 .close = socket_close
472};
473
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100474static const QEMUFileOps socket_write_ops = {
475 .get_fd = socket_get_fd,
Orit Wasserman28085f72013-03-22 16:47:58 +0200476 .writev_buffer = socket_writev_buffer,
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100477 .close = socket_close
478};
479
480QEMUFile *qemu_fopen_socket(int fd, const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000481{
Anthony Liguori7267c092011-08-20 22:09:37 -0500482 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000483
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100484 if (mode == NULL ||
485 (mode[0] != 'r' && mode[0] != 'w') ||
486 mode[1] != 'b' || mode[2] != 0) {
487 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
488 return NULL;
489 }
490
aliguoria672b462008-11-11 21:33:36 +0000491 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100492 if (mode[0] == 'w') {
Stefan Hajnoczif9e8cac2013-03-27 10:10:43 +0100493 qemu_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100494 s->file = qemu_fopen_ops(s, &socket_write_ops);
495 } else {
496 s->file = qemu_fopen_ops(s, &socket_read_ops);
497 }
aliguoria672b462008-11-11 21:33:36 +0000498 return s->file;
499}
500
aliguoria672b462008-11-11 21:33:36 +0000501QEMUFile *qemu_fopen(const char *filename, const char *mode)
502{
503 QEMUFileStdio *s;
504
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200505 if (mode == NULL ||
506 (mode[0] != 'r' && mode[0] != 'w') ||
507 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000508 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200509 return NULL;
510 }
511
Anthony Liguori7267c092011-08-20 22:09:37 -0500512 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000513
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200514 s->stdio_file = fopen(filename, mode);
515 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000516 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200517
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200518 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200519 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200520 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200521 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200522 }
523 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000524fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500525 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000526 return NULL;
527}
528
Kevin Wolf05fcc842013-04-05 21:27:54 +0200529static ssize_t block_writev_buffer(void *opaque, struct iovec *iov, int iovcnt,
530 int64_t pos)
531{
532 int ret;
533 QEMUIOVector qiov;
534
535 qemu_iovec_init_external(&qiov, iov, iovcnt);
536 ret = bdrv_writev_vmstate(opaque, &qiov, pos);
537 if (ret < 0) {
538 return ret;
539 }
540
541 return qiov.size;
542}
543
aliguori178e08a2009-04-05 19:10:55 +0000544static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000545 int64_t pos, int size)
546{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200547 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000548 return size;
549}
550
aliguori178e08a2009-04-05 19:10:55 +0000551static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000552{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200553 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000554}
555
556static int bdrv_fclose(void *opaque)
557{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200558 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000559}
560
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200561static const QEMUFileOps bdrv_read_ops = {
562 .get_buffer = block_get_buffer,
563 .close = bdrv_fclose
564};
565
566static const QEMUFileOps bdrv_write_ops = {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200567 .put_buffer = block_put_buffer,
568 .writev_buffer = block_writev_buffer,
569 .close = bdrv_fclose
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200570};
571
Christoph Hellwig45566e92009-07-10 23:11:57 +0200572static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000573{
aliguoria672b462008-11-11 21:33:36 +0000574 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200575 return qemu_fopen_ops(bs, &bdrv_write_ops);
576 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000577}
578
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200579QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000580{
581 QEMUFile *f;
582
Anthony Liguori7267c092011-08-20 22:09:37 -0500583 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000584
585 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200586 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000587 return f;
588}
589
Juan Quintela624b9cc2011-10-05 01:02:52 +0200590int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000591{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200592 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000593}
594
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100595static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000596{
Juan Quintelaafe41932013-01-14 13:36:28 +0100597 if (f->last_error == 0) {
598 f->last_error = ret;
599 }
aliguori4dabe242009-04-05 19:30:51 +0000600}
601
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200602static inline bool qemu_file_is_writable(QEMUFile *f)
603{
604 return f->ops->writev_buffer || f->ops->put_buffer;
605}
606
Orit Wassermancb88aa82013-03-22 16:48:01 +0200607/**
608 * Flushes QEMUFile buffer
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200609 *
Orit Wassermancb88aa82013-03-22 16:48:01 +0200610 * If there is writev_buffer QEMUFileOps it uses it otherwise uses
611 * put_buffer ops.
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200612 */
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100613static void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000614{
Orit Wassermancb88aa82013-03-22 16:48:01 +0200615 ssize_t ret = 0;
Juan Quintela7311bea2012-08-29 19:08:59 +0200616
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200617 if (!qemu_file_is_writable(f)) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100618 return;
619 }
Orit Wassermancb88aa82013-03-22 16:48:01 +0200620
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200621 if (f->ops->writev_buffer) {
622 if (f->iovcnt > 0) {
Kevin Wolf05fcc842013-04-05 21:27:54 +0200623 ret = f->ops->writev_buffer(f->opaque, f->iov, f->iovcnt, f->pos);
Juan Quintela7311bea2012-08-29 19:08:59 +0200624 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200625 } else {
626 if (f->buf_index > 0) {
627 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200628 }
aliguoria672b462008-11-11 21:33:36 +0000629 }
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200630 if (ret >= 0) {
631 f->pos += ret;
632 }
633 f->buf_index = 0;
634 f->iovcnt = 0;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100635 if (ret < 0) {
636 qemu_file_set_error(f, ret);
637 }
aliguoria672b462008-11-11 21:33:36 +0000638}
639
640static void qemu_fill_buffer(QEMUFile *f)
641{
642 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200643 int pending;
aliguoria672b462008-11-11 21:33:36 +0000644
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200645 assert(!qemu_file_is_writable(f));
aliguoria672b462008-11-11 21:33:36 +0000646
Juan Quintela0046c452011-09-30 19:28:45 +0200647 pending = f->buf_size - f->buf_index;
648 if (pending > 0) {
649 memmove(f->buf, f->buf + f->buf_index, pending);
650 }
651 f->buf_index = 0;
652 f->buf_size = pending;
653
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100654 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200655 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000656 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200657 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100658 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200659 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200660 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000661 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200662 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000663}
664
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200665int qemu_get_fd(QEMUFile *f)
666{
667 if (f->ops->get_fd) {
668 return f->ops->get_fd(f->opaque);
669 }
670 return -1;
671}
672
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200673/** Closes the file
674 *
675 * Returns negative error value if any error happened on previous operations or
676 * while closing the file. Returns 0 or positive number on success.
677 *
678 * The meaning of return value on success depends on the specific backend
679 * being used.
680 */
681int qemu_fclose(QEMUFile *f)
682{
Juan Quintela29eee862012-08-29 19:14:54 +0200683 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100684 qemu_fflush(f);
685 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200686
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200687 if (f->ops->close) {
688 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200689 if (ret >= 0) {
690 ret = ret2;
691 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200692 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200693 /* If any error was spotted before closing, we should report it
694 * instead of the close() return value.
695 */
696 if (f->last_error) {
697 ret = f->last_error;
698 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500699 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000700 return ret;
701}
702
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200703static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
704{
705 /* check for adjacent buffer and coalesce them */
706 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
707 f->iov[f->iovcnt - 1].iov_len) {
708 f->iov[f->iovcnt - 1].iov_len += size;
709 } else {
710 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
711 f->iov[f->iovcnt++].iov_len = size;
712 }
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200713
Paolo Bonzini4d117242013-04-08 13:29:57 +0200714 if (f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200715 qemu_fflush(f);
716 }
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200717}
718
Orit Wasserman6181ec22013-03-22 16:48:02 +0200719void qemu_put_buffer_async(QEMUFile *f, const uint8_t *buf, int size)
720{
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200721 if (!f->ops->writev_buffer) {
722 qemu_put_buffer(f, buf, size);
723 return;
724 }
725
Orit Wasserman6181ec22013-03-22 16:48:02 +0200726 if (f->last_error) {
727 return;
728 }
729
Orit Wasserman6181ec22013-03-22 16:48:02 +0200730 f->bytes_xfer += size;
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200731 add_to_iovec(f, buf, size);
Orit Wasserman6181ec22013-03-22 16:48:02 +0200732}
733
aliguoria672b462008-11-11 21:33:36 +0000734void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
735{
736 int l;
737
Juan Quintelac10682c2012-08-29 19:43:39 +0200738 if (f->last_error) {
739 return;
740 }
741
Juan Quintelac10682c2012-08-29 19:43:39 +0200742 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000743 l = IO_BUF_SIZE - f->buf_index;
744 if (l > size)
745 l = size;
746 memcpy(f->buf + f->buf_index, buf, l);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200747 f->bytes_xfer += size;
748 if (f->ops->writev_buffer) {
749 add_to_iovec(f, f->buf + f->buf_index, l);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200750 }
751 f->buf_index += l;
752 if (f->buf_index == IO_BUF_SIZE) {
753 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200754 }
Orit Wasserman6181ec22013-03-22 16:48:02 +0200755 if (qemu_file_get_error(f)) {
756 break;
757 }
aliguoria672b462008-11-11 21:33:36 +0000758 buf += l;
759 size -= l;
aliguoria672b462008-11-11 21:33:36 +0000760 }
761}
762
763void qemu_put_byte(QEMUFile *f, int v)
764{
Juan Quintelac10682c2012-08-29 19:43:39 +0200765 if (f->last_error) {
766 return;
767 }
768
Paolo Bonziniaf74db72013-04-08 13:29:54 +0200769 f->buf[f->buf_index] = v;
Orit Wasserman7d8a30bb2013-03-22 16:47:59 +0200770 f->bytes_xfer++;
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200771 if (f->ops->writev_buffer) {
772 add_to_iovec(f, f->buf + f->buf_index, 1);
Paolo Bonzini4d117242013-04-08 13:29:57 +0200773 }
774 f->buf_index++;
775 if (f->buf_index == IO_BUF_SIZE) {
776 qemu_fflush(f);
Paolo Bonzini7ce51f12013-04-08 13:29:55 +0200777 }
aliguoria672b462008-11-11 21:33:36 +0000778}
779
Juan Quintelac6380722011-10-04 15:28:31 +0200780static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000781{
Juan Quintelac6380722011-10-04 15:28:31 +0200782 if (f->buf_index + size <= f->buf_size) {
783 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000784 }
aliguoria672b462008-11-11 21:33:36 +0000785}
786
Juan Quintelac6380722011-10-04 15:28:31 +0200787static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200788{
Juan Quintelac6380722011-10-04 15:28:31 +0200789 int pending;
790 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200791
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200792 assert(!qemu_file_is_writable(f));
Juan Quintela811814b2010-07-26 21:38:43 +0200793
Juan Quintelac6380722011-10-04 15:28:31 +0200794 index = f->buf_index + offset;
795 pending = f->buf_size - index;
796 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200797 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200798 index = f->buf_index + offset;
799 pending = f->buf_size - index;
800 }
801
802 if (pending <= 0) {
803 return 0;
804 }
805 if (size > pending) {
806 size = pending;
807 }
808
809 memcpy(buf, f->buf + index, size);
810 return size;
811}
812
813int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
814{
815 int pending = size;
816 int done = 0;
817
818 while (pending > 0) {
819 int res;
820
821 res = qemu_peek_buffer(f, buf, pending, 0);
822 if (res == 0) {
823 return done;
824 }
825 qemu_file_skip(f, res);
826 buf += res;
827 pending -= res;
828 done += res;
829 }
830 return done;
831}
832
833static int qemu_peek_byte(QEMUFile *f, int offset)
834{
835 int index = f->buf_index + offset;
836
Paolo Bonzinid9658c42013-04-08 13:29:56 +0200837 assert(!qemu_file_is_writable(f));
Juan Quintelac6380722011-10-04 15:28:31 +0200838
839 if (index >= f->buf_size) {
840 qemu_fill_buffer(f);
841 index = f->buf_index + offset;
842 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200843 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200844 }
Juan Quintela811814b2010-07-26 21:38:43 +0200845 }
Juan Quintelac6380722011-10-04 15:28:31 +0200846 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200847}
848
aliguoria672b462008-11-11 21:33:36 +0000849int qemu_get_byte(QEMUFile *f)
850{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200851 int result;
aliguoria672b462008-11-11 21:33:36 +0000852
Juan Quintelac6380722011-10-04 15:28:31 +0200853 result = qemu_peek_byte(f, 0);
854 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200855 return result;
aliguoria672b462008-11-11 21:33:36 +0000856}
857
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100858int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000859{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100860 qemu_fflush(f);
861 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000862}
863
aliguoria672b462008-11-11 21:33:36 +0000864int qemu_file_rate_limit(QEMUFile *f)
865{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100866 if (qemu_file_get_error(f)) {
867 return 1;
868 }
869 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
870 return 1;
871 }
aliguoria672b462008-11-11 21:33:36 +0000872 return 0;
873}
874
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200875int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200876{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100877 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200878}
879
Paolo Bonzini1964a392013-02-22 17:36:45 +0100880void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400881{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100882 f->xfer_limit = limit;
883}
Glauber Costa19629532009-05-20 18:26:57 -0400884
Paolo Bonzini1964a392013-02-22 17:36:45 +0100885void qemu_file_reset_rate_limit(QEMUFile *f)
886{
887 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400888}
889
aliguoria672b462008-11-11 21:33:36 +0000890void qemu_put_be16(QEMUFile *f, unsigned int v)
891{
892 qemu_put_byte(f, v >> 8);
893 qemu_put_byte(f, v);
894}
895
896void qemu_put_be32(QEMUFile *f, unsigned int v)
897{
898 qemu_put_byte(f, v >> 24);
899 qemu_put_byte(f, v >> 16);
900 qemu_put_byte(f, v >> 8);
901 qemu_put_byte(f, v);
902}
903
904void qemu_put_be64(QEMUFile *f, uint64_t v)
905{
906 qemu_put_be32(f, v >> 32);
907 qemu_put_be32(f, v);
908}
909
910unsigned int qemu_get_be16(QEMUFile *f)
911{
912 unsigned int v;
913 v = qemu_get_byte(f) << 8;
914 v |= qemu_get_byte(f);
915 return v;
916}
917
918unsigned int qemu_get_be32(QEMUFile *f)
919{
920 unsigned int v;
921 v = qemu_get_byte(f) << 24;
922 v |= qemu_get_byte(f) << 16;
923 v |= qemu_get_byte(f) << 8;
924 v |= qemu_get_byte(f);
925 return v;
926}
927
928uint64_t qemu_get_be64(QEMUFile *f)
929{
930 uint64_t v;
931 v = (uint64_t)qemu_get_be32(f) << 32;
932 v |= qemu_get_be32(f);
933 return v;
934}
935
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200936
937/* timer */
938
939void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
940{
941 uint64_t expire_time;
942
943 expire_time = qemu_timer_expire_time_ns(ts);
944 qemu_put_be64(f, expire_time);
945}
946
947void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
948{
949 uint64_t expire_time;
950
951 expire_time = qemu_get_be64(f);
952 if (expire_time != -1) {
953 qemu_mod_timer_ns(ts, expire_time);
954 } else {
955 qemu_del_timer(ts);
956 }
957}
958
959
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100960/* bool */
961
962static int get_bool(QEMUFile *f, void *pv, size_t size)
963{
964 bool *v = pv;
965 *v = qemu_get_byte(f);
966 return 0;
967}
968
969static void put_bool(QEMUFile *f, void *pv, size_t size)
970{
971 bool *v = pv;
972 qemu_put_byte(f, *v);
973}
974
975const VMStateInfo vmstate_info_bool = {
976 .name = "bool",
977 .get = get_bool,
978 .put = put_bool,
979};
980
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200981/* 8 bit int */
982
983static int get_int8(QEMUFile *f, void *pv, size_t size)
984{
985 int8_t *v = pv;
986 qemu_get_s8s(f, v);
987 return 0;
988}
989
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200990static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200991{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200992 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200993 qemu_put_s8s(f, v);
994}
995
996const VMStateInfo vmstate_info_int8 = {
997 .name = "int8",
998 .get = get_int8,
999 .put = put_int8,
1000};
1001
1002/* 16 bit int */
1003
1004static int get_int16(QEMUFile *f, void *pv, size_t size)
1005{
1006 int16_t *v = pv;
1007 qemu_get_sbe16s(f, v);
1008 return 0;
1009}
1010
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001011static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001012{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001013 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001014 qemu_put_sbe16s(f, v);
1015}
1016
1017const VMStateInfo vmstate_info_int16 = {
1018 .name = "int16",
1019 .get = get_int16,
1020 .put = put_int16,
1021};
1022
1023/* 32 bit int */
1024
1025static int get_int32(QEMUFile *f, void *pv, size_t size)
1026{
1027 int32_t *v = pv;
1028 qemu_get_sbe32s(f, v);
1029 return 0;
1030}
1031
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001032static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001033{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001034 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001035 qemu_put_sbe32s(f, v);
1036}
1037
1038const VMStateInfo vmstate_info_int32 = {
1039 .name = "int32",
1040 .get = get_int32,
1041 .put = put_int32,
1042};
1043
Juan Quintela82501662009-08-20 19:42:32 +02001044/* 32 bit int. See that the received value is the same than the one
1045 in the field */
1046
1047static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
1048{
1049 int32_t *v = pv;
1050 int32_t v2;
1051 qemu_get_sbe32s(f, &v2);
1052
1053 if (*v == v2)
1054 return 0;
1055 return -EINVAL;
1056}
1057
1058const VMStateInfo vmstate_info_int32_equal = {
1059 .name = "int32 equal",
1060 .get = get_int32_equal,
1061 .put = put_int32,
1062};
1063
Juan Quintela0a031e02009-08-20 19:42:37 +02001064/* 32 bit int. See that the received value is the less or the same
1065 than the one in the field */
1066
1067static int get_int32_le(QEMUFile *f, void *pv, size_t size)
1068{
1069 int32_t *old = pv;
1070 int32_t new;
1071 qemu_get_sbe32s(f, &new);
1072
1073 if (*old <= new)
1074 return 0;
1075 return -EINVAL;
1076}
1077
1078const VMStateInfo vmstate_info_int32_le = {
1079 .name = "int32 equal",
1080 .get = get_int32_le,
1081 .put = put_int32,
1082};
1083
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001084/* 64 bit int */
1085
1086static int get_int64(QEMUFile *f, void *pv, size_t size)
1087{
1088 int64_t *v = pv;
1089 qemu_get_sbe64s(f, v);
1090 return 0;
1091}
1092
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001093static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001094{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001095 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001096 qemu_put_sbe64s(f, v);
1097}
1098
1099const VMStateInfo vmstate_info_int64 = {
1100 .name = "int64",
1101 .get = get_int64,
1102 .put = put_int64,
1103};
1104
1105/* 8 bit unsigned int */
1106
1107static int get_uint8(QEMUFile *f, void *pv, size_t size)
1108{
1109 uint8_t *v = pv;
1110 qemu_get_8s(f, v);
1111 return 0;
1112}
1113
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001114static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001115{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001116 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001117 qemu_put_8s(f, v);
1118}
1119
1120const VMStateInfo vmstate_info_uint8 = {
1121 .name = "uint8",
1122 .get = get_uint8,
1123 .put = put_uint8,
1124};
1125
1126/* 16 bit unsigned int */
1127
1128static int get_uint16(QEMUFile *f, void *pv, size_t size)
1129{
1130 uint16_t *v = pv;
1131 qemu_get_be16s(f, v);
1132 return 0;
1133}
1134
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001135static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001136{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001137 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001138 qemu_put_be16s(f, v);
1139}
1140
1141const VMStateInfo vmstate_info_uint16 = {
1142 .name = "uint16",
1143 .get = get_uint16,
1144 .put = put_uint16,
1145};
1146
1147/* 32 bit unsigned int */
1148
1149static int get_uint32(QEMUFile *f, void *pv, size_t size)
1150{
1151 uint32_t *v = pv;
1152 qemu_get_be32s(f, v);
1153 return 0;
1154}
1155
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001156static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001157{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001158 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001159 qemu_put_be32s(f, v);
1160}
1161
1162const VMStateInfo vmstate_info_uint32 = {
1163 .name = "uint32",
1164 .get = get_uint32,
1165 .put = put_uint32,
1166};
1167
Juan Quintela9122a8f2011-03-10 12:33:48 +01001168/* 32 bit uint. See that the received value is the same than the one
1169 in the field */
1170
1171static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1172{
1173 uint32_t *v = pv;
1174 uint32_t v2;
1175 qemu_get_be32s(f, &v2);
1176
1177 if (*v == v2) {
1178 return 0;
1179 }
1180 return -EINVAL;
1181}
1182
1183const VMStateInfo vmstate_info_uint32_equal = {
1184 .name = "uint32 equal",
1185 .get = get_uint32_equal,
1186 .put = put_uint32,
1187};
1188
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001189/* 64 bit unsigned int */
1190
1191static int get_uint64(QEMUFile *f, void *pv, size_t size)
1192{
1193 uint64_t *v = pv;
1194 qemu_get_be64s(f, v);
1195 return 0;
1196}
1197
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001198static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001199{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001200 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001201 qemu_put_be64s(f, v);
1202}
1203
1204const VMStateInfo vmstate_info_uint64 = {
1205 .name = "uint64",
1206 .get = get_uint64,
1207 .put = put_uint64,
1208};
1209
David Gibsone344b8a2013-03-12 14:06:00 +11001210/* 64 bit unsigned int. See that the received value is the same than the one
1211 in the field */
1212
1213static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1214{
1215 uint64_t *v = pv;
1216 uint64_t v2;
1217 qemu_get_be64s(f, &v2);
1218
1219 if (*v == v2) {
1220 return 0;
1221 }
1222 return -EINVAL;
1223}
1224
1225const VMStateInfo vmstate_info_uint64_equal = {
1226 .name = "int64 equal",
1227 .get = get_uint64_equal,
1228 .put = put_uint64,
1229};
1230
Juan Quintela80cd83e2009-09-10 03:04:36 +02001231/* 8 bit int. See that the received value is the same than the one
1232 in the field */
1233
1234static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1235{
1236 uint8_t *v = pv;
1237 uint8_t v2;
1238 qemu_get_8s(f, &v2);
1239
1240 if (*v == v2)
1241 return 0;
1242 return -EINVAL;
1243}
1244
1245const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001246 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001247 .get = get_uint8_equal,
1248 .put = put_uint8,
1249};
1250
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001251/* 16 bit unsigned int int. See that the received value is the same than the one
1252 in the field */
1253
1254static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1255{
1256 uint16_t *v = pv;
1257 uint16_t v2;
1258 qemu_get_be16s(f, &v2);
1259
1260 if (*v == v2)
1261 return 0;
1262 return -EINVAL;
1263}
1264
1265const VMStateInfo vmstate_info_uint16_equal = {
1266 .name = "uint16 equal",
1267 .get = get_uint16_equal,
1268 .put = put_uint16,
1269};
1270
David Gibson213945e2013-03-12 14:06:02 +11001271/* floating point */
1272
1273static int get_float64(QEMUFile *f, void *pv, size_t size)
1274{
1275 float64 *v = pv;
1276
1277 *v = make_float64(qemu_get_be64(f));
1278 return 0;
1279}
1280
1281static void put_float64(QEMUFile *f, void *pv, size_t size)
1282{
1283 uint64_t *v = pv;
1284
1285 qemu_put_be64(f, float64_val(*v));
1286}
1287
1288const VMStateInfo vmstate_info_float64 = {
1289 .name = "float64",
1290 .get = get_float64,
1291 .put = put_float64,
1292};
1293
Juan Quinteladde04632009-08-20 19:42:26 +02001294/* timers */
1295
1296static int get_timer(QEMUFile *f, void *pv, size_t size)
1297{
1298 QEMUTimer *v = pv;
1299 qemu_get_timer(f, v);
1300 return 0;
1301}
1302
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001303static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001304{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001305 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001306 qemu_put_timer(f, v);
1307}
1308
1309const VMStateInfo vmstate_info_timer = {
1310 .name = "timer",
1311 .get = get_timer,
1312 .put = put_timer,
1313};
1314
Juan Quintela6f67c502009-08-20 19:42:35 +02001315/* uint8_t buffers */
1316
1317static int get_buffer(QEMUFile *f, void *pv, size_t size)
1318{
1319 uint8_t *v = pv;
1320 qemu_get_buffer(f, v, size);
1321 return 0;
1322}
1323
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001324static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001325{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001326 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001327 qemu_put_buffer(f, v, size);
1328}
1329
1330const VMStateInfo vmstate_info_buffer = {
1331 .name = "buffer",
1332 .get = get_buffer,
1333 .put = put_buffer,
1334};
1335
Juan Quintela76507c72009-10-19 15:46:28 +02001336/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001337 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001338
1339static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1340{
Jan Kiszka21174c32009-12-02 12:36:35 +01001341 uint8_t buf[1024];
1342 int block_len;
1343
1344 while (size > 0) {
1345 block_len = MIN(sizeof(buf), size);
1346 size -= block_len;
1347 qemu_get_buffer(f, buf, block_len);
1348 }
1349 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001350}
1351
1352static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1353{
Jan Kiszka21174c32009-12-02 12:36:35 +01001354 static const uint8_t buf[1024];
1355 int block_len;
1356
1357 while (size > 0) {
1358 block_len = MIN(sizeof(buf), size);
1359 size -= block_len;
1360 qemu_put_buffer(f, buf, block_len);
1361 }
Juan Quintela76507c72009-10-19 15:46:28 +02001362}
1363
1364const VMStateInfo vmstate_info_unused_buffer = {
1365 .name = "unused_buffer",
1366 .get = get_unused_buffer,
1367 .put = put_unused_buffer,
1368};
1369
Peter Maydell08e99e22012-10-30 07:45:12 +00001370/* bitmaps (as defined by bitmap.h). Note that size here is the size
1371 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1372 * bit words with the bits in big endian order. The in-memory format
1373 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1374 */
1375/* This is the number of 64 bit words sent over the wire */
1376#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1377static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1378{
1379 unsigned long *bmp = pv;
1380 int i, idx = 0;
1381 for (i = 0; i < BITS_TO_U64S(size); i++) {
1382 uint64_t w = qemu_get_be64(f);
1383 bmp[idx++] = w;
1384 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1385 bmp[idx++] = w >> 32;
1386 }
1387 }
1388 return 0;
1389}
1390
1391static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1392{
1393 unsigned long *bmp = pv;
1394 int i, idx = 0;
1395 for (i = 0; i < BITS_TO_U64S(size); i++) {
1396 uint64_t w = bmp[idx++];
1397 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1398 w |= ((uint64_t)bmp[idx++]) << 32;
1399 }
1400 qemu_put_be64(f, w);
1401 }
1402}
1403
1404const VMStateInfo vmstate_info_bitmap = {
1405 .name = "bitmap",
1406 .get = get_bitmap,
1407 .put = put_bitmap,
1408};
1409
Alex Williamson7685ee62010-06-25 11:09:14 -06001410typedef struct CompatEntry {
1411 char idstr[256];
1412 int instance_id;
1413} CompatEntry;
1414
aliguoria672b462008-11-11 21:33:36 +00001415typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001416 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001417 char idstr[256];
1418 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001419 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001420 int version_id;
1421 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001422 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001423 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001424 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001425 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001426 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001427 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001428} SaveStateEntry;
1429
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001430
Blue Swirl72cf2d42009-09-12 07:36:22 +00001431static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1432 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001433static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001434
Juan Quintela8718e992009-09-01 02:12:31 +02001435static int calculate_new_instance_id(const char *idstr)
1436{
1437 SaveStateEntry *se;
1438 int instance_id = 0;
1439
Blue Swirl72cf2d42009-09-12 07:36:22 +00001440 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001441 if (strcmp(idstr, se->idstr) == 0
1442 && instance_id <= se->instance_id) {
1443 instance_id = se->instance_id + 1;
1444 }
1445 }
1446 return instance_id;
1447}
1448
Alex Williamson7685ee62010-06-25 11:09:14 -06001449static int calculate_compat_instance_id(const char *idstr)
1450{
1451 SaveStateEntry *se;
1452 int instance_id = 0;
1453
1454 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1455 if (!se->compat)
1456 continue;
1457
1458 if (strcmp(idstr, se->compat->idstr) == 0
1459 && instance_id <= se->compat->instance_id) {
1460 instance_id = se->compat->instance_id + 1;
1461 }
1462 }
1463 return instance_id;
1464}
1465
aliguoria672b462008-11-11 21:33:36 +00001466/* TODO: Individual devices generally have very little idea about the rest
1467 of the system, so instance_id should be removed/replaced.
1468 Meanwhile pass -1 as instance_id if you do not already have a clearly
1469 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001470int register_savevm_live(DeviceState *dev,
1471 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001472 int instance_id,
1473 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001474 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001475 void *opaque)
1476{
Juan Quintela8718e992009-09-01 02:12:31 +02001477 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001478
Anthony Liguori7267c092011-08-20 22:09:37 -05001479 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001480 se->version_id = version_id;
1481 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001482 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001483 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001484 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001485 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001486 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001487 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001488 se->is_ram = 1;
1489 }
aliguoria672b462008-11-11 21:33:36 +00001490
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001491 if (dev) {
1492 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001493 if (id) {
1494 pstrcpy(se->idstr, sizeof(se->idstr), id);
1495 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001496 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001497
Anthony Liguori7267c092011-08-20 22:09:37 -05001498 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001499 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1500 se->compat->instance_id = instance_id == -1 ?
1501 calculate_compat_instance_id(idstr) : instance_id;
1502 instance_id = -1;
1503 }
1504 }
1505 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1506
Juan Quintela8718e992009-09-01 02:12:31 +02001507 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001508 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001509 } else {
1510 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001511 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001512 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001513 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001514 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001515 return 0;
1516}
1517
Alex Williamson0be71e32010-06-25 11:09:07 -06001518int register_savevm(DeviceState *dev,
1519 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001520 int instance_id,
1521 int version_id,
1522 SaveStateHandler *save_state,
1523 LoadStateHandler *load_state,
1524 void *opaque)
1525{
Juan Quintela7908c782012-06-26 18:46:10 +02001526 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1527 ops->save_state = save_state;
1528 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001529 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001530 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001531}
1532
Alex Williamson0be71e32010-06-25 11:09:07 -06001533void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001534{
Juan Quintela8718e992009-09-01 02:12:31 +02001535 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001536 char id[256] = "";
1537
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001538 if (dev) {
1539 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001540 if (path) {
1541 pstrcpy(id, sizeof(id), path);
1542 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001543 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001544 }
1545 }
1546 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001547
Blue Swirl72cf2d42009-09-12 07:36:22 +00001548 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001549 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001550 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001551 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001552 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001553 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001554 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001555 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001556 }
aliguori41bd13a2009-04-17 17:10:59 +00001557 }
1558}
1559
Alex Williamson0be71e32010-06-25 11:09:07 -06001560int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001561 const VMStateDescription *vmsd,
1562 void *opaque, int alias_id,
1563 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001564{
Juan Quintela8718e992009-09-01 02:12:31 +02001565 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001566
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001567 /* If this triggers, alias support can be dropped for the vmsd. */
1568 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1569
Anthony Liguori7267c092011-08-20 22:09:37 -05001570 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001571 se->version_id = vmsd->version_id;
1572 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001573 se->opaque = opaque;
1574 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001575 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001576 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001577
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001578 if (dev) {
1579 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001580 if (id) {
1581 pstrcpy(se->idstr, sizeof(se->idstr), id);
1582 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001583 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001584
Anthony Liguori7267c092011-08-20 22:09:37 -05001585 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001586 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1587 se->compat->instance_id = instance_id == -1 ?
1588 calculate_compat_instance_id(vmsd->name) : instance_id;
1589 instance_id = -1;
1590 }
1591 }
1592 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1593
Juan Quintela8718e992009-09-01 02:12:31 +02001594 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001595 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001596 } else {
1597 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001598 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001599 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001600 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001601 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001602 return 0;
1603}
1604
Alex Williamson0be71e32010-06-25 11:09:07 -06001605void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1606 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001607{
Juan Quintela1eb75382009-09-10 03:04:29 +02001608 SaveStateEntry *se, *new_se;
1609
Blue Swirl72cf2d42009-09-12 07:36:22 +00001610 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001611 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001612 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001613 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001614 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001615 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001616 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001617 }
1618 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001619}
1620
Juan Quintela811814b2010-07-26 21:38:43 +02001621static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1622 void *opaque);
1623static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1624 void *opaque);
1625
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001626int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1627 void *opaque, int version_id)
1628{
1629 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001630 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001631
1632 if (version_id > vmsd->version_id) {
1633 return -EINVAL;
1634 }
1635 if (version_id < vmsd->minimum_version_id_old) {
1636 return -EINVAL;
1637 }
1638 if (version_id < vmsd->minimum_version_id) {
1639 return vmsd->load_state_old(f, opaque, version_id);
1640 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001641 if (vmsd->pre_load) {
1642 int ret = vmsd->pre_load(opaque);
1643 if (ret)
1644 return ret;
1645 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001646 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001647 if ((field->field_exists &&
1648 field->field_exists(opaque, version_id)) ||
1649 (!field->field_exists &&
1650 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001651 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001652 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001653 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001654
Juan Quintelae61a1e02009-12-02 12:36:38 +01001655 if (field->flags & VMS_VBUFFER) {
1656 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001657 if (field->flags & VMS_MULTIPLY) {
1658 size *= field->size;
1659 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001660 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001661 if (field->flags & VMS_ARRAY) {
1662 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001663 } else if (field->flags & VMS_VARRAY_INT32) {
1664 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001665 } else if (field->flags & VMS_VARRAY_UINT32) {
1666 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001667 } else if (field->flags & VMS_VARRAY_UINT16) {
1668 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001669 } else if (field->flags & VMS_VARRAY_UINT8) {
1670 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001671 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001672 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001673 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001674 }
1675 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001676 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001677
Juan Quintela19df4382009-09-29 22:48:41 +02001678 if (field->flags & VMS_ARRAY_OF_POINTER) {
1679 addr = *(void **)addr;
1680 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001681 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001682 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001683 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001684 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001685
1686 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001687 if (ret < 0) {
1688 return ret;
1689 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001690 }
1691 }
1692 field++;
1693 }
Juan Quintela811814b2010-07-26 21:38:43 +02001694 ret = vmstate_subsection_load(f, vmsd, opaque);
1695 if (ret != 0) {
1696 return ret;
1697 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001698 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001699 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001700 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001701 return 0;
1702}
1703
1704void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001705 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001706{
1707 VMStateField *field = vmsd->fields;
1708
Juan Quintela8fb07912009-09-10 03:04:32 +02001709 if (vmsd->pre_save) {
1710 vmsd->pre_save(opaque);
1711 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001712 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001713 if (!field->field_exists ||
1714 field->field_exists(opaque, vmsd->version_id)) {
1715 void *base_addr = opaque + field->offset;
1716 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001717 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001718
Juan Quintelae61a1e02009-12-02 12:36:38 +01001719 if (field->flags & VMS_VBUFFER) {
1720 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001721 if (field->flags & VMS_MULTIPLY) {
1722 size *= field->size;
1723 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001724 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001725 if (field->flags & VMS_ARRAY) {
1726 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001727 } else if (field->flags & VMS_VARRAY_INT32) {
1728 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001729 } else if (field->flags & VMS_VARRAY_UINT32) {
1730 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001731 } else if (field->flags & VMS_VARRAY_UINT16) {
1732 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001733 } else if (field->flags & VMS_VARRAY_UINT8) {
1734 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001735 }
1736 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001737 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001738 }
1739 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001740 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001741
Juan Quintela85953872009-12-02 12:36:37 +01001742 if (field->flags & VMS_ARRAY_OF_POINTER) {
1743 addr = *(void **)addr;
1744 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001745 if (field->flags & VMS_STRUCT) {
1746 vmstate_save_state(f, field->vmsd, addr);
1747 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001748 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001749 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001750 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001751 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001752 field++;
1753 }
Juan Quintela811814b2010-07-26 21:38:43 +02001754 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001755}
1756
Juan Quintela4082be42009-08-20 19:42:24 +02001757static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1758{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001759 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001760 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001761 }
1762 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001763}
1764
Alex Williamsondc912122011-01-11 14:39:43 -07001765static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001766{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001767 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001768 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001769 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001770 }
1771 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001772}
1773
aliguoria672b462008-11-11 21:33:36 +00001774#define QEMU_VM_FILE_MAGIC 0x5145564d
1775#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1776#define QEMU_VM_FILE_VERSION 0x00000003
1777
1778#define QEMU_VM_EOF 0x00
1779#define QEMU_VM_SECTION_START 0x01
1780#define QEMU_VM_SECTION_PART 0x02
1781#define QEMU_VM_SECTION_END 0x03
1782#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001783#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001784
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001785bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001786{
1787 SaveStateEntry *se;
1788
1789 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1790 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001791 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001792 return true;
1793 }
1794 }
1795 return false;
1796}
1797
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001798void qemu_savevm_state_begin(QEMUFile *f,
1799 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001800{
1801 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001802 int ret;
aliguoria672b462008-11-11 21:33:36 +00001803
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001804 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001805 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001806 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001807 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001808 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001809 }
1810
aliguoria672b462008-11-11 21:33:36 +00001811 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1812 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1813
Blue Swirl72cf2d42009-09-12 07:36:22 +00001814 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001815 int len;
1816
Juan Quintelad1315aa2012-06-28 15:11:57 +02001817 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001818 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001819 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001820 if (se->ops && se->ops->is_active) {
1821 if (!se->ops->is_active(se->opaque)) {
1822 continue;
1823 }
1824 }
aliguoria672b462008-11-11 21:33:36 +00001825 /* Section type */
1826 qemu_put_byte(f, QEMU_VM_SECTION_START);
1827 qemu_put_be32(f, se->section_id);
1828
1829 /* ID string */
1830 len = strlen(se->idstr);
1831 qemu_put_byte(f, len);
1832 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1833
1834 qemu_put_be32(f, se->instance_id);
1835 qemu_put_be32(f, se->version_id);
1836
Juan Quintelad1315aa2012-06-28 15:11:57 +02001837 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001838 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001839 qemu_file_set_error(f, ret);
1840 break;
Juan Quintela29757252011-10-19 15:22:18 +02001841 }
aliguoria672b462008-11-11 21:33:36 +00001842 }
aliguoria672b462008-11-11 21:33:36 +00001843}
1844
Juan Quintela39346382011-09-22 11:02:14 +02001845/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001846 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001847 * negative: there was one error, and we have -errno.
1848 * 0 : We haven't finished, caller have to go again
1849 * 1 : We have finished, we can go to complete phase
1850 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001851int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001852{
1853 SaveStateEntry *se;
1854 int ret = 1;
1855
Blue Swirl72cf2d42009-09-12 07:36:22 +00001856 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001857 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001858 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001859 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001860 if (se->ops && se->ops->is_active) {
1861 if (!se->ops->is_active(se->opaque)) {
1862 continue;
1863 }
1864 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001865 if (qemu_file_rate_limit(f)) {
1866 return 0;
1867 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001868 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001869 /* Section type */
1870 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1871 qemu_put_be32(f, se->section_id);
1872
Juan Quintela16310a32012-06-28 15:31:37 +02001873 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001874 trace_savevm_section_end(se->section_id);
1875
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001876 if (ret < 0) {
1877 qemu_file_set_error(f, ret);
1878 }
Juan Quintela29757252011-10-19 15:22:18 +02001879 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001880 /* Do not proceed to the next vmstate before this one reported
1881 completion of the current stage. This serializes the migration
1882 and reduces the probability that a faster changing state is
1883 synchronized over and over again. */
1884 break;
1885 }
aliguoria672b462008-11-11 21:33:36 +00001886 }
Juan Quintela39346382011-09-22 11:02:14 +02001887 return ret;
aliguoria672b462008-11-11 21:33:36 +00001888}
1889
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001890void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001891{
1892 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001893 int ret;
aliguoria672b462008-11-11 21:33:36 +00001894
Jan Kiszkaea375f92010-03-01 19:10:30 +01001895 cpu_synchronize_all_states();
1896
Blue Swirl72cf2d42009-09-12 07:36:22 +00001897 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001898 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001899 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001900 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001901 if (se->ops && se->ops->is_active) {
1902 if (!se->ops->is_active(se->opaque)) {
1903 continue;
1904 }
1905 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001906 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001907 /* Section type */
1908 qemu_put_byte(f, QEMU_VM_SECTION_END);
1909 qemu_put_be32(f, se->section_id);
1910
Juan Quintela16310a32012-06-28 15:31:37 +02001911 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001912 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001913 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001914 qemu_file_set_error(f, ret);
1915 return;
Juan Quintela29757252011-10-19 15:22:18 +02001916 }
aliguoria672b462008-11-11 21:33:36 +00001917 }
1918
Blue Swirl72cf2d42009-09-12 07:36:22 +00001919 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001920 int len;
1921
Juan Quintela22ea40f2012-06-26 17:19:10 +02001922 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001923 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001924 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001925 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001926 /* Section type */
1927 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1928 qemu_put_be32(f, se->section_id);
1929
1930 /* ID string */
1931 len = strlen(se->idstr);
1932 qemu_put_byte(f, len);
1933 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1934
1935 qemu_put_be32(f, se->instance_id);
1936 qemu_put_be32(f, se->version_id);
1937
Alex Williamsondc912122011-01-11 14:39:43 -07001938 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001939 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001940 }
1941
1942 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001943 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001944}
1945
Juan Quintelae4ed1542012-09-21 11:18:18 +02001946uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1947{
1948 SaveStateEntry *se;
1949 uint64_t ret = 0;
1950
1951 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1952 if (!se->ops || !se->ops->save_live_pending) {
1953 continue;
1954 }
1955 if (se->ops && se->ops->is_active) {
1956 if (!se->ops->is_active(se->opaque)) {
1957 continue;
1958 }
1959 }
1960 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1961 }
1962 return ret;
1963}
1964
Juan Quintela65227732013-01-14 14:14:42 +01001965void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001966{
1967 SaveStateEntry *se;
1968
1969 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001970 if (se->ops && se->ops->cancel) {
1971 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001972 }
1973 }
1974}
1975
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001976static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001977{
aliguoria672b462008-11-11 21:33:36 +00001978 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001979 MigrationParams params = {
1980 .blk = 0,
1981 .shared = 0
1982 };
aliguoria672b462008-11-11 21:33:36 +00001983
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001984 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001985 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07001986 }
1987
Paolo Bonzini9b095032013-02-22 17:36:28 +01001988 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001989 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001990 qemu_mutex_lock_iothread();
1991
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001992 while (qemu_file_get_error(f) == 0) {
1993 if (qemu_savevm_state_iterate(f) > 0) {
1994 break;
1995 }
1996 }
aliguoria672b462008-11-11 21:33:36 +00001997
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001998 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001999 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01002000 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002001 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02002002 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01002003 if (ret != 0) {
2004 qemu_savevm_state_cancel();
2005 }
aliguoria672b462008-11-11 21:33:36 +00002006 return ret;
2007}
2008
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002009static int qemu_save_device_state(QEMUFile *f)
2010{
2011 SaveStateEntry *se;
2012
2013 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
2014 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
2015
2016 cpu_synchronize_all_states();
2017
2018 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
2019 int len;
2020
2021 if (se->is_ram) {
2022 continue;
2023 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02002024 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002025 continue;
2026 }
2027
2028 /* Section type */
2029 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
2030 qemu_put_be32(f, se->section_id);
2031
2032 /* ID string */
2033 len = strlen(se->idstr);
2034 qemu_put_byte(f, len);
2035 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
2036
2037 qemu_put_be32(f, se->instance_id);
2038 qemu_put_be32(f, se->version_id);
2039
2040 vmstate_save(f, se);
2041 }
2042
2043 qemu_put_byte(f, QEMU_VM_EOF);
2044
2045 return qemu_file_get_error(f);
2046}
2047
aliguoria672b462008-11-11 21:33:36 +00002048static SaveStateEntry *find_se(const char *idstr, int instance_id)
2049{
2050 SaveStateEntry *se;
2051
Blue Swirl72cf2d42009-09-12 07:36:22 +00002052 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00002053 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02002054 (instance_id == se->instance_id ||
2055 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00002056 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06002057 /* Migrating from an older version? */
2058 if (strstr(se->idstr, idstr) && se->compat) {
2059 if (!strcmp(se->compat->idstr, idstr) &&
2060 (instance_id == se->compat->instance_id ||
2061 instance_id == se->alias_id))
2062 return se;
2063 }
aliguoria672b462008-11-11 21:33:36 +00002064 }
2065 return NULL;
2066}
2067
Juan Quintela811814b2010-07-26 21:38:43 +02002068static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
2069{
2070 while(sub && sub->needed) {
2071 if (strcmp(idstr, sub->vmsd->name) == 0) {
2072 return sub->vmsd;
2073 }
2074 sub++;
2075 }
2076 return NULL;
2077}
2078
2079static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
2080 void *opaque)
2081{
Juan Quintelac6380722011-10-04 15:28:31 +02002082 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02002083 char idstr[256];
2084 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02002085 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02002086 const VMStateDescription *sub_vmsd;
2087
Juan Quintelac6380722011-10-04 15:28:31 +02002088 len = qemu_peek_byte(f, 1);
2089 if (len < strlen(vmsd->name) + 1) {
2090 /* subsection name has be be "section_name/a" */
2091 return 0;
2092 }
2093 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
2094 if (size != len) {
2095 return 0;
2096 }
2097 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02002098
Juan Quintelac6380722011-10-04 15:28:31 +02002099 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2100 /* it don't have a valid subsection name */
2101 return 0;
2102 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02002103 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02002104 if (sub_vmsd == NULL) {
2105 return -ENOENT;
2106 }
Juan Quintelac6380722011-10-04 15:28:31 +02002107 qemu_file_skip(f, 1); /* subsection */
2108 qemu_file_skip(f, 1); /* len */
2109 qemu_file_skip(f, len); /* idstr */
2110 version_id = qemu_get_be32(f);
2111
Juan Quintela811814b2010-07-26 21:38:43 +02002112 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2113 if (ret) {
2114 return ret;
2115 }
2116 }
2117 return 0;
2118}
2119
2120static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2121 void *opaque)
2122{
2123 const VMStateSubsection *sub = vmsd->subsections;
2124
2125 while (sub && sub->needed) {
2126 if (sub->needed(opaque)) {
2127 const VMStateDescription *vmsd = sub->vmsd;
2128 uint8_t len;
2129
2130 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2131 len = strlen(vmsd->name);
2132 qemu_put_byte(f, len);
2133 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2134 qemu_put_be32(f, vmsd->version_id);
2135 vmstate_save_state(f, vmsd, opaque);
2136 }
2137 sub++;
2138 }
2139}
2140
aliguoria672b462008-11-11 21:33:36 +00002141typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002142 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002143 SaveStateEntry *se;
2144 int section_id;
2145 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002146} LoadStateEntry;
2147
aliguoria672b462008-11-11 21:33:36 +00002148int qemu_loadvm_state(QEMUFile *f)
2149{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002150 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2151 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002152 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002153 uint8_t section_type;
2154 unsigned int v;
2155 int ret;
2156
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002157 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002158 return -EINVAL;
2159 }
2160
aliguoria672b462008-11-11 21:33:36 +00002161 v = qemu_get_be32(f);
2162 if (v != QEMU_VM_FILE_MAGIC)
2163 return -EINVAL;
2164
2165 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002166 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2167 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2168 return -ENOTSUP;
2169 }
aliguoria672b462008-11-11 21:33:36 +00002170 if (v != QEMU_VM_FILE_VERSION)
2171 return -ENOTSUP;
2172
2173 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2174 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002175 SaveStateEntry *se;
2176 char idstr[257];
2177 int len;
2178
2179 switch (section_type) {
2180 case QEMU_VM_SECTION_START:
2181 case QEMU_VM_SECTION_FULL:
2182 /* Read section start */
2183 section_id = qemu_get_be32(f);
2184 len = qemu_get_byte(f);
2185 qemu_get_buffer(f, (uint8_t *)idstr, len);
2186 idstr[len] = 0;
2187 instance_id = qemu_get_be32(f);
2188 version_id = qemu_get_be32(f);
2189
2190 /* Find savevm section */
2191 se = find_se(idstr, instance_id);
2192 if (se == NULL) {
2193 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2194 ret = -EINVAL;
2195 goto out;
2196 }
2197
2198 /* Validate version */
2199 if (version_id > se->version_id) {
2200 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2201 version_id, idstr, se->version_id);
2202 ret = -EINVAL;
2203 goto out;
2204 }
2205
2206 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002207 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002208
2209 le->se = se;
2210 le->section_id = section_id;
2211 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002212 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002213
Juan Quintela4082be42009-08-20 19:42:24 +02002214 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002215 if (ret < 0) {
2216 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2217 instance_id, idstr);
2218 goto out;
2219 }
aliguoria672b462008-11-11 21:33:36 +00002220 break;
2221 case QEMU_VM_SECTION_PART:
2222 case QEMU_VM_SECTION_END:
2223 section_id = qemu_get_be32(f);
2224
Blue Swirl72cf2d42009-09-12 07:36:22 +00002225 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002226 if (le->section_id == section_id) {
2227 break;
2228 }
2229 }
aliguoria672b462008-11-11 21:33:36 +00002230 if (le == NULL) {
2231 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2232 ret = -EINVAL;
2233 goto out;
2234 }
2235
Juan Quintela4082be42009-08-20 19:42:24 +02002236 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002237 if (ret < 0) {
2238 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2239 section_id);
2240 goto out;
2241 }
aliguoria672b462008-11-11 21:33:36 +00002242 break;
2243 default:
2244 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2245 ret = -EINVAL;
2246 goto out;
2247 }
2248 }
2249
Jan Kiszkaea375f92010-03-01 19:10:30 +01002250 cpu_synchronize_all_post_init();
2251
aliguoria672b462008-11-11 21:33:36 +00002252 ret = 0;
2253
2254out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002255 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2256 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002257 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002258 }
2259
Juan Quintela42802d42011-10-05 01:14:46 +02002260 if (ret == 0) {
2261 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002262 }
aliguoria672b462008-11-11 21:33:36 +00002263
2264 return ret;
2265}
2266
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002267static BlockDriverState *find_vmstate_bs(void)
2268{
2269 BlockDriverState *bs = NULL;
2270 while ((bs = bdrv_next(bs))) {
2271 if (bdrv_can_snapshot(bs)) {
2272 return bs;
2273 }
2274 }
2275 return NULL;
2276}
2277
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002278/*
2279 * Deletes snapshots of a given name in all opened images.
2280 */
2281static int del_existing_snapshots(Monitor *mon, const char *name)
2282{
2283 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002284 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2285 int ret;
2286
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002287 bs = NULL;
2288 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002289 if (bdrv_can_snapshot(bs) &&
2290 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2291 {
2292 ret = bdrv_snapshot_delete(bs, name);
2293 if (ret < 0) {
2294 monitor_printf(mon,
2295 "Error while deleting snapshot on '%s'\n",
2296 bdrv_get_device_name(bs));
2297 return -1;
2298 }
2299 }
2300 }
2301
2302 return 0;
2303}
2304
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002305void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002306{
2307 BlockDriverState *bs, *bs1;
2308 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002309 int ret;
aliguoria672b462008-11-11 21:33:36 +00002310 QEMUFile *f;
2311 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002312 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002313 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002314 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002315 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002316
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002317 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002318 bs = NULL;
2319 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002320
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002321 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002322 continue;
2323 }
2324
2325 if (!bdrv_can_snapshot(bs)) {
2326 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2327 bdrv_get_device_name(bs));
2328 return;
2329 }
2330 }
2331
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002332 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002333 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002334 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002335 return;
2336 }
aliguoria672b462008-11-11 21:33:36 +00002337
Luiz Capitulino13548692011-07-29 15:36:43 -03002338 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002339 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002340
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002341 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002342
2343 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002344 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002345 sn->date_sec = tv.tv_sec;
2346 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002347 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002348
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002349 if (name) {
2350 ret = bdrv_snapshot_find(bs, old_sn, name);
2351 if (ret >= 0) {
2352 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2353 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2354 } else {
2355 pstrcpy(sn->name, sizeof(sn->name), name);
2356 }
2357 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002358 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2359 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002360 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002361 }
2362
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002363 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002364 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002365 goto the_end;
2366 }
2367
aliguoria672b462008-11-11 21:33:36 +00002368 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002369 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002370 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002371 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002372 goto the_end;
2373 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002374 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002375 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002376 qemu_fclose(f);
2377 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002378 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002379 goto the_end;
2380 }
2381
2382 /* create the snapshots */
2383
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002384 bs1 = NULL;
2385 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002386 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002387 /* Write VM state size only to the image that contains the state */
2388 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002389 ret = bdrv_snapshot_create(bs1, sn);
2390 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002391 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2392 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002393 }
2394 }
2395 }
2396
2397 the_end:
2398 if (saved_vm_running)
2399 vm_start();
2400}
2401
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002402void qmp_xen_save_devices_state(const char *filename, Error **errp)
2403{
2404 QEMUFile *f;
2405 int saved_vm_running;
2406 int ret;
2407
2408 saved_vm_running = runstate_is_running();
2409 vm_stop(RUN_STATE_SAVE_VM);
2410
2411 f = qemu_fopen(filename, "wb");
2412 if (!f) {
Luiz Capitulino1befce92013-06-07 14:36:58 -04002413 error_setg_file_open(errp, errno, filename);
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002414 goto the_end;
2415 }
2416 ret = qemu_save_device_state(f);
2417 qemu_fclose(f);
2418 if (ret < 0) {
2419 error_set(errp, QERR_IO_ERROR);
2420 }
2421
2422 the_end:
2423 if (saved_vm_running)
2424 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002425}
2426
Markus Armbruster03cd4652010-02-17 16:24:10 +01002427int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002428{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002429 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002430 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002431 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002432 int ret;
aliguoria672b462008-11-11 21:33:36 +00002433
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002434 bs_vm_state = find_vmstate_bs();
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002435 if (!bs_vm_state) {
2436 error_report("No block device supports snapshots");
2437 return -ENOTSUP;
2438 }
2439
2440 /* Don't even try to load empty VM states */
2441 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2442 if (ret < 0) {
2443 return ret;
2444 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002445 error_report("This is a disk-only snapshot. Revert to it offline "
2446 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002447 return -EINVAL;
2448 }
2449
2450 /* Verify if there is any device that doesn't support snapshots and is
2451 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002452 bs = NULL;
2453 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002454
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002455 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002456 continue;
2457 }
2458
2459 if (!bdrv_can_snapshot(bs)) {
2460 error_report("Device '%s' is writable but does not support snapshots.",
2461 bdrv_get_device_name(bs));
2462 return -ENOTSUP;
2463 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002464
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002465 ret = bdrv_snapshot_find(bs, &sn, name);
2466 if (ret < 0) {
2467 error_report("Device '%s' does not have the requested snapshot '%s'",
2468 bdrv_get_device_name(bs), name);
2469 return ret;
2470 }
aliguoria672b462008-11-11 21:33:36 +00002471 }
2472
2473 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002474 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002475
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002476 bs = NULL;
2477 while ((bs = bdrv_next(bs))) {
2478 if (bdrv_can_snapshot(bs)) {
2479 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002480 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002481 error_report("Error %d while activating snapshot '%s' on '%s'",
2482 ret, name, bdrv_get_device_name(bs));
2483 return ret;
aliguoria672b462008-11-11 21:33:36 +00002484 }
2485 }
2486 }
2487
aliguoria672b462008-11-11 21:33:36 +00002488 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002489 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002490 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002491 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002492 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002493 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002494
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002495 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002496 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002497
aliguoria672b462008-11-11 21:33:36 +00002498 qemu_fclose(f);
2499 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002500 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002501 return ret;
aliguoria672b462008-11-11 21:33:36 +00002502 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002503
Juan Quintela05f24012009-08-20 19:42:22 +02002504 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002505}
2506
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002507void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002508{
2509 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002510 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002511 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002512
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002513 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002514 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002515 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002516 return;
2517 }
2518
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002519 bs1 = NULL;
2520 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002521 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002522 ret = bdrv_snapshot_delete(bs1, name);
2523 if (ret < 0) {
2524 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002525 monitor_printf(mon,
2526 "Snapshots not supported on device '%s'\n",
2527 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002528 else
aliguori376253e2009-03-05 23:01:23 +00002529 monitor_printf(mon, "Error %d while deleting snapshot on "
2530 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002531 }
2532 }
2533 }
2534}
2535
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002536void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002537{
2538 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002539 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2540 int nb_sns, i, ret, available;
2541 int total;
2542 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002543
Stefan Hajnoczi29d78272013-05-25 11:09:42 +08002544 bs = find_vmstate_bs();
aliguoria672b462008-11-11 21:33:36 +00002545 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002546 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002547 return;
2548 }
aliguoria672b462008-11-11 21:33:36 +00002549
2550 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2551 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002552 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002553 return;
2554 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002555
2556 if (nb_sns == 0) {
2557 monitor_printf(mon, "There is no snapshot available.\n");
2558 return;
aliguoria672b462008-11-11 21:33:36 +00002559 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002560
Anthony Liguori7267c092011-08-20 22:09:37 -05002561 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002562 total = 0;
2563 for (i = 0; i < nb_sns; i++) {
2564 sn = &sn_tab[i];
2565 available = 1;
2566 bs1 = NULL;
2567
2568 while ((bs1 = bdrv_next(bs1))) {
2569 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2570 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2571 if (ret < 0) {
2572 available = 0;
2573 break;
2574 }
2575 }
2576 }
2577
2578 if (available) {
2579 available_snapshots[total] = i;
2580 total++;
2581 }
2582 }
2583
2584 if (total > 0) {
Wenchao Xia5b917042013-05-25 11:09:45 +08002585 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, NULL);
2586 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002587 for (i = 0; i < total; i++) {
2588 sn = &sn_tab[available_snapshots[i]];
Wenchao Xia5b917042013-05-25 11:09:45 +08002589 bdrv_snapshot_dump((fprintf_function)monitor_printf, mon, sn);
2590 monitor_printf(mon, "\n");
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002591 }
2592 } else {
2593 monitor_printf(mon, "There is no suitable snapshot available\n");
2594 }
2595
Anthony Liguori7267c092011-08-20 22:09:37 -05002596 g_free(sn_tab);
2597 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002598
aliguoria672b462008-11-11 21:33:36 +00002599}
Avi Kivityc5705a72011-12-20 15:59:12 +02002600
2601void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2602{
Avi Kivity1ddde082012-01-08 13:18:19 +02002603 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002604 memory_region_name(mr), dev);
2605}
2606
2607void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2608{
2609 /* Nothing do to while the implementation is in RAMBlock */
2610}
2611
2612void vmstate_register_ram_global(MemoryRegion *mr)
2613{
2614 vmstate_register_ram(mr, NULL);
2615}