blob: bf341bb25f41236be1cb0a2457b6780f17fec9a4 [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#include <unistd.h>
25#include <fcntl.h>
aliguoria672b462008-11-11 21:33:36 +000026#include <time.h>
27#include <errno.h>
28#include <sys/time.h>
29#include <zlib.h>
30
Juan Quintela71e72a12009-07-27 16:12:56 +020031/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000032#include "config-host.h"
33
aliguoria672b462008-11-11 21:33:36 +000034#ifndef _WIN32
35#include <sys/times.h>
36#include <sys/wait.h>
37#include <termios.h>
38#include <sys/mman.h>
39#include <sys/ioctl.h>
40#include <sys/resource.h>
41#include <sys/socket.h>
42#include <netinet/in.h>
43#include <net/if.h>
aliguoria672b462008-11-11 21:33:36 +000044#include <arpa/inet.h>
45#include <dirent.h>
46#include <netdb.h>
47#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020048#ifdef CONFIG_BSD
aliguoria672b462008-11-11 21:33:36 +000049#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010050#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
aliguoria672b462008-11-11 21:33:36 +000051#include <libutil.h>
52#else
53#include <util.h>
54#endif
aliguoria672b462008-11-11 21:33:36 +000055#ifdef __linux__
56#include <pty.h>
57#include <malloc.h>
58#include <linux/rtc.h>
59#endif
60#endif
61#endif
62
63#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000064#include <windows.h>
aliguoria672b462008-11-11 21:33:36 +000065#include <malloc.h>
66#include <sys/timeb.h>
67#include <mmsystem.h>
68#define getopt_long_only getopt_long
69#define memalign(align, size) malloc(size)
70#endif
71
blueswir1511d2b12009-03-07 15:32:56 +000072#include "qemu-common.h"
73#include "hw/hw.h"
Alex Williamson7685ee62010-06-25 11:09:14 -060074#include "hw/qdev.h"
Paolo Bonzini1422e322012-10-24 08:43:34 +020075#include "net/net.h"
Paolo Bonzini83c90892012-12-17 18:19:49 +010076#include "monitor/monitor.h"
blueswir1511d2b12009-03-07 15:32:56 +000077#include "sysemu.h"
78#include "qemu-timer.h"
blueswir1511d2b12009-03-07 15:32:56 +000079#include "audio/audio.h"
80#include "migration.h"
81#include "qemu_socket.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +000082#include "qemu-queue.h"
Paolo Bonzini2ff68d02011-09-12 16:21:44 +020083#include "qemu-timer.h"
Blue Swirl17a46632011-03-27 16:05:08 +000084#include "cpus.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +010085#include "exec/memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000086#include "qmp-commands.h"
Juan Quintela517a13c2012-05-21 23:46:44 +020087#include "trace.h"
Peter Maydell08e99e22012-10-30 07:45:12 +000088#include "bitops.h"
blueswir1511d2b12009-03-07 15:32:56 +000089
aliguoria672b462008-11-11 21:33:36 +000090#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000091
Nolan18995b92009-10-15 16:53:55 -070092#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040093#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070094#endif
95#define ARP_HTYPE_ETH 0x0001
96#define ARP_PTYPE_IP 0x0800
97#define ARP_OP_REQUEST_REV 0x3
98
99static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000100 uint8_t *mac_addr)
101{
Nolan18995b92009-10-15 16:53:55 -0700102 /* Ethernet header. */
103 memset(buf, 0xff, 6); /* destination MAC addr */
104 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
105 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000106
Nolan18995b92009-10-15 16:53:55 -0700107 /* RARP header. */
108 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
109 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
110 *(buf + 18) = 6; /* hardware addr length (ethernet) */
111 *(buf + 19) = 4; /* protocol addr length (IPv4) */
112 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
113 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
114 memset(buf + 28, 0x00, 4); /* source protocol addr */
115 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
116 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000117
Nolan18995b92009-10-15 16:53:55 -0700118 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
119 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000120
Nolan18995b92009-10-15 16:53:55 -0700121 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000122}
123
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000124static void qemu_announce_self_iter(NICState *nic, void *opaque)
125{
126 uint8_t buf[60];
127 int len;
128
129 len = announce_self_create(buf, nic->conf->macaddr.a);
130
131 qemu_send_packet_raw(&nic->nc, buf, len);
132}
133
134
Gleb Natapoved8b3302009-05-21 17:17:44 +0300135static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000136{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300137 static int count = SELF_ANNOUNCE_ROUNDS;
138 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000139
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000140 qemu_foreach_nic(qemu_announce_self_iter, NULL);
141
Nolan18995b92009-10-15 16:53:55 -0700142 if (--count) {
143 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100144 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700145 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300146 } else {
147 qemu_del_timer(timer);
148 qemu_free_timer(timer);
149 }
150}
151
152void qemu_announce_self(void)
153{
154 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100155 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300156 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000157}
158
159/***********************************************************/
160/* savevm/loadvm support */
161
162#define IO_BUF_SIZE 32768
163
164struct QEMUFile {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200165 const QEMUFileOps *ops;
aliguoria672b462008-11-11 21:33:36 +0000166 void *opaque;
167 int is_write;
168
169 int64_t buf_offset; /* start of buffer when writing, end of buffer
170 when reading */
171 int buf_index;
172 int buf_size; /* 0 when writing */
173 uint8_t buf[IO_BUF_SIZE];
174
Juan Quintela3961b4d2011-10-05 01:05:21 +0200175 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000176};
177
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200178typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000179{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200180 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000181 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200182} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000183
184typedef struct QEMUFileSocket
185{
186 int fd;
187 QEMUFile *file;
188} QEMUFileSocket;
189
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200190static int socket_get_fd(void *opaque)
191{
192 QEMUFileSocket *s = opaque;
193
194 return s->fd;
195}
196
aliguoria672b462008-11-11 21:33:36 +0000197static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
198{
199 QEMUFileSocket *s = opaque;
200 ssize_t len;
201
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200202 for (;;) {
Blue Swirl00aa0042011-07-23 20:04:29 +0000203 len = qemu_recv(s->fd, buf, size, 0);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200204 if (len != -1) {
205 break;
206 }
207 if (socket_error() == EAGAIN) {
208 assert(qemu_in_coroutine());
209 qemu_coroutine_yield();
210 } else if (socket_error() != EINTR) {
211 break;
212 }
213 }
aliguoria672b462008-11-11 21:33:36 +0000214
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200215 if (len == -1) {
aliguoria672b462008-11-11 21:33:36 +0000216 len = -socket_error();
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200217 }
aliguoria672b462008-11-11 21:33:36 +0000218 return len;
219}
220
221static int socket_close(void *opaque)
222{
223 QEMUFileSocket *s = opaque;
Paolo Bonziniab52a822012-08-07 10:50:26 +0200224 closesocket(s->fd);
Anthony Liguori7267c092011-08-20 22:09:37 -0500225 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000226 return 0;
227}
228
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200229static int stdio_get_fd(void *opaque)
230{
231 QEMUFileStdio *s = opaque;
232
233 return fileno(s->stdio_file);
234}
235
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200236static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000237{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200238 QEMUFileStdio *s = opaque;
239 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000240}
241
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200242static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000243{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200244 QEMUFileStdio *s = opaque;
245 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300246 int bytes;
247
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200248 for (;;) {
Uri Lublin8a67ec42009-06-08 19:27:21 +0300249 clearerr(fp);
250 bytes = fread(buf, 1, size, fp);
Paolo Bonzini595ab6412012-08-07 11:07:59 +0200251 if (bytes != 0 || !ferror(fp)) {
252 break;
253 }
254 if (errno == EAGAIN) {
255 assert(qemu_in_coroutine());
256 qemu_coroutine_yield();
257 } else if (errno != EINTR) {
258 break;
259 }
260 }
Uri Lublin8a67ec42009-06-08 19:27:21 +0300261 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000262}
263
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200264static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000265{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200266 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500267 int ret;
268 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200269 if (ret == -1) {
270 ret = -errno;
271 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500272 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500273 return ret;
aliguoria672b462008-11-11 21:33:36 +0000274}
275
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200276static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000277{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200278 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200279 int ret = 0;
280 if (fclose(s->stdio_file) == EOF) {
281 ret = -errno;
282 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500283 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200284 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200285}
aliguoria672b462008-11-11 21:33:36 +0000286
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200287static const QEMUFileOps stdio_pipe_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200288 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200289 .get_buffer = stdio_get_buffer,
290 .close = stdio_pclose
291};
292
293static const QEMUFileOps stdio_pipe_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200294 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200295 .put_buffer = stdio_put_buffer,
296 .close = stdio_pclose
297};
298
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200299QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
300{
301 QEMUFileStdio *s;
302
303 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000304 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
305 return NULL;
306 }
307
Anthony Liguori7267c092011-08-20 22:09:37 -0500308 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000309
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200310 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000311
312 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200313 s->file = qemu_fopen_ops(s, &stdio_pipe_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000314 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200315 s->file = qemu_fopen_ops(s, &stdio_pipe_write_ops);
aliguoria672b462008-11-11 21:33:36 +0000316 }
aliguoria672b462008-11-11 21:33:36 +0000317 return s->file;
318}
319
320QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
321{
322 FILE *popen_file;
323
324 popen_file = popen(command, mode);
325 if(popen_file == NULL) {
326 return NULL;
327 }
328
329 return qemu_popen(popen_file, mode);
330}
331
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200332static const QEMUFileOps stdio_file_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200333 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200334 .get_buffer = stdio_get_buffer,
335 .close = stdio_fclose
336};
337
338static const QEMUFileOps stdio_file_write_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200339 .get_fd = stdio_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200340 .put_buffer = stdio_put_buffer,
341 .close = stdio_fclose
342};
343
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200344QEMUFile *qemu_fdopen(int fd, const char *mode)
345{
346 QEMUFileStdio *s;
347
348 if (mode == NULL ||
349 (mode[0] != 'r' && mode[0] != 'w') ||
350 mode[1] != 'b' || mode[2] != 0) {
351 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
352 return NULL;
353 }
354
Anthony Liguori7267c092011-08-20 22:09:37 -0500355 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200356 s->stdio_file = fdopen(fd, mode);
357 if (!s->stdio_file)
358 goto fail;
359
360 if(mode[0] == 'r') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200361 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200362 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200363 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200364 }
365 return s->file;
366
367fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500368 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200369 return NULL;
370}
371
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200372static const QEMUFileOps socket_read_ops = {
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200373 .get_fd = socket_get_fd,
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200374 .get_buffer = socket_get_buffer,
375 .close = socket_close
376};
377
aliguoria672b462008-11-11 21:33:36 +0000378QEMUFile *qemu_fopen_socket(int fd)
379{
Anthony Liguori7267c092011-08-20 22:09:37 -0500380 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000381
aliguoria672b462008-11-11 21:33:36 +0000382 s->fd = fd;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200383 s->file = qemu_fopen_ops(s, &socket_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000384 return s->file;
385}
386
aliguoria672b462008-11-11 21:33:36 +0000387QEMUFile *qemu_fopen(const char *filename, const char *mode)
388{
389 QEMUFileStdio *s;
390
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200391 if (mode == NULL ||
392 (mode[0] != 'r' && mode[0] != 'w') ||
393 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000394 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200395 return NULL;
396 }
397
Anthony Liguori7267c092011-08-20 22:09:37 -0500398 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000399
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200400 s->stdio_file = fopen(filename, mode);
401 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000402 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200403
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200404 if(mode[0] == 'w') {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200405 s->file = qemu_fopen_ops(s, &stdio_file_write_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200406 } else {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200407 s->file = qemu_fopen_ops(s, &stdio_file_read_ops);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200408 }
409 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000410fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500411 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000412 return NULL;
413}
414
aliguori178e08a2009-04-05 19:10:55 +0000415static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000416 int64_t pos, int size)
417{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200418 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000419 return size;
420}
421
aliguori178e08a2009-04-05 19:10:55 +0000422static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000423{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200424 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000425}
426
427static int bdrv_fclose(void *opaque)
428{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200429 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000430}
431
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200432static const QEMUFileOps bdrv_read_ops = {
433 .get_buffer = block_get_buffer,
434 .close = bdrv_fclose
435};
436
437static const QEMUFileOps bdrv_write_ops = {
438 .put_buffer = block_put_buffer,
439 .close = bdrv_fclose
440};
441
Christoph Hellwig45566e92009-07-10 23:11:57 +0200442static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000443{
aliguoria672b462008-11-11 21:33:36 +0000444 if (is_writable)
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200445 return qemu_fopen_ops(bs, &bdrv_write_ops);
446 return qemu_fopen_ops(bs, &bdrv_read_ops);
aliguoria672b462008-11-11 21:33:36 +0000447}
448
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200449QEMUFile *qemu_fopen_ops(void *opaque, const QEMUFileOps *ops)
aliguoria672b462008-11-11 21:33:36 +0000450{
451 QEMUFile *f;
452
Anthony Liguori7267c092011-08-20 22:09:37 -0500453 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000454
455 f->opaque = opaque;
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200456 f->ops = ops;
aliguoria672b462008-11-11 21:33:36 +0000457 f->is_write = 0;
458
459 return f;
460}
461
Juan Quintela624b9cc2011-10-05 01:02:52 +0200462int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000463{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200464 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000465}
466
Juan Quintela6f121ff2012-08-30 13:37:56 +0200467static void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000468{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200469 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000470}
471
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200472/** Flushes QEMUFile buffer
473 *
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200474 */
Juan Quintela7311bea2012-08-29 19:08:59 +0200475static int qemu_fflush(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000476{
Juan Quintela7311bea2012-08-29 19:08:59 +0200477 int ret = 0;
478
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200479 if (!f->ops->put_buffer)
Juan Quintela7311bea2012-08-29 19:08:59 +0200480 return 0;
aliguoria672b462008-11-11 21:33:36 +0000481
482 if (f->is_write && f->buf_index > 0) {
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200483 ret = f->ops->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
Juan Quintela7311bea2012-08-29 19:08:59 +0200484 if (ret >= 0) {
aliguoria672b462008-11-11 21:33:36 +0000485 f->buf_offset += f->buf_index;
Juan Quintela7311bea2012-08-29 19:08:59 +0200486 }
aliguoria672b462008-11-11 21:33:36 +0000487 f->buf_index = 0;
488 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200489 return ret;
aliguoria672b462008-11-11 21:33:36 +0000490}
491
492static void qemu_fill_buffer(QEMUFile *f)
493{
494 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200495 int pending;
aliguoria672b462008-11-11 21:33:36 +0000496
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200497 if (!f->ops->get_buffer)
aliguoria672b462008-11-11 21:33:36 +0000498 return;
499
500 if (f->is_write)
501 abort();
502
Juan Quintela0046c452011-09-30 19:28:45 +0200503 pending = f->buf_size - f->buf_index;
504 if (pending > 0) {
505 memmove(f->buf, f->buf + f->buf_index, pending);
506 }
507 f->buf_index = 0;
508 f->buf_size = pending;
509
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200510 len = f->ops->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
Juan Quintela0046c452011-09-30 19:28:45 +0200511 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000512 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200513 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000514 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200515 } else if (len == 0) {
Juan Quintela02c4a052012-08-29 19:36:26 +0200516 qemu_file_set_error(f, -EIO);
aliguoria672b462008-11-11 21:33:36 +0000517 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200518 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000519}
520
Paolo Bonzini70eb6332012-08-08 10:20:18 +0200521int qemu_get_fd(QEMUFile *f)
522{
523 if (f->ops->get_fd) {
524 return f->ops->get_fd(f->opaque);
525 }
526 return -1;
527}
528
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200529/** Closes the file
530 *
531 * Returns negative error value if any error happened on previous operations or
532 * while closing the file. Returns 0 or positive number on success.
533 *
534 * The meaning of return value on success depends on the specific backend
535 * being used.
536 */
537int qemu_fclose(QEMUFile *f)
538{
Juan Quintela29eee862012-08-29 19:14:54 +0200539 int ret;
Juan Quintela7311bea2012-08-29 19:08:59 +0200540 ret = qemu_fflush(f);
Juan Quintela7311bea2012-08-29 19:08:59 +0200541
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200542 if (f->ops->close) {
543 int ret2 = f->ops->close(f->opaque);
Juan Quintela29eee862012-08-29 19:14:54 +0200544 if (ret >= 0) {
545 ret = ret2;
546 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200547 }
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200548 /* If any error was spotted before closing, we should report it
549 * instead of the close() return value.
550 */
551 if (f->last_error) {
552 ret = f->last_error;
553 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500554 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000555 return ret;
556}
557
Juan Quintelaa2b41352012-09-04 12:45:42 +0200558int qemu_file_put_notify(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000559{
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200560 return f->ops->put_buffer(f->opaque, NULL, 0, 0);
aliguoria672b462008-11-11 21:33:36 +0000561}
562
563void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
564{
565 int l;
566
Juan Quintelac10682c2012-08-29 19:43:39 +0200567 if (f->last_error) {
568 return;
569 }
570
571 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000572 fprintf(stderr,
573 "Attempted to write to buffer while read buffer is not empty\n");
574 abort();
575 }
576
Juan Quintelac10682c2012-08-29 19:43:39 +0200577 while (size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000578 l = IO_BUF_SIZE - f->buf_index;
579 if (l > size)
580 l = size;
581 memcpy(f->buf + f->buf_index, buf, l);
582 f->is_write = 1;
583 f->buf_index += l;
584 buf += l;
585 size -= l;
Juan Quintela7311bea2012-08-29 19:08:59 +0200586 if (f->buf_index >= IO_BUF_SIZE) {
587 int ret = qemu_fflush(f);
Juan Quintelac10682c2012-08-29 19:43:39 +0200588 if (ret < 0) {
589 qemu_file_set_error(f, ret);
590 break;
591 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200592 }
aliguoria672b462008-11-11 21:33:36 +0000593 }
594}
595
596void qemu_put_byte(QEMUFile *f, int v)
597{
Juan Quintelac10682c2012-08-29 19:43:39 +0200598 if (f->last_error) {
599 return;
600 }
601
602 if (f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000603 fprintf(stderr,
604 "Attempted to write to buffer while read buffer is not empty\n");
605 abort();
606 }
607
608 f->buf[f->buf_index++] = v;
609 f->is_write = 1;
Juan Quintela7311bea2012-08-29 19:08:59 +0200610 if (f->buf_index >= IO_BUF_SIZE) {
611 int ret = qemu_fflush(f);
Juan Quintelac10682c2012-08-29 19:43:39 +0200612 if (ret < 0) {
613 qemu_file_set_error(f, ret);
614 }
Juan Quintela7311bea2012-08-29 19:08:59 +0200615 }
aliguoria672b462008-11-11 21:33:36 +0000616}
617
Juan Quintelac6380722011-10-04 15:28:31 +0200618static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000619{
Juan Quintelac6380722011-10-04 15:28:31 +0200620 if (f->buf_index + size <= f->buf_size) {
621 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000622 }
aliguoria672b462008-11-11 21:33:36 +0000623}
624
Juan Quintelac6380722011-10-04 15:28:31 +0200625static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200626{
Juan Quintelac6380722011-10-04 15:28:31 +0200627 int pending;
628 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200629
Juan Quintelab9ce1452011-10-04 13:55:32 +0200630 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200631 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200632 }
Juan Quintela811814b2010-07-26 21:38:43 +0200633
Juan Quintelac6380722011-10-04 15:28:31 +0200634 index = f->buf_index + offset;
635 pending = f->buf_size - index;
636 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200637 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200638 index = f->buf_index + offset;
639 pending = f->buf_size - index;
640 }
641
642 if (pending <= 0) {
643 return 0;
644 }
645 if (size > pending) {
646 size = pending;
647 }
648
649 memcpy(buf, f->buf + index, size);
650 return size;
651}
652
653int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
654{
655 int pending = size;
656 int done = 0;
657
658 while (pending > 0) {
659 int res;
660
661 res = qemu_peek_buffer(f, buf, pending, 0);
662 if (res == 0) {
663 return done;
664 }
665 qemu_file_skip(f, res);
666 buf += res;
667 pending -= res;
668 done += res;
669 }
670 return done;
671}
672
673static int qemu_peek_byte(QEMUFile *f, int offset)
674{
675 int index = f->buf_index + offset;
676
677 if (f->is_write) {
678 abort();
679 }
680
681 if (index >= f->buf_size) {
682 qemu_fill_buffer(f);
683 index = f->buf_index + offset;
684 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200685 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200686 }
Juan Quintela811814b2010-07-26 21:38:43 +0200687 }
Juan Quintelac6380722011-10-04 15:28:31 +0200688 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200689}
690
aliguoria672b462008-11-11 21:33:36 +0000691int qemu_get_byte(QEMUFile *f)
692{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200693 int result;
aliguoria672b462008-11-11 21:33:36 +0000694
Juan Quintelac6380722011-10-04 15:28:31 +0200695 result = qemu_peek_byte(f, 0);
696 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200697 return result;
aliguoria672b462008-11-11 21:33:36 +0000698}
699
Juan Quintela3aee4be2012-08-29 19:16:56 +0200700static int64_t qemu_ftell(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000701{
702 return f->buf_offset - f->buf_size + f->buf_index;
703}
704
aliguoria672b462008-11-11 21:33:36 +0000705int qemu_file_rate_limit(QEMUFile *f)
706{
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200707 if (f->ops->rate_limit)
708 return f->ops->rate_limit(f->opaque);
aliguoria672b462008-11-11 21:33:36 +0000709
710 return 0;
711}
712
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200713int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200714{
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200715 if (f->ops->get_rate_limit)
716 return f->ops->get_rate_limit(f->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200717
718 return 0;
719}
720
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200721int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400722{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400723 /* any failed or completed migration keeps its state to allow probing of
724 * migration data, but has no associated file anymore */
Paolo Bonzini9229bf32012-08-08 10:15:15 +0200725 if (f && f->ops->set_rate_limit)
726 return f->ops->set_rate_limit(f->opaque, new_rate);
Glauber Costa19629532009-05-20 18:26:57 -0400727
728 return 0;
729}
730
aliguoria672b462008-11-11 21:33:36 +0000731void qemu_put_be16(QEMUFile *f, unsigned int v)
732{
733 qemu_put_byte(f, v >> 8);
734 qemu_put_byte(f, v);
735}
736
737void qemu_put_be32(QEMUFile *f, unsigned int v)
738{
739 qemu_put_byte(f, v >> 24);
740 qemu_put_byte(f, v >> 16);
741 qemu_put_byte(f, v >> 8);
742 qemu_put_byte(f, v);
743}
744
745void qemu_put_be64(QEMUFile *f, uint64_t v)
746{
747 qemu_put_be32(f, v >> 32);
748 qemu_put_be32(f, v);
749}
750
751unsigned int qemu_get_be16(QEMUFile *f)
752{
753 unsigned int v;
754 v = qemu_get_byte(f) << 8;
755 v |= qemu_get_byte(f);
756 return v;
757}
758
759unsigned int qemu_get_be32(QEMUFile *f)
760{
761 unsigned int v;
762 v = qemu_get_byte(f) << 24;
763 v |= qemu_get_byte(f) << 16;
764 v |= qemu_get_byte(f) << 8;
765 v |= qemu_get_byte(f);
766 return v;
767}
768
769uint64_t qemu_get_be64(QEMUFile *f)
770{
771 uint64_t v;
772 v = (uint64_t)qemu_get_be32(f) << 32;
773 v |= qemu_get_be32(f);
774 return v;
775}
776
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200777
778/* timer */
779
780void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
781{
782 uint64_t expire_time;
783
784 expire_time = qemu_timer_expire_time_ns(ts);
785 qemu_put_be64(f, expire_time);
786}
787
788void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
789{
790 uint64_t expire_time;
791
792 expire_time = qemu_get_be64(f);
793 if (expire_time != -1) {
794 qemu_mod_timer_ns(ts, expire_time);
795 } else {
796 qemu_del_timer(ts);
797 }
798}
799
800
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100801/* bool */
802
803static int get_bool(QEMUFile *f, void *pv, size_t size)
804{
805 bool *v = pv;
806 *v = qemu_get_byte(f);
807 return 0;
808}
809
810static void put_bool(QEMUFile *f, void *pv, size_t size)
811{
812 bool *v = pv;
813 qemu_put_byte(f, *v);
814}
815
816const VMStateInfo vmstate_info_bool = {
817 .name = "bool",
818 .get = get_bool,
819 .put = put_bool,
820};
821
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200822/* 8 bit int */
823
824static int get_int8(QEMUFile *f, void *pv, size_t size)
825{
826 int8_t *v = pv;
827 qemu_get_s8s(f, v);
828 return 0;
829}
830
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200831static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200832{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200833 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200834 qemu_put_s8s(f, v);
835}
836
837const VMStateInfo vmstate_info_int8 = {
838 .name = "int8",
839 .get = get_int8,
840 .put = put_int8,
841};
842
843/* 16 bit int */
844
845static int get_int16(QEMUFile *f, void *pv, size_t size)
846{
847 int16_t *v = pv;
848 qemu_get_sbe16s(f, v);
849 return 0;
850}
851
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200852static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200853{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200854 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200855 qemu_put_sbe16s(f, v);
856}
857
858const VMStateInfo vmstate_info_int16 = {
859 .name = "int16",
860 .get = get_int16,
861 .put = put_int16,
862};
863
864/* 32 bit int */
865
866static int get_int32(QEMUFile *f, void *pv, size_t size)
867{
868 int32_t *v = pv;
869 qemu_get_sbe32s(f, v);
870 return 0;
871}
872
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200873static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200874{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200875 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200876 qemu_put_sbe32s(f, v);
877}
878
879const VMStateInfo vmstate_info_int32 = {
880 .name = "int32",
881 .get = get_int32,
882 .put = put_int32,
883};
884
Juan Quintela82501662009-08-20 19:42:32 +0200885/* 32 bit int. See that the received value is the same than the one
886 in the field */
887
888static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
889{
890 int32_t *v = pv;
891 int32_t v2;
892 qemu_get_sbe32s(f, &v2);
893
894 if (*v == v2)
895 return 0;
896 return -EINVAL;
897}
898
899const VMStateInfo vmstate_info_int32_equal = {
900 .name = "int32 equal",
901 .get = get_int32_equal,
902 .put = put_int32,
903};
904
Juan Quintela0a031e02009-08-20 19:42:37 +0200905/* 32 bit int. See that the received value is the less or the same
906 than the one in the field */
907
908static int get_int32_le(QEMUFile *f, void *pv, size_t size)
909{
910 int32_t *old = pv;
911 int32_t new;
912 qemu_get_sbe32s(f, &new);
913
914 if (*old <= new)
915 return 0;
916 return -EINVAL;
917}
918
919const VMStateInfo vmstate_info_int32_le = {
920 .name = "int32 equal",
921 .get = get_int32_le,
922 .put = put_int32,
923};
924
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200925/* 64 bit int */
926
927static int get_int64(QEMUFile *f, void *pv, size_t size)
928{
929 int64_t *v = pv;
930 qemu_get_sbe64s(f, v);
931 return 0;
932}
933
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200934static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200935{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200936 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200937 qemu_put_sbe64s(f, v);
938}
939
940const VMStateInfo vmstate_info_int64 = {
941 .name = "int64",
942 .get = get_int64,
943 .put = put_int64,
944};
945
946/* 8 bit unsigned int */
947
948static int get_uint8(QEMUFile *f, void *pv, size_t size)
949{
950 uint8_t *v = pv;
951 qemu_get_8s(f, v);
952 return 0;
953}
954
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200955static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200956{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200957 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200958 qemu_put_8s(f, v);
959}
960
961const VMStateInfo vmstate_info_uint8 = {
962 .name = "uint8",
963 .get = get_uint8,
964 .put = put_uint8,
965};
966
967/* 16 bit unsigned int */
968
969static int get_uint16(QEMUFile *f, void *pv, size_t size)
970{
971 uint16_t *v = pv;
972 qemu_get_be16s(f, v);
973 return 0;
974}
975
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200976static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200977{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200978 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200979 qemu_put_be16s(f, v);
980}
981
982const VMStateInfo vmstate_info_uint16 = {
983 .name = "uint16",
984 .get = get_uint16,
985 .put = put_uint16,
986};
987
988/* 32 bit unsigned int */
989
990static int get_uint32(QEMUFile *f, void *pv, size_t size)
991{
992 uint32_t *v = pv;
993 qemu_get_be32s(f, v);
994 return 0;
995}
996
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200997static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200998{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200999 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001000 qemu_put_be32s(f, v);
1001}
1002
1003const VMStateInfo vmstate_info_uint32 = {
1004 .name = "uint32",
1005 .get = get_uint32,
1006 .put = put_uint32,
1007};
1008
Juan Quintela9122a8f2011-03-10 12:33:48 +01001009/* 32 bit uint. See that the received value is the same than the one
1010 in the field */
1011
1012static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1013{
1014 uint32_t *v = pv;
1015 uint32_t v2;
1016 qemu_get_be32s(f, &v2);
1017
1018 if (*v == v2) {
1019 return 0;
1020 }
1021 return -EINVAL;
1022}
1023
1024const VMStateInfo vmstate_info_uint32_equal = {
1025 .name = "uint32 equal",
1026 .get = get_uint32_equal,
1027 .put = put_uint32,
1028};
1029
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001030/* 64 bit unsigned int */
1031
1032static int get_uint64(QEMUFile *f, void *pv, size_t size)
1033{
1034 uint64_t *v = pv;
1035 qemu_get_be64s(f, v);
1036 return 0;
1037}
1038
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001039static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001040{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001041 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001042 qemu_put_be64s(f, v);
1043}
1044
1045const VMStateInfo vmstate_info_uint64 = {
1046 .name = "uint64",
1047 .get = get_uint64,
1048 .put = put_uint64,
1049};
1050
Juan Quintela80cd83e2009-09-10 03:04:36 +02001051/* 8 bit int. See that the received value is the same than the one
1052 in the field */
1053
1054static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1055{
1056 uint8_t *v = pv;
1057 uint8_t v2;
1058 qemu_get_8s(f, &v2);
1059
1060 if (*v == v2)
1061 return 0;
1062 return -EINVAL;
1063}
1064
1065const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001066 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001067 .get = get_uint8_equal,
1068 .put = put_uint8,
1069};
1070
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001071/* 16 bit unsigned int int. See that the received value is the same than the one
1072 in the field */
1073
1074static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1075{
1076 uint16_t *v = pv;
1077 uint16_t v2;
1078 qemu_get_be16s(f, &v2);
1079
1080 if (*v == v2)
1081 return 0;
1082 return -EINVAL;
1083}
1084
1085const VMStateInfo vmstate_info_uint16_equal = {
1086 .name = "uint16 equal",
1087 .get = get_uint16_equal,
1088 .put = put_uint16,
1089};
1090
Juan Quinteladde04632009-08-20 19:42:26 +02001091/* timers */
1092
1093static int get_timer(QEMUFile *f, void *pv, size_t size)
1094{
1095 QEMUTimer *v = pv;
1096 qemu_get_timer(f, v);
1097 return 0;
1098}
1099
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001100static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001101{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001102 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001103 qemu_put_timer(f, v);
1104}
1105
1106const VMStateInfo vmstate_info_timer = {
1107 .name = "timer",
1108 .get = get_timer,
1109 .put = put_timer,
1110};
1111
Juan Quintela6f67c502009-08-20 19:42:35 +02001112/* uint8_t buffers */
1113
1114static int get_buffer(QEMUFile *f, void *pv, size_t size)
1115{
1116 uint8_t *v = pv;
1117 qemu_get_buffer(f, v, size);
1118 return 0;
1119}
1120
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001121static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001122{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001123 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001124 qemu_put_buffer(f, v, size);
1125}
1126
1127const VMStateInfo vmstate_info_buffer = {
1128 .name = "buffer",
1129 .get = get_buffer,
1130 .put = put_buffer,
1131};
1132
Juan Quintela76507c72009-10-19 15:46:28 +02001133/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001134 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001135
1136static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1137{
Jan Kiszka21174c32009-12-02 12:36:35 +01001138 uint8_t buf[1024];
1139 int block_len;
1140
1141 while (size > 0) {
1142 block_len = MIN(sizeof(buf), size);
1143 size -= block_len;
1144 qemu_get_buffer(f, buf, block_len);
1145 }
1146 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001147}
1148
1149static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1150{
Jan Kiszka21174c32009-12-02 12:36:35 +01001151 static const uint8_t buf[1024];
1152 int block_len;
1153
1154 while (size > 0) {
1155 block_len = MIN(sizeof(buf), size);
1156 size -= block_len;
1157 qemu_put_buffer(f, buf, block_len);
1158 }
Juan Quintela76507c72009-10-19 15:46:28 +02001159}
1160
1161const VMStateInfo vmstate_info_unused_buffer = {
1162 .name = "unused_buffer",
1163 .get = get_unused_buffer,
1164 .put = put_unused_buffer,
1165};
1166
Peter Maydell08e99e22012-10-30 07:45:12 +00001167/* bitmaps (as defined by bitmap.h). Note that size here is the size
1168 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
1169 * bit words with the bits in big endian order. The in-memory format
1170 * is an array of 'unsigned long', which may be either 32 or 64 bits.
1171 */
1172/* This is the number of 64 bit words sent over the wire */
1173#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
1174static int get_bitmap(QEMUFile *f, void *pv, size_t size)
1175{
1176 unsigned long *bmp = pv;
1177 int i, idx = 0;
1178 for (i = 0; i < BITS_TO_U64S(size); i++) {
1179 uint64_t w = qemu_get_be64(f);
1180 bmp[idx++] = w;
1181 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1182 bmp[idx++] = w >> 32;
1183 }
1184 }
1185 return 0;
1186}
1187
1188static void put_bitmap(QEMUFile *f, void *pv, size_t size)
1189{
1190 unsigned long *bmp = pv;
1191 int i, idx = 0;
1192 for (i = 0; i < BITS_TO_U64S(size); i++) {
1193 uint64_t w = bmp[idx++];
1194 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
1195 w |= ((uint64_t)bmp[idx++]) << 32;
1196 }
1197 qemu_put_be64(f, w);
1198 }
1199}
1200
1201const VMStateInfo vmstate_info_bitmap = {
1202 .name = "bitmap",
1203 .get = get_bitmap,
1204 .put = put_bitmap,
1205};
1206
Alex Williamson7685ee62010-06-25 11:09:14 -06001207typedef struct CompatEntry {
1208 char idstr[256];
1209 int instance_id;
1210} CompatEntry;
1211
aliguoria672b462008-11-11 21:33:36 +00001212typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001213 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001214 char idstr[256];
1215 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001216 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001217 int version_id;
1218 int section_id;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001219 SaveVMHandlers *ops;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001220 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001221 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001222 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001223 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001224 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001225} SaveStateEntry;
1226
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001227
Blue Swirl72cf2d42009-09-12 07:36:22 +00001228static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1229 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001230static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001231
Juan Quintela8718e992009-09-01 02:12:31 +02001232static int calculate_new_instance_id(const char *idstr)
1233{
1234 SaveStateEntry *se;
1235 int instance_id = 0;
1236
Blue Swirl72cf2d42009-09-12 07:36:22 +00001237 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001238 if (strcmp(idstr, se->idstr) == 0
1239 && instance_id <= se->instance_id) {
1240 instance_id = se->instance_id + 1;
1241 }
1242 }
1243 return instance_id;
1244}
1245
Alex Williamson7685ee62010-06-25 11:09:14 -06001246static int calculate_compat_instance_id(const char *idstr)
1247{
1248 SaveStateEntry *se;
1249 int instance_id = 0;
1250
1251 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1252 if (!se->compat)
1253 continue;
1254
1255 if (strcmp(idstr, se->compat->idstr) == 0
1256 && instance_id <= se->compat->instance_id) {
1257 instance_id = se->compat->instance_id + 1;
1258 }
1259 }
1260 return instance_id;
1261}
1262
aliguoria672b462008-11-11 21:33:36 +00001263/* TODO: Individual devices generally have very little idea about the rest
1264 of the system, so instance_id should be removed/replaced.
1265 Meanwhile pass -1 as instance_id if you do not already have a clearly
1266 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001267int register_savevm_live(DeviceState *dev,
1268 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001269 int instance_id,
1270 int version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001271 SaveVMHandlers *ops,
aliguoria672b462008-11-11 21:33:36 +00001272 void *opaque)
1273{
Juan Quintela8718e992009-09-01 02:12:31 +02001274 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001275
Anthony Liguori7267c092011-08-20 22:09:37 -05001276 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001277 se->version_id = version_id;
1278 se->section_id = global_section_id++;
Juan Quintela7908c782012-06-26 18:46:10 +02001279 se->ops = ops;
aliguoria672b462008-11-11 21:33:36 +00001280 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001281 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001282 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001283 /* if this is a live_savem then set is_ram */
Juan Quintela16310a32012-06-28 15:31:37 +02001284 if (ops->save_live_setup != NULL) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001285 se->is_ram = 1;
1286 }
aliguoria672b462008-11-11 21:33:36 +00001287
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001288 if (dev) {
1289 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001290 if (id) {
1291 pstrcpy(se->idstr, sizeof(se->idstr), id);
1292 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001293 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001294
Anthony Liguori7267c092011-08-20 22:09:37 -05001295 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001296 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1297 se->compat->instance_id = instance_id == -1 ?
1298 calculate_compat_instance_id(idstr) : instance_id;
1299 instance_id = -1;
1300 }
1301 }
1302 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1303
Juan Quintela8718e992009-09-01 02:12:31 +02001304 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001305 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001306 } else {
1307 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001308 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001309 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001310 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001311 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001312 return 0;
1313}
1314
Alex Williamson0be71e32010-06-25 11:09:07 -06001315int register_savevm(DeviceState *dev,
1316 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001317 int instance_id,
1318 int version_id,
1319 SaveStateHandler *save_state,
1320 LoadStateHandler *load_state,
1321 void *opaque)
1322{
Juan Quintela7908c782012-06-26 18:46:10 +02001323 SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1324 ops->save_state = save_state;
1325 ops->load_state = load_state;
Alex Williamson0be71e32010-06-25 11:09:07 -06001326 return register_savevm_live(dev, idstr, instance_id, version_id,
Juan Quintela7908c782012-06-26 18:46:10 +02001327 ops, opaque);
aliguoria672b462008-11-11 21:33:36 +00001328}
1329
Alex Williamson0be71e32010-06-25 11:09:07 -06001330void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001331{
Juan Quintela8718e992009-09-01 02:12:31 +02001332 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001333 char id[256] = "";
1334
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001335 if (dev) {
1336 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001337 if (path) {
1338 pstrcpy(id, sizeof(id), path);
1339 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001340 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001341 }
1342 }
1343 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001344
Blue Swirl72cf2d42009-09-12 07:36:22 +00001345 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001346 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001347 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001348 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001349 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001350 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001351 g_free(se->ops);
Anthony Liguori7267c092011-08-20 22:09:37 -05001352 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001353 }
aliguori41bd13a2009-04-17 17:10:59 +00001354 }
1355}
1356
Alex Williamson0be71e32010-06-25 11:09:07 -06001357int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001358 const VMStateDescription *vmsd,
1359 void *opaque, int alias_id,
1360 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001361{
Juan Quintela8718e992009-09-01 02:12:31 +02001362 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001363
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001364 /* If this triggers, alias support can be dropped for the vmsd. */
1365 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1366
Anthony Liguori7267c092011-08-20 22:09:37 -05001367 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001368 se->version_id = vmsd->version_id;
1369 se->section_id = global_section_id++;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001370 se->opaque = opaque;
1371 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001372 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001373 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001374
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001375 if (dev) {
1376 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001377 if (id) {
1378 pstrcpy(se->idstr, sizeof(se->idstr), id);
1379 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001380 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001381
Anthony Liguori7267c092011-08-20 22:09:37 -05001382 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001383 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1384 se->compat->instance_id = instance_id == -1 ?
1385 calculate_compat_instance_id(vmsd->name) : instance_id;
1386 instance_id = -1;
1387 }
1388 }
1389 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1390
Juan Quintela8718e992009-09-01 02:12:31 +02001391 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001392 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001393 } else {
1394 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001395 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001396 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001397 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001398 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001399 return 0;
1400}
1401
Alex Williamson0be71e32010-06-25 11:09:07 -06001402int vmstate_register(DeviceState *dev, int instance_id,
1403 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001404{
Alex Williamson0be71e32010-06-25 11:09:07 -06001405 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1406 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001407}
1408
Alex Williamson0be71e32010-06-25 11:09:07 -06001409void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1410 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001411{
Juan Quintela1eb75382009-09-10 03:04:29 +02001412 SaveStateEntry *se, *new_se;
1413
Blue Swirl72cf2d42009-09-12 07:36:22 +00001414 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001415 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001416 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001417 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001418 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001419 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001420 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001421 }
1422 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001423}
1424
Juan Quintela811814b2010-07-26 21:38:43 +02001425static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1426 void *opaque);
1427static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1428 void *opaque);
1429
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001430int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1431 void *opaque, int version_id)
1432{
1433 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001434 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001435
1436 if (version_id > vmsd->version_id) {
1437 return -EINVAL;
1438 }
1439 if (version_id < vmsd->minimum_version_id_old) {
1440 return -EINVAL;
1441 }
1442 if (version_id < vmsd->minimum_version_id) {
1443 return vmsd->load_state_old(f, opaque, version_id);
1444 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001445 if (vmsd->pre_load) {
1446 int ret = vmsd->pre_load(opaque);
1447 if (ret)
1448 return ret;
1449 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001450 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001451 if ((field->field_exists &&
1452 field->field_exists(opaque, version_id)) ||
1453 (!field->field_exists &&
1454 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001455 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001456 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001457 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001458
Juan Quintelae61a1e02009-12-02 12:36:38 +01001459 if (field->flags & VMS_VBUFFER) {
1460 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001461 if (field->flags & VMS_MULTIPLY) {
1462 size *= field->size;
1463 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001464 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001465 if (field->flags & VMS_ARRAY) {
1466 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001467 } else if (field->flags & VMS_VARRAY_INT32) {
1468 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001469 } else if (field->flags & VMS_VARRAY_UINT32) {
1470 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001471 } else if (field->flags & VMS_VARRAY_UINT16) {
1472 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001473 } else if (field->flags & VMS_VARRAY_UINT8) {
1474 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001475 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001476 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001477 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001478 }
1479 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001480 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001481
Juan Quintela19df4382009-09-29 22:48:41 +02001482 if (field->flags & VMS_ARRAY_OF_POINTER) {
1483 addr = *(void **)addr;
1484 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001485 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001486 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001487 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001488 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001489
1490 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001491 if (ret < 0) {
1492 return ret;
1493 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001494 }
1495 }
1496 field++;
1497 }
Juan Quintela811814b2010-07-26 21:38:43 +02001498 ret = vmstate_subsection_load(f, vmsd, opaque);
1499 if (ret != 0) {
1500 return ret;
1501 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001502 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001503 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001504 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001505 return 0;
1506}
1507
1508void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001509 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001510{
1511 VMStateField *field = vmsd->fields;
1512
Juan Quintela8fb07912009-09-10 03:04:32 +02001513 if (vmsd->pre_save) {
1514 vmsd->pre_save(opaque);
1515 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001516 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001517 if (!field->field_exists ||
1518 field->field_exists(opaque, vmsd->version_id)) {
1519 void *base_addr = opaque + field->offset;
1520 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001521 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001522
Juan Quintelae61a1e02009-12-02 12:36:38 +01001523 if (field->flags & VMS_VBUFFER) {
1524 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001525 if (field->flags & VMS_MULTIPLY) {
1526 size *= field->size;
1527 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001528 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001529 if (field->flags & VMS_ARRAY) {
1530 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001531 } else if (field->flags & VMS_VARRAY_INT32) {
1532 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001533 } else if (field->flags & VMS_VARRAY_UINT32) {
1534 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001535 } else if (field->flags & VMS_VARRAY_UINT16) {
1536 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001537 } else if (field->flags & VMS_VARRAY_UINT8) {
1538 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001539 }
1540 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001541 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001542 }
1543 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001544 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001545
Juan Quintela85953872009-12-02 12:36:37 +01001546 if (field->flags & VMS_ARRAY_OF_POINTER) {
1547 addr = *(void **)addr;
1548 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001549 if (field->flags & VMS_STRUCT) {
1550 vmstate_save_state(f, field->vmsd, addr);
1551 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001552 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001553 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001554 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001555 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001556 field++;
1557 }
Juan Quintela811814b2010-07-26 21:38:43 +02001558 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001559}
1560
Juan Quintela4082be42009-08-20 19:42:24 +02001561static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1562{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001563 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001564 return se->ops->load_state(f, se->opaque, version_id);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001565 }
1566 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001567}
1568
Alex Williamsondc912122011-01-11 14:39:43 -07001569static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001570{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001571 if (!se->vmsd) { /* Old style */
Juan Quintela22ea40f2012-06-26 17:19:10 +02001572 se->ops->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001573 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001574 }
1575 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001576}
1577
aliguoria672b462008-11-11 21:33:36 +00001578#define QEMU_VM_FILE_MAGIC 0x5145564d
1579#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1580#define QEMU_VM_FILE_VERSION 0x00000003
1581
1582#define QEMU_VM_EOF 0x00
1583#define QEMU_VM_SECTION_START 0x01
1584#define QEMU_VM_SECTION_PART 0x02
1585#define QEMU_VM_SECTION_END 0x03
1586#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001587#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001588
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001589bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001590{
1591 SaveStateEntry *se;
1592
1593 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1594 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001595 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001596 return true;
1597 }
1598 }
1599 return false;
1600}
1601
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001602int qemu_savevm_state_begin(QEMUFile *f,
1603 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001604{
1605 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001606 int ret;
aliguoria672b462008-11-11 21:33:36 +00001607
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001608 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela22ea40f2012-06-26 17:19:10 +02001609 if (!se->ops || !se->ops->set_params) {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001610 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001611 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001612 se->ops->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001613 }
1614
aliguoria672b462008-11-11 21:33:36 +00001615 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1616 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1617
Blue Swirl72cf2d42009-09-12 07:36:22 +00001618 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001619 int len;
1620
Juan Quintelad1315aa2012-06-28 15:11:57 +02001621 if (!se->ops || !se->ops->save_live_setup) {
aliguoria672b462008-11-11 21:33:36 +00001622 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001623 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001624 if (se->ops && se->ops->is_active) {
1625 if (!se->ops->is_active(se->opaque)) {
1626 continue;
1627 }
1628 }
aliguoria672b462008-11-11 21:33:36 +00001629 /* Section type */
1630 qemu_put_byte(f, QEMU_VM_SECTION_START);
1631 qemu_put_be32(f, se->section_id);
1632
1633 /* ID string */
1634 len = strlen(se->idstr);
1635 qemu_put_byte(f, len);
1636 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1637
1638 qemu_put_be32(f, se->instance_id);
1639 qemu_put_be32(f, se->version_id);
1640
Juan Quintelad1315aa2012-06-28 15:11:57 +02001641 ret = se->ops->save_live_setup(f, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001642 if (ret < 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001643 qemu_savevm_state_cancel(f);
Juan Quintela29757252011-10-19 15:22:18 +02001644 return ret;
1645 }
aliguoria672b462008-11-11 21:33:36 +00001646 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001647 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001648 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001649 qemu_savevm_state_cancel(f);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001650 }
aliguoria672b462008-11-11 21:33:36 +00001651
Juan Quintela39346382011-09-22 11:02:14 +02001652 return ret;
1653
aliguoria672b462008-11-11 21:33:36 +00001654}
1655
Juan Quintela39346382011-09-22 11:02:14 +02001656/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001657 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001658 * negative: there was one error, and we have -errno.
1659 * 0 : We haven't finished, caller have to go again
1660 * 1 : We have finished, we can go to complete phase
1661 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001662int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001663{
1664 SaveStateEntry *se;
1665 int ret = 1;
1666
Blue Swirl72cf2d42009-09-12 07:36:22 +00001667 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001668 if (!se->ops || !se->ops->save_live_iterate) {
aliguoria672b462008-11-11 21:33:36 +00001669 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001670 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001671 if (se->ops && se->ops->is_active) {
1672 if (!se->ops->is_active(se->opaque)) {
1673 continue;
1674 }
1675 }
Juan Quintelaaac844e2012-05-22 00:38:26 +02001676 if (qemu_file_rate_limit(f)) {
1677 return 0;
1678 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001679 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001680 /* Section type */
1681 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1682 qemu_put_be32(f, se->section_id);
1683
Juan Quintela16310a32012-06-28 15:31:37 +02001684 ret = se->ops->save_live_iterate(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001685 trace_savevm_section_end(se->section_id);
1686
Juan Quintela29757252011-10-19 15:22:18 +02001687 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001688 /* Do not proceed to the next vmstate before this one reported
1689 completion of the current stage. This serializes the migration
1690 and reduces the probability that a faster changing state is
1691 synchronized over and over again. */
1692 break;
1693 }
aliguoria672b462008-11-11 21:33:36 +00001694 }
Juan Quintela39346382011-09-22 11:02:14 +02001695 if (ret != 0) {
1696 return ret;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001697 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001698 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001699 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001700 qemu_savevm_state_cancel(f);
Juan Quintela39346382011-09-22 11:02:14 +02001701 }
1702 return ret;
aliguoria672b462008-11-11 21:33:36 +00001703}
1704
Luiz Capitulino539de122011-12-05 14:06:56 -02001705int qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001706{
1707 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001708 int ret;
aliguoria672b462008-11-11 21:33:36 +00001709
Jan Kiszkaea375f92010-03-01 19:10:30 +01001710 cpu_synchronize_all_states();
1711
Blue Swirl72cf2d42009-09-12 07:36:22 +00001712 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela16310a32012-06-28 15:31:37 +02001713 if (!se->ops || !se->ops->save_live_complete) {
aliguoria672b462008-11-11 21:33:36 +00001714 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001715 }
Juan Quintela6bd68782012-06-27 10:59:15 +02001716 if (se->ops && se->ops->is_active) {
1717 if (!se->ops->is_active(se->opaque)) {
1718 continue;
1719 }
1720 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001721 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001722 /* Section type */
1723 qemu_put_byte(f, QEMU_VM_SECTION_END);
1724 qemu_put_be32(f, se->section_id);
1725
Juan Quintela16310a32012-06-28 15:31:37 +02001726 ret = se->ops->save_live_complete(f, se->opaque);
Juan Quintela517a13c2012-05-21 23:46:44 +02001727 trace_savevm_section_end(se->section_id);
Juan Quintela29757252011-10-19 15:22:18 +02001728 if (ret < 0) {
1729 return ret;
1730 }
aliguoria672b462008-11-11 21:33:36 +00001731 }
1732
Blue Swirl72cf2d42009-09-12 07:36:22 +00001733 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001734 int len;
1735
Juan Quintela22ea40f2012-06-26 17:19:10 +02001736 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
aliguoria672b462008-11-11 21:33:36 +00001737 continue;
Juan Quintela22ea40f2012-06-26 17:19:10 +02001738 }
Juan Quintela517a13c2012-05-21 23:46:44 +02001739 trace_savevm_section_start();
aliguoria672b462008-11-11 21:33:36 +00001740 /* Section type */
1741 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1742 qemu_put_be32(f, se->section_id);
1743
1744 /* ID string */
1745 len = strlen(se->idstr);
1746 qemu_put_byte(f, len);
1747 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1748
1749 qemu_put_be32(f, se->instance_id);
1750 qemu_put_be32(f, se->version_id);
1751
Alex Williamsondc912122011-01-11 14:39:43 -07001752 vmstate_save(f, se);
Juan Quintela517a13c2012-05-21 23:46:44 +02001753 trace_savevm_section_end(se->section_id);
aliguoria672b462008-11-11 21:33:36 +00001754 }
1755
1756 qemu_put_byte(f, QEMU_VM_EOF);
1757
Juan Quintela624b9cc2011-10-05 01:02:52 +02001758 return qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00001759}
1760
Luiz Capitulino539de122011-12-05 14:06:56 -02001761void qemu_savevm_state_cancel(QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001762{
1763 SaveStateEntry *se;
1764
1765 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela9b5bfab2012-06-26 19:26:41 +02001766 if (se->ops && se->ops->cancel) {
1767 se->ops->cancel(se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001768 }
1769 }
1770}
1771
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001772static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001773{
aliguoria672b462008-11-11 21:33:36 +00001774 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001775 MigrationParams params = {
1776 .blk = 0,
1777 .shared = 0
1778 };
aliguoria672b462008-11-11 21:33:36 +00001779
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001780 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001781 ret = -EINVAL;
1782 goto out;
1783 }
1784
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001785 ret = qemu_savevm_state_begin(f, &params);
aliguoria672b462008-11-11 21:33:36 +00001786 if (ret < 0)
1787 goto out;
1788
1789 do {
Luiz Capitulino539de122011-12-05 14:06:56 -02001790 ret = qemu_savevm_state_iterate(f);
aliguoria672b462008-11-11 21:33:36 +00001791 if (ret < 0)
1792 goto out;
1793 } while (ret == 0);
1794
Luiz Capitulino539de122011-12-05 14:06:56 -02001795 ret = qemu_savevm_state_complete(f);
aliguoria672b462008-11-11 21:33:36 +00001796
1797out:
Juan Quintela39346382011-09-22 11:02:14 +02001798 if (ret == 0) {
Juan Quintela624b9cc2011-10-05 01:02:52 +02001799 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001800 }
aliguoria672b462008-11-11 21:33:36 +00001801
aliguoria672b462008-11-11 21:33:36 +00001802 return ret;
1803}
1804
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001805static int qemu_save_device_state(QEMUFile *f)
1806{
1807 SaveStateEntry *se;
1808
1809 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1810 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1811
1812 cpu_synchronize_all_states();
1813
1814 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1815 int len;
1816
1817 if (se->is_ram) {
1818 continue;
1819 }
Juan Quintela22ea40f2012-06-26 17:19:10 +02001820 if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001821 continue;
1822 }
1823
1824 /* Section type */
1825 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1826 qemu_put_be32(f, se->section_id);
1827
1828 /* ID string */
1829 len = strlen(se->idstr);
1830 qemu_put_byte(f, len);
1831 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1832
1833 qemu_put_be32(f, se->instance_id);
1834 qemu_put_be32(f, se->version_id);
1835
1836 vmstate_save(f, se);
1837 }
1838
1839 qemu_put_byte(f, QEMU_VM_EOF);
1840
1841 return qemu_file_get_error(f);
1842}
1843
aliguoria672b462008-11-11 21:33:36 +00001844static SaveStateEntry *find_se(const char *idstr, int instance_id)
1845{
1846 SaveStateEntry *se;
1847
Blue Swirl72cf2d42009-09-12 07:36:22 +00001848 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001849 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001850 (instance_id == se->instance_id ||
1851 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001852 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001853 /* Migrating from an older version? */
1854 if (strstr(se->idstr, idstr) && se->compat) {
1855 if (!strcmp(se->compat->idstr, idstr) &&
1856 (instance_id == se->compat->instance_id ||
1857 instance_id == se->alias_id))
1858 return se;
1859 }
aliguoria672b462008-11-11 21:33:36 +00001860 }
1861 return NULL;
1862}
1863
Juan Quintela811814b2010-07-26 21:38:43 +02001864static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1865{
1866 while(sub && sub->needed) {
1867 if (strcmp(idstr, sub->vmsd->name) == 0) {
1868 return sub->vmsd;
1869 }
1870 sub++;
1871 }
1872 return NULL;
1873}
1874
1875static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1876 void *opaque)
1877{
Juan Quintelac6380722011-10-04 15:28:31 +02001878 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001879 char idstr[256];
1880 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001881 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001882 const VMStateDescription *sub_vmsd;
1883
Juan Quintelac6380722011-10-04 15:28:31 +02001884 len = qemu_peek_byte(f, 1);
1885 if (len < strlen(vmsd->name) + 1) {
1886 /* subsection name has be be "section_name/a" */
1887 return 0;
1888 }
1889 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1890 if (size != len) {
1891 return 0;
1892 }
1893 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001894
Juan Quintelac6380722011-10-04 15:28:31 +02001895 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1896 /* it don't have a valid subsection name */
1897 return 0;
1898 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001899 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001900 if (sub_vmsd == NULL) {
1901 return -ENOENT;
1902 }
Juan Quintelac6380722011-10-04 15:28:31 +02001903 qemu_file_skip(f, 1); /* subsection */
1904 qemu_file_skip(f, 1); /* len */
1905 qemu_file_skip(f, len); /* idstr */
1906 version_id = qemu_get_be32(f);
1907
Juan Quintela811814b2010-07-26 21:38:43 +02001908 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1909 if (ret) {
1910 return ret;
1911 }
1912 }
1913 return 0;
1914}
1915
1916static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1917 void *opaque)
1918{
1919 const VMStateSubsection *sub = vmsd->subsections;
1920
1921 while (sub && sub->needed) {
1922 if (sub->needed(opaque)) {
1923 const VMStateDescription *vmsd = sub->vmsd;
1924 uint8_t len;
1925
1926 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1927 len = strlen(vmsd->name);
1928 qemu_put_byte(f, len);
1929 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1930 qemu_put_be32(f, vmsd->version_id);
1931 vmstate_save_state(f, vmsd, opaque);
1932 }
1933 sub++;
1934 }
1935}
1936
aliguoria672b462008-11-11 21:33:36 +00001937typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001938 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001939 SaveStateEntry *se;
1940 int section_id;
1941 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001942} LoadStateEntry;
1943
aliguoria672b462008-11-11 21:33:36 +00001944int qemu_loadvm_state(QEMUFile *f)
1945{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001946 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1947 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001948 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001949 uint8_t section_type;
1950 unsigned int v;
1951 int ret;
1952
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001953 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001954 return -EINVAL;
1955 }
1956
aliguoria672b462008-11-11 21:33:36 +00001957 v = qemu_get_be32(f);
1958 if (v != QEMU_VM_FILE_MAGIC)
1959 return -EINVAL;
1960
1961 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001962 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1963 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1964 return -ENOTSUP;
1965 }
aliguoria672b462008-11-11 21:33:36 +00001966 if (v != QEMU_VM_FILE_VERSION)
1967 return -ENOTSUP;
1968
1969 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1970 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001971 SaveStateEntry *se;
1972 char idstr[257];
1973 int len;
1974
1975 switch (section_type) {
1976 case QEMU_VM_SECTION_START:
1977 case QEMU_VM_SECTION_FULL:
1978 /* Read section start */
1979 section_id = qemu_get_be32(f);
1980 len = qemu_get_byte(f);
1981 qemu_get_buffer(f, (uint8_t *)idstr, len);
1982 idstr[len] = 0;
1983 instance_id = qemu_get_be32(f);
1984 version_id = qemu_get_be32(f);
1985
1986 /* Find savevm section */
1987 se = find_se(idstr, instance_id);
1988 if (se == NULL) {
1989 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1990 ret = -EINVAL;
1991 goto out;
1992 }
1993
1994 /* Validate version */
1995 if (version_id > se->version_id) {
1996 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1997 version_id, idstr, se->version_id);
1998 ret = -EINVAL;
1999 goto out;
2000 }
2001
2002 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05002003 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00002004
2005 le->se = se;
2006 le->section_id = section_id;
2007 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00002008 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00002009
Juan Quintela4082be42009-08-20 19:42:24 +02002010 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002011 if (ret < 0) {
2012 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
2013 instance_id, idstr);
2014 goto out;
2015 }
aliguoria672b462008-11-11 21:33:36 +00002016 break;
2017 case QEMU_VM_SECTION_PART:
2018 case QEMU_VM_SECTION_END:
2019 section_id = qemu_get_be32(f);
2020
Blue Swirl72cf2d42009-09-12 07:36:22 +00002021 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02002022 if (le->section_id == section_id) {
2023 break;
2024 }
2025 }
aliguoria672b462008-11-11 21:33:36 +00002026 if (le == NULL) {
2027 fprintf(stderr, "Unknown savevm section %d\n", section_id);
2028 ret = -EINVAL;
2029 goto out;
2030 }
2031
Juan Quintela4082be42009-08-20 19:42:24 +02002032 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02002033 if (ret < 0) {
2034 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
2035 section_id);
2036 goto out;
2037 }
aliguoria672b462008-11-11 21:33:36 +00002038 break;
2039 default:
2040 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
2041 ret = -EINVAL;
2042 goto out;
2043 }
2044 }
2045
Jan Kiszkaea375f92010-03-01 19:10:30 +01002046 cpu_synchronize_all_post_init();
2047
aliguoria672b462008-11-11 21:33:36 +00002048 ret = 0;
2049
2050out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00002051 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2052 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05002053 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00002054 }
2055
Juan Quintela42802d42011-10-05 01:14:46 +02002056 if (ret == 0) {
2057 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02002058 }
aliguoria672b462008-11-11 21:33:36 +00002059
2060 return ret;
2061}
2062
aliguoria672b462008-11-11 21:33:36 +00002063static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2064 const char *name)
2065{
2066 QEMUSnapshotInfo *sn_tab, *sn;
2067 int nb_sns, i, ret;
2068
2069 ret = -ENOENT;
2070 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2071 if (nb_sns < 0)
2072 return ret;
2073 for(i = 0; i < nb_sns; i++) {
2074 sn = &sn_tab[i];
2075 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2076 *sn_info = *sn;
2077 ret = 0;
2078 break;
2079 }
2080 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002081 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002082 return ret;
2083}
2084
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002085/*
2086 * Deletes snapshots of a given name in all opened images.
2087 */
2088static int del_existing_snapshots(Monitor *mon, const char *name)
2089{
2090 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002091 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2092 int ret;
2093
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002094 bs = NULL;
2095 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002096 if (bdrv_can_snapshot(bs) &&
2097 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2098 {
2099 ret = bdrv_snapshot_delete(bs, name);
2100 if (ret < 0) {
2101 monitor_printf(mon,
2102 "Error while deleting snapshot on '%s'\n",
2103 bdrv_get_device_name(bs));
2104 return -1;
2105 }
2106 }
2107 }
2108
2109 return 0;
2110}
2111
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002112void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002113{
2114 BlockDriverState *bs, *bs1;
2115 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002116 int ret;
aliguoria672b462008-11-11 21:33:36 +00002117 QEMUFile *f;
2118 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002119 uint64_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00002120#ifdef _WIN32
2121 struct _timeb tb;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002122 struct tm *ptm;
aliguoria672b462008-11-11 21:33:36 +00002123#else
2124 struct timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002125 struct tm tm;
aliguoria672b462008-11-11 21:33:36 +00002126#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002127 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002128
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002129 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002130 bs = NULL;
2131 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002132
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002133 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002134 continue;
2135 }
2136
2137 if (!bdrv_can_snapshot(bs)) {
2138 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2139 bdrv_get_device_name(bs));
2140 return;
2141 }
2142 }
2143
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002144 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002145 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002146 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002147 return;
2148 }
aliguoria672b462008-11-11 21:33:36 +00002149
Luiz Capitulino13548692011-07-29 15:36:43 -03002150 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002151 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002152
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002153 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002154
2155 /* fill auxiliary fields */
2156#ifdef _WIN32
2157 _ftime(&tb);
2158 sn->date_sec = tb.time;
2159 sn->date_nsec = tb.millitm * 1000000;
2160#else
2161 gettimeofday(&tv, NULL);
2162 sn->date_sec = tv.tv_sec;
2163 sn->date_nsec = tv.tv_usec * 1000;
2164#endif
Paolo Bonzini74475452011-03-11 16:47:48 +01002165 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002166
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002167 if (name) {
2168 ret = bdrv_snapshot_find(bs, old_sn, name);
2169 if (ret >= 0) {
2170 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2171 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2172 } else {
2173 pstrcpy(sn->name, sizeof(sn->name), name);
2174 }
2175 } else {
2176#ifdef _WIN32
Stefan Weil55dd9ff2012-04-12 22:33:12 +02002177 time_t t = tb.time;
2178 ptm = localtime(&t);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002179 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2180#else
Blue Swirld7d9b522010-09-09 19:13:04 +00002181 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2182 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002183 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2184#endif
2185 }
2186
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002187 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002188 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002189 goto the_end;
2190 }
2191
aliguoria672b462008-11-11 21:33:36 +00002192 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002193 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002194 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002195 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002196 goto the_end;
2197 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002198 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002199 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002200 qemu_fclose(f);
2201 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002202 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002203 goto the_end;
2204 }
2205
2206 /* create the snapshots */
2207
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002208 bs1 = NULL;
2209 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002210 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002211 /* Write VM state size only to the image that contains the state */
2212 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002213 ret = bdrv_snapshot_create(bs1, sn);
2214 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002215 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2216 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002217 }
2218 }
2219 }
2220
2221 the_end:
2222 if (saved_vm_running)
2223 vm_start();
2224}
2225
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002226void qmp_xen_save_devices_state(const char *filename, Error **errp)
2227{
2228 QEMUFile *f;
2229 int saved_vm_running;
2230 int ret;
2231
2232 saved_vm_running = runstate_is_running();
2233 vm_stop(RUN_STATE_SAVE_VM);
2234
2235 f = qemu_fopen(filename, "wb");
2236 if (!f) {
2237 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2238 goto the_end;
2239 }
2240 ret = qemu_save_device_state(f);
2241 qemu_fclose(f);
2242 if (ret < 0) {
2243 error_set(errp, QERR_IO_ERROR);
2244 }
2245
2246 the_end:
2247 if (saved_vm_running)
2248 vm_start();
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002249}
2250
Markus Armbruster03cd4652010-02-17 16:24:10 +01002251int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002252{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002253 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002254 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002255 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002256 int ret;
aliguoria672b462008-11-11 21:33:36 +00002257
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002258 bs_vm_state = bdrv_snapshots();
2259 if (!bs_vm_state) {
2260 error_report("No block device supports snapshots");
2261 return -ENOTSUP;
2262 }
2263
2264 /* Don't even try to load empty VM states */
2265 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2266 if (ret < 0) {
2267 return ret;
2268 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002269 error_report("This is a disk-only snapshot. Revert to it offline "
2270 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002271 return -EINVAL;
2272 }
2273
2274 /* Verify if there is any device that doesn't support snapshots and is
2275 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002276 bs = NULL;
2277 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002278
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002279 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002280 continue;
2281 }
2282
2283 if (!bdrv_can_snapshot(bs)) {
2284 error_report("Device '%s' is writable but does not support snapshots.",
2285 bdrv_get_device_name(bs));
2286 return -ENOTSUP;
2287 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002288
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002289 ret = bdrv_snapshot_find(bs, &sn, name);
2290 if (ret < 0) {
2291 error_report("Device '%s' does not have the requested snapshot '%s'",
2292 bdrv_get_device_name(bs), name);
2293 return ret;
2294 }
aliguoria672b462008-11-11 21:33:36 +00002295 }
2296
2297 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002298 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002299
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002300 bs = NULL;
2301 while ((bs = bdrv_next(bs))) {
2302 if (bdrv_can_snapshot(bs)) {
2303 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002304 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002305 error_report("Error %d while activating snapshot '%s' on '%s'",
2306 ret, name, bdrv_get_device_name(bs));
2307 return ret;
aliguoria672b462008-11-11 21:33:36 +00002308 }
2309 }
2310 }
2311
aliguoria672b462008-11-11 21:33:36 +00002312 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002313 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002314 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002315 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002316 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002317 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002318
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002319 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002320 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002321
aliguoria672b462008-11-11 21:33:36 +00002322 qemu_fclose(f);
2323 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002324 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002325 return ret;
aliguoria672b462008-11-11 21:33:36 +00002326 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002327
Juan Quintela05f24012009-08-20 19:42:22 +02002328 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002329}
2330
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002331void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002332{
2333 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002334 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002335 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002336
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002337 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002338 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002339 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002340 return;
2341 }
2342
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002343 bs1 = NULL;
2344 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002345 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002346 ret = bdrv_snapshot_delete(bs1, name);
2347 if (ret < 0) {
2348 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002349 monitor_printf(mon,
2350 "Snapshots not supported on device '%s'\n",
2351 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002352 else
aliguori376253e2009-03-05 23:01:23 +00002353 monitor_printf(mon, "Error %d while deleting snapshot on "
2354 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002355 }
2356 }
2357 }
2358}
2359
aliguori376253e2009-03-05 23:01:23 +00002360void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002361{
2362 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002363 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2364 int nb_sns, i, ret, available;
2365 int total;
2366 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002367 char buf[256];
2368
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002369 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002370 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002371 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002372 return;
2373 }
aliguoria672b462008-11-11 21:33:36 +00002374
2375 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2376 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002377 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002378 return;
2379 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002380
2381 if (nb_sns == 0) {
2382 monitor_printf(mon, "There is no snapshot available.\n");
2383 return;
aliguoria672b462008-11-11 21:33:36 +00002384 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002385
Anthony Liguori7267c092011-08-20 22:09:37 -05002386 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002387 total = 0;
2388 for (i = 0; i < nb_sns; i++) {
2389 sn = &sn_tab[i];
2390 available = 1;
2391 bs1 = NULL;
2392
2393 while ((bs1 = bdrv_next(bs1))) {
2394 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2395 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2396 if (ret < 0) {
2397 available = 0;
2398 break;
2399 }
2400 }
2401 }
2402
2403 if (available) {
2404 available_snapshots[total] = i;
2405 total++;
2406 }
2407 }
2408
2409 if (total > 0) {
2410 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2411 for (i = 0; i < total; i++) {
2412 sn = &sn_tab[available_snapshots[i]];
2413 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2414 }
2415 } else {
2416 monitor_printf(mon, "There is no suitable snapshot available\n");
2417 }
2418
Anthony Liguori7267c092011-08-20 22:09:37 -05002419 g_free(sn_tab);
2420 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002421
aliguoria672b462008-11-11 21:33:36 +00002422}
Avi Kivityc5705a72011-12-20 15:59:12 +02002423
2424void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2425{
Avi Kivity1ddde082012-01-08 13:18:19 +02002426 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002427 memory_region_name(mr), dev);
2428}
2429
2430void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2431{
2432 /* Nothing do to while the implementation is in RAMBlock */
2433}
2434
2435void vmstate_register_ram_global(MemoryRegion *mr)
2436{
2437 vmstate_register_ram(mr, NULL);
2438}
Orit Wasserman302dfbe2012-08-06 21:42:52 +03002439
2440/*
2441 page = zrun nzrun
2442 | zrun nzrun page
2443
2444 zrun = length
2445
2446 nzrun = length byte...
2447
2448 length = uleb128 encoded integer
2449 */
2450int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2451 uint8_t *dst, int dlen)
2452{
2453 uint32_t zrun_len = 0, nzrun_len = 0;
2454 int d = 0, i = 0;
2455 long res, xor;
2456 uint8_t *nzrun_start = NULL;
2457
2458 g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2459 sizeof(long)));
2460
2461 while (i < slen) {
2462 /* overflow */
2463 if (d + 2 > dlen) {
2464 return -1;
2465 }
2466
2467 /* not aligned to sizeof(long) */
2468 res = (slen - i) % sizeof(long);
2469 while (res && old_buf[i] == new_buf[i]) {
2470 zrun_len++;
2471 i++;
2472 res--;
2473 }
2474
2475 /* word at a time for speed */
2476 if (!res) {
2477 while (i < slen &&
2478 (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2479 i += sizeof(long);
2480 zrun_len += sizeof(long);
2481 }
2482
2483 /* go over the rest */
2484 while (i < slen && old_buf[i] == new_buf[i]) {
2485 zrun_len++;
2486 i++;
2487 }
2488 }
2489
2490 /* buffer unchanged */
2491 if (zrun_len == slen) {
2492 return 0;
2493 }
2494
2495 /* skip last zero run */
2496 if (i == slen) {
2497 return d;
2498 }
2499
2500 d += uleb128_encode_small(dst + d, zrun_len);
2501
2502 zrun_len = 0;
2503 nzrun_start = new_buf + i;
2504
2505 /* overflow */
2506 if (d + 2 > dlen) {
2507 return -1;
2508 }
2509 /* not aligned to sizeof(long) */
2510 res = (slen - i) % sizeof(long);
2511 while (res && old_buf[i] != new_buf[i]) {
2512 i++;
2513 nzrun_len++;
2514 res--;
2515 }
2516
2517 /* word at a time for speed, use of 32-bit long okay */
2518 if (!res) {
2519 /* truncation to 32-bit long okay */
Alexander Grafa5b71722012-08-14 12:53:18 +02002520 long mask = (long)0x0101010101010101ULL;
Orit Wasserman302dfbe2012-08-06 21:42:52 +03002521 while (i < slen) {
2522 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2523 if ((xor - mask) & ~xor & (mask << 7)) {
2524 /* found the end of an nzrun within the current long */
2525 while (old_buf[i] != new_buf[i]) {
2526 nzrun_len++;
2527 i++;
2528 }
2529 break;
2530 } else {
2531 i += sizeof(long);
2532 nzrun_len += sizeof(long);
2533 }
2534 }
2535 }
2536
2537 d += uleb128_encode_small(dst + d, nzrun_len);
2538 /* overflow */
2539 if (d + nzrun_len > dlen) {
2540 return -1;
2541 }
2542 memcpy(dst + d, nzrun_start, nzrun_len);
2543 d += nzrun_len;
2544 nzrun_len = 0;
2545 }
2546
2547 return d;
2548}
2549
2550int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2551{
2552 int i = 0, d = 0;
2553 int ret;
2554 uint32_t count = 0;
2555
2556 while (i < slen) {
2557
2558 /* zrun */
2559 if ((slen - i) < 2) {
2560 return -1;
2561 }
2562
2563 ret = uleb128_decode_small(src + i, &count);
2564 if (ret < 0 || (i && !count)) {
2565 return -1;
2566 }
2567 i += ret;
2568 d += count;
2569
2570 /* overflow */
2571 if (d > dlen) {
2572 return -1;
2573 }
2574
2575 /* nzrun */
2576 if ((slen - i) < 2) {
2577 return -1;
2578 }
2579
2580 ret = uleb128_decode_small(src + i, &count);
2581 if (ret < 0 || !count) {
2582 return -1;
2583 }
2584 i += ret;
2585
2586 /* overflow */
2587 if (d + count > dlen || i + count > slen) {
2588 return -1;
2589 }
2590
2591 memcpy(dst + d, src + i, count);
2592 d += count;
2593 i += count;
2594 }
2595
2596 return d;
2597}