blob: 4e970ca0db47ddc26300506c04d718b5848a7194 [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
84 qemu_send_packet_raw(&nic->nc, buf, len);
85}
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
122 int64_t buf_offset; /* start of buffer when writing, end of buffer
123 when reading */
124 int buf_index;
125 int buf_size; /* 0 when writing */
126 uint8_t buf[IO_BUF_SIZE];
127
Juan Quintela3961b4d2011-10-05 01:05:21 +0200128 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000129};
130
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200131typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000132{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200133 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000134 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200135} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000136
137typedef struct QEMUFileSocket
138{
139 int fd;
140 QEMUFile *file;
141} QEMUFileSocket;
142
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200143static int socket_get_fd(void *opaque)
144{
145 QEMUFileSocket *s = opaque;
146
147 return s->fd;
148}
149
aliguoria672b462008-11-11 21:33:36 +0000150static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
151{
152 QEMUFileSocket *s = opaque;
153 ssize_t len;
154
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200155 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000156 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200157 if (len != -1) {
158 break;
159 }
160 if (socket_error() == EAGAIN) {
161 assert(qemu_in_coroutine());
162 qemu_coroutine_yield();
163 } else if (socket_error() != EINTR) {
164 break;
165 }
166 }
aliguoria672b462008-11-11 21:33:36 +0000167
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200168 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000169 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200170 }
aliguoria672b462008-11-11 21:33:36 +0000171 return len;
172}
173
174static int socket_close(void *opaque)
175{
176 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200177 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500178 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000179 return 0;
180}
181
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200182static int stdio_get_fd(void *opaque)
183{
184 QEMUFileStdio *s = opaque;
185
186 return fileno(s->stdio_file);
187}
188
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200189static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000190{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200191 QEMUFileStdio *s = opaque;
192 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000193}
194
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200195static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000196{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200197 QEMUFileStdio *s = opaque;
198 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300199 int bytes;
200
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200201 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300202 clearerr(fp);
203 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200204 if (bytes != 0 || !ferror(fp)) {
205 break;
206 }
207 if (errno == EAGAIN) {
208 assert(qemu_in_coroutine());
209 qemu_coroutine_yield();
210 } else if (errno != EINTR) {
211 break;
212 }
213 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300214 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000215}
216
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200217static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000218{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200219 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500220 int ret;
221 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200222 if (ret == -1) {
223 ret = -errno;
224 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500225 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500226 return ret;
aliguoria672b462008-11-11 21:33:36 +0000227}
228
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200229static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000230{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200231 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200232 int ret = 0;
233 if (fclose(s->stdio_file) == EOF) {
234 ret = -errno;
235 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500236 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200237 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200238}
aliguoria672b462008-11-11 21:33:36 +0000239
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200240static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200241 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200242 .get_buffer = stdio_get_buffer,
243 .close = stdio_pclose
244};
245
246static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200247 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200248 .put_buffer = stdio_put_buffer,
249 .close = stdio_pclose
250};
251
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200252QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
253{
254 QEMUFileStdio *s;
255
256 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000257 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
258 return NULL;
259 }
260
Anthony Liguori7267c092011-08-20 22:09:37 -0500261 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000262
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200263 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000264
265 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200266 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000267 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200268 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000269 }
aliguoria672b462008-11-11 21:33:36 +0000270 return s->file;
271}
272
273QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
274{
275 FILE *popen_file;
276
277 popen_file = popen(command, mode);
278 if(popen_file == NULL) {
279 return NULL;
280 }
281
282 return qemu_popen(popen_file, mode);
283}
284
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200285static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200286 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200287 .get_buffer = stdio_get_buffer,
288 .close = stdio_fclose
289};
290
291static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200292 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200293 .put_buffer = stdio_put_buffer,
294 .close = stdio_fclose
295};
296
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200297QEMUFile *qemu_fdopen(int fd, const char *mode)
298{
299 QEMUFileStdio *s;
300
301 if (mode == NULL ||
302 (mode[0] != 'r' && mode[0] != 'w') ||
303 mode[1] != 'b' || mode[2] != 0) {
304 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
305 return NULL;
306 }
307
Anthony Liguori7267c092011-08-20 22:09:37 -0500308 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200309 s->stdio_file = fdopen(fd, mode);
310 if (!s->stdio_file)
311 goto fail;
312
313 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200314 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200315 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200316 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200317 }
318 return s->file;
319
320fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500321 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200322 return NULL;
323}
324
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200325static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200326 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200327 .get_buffer = socket_get_buffer,
328 .close = socket_close
329};
330
aliguoria672b462008-11-11 21:33:36 +0000331QEMUFile *qemu_fopen_socket(int fd)
332{
Anthony Liguori7267c092011-08-20 22:09:37 -0500333 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000334
aliguoria672b462008-11-11 21:33:36 +0000335 s->fd = fd;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200336 s->file = qemu_fopen_ops(s, &socket_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000337 return s->file;
338}
339
aliguoria672b462008-11-11 21:33:36 +0000340QEMUFile *qemu_fopen(const char *filename, const char *mode)
341{
342 QEMUFileStdio *s;
343
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200344 if (mode == NULL ||
345 (mode[0] != 'r' && mode[0] != 'w') ||
346 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000347 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200348 return NULL;
349 }
350
Anthony Liguori7267c092011-08-20 22:09:37 -0500351 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000352
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200353 s->stdio_file = fopen(filename, mode);
354 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000355 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200356
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200357 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200358 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200359 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200360 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200361 }
362 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000363fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500364 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000365 return NULL;
366}
367
aliguori178e08a2009-04-05 19:10:55 +0000368static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000369 int64_t pos, int size)
370{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200371 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000372 return size;
373}
374
aliguori178e08a2009-04-05 19:10:55 +0000375static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000376{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200377 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000378}
379
380static int bdrv_fclose(void *opaque)
381{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200382 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000383}
384
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200385static const QEMUFileOps bdrv_read_ops = {
386 .get_buffer = block_get_buffer,
387 .close = bdrv_fclose
388};
389
390static const QEMUFileOps bdrv_write_ops = {
391 .put_buffer = block_put_buffer,
392 .close = bdrv_fclose
393};
394
Christoph Hellwig45566e92009-07-10 23:11:57 +0200395static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000396{
aliguoria672b462008-11-11 21:33:36 +0000397 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200398 return qemu_fopen_ops(bs, &bdrv_write_ops);
399 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000400}
401
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200402QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000403{
404 QEMUFile *f;
405
Anthony Liguori7267c092011-08-20 22:09:37 -0500406 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000407
408 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200409 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000410 f->is_write = 0;
411
412 return f;
413}
414
Juan Quintela624b9cc2011-10-05 01:02:52 +0200415int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000416{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200417 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000418}
419
Juan Quintela6f121ff2012-08-30 13:37:56 +0200420static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000421{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200422 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000423}
424
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200425/** Flushes QEMUFile buffer
426 *
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200427 */
Juan Quintela7311bea2012-08-29 19:08:59 +0200428static int qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000429{
Juan Quintela7311bea2012-08-29 19:08:59 +0200430 int ret = 0;
431
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200432 if (!f->ops->put_buffer)
Juan Quintela7311bea2012-08-29 19:08:59 +0200433 return 0;
aliguoria672b462008-11-11 21:33:36 +0000434
435 if (f->is_write && f->buf_index > 0) {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200436 ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
Juan Quintela7311bea2012-08-29 19:08:59 +0200437 if (ret >= 0) {
aliguoria672b462008-11-11 21:33:36 +0000438 f->buf_offset += f->buf_index;
Juan Quintela7311bea2012-08-29 19:08:59 +0200439 }
aliguoria672b462008-11-11 21:33:36 +0000440 f->buf_index = 0;
441 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200442 return ret;
aliguoria672b462008-11-11 21:33:36 +0000443}
444
445static void qemu_fill_buffer(QEMUFile *f)
446{
447 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200448 int pending;
aliguoria672b462008-11-11 21:33:36 +0000449
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200450 if (!f->ops->get_buffer)
aliguoria672b462008-11-11 21:33:36 +0000451 return;
452
453 if (f->is_write)
454 abort();
455
Juan Quintela0046c452011-09-30 19:28:45 +0200456 pending = f->buf_size - f->buf_index;
457 if (pending > 0) {
458 memmove(f->buf, f->buf + f->buf_index, pending);
459 }
460 f->buf_index = 0;
461 f->buf_size = pending;
462
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200463 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
Juan Quintela0046c452011-09-30 19:28:45 +0200464 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000465 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200466 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000467 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200468 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200469 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000470 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200471 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000472}
473
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200474int qemu_get_fd(QEMUFile *f)
475{
476 if (f->ops->get_fd) {
477 return f->ops->get_fd(f->opaque);
478 }
479 return -1;
480}
481
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200482/** Closes the file
483 *
484 * Returns negative error value if any error happened on previous operations or
485 * while closing the file. Returns 0 or positive number on success.
486 *
487 * The meaning of return value on success depends on the specific backend
488 * being used.
489 */
490int qemu_fclose(QEMUFile *f)
491{
Juan Quintela29eee862012-08-29 19:14:54 +0200492 int ret;
Juan Quintela7311bea2012-08-29 19:08:59 +0200493 ret = qemu_fflush(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200494
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200495 if (f->ops->close) {
496 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200497 if (ret >= 0) {
498 ret = ret2;
499 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200500 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200501 /* If any error was spotted before closing, we should report it
502 * instead of the close() return value.
503 */
504 if (f->last_error) {
505 ret = f->last_error;
506 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500507 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000508 return ret;
509}
510
aliguoria672b462008-11-11 21:33:36 +0000511void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
512{
513 int l;
514
Juan Quintelac10682c2012-08-29 19:43:39 +0200515 if (f->last_error) {
516 return;
517 }
518
519 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000520 fprintf(stderr,
521 "Attempted to write to buffer while read buffer is not empty\n");
522 abort();
523 }
524
Juan Quintelac10682c2012-08-29 19:43:39 +0200525 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000526 l = IO_BUF_SIZE - f->buf_index;
527 if (l > size)
528 l = size;
529 memcpy(f->buf + f->buf_index, buf, l);
530 f->is_write = 1;
531 f->buf_index += l;
532 buf += l;
533 size -= l;
Juan Quintela7311bea2012-08-29 19:08:59 +0200534 if (f->buf_index >= IO_BUF_SIZE) {
535 int ret = qemu_fflush(f);
Juan Quintelac10682c2012-08-29 19:43:39 +0200536 if (ret < 0) {
537 qemu_file_set_error(f, ret);
538 break;
539 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200540 }
aliguoria672b462008-11-11 21:33:36 +0000541 }
542}
543
544void qemu_put_byte(QEMUFile *f, int v)
545{
Juan Quintelac10682c2012-08-29 19:43:39 +0200546 if (f->last_error) {
547 return;
548 }
549
550 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000551 fprintf(stderr,
552 "Attempted to write to buffer while read buffer is not empty\n");
553 abort();
554 }
555
556 f->buf[f->buf_index++] = v;
557 f->is_write = 1;
Juan Quintela7311bea2012-08-29 19:08:59 +0200558 if (f->buf_index >= IO_BUF_SIZE) {
559 int ret = qemu_fflush(f);
Juan Quintelac10682c2012-08-29 19:43:39 +0200560 if (ret < 0) {
561 qemu_file_set_error(f, ret);
562 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200563 }
aliguoria672b462008-11-11 21:33:36 +0000564}
565
Juan Quintelac6380722011-10-04 15:28:31 +0200566static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000567{
Juan Quintelac6380722011-10-04 15:28:31 +0200568 if (f->buf_index + size <= f->buf_size) {
569 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000570 }
aliguoria672b462008-11-11 21:33:36 +0000571}
572
Juan Quintelac6380722011-10-04 15:28:31 +0200573static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200574{
Juan Quintelac6380722011-10-04 15:28:31 +0200575 int pending;
576 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200577
Juan Quintelab9ce1452011-10-04 13:55:32 +0200578 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200579 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200580 }
Juan Quintela811814b2010-07-26 21:38:43 +0200581
Juan Quintelac6380722011-10-04 15:28:31 +0200582 index = f->buf_index + offset;
583 pending = f->buf_size - index;
584 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200585 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200586 index = f->buf_index + offset;
587 pending = f->buf_size - index;
588 }
589
590 if (pending <= 0) {
591 return 0;
592 }
593 if (size > pending) {
594 size = pending;
595 }
596
597 memcpy(buf, f->buf + index, size);
598 return size;
599}
600
601int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
602{
603 int pending = size;
604 int done = 0;
605
606 while (pending > 0) {
607 int res;
608
609 res = qemu_peek_buffer(f, buf, pending, 0);
610 if (res == 0) {
611 return done;
612 }
613 qemu_file_skip(f, res);
614 buf += res;
615 pending -= res;
616 done += res;
617 }
618 return done;
619}
620
621static int qemu_peek_byte(QEMUFile *f, int offset)
622{
623 int index = f->buf_index + offset;
624
625 if (f->is_write) {
626 abort();
627 }
628
629 if (index >= f->buf_size) {
630 qemu_fill_buffer(f);
631 index = f->buf_index + offset;
632 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200633 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200634 }
Juan Quintela811814b2010-07-26 21:38:43 +0200635 }
Juan Quintelac6380722011-10-04 15:28:31 +0200636 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200637}
638
aliguoria672b462008-11-11 21:33:36 +0000639int qemu_get_byte(QEMUFile *f)
640{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200641 int result;
aliguoria672b462008-11-11 21:33:36 +0000642
Juan Quintelac6380722011-10-04 15:28:31 +0200643 result = qemu_peek_byte(f, 0);
644 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200645 return result;
aliguoria672b462008-11-11 21:33:36 +0000646}
647
Juan Quintela3aee4be2012-08-29 19:16:56 +0200648static int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000649{
650 return f->buf_offset - f->buf_size + f->buf_index;
651}
652
aliguoria672b462008-11-11 21:33:36 +0000653int qemu_file_rate_limit(QEMUFile *f)
654{
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200655 if (f->ops->rate_limit)
656 return f->ops->rate_limit(f->opaque);
aliguoria672b462008-11-11 21:33:36 +0000657
658 return 0;
659}
660
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200661int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200662{
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200663 if (f->ops->get_rate_limit)
664 return f->ops->get_rate_limit(f->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200665
666 return 0;
667}
668
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200669int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400670{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400671 /* any failed or completed migration keeps its state to allow probing of
672 * migration data, but has no associated file anymore */
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200673 if (f && f->ops->set_rate_limit)
674 return f->ops->set_rate_limit(f->opaque, new_rate);
Glauber Costa19629532009-05-20 18:26:57 -0400675
676 return 0;
677}
678
aliguoria672b462008-11-11 21:33:36 +0000679void qemu_put_be16(QEMUFile *f, unsigned int v)
680{
681 qemu_put_byte(f, v >> 8);
682 qemu_put_byte(f, v);
683}
684
685void qemu_put_be32(QEMUFile *f, unsigned int v)
686{
687 qemu_put_byte(f, v >> 24);
688 qemu_put_byte(f, v >> 16);
689 qemu_put_byte(f, v >> 8);
690 qemu_put_byte(f, v);
691}
692
693void qemu_put_be64(QEMUFile *f, uint64_t v)
694{
695 qemu_put_be32(f, v >> 32);
696 qemu_put_be32(f, v);
697}
698
699unsigned int qemu_get_be16(QEMUFile *f)
700{
701 unsigned int v;
702 v = qemu_get_byte(f) << 8;
703 v |= qemu_get_byte(f);
704 return v;
705}
706
707unsigned int qemu_get_be32(QEMUFile *f)
708{
709 unsigned int v;
710 v = qemu_get_byte(f) << 24;
711 v |= qemu_get_byte(f) << 16;
712 v |= qemu_get_byte(f) << 8;
713 v |= qemu_get_byte(f);
714 return v;
715}
716
717uint64_t qemu_get_be64(QEMUFile *f)
718{
719 uint64_t v;
720 v = (uint64_t)qemu_get_be32(f) << 32;
721 v |= qemu_get_be32(f);
722 return v;
723}
724
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200725
726/* timer */
727
728void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
729{
730 uint64_t expire_time;
731
732 expire_time = qemu_timer_expire_time_ns(ts);
733 qemu_put_be64(f, expire_time);
734}
735
736void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
737{
738 uint64_t expire_time;
739
740 expire_time = qemu_get_be64(f);
741 if (expire_time != -1) {
742 qemu_mod_timer_ns(ts, expire_time);
743 } else {
744 qemu_del_timer(ts);
745 }
746}
747
748
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100749/* bool */
750
751static int get_bool(QEMUFile *f, void *pv, size_t size)
752{
753 bool *v = pv;
754 *v = qemu_get_byte(f);
755 return 0;
756}
757
758static void put_bool(QEMUFile *f, void *pv, size_t size)
759{
760 bool *v = pv;
761 qemu_put_byte(f, *v);
762}
763
764const VMStateInfo vmstate_info_bool = {
765 .name = "bool",
766 .get = get_bool,
767 .put = put_bool,
768};
769
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200770/* 8 bit int */
771
772static int get_int8(QEMUFile *f, void *pv, size_t size)
773{
774 int8_t *v = pv;
775 qemu_get_s8s(f, v);
776 return 0;
777}
778
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200779static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200780{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200781 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200782 qemu_put_s8s(f, v);
783}
784
785const VMStateInfo vmstate_info_int8 = {
786 .name = "int8",
787 .get = get_int8,
788 .put = put_int8,
789};
790
791/* 16 bit int */
792
793static int get_int16(QEMUFile *f, void *pv, size_t size)
794{
795 int16_t *v = pv;
796 qemu_get_sbe16s(f, v);
797 return 0;
798}
799
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200800static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200801{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200802 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200803 qemu_put_sbe16s(f, v);
804}
805
806const VMStateInfo vmstate_info_int16 = {
807 .name = "int16",
808 .get = get_int16,
809 .put = put_int16,
810};
811
812/* 32 bit int */
813
814static int get_int32(QEMUFile *f, void *pv, size_t size)
815{
816 int32_t *v = pv;
817 qemu_get_sbe32s(f, v);
818 return 0;
819}
820
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200821static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200822{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200823 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200824 qemu_put_sbe32s(f, v);
825}
826
827const VMStateInfo vmstate_info_int32 = {
828 .name = "int32",
829 .get = get_int32,
830 .put = put_int32,
831};
832
Juan Quintela82501662009-08-20 19:42:32 +0200833/* 32 bit int. See that the received value is the same than the one
834 in the field */
835
836static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
837{
838 int32_t *v = pv;
839 int32_t v2;
840 qemu_get_sbe32s(f, &v2);
841
842 if (*v == v2)
843 return 0;
844 return -EINVAL;
845}
846
847const VMStateInfo vmstate_info_int32_equal = {
848 .name = "int32 equal",
849 .get = get_int32_equal,
850 .put = put_int32,
851};
852
Juan Quintela0a031e02009-08-20 19:42:37 +0200853/* 32 bit int. See that the received value is the less or the same
854 than the one in the field */
855
856static int get_int32_le(QEMUFile *f, void *pv, size_t size)
857{
858 int32_t *old = pv;
859 int32_t new;
860 qemu_get_sbe32s(f, &new);
861
862 if (*old <= new)
863 return 0;
864 return -EINVAL;
865}
866
867const VMStateInfo vmstate_info_int32_le = {
868 .name = "int32 equal",
869 .get = get_int32_le,
870 .put = put_int32,
871};
872
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200873/* 64 bit int */
874
875static int get_int64(QEMUFile *f, void *pv, size_t size)
876{
877 int64_t *v = pv;
878 qemu_get_sbe64s(f, v);
879 return 0;
880}
881
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200882static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200883{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200884 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200885 qemu_put_sbe64s(f, v);
886}
887
888const VMStateInfo vmstate_info_int64 = {
889 .name = "int64",
890 .get = get_int64,
891 .put = put_int64,
892};
893
894/* 8 bit unsigned int */
895
896static int get_uint8(QEMUFile *f, void *pv, size_t size)
897{
898 uint8_t *v = pv;
899 qemu_get_8s(f, v);
900 return 0;
901}
902
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200903static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200904{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200905 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200906 qemu_put_8s(f, v);
907}
908
909const VMStateInfo vmstate_info_uint8 = {
910 .name = "uint8",
911 .get = get_uint8,
912 .put = put_uint8,
913};
914
915/* 16 bit unsigned int */
916
917static int get_uint16(QEMUFile *f, void *pv, size_t size)
918{
919 uint16_t *v = pv;
920 qemu_get_be16s(f, v);
921 return 0;
922}
923
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200924static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200925{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200926 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200927 qemu_put_be16s(f, v);
928}
929
930const VMStateInfo vmstate_info_uint16 = {
931 .name = "uint16",
932 .get = get_uint16,
933 .put = put_uint16,
934};
935
936/* 32 bit unsigned int */
937
938static int get_uint32(QEMUFile *f, void *pv, size_t size)
939{
940 uint32_t *v = pv;
941 qemu_get_be32s(f, v);
942 return 0;
943}
944
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200945static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200946{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200947 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200948 qemu_put_be32s(f, v);
949}
950
951const VMStateInfo vmstate_info_uint32 = {
952 .name = "uint32",
953 .get = get_uint32,
954 .put = put_uint32,
955};
956
Juan Quintela9122a8f2011-03-10 12:33:48 +0100957/* 32 bit uint. See that the received value is the same than the one
958 in the field */
959
960static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
961{
962 uint32_t *v = pv;
963 uint32_t v2;
964 qemu_get_be32s(f, &v2);
965
966 if (*v == v2) {
967 return 0;
968 }
969 return -EINVAL;
970}
971
972const VMStateInfo vmstate_info_uint32_equal = {
973 .name = "uint32 equal",
974 .get = get_uint32_equal,
975 .put = put_uint32,
976};
977
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200978/* 64 bit unsigned int */
979
980static int get_uint64(QEMUFile *f, void *pv, size_t size)
981{
982 uint64_t *v = pv;
983 qemu_get_be64s(f, v);
984 return 0;
985}
986
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200987static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200988{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200989 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200990 qemu_put_be64s(f, v);
991}
992
993const VMStateInfo vmstate_info_uint64 = {
994 .name = "uint64",
995 .get = get_uint64,
996 .put = put_uint64,
997};
998
Juan Quintela80cd83e2009-09-10 03:04:36 +0200999/* 8 bit int. See that the received value is the same than the one
1000 in the field */
1001
1002static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1003{
1004 uint8_t *v = pv;
1005 uint8_t v2;
1006 qemu_get_8s(f, &v2);
1007
1008 if (*v == v2)
1009 return 0;
1010 return -EINVAL;
1011}
1012
1013const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001014 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001015 .get = get_uint8_equal,
1016 .put = put_uint8,
1017};
1018
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001019/* 16 bit unsigned int int. See that the received value is the same than the one
1020 in the field */
1021
1022static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1023{
1024 uint16_t *v = pv;
1025 uint16_t v2;
1026 qemu_get_be16s(f, &v2);
1027
1028 if (*v == v2)
1029 return 0;
1030 return -EINVAL;
1031}
1032
1033const VMStateInfo vmstate_info_uint16_equal = {
1034 .name = "uint16 equal",
1035 .get = get_uint16_equal,
1036 .put = put_uint16,
1037};
1038
Juan Quinteladde04632009-08-20 19:42:26 +02001039/* timers */
1040
1041static int get_timer(QEMUFile *f, void *pv, size_t size)
1042{
1043 QEMUTimer *v = pv;
1044 qemu_get_timer(f, v);
1045 return 0;
1046}
1047
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001048static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001049{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001050 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001051 qemu_put_timer(f, v);
1052}
1053
1054const VMStateInfo vmstate_info_timer = {
1055 .name = "timer",
1056 .get = get_timer,
1057 .put = put_timer,
1058};
1059
Juan Quintela6f67c502009-08-20 19:42:35 +02001060/* uint8_t buffers */
1061
1062static int get_buffer(QEMUFile *f, void *pv, size_t size)
1063{
1064 uint8_t *v = pv;
1065 qemu_get_buffer(f, v, size);
1066 return 0;
1067}
1068
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001069static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001070{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001071 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001072 qemu_put_buffer(f, v, size);
1073}
1074
1075const VMStateInfo vmstate_info_buffer = {
1076 .name = "buffer",
1077 .get = get_buffer,
1078 .put = put_buffer,
1079};
1080
Juan Quintela76507c72009-10-19 15:46:28 +02001081/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001082 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001083
1084static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1085{
Jan Kiszka21174c32009-12-02 12:36:35 +01001086 uint8_t buf[1024];
1087 int block_len;
1088
1089 while (size > 0) {
1090 block_len = MIN(sizeof(buf), size);
1091 size -= block_len;
1092 qemu_get_buffer(f, buf, block_len);
1093 }
1094 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001095}
1096
1097static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1098{
Jan Kiszka21174c32009-12-02 12:36:35 +01001099 static const uint8_t buf[1024];
1100 int block_len;
1101
1102 while (size > 0) {
1103 block_len = MIN(sizeof(buf), size);
1104 size -= block_len;
1105 qemu_put_buffer(f, buf, block_len);
1106 }
Juan Quintela76507c72009-10-19 15:46:28 +02001107}
1108
1109const VMStateInfo vmstate_info_unused_buffer = {
1110 .name = "unused_buffer",
1111 .get = get_unused_buffer,
1112 .put = put_unused_buffer,
1113};
1114
Peter Maydell08e99e22012-10-30 07:45:12 +00001115/* bitmaps (as defined by bitmap.h). Note that size here is the size
1116 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1117 * bit words with the bits in big endian order. The in-memory format
1118 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1119 */
1120/* This is the number of 64 bit words sent over the wire */
1121#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1122static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1123{
1124 unsigned long *bmp = pv;
1125 int i, idx = 0;
1126 for (i = 0; i < BITS_TO_U64S(size); i++) {
1127 uint64_t w = qemu_get_be64(f);
1128 bmp[idx++] = w;
1129 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1130 bmp[idx++] = w >> 32;
1131 }
1132 }
1133 return 0;
1134}
1135
1136static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1137{
1138 unsigned long *bmp = pv;
1139 int i, idx = 0;
1140 for (i = 0; i < BITS_TO_U64S(size); i++) {
1141 uint64_t w = bmp[idx++];
1142 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1143 w |= ((uint64_t)bmp[idx++]) << 32;
1144 }
1145 qemu_put_be64(f, w);
1146 }
1147}
1148
1149const VMStateInfo vmstate_info_bitmap = {
1150 .name = "bitmap",
1151 .get = get_bitmap,
1152 .put = put_bitmap,
1153};
1154
Alex Williamson7685ee62010-06-25 11:09:14 -06001155typedef struct CompatEntry {
1156 char idstr[256];
1157 int instance_id;
1158} CompatEntry;
1159
aliguoria672b462008-11-11 21:33:36 +00001160typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001161 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001162 char idstr[256];
1163 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001164 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001165 int version_id;
1166 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001167 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001168 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001169 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001170 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001171 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001172 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001173} SaveStateEntry;
1174
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001175
Blue Swirl72cf2d42009-09-12 07:36:22 +00001176static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1177 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001178static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001179
Juan Quintela8718e992009-09-01 02:12:31 +02001180static int calculate_new_instance_id(const char *idstr)
1181{
1182 SaveStateEntry *se;
1183 int instance_id = 0;
1184
Blue Swirl72cf2d42009-09-12 07:36:22 +00001185 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001186 if (strcmp(idstr, se->idstr) == 0
1187 && instance_id <= se->instance_id) {
1188 instance_id = se->instance_id + 1;
1189 }
1190 }
1191 return instance_id;
1192}
1193
Alex Williamson7685ee62010-06-25 11:09:14 -06001194static int calculate_compat_instance_id(const char *idstr)
1195{
1196 SaveStateEntry *se;
1197 int instance_id = 0;
1198
1199 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1200 if (!se->compat)
1201 continue;
1202
1203 if (strcmp(idstr, se->compat->idstr) == 0
1204 && instance_id <= se->compat->instance_id) {
1205 instance_id = se->compat->instance_id + 1;
1206 }
1207 }
1208 return instance_id;
1209}
1210
aliguoria672b462008-11-11 21:33:36 +00001211/* TODO: Individual devices generally have very little idea about the rest
1212 of the system, so instance_id should be removed/replaced.
1213 Meanwhile pass -1 as instance_id if you do not already have a clearly
1214 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001215int register_savevm_live(DeviceState *dev,
1216 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001217 int instance_id,
1218 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001219 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001220 void *opaque)
1221{
Juan Quintela8718e992009-09-01 02:12:31 +02001222 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001223
Anthony Liguori7267c092011-08-20 22:09:37 -05001224 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001225 se->version_id = version_id;
1226 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001227 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001228 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001229 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001230 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001231 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001232 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001233 se->is_ram = 1;
1234 }
aliguoria672b462008-11-11 21:33:36 +00001235
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001236 if (dev) {
1237 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001238 if (id) {
1239 pstrcpy(se->idstr, sizeof(se->idstr), id);
1240 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001241 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001242
Anthony Liguori7267c092011-08-20 22:09:37 -05001243 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001244 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1245 se->compat->instance_id = instance_id == -1 ?
1246 calculate_compat_instance_id(idstr) : instance_id;
1247 instance_id = -1;
1248 }
1249 }
1250 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1251
Juan Quintela8718e992009-09-01 02:12:31 +02001252 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001253 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001254 } else {
1255 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001256 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001257 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001258 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001259 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001260 return 0;
1261}
1262
Alex Williamson0be71e32010-06-25 11:09:07 -06001263int register_savevm(DeviceState *dev,
1264 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001265 int instance_id,
1266 int version_id,
1267 SaveStateHandler *save_state,
1268 LoadStateHandler *load_state,
1269 void *opaque)
1270{
Juan Quintela7908c782012-06-26 18:46:10 +02001271 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1272 ops->save_state = save_state;
1273 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001274 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001275 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001276}
1277
Alex Williamson0be71e32010-06-25 11:09:07 -06001278void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001279{
Juan Quintela8718e992009-09-01 02:12:31 +02001280 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001281 char id[256] = "";
1282
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001283 if (dev) {
1284 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001285 if (path) {
1286 pstrcpy(id, sizeof(id), path);
1287 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001288 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001289 }
1290 }
1291 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001292
Blue Swirl72cf2d42009-09-12 07:36:22 +00001293 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001294 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001295 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001296 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001297 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001298 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001299 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001300 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001301 }
aliguori41bd13a2009-04-17 17:10:59 +00001302 }
1303}
1304
Alex Williamson0be71e32010-06-25 11:09:07 -06001305int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001306 const VMStateDescription *vmsd,
1307 void *opaque, int alias_id,
1308 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001309{
Juan Quintela8718e992009-09-01 02:12:31 +02001310 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001311
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001312 /* If this triggers, alias support can be dropped for the vmsd. */
1313 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1314
Anthony Liguori7267c092011-08-20 22:09:37 -05001315 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001316 se->version_id = vmsd->version_id;
1317 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001318 se->opaque = opaque;
1319 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001320 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001321 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001322
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001323 if (dev) {
1324 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001325 if (id) {
1326 pstrcpy(se->idstr, sizeof(se->idstr), id);
1327 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001328 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001329
Anthony Liguori7267c092011-08-20 22:09:37 -05001330 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001331 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1332 se->compat->instance_id = instance_id == -1 ?
1333 calculate_compat_instance_id(vmsd->name) : instance_id;
1334 instance_id = -1;
1335 }
1336 }
1337 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1338
Juan Quintela8718e992009-09-01 02:12:31 +02001339 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001340 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001341 } else {
1342 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001343 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001344 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001345 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001346 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001347 return 0;
1348}
1349
Alex Williamson0be71e32010-06-25 11:09:07 -06001350int vmstate_register(DeviceState *dev, int instance_id,
1351 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001352{
Alex Williamson0be71e32010-06-25 11:09:07 -06001353 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1354 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001355}
1356
Alex Williamson0be71e32010-06-25 11:09:07 -06001357void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1358 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001359{
Juan Quintela1eb75382009-09-10 03:04:29 +02001360 SaveStateEntry *se, *new_se;
1361
Blue Swirl72cf2d42009-09-12 07:36:22 +00001362 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001363 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001364 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001365 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001366 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001367 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001368 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001369 }
1370 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001371}
1372
Juan Quintela811814b2010-07-26 21:38:43 +02001373static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1374 void *opaque);
1375static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1376 void *opaque);
1377
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001378int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1379 void *opaque, int version_id)
1380{
1381 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001382 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001383
1384 if (version_id > vmsd->version_id) {
1385 return -EINVAL;
1386 }
1387 if (version_id < vmsd->minimum_version_id_old) {
1388 return -EINVAL;
1389 }
1390 if (version_id < vmsd->minimum_version_id) {
1391 return vmsd->load_state_old(f, opaque, version_id);
1392 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001393 if (vmsd->pre_load) {
1394 int ret = vmsd->pre_load(opaque);
1395 if (ret)
1396 return ret;
1397 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001398 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001399 if ((field->field_exists &&
1400 field->field_exists(opaque, version_id)) ||
1401 (!field->field_exists &&
1402 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001403 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001404 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001405 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001406
Juan Quintelae61a1e02009-12-02 12:36:38 +01001407 if (field->flags & VMS_VBUFFER) {
1408 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001409 if (field->flags & VMS_MULTIPLY) {
1410 size *= field->size;
1411 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001412 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001413 if (field->flags & VMS_ARRAY) {
1414 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001415 } else if (field->flags & VMS_VARRAY_INT32) {
1416 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001417 } else if (field->flags & VMS_VARRAY_UINT32) {
1418 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001419 } else if (field->flags & VMS_VARRAY_UINT16) {
1420 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001421 } else if (field->flags & VMS_VARRAY_UINT8) {
1422 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001423 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001424 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001425 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001426 }
1427 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001428 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001429
Juan Quintela19df4382009-09-29 22:48:41 +02001430 if (field->flags & VMS_ARRAY_OF_POINTER) {
1431 addr = *(void **)addr;
1432 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001433 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001434 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001435 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001436 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001437
1438 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001439 if (ret < 0) {
1440 return ret;
1441 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001442 }
1443 }
1444 field++;
1445 }
Juan Quintela811814b2010-07-26 21:38:43 +02001446 ret = vmstate_subsection_load(f, vmsd, opaque);
1447 if (ret != 0) {
1448 return ret;
1449 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001450 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001451 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001452 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001453 return 0;
1454}
1455
1456void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001457 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001458{
1459 VMStateField *field = vmsd->fields;
1460
Juan Quintela8fb07912009-09-10 03:04:32 +02001461 if (vmsd->pre_save) {
1462 vmsd->pre_save(opaque);
1463 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001464 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001465 if (!field->field_exists ||
1466 field->field_exists(opaque, vmsd->version_id)) {
1467 void *base_addr = opaque + field->offset;
1468 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001469 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001470
Juan Quintelae61a1e02009-12-02 12:36:38 +01001471 if (field->flags & VMS_VBUFFER) {
1472 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001473 if (field->flags & VMS_MULTIPLY) {
1474 size *= field->size;
1475 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001476 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001477 if (field->flags & VMS_ARRAY) {
1478 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001479 } else if (field->flags & VMS_VARRAY_INT32) {
1480 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001481 } else if (field->flags & VMS_VARRAY_UINT32) {
1482 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001483 } else if (field->flags & VMS_VARRAY_UINT16) {
1484 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001485 } else if (field->flags & VMS_VARRAY_UINT8) {
1486 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001487 }
1488 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001489 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001490 }
1491 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001492 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001493
Juan Quintela85953872009-12-02 12:36:37 +01001494 if (field->flags & VMS_ARRAY_OF_POINTER) {
1495 addr = *(void **)addr;
1496 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001497 if (field->flags & VMS_STRUCT) {
1498 vmstate_save_state(f, field->vmsd, addr);
1499 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001500 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001501 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001502 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001503 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001504 field++;
1505 }
Juan Quintela811814b2010-07-26 21:38:43 +02001506 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001507}
1508
Juan Quintela4082be42009-08-20 19:42:24 +02001509static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1510{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001511 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001512 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001513 }
1514 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001515}
1516
Alex Williamsondc912122011-01-11 14:39:43 -07001517static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001518{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001519 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001520 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001521 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001522 }
1523 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001524}
1525
aliguoria672b462008-11-11 21:33:36 +00001526#define QEMU_VM_FILE_MAGIC 0x5145564d
1527#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1528#define QEMU_VM_FILE_VERSION 0x00000003
1529
1530#define QEMU_VM_EOF 0x00
1531#define QEMU_VM_SECTION_START 0x01
1532#define QEMU_VM_SECTION_PART 0x02
1533#define QEMU_VM_SECTION_END 0x03
1534#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001535#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001536
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001537bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001538{
1539 SaveStateEntry *se;
1540
1541 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1542 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001543 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001544 return true;
1545 }
1546 }
1547 return false;
1548}
1549
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001550int qemu_savevm_state_begin(QEMUFile *f,
1551 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001552{
1553 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001554 int ret;
aliguoria672b462008-11-11 21:33:36 +00001555
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001556 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001557 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001558 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001559 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001560 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001561 }
1562
aliguoria672b462008-11-11 21:33:36 +00001563 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1564 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1565
Blue Swirl72cf2d42009-09-12 07:36:22 +00001566 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001567 int len;
1568
Juan Quintelad1315aa2012-06-28 15:11:57 +02001569 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001570 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001571 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001572 if (se->ops && se->ops->is_active) {
1573 if (!se->ops->is_active(se->opaque)) {
1574 continue;
1575 }
1576 }
aliguoria672b462008-11-11 21:33:36 +00001577 /* Section type */
1578 qemu_put_byte(f, QEMU_VM_SECTION_START);
1579 qemu_put_be32(f, se->section_id);
1580
1581 /* ID string */
1582 len = strlen(se->idstr);
1583 qemu_put_byte(f, len);
1584 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1585
1586 qemu_put_be32(f, se->instance_id);
1587 qemu_put_be32(f, se->version_id);
1588
Juan Quintelad1315aa2012-06-28 15:11:57 +02001589 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001590 if (ret < 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001591 qemu_savevm_state_cancel(f);
Juan Quintela29757252011-10-19 15:22:18 +02001592 return ret;
1593 }
aliguoria672b462008-11-11 21:33:36 +00001594 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001595 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001596 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001597 qemu_savevm_state_cancel(f);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001598 }
aliguoria672b462008-11-11 21:33:36 +00001599
Juan Quintela39346382011-09-22 11:02:14 +02001600 return ret;
1601
aliguoria672b462008-11-11 21:33:36 +00001602}
1603
Juan Quintela39346382011-09-22 11:02:14 +02001604/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001605 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001606 * negative: there was one error, and we have -errno.
1607 * 0 : We haven't finished, caller have to go again
1608 * 1 : We have finished, we can go to complete phase
1609 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001610int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001611{
1612 SaveStateEntry *se;
1613 int ret = 1;
1614
Blue Swirl72cf2d42009-09-12 07:36:22 +00001615 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001616 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001617 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001618 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001619 if (se->ops && se->ops->is_active) {
1620 if (!se->ops->is_active(se->opaque)) {
1621 continue;
1622 }
1623 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001624 if (qemu_file_rate_limit(f)) {
1625 return 0;
1626 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001627 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001628 /* Section type */
1629 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1630 qemu_put_be32(f, se->section_id);
1631
Juan Quintela16310a32012-06-28 15:31:37 +02001632 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001633 trace_savevm_section_end(se->section_id);
1634
Juan Quintela29757252011-10-19 15:22:18 +02001635 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001636 /* Do not proceed to the next vmstate before this one reported
1637 completion of the current stage. This serializes the migration
1638 and reduces the probability that a faster changing state is
1639 synchronized over and over again. */
1640 break;
1641 }
aliguoria672b462008-11-11 21:33:36 +00001642 }
Juan Quintela39346382011-09-22 11:02:14 +02001643 if (ret != 0) {
1644 return ret;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001645 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001646 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001647 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001648 qemu_savevm_state_cancel(f);
Juan Quintela39346382011-09-22 11:02:14 +02001649 }
1650 return ret;
aliguoria672b462008-11-11 21:33:36 +00001651}
1652
Luiz Capitulino539de122011-12-05 14:06:56 -02001653int qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001654{
1655 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001656 int ret;
aliguoria672b462008-11-11 21:33:36 +00001657
Jan Kiszkaea375f92010-03-01 19:10:30 +01001658 cpu_synchronize_all_states();
1659
Blue Swirl72cf2d42009-09-12 07:36:22 +00001660 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001661 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001662 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001663 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001664 if (se->ops && se->ops->is_active) {
1665 if (!se->ops->is_active(se->opaque)) {
1666 continue;
1667 }
1668 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001669 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001670 /* Section type */
1671 qemu_put_byte(f, QEMU_VM_SECTION_END);
1672 qemu_put_be32(f, se->section_id);
1673
Juan Quintela16310a32012-06-28 15:31:37 +02001674 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001675 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001676 if (ret < 0) {
1677 return ret;
1678 }
aliguoria672b462008-11-11 21:33:36 +00001679 }
1680
Blue Swirl72cf2d42009-09-12 07:36:22 +00001681 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001682 int len;
1683
Juan Quintela22ea40f2012-06-26 17:19:10 +02001684 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001685 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001686 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001687 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001688 /* Section type */
1689 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1690 qemu_put_be32(f, se->section_id);
1691
1692 /* ID string */
1693 len = strlen(se->idstr);
1694 qemu_put_byte(f, len);
1695 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1696
1697 qemu_put_be32(f, se->instance_id);
1698 qemu_put_be32(f, se->version_id);
1699
Alex Williamsondc912122011-01-11 14:39:43 -07001700 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001701 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001702 }
1703
1704 qemu_put_byte(f, QEMU_VM_EOF);
1705
Juan Quintela624b9cc2011-10-05 01:02:52 +02001706 return qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00001707}
1708
Juan Quintelae4ed1542012-09-21 11:18:18 +02001709uint64_t qemu_savevm_state_pending(QEMUFile *f, uint64_t max_size)
1710{
1711 SaveStateEntry *se;
1712 uint64_t ret = 0;
1713
1714 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1715 if (!se->ops || !se->ops->save_live_pending) {
1716 continue;
1717 }
1718 if (se->ops && se->ops->is_active) {
1719 if (!se->ops->is_active(se->opaque)) {
1720 continue;
1721 }
1722 }
1723 ret += se->ops->save_live_pending(f, se->opaque, max_size);
1724 }
1725 return ret;
1726}
1727
Luiz Capitulino539de122011-12-05 14:06:56 -02001728void qemu_savevm_state_cancel(QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001729{
1730 SaveStateEntry *se;
1731
1732 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001733 if (se->ops && se->ops->cancel) {
1734 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001735 }
1736 }
1737}
1738
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001739static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001740{
aliguoria672b462008-11-11 21:33:36 +00001741 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001742 MigrationParams params = {
1743 .blk = 0,
1744 .shared = 0
1745 };
aliguoria672b462008-11-11 21:33:36 +00001746
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001747 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001748 ret = -EINVAL;
1749 goto out;
1750 }
1751
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001752 ret = qemu_savevm_state_begin(f, &params);
aliguoria672b462008-11-11 21:33:36 +00001753 if (ret < 0)
1754 goto out;
1755
1756 do {
Luiz Capitulino539de122011-12-05 14:06:56 -02001757 ret = qemu_savevm_state_iterate(f);
aliguoria672b462008-11-11 21:33:36 +00001758 if (ret < 0)
1759 goto out;
1760 } while (ret == 0);
1761
Luiz Capitulino539de122011-12-05 14:06:56 -02001762 ret = qemu_savevm_state_complete(f);
aliguoria672b462008-11-11 21:33:36 +00001763
1764out:
Juan Quintela39346382011-09-22 11:02:14 +02001765 if (ret == 0) {
Juan Quintela624b9cc2011-10-05 01:02:52 +02001766 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001767 }
aliguoria672b462008-11-11 21:33:36 +00001768
aliguoria672b462008-11-11 21:33:36 +00001769 return ret;
1770}
1771
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001772static int qemu_save_device_state(QEMUFile *f)
1773{
1774 SaveStateEntry *se;
1775
1776 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1777 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1778
1779 cpu_synchronize_all_states();
1780
1781 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1782 int len;
1783
1784 if (se->is_ram) {
1785 continue;
1786 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001787 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001788 continue;
1789 }
1790
1791 /* Section type */
1792 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1793 qemu_put_be32(f, se->section_id);
1794
1795 /* ID string */
1796 len = strlen(se->idstr);
1797 qemu_put_byte(f, len);
1798 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1799
1800 qemu_put_be32(f, se->instance_id);
1801 qemu_put_be32(f, se->version_id);
1802
1803 vmstate_save(f, se);
1804 }
1805
1806 qemu_put_byte(f, QEMU_VM_EOF);
1807
1808 return qemu_file_get_error(f);
1809}
1810
aliguoria672b462008-11-11 21:33:36 +00001811static SaveStateEntry *find_se(const char *idstr, int instance_id)
1812{
1813 SaveStateEntry *se;
1814
Blue Swirl72cf2d42009-09-12 07:36:22 +00001815 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001816 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001817 (instance_id == se->instance_id ||
1818 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001819 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001820 /* Migrating from an older version? */
1821 if (strstr(se->idstr, idstr) && se->compat) {
1822 if (!strcmp(se->compat->idstr, idstr) &&
1823 (instance_id == se->compat->instance_id ||
1824 instance_id == se->alias_id))
1825 return se;
1826 }
aliguoria672b462008-11-11 21:33:36 +00001827 }
1828 return NULL;
1829}
1830
Juan Quintela811814b2010-07-26 21:38:43 +02001831static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1832{
1833 while(sub && sub->needed) {
1834 if (strcmp(idstr, sub->vmsd->name) == 0) {
1835 return sub->vmsd;
1836 }
1837 sub++;
1838 }
1839 return NULL;
1840}
1841
1842static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1843 void *opaque)
1844{
Juan Quintelac6380722011-10-04 15:28:31 +02001845 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001846 char idstr[256];
1847 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001848 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001849 const VMStateDescription *sub_vmsd;
1850
Juan Quintelac6380722011-10-04 15:28:31 +02001851 len = qemu_peek_byte(f, 1);
1852 if (len < strlen(vmsd->name) + 1) {
1853 /* subsection name has be be "section_name/a" */
1854 return 0;
1855 }
1856 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1857 if (size != len) {
1858 return 0;
1859 }
1860 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001861
Juan Quintelac6380722011-10-04 15:28:31 +02001862 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1863 /* it don't have a valid subsection name */
1864 return 0;
1865 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001866 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001867 if (sub_vmsd == NULL) {
1868 return -ENOENT;
1869 }
Juan Quintelac6380722011-10-04 15:28:31 +02001870 qemu_file_skip(f, 1); /* subsection */
1871 qemu_file_skip(f, 1); /* len */
1872 qemu_file_skip(f, len); /* idstr */
1873 version_id = qemu_get_be32(f);
1874
Juan Quintela811814b2010-07-26 21:38:43 +02001875 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1876 if (ret) {
1877 return ret;
1878 }
1879 }
1880 return 0;
1881}
1882
1883static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1884 void *opaque)
1885{
1886 const VMStateSubsection *sub = vmsd->subsections;
1887
1888 while (sub && sub->needed) {
1889 if (sub->needed(opaque)) {
1890 const VMStateDescription *vmsd = sub->vmsd;
1891 uint8_t len;
1892
1893 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1894 len = strlen(vmsd->name);
1895 qemu_put_byte(f, len);
1896 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1897 qemu_put_be32(f, vmsd->version_id);
1898 vmstate_save_state(f, vmsd, opaque);
1899 }
1900 sub++;
1901 }
1902}
1903
aliguoria672b462008-11-11 21:33:36 +00001904typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001905 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001906 SaveStateEntry *se;
1907 int section_id;
1908 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001909} LoadStateEntry;
1910
aliguoria672b462008-11-11 21:33:36 +00001911int qemu_loadvm_state(QEMUFile *f)
1912{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001913 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1914 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001915 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001916 uint8_t section_type;
1917 unsigned int v;
1918 int ret;
1919
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001920 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001921 return -EINVAL;
1922 }
1923
aliguoria672b462008-11-11 21:33:36 +00001924 v = qemu_get_be32(f);
1925 if (v != QEMU_VM_FILE_MAGIC)
1926 return -EINVAL;
1927
1928 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001929 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1930 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1931 return -ENOTSUP;
1932 }
aliguoria672b462008-11-11 21:33:36 +00001933 if (v != QEMU_VM_FILE_VERSION)
1934 return -ENOTSUP;
1935
1936 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1937 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001938 SaveStateEntry *se;
1939 char idstr[257];
1940 int len;
1941
1942 switch (section_type) {
1943 case QEMU_VM_SECTION_START:
1944 case QEMU_VM_SECTION_FULL:
1945 /* Read section start */
1946 section_id = qemu_get_be32(f);
1947 len = qemu_get_byte(f);
1948 qemu_get_buffer(f, (uint8_t *)idstr, len);
1949 idstr[len] = 0;
1950 instance_id = qemu_get_be32(f);
1951 version_id = qemu_get_be32(f);
1952
1953 /* Find savevm section */
1954 se = find_se(idstr, instance_id);
1955 if (se == NULL) {
1956 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1957 ret = -EINVAL;
1958 goto out;
1959 }
1960
1961 /* Validate version */
1962 if (version_id > se->version_id) {
1963 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1964 version_id, idstr, se->version_id);
1965 ret = -EINVAL;
1966 goto out;
1967 }
1968
1969 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05001970 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001971
1972 le->se = se;
1973 le->section_id = section_id;
1974 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001975 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001976
Juan Quintela4082be42009-08-20 19:42:24 +02001977 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001978 if (ret < 0) {
1979 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1980 instance_id, idstr);
1981 goto out;
1982 }
aliguoria672b462008-11-11 21:33:36 +00001983 break;
1984 case QEMU_VM_SECTION_PART:
1985 case QEMU_VM_SECTION_END:
1986 section_id = qemu_get_be32(f);
1987
Blue Swirl72cf2d42009-09-12 07:36:22 +00001988 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001989 if (le->section_id == section_id) {
1990 break;
1991 }
1992 }
aliguoria672b462008-11-11 21:33:36 +00001993 if (le == NULL) {
1994 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1995 ret = -EINVAL;
1996 goto out;
1997 }
1998
Juan Quintela4082be42009-08-20 19:42:24 +02001999 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002000 if (ret < 0) {
2001 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2002 section_id);
2003 goto out;
2004 }
aliguoria672b462008-11-11 21:33:36 +00002005 break;
2006 default:
2007 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2008 ret = -EINVAL;
2009 goto out;
2010 }
2011 }
2012
Jan Kiszkaea375f92010-03-01 19:10:30 +01002013 cpu_synchronize_all_post_init();
2014
aliguoria672b462008-11-11 21:33:36 +00002015 ret = 0;
2016
2017out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002018 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2019 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002020 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002021 }
2022
Juan Quintela42802d42011-10-05 01:14:46 +02002023 if (ret == 0) {
2024 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002025 }
aliguoria672b462008-11-11 21:33:36 +00002026
2027 return ret;
2028}
2029
aliguoria672b462008-11-11 21:33:36 +00002030static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2031 const char *name)
2032{
2033 QEMUSnapshotInfo *sn_tab, *sn;
2034 int nb_sns, i, ret;
2035
2036 ret = -ENOENT;
2037 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2038 if (nb_sns < 0)
2039 return ret;
2040 for(i = 0; i < nb_sns; i++) {
2041 sn = &sn_tab[i];
2042 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2043 *sn_info = *sn;
2044 ret = 0;
2045 break;
2046 }
2047 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002048 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002049 return ret;
2050}
2051
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002052/*
2053 * Deletes snapshots of a given name in all opened images.
2054 */
2055static int del_existing_snapshots(Monitor *mon, const char *name)
2056{
2057 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002058 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2059 int ret;
2060
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002061 bs = NULL;
2062 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002063 if (bdrv_can_snapshot(bs) &&
2064 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2065 {
2066 ret = bdrv_snapshot_delete(bs, name);
2067 if (ret < 0) {
2068 monitor_printf(mon,
2069 "Error while deleting snapshot on '%s'\n",
2070 bdrv_get_device_name(bs));
2071 return -1;
2072 }
2073 }
2074 }
2075
2076 return 0;
2077}
2078
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002079void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002080{
2081 BlockDriverState *bs, *bs1;
2082 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002083 int ret;
aliguoria672b462008-11-11 21:33:36 +00002084 QEMUFile *f;
2085 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002086 uint64_t vm_state_size;
Stefan Weil68b891e2013-01-07 22:20:27 +01002087 qemu_timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002088 struct tm tm;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002089 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002090
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002091 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002092 bs = NULL;
2093 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002094
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002095 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002096 continue;
2097 }
2098
2099 if (!bdrv_can_snapshot(bs)) {
2100 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2101 bdrv_get_device_name(bs));
2102 return;
2103 }
2104 }
2105
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002106 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002107 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002108 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002109 return;
2110 }
aliguoria672b462008-11-11 21:33:36 +00002111
Luiz Capitulino13548692011-07-29 15:36:43 -03002112 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002113 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002114
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002115 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002116
2117 /* fill auxiliary fields */
Stefan Weil68b891e2013-01-07 22:20:27 +01002118 qemu_gettimeofday(&tv);
aliguoria672b462008-11-11 21:33:36 +00002119 sn->date_sec = tv.tv_sec;
2120 sn->date_nsec = tv.tv_usec * 1000;
Paolo Bonzini74475452011-03-11 16:47:48 +01002121 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002122
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002123 if (name) {
2124 ret = bdrv_snapshot_find(bs, old_sn, name);
2125 if (ret >= 0) {
2126 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2127 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2128 } else {
2129 pstrcpy(sn->name, sizeof(sn->name), name);
2130 }
2131 } else {
Blue Swirld7d9b522010-09-09 19:13:04 +00002132 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2133 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002134 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002135 }
2136
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002137 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002138 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002139 goto the_end;
2140 }
2141
aliguoria672b462008-11-11 21:33:36 +00002142 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002143 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002144 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002145 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002146 goto the_end;
2147 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002148 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002149 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002150 qemu_fclose(f);
2151 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002152 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002153 goto the_end;
2154 }
2155
2156 /* create the snapshots */
2157
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002158 bs1 = NULL;
2159 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002160 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002161 /* Write VM state size only to the image that contains the state */
2162 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002163 ret = bdrv_snapshot_create(bs1, sn);
2164 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002165 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2166 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002167 }
2168 }
2169 }
2170
2171 the_end:
2172 if (saved_vm_running)
2173 vm_start();
2174}
2175
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002176void qmp_xen_save_devices_state(const char *filename, Error **errp)
2177{
2178 QEMUFile *f;
2179 int saved_vm_running;
2180 int ret;
2181
2182 saved_vm_running = runstate_is_running();
2183 vm_stop(RUN_STATE_SAVE_VM);
2184
2185 f = qemu_fopen(filename, "wb");
2186 if (!f) {
2187 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2188 goto the_end;
2189 }
2190 ret = qemu_save_device_state(f);
2191 qemu_fclose(f);
2192 if (ret < 0) {
2193 error_set(errp, QERR_IO_ERROR);
2194 }
2195
2196 the_end:
2197 if (saved_vm_running)
2198 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002199}
2200
Markus Armbruster03cd4652010-02-17 16:24:10 +01002201int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002202{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002203 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002204 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002205 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002206 int ret;
aliguoria672b462008-11-11 21:33:36 +00002207
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002208 bs_vm_state = bdrv_snapshots();
2209 if (!bs_vm_state) {
2210 error_report("No block device supports snapshots");
2211 return -ENOTSUP;
2212 }
2213
2214 /* Don't even try to load empty VM states */
2215 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2216 if (ret < 0) {
2217 return ret;
2218 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002219 error_report("This is a disk-only snapshot. Revert to it offline "
2220 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002221 return -EINVAL;
2222 }
2223
2224 /* Verify if there is any device that doesn't support snapshots and is
2225 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002226 bs = NULL;
2227 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002228
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002229 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002230 continue;
2231 }
2232
2233 if (!bdrv_can_snapshot(bs)) {
2234 error_report("Device '%s' is writable but does not support snapshots.",
2235 bdrv_get_device_name(bs));
2236 return -ENOTSUP;
2237 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002238
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002239 ret = bdrv_snapshot_find(bs, &sn, name);
2240 if (ret < 0) {
2241 error_report("Device '%s' does not have the requested snapshot '%s'",
2242 bdrv_get_device_name(bs), name);
2243 return ret;
2244 }
aliguoria672b462008-11-11 21:33:36 +00002245 }
2246
2247 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002248 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002249
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002250 bs = NULL;
2251 while ((bs = bdrv_next(bs))) {
2252 if (bdrv_can_snapshot(bs)) {
2253 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002254 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002255 error_report("Error %d while activating snapshot '%s' on '%s'",
2256 ret, name, bdrv_get_device_name(bs));
2257 return ret;
aliguoria672b462008-11-11 21:33:36 +00002258 }
2259 }
2260 }
2261
aliguoria672b462008-11-11 21:33:36 +00002262 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002263 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002264 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002265 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002266 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002267 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002268
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002269 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002270 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002271
aliguoria672b462008-11-11 21:33:36 +00002272 qemu_fclose(f);
2273 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002274 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002275 return ret;
aliguoria672b462008-11-11 21:33:36 +00002276 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002277
Juan Quintela05f24012009-08-20 19:42:22 +02002278 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002279}
2280
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002281void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002282{
2283 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002284 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002285 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002286
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002287 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002288 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002289 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002290 return;
2291 }
2292
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002293 bs1 = NULL;
2294 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002295 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002296 ret = bdrv_snapshot_delete(bs1, name);
2297 if (ret < 0) {
2298 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002299 monitor_printf(mon,
2300 "Snapshots not supported on device '%s'\n",
2301 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002302 else
aliguori376253e2009-03-05 23:01:23 +00002303 monitor_printf(mon, "Error %d while deleting snapshot on "
2304 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002305 }
2306 }
2307 }
2308}
2309
aliguori376253e2009-03-05 23:01:23 +00002310void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002311{
2312 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002313 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2314 int nb_sns, i, ret, available;
2315 int total;
2316 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002317 char buf[256];
2318
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002319 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002320 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002321 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002322 return;
2323 }
aliguoria672b462008-11-11 21:33:36 +00002324
2325 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2326 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002327 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002328 return;
2329 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002330
2331 if (nb_sns == 0) {
2332 monitor_printf(mon, "There is no snapshot available.\n");
2333 return;
aliguoria672b462008-11-11 21:33:36 +00002334 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002335
Anthony Liguori7267c092011-08-20 22:09:37 -05002336 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002337 total = 0;
2338 for (i = 0; i < nb_sns; i++) {
2339 sn = &sn_tab[i];
2340 available = 1;
2341 bs1 = NULL;
2342
2343 while ((bs1 = bdrv_next(bs1))) {
2344 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2345 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2346 if (ret < 0) {
2347 available = 0;
2348 break;
2349 }
2350 }
2351 }
2352
2353 if (available) {
2354 available_snapshots[total] = i;
2355 total++;
2356 }
2357 }
2358
2359 if (total > 0) {
2360 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2361 for (i = 0; i < total; i++) {
2362 sn = &sn_tab[available_snapshots[i]];
2363 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2364 }
2365 } else {
2366 monitor_printf(mon, "There is no suitable snapshot available\n");
2367 }
2368
Anthony Liguori7267c092011-08-20 22:09:37 -05002369 g_free(sn_tab);
2370 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002371
aliguoria672b462008-11-11 21:33:36 +00002372}
Avi Kivityc5705a72011-12-20 15:59:12 +02002373
2374void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2375{
Avi Kivity1ddde082012-01-08 13:18:19 +02002376 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002377 memory_region_name(mr), dev);
2378}
2379
2380void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2381{
2382 /* Nothing do to while the implementation is in RAMBlock */
2383}
2384
2385void vmstate_register_ram_global(MemoryRegion *mr)
2386{
2387 vmstate_register_ram(mr, NULL);
2388}
Orit Wasserman302dfbe2012-08-06 21:42:52 +03002389
2390/*
2391 page = zrun nzrun
2392 | zrun nzrun page
2393
2394 zrun = length
2395
2396 nzrun = length byte...
2397
2398 length = uleb128 encoded integer
2399 */
2400int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2401 uint8_t *dst, int dlen)
2402{
2403 uint32_t zrun_len = 0, nzrun_len = 0;
2404 int d = 0, i = 0;
2405 long res, xor;
2406 uint8_t *nzrun_start = NULL;
2407
2408 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2409 sizeof(long)));
2410
2411 while (i < slen) {
2412 /* overflow */
2413 if (d + 2 > dlen) {
2414 return -1;
2415 }
2416
2417 /* not aligned to sizeof(long) */
2418 res = (slen - i) % sizeof(long);
2419 while (res && old_buf[i] == new_buf[i]) {
2420 zrun_len++;
2421 i++;
2422 res--;
2423 }
2424
2425 /* word at a time for speed */
2426 if (!res) {
2427 while (i < slen &&
2428 (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2429 i += sizeof(long);
2430 zrun_len += sizeof(long);
2431 }
2432
2433 /* go over the rest */
2434 while (i < slen && old_buf[i] == new_buf[i]) {
2435 zrun_len++;
2436 i++;
2437 }
2438 }
2439
2440 /* buffer unchanged */
2441 if (zrun_len == slen) {
2442 return 0;
2443 }
2444
2445 /* skip last zero run */
2446 if (i == slen) {
2447 return d;
2448 }
2449
2450 d += uleb128_encode_small(dst + d, zrun_len);
2451
2452 zrun_len = 0;
2453 nzrun_start = new_buf + i;
2454
2455 /* overflow */
2456 if (d + 2 > dlen) {
2457 return -1;
2458 }
2459 /* not aligned to sizeof(long) */
2460 res = (slen - i) % sizeof(long);
2461 while (res && old_buf[i] != new_buf[i]) {
2462 i++;
2463 nzrun_len++;
2464 res--;
2465 }
2466
2467 /* word at a time for speed, use of 32-bit long okay */
2468 if (!res) {
2469 /* truncation to 32-bit long okay */
Alexander Grafa5b71722012-08-14 12:53:18 +02002470 long mask = (long)0x0101010101010101ULL;
Orit Wasserman302dfbe2012-08-06 21:42:52 +03002471 while (i < slen) {
2472 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2473 if ((xor - mask) & ~xor & (mask << 7)) {
2474 /* found the end of an nzrun within the current long */
2475 while (old_buf[i] != new_buf[i]) {
2476 nzrun_len++;
2477 i++;
2478 }
2479 break;
2480 } else {
2481 i += sizeof(long);
2482 nzrun_len += sizeof(long);
2483 }
2484 }
2485 }
2486
2487 d += uleb128_encode_small(dst + d, nzrun_len);
2488 /* overflow */
2489 if (d + nzrun_len > dlen) {
2490 return -1;
2491 }
2492 memcpy(dst + d, nzrun_start, nzrun_len);
2493 d += nzrun_len;
2494 nzrun_len = 0;
2495 }
2496
2497 return d;
2498}
2499
2500int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2501{
2502 int i = 0, d = 0;
2503 int ret;
2504 uint32_t count = 0;
2505
2506 while (i < slen) {
2507
2508 /* zrun */
2509 if ((slen - i) < 2) {
2510 return -1;
2511 }
2512
2513 ret = uleb128_decode_small(src + i, &count);
2514 if (ret < 0 || (i && !count)) {
2515 return -1;
2516 }
2517 i += ret;
2518 d += count;
2519
2520 /* overflow */
2521 if (d > dlen) {
2522 return -1;
2523 }
2524
2525 /* nzrun */
2526 if ((slen - i) < 2) {
2527 return -1;
2528 }
2529
2530 ret = uleb128_decode_small(src + i, &count);
2531 if (ret < 0 || !count) {
2532 return -1;
2533 }
2534 i += ret;
2535
2536 /* overflow */
2537 if (d + count > dlen || i + count > slen) {
2538 return -1;
2539 }
2540
2541 memcpy(dst + d, src + i, count);
2542 d += count;
2543 i += count;
2544 }
2545
2546 return d;
2547}