blob: aaaf39a678e0c30a55389e7f6671695a21c40103 [file] [log] [blame]
aliguoria672b462008-11-11 21:33:36 +00001/*
2 * QEMU System Emulator
3 *
4 * Copyright (c) 2003-2008 Fabrice Bellard
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
aliguoria672b462008-11-11 21:33:36 +000024
blueswir1d40cdb12009-03-07 16:52:02 +000025#include "config-host.h"
blueswir1511d2b12009-03-07 15:32:56 +000026#include "qemu-common.h"
27#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060028#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020029#include "net/net.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010030#include "monitor/monitor.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010031#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010032#include "qemu/timer.h"
blueswir1511d2b12009-03-07 15:32:56 +000033#include "audio/audio.h"
Paolo Bonzinicaf71f82012-12-17 18:19:50 +010034#include "migration/migration.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010035#include "qemu/sockets.h"
36#include "qemu/queue.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +010037#include "sysemu/cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010038#include "exec/memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000039#include "qmp-commands.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020040#include "trace.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +010041#include "qemu/bitops.h"
Orit Wasserman28085f72013-03-22 16:47:58 +020042#include "qemu/iov.h"
blueswir1511d2b12009-03-07 15:32:56 +000043
aliguoria672b462008-11-11 21:33:36 +000044#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000045
Nolan18995b92009-10-15 16:53:55 -070046#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040047#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070048#endif
49#define ARP_HTYPE_ETH 0x0001
50#define ARP_PTYPE_IP 0x0800
51#define ARP_OP_REQUEST_REV 0x3
52
53static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000054 uint8_t *mac_addr)
55{
Nolan18995b92009-10-15 16:53:55 -070056 /* Ethernet header. */
57 memset(buf, 0xff, 6); /* destination MAC addr */
58 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
59 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000060
Nolan18995b92009-10-15 16:53:55 -070061 /* RARP header. */
62 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
63 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
64 *(buf + 18) = 6; /* hardware addr length (ethernet) */
65 *(buf + 19) = 4; /* protocol addr length (IPv4) */
66 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
67 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
68 memset(buf + 28, 0x00, 4); /* source protocol addr */
69 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
70 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000071
Nolan18995b92009-10-15 16:53:55 -070072 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
73 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000074
Nolan18995b92009-10-15 16:53:55 -070075 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000076}
77
Mark McLoughlinf401ca22009-11-25 18:49:32 +000078static void qemu_announce_self_iter(NICState *nic, void *opaque)
79{
80 uint8_t buf[60];
81 int len;
82
83 len = announce_self_create(buf, nic->conf->macaddr.a);
84
Jason Wangb356f762013-01-30 19:12:22 +080085 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000086}
87
88
Gleb Natapoved8b3302009-05-21 17:17:44 +030089static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000090{
Gleb Natapoved8b3302009-05-21 17:17:44 +030091 static int count = SELF_ANNOUNCE_ROUNDS;
92 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000093
Mark McLoughlinf401ca22009-11-25 18:49:32 +000094 qemu_foreach_nic(qemu_announce_self_iter, NULL);
95
Nolan18995b92009-10-15 16:53:55 -070096 if (--count) {
97 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +010098 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -070099 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300100 } else {
101 qemu_del_timer(timer);
102 qemu_free_timer(timer);
103 }
104}
105
106void qemu_announce_self(void)
107{
108 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100109 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300110 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000111}
112
113/***********************************************************/
114/* savevm/loadvm support */
115
116#define IO_BUF_SIZE 32768
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200117#define MAX_IOV_SIZE MIN(IOV_MAX, 64)
aliguoria672b462008-11-11 21:33:36 +0000118
119struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200120 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000121 void *opaque;
122 int is_write;
123
Paolo Bonzini1964a392013-02-22 17:36:45 +0100124 int64_t bytes_xfer;
125 int64_t xfer_limit;
126
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100127 int64_t pos; /* start of buffer when writing, end of buffer
128 when reading */
aliguoria672b462008-11-11 21:33:36 +0000129 int buf_index;
130 int buf_size; /* 0 when writing */
131 uint8_t buf[IO_BUF_SIZE];
132
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200133 struct iovec iov[MAX_IOV_SIZE];
134 unsigned int iovcnt;
135
Juan Quintela3961b4d2011-10-05 01:05:21 +0200136 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000137};
138
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200139typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000140{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200141 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000142 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200143} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000144
145typedef struct QEMUFileSocket
146{
147 int fd;
148 QEMUFile *file;
149} QEMUFileSocket;
150
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100151typedef struct {
152 Coroutine *co;
153 int fd;
154} FDYieldUntilData;
155
156static void fd_coroutine_enter(void *opaque)
157{
158 FDYieldUntilData *data = opaque;
159 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
160 qemu_coroutine_enter(data->co, NULL);
161}
162
163/**
164 * Yield until a file descriptor becomes readable
165 *
166 * Note that this function clobbers the handlers for the file descriptor.
167 */
168static void coroutine_fn yield_until_fd_readable(int fd)
169{
170 FDYieldUntilData data;
171
172 assert(qemu_in_coroutine());
173 data.co = qemu_coroutine_self();
174 data.fd = fd;
175 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
176 qemu_coroutine_yield();
177}
178
Orit Wasserman28085f72013-03-22 16:47:58 +0200179static ssize_t socket_writev_buffer(void *opaque, struct iovec *iov, int iovcnt)
180{
181 QEMUFileSocket *s = opaque;
182 ssize_t len;
183 ssize_t size = iov_size(iov, iovcnt);
184
185 len = iov_send(s->fd, iov, iovcnt, 0, size);
186 if (len < size) {
187 len = -socket_error();
188 }
189 return len;
190}
191
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200192static int socket_get_fd(void *opaque)
193{
194 QEMUFileSocket *s = opaque;
195
196 return s->fd;
197}
198
aliguoria672b462008-11-11 21:33:36 +0000199static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
200{
201 QEMUFileSocket *s = opaque;
202 ssize_t len;
203
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200204 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000205 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200206 if (len != -1) {
207 break;
208 }
209 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100210 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200211 } else if (socket_error() != EINTR) {
212 break;
213 }
214 }
aliguoria672b462008-11-11 21:33:36 +0000215
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200216 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000217 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200218 }
aliguoria672b462008-11-11 21:33:36 +0000219 return len;
220}
221
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100222static int socket_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
223{
224 QEMUFileSocket *s = opaque;
225 ssize_t len;
226
227 len = qemu_send_full(s->fd, buf, size, 0);
228 if (len < size) {
229 len = -socket_error();
230 }
231 return len;
232}
233
aliguoria672b462008-11-11 21:33:36 +0000234static int socket_close(void *opaque)
235{
236 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200237 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500238 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000239 return 0;
240}
241
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200242static int stdio_get_fd(void *opaque)
243{
244 QEMUFileStdio *s = opaque;
245
246 return fileno(s->stdio_file);
247}
248
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200249static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000250{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200251 QEMUFileStdio *s = opaque;
252 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000253}
254
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200255static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000256{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200257 QEMUFileStdio *s = opaque;
258 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300259 int bytes;
260
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200261 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300262 clearerr(fp);
263 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200264 if (bytes != 0 || !ferror(fp)) {
265 break;
266 }
267 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100268 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200269 } else if (errno != EINTR) {
270 break;
271 }
272 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300273 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000274}
275
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200276static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000277{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200278 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500279 int ret;
280 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200281 if (ret == -1) {
282 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100283 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
284 /* close succeeded, but non-zero exit code: */
285 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200286 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500287 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500288 return ret;
aliguoria672b462008-11-11 21:33:36 +0000289}
290
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200291static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000292{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200293 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200294 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100295
296 if (s->file->ops->put_buffer) {
297 int fd = fileno(s->stdio_file);
298 struct stat st;
299
300 ret = fstat(fd, &st);
301 if (ret == 0 && S_ISREG(st.st_mode)) {
302 /*
303 * If the file handle is a regular file make sure the
304 * data is flushed to disk before signaling success.
305 */
306 ret = fsync(fd);
307 if (ret != 0) {
308 ret = -errno;
309 return ret;
310 }
311 }
312 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200313 if (fclose(s->stdio_file) == EOF) {
314 ret = -errno;
315 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500316 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200317 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200318}
aliguoria672b462008-11-11 21:33:36 +0000319
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200320static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200321 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200322 .get_buffer = stdio_get_buffer,
323 .close = stdio_pclose
324};
325
326static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200327 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200328 .put_buffer = stdio_put_buffer,
329 .close = stdio_pclose
330};
331
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100332QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200333{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100334 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200335 QEMUFileStdio *s;
336
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100337 stdio_file = popen(command, mode);
338 if (stdio_file == NULL) {
339 return NULL;
340 }
341
342 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000343 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
344 return NULL;
345 }
346
Anthony Liguori7267c092011-08-20 22:09:37 -0500347 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000348
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200349 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000350
351 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200352 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000353 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200354 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000355 }
aliguoria672b462008-11-11 21:33:36 +0000356 return s->file;
357}
358
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200359static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200360 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200361 .get_buffer = stdio_get_buffer,
362 .close = stdio_fclose
363};
364
365static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200366 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200367 .put_buffer = stdio_put_buffer,
368 .close = stdio_fclose
369};
370
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200371QEMUFile *qemu_fdopen(int fd, const char *mode)
372{
373 QEMUFileStdio *s;
374
375 if (mode == NULL ||
376 (mode[0] != 'r' && mode[0] != 'w') ||
377 mode[1] != 'b' || mode[2] != 0) {
378 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
379 return NULL;
380 }
381
Anthony Liguori7267c092011-08-20 22:09:37 -0500382 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200383 s->stdio_file = fdopen(fd, mode);
384 if (!s->stdio_file)
385 goto fail;
386
387 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200388 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200389 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200390 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200391 }
392 return s->file;
393
394fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500395 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200396 return NULL;
397}
398
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200399static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200400 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200401 .get_buffer = socket_get_buffer,
402 .close = socket_close
403};
404
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100405static const QEMUFileOps socket_write_ops = {
406 .get_fd = socket_get_fd,
407 .put_buffer = socket_put_buffer,
Orit Wasserman28085f72013-03-22 16:47:58 +0200408 .writev_buffer = socket_writev_buffer,
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100409 .close = socket_close
410};
411
412QEMUFile *qemu_fopen_socket(int fd, const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000413{
Anthony Liguori7267c092011-08-20 22:09:37 -0500414 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000415
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100416 if (mode == NULL ||
417 (mode[0] != 'r' && mode[0] != 'w') ||
418 mode[1] != 'b' || mode[2] != 0) {
419 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
420 return NULL;
421 }
422
aliguoria672b462008-11-11 21:33:36 +0000423 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100424 if (mode[0] == 'w') {
Paolo Bonzinif8bbc122013-02-22 17:36:41 +0100425 socket_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100426 s->file = qemu_fopen_ops(s, &socket_write_ops);
427 } else {
428 s->file = qemu_fopen_ops(s, &socket_read_ops);
429 }
aliguoria672b462008-11-11 21:33:36 +0000430 return s->file;
431}
432
aliguoria672b462008-11-11 21:33:36 +0000433QEMUFile *qemu_fopen(const char *filename, const char *mode)
434{
435 QEMUFileStdio *s;
436
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200437 if (mode == NULL ||
438 (mode[0] != 'r' && mode[0] != 'w') ||
439 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000440 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200441 return NULL;
442 }
443
Anthony Liguori7267c092011-08-20 22:09:37 -0500444 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000445
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200446 s->stdio_file = fopen(filename, mode);
447 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000448 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200449
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200450 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200451 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200452 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200453 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200454 }
455 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000456fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500457 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000458 return NULL;
459}
460
aliguori178e08a2009-04-05 19:10:55 +0000461static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000462 int64_t pos, int size)
463{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200464 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000465 return size;
466}
467
aliguori178e08a2009-04-05 19:10:55 +0000468static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000469{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200470 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000471}
472
473static int bdrv_fclose(void *opaque)
474{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200475 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000476}
477
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200478static const QEMUFileOps bdrv_read_ops = {
479 .get_buffer = block_get_buffer,
480 .close = bdrv_fclose
481};
482
483static const QEMUFileOps bdrv_write_ops = {
484 .put_buffer = block_put_buffer,
485 .close = bdrv_fclose
486};
487
Christoph Hellwig45566e92009-07-10 23:11:57 +0200488static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000489{
aliguoria672b462008-11-11 21:33:36 +0000490 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200491 return qemu_fopen_ops(bs, &bdrv_write_ops);
492 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000493}
494
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200495QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000496{
497 QEMUFile *f;
498
Anthony Liguori7267c092011-08-20 22:09:37 -0500499 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000500
501 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200502 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000503 f->is_write = 0;
aliguoria672b462008-11-11 21:33:36 +0000504 return f;
505}
506
Juan Quintela624b9cc2011-10-05 01:02:52 +0200507int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000508{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200509 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000510}
511
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100512static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000513{
Juan Quintelaafe41932013-01-14 13:36:28 +0100514 if (f->last_error == 0) {
515 f->last_error = ret;
516 }
aliguori4dabe242009-04-05 19:30:51 +0000517}
518
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200519/** Flushes QEMUFile buffer
520 *
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200521 */
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100522static void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000523{
Juan Quintela7311bea2012-08-29 19:08:59 +0200524 int ret = 0;
525
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100526 if (!f->ops->put_buffer) {
527 return;
528 }
aliguoria672b462008-11-11 21:33:36 +0000529 if (f->is_write && f->buf_index > 0) {
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100530 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Juan Quintela7311bea2012-08-29 19:08:59 +0200531 if (ret >= 0) {
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100532 f->pos += f->buf_index;
Juan Quintela7311bea2012-08-29 19:08:59 +0200533 }
aliguoria672b462008-11-11 21:33:36 +0000534 f->buf_index = 0;
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200535 f->iovcnt = 0;
aliguoria672b462008-11-11 21:33:36 +0000536 }
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100537 if (ret < 0) {
538 qemu_file_set_error(f, ret);
539 }
aliguoria672b462008-11-11 21:33:36 +0000540}
541
542static void qemu_fill_buffer(QEMUFile *f)
543{
544 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200545 int pending;
aliguoria672b462008-11-11 21:33:36 +0000546
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200547 if (!f->ops->get_buffer)
aliguoria672b462008-11-11 21:33:36 +0000548 return;
549
550 if (f->is_write)
551 abort();
552
Juan Quintela0046c452011-09-30 19:28:45 +0200553 pending = f->buf_size - f->buf_index;
554 if (pending > 0) {
555 memmove(f->buf, f->buf + f->buf_index, pending);
556 }
557 f->buf_index = 0;
558 f->buf_size = pending;
559
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100560 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200561 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000562 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200563 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100564 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200565 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200566 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000567 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200568 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000569}
570
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200571int qemu_get_fd(QEMUFile *f)
572{
573 if (f->ops->get_fd) {
574 return f->ops->get_fd(f->opaque);
575 }
576 return -1;
577}
578
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200579/** Closes the file
580 *
581 * Returns negative error value if any error happened on previous operations or
582 * while closing the file. Returns 0 or positive number on success.
583 *
584 * The meaning of return value on success depends on the specific backend
585 * being used.
586 */
587int qemu_fclose(QEMUFile *f)
588{
Juan Quintela29eee862012-08-29 19:14:54 +0200589 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100590 qemu_fflush(f);
591 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200592
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200593 if (f->ops->close) {
594 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200595 if (ret >= 0) {
596 ret = ret2;
597 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200598 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200599 /* If any error was spotted before closing, we should report it
600 * instead of the close() return value.
601 */
602 if (f->last_error) {
603 ret = f->last_error;
604 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500605 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000606 return ret;
607}
608
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200609static void add_to_iovec(QEMUFile *f, const uint8_t *buf, int size)
610{
611 /* check for adjacent buffer and coalesce them */
612 if (f->iovcnt > 0 && buf == f->iov[f->iovcnt - 1].iov_base +
613 f->iov[f->iovcnt - 1].iov_len) {
614 f->iov[f->iovcnt - 1].iov_len += size;
615 } else {
616 f->iov[f->iovcnt].iov_base = (uint8_t *)buf;
617 f->iov[f->iovcnt++].iov_len = size;
618 }
619}
620
aliguoria672b462008-11-11 21:33:36 +0000621void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
622{
623 int l;
624
Juan Quintelac10682c2012-08-29 19:43:39 +0200625 if (f->last_error) {
626 return;
627 }
628
629 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000630 fprintf(stderr,
631 "Attempted to write to buffer while read buffer is not empty\n");
632 abort();
633 }
634
Juan Quintelac10682c2012-08-29 19:43:39 +0200635 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000636 l = IO_BUF_SIZE - f->buf_index;
637 if (l > size)
638 l = size;
639 memcpy(f->buf + f->buf_index, buf, l);
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200640 add_to_iovec(f, f->buf + f->buf_index, l);
aliguoria672b462008-11-11 21:33:36 +0000641 f->is_write = 1;
642 f->buf_index += l;
Paolo Bonzini1964a392013-02-22 17:36:45 +0100643 f->bytes_xfer += l;
aliguoria672b462008-11-11 21:33:36 +0000644 buf += l;
645 size -= l;
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200646 if (f->buf_index >= IO_BUF_SIZE || f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100647 qemu_fflush(f);
648 if (qemu_file_get_error(f)) {
Juan Quintelac10682c2012-08-29 19:43:39 +0200649 break;
650 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200651 }
aliguoria672b462008-11-11 21:33:36 +0000652 }
653}
654
655void qemu_put_byte(QEMUFile *f, int v)
656{
Juan Quintelac10682c2012-08-29 19:43:39 +0200657 if (f->last_error) {
658 return;
659 }
660
661 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000662 fprintf(stderr,
663 "Attempted to write to buffer while read buffer is not empty\n");
664 abort();
665 }
666
667 f->buf[f->buf_index++] = v;
668 f->is_write = 1;
Orit Wasserman7d8a30b2013-03-22 16:47:59 +0200669 f->bytes_xfer++;
670
Orit Wassermanb3ea2bd2013-03-22 16:48:00 +0200671 add_to_iovec(f, f->buf + (f->buf_index - 1), 1);
672
673 if (f->buf_index >= IO_BUF_SIZE || f->iovcnt >= MAX_IOV_SIZE) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100674 qemu_fflush(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200675 }
aliguoria672b462008-11-11 21:33:36 +0000676}
677
Juan Quintelac6380722011-10-04 15:28:31 +0200678static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000679{
Juan Quintelac6380722011-10-04 15:28:31 +0200680 if (f->buf_index + size <= f->buf_size) {
681 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000682 }
aliguoria672b462008-11-11 21:33:36 +0000683}
684
Juan Quintelac6380722011-10-04 15:28:31 +0200685static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200686{
Juan Quintelac6380722011-10-04 15:28:31 +0200687 int pending;
688 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200689
Juan Quintelab9ce1452011-10-04 13:55:32 +0200690 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200691 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200692 }
Juan Quintela811814b2010-07-26 21:38:43 +0200693
Juan Quintelac6380722011-10-04 15:28:31 +0200694 index = f->buf_index + offset;
695 pending = f->buf_size - index;
696 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200697 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200698 index = f->buf_index + offset;
699 pending = f->buf_size - index;
700 }
701
702 if (pending <= 0) {
703 return 0;
704 }
705 if (size > pending) {
706 size = pending;
707 }
708
709 memcpy(buf, f->buf + index, size);
710 return size;
711}
712
713int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
714{
715 int pending = size;
716 int done = 0;
717
718 while (pending > 0) {
719 int res;
720
721 res = qemu_peek_buffer(f, buf, pending, 0);
722 if (res == 0) {
723 return done;
724 }
725 qemu_file_skip(f, res);
726 buf += res;
727 pending -= res;
728 done += res;
729 }
730 return done;
731}
732
733static int qemu_peek_byte(QEMUFile *f, int offset)
734{
735 int index = f->buf_index + offset;
736
737 if (f->is_write) {
738 abort();
739 }
740
741 if (index >= f->buf_size) {
742 qemu_fill_buffer(f);
743 index = f->buf_index + offset;
744 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200745 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200746 }
Juan Quintela811814b2010-07-26 21:38:43 +0200747 }
Juan Quintelac6380722011-10-04 15:28:31 +0200748 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200749}
750
aliguoria672b462008-11-11 21:33:36 +0000751int qemu_get_byte(QEMUFile *f)
752{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200753 int result;
aliguoria672b462008-11-11 21:33:36 +0000754
Juan Quintelac6380722011-10-04 15:28:31 +0200755 result = qemu_peek_byte(f, 0);
756 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200757 return result;
aliguoria672b462008-11-11 21:33:36 +0000758}
759
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100760int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000761{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100762 qemu_fflush(f);
763 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000764}
765
aliguoria672b462008-11-11 21:33:36 +0000766int qemu_file_rate_limit(QEMUFile *f)
767{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100768 if (qemu_file_get_error(f)) {
769 return 1;
770 }
771 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
772 return 1;
773 }
aliguoria672b462008-11-11 21:33:36 +0000774 return 0;
775}
776
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200777int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200778{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100779 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200780}
781
Paolo Bonzini1964a392013-02-22 17:36:45 +0100782void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400783{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100784 f->xfer_limit = limit;
785}
Glauber Costa19629532009-05-20 18:26:57 -0400786
Paolo Bonzini1964a392013-02-22 17:36:45 +0100787void qemu_file_reset_rate_limit(QEMUFile *f)
788{
789 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400790}
791
aliguoria672b462008-11-11 21:33:36 +0000792void qemu_put_be16(QEMUFile *f, unsigned int v)
793{
794 qemu_put_byte(f, v >> 8);
795 qemu_put_byte(f, v);
796}
797
798void qemu_put_be32(QEMUFile *f, unsigned int v)
799{
800 qemu_put_byte(f, v >> 24);
801 qemu_put_byte(f, v >> 16);
802 qemu_put_byte(f, v >> 8);
803 qemu_put_byte(f, v);
804}
805
806void qemu_put_be64(QEMUFile *f, uint64_t v)
807{
808 qemu_put_be32(f, v >> 32);
809 qemu_put_be32(f, v);
810}
811
812unsigned int qemu_get_be16(QEMUFile *f)
813{
814 unsigned int v;
815 v = qemu_get_byte(f) << 8;
816 v |= qemu_get_byte(f);
817 return v;
818}
819
820unsigned int qemu_get_be32(QEMUFile *f)
821{
822 unsigned int v;
823 v = qemu_get_byte(f) << 24;
824 v |= qemu_get_byte(f) << 16;
825 v |= qemu_get_byte(f) << 8;
826 v |= qemu_get_byte(f);
827 return v;
828}
829
830uint64_t qemu_get_be64(QEMUFile *f)
831{
832 uint64_t v;
833 v = (uint64_t)qemu_get_be32(f) << 32;
834 v |= qemu_get_be32(f);
835 return v;
836}
837
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200838
839/* timer */
840
841void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
842{
843 uint64_t expire_time;
844
845 expire_time = qemu_timer_expire_time_ns(ts);
846 qemu_put_be64(f, expire_time);
847}
848
849void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
850{
851 uint64_t expire_time;
852
853 expire_time = qemu_get_be64(f);
854 if (expire_time != -1) {
855 qemu_mod_timer_ns(ts, expire_time);
856 } else {
857 qemu_del_timer(ts);
858 }
859}
860
861
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100862/* bool */
863
864static int get_bool(QEMUFile *f, void *pv, size_t size)
865{
866 bool *v = pv;
867 *v = qemu_get_byte(f);
868 return 0;
869}
870
871static void put_bool(QEMUFile *f, void *pv, size_t size)
872{
873 bool *v = pv;
874 qemu_put_byte(f, *v);
875}
876
877const VMStateInfo vmstate_info_bool = {
878 .name = "bool",
879 .get = get_bool,
880 .put = put_bool,
881};
882
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200883/* 8 bit int */
884
885static int get_int8(QEMUFile *f, void *pv, size_t size)
886{
887 int8_t *v = pv;
888 qemu_get_s8s(f, v);
889 return 0;
890}
891
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200892static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200893{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200894 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200895 qemu_put_s8s(f, v);
896}
897
898const VMStateInfo vmstate_info_int8 = {
899 .name = "int8",
900 .get = get_int8,
901 .put = put_int8,
902};
903
904/* 16 bit int */
905
906static int get_int16(QEMUFile *f, void *pv, size_t size)
907{
908 int16_t *v = pv;
909 qemu_get_sbe16s(f, v);
910 return 0;
911}
912
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200913static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200914{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200915 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200916 qemu_put_sbe16s(f, v);
917}
918
919const VMStateInfo vmstate_info_int16 = {
920 .name = "int16",
921 .get = get_int16,
922 .put = put_int16,
923};
924
925/* 32 bit int */
926
927static int get_int32(QEMUFile *f, void *pv, size_t size)
928{
929 int32_t *v = pv;
930 qemu_get_sbe32s(f, v);
931 return 0;
932}
933
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200934static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200935{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200936 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200937 qemu_put_sbe32s(f, v);
938}
939
940const VMStateInfo vmstate_info_int32 = {
941 .name = "int32",
942 .get = get_int32,
943 .put = put_int32,
944};
945
Juan Quintela82501662009-08-20 19:42:32 +0200946/* 32 bit int. See that the received value is the same than the one
947 in the field */
948
949static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
950{
951 int32_t *v = pv;
952 int32_t v2;
953 qemu_get_sbe32s(f, &v2);
954
955 if (*v == v2)
956 return 0;
957 return -EINVAL;
958}
959
960const VMStateInfo vmstate_info_int32_equal = {
961 .name = "int32 equal",
962 .get = get_int32_equal,
963 .put = put_int32,
964};
965
Juan Quintela0a031e02009-08-20 19:42:37 +0200966/* 32 bit int. See that the received value is the less or the same
967 than the one in the field */
968
969static int get_int32_le(QEMUFile *f, void *pv, size_t size)
970{
971 int32_t *old = pv;
972 int32_t new;
973 qemu_get_sbe32s(f, &new);
974
975 if (*old <= new)
976 return 0;
977 return -EINVAL;
978}
979
980const VMStateInfo vmstate_info_int32_le = {
981 .name = "int32 equal",
982 .get = get_int32_le,
983 .put = put_int32,
984};
985
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200986/* 64 bit int */
987
988static int get_int64(QEMUFile *f, void *pv, size_t size)
989{
990 int64_t *v = pv;
991 qemu_get_sbe64s(f, v);
992 return 0;
993}
994
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200995static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200996{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200997 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200998 qemu_put_sbe64s(f, v);
999}
1000
1001const VMStateInfo vmstate_info_int64 = {
1002 .name = "int64",
1003 .get = get_int64,
1004 .put = put_int64,
1005};
1006
1007/* 8 bit unsigned int */
1008
1009static int get_uint8(QEMUFile *f, void *pv, size_t size)
1010{
1011 uint8_t *v = pv;
1012 qemu_get_8s(f, v);
1013 return 0;
1014}
1015
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001016static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001017{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001018 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001019 qemu_put_8s(f, v);
1020}
1021
1022const VMStateInfo vmstate_info_uint8 = {
1023 .name = "uint8",
1024 .get = get_uint8,
1025 .put = put_uint8,
1026};
1027
1028/* 16 bit unsigned int */
1029
1030static int get_uint16(QEMUFile *f, void *pv, size_t size)
1031{
1032 uint16_t *v = pv;
1033 qemu_get_be16s(f, v);
1034 return 0;
1035}
1036
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001037static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001038{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001039 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001040 qemu_put_be16s(f, v);
1041}
1042
1043const VMStateInfo vmstate_info_uint16 = {
1044 .name = "uint16",
1045 .get = get_uint16,
1046 .put = put_uint16,
1047};
1048
1049/* 32 bit unsigned int */
1050
1051static int get_uint32(QEMUFile *f, void *pv, size_t size)
1052{
1053 uint32_t *v = pv;
1054 qemu_get_be32s(f, v);
1055 return 0;
1056}
1057
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001058static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001059{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001060 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001061 qemu_put_be32s(f, v);
1062}
1063
1064const VMStateInfo vmstate_info_uint32 = {
1065 .name = "uint32",
1066 .get = get_uint32,
1067 .put = put_uint32,
1068};
1069
Juan Quintela9122a8f2011-03-10 12:33:48 +01001070/* 32 bit uint. See that the received value is the same than the one
1071 in the field */
1072
1073static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1074{
1075 uint32_t *v = pv;
1076 uint32_t v2;
1077 qemu_get_be32s(f, &v2);
1078
1079 if (*v == v2) {
1080 return 0;
1081 }
1082 return -EINVAL;
1083}
1084
1085const VMStateInfo vmstate_info_uint32_equal = {
1086 .name = "uint32 equal",
1087 .get = get_uint32_equal,
1088 .put = put_uint32,
1089};
1090
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001091/* 64 bit unsigned int */
1092
1093static int get_uint64(QEMUFile *f, void *pv, size_t size)
1094{
1095 uint64_t *v = pv;
1096 qemu_get_be64s(f, v);
1097 return 0;
1098}
1099
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001100static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001101{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001102 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001103 qemu_put_be64s(f, v);
1104}
1105
1106const VMStateInfo vmstate_info_uint64 = {
1107 .name = "uint64",
1108 .get = get_uint64,
1109 .put = put_uint64,
1110};
1111
David Gibsone344b8a2013-03-12 14:06:00 +11001112/* 64 bit unsigned int. See that the received value is the same than the one
1113 in the field */
1114
1115static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
1116{
1117 uint64_t *v = pv;
1118 uint64_t v2;
1119 qemu_get_be64s(f, &v2);
1120
1121 if (*v == v2) {
1122 return 0;
1123 }
1124 return -EINVAL;
1125}
1126
1127const VMStateInfo vmstate_info_uint64_equal = {
1128 .name = "int64 equal",
1129 .get = get_uint64_equal,
1130 .put = put_uint64,
1131};
1132
Juan Quintela80cd83e2009-09-10 03:04:36 +02001133/* 8 bit int. See that the received value is the same than the one
1134 in the field */
1135
1136static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1137{
1138 uint8_t *v = pv;
1139 uint8_t v2;
1140 qemu_get_8s(f, &v2);
1141
1142 if (*v == v2)
1143 return 0;
1144 return -EINVAL;
1145}
1146
1147const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001148 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001149 .get = get_uint8_equal,
1150 .put = put_uint8,
1151};
1152
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001153/* 16 bit unsigned int int. See that the received value is the same than the one
1154 in the field */
1155
1156static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1157{
1158 uint16_t *v = pv;
1159 uint16_t v2;
1160 qemu_get_be16s(f, &v2);
1161
1162 if (*v == v2)
1163 return 0;
1164 return -EINVAL;
1165}
1166
1167const VMStateInfo vmstate_info_uint16_equal = {
1168 .name = "uint16 equal",
1169 .get = get_uint16_equal,
1170 .put = put_uint16,
1171};
1172
David Gibson213945e2013-03-12 14:06:02 +11001173/* floating point */
1174
1175static int get_float64(QEMUFile *f, void *pv, size_t size)
1176{
1177 float64 *v = pv;
1178
1179 *v = make_float64(qemu_get_be64(f));
1180 return 0;
1181}
1182
1183static void put_float64(QEMUFile *f, void *pv, size_t size)
1184{
1185 uint64_t *v = pv;
1186
1187 qemu_put_be64(f, float64_val(*v));
1188}
1189
1190const VMStateInfo vmstate_info_float64 = {
1191 .name = "float64",
1192 .get = get_float64,
1193 .put = put_float64,
1194};
1195
Juan Quinteladde04632009-08-20 19:42:26 +02001196/* timers */
1197
1198static int get_timer(QEMUFile *f, void *pv, size_t size)
1199{
1200 QEMUTimer *v = pv;
1201 qemu_get_timer(f, v);
1202 return 0;
1203}
1204
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001205static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001206{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001207 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001208 qemu_put_timer(f, v);
1209}
1210
1211const VMStateInfo vmstate_info_timer = {
1212 .name = "timer",
1213 .get = get_timer,
1214 .put = put_timer,
1215};
1216
Juan Quintela6f67c502009-08-20 19:42:35 +02001217/* uint8_t buffers */
1218
1219static int get_buffer(QEMUFile *f, void *pv, size_t size)
1220{
1221 uint8_t *v = pv;
1222 qemu_get_buffer(f, v, size);
1223 return 0;
1224}
1225
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001226static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001227{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001228 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001229 qemu_put_buffer(f, v, size);
1230}
1231
1232const VMStateInfo vmstate_info_buffer = {
1233 .name = "buffer",
1234 .get = get_buffer,
1235 .put = put_buffer,
1236};
1237
Juan Quintela76507c72009-10-19 15:46:28 +02001238/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001239 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001240
1241static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1242{
Jan Kiszka21174c32009-12-02 12:36:35 +01001243 uint8_t buf[1024];
1244 int block_len;
1245
1246 while (size > 0) {
1247 block_len = MIN(sizeof(buf), size);
1248 size -= block_len;
1249 qemu_get_buffer(f, buf, block_len);
1250 }
1251 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001252}
1253
1254static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1255{
Jan Kiszka21174c32009-12-02 12:36:35 +01001256 static const uint8_t buf[1024];
1257 int block_len;
1258
1259 while (size > 0) {
1260 block_len = MIN(sizeof(buf), size);
1261 size -= block_len;
1262 qemu_put_buffer(f, buf, block_len);
1263 }
Juan Quintela76507c72009-10-19 15:46:28 +02001264}
1265
1266const VMStateInfo vmstate_info_unused_buffer = {
1267 .name = "unused_buffer",
1268 .get = get_unused_buffer,
1269 .put = put_unused_buffer,
1270};
1271
Peter Maydell08e99e22012-10-30 07:45:12 +00001272/* bitmaps (as defined by bitmap.h). Note that size here is the size
1273 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1274 * bit words with the bits in big endian order. The in-memory format
1275 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1276 */
1277/* This is the number of 64 bit words sent over the wire */
1278#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1279static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1280{
1281 unsigned long *bmp = pv;
1282 int i, idx = 0;
1283 for (i = 0; i < BITS_TO_U64S(size); i++) {
1284 uint64_t w = qemu_get_be64(f);
1285 bmp[idx++] = w;
1286 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1287 bmp[idx++] = w >> 32;
1288 }
1289 }
1290 return 0;
1291}
1292
1293static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1294{
1295 unsigned long *bmp = pv;
1296 int i, idx = 0;
1297 for (i = 0; i < BITS_TO_U64S(size); i++) {
1298 uint64_t w = bmp[idx++];
1299 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1300 w |= ((uint64_t)bmp[idx++]) << 32;
1301 }
1302 qemu_put_be64(f, w);
1303 }
1304}
1305
1306const VMStateInfo vmstate_info_bitmap = {
1307 .name = "bitmap",
1308 .get = get_bitmap,
1309 .put = put_bitmap,
1310};
1311
Alex Williamson7685ee62010-06-25 11:09:14 -06001312typedef struct CompatEntry {
1313 char idstr[256];
1314 int instance_id;
1315} CompatEntry;
1316
aliguoria672b462008-11-11 21:33:36 +00001317typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001318 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001319 char idstr[256];
1320 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001321 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001322 int version_id;
1323 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001324 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001325 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001326 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001327 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001328 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001329 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001330} SaveStateEntry;
1331
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001332
Blue Swirl72cf2d42009-09-12 07:36:22 +00001333static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1334 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001335static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001336
Juan Quintela8718e992009-09-01 02:12:31 +02001337static int calculate_new_instance_id(const char *idstr)
1338{
1339 SaveStateEntry *se;
1340 int instance_id = 0;
1341
Blue Swirl72cf2d42009-09-12 07:36:22 +00001342 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001343 if (strcmp(idstr, se->idstr) == 0
1344 && instance_id <= se->instance_id) {
1345 instance_id = se->instance_id + 1;
1346 }
1347 }
1348 return instance_id;
1349}
1350
Alex Williamson7685ee62010-06-25 11:09:14 -06001351static int calculate_compat_instance_id(const char *idstr)
1352{
1353 SaveStateEntry *se;
1354 int instance_id = 0;
1355
1356 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1357 if (!se->compat)
1358 continue;
1359
1360 if (strcmp(idstr, se->compat->idstr) == 0
1361 && instance_id <= se->compat->instance_id) {
1362 instance_id = se->compat->instance_id + 1;
1363 }
1364 }
1365 return instance_id;
1366}
1367
aliguoria672b462008-11-11 21:33:36 +00001368/* TODO: Individual devices generally have very little idea about the rest
1369 of the system, so instance_id should be removed/replaced.
1370 Meanwhile pass -1 as instance_id if you do not already have a clearly
1371 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001372int register_savevm_live(DeviceState *dev,
1373 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001374 int instance_id,
1375 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001376 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001377 void *opaque)
1378{
Juan Quintela8718e992009-09-01 02:12:31 +02001379 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001380
Anthony Liguori7267c092011-08-20 22:09:37 -05001381 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001382 se->version_id = version_id;
1383 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001384 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001385 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001386 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001387 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001388 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001389 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001390 se->is_ram = 1;
1391 }
aliguoria672b462008-11-11 21:33:36 +00001392
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001393 if (dev) {
1394 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001395 if (id) {
1396 pstrcpy(se->idstr, sizeof(se->idstr), id);
1397 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001398 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001399
Anthony Liguori7267c092011-08-20 22:09:37 -05001400 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001401 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1402 se->compat->instance_id = instance_id == -1 ?
1403 calculate_compat_instance_id(idstr) : instance_id;
1404 instance_id = -1;
1405 }
1406 }
1407 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1408
Juan Quintela8718e992009-09-01 02:12:31 +02001409 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001410 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001411 } else {
1412 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001413 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001414 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001415 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001416 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001417 return 0;
1418}
1419
Alex Williamson0be71e32010-06-25 11:09:07 -06001420int register_savevm(DeviceState *dev,
1421 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001422 int instance_id,
1423 int version_id,
1424 SaveStateHandler *save_state,
1425 LoadStateHandler *load_state,
1426 void *opaque)
1427{
Juan Quintela7908c782012-06-26 18:46:10 +02001428 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1429 ops->save_state = save_state;
1430 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001431 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001432 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001433}
1434
Alex Williamson0be71e32010-06-25 11:09:07 -06001435void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001436{
Juan Quintela8718e992009-09-01 02:12:31 +02001437 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001438 char id[256] = "";
1439
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001440 if (dev) {
1441 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001442 if (path) {
1443 pstrcpy(id, sizeof(id), path);
1444 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001445 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001446 }
1447 }
1448 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001449
Blue Swirl72cf2d42009-09-12 07:36:22 +00001450 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001451 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001452 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001453 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001454 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001455 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001456 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001457 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001458 }
aliguori41bd13a2009-04-17 17:10:59 +00001459 }
1460}
1461
Alex Williamson0be71e32010-06-25 11:09:07 -06001462int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001463 const VMStateDescription *vmsd,
1464 void *opaque, int alias_id,
1465 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001466{
Juan Quintela8718e992009-09-01 02:12:31 +02001467 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001468
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001469 /* If this triggers, alias support can be dropped for the vmsd. */
1470 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1471
Anthony Liguori7267c092011-08-20 22:09:37 -05001472 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001473 se->version_id = vmsd->version_id;
1474 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001475 se->opaque = opaque;
1476 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001477 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001478 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001479
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001480 if (dev) {
1481 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001482 if (id) {
1483 pstrcpy(se->idstr, sizeof(se->idstr), id);
1484 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001485 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001486
Anthony Liguori7267c092011-08-20 22:09:37 -05001487 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001488 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1489 se->compat->instance_id = instance_id == -1 ?
1490 calculate_compat_instance_id(vmsd->name) : instance_id;
1491 instance_id = -1;
1492 }
1493 }
1494 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1495
Juan Quintela8718e992009-09-01 02:12:31 +02001496 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001497 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001498 } else {
1499 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001500 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001501 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001502 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001503 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001504 return 0;
1505}
1506
Alex Williamson0be71e32010-06-25 11:09:07 -06001507void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1508 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001509{
Juan Quintela1eb75382009-09-10 03:04:29 +02001510 SaveStateEntry *se, *new_se;
1511
Blue Swirl72cf2d42009-09-12 07:36:22 +00001512 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001513 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001514 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001515 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001516 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001517 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001518 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001519 }
1520 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001521}
1522
Juan Quintela811814b2010-07-26 21:38:43 +02001523static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1524 void *opaque);
1525static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1526 void *opaque);
1527
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001528int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1529 void *opaque, int version_id)
1530{
1531 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001532 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001533
1534 if (version_id > vmsd->version_id) {
1535 return -EINVAL;
1536 }
1537 if (version_id < vmsd->minimum_version_id_old) {
1538 return -EINVAL;
1539 }
1540 if (version_id < vmsd->minimum_version_id) {
1541 return vmsd->load_state_old(f, opaque, version_id);
1542 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001543 if (vmsd->pre_load) {
1544 int ret = vmsd->pre_load(opaque);
1545 if (ret)
1546 return ret;
1547 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001548 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001549 if ((field->field_exists &&
1550 field->field_exists(opaque, version_id)) ||
1551 (!field->field_exists &&
1552 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001553 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001554 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001555 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001556
Juan Quintelae61a1e02009-12-02 12:36:38 +01001557 if (field->flags & VMS_VBUFFER) {
1558 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001559 if (field->flags & VMS_MULTIPLY) {
1560 size *= field->size;
1561 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001562 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001563 if (field->flags & VMS_ARRAY) {
1564 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001565 } else if (field->flags & VMS_VARRAY_INT32) {
1566 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001567 } else if (field->flags & VMS_VARRAY_UINT32) {
1568 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001569 } else if (field->flags & VMS_VARRAY_UINT16) {
1570 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001571 } else if (field->flags & VMS_VARRAY_UINT8) {
1572 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001573 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001574 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001575 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001576 }
1577 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001578 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001579
Juan Quintela19df4382009-09-29 22:48:41 +02001580 if (field->flags & VMS_ARRAY_OF_POINTER) {
1581 addr = *(void **)addr;
1582 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001583 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001584 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001585 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001586 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001587
1588 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001589 if (ret < 0) {
1590 return ret;
1591 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001592 }
1593 }
1594 field++;
1595 }
Juan Quintela811814b2010-07-26 21:38:43 +02001596 ret = vmstate_subsection_load(f, vmsd, opaque);
1597 if (ret != 0) {
1598 return ret;
1599 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001600 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001601 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001602 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001603 return 0;
1604}
1605
1606void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001607 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001608{
1609 VMStateField *field = vmsd->fields;
1610
Juan Quintela8fb07912009-09-10 03:04:32 +02001611 if (vmsd->pre_save) {
1612 vmsd->pre_save(opaque);
1613 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001614 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001615 if (!field->field_exists ||
1616 field->field_exists(opaque, vmsd->version_id)) {
1617 void *base_addr = opaque + field->offset;
1618 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001619 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001620
Juan Quintelae61a1e02009-12-02 12:36:38 +01001621 if (field->flags & VMS_VBUFFER) {
1622 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001623 if (field->flags & VMS_MULTIPLY) {
1624 size *= field->size;
1625 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001626 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001627 if (field->flags & VMS_ARRAY) {
1628 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001629 } else if (field->flags & VMS_VARRAY_INT32) {
1630 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001631 } else if (field->flags & VMS_VARRAY_UINT32) {
1632 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001633 } else if (field->flags & VMS_VARRAY_UINT16) {
1634 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001635 } else if (field->flags & VMS_VARRAY_UINT8) {
1636 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001637 }
1638 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001639 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001640 }
1641 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001642 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001643
Juan Quintela85953872009-12-02 12:36:37 +01001644 if (field->flags & VMS_ARRAY_OF_POINTER) {
1645 addr = *(void **)addr;
1646 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001647 if (field->flags & VMS_STRUCT) {
1648 vmstate_save_state(f, field->vmsd, addr);
1649 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001650 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001651 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001652 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001653 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001654 field++;
1655 }
Juan Quintela811814b2010-07-26 21:38:43 +02001656 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001657}
1658
Juan Quintela4082be42009-08-20 19:42:24 +02001659static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1660{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001661 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001662 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001663 }
1664 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001665}
1666
Alex Williamsondc912122011-01-11 14:39:43 -07001667static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001668{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001669 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001670 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001671 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001672 }
1673 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001674}
1675
aliguoria672b462008-11-11 21:33:36 +00001676#define QEMU_VM_FILE_MAGIC 0x5145564d
1677#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1678#define QEMU_VM_FILE_VERSION 0x00000003
1679
1680#define QEMU_VM_EOF 0x00
1681#define QEMU_VM_SECTION_START 0x01
1682#define QEMU_VM_SECTION_PART 0x02
1683#define QEMU_VM_SECTION_END 0x03
1684#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001685#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001686
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001687bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001688{
1689 SaveStateEntry *se;
1690
1691 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1692 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001693 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001694 return true;
1695 }
1696 }
1697 return false;
1698}
1699
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001700void qemu_savevm_state_begin(QEMUFile *f,
1701 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001702{
1703 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001704 int ret;
aliguoria672b462008-11-11 21:33:36 +00001705
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001706 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001707 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001708 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001709 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001710 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001711 }
1712
aliguoria672b462008-11-11 21:33:36 +00001713 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1714 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1715
Blue Swirl72cf2d42009-09-12 07:36:22 +00001716 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001717 int len;
1718
Juan Quintelad1315aa2012-06-28 15:11:57 +02001719 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001720 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001721 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001722 if (se->ops && se->ops->is_active) {
1723 if (!se->ops->is_active(se->opaque)) {
1724 continue;
1725 }
1726 }
aliguoria672b462008-11-11 21:33:36 +00001727 /* Section type */
1728 qemu_put_byte(f, QEMU_VM_SECTION_START);
1729 qemu_put_be32(f, se->section_id);
1730
1731 /* ID string */
1732 len = strlen(se->idstr);
1733 qemu_put_byte(f, len);
1734 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1735
1736 qemu_put_be32(f, se->instance_id);
1737 qemu_put_be32(f, se->version_id);
1738
Juan Quintelad1315aa2012-06-28 15:11:57 +02001739 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001740 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001741 qemu_file_set_error(f, ret);
1742 break;
Juan Quintela29757252011-10-19 15:22:18 +02001743 }
aliguoria672b462008-11-11 21:33:36 +00001744 }
aliguoria672b462008-11-11 21:33:36 +00001745}
1746
Juan Quintela39346382011-09-22 11:02:14 +02001747/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001748 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001749 * negative: there was one error, and we have -errno.
1750 * 0 : We haven't finished, caller have to go again
1751 * 1 : We have finished, we can go to complete phase
1752 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001753int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001754{
1755 SaveStateEntry *se;
1756 int ret = 1;
1757
Blue Swirl72cf2d42009-09-12 07:36:22 +00001758 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001759 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001760 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001761 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001762 if (se->ops && se->ops->is_active) {
1763 if (!se->ops->is_active(se->opaque)) {
1764 continue;
1765 }
1766 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001767 if (qemu_file_rate_limit(f)) {
1768 return 0;
1769 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001770 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001771 /* Section type */
1772 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1773 qemu_put_be32(f, se->section_id);
1774
Juan Quintela16310a32012-06-28 15:31:37 +02001775 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001776 trace_savevm_section_end(se->section_id);
1777
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001778 if (ret < 0) {
1779 qemu_file_set_error(f, ret);
1780 }
Juan Quintela29757252011-10-19 15:22:18 +02001781 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001782 /* Do not proceed to the next vmstate before this one reported
1783 completion of the current stage. This serializes the migration
1784 and reduces the probability that a faster changing state is
1785 synchronized over and over again. */
1786 break;
1787 }
aliguoria672b462008-11-11 21:33:36 +00001788 }
Juan Quintela39346382011-09-22 11:02:14 +02001789 return ret;
aliguoria672b462008-11-11 21:33:36 +00001790}
1791
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001792void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001793{
1794 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001795 int ret;
aliguoria672b462008-11-11 21:33:36 +00001796
Jan Kiszkaea375f92010-03-01 19:10:30 +01001797 cpu_synchronize_all_states();
1798
Blue Swirl72cf2d42009-09-12 07:36:22 +00001799 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001800 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001801 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001802 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001803 if (se->ops && se->ops->is_active) {
1804 if (!se->ops->is_active(se->opaque)) {
1805 continue;
1806 }
1807 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001808 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001809 /* Section type */
1810 qemu_put_byte(f, QEMU_VM_SECTION_END);
1811 qemu_put_be32(f, se->section_id);
1812
Juan Quintela16310a32012-06-28 15:31:37 +02001813 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001814 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001815 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001816 qemu_file_set_error(f, ret);
1817 return;
Juan Quintela29757252011-10-19 15:22:18 +02001818 }
aliguoria672b462008-11-11 21:33:36 +00001819 }
1820
Blue Swirl72cf2d42009-09-12 07:36:22 +00001821 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001822 int len;
1823
Juan Quintela22ea40f2012-06-26 17:19:10 +02001824 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001825 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001826 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001827 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001828 /* Section type */
1829 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1830 qemu_put_be32(f, se->section_id);
1831
1832 /* ID string */
1833 len = strlen(se->idstr);
1834 qemu_put_byte(f, len);
1835 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1836
1837 qemu_put_be32(f, se->instance_id);
1838 qemu_put_be32(f, se->version_id);
1839
Alex Williamsondc912122011-01-11 14:39:43 -07001840 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001841 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001842 }
1843
1844 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001845 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001846}
1847
Juan Quintelae4ed1542012-09-21 11:18:18 +02001848uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1849{
1850 SaveStateEntry *se;
1851 uint64_t ret = 0;
1852
1853 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1854 if (!se->ops || !se->ops->save_live_pending) {
1855 continue;
1856 }
1857 if (se->ops && se->ops->is_active) {
1858 if (!se->ops->is_active(se->opaque)) {
1859 continue;
1860 }
1861 }
1862 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1863 }
1864 return ret;
1865}
1866
Juan Quintela65227732013-01-14 14:14:42 +01001867void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001868{
1869 SaveStateEntry *se;
1870
1871 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001872 if (se->ops && se->ops->cancel) {
1873 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001874 }
1875 }
1876}
1877
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001878static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001879{
aliguoria672b462008-11-11 21:33:36 +00001880 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001881 MigrationParams params = {
1882 .blk = 0,
1883 .shared = 0
1884 };
aliguoria672b462008-11-11 21:33:36 +00001885
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001886 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001887 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07001888 }
1889
Paolo Bonzini9b095032013-02-22 17:36:28 +01001890 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001891 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001892 qemu_mutex_lock_iothread();
1893
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001894 while (qemu_file_get_error(f) == 0) {
1895 if (qemu_savevm_state_iterate(f) > 0) {
1896 break;
1897 }
1898 }
aliguoria672b462008-11-11 21:33:36 +00001899
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001900 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001901 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001902 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001903 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001904 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001905 if (ret != 0) {
1906 qemu_savevm_state_cancel();
1907 }
aliguoria672b462008-11-11 21:33:36 +00001908 return ret;
1909}
1910
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001911static int qemu_save_device_state(QEMUFile *f)
1912{
1913 SaveStateEntry *se;
1914
1915 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1916 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1917
1918 cpu_synchronize_all_states();
1919
1920 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1921 int len;
1922
1923 if (se->is_ram) {
1924 continue;
1925 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001926 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001927 continue;
1928 }
1929
1930 /* Section type */
1931 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1932 qemu_put_be32(f, se->section_id);
1933
1934 /* ID string */
1935 len = strlen(se->idstr);
1936 qemu_put_byte(f, len);
1937 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1938
1939 qemu_put_be32(f, se->instance_id);
1940 qemu_put_be32(f, se->version_id);
1941
1942 vmstate_save(f, se);
1943 }
1944
1945 qemu_put_byte(f, QEMU_VM_EOF);
1946
1947 return qemu_file_get_error(f);
1948}
1949
aliguoria672b462008-11-11 21:33:36 +00001950static SaveStateEntry *find_se(const char *idstr, int instance_id)
1951{
1952 SaveStateEntry *se;
1953
Blue Swirl72cf2d42009-09-12 07:36:22 +00001954 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001955 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001956 (instance_id == se->instance_id ||
1957 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001958 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001959 /* Migrating from an older version? */
1960 if (strstr(se->idstr, idstr) && se->compat) {
1961 if (!strcmp(se->compat->idstr, idstr) &&
1962 (instance_id == se->compat->instance_id ||
1963 instance_id == se->alias_id))
1964 return se;
1965 }
aliguoria672b462008-11-11 21:33:36 +00001966 }
1967 return NULL;
1968}
1969
Juan Quintela811814b2010-07-26 21:38:43 +02001970static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1971{
1972 while(sub && sub->needed) {
1973 if (strcmp(idstr, sub->vmsd->name) == 0) {
1974 return sub->vmsd;
1975 }
1976 sub++;
1977 }
1978 return NULL;
1979}
1980
1981static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1982 void *opaque)
1983{
Juan Quintelac6380722011-10-04 15:28:31 +02001984 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001985 char idstr[256];
1986 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001987 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001988 const VMStateDescription *sub_vmsd;
1989
Juan Quintelac6380722011-10-04 15:28:31 +02001990 len = qemu_peek_byte(f, 1);
1991 if (len < strlen(vmsd->name) + 1) {
1992 /* subsection name has be be "section_name/a" */
1993 return 0;
1994 }
1995 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1996 if (size != len) {
1997 return 0;
1998 }
1999 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02002000
Juan Quintelac6380722011-10-04 15:28:31 +02002001 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
2002 /* it don't have a valid subsection name */
2003 return 0;
2004 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02002005 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02002006 if (sub_vmsd == NULL) {
2007 return -ENOENT;
2008 }
Juan Quintelac6380722011-10-04 15:28:31 +02002009 qemu_file_skip(f, 1); /* subsection */
2010 qemu_file_skip(f, 1); /* len */
2011 qemu_file_skip(f, len); /* idstr */
2012 version_id = qemu_get_be32(f);
2013
Juan Quintela811814b2010-07-26 21:38:43 +02002014 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
2015 if (ret) {
2016 return ret;
2017 }
2018 }
2019 return 0;
2020}
2021
2022static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
2023 void *opaque)
2024{
2025 const VMStateSubsection *sub = vmsd->subsections;
2026
2027 while (sub && sub->needed) {
2028 if (sub->needed(opaque)) {
2029 const VMStateDescription *vmsd = sub->vmsd;
2030 uint8_t len;
2031
2032 qemu_put_byte(f, QEMU_VM_SUBSECTION);
2033 len = strlen(vmsd->name);
2034 qemu_put_byte(f, len);
2035 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
2036 qemu_put_be32(f, vmsd->version_id);
2037 vmstate_save_state(f, vmsd, opaque);
2038 }
2039 sub++;
2040 }
2041}
2042
aliguoria672b462008-11-11 21:33:36 +00002043typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00002044 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00002045 SaveStateEntry *se;
2046 int section_id;
2047 int version_id;
aliguoria672b462008-11-11 21:33:36 +00002048} LoadStateEntry;
2049
aliguoria672b462008-11-11 21:33:36 +00002050int qemu_loadvm_state(QEMUFile *f)
2051{
Blue Swirl72cf2d42009-09-12 07:36:22 +00002052 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
2053 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002054 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00002055 uint8_t section_type;
2056 unsigned int v;
2057 int ret;
2058
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002059 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07002060 return -EINVAL;
2061 }
2062
aliguoria672b462008-11-11 21:33:36 +00002063 v = qemu_get_be32(f);
2064 if (v != QEMU_VM_FILE_MAGIC)
2065 return -EINVAL;
2066
2067 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02002068 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
2069 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
2070 return -ENOTSUP;
2071 }
aliguoria672b462008-11-11 21:33:36 +00002072 if (v != QEMU_VM_FILE_VERSION)
2073 return -ENOTSUP;
2074
2075 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2076 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002077 SaveStateEntry *se;
2078 char idstr[257];
2079 int len;
2080
2081 switch (section_type) {
2082 case QEMU_VM_SECTION_START:
2083 case QEMU_VM_SECTION_FULL:
2084 /* Read section start */
2085 section_id = qemu_get_be32(f);
2086 len = qemu_get_byte(f);
2087 qemu_get_buffer(f, (uint8_t *)idstr, len);
2088 idstr[len] = 0;
2089 instance_id = qemu_get_be32(f);
2090 version_id = qemu_get_be32(f);
2091
2092 /* Find savevm section */
2093 se = find_se(idstr, instance_id);
2094 if (se == NULL) {
2095 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2096 ret = -EINVAL;
2097 goto out;
2098 }
2099
2100 /* Validate version */
2101 if (version_id > se->version_id) {
2102 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2103 version_id, idstr, se->version_id);
2104 ret = -EINVAL;
2105 goto out;
2106 }
2107
2108 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002109 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002110
2111 le->se = se;
2112 le->section_id = section_id;
2113 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002114 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002115
Juan Quintela4082be42009-08-20 19:42:24 +02002116 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002117 if (ret < 0) {
2118 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2119 instance_id, idstr);
2120 goto out;
2121 }
aliguoria672b462008-11-11 21:33:36 +00002122 break;
2123 case QEMU_VM_SECTION_PART:
2124 case QEMU_VM_SECTION_END:
2125 section_id = qemu_get_be32(f);
2126
Blue Swirl72cf2d42009-09-12 07:36:22 +00002127 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002128 if (le->section_id == section_id) {
2129 break;
2130 }
2131 }
aliguoria672b462008-11-11 21:33:36 +00002132 if (le == NULL) {
2133 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2134 ret = -EINVAL;
2135 goto out;
2136 }
2137
Juan Quintela4082be42009-08-20 19:42:24 +02002138 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002139 if (ret < 0) {
2140 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2141 section_id);
2142 goto out;
2143 }
aliguoria672b462008-11-11 21:33:36 +00002144 break;
2145 default:
2146 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2147 ret = -EINVAL;
2148 goto out;
2149 }
2150 }
2151
Jan Kiszkaea375f92010-03-01 19:10:30 +01002152 cpu_synchronize_all_post_init();
2153
aliguoria672b462008-11-11 21:33:36 +00002154 ret = 0;
2155
2156out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002157 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2158 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002159 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002160 }
2161
Juan Quintela42802d42011-10-05 01:14:46 +02002162 if (ret == 0) {
2163 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002164 }
aliguoria672b462008-11-11 21:33:36 +00002165
2166 return ret;
2167}
2168
aliguoria672b462008-11-11 21:33:36 +00002169static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2170 const char *name)
2171{
2172 QEMUSnapshotInfo *sn_tab, *sn;
2173 int nb_sns, i, ret;
2174
2175 ret = -ENOENT;
2176 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2177 if (nb_sns < 0)
2178 return ret;
2179 for(i = 0; i < nb_sns; i++) {
2180 sn = &sn_tab[i];
2181 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2182 *sn_info = *sn;
2183 ret = 0;
2184 break;
2185 }
2186 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002187 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002188 return ret;
2189}
2190
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002191/*
2192 * Deletes snapshots of a given name in all opened images.
2193 */
2194static int del_existing_snapshots(Monitor *mon, const char *name)
2195{
2196 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002197 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2198 int ret;
2199
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002200 bs = NULL;
2201 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002202 if (bdrv_can_snapshot(bs) &&
2203 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2204 {
2205 ret = bdrv_snapshot_delete(bs, name);
2206 if (ret < 0) {
2207 monitor_printf(mon,
2208 "Error while deleting snapshot on '%s'\n",
2209 bdrv_get_device_name(bs));
2210 return -1;
2211 }
2212 }
2213 }
2214
2215 return 0;
2216}
2217
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002218void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002219{
2220 BlockDriverState *bs, *bs1;
2221 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002222 int ret;
aliguoria672b462008-11-11 21:33:36 +00002223 QEMUFile *f;
2224 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002225 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002226 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002227 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002228 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002229
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002230 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002231 bs = NULL;
2232 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002233
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002234 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002235 continue;
2236 }
2237
2238 if (!bdrv_can_snapshot(bs)) {
2239 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2240 bdrv_get_device_name(bs));
2241 return;
2242 }
2243 }
2244
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002245 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002246 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002247 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002248 return;
2249 }
aliguoria672b462008-11-11 21:33:36 +00002250
Luiz Capitulino13548692011-07-29 15:36:43 -03002251 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002252 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002253
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002254 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002255
2256 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002257 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002258 sn->date_sec = tv.tv_sec;
2259 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002260 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002261
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002262 if (name) {
2263 ret = bdrv_snapshot_find(bs, old_sn, name);
2264 if (ret >= 0) {
2265 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2266 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2267 } else {
2268 pstrcpy(sn->name, sizeof(sn->name), name);
2269 }
2270 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002271 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2272 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002273 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002274 }
2275
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002276 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002277 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002278 goto the_end;
2279 }
2280
aliguoria672b462008-11-11 21:33:36 +00002281 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002282 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002283 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002284 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002285 goto the_end;
2286 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002287 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002288 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002289 qemu_fclose(f);
2290 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002291 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002292 goto the_end;
2293 }
2294
2295 /* create the snapshots */
2296
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002297 bs1 = NULL;
2298 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002299 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002300 /* Write VM state size only to the image that contains the state */
2301 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002302 ret = bdrv_snapshot_create(bs1, sn);
2303 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002304 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2305 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002306 }
2307 }
2308 }
2309
2310 the_end:
2311 if (saved_vm_running)
2312 vm_start();
2313}
2314
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002315void qmp_xen_save_devices_state(const char *filename, Error **errp)
2316{
2317 QEMUFile *f;
2318 int saved_vm_running;
2319 int ret;
2320
2321 saved_vm_running = runstate_is_running();
2322 vm_stop(RUN_STATE_SAVE_VM);
2323
2324 f = qemu_fopen(filename, "wb");
2325 if (!f) {
2326 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2327 goto the_end;
2328 }
2329 ret = qemu_save_device_state(f);
2330 qemu_fclose(f);
2331 if (ret < 0) {
2332 error_set(errp, QERR_IO_ERROR);
2333 }
2334
2335 the_end:
2336 if (saved_vm_running)
2337 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002338}
2339
Markus Armbruster03cd4652010-02-17 16:24:10 +01002340int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002341{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002342 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002343 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002344 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002345 int ret;
aliguoria672b462008-11-11 21:33:36 +00002346
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002347 bs_vm_state = bdrv_snapshots();
2348 if (!bs_vm_state) {
2349 error_report("No block device supports snapshots");
2350 return -ENOTSUP;
2351 }
2352
2353 /* Don't even try to load empty VM states */
2354 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2355 if (ret < 0) {
2356 return ret;
2357 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002358 error_report("This is a disk-only snapshot. Revert to it offline "
2359 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002360 return -EINVAL;
2361 }
2362
2363 /* Verify if there is any device that doesn't support snapshots and is
2364 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002365 bs = NULL;
2366 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002367
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002368 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002369 continue;
2370 }
2371
2372 if (!bdrv_can_snapshot(bs)) {
2373 error_report("Device '%s' is writable but does not support snapshots.",
2374 bdrv_get_device_name(bs));
2375 return -ENOTSUP;
2376 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002377
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002378 ret = bdrv_snapshot_find(bs, &sn, name);
2379 if (ret < 0) {
2380 error_report("Device '%s' does not have the requested snapshot '%s'",
2381 bdrv_get_device_name(bs), name);
2382 return ret;
2383 }
aliguoria672b462008-11-11 21:33:36 +00002384 }
2385
2386 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002387 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002388
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002389 bs = NULL;
2390 while ((bs = bdrv_next(bs))) {
2391 if (bdrv_can_snapshot(bs)) {
2392 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002393 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002394 error_report("Error %d while activating snapshot '%s' on '%s'",
2395 ret, name, bdrv_get_device_name(bs));
2396 return ret;
aliguoria672b462008-11-11 21:33:36 +00002397 }
2398 }
2399 }
2400
aliguoria672b462008-11-11 21:33:36 +00002401 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002402 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002403 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002404 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002405 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002406 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002407
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002408 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002409 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002410
aliguoria672b462008-11-11 21:33:36 +00002411 qemu_fclose(f);
2412 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002413 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002414 return ret;
aliguoria672b462008-11-11 21:33:36 +00002415 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002416
Juan Quintela05f24012009-08-20 19:42:22 +02002417 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002418}
2419
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002420void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002421{
2422 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002423 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002424 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002425
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002426 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002427 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002428 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002429 return;
2430 }
2431
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002432 bs1 = NULL;
2433 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002434 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002435 ret = bdrv_snapshot_delete(bs1, name);
2436 if (ret < 0) {
2437 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002438 monitor_printf(mon,
2439 "Snapshots not supported on device '%s'\n",
2440 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002441 else
aliguori376253e2009-03-05 23:01:23 +00002442 monitor_printf(mon, "Error %d while deleting snapshot on "
2443 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002444 }
2445 }
2446 }
2447}
2448
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002449void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002450{
2451 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002452 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2453 int nb_sns, i, ret, available;
2454 int total;
2455 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002456 char buf[256];
2457
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002458 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002459 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002460 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002461 return;
2462 }
aliguoria672b462008-11-11 21:33:36 +00002463
2464 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2465 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002466 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002467 return;
2468 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002469
2470 if (nb_sns == 0) {
2471 monitor_printf(mon, "There is no snapshot available.\n");
2472 return;
aliguoria672b462008-11-11 21:33:36 +00002473 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002474
Anthony Liguori7267c092011-08-20 22:09:37 -05002475 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002476 total = 0;
2477 for (i = 0; i < nb_sns; i++) {
2478 sn = &sn_tab[i];
2479 available = 1;
2480 bs1 = NULL;
2481
2482 while ((bs1 = bdrv_next(bs1))) {
2483 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2484 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2485 if (ret < 0) {
2486 available = 0;
2487 break;
2488 }
2489 }
2490 }
2491
2492 if (available) {
2493 available_snapshots[total] = i;
2494 total++;
2495 }
2496 }
2497
2498 if (total > 0) {
2499 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2500 for (i = 0; i < total; i++) {
2501 sn = &sn_tab[available_snapshots[i]];
2502 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2503 }
2504 } else {
2505 monitor_printf(mon, "There is no suitable snapshot available\n");
2506 }
2507
Anthony Liguori7267c092011-08-20 22:09:37 -05002508 g_free(sn_tab);
2509 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002510
aliguoria672b462008-11-11 21:33:36 +00002511}
Avi Kivityc5705a72011-12-20 15:59:12 +02002512
2513void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2514{
Avi Kivity1ddde082012-01-08 13:18:19 +02002515 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002516 memory_region_name(mr), dev);
2517}
2518
2519void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2520{
2521 /* Nothing do to while the implementation is in RAMBlock */
2522}
2523
2524void vmstate_register_ram_global(MemoryRegion *mr)
2525{
2526 vmstate_register_ram(mr, NULL);
2527}