blob: 5ab256c97df1448e21965c6c0e583a92113295cc [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
aliguoria672b462008-11-11 21:33:36 +000024
blueswir1d40cdb12009-03-07 16:52:02 +000025#include "config-host.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include "qemu-common.h"
27#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060028#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/net.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/timer.h"
blueswir1511d2b12009-03-07 15:32:56 +000033#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010034#include "migration/migration.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
36#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000039#include "qmp-commands.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020040#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/bitops.h"
Orit Wasserman28085f72013-03-22 16:47:58 +020042#include "qemu/iov.h"
blueswir1511d2b12009-03-07 15:32:56 +000043
aliguoria672b462008-11-11 21:33:36 +000044#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000045
Nolan18995b92009-10-15 16:53:55 -070046#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040047#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070048#endif
49#define ARP_HTYPE_ETH 0x0001
50#define ARP_PTYPE_IP 0x0800
51#define ARP_OP_REQUEST_REV 0x3
52
53static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000054 uint8_t *mac_addr)
55{
Nolan18995b92009-10-15 16:53:55 -070056 /* Ethernet header. */
57 memset(buf, 0xff, 6); /* destination MAC addr */
58 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
59 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000060
Nolan18995b92009-10-15 16:53:55 -070061 /* RARP header. */
62 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
63 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
64 *(buf + 18) = 6; /* hardware addr length (ethernet) */
65 *(buf + 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
67 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
68 memset(buf + 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
70 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000071
Nolan18995b92009-10-15 16:53:55 -070072 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000074
Nolan18995b92009-10-15 16:53:55 -070075 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000076}
77
Mark McLoughlinf401ca22009-11-25 18:49:32 +000078static void qemu_announce_self_iter(NICState *nic, void *opaque)
79{
80 uint8_t buf[60];
81 int len;
82
83 len = announce_self_create(buf, nic->conf->macaddr.a);
84
Jason Wangb356f762013-01-30 19:12:22 +080085 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000086}
87
88
Gleb Natapoved8b3302009-05-21 17:17:44 +030089static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000090{
Gleb Natapoved8b3302009-05-21 17:17:44 +030091 static int count = SELF_ANNOUNCE_ROUNDS;
92 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000093
Mark McLoughlinf401ca22009-11-25 18:49:32 +000094 qemu_foreach_nic(qemu_announce_self_iter, NULL);
95
Nolan18995b92009-10-15 16:53:55 -070096 if (--count) {
97 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +010098 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -070099 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300100 } else {
101 qemu_del_timer(timer);
102 qemu_free_timer(timer);
103 }
104}
105
106void qemu_announce_self(void)
107{
108 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100109 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300110 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000111}
112
113/***********************************************************/
114/* savevm/loadvm support */
115
116#define IO_BUF_SIZE 32768
117
118struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200119 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000120 void *opaque;
121 int is_write;
122
Paolo Bonzini1964a392013-02-22 17:36:45 +0100123 int64_t bytes_xfer;
124 int64_t xfer_limit;
125
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100126 int64_t pos; /* start of buffer when writing, end of buffer
127 when reading */
aliguoria672b462008-11-11 21:33:36 +0000128 int buf_index;
129 int buf_size; /* 0 when writing */
130 uint8_t buf[IO_BUF_SIZE];
131
Juan Quintela3961b4d2011-10-05 01:05:21 +0200132 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000133};
134
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200135typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000136{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200137 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000138 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200139} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000140
141typedef struct QEMUFileSocket
142{
143 int fd;
144 QEMUFile *file;
145} QEMUFileSocket;
146
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100147typedef struct {
148 Coroutine *co;
149 int fd;
150} FDYieldUntilData;
151
152static void fd_coroutine_enter(void *opaque)
153{
154 FDYieldUntilData *data = opaque;
155 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
156 qemu_coroutine_enter(data->co, NULL);
157}
158
159/**
160 * Yield until a file descriptor becomes readable
161 *
162 * Note that this function clobbers the handlers for the file descriptor.
163 */
164static void coroutine_fn yield_until_fd_readable(int fd)
165{
166 FDYieldUntilData data;
167
168 assert(qemu_in_coroutine());
169 data.co = qemu_coroutine_self();
170 data.fd = fd;
171 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
172 qemu_coroutine_yield();
173}
174
Orit Wasserman28085f72013-03-22 16:47:58 +0200175static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt)
176{
177 QEMUFileSocket *s = opaque;
178 ssize_t len;
179 ssize_t size = iov_size(iov, iovcnt);
180
181 len = iov_send(s->fd, iov, iovcnt, 0, size);
182 if (len < size) {
183 len = -socket_error();
184 }
185 return len;
186}
187
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200188static int socket_get_fd(void *opaque)
189{
190 QEMUFileSocket *s = opaque;
191
192 return s->fd;
193}
194
aliguoria672b462008-11-11 21:33:36 +0000195static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
196{
197 QEMUFileSocket *s = opaque;
198 ssize_t len;
199
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200200 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000201 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200202 if (len != -1) {
203 break;
204 }
205 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100206 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200207 } else if (socket_error() != EINTR) {
208 break;
209 }
210 }
aliguoria672b462008-11-11 21:33:36 +0000211
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200212 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000213 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200214 }
aliguoria672b462008-11-11 21:33:36 +0000215 return len;
216}
217
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100218static int socket_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
219{
220 QEMUFileSocket *s = opaque;
221 ssize_t len;
222
223 len = qemu_send_full(s->fd, buf, size, 0);
224 if (len < size) {
225 len = -socket_error();
226 }
227 return len;
228}
229
aliguoria672b462008-11-11 21:33:36 +0000230static int socket_close(void *opaque)
231{
232 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200233 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500234 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000235 return 0;
236}
237
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200238static int stdio_get_fd(void *opaque)
239{
240 QEMUFileStdio *s = opaque;
241
242 return fileno(s->stdio_file);
243}
244
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200245static int stdio_put_buffer(void *opaque, const 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 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000249}
250
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200251static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000252{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200253 QEMUFileStdio *s = opaque;
254 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300255 int bytes;
256
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200257 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300258 clearerr(fp);
259 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200260 if (bytes != 0 || !ferror(fp)) {
261 break;
262 }
263 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100264 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200265 } else if (errno != EINTR) {
266 break;
267 }
268 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300269 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000270}
271
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200272static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000273{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200274 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500275 int ret;
276 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200277 if (ret == -1) {
278 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100279 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
280 /* close succeeded, but non-zero exit code: */
281 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200282 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500283 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500284 return ret;
aliguoria672b462008-11-11 21:33:36 +0000285}
286
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200287static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000288{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200289 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200290 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100291
292 if (s->file->ops->put_buffer) {
293 int fd = fileno(s->stdio_file);
294 struct stat st;
295
296 ret = fstat(fd, &st);
297 if (ret == 0 && S_ISREG(st.st_mode)) {
298 /*
299 * If the file handle is a regular file make sure the
300 * data is flushed to disk before signaling success.
301 */
302 ret = fsync(fd);
303 if (ret != 0) {
304 ret = -errno;
305 return ret;
306 }
307 }
308 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200309 if (fclose(s->stdio_file) == EOF) {
310 ret = -errno;
311 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500312 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200313 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200314}
aliguoria672b462008-11-11 21:33:36 +0000315
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200316static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200317 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200318 .get_buffer = stdio_get_buffer,
319 .close = stdio_pclose
320};
321
322static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200323 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200324 .put_buffer = stdio_put_buffer,
325 .close = stdio_pclose
326};
327
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100328QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200329{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100330 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200331 QEMUFileStdio *s;
332
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100333 stdio_file = popen(command, mode);
334 if (stdio_file == NULL) {
335 return NULL;
336 }
337
338 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000339 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
340 return NULL;
341 }
342
Anthony Liguori7267c092011-08-20 22:09:37 -0500343 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000344
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200345 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000346
347 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200348 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000349 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200350 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000351 }
aliguoria672b462008-11-11 21:33:36 +0000352 return s->file;
353}
354
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200355static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200356 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200357 .get_buffer = stdio_get_buffer,
358 .close = stdio_fclose
359};
360
361static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200362 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200363 .put_buffer = stdio_put_buffer,
364 .close = stdio_fclose
365};
366
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200367QEMUFile *qemu_fdopen(int fd, const char *mode)
368{
369 QEMUFileStdio *s;
370
371 if (mode == NULL ||
372 (mode[0] != 'r' && mode[0] != 'w') ||
373 mode[1] != 'b' || mode[2] != 0) {
374 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
375 return NULL;
376 }
377
Anthony Liguori7267c092011-08-20 22:09:37 -0500378 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200379 s->stdio_file = fdopen(fd, mode);
380 if (!s->stdio_file)
381 goto fail;
382
383 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200384 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200385 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200386 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200387 }
388 return s->file;
389
390fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500391 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200392 return NULL;
393}
394
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200395static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200396 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200397 .get_buffer = socket_get_buffer,
398 .close = socket_close
399};
400
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100401static const QEMUFileOps socket_write_ops = {
402 .get_fd = socket_get_fd,
403 .put_buffer = socket_put_buffer,
Orit Wasserman28085f72013-03-22 16:47:58 +0200404 .writev_buffer = socket_writev_buffer,
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100405 .close = socket_close
406};
407
408QEMUFile *qemu_fopen_socket(int fd, const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000409{
Anthony Liguori7267c092011-08-20 22:09:37 -0500410 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000411
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100412 if (mode == NULL ||
413 (mode[0] != 'r' && mode[0] != 'w') ||
414 mode[1] != 'b' || mode[2] != 0) {
415 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
416 return NULL;
417 }
418
aliguoria672b462008-11-11 21:33:36 +0000419 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100420 if (mode[0] == 'w') {
Paolo Bonzinif8bbc122013-02-22 17:36:41 +0100421 socket_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100422 s->file = qemu_fopen_ops(s, &socket_write_ops);
423 } else {
424 s->file = qemu_fopen_ops(s, &socket_read_ops);
425 }
aliguoria672b462008-11-11 21:33:36 +0000426 return s->file;
427}
428
aliguoria672b462008-11-11 21:33:36 +0000429QEMUFile *qemu_fopen(const char *filename, const char *mode)
430{
431 QEMUFileStdio *s;
432
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200433 if (mode == NULL ||
434 (mode[0] != 'r' && mode[0] != 'w') ||
435 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000436 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200437 return NULL;
438 }
439
Anthony Liguori7267c092011-08-20 22:09:37 -0500440 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000441
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200442 s->stdio_file = fopen(filename, mode);
443 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000444 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200445
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200446 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200447 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200448 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200449 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200450 }
451 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000452fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500453 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000454 return NULL;
455}
456
aliguori178e08a2009-04-05 19:10:55 +0000457static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000458 int64_t pos, int size)
459{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200460 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000461 return size;
462}
463
aliguori178e08a2009-04-05 19:10:55 +0000464static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000465{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200466 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000467}
468
469static int bdrv_fclose(void *opaque)
470{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200471 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000472}
473
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200474static const QEMUFileOps bdrv_read_ops = {
475 .get_buffer = block_get_buffer,
476 .close = bdrv_fclose
477};
478
479static const QEMUFileOps bdrv_write_ops = {
480 .put_buffer = block_put_buffer,
481 .close = bdrv_fclose
482};
483
Christoph Hellwig45566e92009-07-10 23:11:57 +0200484static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000485{
aliguoria672b462008-11-11 21:33:36 +0000486 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200487 return qemu_fopen_ops(bs, &bdrv_write_ops);
488 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000489}
490
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200491QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000492{
493 QEMUFile *f;
494
Anthony Liguori7267c092011-08-20 22:09:37 -0500495 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000496
497 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200498 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000499 f->is_write = 0;
aliguoria672b462008-11-11 21:33:36 +0000500 return f;
501}
502
Juan Quintela624b9cc2011-10-05 01:02:52 +0200503int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000504{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200505 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000506}
507
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100508static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000509{
Juan Quintelaafe41932013-01-14 13:36:28 +0100510 if (f->last_error == 0) {
511 f->last_error = ret;
512 }
aliguori4dabe242009-04-05 19:30:51 +0000513}
514
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200515/** Flushes QEMUFile buffer
516 *
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200517 */
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100518static void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000519{
Juan Quintela7311bea2012-08-29 19:08:59 +0200520 int ret = 0;
521
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100522 if (!f->ops->put_buffer) {
523 return;
524 }
aliguoria672b462008-11-11 21:33:36 +0000525 if (f->is_write && f->buf_index > 0) {
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100526 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Juan Quintela7311bea2012-08-29 19:08:59 +0200527 if (ret >= 0) {
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100528 f->pos += f->buf_index;
Juan Quintela7311bea2012-08-29 19:08:59 +0200529 }
aliguoria672b462008-11-11 21:33:36 +0000530 f->buf_index = 0;
531 }
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100532 if (ret < 0) {
533 qemu_file_set_error(f, ret);
534 }
aliguoria672b462008-11-11 21:33:36 +0000535}
536
537static void qemu_fill_buffer(QEMUFile *f)
538{
539 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200540 int pending;
aliguoria672b462008-11-11 21:33:36 +0000541
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200542 if (!f->ops->get_buffer)
aliguoria672b462008-11-11 21:33:36 +0000543 return;
544
545 if (f->is_write)
546 abort();
547
Juan Quintela0046c452011-09-30 19:28:45 +0200548 pending = f->buf_size - f->buf_index;
549 if (pending > 0) {
550 memmove(f->buf, f->buf + f->buf_index, pending);
551 }
552 f->buf_index = 0;
553 f->buf_size = pending;
554
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100555 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200556 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000557 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200558 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100559 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200560 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200561 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000562 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200563 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000564}
565
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200566int qemu_get_fd(QEMUFile *f)
567{
568 if (f->ops->get_fd) {
569 return f->ops->get_fd(f->opaque);
570 }
571 return -1;
572}
573
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200574/** Closes the file
575 *
576 * Returns negative error value if any error happened on previous operations or
577 * while closing the file. Returns 0 or positive number on success.
578 *
579 * The meaning of return value on success depends on the specific backend
580 * being used.
581 */
582int qemu_fclose(QEMUFile *f)
583{
Juan Quintela29eee862012-08-29 19:14:54 +0200584 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100585 qemu_fflush(f);
586 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200587
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200588 if (f->ops->close) {
589 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200590 if (ret >= 0) {
591 ret = ret2;
592 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200593 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200594 /* If any error was spotted before closing, we should report it
595 * instead of the close() return value.
596 */
597 if (f->last_error) {
598 ret = f->last_error;
599 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500600 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000601 return ret;
602}
603
aliguoria672b462008-11-11 21:33:36 +0000604void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
605{
606 int l;
607
Juan Quintelac10682c2012-08-29 19:43:39 +0200608 if (f->last_error) {
609 return;
610 }
611
612 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000613 fprintf(stderr,
614 "Attempted to write to buffer while read buffer is not empty\n");
615 abort();
616 }
617
Juan Quintelac10682c2012-08-29 19:43:39 +0200618 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000619 l = IO_BUF_SIZE - f->buf_index;
620 if (l > size)
621 l = size;
622 memcpy(f->buf + f->buf_index, buf, l);
623 f->is_write = 1;
624 f->buf_index += l;
Paolo Bonzini1964a392013-02-22 17:36:45 +0100625 f->bytes_xfer += l;
aliguoria672b462008-11-11 21:33:36 +0000626 buf += l;
627 size -= l;
Juan Quintela7311bea2012-08-29 19:08:59 +0200628 if (f->buf_index >= IO_BUF_SIZE) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100629 qemu_fflush(f);
630 if (qemu_file_get_error(f)) {
Juan Quintelac10682c2012-08-29 19:43:39 +0200631 break;
632 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200633 }
aliguoria672b462008-11-11 21:33:36 +0000634 }
635}
636
637void qemu_put_byte(QEMUFile *f, int v)
638{
Juan Quintelac10682c2012-08-29 19:43:39 +0200639 if (f->last_error) {
640 return;
641 }
642
643 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000644 fprintf(stderr,
645 "Attempted to write to buffer while read buffer is not empty\n");
646 abort();
647 }
648
649 f->buf[f->buf_index++] = v;
650 f->is_write = 1;
Orit Wasserman7d8a30b2013-03-22 16:47:59 +0200651 f->bytes_xfer++;
652
Juan Quintela7311bea2012-08-29 19:08:59 +0200653 if (f->buf_index >= IO_BUF_SIZE) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100654 qemu_fflush(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200655 }
aliguoria672b462008-11-11 21:33:36 +0000656}
657
Juan Quintelac6380722011-10-04 15:28:31 +0200658static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000659{
Juan Quintelac6380722011-10-04 15:28:31 +0200660 if (f->buf_index + size <= f->buf_size) {
661 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000662 }
aliguoria672b462008-11-11 21:33:36 +0000663}
664
Juan Quintelac6380722011-10-04 15:28:31 +0200665static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200666{
Juan Quintelac6380722011-10-04 15:28:31 +0200667 int pending;
668 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200669
Juan Quintelab9ce1452011-10-04 13:55:32 +0200670 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200671 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200672 }
Juan Quintela811814b2010-07-26 21:38:43 +0200673
Juan Quintelac6380722011-10-04 15:28:31 +0200674 index = f->buf_index + offset;
675 pending = f->buf_size - index;
676 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200677 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200678 index = f->buf_index + offset;
679 pending = f->buf_size - index;
680 }
681
682 if (pending <= 0) {
683 return 0;
684 }
685 if (size > pending) {
686 size = pending;
687 }
688
689 memcpy(buf, f->buf + index, size);
690 return size;
691}
692
693int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
694{
695 int pending = size;
696 int done = 0;
697
698 while (pending > 0) {
699 int res;
700
701 res = qemu_peek_buffer(f, buf, pending, 0);
702 if (res == 0) {
703 return done;
704 }
705 qemu_file_skip(f, res);
706 buf += res;
707 pending -= res;
708 done += res;
709 }
710 return done;
711}
712
713static int qemu_peek_byte(QEMUFile *f, int offset)
714{
715 int index = f->buf_index + offset;
716
717 if (f->is_write) {
718 abort();
719 }
720
721 if (index >= f->buf_size) {
722 qemu_fill_buffer(f);
723 index = f->buf_index + offset;
724 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200725 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200726 }
Juan Quintela811814b2010-07-26 21:38:43 +0200727 }
Juan Quintelac6380722011-10-04 15:28:31 +0200728 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200729}
730
aliguoria672b462008-11-11 21:33:36 +0000731int qemu_get_byte(QEMUFile *f)
732{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200733 int result;
aliguoria672b462008-11-11 21:33:36 +0000734
Juan Quintelac6380722011-10-04 15:28:31 +0200735 result = qemu_peek_byte(f, 0);
736 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200737 return result;
aliguoria672b462008-11-11 21:33:36 +0000738}
739
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100740int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000741{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100742 qemu_fflush(f);
743 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000744}
745
aliguoria672b462008-11-11 21:33:36 +0000746int qemu_file_rate_limit(QEMUFile *f)
747{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100748 if (qemu_file_get_error(f)) {
749 return 1;
750 }
751 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
752 return 1;
753 }
aliguoria672b462008-11-11 21:33:36 +0000754 return 0;
755}
756
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200757int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200758{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100759 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200760}
761
Paolo Bonzini1964a392013-02-22 17:36:45 +0100762void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400763{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100764 f->xfer_limit = limit;
765}
Glauber Costa19629532009-05-20 18:26:57 -0400766
Paolo Bonzini1964a392013-02-22 17:36:45 +0100767void qemu_file_reset_rate_limit(QEMUFile *f)
768{
769 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400770}
771
aliguoria672b462008-11-11 21:33:36 +0000772void qemu_put_be16(QEMUFile *f, unsigned int v)
773{
774 qemu_put_byte(f, v >> 8);
775 qemu_put_byte(f, v);
776}
777
778void qemu_put_be32(QEMUFile *f, unsigned int v)
779{
780 qemu_put_byte(f, v >> 24);
781 qemu_put_byte(f, v >> 16);
782 qemu_put_byte(f, v >> 8);
783 qemu_put_byte(f, v);
784}
785
786void qemu_put_be64(QEMUFile *f, uint64_t v)
787{
788 qemu_put_be32(f, v >> 32);
789 qemu_put_be32(f, v);
790}
791
792unsigned int qemu_get_be16(QEMUFile *f)
793{
794 unsigned int v;
795 v = qemu_get_byte(f) << 8;
796 v |= qemu_get_byte(f);
797 return v;
798}
799
800unsigned int qemu_get_be32(QEMUFile *f)
801{
802 unsigned int v;
803 v = qemu_get_byte(f) << 24;
804 v |= qemu_get_byte(f) << 16;
805 v |= qemu_get_byte(f) << 8;
806 v |= qemu_get_byte(f);
807 return v;
808}
809
810uint64_t qemu_get_be64(QEMUFile *f)
811{
812 uint64_t v;
813 v = (uint64_t)qemu_get_be32(f) << 32;
814 v |= qemu_get_be32(f);
815 return v;
816}
817
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200818
819/* timer */
820
821void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
822{
823 uint64_t expire_time;
824
825 expire_time = qemu_timer_expire_time_ns(ts);
826 qemu_put_be64(f, expire_time);
827}
828
829void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
830{
831 uint64_t expire_time;
832
833 expire_time = qemu_get_be64(f);
834 if (expire_time != -1) {
835 qemu_mod_timer_ns(ts, expire_time);
836 } else {
837 qemu_del_timer(ts);
838 }
839}
840
841
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100842/* bool */
843
844static int get_bool(QEMUFile *f, void *pv, size_t size)
845{
846 bool *v = pv;
847 *v = qemu_get_byte(f);
848 return 0;
849}
850
851static void put_bool(QEMUFile *f, void *pv, size_t size)
852{
853 bool *v = pv;
854 qemu_put_byte(f, *v);
855}
856
857const VMStateInfo vmstate_info_bool = {
858 .name = "bool",
859 .get = get_bool,
860 .put = put_bool,
861};
862
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200863/* 8 bit int */
864
865static int get_int8(QEMUFile *f, void *pv, size_t size)
866{
867 int8_t *v = pv;
868 qemu_get_s8s(f, v);
869 return 0;
870}
871
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200872static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200873{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200874 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200875 qemu_put_s8s(f, v);
876}
877
878const VMStateInfo vmstate_info_int8 = {
879 .name = "int8",
880 .get = get_int8,
881 .put = put_int8,
882};
883
884/* 16 bit int */
885
886static int get_int16(QEMUFile *f, void *pv, size_t size)
887{
888 int16_t *v = pv;
889 qemu_get_sbe16s(f, v);
890 return 0;
891}
892
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200893static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200894{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200895 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200896 qemu_put_sbe16s(f, v);
897}
898
899const VMStateInfo vmstate_info_int16 = {
900 .name = "int16",
901 .get = get_int16,
902 .put = put_int16,
903};
904
905/* 32 bit int */
906
907static int get_int32(QEMUFile *f, void *pv, size_t size)
908{
909 int32_t *v = pv;
910 qemu_get_sbe32s(f, v);
911 return 0;
912}
913
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200914static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200915{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200916 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200917 qemu_put_sbe32s(f, v);
918}
919
920const VMStateInfo vmstate_info_int32 = {
921 .name = "int32",
922 .get = get_int32,
923 .put = put_int32,
924};
925
Juan Quintela82501662009-08-20 19:42:32 +0200926/* 32 bit int. See that the received value is the same than the one
927 in the field */
928
929static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
930{
931 int32_t *v = pv;
932 int32_t v2;
933 qemu_get_sbe32s(f, &v2);
934
935 if (*v == v2)
936 return 0;
937 return -EINVAL;
938}
939
940const VMStateInfo vmstate_info_int32_equal = {
941 .name = "int32 equal",
942 .get = get_int32_equal,
943 .put = put_int32,
944};
945
Juan Quintela0a031e02009-08-20 19:42:37 +0200946/* 32 bit int. See that the received value is the less or the same
947 than the one in the field */
948
949static int get_int32_le(QEMUFile *f, void *pv, size_t size)
950{
951 int32_t *old = pv;
952 int32_t new;
953 qemu_get_sbe32s(f, &new);
954
955 if (*old <= new)
956 return 0;
957 return -EINVAL;
958}
959
960const VMStateInfo vmstate_info_int32_le = {
961 .name = "int32 equal",
962 .get = get_int32_le,
963 .put = put_int32,
964};
965
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200966/* 64 bit int */
967
968static int get_int64(QEMUFile *f, void *pv, size_t size)
969{
970 int64_t *v = pv;
971 qemu_get_sbe64s(f, v);
972 return 0;
973}
974
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200975static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200976{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200977 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200978 qemu_put_sbe64s(f, v);
979}
980
981const VMStateInfo vmstate_info_int64 = {
982 .name = "int64",
983 .get = get_int64,
984 .put = put_int64,
985};
986
987/* 8 bit unsigned int */
988
989static int get_uint8(QEMUFile *f, void *pv, size_t size)
990{
991 uint8_t *v = pv;
992 qemu_get_8s(f, v);
993 return 0;
994}
995
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200996static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200997{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200998 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200999 qemu_put_8s(f, v);
1000}
1001
1002const VMStateInfo vmstate_info_uint8 = {
1003 .name = "uint8",
1004 .get = get_uint8,
1005 .put = put_uint8,
1006};
1007
1008/* 16 bit unsigned int */
1009
1010static int get_uint16(QEMUFile *f, void *pv, size_t size)
1011{
1012 uint16_t *v = pv;
1013 qemu_get_be16s(f, v);
1014 return 0;
1015}
1016
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001017static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001018{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001019 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001020 qemu_put_be16s(f, v);
1021}
1022
1023const VMStateInfo vmstate_info_uint16 = {
1024 .name = "uint16",
1025 .get = get_uint16,
1026 .put = put_uint16,
1027};
1028
1029/* 32 bit unsigned int */
1030
1031static int get_uint32(QEMUFile *f, void *pv, size_t size)
1032{
1033 uint32_t *v = pv;
1034 qemu_get_be32s(f, v);
1035 return 0;
1036}
1037
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001038static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001039{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001040 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001041 qemu_put_be32s(f, v);
1042}
1043
1044const VMStateInfo vmstate_info_uint32 = {
1045 .name = "uint32",
1046 .get = get_uint32,
1047 .put = put_uint32,
1048};
1049
Juan Quintela9122a8f2011-03-10 12:33:48 +01001050/* 32 bit uint. See that the received value is the same than the one
1051 in the field */
1052
1053static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1054{
1055 uint32_t *v = pv;
1056 uint32_t v2;
1057 qemu_get_be32s(f, &v2);
1058
1059 if (*v == v2) {
1060 return 0;
1061 }
1062 return -EINVAL;
1063}
1064
1065const VMStateInfo vmstate_info_uint32_equal = {
1066 .name = "uint32 equal",
1067 .get = get_uint32_equal,
1068 .put = put_uint32,
1069};
1070
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001071/* 64 bit unsigned int */
1072
1073static int get_uint64(QEMUFile *f, void *pv, size_t size)
1074{
1075 uint64_t *v = pv;
1076 qemu_get_be64s(f, v);
1077 return 0;
1078}
1079
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001080static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001081{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001082 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001083 qemu_put_be64s(f, v);
1084}
1085
1086const VMStateInfo vmstate_info_uint64 = {
1087 .name = "uint64",
1088 .get = get_uint64,
1089 .put = put_uint64,
1090};
1091
David Gibsone344b8a2013-03-12 14:06:00 +11001092/* 64 bit unsigned int. See that the received value is the same than the one
1093 in the field */
1094
1095static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1096{
1097 uint64_t *v = pv;
1098 uint64_t v2;
1099 qemu_get_be64s(f, &v2);
1100
1101 if (*v == v2) {
1102 return 0;
1103 }
1104 return -EINVAL;
1105}
1106
1107const VMStateInfo vmstate_info_uint64_equal = {
1108 .name = "int64 equal",
1109 .get = get_uint64_equal,
1110 .put = put_uint64,
1111};
1112
Juan Quintela80cd83e2009-09-10 03:04:36 +02001113/* 8 bit int. See that the received value is the same than the one
1114 in the field */
1115
1116static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1117{
1118 uint8_t *v = pv;
1119 uint8_t v2;
1120 qemu_get_8s(f, &v2);
1121
1122 if (*v == v2)
1123 return 0;
1124 return -EINVAL;
1125}
1126
1127const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001128 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001129 .get = get_uint8_equal,
1130 .put = put_uint8,
1131};
1132
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001133/* 16 bit unsigned int int. See that the received value is the same than the one
1134 in the field */
1135
1136static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1137{
1138 uint16_t *v = pv;
1139 uint16_t v2;
1140 qemu_get_be16s(f, &v2);
1141
1142 if (*v == v2)
1143 return 0;
1144 return -EINVAL;
1145}
1146
1147const VMStateInfo vmstate_info_uint16_equal = {
1148 .name = "uint16 equal",
1149 .get = get_uint16_equal,
1150 .put = put_uint16,
1151};
1152
David Gibson213945e2013-03-12 14:06:02 +11001153/* floating point */
1154
1155static int get_float64(QEMUFile *f, void *pv, size_t size)
1156{
1157 float64 *v = pv;
1158
1159 *v = make_float64(qemu_get_be64(f));
1160 return 0;
1161}
1162
1163static void put_float64(QEMUFile *f, void *pv, size_t size)
1164{
1165 uint64_t *v = pv;
1166
1167 qemu_put_be64(f, float64_val(*v));
1168}
1169
1170const VMStateInfo vmstate_info_float64 = {
1171 .name = "float64",
1172 .get = get_float64,
1173 .put = put_float64,
1174};
1175
Juan Quinteladde04632009-08-20 19:42:26 +02001176/* timers */
1177
1178static int get_timer(QEMUFile *f, void *pv, size_t size)
1179{
1180 QEMUTimer *v = pv;
1181 qemu_get_timer(f, v);
1182 return 0;
1183}
1184
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001185static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001186{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001187 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001188 qemu_put_timer(f, v);
1189}
1190
1191const VMStateInfo vmstate_info_timer = {
1192 .name = "timer",
1193 .get = get_timer,
1194 .put = put_timer,
1195};
1196
Juan Quintela6f67c502009-08-20 19:42:35 +02001197/* uint8_t buffers */
1198
1199static int get_buffer(QEMUFile *f, void *pv, size_t size)
1200{
1201 uint8_t *v = pv;
1202 qemu_get_buffer(f, v, size);
1203 return 0;
1204}
1205
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001206static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001207{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001208 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001209 qemu_put_buffer(f, v, size);
1210}
1211
1212const VMStateInfo vmstate_info_buffer = {
1213 .name = "buffer",
1214 .get = get_buffer,
1215 .put = put_buffer,
1216};
1217
Juan Quintela76507c72009-10-19 15:46:28 +02001218/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001219 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001220
1221static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1222{
Jan Kiszka21174c32009-12-02 12:36:35 +01001223 uint8_t buf[1024];
1224 int block_len;
1225
1226 while (size > 0) {
1227 block_len = MIN(sizeof(buf), size);
1228 size -= block_len;
1229 qemu_get_buffer(f, buf, block_len);
1230 }
1231 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001232}
1233
1234static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1235{
Jan Kiszka21174c32009-12-02 12:36:35 +01001236 static const uint8_t buf[1024];
1237 int block_len;
1238
1239 while (size > 0) {
1240 block_len = MIN(sizeof(buf), size);
1241 size -= block_len;
1242 qemu_put_buffer(f, buf, block_len);
1243 }
Juan Quintela76507c72009-10-19 15:46:28 +02001244}
1245
1246const VMStateInfo vmstate_info_unused_buffer = {
1247 .name = "unused_buffer",
1248 .get = get_unused_buffer,
1249 .put = put_unused_buffer,
1250};
1251
Peter Maydell08e99e22012-10-30 07:45:12 +00001252/* bitmaps (as defined by bitmap.h). Note that size here is the size
1253 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1254 * bit words with the bits in big endian order. The in-memory format
1255 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1256 */
1257/* This is the number of 64 bit words sent over the wire */
1258#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1259static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1260{
1261 unsigned long *bmp = pv;
1262 int i, idx = 0;
1263 for (i = 0; i < BITS_TO_U64S(size); i++) {
1264 uint64_t w = qemu_get_be64(f);
1265 bmp[idx++] = w;
1266 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1267 bmp[idx++] = w >> 32;
1268 }
1269 }
1270 return 0;
1271}
1272
1273static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1274{
1275 unsigned long *bmp = pv;
1276 int i, idx = 0;
1277 for (i = 0; i < BITS_TO_U64S(size); i++) {
1278 uint64_t w = bmp[idx++];
1279 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1280 w |= ((uint64_t)bmp[idx++]) << 32;
1281 }
1282 qemu_put_be64(f, w);
1283 }
1284}
1285
1286const VMStateInfo vmstate_info_bitmap = {
1287 .name = "bitmap",
1288 .get = get_bitmap,
1289 .put = put_bitmap,
1290};
1291
Alex Williamson7685ee62010-06-25 11:09:14 -06001292typedef struct CompatEntry {
1293 char idstr[256];
1294 int instance_id;
1295} CompatEntry;
1296
aliguoria672b462008-11-11 21:33:36 +00001297typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001298 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001299 char idstr[256];
1300 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001301 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001302 int version_id;
1303 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001304 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001305 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001306 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001307 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001308 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001309 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001310} SaveStateEntry;
1311
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001312
Blue Swirl72cf2d42009-09-12 07:36:22 +00001313static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1314 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001315static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001316
Juan Quintela8718e992009-09-01 02:12:31 +02001317static int calculate_new_instance_id(const char *idstr)
1318{
1319 SaveStateEntry *se;
1320 int instance_id = 0;
1321
Blue Swirl72cf2d42009-09-12 07:36:22 +00001322 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001323 if (strcmp(idstr, se->idstr) == 0
1324 && instance_id <= se->instance_id) {
1325 instance_id = se->instance_id + 1;
1326 }
1327 }
1328 return instance_id;
1329}
1330
Alex Williamson7685ee62010-06-25 11:09:14 -06001331static int calculate_compat_instance_id(const char *idstr)
1332{
1333 SaveStateEntry *se;
1334 int instance_id = 0;
1335
1336 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1337 if (!se->compat)
1338 continue;
1339
1340 if (strcmp(idstr, se->compat->idstr) == 0
1341 && instance_id <= se->compat->instance_id) {
1342 instance_id = se->compat->instance_id + 1;
1343 }
1344 }
1345 return instance_id;
1346}
1347
aliguoria672b462008-11-11 21:33:36 +00001348/* TODO: Individual devices generally have very little idea about the rest
1349 of the system, so instance_id should be removed/replaced.
1350 Meanwhile pass -1 as instance_id if you do not already have a clearly
1351 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001352int register_savevm_live(DeviceState *dev,
1353 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001354 int instance_id,
1355 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001356 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001357 void *opaque)
1358{
Juan Quintela8718e992009-09-01 02:12:31 +02001359 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001360
Anthony Liguori7267c092011-08-20 22:09:37 -05001361 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001362 se->version_id = version_id;
1363 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001364 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001365 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001366 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001367 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001368 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001369 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001370 se->is_ram = 1;
1371 }
aliguoria672b462008-11-11 21:33:36 +00001372
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001373 if (dev) {
1374 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001375 if (id) {
1376 pstrcpy(se->idstr, sizeof(se->idstr), id);
1377 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001378 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001379
Anthony Liguori7267c092011-08-20 22:09:37 -05001380 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001381 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1382 se->compat->instance_id = instance_id == -1 ?
1383 calculate_compat_instance_id(idstr) : instance_id;
1384 instance_id = -1;
1385 }
1386 }
1387 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1388
Juan Quintela8718e992009-09-01 02:12:31 +02001389 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001390 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001391 } else {
1392 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001393 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001394 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001395 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001396 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001397 return 0;
1398}
1399
Alex Williamson0be71e32010-06-25 11:09:07 -06001400int register_savevm(DeviceState *dev,
1401 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001402 int instance_id,
1403 int version_id,
1404 SaveStateHandler *save_state,
1405 LoadStateHandler *load_state,
1406 void *opaque)
1407{
Juan Quintela7908c782012-06-26 18:46:10 +02001408 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1409 ops->save_state = save_state;
1410 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001411 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001412 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001413}
1414
Alex Williamson0be71e32010-06-25 11:09:07 -06001415void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001416{
Juan Quintela8718e992009-09-01 02:12:31 +02001417 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001418 char id[256] = "";
1419
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001420 if (dev) {
1421 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001422 if (path) {
1423 pstrcpy(id, sizeof(id), path);
1424 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001425 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001426 }
1427 }
1428 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001429
Blue Swirl72cf2d42009-09-12 07:36:22 +00001430 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001431 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001432 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001433 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001434 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001435 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001436 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001437 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001438 }
aliguori41bd13a2009-04-17 17:10:59 +00001439 }
1440}
1441
Alex Williamson0be71e32010-06-25 11:09:07 -06001442int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001443 const VMStateDescription *vmsd,
1444 void *opaque, int alias_id,
1445 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001446{
Juan Quintela8718e992009-09-01 02:12:31 +02001447 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001448
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001449 /* If this triggers, alias support can be dropped for the vmsd. */
1450 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1451
Anthony Liguori7267c092011-08-20 22:09:37 -05001452 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001453 se->version_id = vmsd->version_id;
1454 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001455 se->opaque = opaque;
1456 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001457 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001458 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001459
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001460 if (dev) {
1461 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001462 if (id) {
1463 pstrcpy(se->idstr, sizeof(se->idstr), id);
1464 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001465 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001466
Anthony Liguori7267c092011-08-20 22:09:37 -05001467 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001468 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1469 se->compat->instance_id = instance_id == -1 ?
1470 calculate_compat_instance_id(vmsd->name) : instance_id;
1471 instance_id = -1;
1472 }
1473 }
1474 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1475
Juan Quintela8718e992009-09-01 02:12:31 +02001476 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001477 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001478 } else {
1479 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001480 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001481 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001482 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001483 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001484 return 0;
1485}
1486
Alex Williamson0be71e32010-06-25 11:09:07 -06001487void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1488 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001489{
Juan Quintela1eb75382009-09-10 03:04:29 +02001490 SaveStateEntry *se, *new_se;
1491
Blue Swirl72cf2d42009-09-12 07:36:22 +00001492 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001493 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001494 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001495 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001496 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001497 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001498 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001499 }
1500 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001501}
1502
Juan Quintela811814b2010-07-26 21:38:43 +02001503static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1504 void *opaque);
1505static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1506 void *opaque);
1507
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001508int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1509 void *opaque, int version_id)
1510{
1511 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001512 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001513
1514 if (version_id > vmsd->version_id) {
1515 return -EINVAL;
1516 }
1517 if (version_id < vmsd->minimum_version_id_old) {
1518 return -EINVAL;
1519 }
1520 if (version_id < vmsd->minimum_version_id) {
1521 return vmsd->load_state_old(f, opaque, version_id);
1522 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001523 if (vmsd->pre_load) {
1524 int ret = vmsd->pre_load(opaque);
1525 if (ret)
1526 return ret;
1527 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001528 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001529 if ((field->field_exists &&
1530 field->field_exists(opaque, version_id)) ||
1531 (!field->field_exists &&
1532 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001533 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001534 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001535 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001536
Juan Quintelae61a1e02009-12-02 12:36:38 +01001537 if (field->flags & VMS_VBUFFER) {
1538 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001539 if (field->flags & VMS_MULTIPLY) {
1540 size *= field->size;
1541 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001542 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001543 if (field->flags & VMS_ARRAY) {
1544 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001545 } else if (field->flags & VMS_VARRAY_INT32) {
1546 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001547 } else if (field->flags & VMS_VARRAY_UINT32) {
1548 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001549 } else if (field->flags & VMS_VARRAY_UINT16) {
1550 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001551 } else if (field->flags & VMS_VARRAY_UINT8) {
1552 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001553 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001554 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001555 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001556 }
1557 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001558 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001559
Juan Quintela19df4382009-09-29 22:48:41 +02001560 if (field->flags & VMS_ARRAY_OF_POINTER) {
1561 addr = *(void **)addr;
1562 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001563 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001564 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001565 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001566 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001567
1568 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001569 if (ret < 0) {
1570 return ret;
1571 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001572 }
1573 }
1574 field++;
1575 }
Juan Quintela811814b2010-07-26 21:38:43 +02001576 ret = vmstate_subsection_load(f, vmsd, opaque);
1577 if (ret != 0) {
1578 return ret;
1579 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001580 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001581 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001582 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001583 return 0;
1584}
1585
1586void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001587 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001588{
1589 VMStateField *field = vmsd->fields;
1590
Juan Quintela8fb07912009-09-10 03:04:32 +02001591 if (vmsd->pre_save) {
1592 vmsd->pre_save(opaque);
1593 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001594 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001595 if (!field->field_exists ||
1596 field->field_exists(opaque, vmsd->version_id)) {
1597 void *base_addr = opaque + field->offset;
1598 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001599 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001600
Juan Quintelae61a1e02009-12-02 12:36:38 +01001601 if (field->flags & VMS_VBUFFER) {
1602 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001603 if (field->flags & VMS_MULTIPLY) {
1604 size *= field->size;
1605 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001606 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001607 if (field->flags & VMS_ARRAY) {
1608 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001609 } else if (field->flags & VMS_VARRAY_INT32) {
1610 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001611 } else if (field->flags & VMS_VARRAY_UINT32) {
1612 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001613 } else if (field->flags & VMS_VARRAY_UINT16) {
1614 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001615 } else if (field->flags & VMS_VARRAY_UINT8) {
1616 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001617 }
1618 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001619 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001620 }
1621 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001622 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001623
Juan Quintela85953872009-12-02 12:36:37 +01001624 if (field->flags & VMS_ARRAY_OF_POINTER) {
1625 addr = *(void **)addr;
1626 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001627 if (field->flags & VMS_STRUCT) {
1628 vmstate_save_state(f, field->vmsd, addr);
1629 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001630 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001631 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001632 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001633 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001634 field++;
1635 }
Juan Quintela811814b2010-07-26 21:38:43 +02001636 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001637}
1638
Juan Quintela4082be42009-08-20 19:42:24 +02001639static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1640{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001641 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001642 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001643 }
1644 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001645}
1646
Alex Williamsondc912122011-01-11 14:39:43 -07001647static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001648{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001649 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001650 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001651 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001652 }
1653 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001654}
1655
aliguoria672b462008-11-11 21:33:36 +00001656#define QEMU_VM_FILE_MAGIC 0x5145564d
1657#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1658#define QEMU_VM_FILE_VERSION 0x00000003
1659
1660#define QEMU_VM_EOF 0x00
1661#define QEMU_VM_SECTION_START 0x01
1662#define QEMU_VM_SECTION_PART 0x02
1663#define QEMU_VM_SECTION_END 0x03
1664#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001665#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001666
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001667bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001668{
1669 SaveStateEntry *se;
1670
1671 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1672 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001673 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001674 return true;
1675 }
1676 }
1677 return false;
1678}
1679
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001680void qemu_savevm_state_begin(QEMUFile *f,
1681 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001682{
1683 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001684 int ret;
aliguoria672b462008-11-11 21:33:36 +00001685
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001686 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001687 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001688 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001689 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001690 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001691 }
1692
aliguoria672b462008-11-11 21:33:36 +00001693 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1694 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1695
Blue Swirl72cf2d42009-09-12 07:36:22 +00001696 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001697 int len;
1698
Juan Quintelad1315aa2012-06-28 15:11:57 +02001699 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001700 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001701 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001702 if (se->ops && se->ops->is_active) {
1703 if (!se->ops->is_active(se->opaque)) {
1704 continue;
1705 }
1706 }
aliguoria672b462008-11-11 21:33:36 +00001707 /* Section type */
1708 qemu_put_byte(f, QEMU_VM_SECTION_START);
1709 qemu_put_be32(f, se->section_id);
1710
1711 /* ID string */
1712 len = strlen(se->idstr);
1713 qemu_put_byte(f, len);
1714 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1715
1716 qemu_put_be32(f, se->instance_id);
1717 qemu_put_be32(f, se->version_id);
1718
Juan Quintelad1315aa2012-06-28 15:11:57 +02001719 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001720 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001721 qemu_file_set_error(f, ret);
1722 break;
Juan Quintela29757252011-10-19 15:22:18 +02001723 }
aliguoria672b462008-11-11 21:33:36 +00001724 }
aliguoria672b462008-11-11 21:33:36 +00001725}
1726
Juan Quintela39346382011-09-22 11:02:14 +02001727/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001728 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001729 * negative: there was one error, and we have -errno.
1730 * 0 : We haven't finished, caller have to go again
1731 * 1 : We have finished, we can go to complete phase
1732 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001733int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001734{
1735 SaveStateEntry *se;
1736 int ret = 1;
1737
Blue Swirl72cf2d42009-09-12 07:36:22 +00001738 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001739 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001740 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001741 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001742 if (se->ops && se->ops->is_active) {
1743 if (!se->ops->is_active(se->opaque)) {
1744 continue;
1745 }
1746 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001747 if (qemu_file_rate_limit(f)) {
1748 return 0;
1749 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001750 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001751 /* Section type */
1752 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1753 qemu_put_be32(f, se->section_id);
1754
Juan Quintela16310a32012-06-28 15:31:37 +02001755 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001756 trace_savevm_section_end(se->section_id);
1757
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001758 if (ret < 0) {
1759 qemu_file_set_error(f, ret);
1760 }
Juan Quintela29757252011-10-19 15:22:18 +02001761 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001762 /* Do not proceed to the next vmstate before this one reported
1763 completion of the current stage. This serializes the migration
1764 and reduces the probability that a faster changing state is
1765 synchronized over and over again. */
1766 break;
1767 }
aliguoria672b462008-11-11 21:33:36 +00001768 }
Juan Quintela39346382011-09-22 11:02:14 +02001769 return ret;
aliguoria672b462008-11-11 21:33:36 +00001770}
1771
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001772void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001773{
1774 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001775 int ret;
aliguoria672b462008-11-11 21:33:36 +00001776
Jan Kiszkaea375f92010-03-01 19:10:30 +01001777 cpu_synchronize_all_states();
1778
Blue Swirl72cf2d42009-09-12 07:36:22 +00001779 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001780 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001781 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001782 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001783 if (se->ops && se->ops->is_active) {
1784 if (!se->ops->is_active(se->opaque)) {
1785 continue;
1786 }
1787 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001788 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001789 /* Section type */
1790 qemu_put_byte(f, QEMU_VM_SECTION_END);
1791 qemu_put_be32(f, se->section_id);
1792
Juan Quintela16310a32012-06-28 15:31:37 +02001793 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001794 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001795 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001796 qemu_file_set_error(f, ret);
1797 return;
Juan Quintela29757252011-10-19 15:22:18 +02001798 }
aliguoria672b462008-11-11 21:33:36 +00001799 }
1800
Blue Swirl72cf2d42009-09-12 07:36:22 +00001801 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001802 int len;
1803
Juan Quintela22ea40f2012-06-26 17:19:10 +02001804 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001805 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001806 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001807 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001808 /* Section type */
1809 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1810 qemu_put_be32(f, se->section_id);
1811
1812 /* ID string */
1813 len = strlen(se->idstr);
1814 qemu_put_byte(f, len);
1815 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1816
1817 qemu_put_be32(f, se->instance_id);
1818 qemu_put_be32(f, se->version_id);
1819
Alex Williamsondc912122011-01-11 14:39:43 -07001820 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001821 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001822 }
1823
1824 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001825 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001826}
1827
Juan Quintelae4ed1542012-09-21 11:18:18 +02001828uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1829{
1830 SaveStateEntry *se;
1831 uint64_t ret = 0;
1832
1833 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1834 if (!se->ops || !se->ops->save_live_pending) {
1835 continue;
1836 }
1837 if (se->ops && se->ops->is_active) {
1838 if (!se->ops->is_active(se->opaque)) {
1839 continue;
1840 }
1841 }
1842 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1843 }
1844 return ret;
1845}
1846
Juan Quintela65227732013-01-14 14:14:42 +01001847void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001848{
1849 SaveStateEntry *se;
1850
1851 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001852 if (se->ops && se->ops->cancel) {
1853 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001854 }
1855 }
1856}
1857
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001858static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001859{
aliguoria672b462008-11-11 21:33:36 +00001860 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001861 MigrationParams params = {
1862 .blk = 0,
1863 .shared = 0
1864 };
aliguoria672b462008-11-11 21:33:36 +00001865
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001866 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001867 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07001868 }
1869
Paolo Bonzini9b095032013-02-22 17:36:28 +01001870 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001871 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001872 qemu_mutex_lock_iothread();
1873
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001874 while (qemu_file_get_error(f) == 0) {
1875 if (qemu_savevm_state_iterate(f) > 0) {
1876 break;
1877 }
1878 }
aliguoria672b462008-11-11 21:33:36 +00001879
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001880 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001881 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001882 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001883 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001884 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001885 if (ret != 0) {
1886 qemu_savevm_state_cancel();
1887 }
aliguoria672b462008-11-11 21:33:36 +00001888 return ret;
1889}
1890
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001891static int qemu_save_device_state(QEMUFile *f)
1892{
1893 SaveStateEntry *se;
1894
1895 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1896 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1897
1898 cpu_synchronize_all_states();
1899
1900 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1901 int len;
1902
1903 if (se->is_ram) {
1904 continue;
1905 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001906 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001907 continue;
1908 }
1909
1910 /* Section type */
1911 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1912 qemu_put_be32(f, se->section_id);
1913
1914 /* ID string */
1915 len = strlen(se->idstr);
1916 qemu_put_byte(f, len);
1917 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1918
1919 qemu_put_be32(f, se->instance_id);
1920 qemu_put_be32(f, se->version_id);
1921
1922 vmstate_save(f, se);
1923 }
1924
1925 qemu_put_byte(f, QEMU_VM_EOF);
1926
1927 return qemu_file_get_error(f);
1928}
1929
aliguoria672b462008-11-11 21:33:36 +00001930static SaveStateEntry *find_se(const char *idstr, int instance_id)
1931{
1932 SaveStateEntry *se;
1933
Blue Swirl72cf2d42009-09-12 07:36:22 +00001934 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001935 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001936 (instance_id == se->instance_id ||
1937 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001938 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001939 /* Migrating from an older version? */
1940 if (strstr(se->idstr, idstr) && se->compat) {
1941 if (!strcmp(se->compat->idstr, idstr) &&
1942 (instance_id == se->compat->instance_id ||
1943 instance_id == se->alias_id))
1944 return se;
1945 }
aliguoria672b462008-11-11 21:33:36 +00001946 }
1947 return NULL;
1948}
1949
Juan Quintela811814b2010-07-26 21:38:43 +02001950static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1951{
1952 while(sub && sub->needed) {
1953 if (strcmp(idstr, sub->vmsd->name) == 0) {
1954 return sub->vmsd;
1955 }
1956 sub++;
1957 }
1958 return NULL;
1959}
1960
1961static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1962 void *opaque)
1963{
Juan Quintelac6380722011-10-04 15:28:31 +02001964 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001965 char idstr[256];
1966 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001967 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001968 const VMStateDescription *sub_vmsd;
1969
Juan Quintelac6380722011-10-04 15:28:31 +02001970 len = qemu_peek_byte(f, 1);
1971 if (len < strlen(vmsd->name) + 1) {
1972 /* subsection name has be be "section_name/a" */
1973 return 0;
1974 }
1975 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1976 if (size != len) {
1977 return 0;
1978 }
1979 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001980
Juan Quintelac6380722011-10-04 15:28:31 +02001981 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1982 /* it don't have a valid subsection name */
1983 return 0;
1984 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001985 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001986 if (sub_vmsd == NULL) {
1987 return -ENOENT;
1988 }
Juan Quintelac6380722011-10-04 15:28:31 +02001989 qemu_file_skip(f, 1); /* subsection */
1990 qemu_file_skip(f, 1); /* len */
1991 qemu_file_skip(f, len); /* idstr */
1992 version_id = qemu_get_be32(f);
1993
Juan Quintela811814b2010-07-26 21:38:43 +02001994 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1995 if (ret) {
1996 return ret;
1997 }
1998 }
1999 return 0;
2000}
2001
2002static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2003 void *opaque)
2004{
2005 const VMStateSubsection *sub = vmsd->subsections;
2006
2007 while (sub && sub->needed) {
2008 if (sub->needed(opaque)) {
2009 const VMStateDescription *vmsd = sub->vmsd;
2010 uint8_t len;
2011
2012 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2013 len = strlen(vmsd->name);
2014 qemu_put_byte(f, len);
2015 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2016 qemu_put_be32(f, vmsd->version_id);
2017 vmstate_save_state(f, vmsd, opaque);
2018 }
2019 sub++;
2020 }
2021}
2022
aliguoria672b462008-11-11 21:33:36 +00002023typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002024 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002025 SaveStateEntry *se;
2026 int section_id;
2027 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002028} LoadStateEntry;
2029
aliguoria672b462008-11-11 21:33:36 +00002030int qemu_loadvm_state(QEMUFile *f)
2031{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002032 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2033 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002034 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002035 uint8_t section_type;
2036 unsigned int v;
2037 int ret;
2038
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002039 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002040 return -EINVAL;
2041 }
2042
aliguoria672b462008-11-11 21:33:36 +00002043 v = qemu_get_be32(f);
2044 if (v != QEMU_VM_FILE_MAGIC)
2045 return -EINVAL;
2046
2047 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002048 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2049 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2050 return -ENOTSUP;
2051 }
aliguoria672b462008-11-11 21:33:36 +00002052 if (v != QEMU_VM_FILE_VERSION)
2053 return -ENOTSUP;
2054
2055 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2056 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002057 SaveStateEntry *se;
2058 char idstr[257];
2059 int len;
2060
2061 switch (section_type) {
2062 case QEMU_VM_SECTION_START:
2063 case QEMU_VM_SECTION_FULL:
2064 /* Read section start */
2065 section_id = qemu_get_be32(f);
2066 len = qemu_get_byte(f);
2067 qemu_get_buffer(f, (uint8_t *)idstr, len);
2068 idstr[len] = 0;
2069 instance_id = qemu_get_be32(f);
2070 version_id = qemu_get_be32(f);
2071
2072 /* Find savevm section */
2073 se = find_se(idstr, instance_id);
2074 if (se == NULL) {
2075 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2076 ret = -EINVAL;
2077 goto out;
2078 }
2079
2080 /* Validate version */
2081 if (version_id > se->version_id) {
2082 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2083 version_id, idstr, se->version_id);
2084 ret = -EINVAL;
2085 goto out;
2086 }
2087
2088 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002089 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002090
2091 le->se = se;
2092 le->section_id = section_id;
2093 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002094 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002095
Juan Quintela4082be42009-08-20 19:42:24 +02002096 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002097 if (ret < 0) {
2098 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2099 instance_id, idstr);
2100 goto out;
2101 }
aliguoria672b462008-11-11 21:33:36 +00002102 break;
2103 case QEMU_VM_SECTION_PART:
2104 case QEMU_VM_SECTION_END:
2105 section_id = qemu_get_be32(f);
2106
Blue Swirl72cf2d42009-09-12 07:36:22 +00002107 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002108 if (le->section_id == section_id) {
2109 break;
2110 }
2111 }
aliguoria672b462008-11-11 21:33:36 +00002112 if (le == NULL) {
2113 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2114 ret = -EINVAL;
2115 goto out;
2116 }
2117
Juan Quintela4082be42009-08-20 19:42:24 +02002118 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002119 if (ret < 0) {
2120 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2121 section_id);
2122 goto out;
2123 }
aliguoria672b462008-11-11 21:33:36 +00002124 break;
2125 default:
2126 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2127 ret = -EINVAL;
2128 goto out;
2129 }
2130 }
2131
Jan Kiszkaea375f92010-03-01 19:10:30 +01002132 cpu_synchronize_all_post_init();
2133
aliguoria672b462008-11-11 21:33:36 +00002134 ret = 0;
2135
2136out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002137 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2138 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002139 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002140 }
2141
Juan Quintela42802d42011-10-05 01:14:46 +02002142 if (ret == 0) {
2143 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002144 }
aliguoria672b462008-11-11 21:33:36 +00002145
2146 return ret;
2147}
2148
aliguoria672b462008-11-11 21:33:36 +00002149static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2150 const char *name)
2151{
2152 QEMUSnapshotInfo *sn_tab, *sn;
2153 int nb_sns, i, ret;
2154
2155 ret = -ENOENT;
2156 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2157 if (nb_sns < 0)
2158 return ret;
2159 for(i = 0; i < nb_sns; i++) {
2160 sn = &sn_tab[i];
2161 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2162 *sn_info = *sn;
2163 ret = 0;
2164 break;
2165 }
2166 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002167 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002168 return ret;
2169}
2170
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002171/*
2172 * Deletes snapshots of a given name in all opened images.
2173 */
2174static int del_existing_snapshots(Monitor *mon, const char *name)
2175{
2176 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002177 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2178 int ret;
2179
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002180 bs = NULL;
2181 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002182 if (bdrv_can_snapshot(bs) &&
2183 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2184 {
2185 ret = bdrv_snapshot_delete(bs, name);
2186 if (ret < 0) {
2187 monitor_printf(mon,
2188 "Error while deleting snapshot on '%s'\n",
2189 bdrv_get_device_name(bs));
2190 return -1;
2191 }
2192 }
2193 }
2194
2195 return 0;
2196}
2197
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002198void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002199{
2200 BlockDriverState *bs, *bs1;
2201 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002202 int ret;
aliguoria672b462008-11-11 21:33:36 +00002203 QEMUFile *f;
2204 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002205 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002206 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002207 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002208 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002209
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002210 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002211 bs = NULL;
2212 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002213
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002214 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002215 continue;
2216 }
2217
2218 if (!bdrv_can_snapshot(bs)) {
2219 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2220 bdrv_get_device_name(bs));
2221 return;
2222 }
2223 }
2224
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002225 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002226 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002227 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002228 return;
2229 }
aliguoria672b462008-11-11 21:33:36 +00002230
Luiz Capitulino13548692011-07-29 15:36:43 -03002231 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002232 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002233
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002234 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002235
2236 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002237 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002238 sn->date_sec = tv.tv_sec;
2239 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002240 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002241
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002242 if (name) {
2243 ret = bdrv_snapshot_find(bs, old_sn, name);
2244 if (ret >= 0) {
2245 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2246 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2247 } else {
2248 pstrcpy(sn->name, sizeof(sn->name), name);
2249 }
2250 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002251 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2252 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002253 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002254 }
2255
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002256 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002257 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002258 goto the_end;
2259 }
2260
aliguoria672b462008-11-11 21:33:36 +00002261 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002262 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002263 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002264 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002265 goto the_end;
2266 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002267 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002268 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002269 qemu_fclose(f);
2270 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002271 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002272 goto the_end;
2273 }
2274
2275 /* create the snapshots */
2276
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002277 bs1 = NULL;
2278 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002279 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002280 /* Write VM state size only to the image that contains the state */
2281 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002282 ret = bdrv_snapshot_create(bs1, sn);
2283 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002284 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2285 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002286 }
2287 }
2288 }
2289
2290 the_end:
2291 if (saved_vm_running)
2292 vm_start();
2293}
2294
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002295void qmp_xen_save_devices_state(const char *filename, Error **errp)
2296{
2297 QEMUFile *f;
2298 int saved_vm_running;
2299 int ret;
2300
2301 saved_vm_running = runstate_is_running();
2302 vm_stop(RUN_STATE_SAVE_VM);
2303
2304 f = qemu_fopen(filename, "wb");
2305 if (!f) {
2306 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2307 goto the_end;
2308 }
2309 ret = qemu_save_device_state(f);
2310 qemu_fclose(f);
2311 if (ret < 0) {
2312 error_set(errp, QERR_IO_ERROR);
2313 }
2314
2315 the_end:
2316 if (saved_vm_running)
2317 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002318}
2319
Markus Armbruster03cd4652010-02-17 16:24:10 +01002320int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002321{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002322 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002323 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002324 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002325 int ret;
aliguoria672b462008-11-11 21:33:36 +00002326
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002327 bs_vm_state = bdrv_snapshots();
2328 if (!bs_vm_state) {
2329 error_report("No block device supports snapshots");
2330 return -ENOTSUP;
2331 }
2332
2333 /* Don't even try to load empty VM states */
2334 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2335 if (ret < 0) {
2336 return ret;
2337 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002338 error_report("This is a disk-only snapshot. Revert to it offline "
2339 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002340 return -EINVAL;
2341 }
2342
2343 /* Verify if there is any device that doesn't support snapshots and is
2344 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002345 bs = NULL;
2346 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002347
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002348 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002349 continue;
2350 }
2351
2352 if (!bdrv_can_snapshot(bs)) {
2353 error_report("Device '%s' is writable but does not support snapshots.",
2354 bdrv_get_device_name(bs));
2355 return -ENOTSUP;
2356 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002357
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002358 ret = bdrv_snapshot_find(bs, &sn, name);
2359 if (ret < 0) {
2360 error_report("Device '%s' does not have the requested snapshot '%s'",
2361 bdrv_get_device_name(bs), name);
2362 return ret;
2363 }
aliguoria672b462008-11-11 21:33:36 +00002364 }
2365
2366 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002367 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002368
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002369 bs = NULL;
2370 while ((bs = bdrv_next(bs))) {
2371 if (bdrv_can_snapshot(bs)) {
2372 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002373 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002374 error_report("Error %d while activating snapshot '%s' on '%s'",
2375 ret, name, bdrv_get_device_name(bs));
2376 return ret;
aliguoria672b462008-11-11 21:33:36 +00002377 }
2378 }
2379 }
2380
aliguoria672b462008-11-11 21:33:36 +00002381 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002382 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002383 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002384 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002385 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002386 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002387
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002388 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002389 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002390
aliguoria672b462008-11-11 21:33:36 +00002391 qemu_fclose(f);
2392 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002393 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002394 return ret;
aliguoria672b462008-11-11 21:33:36 +00002395 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002396
Juan Quintela05f24012009-08-20 19:42:22 +02002397 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002398}
2399
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002400void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002401{
2402 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002403 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002404 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002405
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002406 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002407 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002408 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002409 return;
2410 }
2411
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002412 bs1 = NULL;
2413 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002414 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002415 ret = bdrv_snapshot_delete(bs1, name);
2416 if (ret < 0) {
2417 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002418 monitor_printf(mon,
2419 "Snapshots not supported on device '%s'\n",
2420 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002421 else
aliguori376253e2009-03-05 23:01:23 +00002422 monitor_printf(mon, "Error %d while deleting snapshot on "
2423 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002424 }
2425 }
2426 }
2427}
2428
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002429void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002430{
2431 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002432 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2433 int nb_sns, i, ret, available;
2434 int total;
2435 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002436 char buf[256];
2437
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002438 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002439 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002440 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002441 return;
2442 }
aliguoria672b462008-11-11 21:33:36 +00002443
2444 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2445 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002446 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002447 return;
2448 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002449
2450 if (nb_sns == 0) {
2451 monitor_printf(mon, "There is no snapshot available.\n");
2452 return;
aliguoria672b462008-11-11 21:33:36 +00002453 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002454
Anthony Liguori7267c092011-08-20 22:09:37 -05002455 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002456 total = 0;
2457 for (i = 0; i < nb_sns; i++) {
2458 sn = &sn_tab[i];
2459 available = 1;
2460 bs1 = NULL;
2461
2462 while ((bs1 = bdrv_next(bs1))) {
2463 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2464 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2465 if (ret < 0) {
2466 available = 0;
2467 break;
2468 }
2469 }
2470 }
2471
2472 if (available) {
2473 available_snapshots[total] = i;
2474 total++;
2475 }
2476 }
2477
2478 if (total > 0) {
2479 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2480 for (i = 0; i < total; i++) {
2481 sn = &sn_tab[available_snapshots[i]];
2482 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2483 }
2484 } else {
2485 monitor_printf(mon, "There is no suitable snapshot available\n");
2486 }
2487
Anthony Liguori7267c092011-08-20 22:09:37 -05002488 g_free(sn_tab);
2489 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002490
aliguoria672b462008-11-11 21:33:36 +00002491}
Avi Kivityc5705a72011-12-20 15:59:12 +02002492
2493void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2494{
Avi Kivity1ddde082012-01-08 13:18:19 +02002495 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002496 memory_region_name(mr), dev);
2497}
2498
2499void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2500{
2501 /* Nothing do to while the implementation is in RAMBlock */
2502}
2503
2504void vmstate_register_ram_global(MemoryRegion *mr)
2505{
2506 vmstate_register_ram(mr, NULL);
2507}