blob: 147e2d232e91aeba57496993eb9e9d6e029cfd0e [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"
blueswir1511d2b12009-03-07 15:32:56 +000042
aliguoria672b462008-11-11 21:33:36 +000043#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000044
Nolan18995b92009-10-15 16:53:55 -070045#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040046#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070047#endif
48#define ARP_HTYPE_ETH 0x0001
49#define ARP_PTYPE_IP 0x0800
50#define ARP_OP_REQUEST_REV 0x3
51
52static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000053 uint8_t *mac_addr)
54{
Nolan18995b92009-10-15 16:53:55 -070055 /* Ethernet header. */
56 memset(buf, 0xff, 6); /* destination MAC addr */
57 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
58 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +000059
Nolan18995b92009-10-15 16:53:55 -070060 /* RARP header. */
61 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
62 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
63 *(buf + 18) = 6; /* hardware addr length (ethernet) */
64 *(buf + 19) = 4; /* protocol addr length (IPv4) */
65 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
66 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
67 memset(buf + 28, 0x00, 4); /* source protocol addr */
68 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
69 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +000070
Nolan18995b92009-10-15 16:53:55 -070071 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
72 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +000073
Nolan18995b92009-10-15 16:53:55 -070074 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +000075}
76
Mark McLoughlinf401ca22009-11-25 18:49:32 +000077static void qemu_announce_self_iter(NICState *nic, void *opaque)
78{
79 uint8_t buf[60];
80 int len;
81
82 len = announce_self_create(buf, nic->conf->macaddr.a);
83
Jason Wangb356f762013-01-30 19:12:22 +080084 qemu_send_packet_raw(qemu_get_queue(nic), buf, len);
Mark McLoughlinf401ca22009-11-25 18:49:32 +000085}
86
87
Gleb Natapoved8b3302009-05-21 17:17:44 +030088static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +000089{
Gleb Natapoved8b3302009-05-21 17:17:44 +030090 static int count = SELF_ANNOUNCE_ROUNDS;
91 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +000092
Mark McLoughlinf401ca22009-11-25 18:49:32 +000093 qemu_foreach_nic(qemu_announce_self_iter, NULL);
94
Nolan18995b92009-10-15 16:53:55 -070095 if (--count) {
96 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +010097 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -070098 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +030099 } else {
100 qemu_del_timer(timer);
101 qemu_free_timer(timer);
102 }
103}
104
105void qemu_announce_self(void)
106{
107 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100108 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300109 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000110}
111
112/***********************************************************/
113/* savevm/loadvm support */
114
115#define IO_BUF_SIZE 32768
116
117struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200118 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000119 void *opaque;
120 int is_write;
121
Paolo Bonzini1964a392013-02-22 17:36:45 +0100122 int64_t bytes_xfer;
123 int64_t xfer_limit;
124
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100125 int64_t pos; /* start of buffer when writing, end of buffer
126 when reading */
aliguoria672b462008-11-11 21:33:36 +0000127 int buf_index;
128 int buf_size; /* 0 when writing */
129 uint8_t buf[IO_BUF_SIZE];
130
Juan Quintela3961b4d2011-10-05 01:05:21 +0200131 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000132};
133
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200134typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000135{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200136 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000137 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200138} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000139
140typedef struct QEMUFileSocket
141{
142 int fd;
143 QEMUFile *file;
144} QEMUFileSocket;
145
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100146typedef struct {
147 Coroutine *co;
148 int fd;
149} FDYieldUntilData;
150
151static void fd_coroutine_enter(void *opaque)
152{
153 FDYieldUntilData *data = opaque;
154 qemu_set_fd_handler(data->fd, NULL, NULL, NULL);
155 qemu_coroutine_enter(data->co, NULL);
156}
157
158/**
159 * Yield until a file descriptor becomes readable
160 *
161 * Note that this function clobbers the handlers for the file descriptor.
162 */
163static void coroutine_fn yield_until_fd_readable(int fd)
164{
165 FDYieldUntilData data;
166
167 assert(qemu_in_coroutine());
168 data.co = qemu_coroutine_self();
169 data.fd = fd;
170 qemu_set_fd_handler(fd, fd_coroutine_enter, NULL, &data);
171 qemu_coroutine_yield();
172}
173
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200174static int socket_get_fd(void *opaque)
175{
176 QEMUFileSocket *s = opaque;
177
178 return s->fd;
179}
180
aliguoria672b462008-11-11 21:33:36 +0000181static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
182{
183 QEMUFileSocket *s = opaque;
184 ssize_t len;
185
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200186 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000187 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200188 if (len != -1) {
189 break;
190 }
191 if (socket_error() == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100192 yield_until_fd_readable(s->fd);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200193 } else if (socket_error() != EINTR) {
194 break;
195 }
196 }
aliguoria672b462008-11-11 21:33:36 +0000197
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200198 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000199 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200200 }
aliguoria672b462008-11-11 21:33:36 +0000201 return len;
202}
203
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100204static int socket_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
205{
206 QEMUFileSocket *s = opaque;
207 ssize_t len;
208
209 len = qemu_send_full(s->fd, buf, size, 0);
210 if (len < size) {
211 len = -socket_error();
212 }
213 return len;
214}
215
aliguoria672b462008-11-11 21:33:36 +0000216static int socket_close(void *opaque)
217{
218 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200219 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500220 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000221 return 0;
222}
223
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200224static int stdio_get_fd(void *opaque)
225{
226 QEMUFileStdio *s = opaque;
227
228 return fileno(s->stdio_file);
229}
230
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200231static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000232{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200233 QEMUFileStdio *s = opaque;
234 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000235}
236
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200237static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000238{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200239 QEMUFileStdio *s = opaque;
240 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300241 int bytes;
242
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200243 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300244 clearerr(fp);
245 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200246 if (bytes != 0 || !ferror(fp)) {
247 break;
248 }
249 if (errno == EAGAIN) {
Stefan Hajnoczid7cd3692013-02-11 17:01:45 +0100250 yield_until_fd_readable(fileno(fp));
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200251 } else if (errno != EINTR) {
252 break;
253 }
254 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300255 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000256}
257
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200258static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000259{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200260 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500261 int ret;
262 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200263 if (ret == -1) {
264 ret = -errno;
Paolo Bonzini13c7b2d2013-02-22 17:36:38 +0100265 } else if (!WIFEXITED(ret) || WEXITSTATUS(ret) != 0) {
266 /* close succeeded, but non-zero exit code: */
267 ret = -EIO; /* fake errno value */
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200268 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500269 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500270 return ret;
aliguoria672b462008-11-11 21:33:36 +0000271}
272
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200273static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000274{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200275 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200276 int ret = 0;
Paolo Bonzinice39ee32013-02-22 17:36:37 +0100277
278 if (s->file->ops->put_buffer) {
279 int fd = fileno(s->stdio_file);
280 struct stat st;
281
282 ret = fstat(fd, &st);
283 if (ret == 0 && S_ISREG(st.st_mode)) {
284 /*
285 * If the file handle is a regular file make sure the
286 * data is flushed to disk before signaling success.
287 */
288 ret = fsync(fd);
289 if (ret != 0) {
290 ret = -errno;
291 return ret;
292 }
293 }
294 }
Eduardo Habkost0e286702011-11-10 10:41:45 -0200295 if (fclose(s->stdio_file) == EOF) {
296 ret = -errno;
297 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500298 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200299 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200300}
aliguoria672b462008-11-11 21:33:36 +0000301
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200302static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200303 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200304 .get_buffer = stdio_get_buffer,
305 .close = stdio_pclose
306};
307
308static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200309 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200310 .put_buffer = stdio_put_buffer,
311 .close = stdio_pclose
312};
313
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100314QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200315{
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100316 FILE *stdio_file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200317 QEMUFileStdio *s;
318
Paolo Bonzini817b9ed2013-02-22 17:36:36 +0100319 stdio_file = popen(command, mode);
320 if (stdio_file == NULL) {
321 return NULL;
322 }
323
324 if (mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000325 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
326 return NULL;
327 }
328
Anthony Liguori7267c092011-08-20 22:09:37 -0500329 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000330
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200331 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000332
333 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200334 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000335 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200336 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000337 }
aliguoria672b462008-11-11 21:33:36 +0000338 return s->file;
339}
340
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200341static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200342 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200343 .get_buffer = stdio_get_buffer,
344 .close = stdio_fclose
345};
346
347static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200348 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200349 .put_buffer = stdio_put_buffer,
350 .close = stdio_fclose
351};
352
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200353QEMUFile *qemu_fdopen(int fd, const char *mode)
354{
355 QEMUFileStdio *s;
356
357 if (mode == NULL ||
358 (mode[0] != 'r' && mode[0] != 'w') ||
359 mode[1] != 'b' || mode[2] != 0) {
360 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
361 return NULL;
362 }
363
Anthony Liguori7267c092011-08-20 22:09:37 -0500364 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200365 s->stdio_file = fdopen(fd, mode);
366 if (!s->stdio_file)
367 goto fail;
368
369 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200370 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200371 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200372 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200373 }
374 return s->file;
375
376fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500377 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200378 return NULL;
379}
380
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200381static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200382 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200383 .get_buffer = socket_get_buffer,
384 .close = socket_close
385};
386
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100387static const QEMUFileOps socket_write_ops = {
388 .get_fd = socket_get_fd,
389 .put_buffer = socket_put_buffer,
390 .close = socket_close
391};
392
393QEMUFile *qemu_fopen_socket(int fd, const char *mode)
aliguoria672b462008-11-11 21:33:36 +0000394{
Anthony Liguori7267c092011-08-20 22:09:37 -0500395 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000396
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100397 if (mode == NULL ||
398 (mode[0] != 'r' && mode[0] != 'w') ||
399 mode[1] != 'b' || mode[2] != 0) {
400 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
401 return NULL;
402 }
403
aliguoria672b462008-11-11 21:33:36 +0000404 s->fd = fd;
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100405 if (mode[0] == 'w') {
Paolo Bonzinif8bbc122013-02-22 17:36:41 +0100406 socket_set_block(s->fd);
Paolo Bonzini0cc3f3c2013-02-22 17:36:39 +0100407 s->file = qemu_fopen_ops(s, &socket_write_ops);
408 } else {
409 s->file = qemu_fopen_ops(s, &socket_read_ops);
410 }
aliguoria672b462008-11-11 21:33:36 +0000411 return s->file;
412}
413
aliguoria672b462008-11-11 21:33:36 +0000414QEMUFile *qemu_fopen(const char *filename, const char *mode)
415{
416 QEMUFileStdio *s;
417
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200418 if (mode == NULL ||
419 (mode[0] != 'r' && mode[0] != 'w') ||
420 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000421 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200422 return NULL;
423 }
424
Anthony Liguori7267c092011-08-20 22:09:37 -0500425 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000426
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200427 s->stdio_file = fopen(filename, mode);
428 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000429 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200430
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200431 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200432 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200433 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200434 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200435 }
436 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000437fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500438 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000439 return NULL;
440}
441
aliguori178e08a2009-04-05 19:10:55 +0000442static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000443 int64_t pos, int size)
444{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200445 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000446 return size;
447}
448
aliguori178e08a2009-04-05 19:10:55 +0000449static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000450{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200451 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000452}
453
454static int bdrv_fclose(void *opaque)
455{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200456 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000457}
458
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200459static const QEMUFileOps bdrv_read_ops = {
460 .get_buffer = block_get_buffer,
461 .close = bdrv_fclose
462};
463
464static const QEMUFileOps bdrv_write_ops = {
465 .put_buffer = block_put_buffer,
466 .close = bdrv_fclose
467};
468
Christoph Hellwig45566e92009-07-10 23:11:57 +0200469static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000470{
aliguoria672b462008-11-11 21:33:36 +0000471 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200472 return qemu_fopen_ops(bs, &bdrv_write_ops);
473 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000474}
475
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200476QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000477{
478 QEMUFile *f;
479
Anthony Liguori7267c092011-08-20 22:09:37 -0500480 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000481
482 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200483 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000484 f->is_write = 0;
aliguoria672b462008-11-11 21:33:36 +0000485 return f;
486}
487
Juan Quintela624b9cc2011-10-05 01:02:52 +0200488int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000489{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200490 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000491}
492
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100493static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000494{
Juan Quintelaafe41932013-01-14 13:36:28 +0100495 if (f->last_error == 0) {
496 f->last_error = ret;
497 }
aliguori4dabe242009-04-05 19:30:51 +0000498}
499
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200500/** Flushes QEMUFile buffer
501 *
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200502 */
Paolo Bonzini05f28b82013-02-22 17:36:31 +0100503static void qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000504{
Juan Quintela7311bea2012-08-29 19:08:59 +0200505 int ret = 0;
506
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100507 if (!f->ops->put_buffer) {
508 return;
509 }
aliguoria672b462008-11-11 21:33:36 +0000510 if (f->is_write && f->buf_index > 0) {
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100511 ret = f->ops->put_buffer(f->opaque, f->buf, f->pos, f->buf_index);
Juan Quintela7311bea2012-08-29 19:08:59 +0200512 if (ret >= 0) {
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100513 f->pos += f->buf_index;
Juan Quintela7311bea2012-08-29 19:08:59 +0200514 }
aliguoria672b462008-11-11 21:33:36 +0000515 f->buf_index = 0;
516 }
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100517 if (ret < 0) {
518 qemu_file_set_error(f, ret);
519 }
aliguoria672b462008-11-11 21:33:36 +0000520}
521
522static void qemu_fill_buffer(QEMUFile *f)
523{
524 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200525 int pending;
aliguoria672b462008-11-11 21:33:36 +0000526
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200527 if (!f->ops->get_buffer)
aliguoria672b462008-11-11 21:33:36 +0000528 return;
529
530 if (f->is_write)
531 abort();
532
Juan Quintela0046c452011-09-30 19:28:45 +0200533 pending = f->buf_size - f->buf_index;
534 if (pending > 0) {
535 memmove(f->buf, f->buf + f->buf_index, pending);
536 }
537 f->buf_index = 0;
538 f->buf_size = pending;
539
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100540 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->pos,
Juan Quintela0046c452011-09-30 19:28:45 +0200541 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000542 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200543 f->buf_size += len;
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100544 f->pos += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200545 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200546 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000547 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200548 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000549}
550
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200551int qemu_get_fd(QEMUFile *f)
552{
553 if (f->ops->get_fd) {
554 return f->ops->get_fd(f->opaque);
555 }
556 return -1;
557}
558
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200559/** Closes the file
560 *
561 * Returns negative error value if any error happened on previous operations or
562 * while closing the file. Returns 0 or positive number on success.
563 *
564 * The meaning of return value on success depends on the specific backend
565 * being used.
566 */
567int qemu_fclose(QEMUFile *f)
568{
Juan Quintela29eee862012-08-29 19:14:54 +0200569 int ret;
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100570 qemu_fflush(f);
571 ret = qemu_file_get_error(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200572
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200573 if (f->ops->close) {
574 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200575 if (ret >= 0) {
576 ret = ret2;
577 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200578 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200579 /* If any error was spotted before closing, we should report it
580 * instead of the close() return value.
581 */
582 if (f->last_error) {
583 ret = f->last_error;
584 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500585 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000586 return ret;
587}
588
aliguoria672b462008-11-11 21:33:36 +0000589void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
590{
591 int l;
592
Juan Quintelac10682c2012-08-29 19:43:39 +0200593 if (f->last_error) {
594 return;
595 }
596
597 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000598 fprintf(stderr,
599 "Attempted to write to buffer while read buffer is not empty\n");
600 abort();
601 }
602
Juan Quintelac10682c2012-08-29 19:43:39 +0200603 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000604 l = IO_BUF_SIZE - f->buf_index;
605 if (l > size)
606 l = size;
607 memcpy(f->buf + f->buf_index, buf, l);
608 f->is_write = 1;
609 f->buf_index += l;
Paolo Bonzini1964a392013-02-22 17:36:45 +0100610 f->bytes_xfer += l;
aliguoria672b462008-11-11 21:33:36 +0000611 buf += l;
612 size -= l;
Juan Quintela7311bea2012-08-29 19:08:59 +0200613 if (f->buf_index >= IO_BUF_SIZE) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100614 qemu_fflush(f);
615 if (qemu_file_get_error(f)) {
Juan Quintelac10682c2012-08-29 19:43:39 +0200616 break;
617 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200618 }
aliguoria672b462008-11-11 21:33:36 +0000619 }
620}
621
622void qemu_put_byte(QEMUFile *f, int v)
623{
Juan Quintelac10682c2012-08-29 19:43:39 +0200624 if (f->last_error) {
625 return;
626 }
627
628 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000629 fprintf(stderr,
630 "Attempted to write to buffer while read buffer is not empty\n");
631 abort();
632 }
633
634 f->buf[f->buf_index++] = v;
635 f->is_write = 1;
Juan Quintela7311bea2012-08-29 19:08:59 +0200636 if (f->buf_index >= IO_BUF_SIZE) {
Paolo Bonzini93bf2102013-02-22 17:36:12 +0100637 qemu_fflush(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200638 }
aliguoria672b462008-11-11 21:33:36 +0000639}
640
Juan Quintelac6380722011-10-04 15:28:31 +0200641static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000642{
Juan Quintelac6380722011-10-04 15:28:31 +0200643 if (f->buf_index + size <= f->buf_size) {
644 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000645 }
aliguoria672b462008-11-11 21:33:36 +0000646}
647
Juan Quintelac6380722011-10-04 15:28:31 +0200648static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200649{
Juan Quintelac6380722011-10-04 15:28:31 +0200650 int pending;
651 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200652
Juan Quintelab9ce1452011-10-04 13:55:32 +0200653 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200654 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200655 }
Juan Quintela811814b2010-07-26 21:38:43 +0200656
Juan Quintelac6380722011-10-04 15:28:31 +0200657 index = f->buf_index + offset;
658 pending = f->buf_size - index;
659 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200660 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200661 index = f->buf_index + offset;
662 pending = f->buf_size - index;
663 }
664
665 if (pending <= 0) {
666 return 0;
667 }
668 if (size > pending) {
669 size = pending;
670 }
671
672 memcpy(buf, f->buf + index, size);
673 return size;
674}
675
676int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
677{
678 int pending = size;
679 int done = 0;
680
681 while (pending > 0) {
682 int res;
683
684 res = qemu_peek_buffer(f, buf, pending, 0);
685 if (res == 0) {
686 return done;
687 }
688 qemu_file_skip(f, res);
689 buf += res;
690 pending -= res;
691 done += res;
692 }
693 return done;
694}
695
696static int qemu_peek_byte(QEMUFile *f, int offset)
697{
698 int index = f->buf_index + offset;
699
700 if (f->is_write) {
701 abort();
702 }
703
704 if (index >= f->buf_size) {
705 qemu_fill_buffer(f);
706 index = f->buf_index + offset;
707 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200708 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200709 }
Juan Quintela811814b2010-07-26 21:38:43 +0200710 }
Juan Quintelac6380722011-10-04 15:28:31 +0200711 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200712}
713
aliguoria672b462008-11-11 21:33:36 +0000714int qemu_get_byte(QEMUFile *f)
715{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200716 int result;
aliguoria672b462008-11-11 21:33:36 +0000717
Juan Quintelac6380722011-10-04 15:28:31 +0200718 result = qemu_peek_byte(f, 0);
719 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200720 return result;
aliguoria672b462008-11-11 21:33:36 +0000721}
722
Stefan Hajnocziad55ab42013-02-12 10:37:14 +0100723int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000724{
Paolo Bonzini3f2d38f2013-02-22 17:36:40 +0100725 qemu_fflush(f);
726 return f->pos;
aliguoria672b462008-11-11 21:33:36 +0000727}
728
aliguoria672b462008-11-11 21:33:36 +0000729int qemu_file_rate_limit(QEMUFile *f)
730{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100731 if (qemu_file_get_error(f)) {
732 return 1;
733 }
734 if (f->xfer_limit > 0 && f->bytes_xfer > f->xfer_limit) {
735 return 1;
736 }
aliguoria672b462008-11-11 21:33:36 +0000737 return 0;
738}
739
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200740int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200741{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100742 return f->xfer_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200743}
744
Paolo Bonzini1964a392013-02-22 17:36:45 +0100745void qemu_file_set_rate_limit(QEMUFile *f, int64_t limit)
Glauber Costa19629532009-05-20 18:26:57 -0400746{
Paolo Bonzini1964a392013-02-22 17:36:45 +0100747 f->xfer_limit = limit;
748}
Glauber Costa19629532009-05-20 18:26:57 -0400749
Paolo Bonzini1964a392013-02-22 17:36:45 +0100750void qemu_file_reset_rate_limit(QEMUFile *f)
751{
752 f->bytes_xfer = 0;
Glauber Costa19629532009-05-20 18:26:57 -0400753}
754
aliguoria672b462008-11-11 21:33:36 +0000755void qemu_put_be16(QEMUFile *f, unsigned int v)
756{
757 qemu_put_byte(f, v >> 8);
758 qemu_put_byte(f, v);
759}
760
761void qemu_put_be32(QEMUFile *f, unsigned int v)
762{
763 qemu_put_byte(f, v >> 24);
764 qemu_put_byte(f, v >> 16);
765 qemu_put_byte(f, v >> 8);
766 qemu_put_byte(f, v);
767}
768
769void qemu_put_be64(QEMUFile *f, uint64_t v)
770{
771 qemu_put_be32(f, v >> 32);
772 qemu_put_be32(f, v);
773}
774
775unsigned int qemu_get_be16(QEMUFile *f)
776{
777 unsigned int v;
778 v = qemu_get_byte(f) << 8;
779 v |= qemu_get_byte(f);
780 return v;
781}
782
783unsigned int qemu_get_be32(QEMUFile *f)
784{
785 unsigned int v;
786 v = qemu_get_byte(f) << 24;
787 v |= qemu_get_byte(f) << 16;
788 v |= qemu_get_byte(f) << 8;
789 v |= qemu_get_byte(f);
790 return v;
791}
792
793uint64_t qemu_get_be64(QEMUFile *f)
794{
795 uint64_t v;
796 v = (uint64_t)qemu_get_be32(f) << 32;
797 v |= qemu_get_be32(f);
798 return v;
799}
800
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200801
802/* timer */
803
804void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
805{
806 uint64_t expire_time;
807
808 expire_time = qemu_timer_expire_time_ns(ts);
809 qemu_put_be64(f, expire_time);
810}
811
812void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
813{
814 uint64_t expire_time;
815
816 expire_time = qemu_get_be64(f);
817 if (expire_time != -1) {
818 qemu_mod_timer_ns(ts, expire_time);
819 } else {
820 qemu_del_timer(ts);
821 }
822}
823
824
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100825/* bool */
826
827static int get_bool(QEMUFile *f, void *pv, size_t size)
828{
829 bool *v = pv;
830 *v = qemu_get_byte(f);
831 return 0;
832}
833
834static void put_bool(QEMUFile *f, void *pv, size_t size)
835{
836 bool *v = pv;
837 qemu_put_byte(f, *v);
838}
839
840const VMStateInfo vmstate_info_bool = {
841 .name = "bool",
842 .get = get_bool,
843 .put = put_bool,
844};
845
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200846/* 8 bit int */
847
848static int get_int8(QEMUFile *f, void *pv, size_t size)
849{
850 int8_t *v = pv;
851 qemu_get_s8s(f, v);
852 return 0;
853}
854
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200855static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200856{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200857 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200858 qemu_put_s8s(f, v);
859}
860
861const VMStateInfo vmstate_info_int8 = {
862 .name = "int8",
863 .get = get_int8,
864 .put = put_int8,
865};
866
867/* 16 bit int */
868
869static int get_int16(QEMUFile *f, void *pv, size_t size)
870{
871 int16_t *v = pv;
872 qemu_get_sbe16s(f, v);
873 return 0;
874}
875
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200876static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200877{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200878 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200879 qemu_put_sbe16s(f, v);
880}
881
882const VMStateInfo vmstate_info_int16 = {
883 .name = "int16",
884 .get = get_int16,
885 .put = put_int16,
886};
887
888/* 32 bit int */
889
890static int get_int32(QEMUFile *f, void *pv, size_t size)
891{
892 int32_t *v = pv;
893 qemu_get_sbe32s(f, v);
894 return 0;
895}
896
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200897static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200898{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200899 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200900 qemu_put_sbe32s(f, v);
901}
902
903const VMStateInfo vmstate_info_int32 = {
904 .name = "int32",
905 .get = get_int32,
906 .put = put_int32,
907};
908
Juan Quintela82501662009-08-20 19:42:32 +0200909/* 32 bit int. See that the received value is the same than the one
910 in the field */
911
912static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
913{
914 int32_t *v = pv;
915 int32_t v2;
916 qemu_get_sbe32s(f, &v2);
917
918 if (*v == v2)
919 return 0;
920 return -EINVAL;
921}
922
923const VMStateInfo vmstate_info_int32_equal = {
924 .name = "int32 equal",
925 .get = get_int32_equal,
926 .put = put_int32,
927};
928
Juan Quintela0a031e02009-08-20 19:42:37 +0200929/* 32 bit int. See that the received value is the less or the same
930 than the one in the field */
931
932static int get_int32_le(QEMUFile *f, void *pv, size_t size)
933{
934 int32_t *old = pv;
935 int32_t new;
936 qemu_get_sbe32s(f, &new);
937
938 if (*old <= new)
939 return 0;
940 return -EINVAL;
941}
942
943const VMStateInfo vmstate_info_int32_le = {
944 .name = "int32 equal",
945 .get = get_int32_le,
946 .put = put_int32,
947};
948
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200949/* 64 bit int */
950
951static int get_int64(QEMUFile *f, void *pv, size_t size)
952{
953 int64_t *v = pv;
954 qemu_get_sbe64s(f, v);
955 return 0;
956}
957
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200958static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200959{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200960 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200961 qemu_put_sbe64s(f, v);
962}
963
964const VMStateInfo vmstate_info_int64 = {
965 .name = "int64",
966 .get = get_int64,
967 .put = put_int64,
968};
969
970/* 8 bit unsigned int */
971
972static int get_uint8(QEMUFile *f, void *pv, size_t size)
973{
974 uint8_t *v = pv;
975 qemu_get_8s(f, v);
976 return 0;
977}
978
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200979static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200980{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200981 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200982 qemu_put_8s(f, v);
983}
984
985const VMStateInfo vmstate_info_uint8 = {
986 .name = "uint8",
987 .get = get_uint8,
988 .put = put_uint8,
989};
990
991/* 16 bit unsigned int */
992
993static int get_uint16(QEMUFile *f, void *pv, size_t size)
994{
995 uint16_t *v = pv;
996 qemu_get_be16s(f, v);
997 return 0;
998}
999
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001000static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001001{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001002 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001003 qemu_put_be16s(f, v);
1004}
1005
1006const VMStateInfo vmstate_info_uint16 = {
1007 .name = "uint16",
1008 .get = get_uint16,
1009 .put = put_uint16,
1010};
1011
1012/* 32 bit unsigned int */
1013
1014static int get_uint32(QEMUFile *f, void *pv, size_t size)
1015{
1016 uint32_t *v = pv;
1017 qemu_get_be32s(f, v);
1018 return 0;
1019}
1020
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001021static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001022{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001023 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001024 qemu_put_be32s(f, v);
1025}
1026
1027const VMStateInfo vmstate_info_uint32 = {
1028 .name = "uint32",
1029 .get = get_uint32,
1030 .put = put_uint32,
1031};
1032
Juan Quintela9122a8f2011-03-10 12:33:48 +01001033/* 32 bit uint. See that the received value is the same than the one
1034 in the field */
1035
1036static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1037{
1038 uint32_t *v = pv;
1039 uint32_t v2;
1040 qemu_get_be32s(f, &v2);
1041
1042 if (*v == v2) {
1043 return 0;
1044 }
1045 return -EINVAL;
1046}
1047
1048const VMStateInfo vmstate_info_uint32_equal = {
1049 .name = "uint32 equal",
1050 .get = get_uint32_equal,
1051 .put = put_uint32,
1052};
1053
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001054/* 64 bit unsigned int */
1055
1056static int get_uint64(QEMUFile *f, void *pv, size_t size)
1057{
1058 uint64_t *v = pv;
1059 qemu_get_be64s(f, v);
1060 return 0;
1061}
1062
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001063static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001064{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001065 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001066 qemu_put_be64s(f, v);
1067}
1068
1069const VMStateInfo vmstate_info_uint64 = {
1070 .name = "uint64",
1071 .get = get_uint64,
1072 .put = put_uint64,
1073};
1074
Juan Quintela80cd83e2009-09-10 03:04:36 +02001075/* 8 bit int. See that the received value is the same than the one
1076 in the field */
1077
1078static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1079{
1080 uint8_t *v = pv;
1081 uint8_t v2;
1082 qemu_get_8s(f, &v2);
1083
1084 if (*v == v2)
1085 return 0;
1086 return -EINVAL;
1087}
1088
1089const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001090 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001091 .get = get_uint8_equal,
1092 .put = put_uint8,
1093};
1094
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001095/* 16 bit unsigned int int. See that the received value is the same than the one
1096 in the field */
1097
1098static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1099{
1100 uint16_t *v = pv;
1101 uint16_t v2;
1102 qemu_get_be16s(f, &v2);
1103
1104 if (*v == v2)
1105 return 0;
1106 return -EINVAL;
1107}
1108
1109const VMStateInfo vmstate_info_uint16_equal = {
1110 .name = "uint16 equal",
1111 .get = get_uint16_equal,
1112 .put = put_uint16,
1113};
1114
Juan Quinteladde04632009-08-20 19:42:26 +02001115/* timers */
1116
1117static int get_timer(QEMUFile *f, void *pv, size_t size)
1118{
1119 QEMUTimer *v = pv;
1120 qemu_get_timer(f, v);
1121 return 0;
1122}
1123
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001124static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001125{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001126 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001127 qemu_put_timer(f, v);
1128}
1129
1130const VMStateInfo vmstate_info_timer = {
1131 .name = "timer",
1132 .get = get_timer,
1133 .put = put_timer,
1134};
1135
Juan Quintela6f67c502009-08-20 19:42:35 +02001136/* uint8_t buffers */
1137
1138static int get_buffer(QEMUFile *f, void *pv, size_t size)
1139{
1140 uint8_t *v = pv;
1141 qemu_get_buffer(f, v, size);
1142 return 0;
1143}
1144
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001145static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001146{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001147 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001148 qemu_put_buffer(f, v, size);
1149}
1150
1151const VMStateInfo vmstate_info_buffer = {
1152 .name = "buffer",
1153 .get = get_buffer,
1154 .put = put_buffer,
1155};
1156
Juan Quintela76507c72009-10-19 15:46:28 +02001157/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001158 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001159
1160static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1161{
Jan Kiszka21174c32009-12-02 12:36:35 +01001162 uint8_t buf[1024];
1163 int block_len;
1164
1165 while (size > 0) {
1166 block_len = MIN(sizeof(buf), size);
1167 size -= block_len;
1168 qemu_get_buffer(f, buf, block_len);
1169 }
1170 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001171}
1172
1173static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1174{
Jan Kiszka21174c32009-12-02 12:36:35 +01001175 static const uint8_t buf[1024];
1176 int block_len;
1177
1178 while (size > 0) {
1179 block_len = MIN(sizeof(buf), size);
1180 size -= block_len;
1181 qemu_put_buffer(f, buf, block_len);
1182 }
Juan Quintela76507c72009-10-19 15:46:28 +02001183}
1184
1185const VMStateInfo vmstate_info_unused_buffer = {
1186 .name = "unused_buffer",
1187 .get = get_unused_buffer,
1188 .put = put_unused_buffer,
1189};
1190
Peter Maydell08e99e22012-10-30 07:45:12 +00001191/* bitmaps (as defined by bitmap.h). Note that size here is the size
1192 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1193 * bit words with the bits in big endian order. The in-memory format
1194 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1195 */
1196/* This is the number of 64 bit words sent over the wire */
1197#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1198static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1199{
1200 unsigned long *bmp = pv;
1201 int i, idx = 0;
1202 for (i = 0; i < BITS_TO_U64S(size); i++) {
1203 uint64_t w = qemu_get_be64(f);
1204 bmp[idx++] = w;
1205 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1206 bmp[idx++] = w >> 32;
1207 }
1208 }
1209 return 0;
1210}
1211
1212static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1213{
1214 unsigned long *bmp = pv;
1215 int i, idx = 0;
1216 for (i = 0; i < BITS_TO_U64S(size); i++) {
1217 uint64_t w = bmp[idx++];
1218 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1219 w |= ((uint64_t)bmp[idx++]) << 32;
1220 }
1221 qemu_put_be64(f, w);
1222 }
1223}
1224
1225const VMStateInfo vmstate_info_bitmap = {
1226 .name = "bitmap",
1227 .get = get_bitmap,
1228 .put = put_bitmap,
1229};
1230
Alex Williamson7685ee62010-06-25 11:09:14 -06001231typedef struct CompatEntry {
1232 char idstr[256];
1233 int instance_id;
1234} CompatEntry;
1235
aliguoria672b462008-11-11 21:33:36 +00001236typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001237 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001238 char idstr[256];
1239 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001240 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001241 int version_id;
1242 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001243 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001244 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001245 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001246 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001247 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001248 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001249} SaveStateEntry;
1250
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001251
Blue Swirl72cf2d42009-09-12 07:36:22 +00001252static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1253 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001254static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001255
Juan Quintela8718e992009-09-01 02:12:31 +02001256static int calculate_new_instance_id(const char *idstr)
1257{
1258 SaveStateEntry *se;
1259 int instance_id = 0;
1260
Blue Swirl72cf2d42009-09-12 07:36:22 +00001261 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001262 if (strcmp(idstr, se->idstr) == 0
1263 && instance_id <= se->instance_id) {
1264 instance_id = se->instance_id + 1;
1265 }
1266 }
1267 return instance_id;
1268}
1269
Alex Williamson7685ee62010-06-25 11:09:14 -06001270static int calculate_compat_instance_id(const char *idstr)
1271{
1272 SaveStateEntry *se;
1273 int instance_id = 0;
1274
1275 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1276 if (!se->compat)
1277 continue;
1278
1279 if (strcmp(idstr, se->compat->idstr) == 0
1280 && instance_id <= se->compat->instance_id) {
1281 instance_id = se->compat->instance_id + 1;
1282 }
1283 }
1284 return instance_id;
1285}
1286
aliguoria672b462008-11-11 21:33:36 +00001287/* TODO: Individual devices generally have very little idea about the rest
1288 of the system, so instance_id should be removed/replaced.
1289 Meanwhile pass -1 as instance_id if you do not already have a clearly
1290 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001291int register_savevm_live(DeviceState *dev,
1292 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001293 int instance_id,
1294 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001295 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001296 void *opaque)
1297{
Juan Quintela8718e992009-09-01 02:12:31 +02001298 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001299
Anthony Liguori7267c092011-08-20 22:09:37 -05001300 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001301 se->version_id = version_id;
1302 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001303 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001304 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001305 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001306 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001307 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001308 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001309 se->is_ram = 1;
1310 }
aliguoria672b462008-11-11 21:33:36 +00001311
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001312 if (dev) {
1313 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001314 if (id) {
1315 pstrcpy(se->idstr, sizeof(se->idstr), id);
1316 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001317 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001318
Anthony Liguori7267c092011-08-20 22:09:37 -05001319 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001320 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1321 se->compat->instance_id = instance_id == -1 ?
1322 calculate_compat_instance_id(idstr) : instance_id;
1323 instance_id = -1;
1324 }
1325 }
1326 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1327
Juan Quintela8718e992009-09-01 02:12:31 +02001328 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001329 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001330 } else {
1331 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001332 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001333 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001334 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001335 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001336 return 0;
1337}
1338
Alex Williamson0be71e32010-06-25 11:09:07 -06001339int register_savevm(DeviceState *dev,
1340 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001341 int instance_id,
1342 int version_id,
1343 SaveStateHandler *save_state,
1344 LoadStateHandler *load_state,
1345 void *opaque)
1346{
Juan Quintela7908c782012-06-26 18:46:10 +02001347 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1348 ops->save_state = save_state;
1349 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001350 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001351 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001352}
1353
Alex Williamson0be71e32010-06-25 11:09:07 -06001354void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001355{
Juan Quintela8718e992009-09-01 02:12:31 +02001356 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001357 char id[256] = "";
1358
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001359 if (dev) {
1360 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001361 if (path) {
1362 pstrcpy(id, sizeof(id), path);
1363 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001364 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001365 }
1366 }
1367 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001368
Blue Swirl72cf2d42009-09-12 07:36:22 +00001369 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001370 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001371 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001372 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001373 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001374 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001375 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001376 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001377 }
aliguori41bd13a2009-04-17 17:10:59 +00001378 }
1379}
1380
Alex Williamson0be71e32010-06-25 11:09:07 -06001381int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001382 const VMStateDescription *vmsd,
1383 void *opaque, int alias_id,
1384 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001385{
Juan Quintela8718e992009-09-01 02:12:31 +02001386 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001387
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001388 /* If this triggers, alias support can be dropped for the vmsd. */
1389 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1390
Anthony Liguori7267c092011-08-20 22:09:37 -05001391 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001392 se->version_id = vmsd->version_id;
1393 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001394 se->opaque = opaque;
1395 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001396 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001397 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001398
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001399 if (dev) {
1400 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001401 if (id) {
1402 pstrcpy(se->idstr, sizeof(se->idstr), id);
1403 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001404 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001405
Anthony Liguori7267c092011-08-20 22:09:37 -05001406 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001407 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1408 se->compat->instance_id = instance_id == -1 ?
1409 calculate_compat_instance_id(vmsd->name) : instance_id;
1410 instance_id = -1;
1411 }
1412 }
1413 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1414
Juan Quintela8718e992009-09-01 02:12:31 +02001415 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001416 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001417 } else {
1418 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001419 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001420 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001421 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001422 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001423 return 0;
1424}
1425
Alex Williamson0be71e32010-06-25 11:09:07 -06001426int vmstate_register(DeviceState *dev, int instance_id,
1427 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001428{
Alex Williamson0be71e32010-06-25 11:09:07 -06001429 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1430 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001431}
1432
Alex Williamson0be71e32010-06-25 11:09:07 -06001433void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1434 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001435{
Juan Quintela1eb75382009-09-10 03:04:29 +02001436 SaveStateEntry *se, *new_se;
1437
Blue Swirl72cf2d42009-09-12 07:36:22 +00001438 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001439 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001440 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001441 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001442 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001443 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001444 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001445 }
1446 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001447}
1448
Juan Quintela811814b2010-07-26 21:38:43 +02001449static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1450 void *opaque);
1451static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1452 void *opaque);
1453
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001454int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1455 void *opaque, int version_id)
1456{
1457 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001458 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001459
1460 if (version_id > vmsd->version_id) {
1461 return -EINVAL;
1462 }
1463 if (version_id < vmsd->minimum_version_id_old) {
1464 return -EINVAL;
1465 }
1466 if (version_id < vmsd->minimum_version_id) {
1467 return vmsd->load_state_old(f, opaque, version_id);
1468 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001469 if (vmsd->pre_load) {
1470 int ret = vmsd->pre_load(opaque);
1471 if (ret)
1472 return ret;
1473 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001474 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001475 if ((field->field_exists &&
1476 field->field_exists(opaque, version_id)) ||
1477 (!field->field_exists &&
1478 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001479 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001480 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001481 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001482
Juan Quintelae61a1e02009-12-02 12:36:38 +01001483 if (field->flags & VMS_VBUFFER) {
1484 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001485 if (field->flags & VMS_MULTIPLY) {
1486 size *= field->size;
1487 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001488 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001489 if (field->flags & VMS_ARRAY) {
1490 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001491 } else if (field->flags & VMS_VARRAY_INT32) {
1492 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001493 } else if (field->flags & VMS_VARRAY_UINT32) {
1494 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001495 } else if (field->flags & VMS_VARRAY_UINT16) {
1496 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001497 } else if (field->flags & VMS_VARRAY_UINT8) {
1498 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001499 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001500 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001501 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001502 }
1503 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001504 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001505
Juan Quintela19df4382009-09-29 22:48:41 +02001506 if (field->flags & VMS_ARRAY_OF_POINTER) {
1507 addr = *(void **)addr;
1508 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001509 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001510 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001511 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001512 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001513
1514 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001515 if (ret < 0) {
1516 return ret;
1517 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001518 }
1519 }
1520 field++;
1521 }
Juan Quintela811814b2010-07-26 21:38:43 +02001522 ret = vmstate_subsection_load(f, vmsd, opaque);
1523 if (ret != 0) {
1524 return ret;
1525 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001526 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001527 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001528 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001529 return 0;
1530}
1531
1532void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001533 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001534{
1535 VMStateField *field = vmsd->fields;
1536
Juan Quintela8fb07912009-09-10 03:04:32 +02001537 if (vmsd->pre_save) {
1538 vmsd->pre_save(opaque);
1539 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001540 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001541 if (!field->field_exists ||
1542 field->field_exists(opaque, vmsd->version_id)) {
1543 void *base_addr = opaque + field->offset;
1544 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001545 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001546
Juan Quintelae61a1e02009-12-02 12:36:38 +01001547 if (field->flags & VMS_VBUFFER) {
1548 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001549 if (field->flags & VMS_MULTIPLY) {
1550 size *= field->size;
1551 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001552 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001553 if (field->flags & VMS_ARRAY) {
1554 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001555 } else if (field->flags & VMS_VARRAY_INT32) {
1556 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001557 } else if (field->flags & VMS_VARRAY_UINT32) {
1558 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001559 } else if (field->flags & VMS_VARRAY_UINT16) {
1560 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001561 } else if (field->flags & VMS_VARRAY_UINT8) {
1562 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001563 }
1564 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001565 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001566 }
1567 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001568 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001569
Juan Quintela85953872009-12-02 12:36:37 +01001570 if (field->flags & VMS_ARRAY_OF_POINTER) {
1571 addr = *(void **)addr;
1572 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001573 if (field->flags & VMS_STRUCT) {
1574 vmstate_save_state(f, field->vmsd, addr);
1575 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001576 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001577 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001578 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001579 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001580 field++;
1581 }
Juan Quintela811814b2010-07-26 21:38:43 +02001582 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001583}
1584
Juan Quintela4082be42009-08-20 19:42:24 +02001585static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1586{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001587 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001588 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001589 }
1590 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001591}
1592
Alex Williamsondc912122011-01-11 14:39:43 -07001593static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001594{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001595 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001596 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001597 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001598 }
1599 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001600}
1601
aliguoria672b462008-11-11 21:33:36 +00001602#define QEMU_VM_FILE_MAGIC 0x5145564d
1603#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1604#define QEMU_VM_FILE_VERSION 0x00000003
1605
1606#define QEMU_VM_EOF 0x00
1607#define QEMU_VM_SECTION_START 0x01
1608#define QEMU_VM_SECTION_PART 0x02
1609#define QEMU_VM_SECTION_END 0x03
1610#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001611#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001612
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001613bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001614{
1615 SaveStateEntry *se;
1616
1617 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1618 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001619 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001620 return true;
1621 }
1622 }
1623 return false;
1624}
1625
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001626void qemu_savevm_state_begin(QEMUFile *f,
1627 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001628{
1629 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001630 int ret;
aliguoria672b462008-11-11 21:33:36 +00001631
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001632 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001633 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001634 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001635 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001636 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001637 }
1638
aliguoria672b462008-11-11 21:33:36 +00001639 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1640 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1641
Blue Swirl72cf2d42009-09-12 07:36:22 +00001642 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001643 int len;
1644
Juan Quintelad1315aa2012-06-28 15:11:57 +02001645 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001646 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001647 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001648 if (se->ops && se->ops->is_active) {
1649 if (!se->ops->is_active(se->opaque)) {
1650 continue;
1651 }
1652 }
aliguoria672b462008-11-11 21:33:36 +00001653 /* Section type */
1654 qemu_put_byte(f, QEMU_VM_SECTION_START);
1655 qemu_put_be32(f, se->section_id);
1656
1657 /* ID string */
1658 len = strlen(se->idstr);
1659 qemu_put_byte(f, len);
1660 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1661
1662 qemu_put_be32(f, se->instance_id);
1663 qemu_put_be32(f, se->version_id);
1664
Juan Quintelad1315aa2012-06-28 15:11:57 +02001665 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001666 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001667 qemu_file_set_error(f, ret);
1668 break;
Juan Quintela29757252011-10-19 15:22:18 +02001669 }
aliguoria672b462008-11-11 21:33:36 +00001670 }
aliguoria672b462008-11-11 21:33:36 +00001671}
1672
Juan Quintela39346382011-09-22 11:02:14 +02001673/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001674 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001675 * negative: there was one error, and we have -errno.
1676 * 0 : We haven't finished, caller have to go again
1677 * 1 : We have finished, we can go to complete phase
1678 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001679int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001680{
1681 SaveStateEntry *se;
1682 int ret = 1;
1683
Blue Swirl72cf2d42009-09-12 07:36:22 +00001684 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001685 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001686 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001687 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001688 if (se->ops && se->ops->is_active) {
1689 if (!se->ops->is_active(se->opaque)) {
1690 continue;
1691 }
1692 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001693 if (qemu_file_rate_limit(f)) {
1694 return 0;
1695 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001696 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001697 /* Section type */
1698 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1699 qemu_put_be32(f, se->section_id);
1700
Juan Quintela16310a32012-06-28 15:31:37 +02001701 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001702 trace_savevm_section_end(se->section_id);
1703
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001704 if (ret < 0) {
1705 qemu_file_set_error(f, ret);
1706 }
Juan Quintela29757252011-10-19 15:22:18 +02001707 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001708 /* Do not proceed to the next vmstate before this one reported
1709 completion of the current stage. This serializes the migration
1710 and reduces the probability that a faster changing state is
1711 synchronized over and over again. */
1712 break;
1713 }
aliguoria672b462008-11-11 21:33:36 +00001714 }
Juan Quintela39346382011-09-22 11:02:14 +02001715 return ret;
aliguoria672b462008-11-11 21:33:36 +00001716}
1717
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001718void qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001719{
1720 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001721 int ret;
aliguoria672b462008-11-11 21:33:36 +00001722
Jan Kiszkaea375f92010-03-01 19:10:30 +01001723 cpu_synchronize_all_states();
1724
Blue Swirl72cf2d42009-09-12 07:36:22 +00001725 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001726 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001727 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001728 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001729 if (se->ops && se->ops->is_active) {
1730 if (!se->ops->is_active(se->opaque)) {
1731 continue;
1732 }
1733 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001734 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001735 /* Section type */
1736 qemu_put_byte(f, QEMU_VM_SECTION_END);
1737 qemu_put_be32(f, se->section_id);
1738
Juan Quintela16310a32012-06-28 15:31:37 +02001739 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001740 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001741 if (ret < 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001742 qemu_file_set_error(f, ret);
1743 return;
Juan Quintela29757252011-10-19 15:22:18 +02001744 }
aliguoria672b462008-11-11 21:33:36 +00001745 }
1746
Blue Swirl72cf2d42009-09-12 07:36:22 +00001747 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001748 int len;
1749
Juan Quintela22ea40f2012-06-26 17:19:10 +02001750 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001751 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001752 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001753 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001754 /* Section type */
1755 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1756 qemu_put_be32(f, se->section_id);
1757
1758 /* ID string */
1759 len = strlen(se->idstr);
1760 qemu_put_byte(f, len);
1761 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1762
1763 qemu_put_be32(f, se->instance_id);
1764 qemu_put_be32(f, se->version_id);
1765
Alex Williamsondc912122011-01-11 14:39:43 -07001766 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001767 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001768 }
1769
1770 qemu_put_byte(f, QEMU_VM_EOF);
Paolo Bonziniedaae612013-02-22 17:36:29 +01001771 qemu_fflush(f);
aliguoria672b462008-11-11 21:33:36 +00001772}
1773
Juan Quintelae4ed1542012-09-21 11:18:18 +02001774uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1775{
1776 SaveStateEntry *se;
1777 uint64_t ret = 0;
1778
1779 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1780 if (!se->ops || !se->ops->save_live_pending) {
1781 continue;
1782 }
1783 if (se->ops && se->ops->is_active) {
1784 if (!se->ops->is_active(se->opaque)) {
1785 continue;
1786 }
1787 }
1788 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1789 }
1790 return ret;
1791}
1792
Juan Quintela65227732013-01-14 14:14:42 +01001793void qemu_savevm_state_cancel(void)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001794{
1795 SaveStateEntry *se;
1796
1797 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001798 if (se->ops && se->ops->cancel) {
1799 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001800 }
1801 }
1802}
1803
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001804static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001805{
aliguoria672b462008-11-11 21:33:36 +00001806 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001807 MigrationParams params = {
1808 .blk = 0,
1809 .shared = 0
1810 };
aliguoria672b462008-11-11 21:33:36 +00001811
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001812 if (qemu_savevm_state_blocked(NULL)) {
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001813 return -EINVAL;
Alex Williamsondc912122011-01-11 14:39:43 -07001814 }
1815
Paolo Bonzini9b095032013-02-22 17:36:28 +01001816 qemu_mutex_unlock_iothread();
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001817 qemu_savevm_state_begin(f, &params);
Paolo Bonzini9b095032013-02-22 17:36:28 +01001818 qemu_mutex_lock_iothread();
1819
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001820 while (qemu_file_get_error(f) == 0) {
1821 if (qemu_savevm_state_iterate(f) > 0) {
1822 break;
1823 }
1824 }
aliguoria672b462008-11-11 21:33:36 +00001825
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001826 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001827 if (ret == 0) {
Paolo Bonzini47c8c172013-02-22 17:36:13 +01001828 qemu_savevm_state_complete(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001829 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001830 }
Paolo Bonzini04943eb2013-02-22 17:36:10 +01001831 if (ret != 0) {
1832 qemu_savevm_state_cancel();
1833 }
aliguoria672b462008-11-11 21:33:36 +00001834 return ret;
1835}
1836
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001837static int qemu_save_device_state(QEMUFile *f)
1838{
1839 SaveStateEntry *se;
1840
1841 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1842 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1843
1844 cpu_synchronize_all_states();
1845
1846 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1847 int len;
1848
1849 if (se->is_ram) {
1850 continue;
1851 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001852 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001853 continue;
1854 }
1855
1856 /* Section type */
1857 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1858 qemu_put_be32(f, se->section_id);
1859
1860 /* ID string */
1861 len = strlen(se->idstr);
1862 qemu_put_byte(f, len);
1863 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1864
1865 qemu_put_be32(f, se->instance_id);
1866 qemu_put_be32(f, se->version_id);
1867
1868 vmstate_save(f, se);
1869 }
1870
1871 qemu_put_byte(f, QEMU_VM_EOF);
1872
1873 return qemu_file_get_error(f);
1874}
1875
aliguoria672b462008-11-11 21:33:36 +00001876static SaveStateEntry *find_se(const char *idstr, int instance_id)
1877{
1878 SaveStateEntry *se;
1879
Blue Swirl72cf2d42009-09-12 07:36:22 +00001880 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001881 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001882 (instance_id == se->instance_id ||
1883 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001884 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001885 /* Migrating from an older version? */
1886 if (strstr(se->idstr, idstr) && se->compat) {
1887 if (!strcmp(se->compat->idstr, idstr) &&
1888 (instance_id == se->compat->instance_id ||
1889 instance_id == se->alias_id))
1890 return se;
1891 }
aliguoria672b462008-11-11 21:33:36 +00001892 }
1893 return NULL;
1894}
1895
Juan Quintela811814b2010-07-26 21:38:43 +02001896static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1897{
1898 while(sub && sub->needed) {
1899 if (strcmp(idstr, sub->vmsd->name) == 0) {
1900 return sub->vmsd;
1901 }
1902 sub++;
1903 }
1904 return NULL;
1905}
1906
1907static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1908 void *opaque)
1909{
Juan Quintelac6380722011-10-04 15:28:31 +02001910 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001911 char idstr[256];
1912 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001913 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001914 const VMStateDescription *sub_vmsd;
1915
Juan Quintelac6380722011-10-04 15:28:31 +02001916 len = qemu_peek_byte(f, 1);
1917 if (len < strlen(vmsd->name) + 1) {
1918 /* subsection name has be be "section_name/a" */
1919 return 0;
1920 }
1921 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1922 if (size != len) {
1923 return 0;
1924 }
1925 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001926
Juan Quintelac6380722011-10-04 15:28:31 +02001927 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1928 /* it don't have a valid subsection name */
1929 return 0;
1930 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001931 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001932 if (sub_vmsd == NULL) {
1933 return -ENOENT;
1934 }
Juan Quintelac6380722011-10-04 15:28:31 +02001935 qemu_file_skip(f, 1); /* subsection */
1936 qemu_file_skip(f, 1); /* len */
1937 qemu_file_skip(f, len); /* idstr */
1938 version_id = qemu_get_be32(f);
1939
Juan Quintela811814b2010-07-26 21:38:43 +02001940 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1941 if (ret) {
1942 return ret;
1943 }
1944 }
1945 return 0;
1946}
1947
1948static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1949 void *opaque)
1950{
1951 const VMStateSubsection *sub = vmsd->subsections;
1952
1953 while (sub && sub->needed) {
1954 if (sub->needed(opaque)) {
1955 const VMStateDescription *vmsd = sub->vmsd;
1956 uint8_t len;
1957
1958 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1959 len = strlen(vmsd->name);
1960 qemu_put_byte(f, len);
1961 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1962 qemu_put_be32(f, vmsd->version_id);
1963 vmstate_save_state(f, vmsd, opaque);
1964 }
1965 sub++;
1966 }
1967}
1968
aliguoria672b462008-11-11 21:33:36 +00001969typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001970 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001971 SaveStateEntry *se;
1972 int section_id;
1973 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001974} LoadStateEntry;
1975
aliguoria672b462008-11-11 21:33:36 +00001976int qemu_loadvm_state(QEMUFile *f)
1977{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001978 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1979 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001980 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001981 uint8_t section_type;
1982 unsigned int v;
1983 int ret;
1984
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001985 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001986 return -EINVAL;
1987 }
1988
aliguoria672b462008-11-11 21:33:36 +00001989 v = qemu_get_be32(f);
1990 if (v != QEMU_VM_FILE_MAGIC)
1991 return -EINVAL;
1992
1993 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001994 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1995 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1996 return -ENOTSUP;
1997 }
aliguoria672b462008-11-11 21:33:36 +00001998 if (v != QEMU_VM_FILE_VERSION)
1999 return -ENOTSUP;
2000
2001 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
2002 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00002003 SaveStateEntry *se;
2004 char idstr[257];
2005 int len;
2006
2007 switch (section_type) {
2008 case QEMU_VM_SECTION_START:
2009 case QEMU_VM_SECTION_FULL:
2010 /* Read section start */
2011 section_id = qemu_get_be32(f);
2012 len = qemu_get_byte(f);
2013 qemu_get_buffer(f, (uint8_t *)idstr, len);
2014 idstr[len] = 0;
2015 instance_id = qemu_get_be32(f);
2016 version_id = qemu_get_be32(f);
2017
2018 /* Find savevm section */
2019 se = find_se(idstr, instance_id);
2020 if (se == NULL) {
2021 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
2022 ret = -EINVAL;
2023 goto out;
2024 }
2025
2026 /* Validate version */
2027 if (version_id > se->version_id) {
2028 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
2029 version_id, idstr, se->version_id);
2030 ret = -EINVAL;
2031 goto out;
2032 }
2033
2034 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002035 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002036
2037 le->se = se;
2038 le->section_id = section_id;
2039 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002040 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002041
Juan Quintela4082be42009-08-20 19:42:24 +02002042 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002043 if (ret < 0) {
2044 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2045 instance_id, idstr);
2046 goto out;
2047 }
aliguoria672b462008-11-11 21:33:36 +00002048 break;
2049 case QEMU_VM_SECTION_PART:
2050 case QEMU_VM_SECTION_END:
2051 section_id = qemu_get_be32(f);
2052
Blue Swirl72cf2d42009-09-12 07:36:22 +00002053 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002054 if (le->section_id == section_id) {
2055 break;
2056 }
2057 }
aliguoria672b462008-11-11 21:33:36 +00002058 if (le == NULL) {
2059 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2060 ret = -EINVAL;
2061 goto out;
2062 }
2063
Juan Quintela4082be42009-08-20 19:42:24 +02002064 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002065 if (ret < 0) {
2066 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2067 section_id);
2068 goto out;
2069 }
aliguoria672b462008-11-11 21:33:36 +00002070 break;
2071 default:
2072 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2073 ret = -EINVAL;
2074 goto out;
2075 }
2076 }
2077
Jan Kiszkaea375f92010-03-01 19:10:30 +01002078 cpu_synchronize_all_post_init();
2079
aliguoria672b462008-11-11 21:33:36 +00002080 ret = 0;
2081
2082out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002083 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2084 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002085 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002086 }
2087
Juan Quintela42802d42011-10-05 01:14:46 +02002088 if (ret == 0) {
2089 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002090 }
aliguoria672b462008-11-11 21:33:36 +00002091
2092 return ret;
2093}
2094
aliguoria672b462008-11-11 21:33:36 +00002095static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2096 const char *name)
2097{
2098 QEMUSnapshotInfo *sn_tab, *sn;
2099 int nb_sns, i, ret;
2100
2101 ret = -ENOENT;
2102 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2103 if (nb_sns < 0)
2104 return ret;
2105 for(i = 0; i < nb_sns; i++) {
2106 sn = &sn_tab[i];
2107 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2108 *sn_info = *sn;
2109 ret = 0;
2110 break;
2111 }
2112 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002113 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002114 return ret;
2115}
2116
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002117/*
2118 * Deletes snapshots of a given name in all opened images.
2119 */
2120static int del_existing_snapshots(Monitor *mon, const char *name)
2121{
2122 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002123 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2124 int ret;
2125
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002126 bs = NULL;
2127 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002128 if (bdrv_can_snapshot(bs) &&
2129 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2130 {
2131 ret = bdrv_snapshot_delete(bs, name);
2132 if (ret < 0) {
2133 monitor_printf(mon,
2134 "Error while deleting snapshot on '%s'\n",
2135 bdrv_get_device_name(bs));
2136 return -1;
2137 }
2138 }
2139 }
2140
2141 return 0;
2142}
2143
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002144void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002145{
2146 BlockDriverState *bs, *bs1;
2147 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002148 int ret;
aliguoria672b462008-11-11 21:33:36 +00002149 QEMUFile *f;
2150 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002151 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002152 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002153 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002154 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002155
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002156 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002157 bs = NULL;
2158 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002159
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002160 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002161 continue;
2162 }
2163
2164 if (!bdrv_can_snapshot(bs)) {
2165 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2166 bdrv_get_device_name(bs));
2167 return;
2168 }
2169 }
2170
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002171 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002172 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002173 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002174 return;
2175 }
aliguoria672b462008-11-11 21:33:36 +00002176
Luiz Capitulino13548692011-07-29 15:36:43 -03002177 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002178 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002179
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002180 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002181
2182 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002183 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002184 sn->date_sec = tv.tv_sec;
2185 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002186 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002187
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002188 if (name) {
2189 ret = bdrv_snapshot_find(bs, old_sn, name);
2190 if (ret >= 0) {
2191 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2192 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2193 } else {
2194 pstrcpy(sn->name, sizeof(sn->name), name);
2195 }
2196 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002197 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2198 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002199 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002200 }
2201
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002202 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002203 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002204 goto the_end;
2205 }
2206
aliguoria672b462008-11-11 21:33:36 +00002207 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002208 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002209 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002210 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002211 goto the_end;
2212 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002213 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002214 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002215 qemu_fclose(f);
2216 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002217 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002218 goto the_end;
2219 }
2220
2221 /* create the snapshots */
2222
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002223 bs1 = NULL;
2224 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002225 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002226 /* Write VM state size only to the image that contains the state */
2227 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002228 ret = bdrv_snapshot_create(bs1, sn);
2229 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002230 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2231 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002232 }
2233 }
2234 }
2235
2236 the_end:
2237 if (saved_vm_running)
2238 vm_start();
2239}
2240
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002241void qmp_xen_save_devices_state(const char *filename, Error **errp)
2242{
2243 QEMUFile *f;
2244 int saved_vm_running;
2245 int ret;
2246
2247 saved_vm_running = runstate_is_running();
2248 vm_stop(RUN_STATE_SAVE_VM);
2249
2250 f = qemu_fopen(filename, "wb");
2251 if (!f) {
2252 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2253 goto the_end;
2254 }
2255 ret = qemu_save_device_state(f);
2256 qemu_fclose(f);
2257 if (ret < 0) {
2258 error_set(errp, QERR_IO_ERROR);
2259 }
2260
2261 the_end:
2262 if (saved_vm_running)
2263 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002264}
2265
Markus Armbruster03cd4652010-02-17 16:24:10 +01002266int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002267{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002268 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002269 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002270 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002271 int ret;
aliguoria672b462008-11-11 21:33:36 +00002272
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002273 bs_vm_state = bdrv_snapshots();
2274 if (!bs_vm_state) {
2275 error_report("No block device supports snapshots");
2276 return -ENOTSUP;
2277 }
2278
2279 /* Don't even try to load empty VM states */
2280 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2281 if (ret < 0) {
2282 return ret;
2283 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002284 error_report("This is a disk-only snapshot. Revert to it offline "
2285 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002286 return -EINVAL;
2287 }
2288
2289 /* Verify if there is any device that doesn't support snapshots and is
2290 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002291 bs = NULL;
2292 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002293
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002294 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002295 continue;
2296 }
2297
2298 if (!bdrv_can_snapshot(bs)) {
2299 error_report("Device '%s' is writable but does not support snapshots.",
2300 bdrv_get_device_name(bs));
2301 return -ENOTSUP;
2302 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002303
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002304 ret = bdrv_snapshot_find(bs, &sn, name);
2305 if (ret < 0) {
2306 error_report("Device '%s' does not have the requested snapshot '%s'",
2307 bdrv_get_device_name(bs), name);
2308 return ret;
2309 }
aliguoria672b462008-11-11 21:33:36 +00002310 }
2311
2312 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002313 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002314
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002315 bs = NULL;
2316 while ((bs = bdrv_next(bs))) {
2317 if (bdrv_can_snapshot(bs)) {
2318 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002319 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002320 error_report("Error %d while activating snapshot '%s' on '%s'",
2321 ret, name, bdrv_get_device_name(bs));
2322 return ret;
aliguoria672b462008-11-11 21:33:36 +00002323 }
2324 }
2325 }
2326
aliguoria672b462008-11-11 21:33:36 +00002327 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002328 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002329 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002330 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002331 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002332 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002333
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002334 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002335 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002336
aliguoria672b462008-11-11 21:33:36 +00002337 qemu_fclose(f);
2338 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002339 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002340 return ret;
aliguoria672b462008-11-11 21:33:36 +00002341 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002342
Juan Quintela05f24012009-08-20 19:42:22 +02002343 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002344}
2345
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002346void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002347{
2348 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002349 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002350 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002351
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002352 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002353 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002354 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002355 return;
2356 }
2357
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002358 bs1 = NULL;
2359 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002360 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002361 ret = bdrv_snapshot_delete(bs1, name);
2362 if (ret < 0) {
2363 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002364 monitor_printf(mon,
2365 "Snapshots not supported on device '%s'\n",
2366 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002367 else
aliguori376253e2009-03-05 23:01:23 +00002368 monitor_printf(mon, "Error %d while deleting snapshot on "
2369 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002370 }
2371 }
2372 }
2373}
2374
Wenchao Xia84f2d0e2013-01-14 14:06:25 +08002375void do_info_snapshots(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002376{
2377 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002378 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2379 int nb_sns, i, ret, available;
2380 int total;
2381 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002382 char buf[256];
2383
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002384 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002385 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002386 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002387 return;
2388 }
aliguoria672b462008-11-11 21:33:36 +00002389
2390 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2391 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002392 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002393 return;
2394 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002395
2396 if (nb_sns == 0) {
2397 monitor_printf(mon, "There is no snapshot available.\n");
2398 return;
aliguoria672b462008-11-11 21:33:36 +00002399 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002400
Anthony Liguori7267c092011-08-20 22:09:37 -05002401 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002402 total = 0;
2403 for (i = 0; i < nb_sns; i++) {
2404 sn = &sn_tab[i];
2405 available = 1;
2406 bs1 = NULL;
2407
2408 while ((bs1 = bdrv_next(bs1))) {
2409 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2410 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2411 if (ret < 0) {
2412 available = 0;
2413 break;
2414 }
2415 }
2416 }
2417
2418 if (available) {
2419 available_snapshots[total] = i;
2420 total++;
2421 }
2422 }
2423
2424 if (total > 0) {
2425 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2426 for (i = 0; i < total; i++) {
2427 sn = &sn_tab[available_snapshots[i]];
2428 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2429 }
2430 } else {
2431 monitor_printf(mon, "There is no suitable snapshot available\n");
2432 }
2433
Anthony Liguori7267c092011-08-20 22:09:37 -05002434 g_free(sn_tab);
2435 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002436
aliguoria672b462008-11-11 21:33:36 +00002437}
Avi Kivityc5705a72011-12-20 15:59:12 +02002438
2439void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2440{
Avi Kivity1ddde082012-01-08 13:18:19 +02002441 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002442 memory_region_name(mr), dev);
2443}
2444
2445void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2446{
2447 /* Nothing do to while the implementation is in RAMBlock */
2448}
2449
2450void vmstate_register_ram_global(MemoryRegion *mr)
2451{
2452 vmstate_register_ram(mr, NULL);
2453}