blob: d1d90200e1053bbbdaa158327aa9ba35d31debed [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"
blueswir1511d2b12009-03-07 15:32:56 +000075#include "net.h"
76#include "monitor.h"
77#include "sysemu.h"
78#include "qemu-timer.h"
79#include "qemu-char.h"
blueswir1511d2b12009-03-07 15:32:56 +000080#include "audio/audio.h"
81#include "migration.h"
82#include "qemu_socket.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +000083#include "qemu-queue.h"
Paolo Bonzini2ff68d02011-09-12 16:21:44 +020084#include "qemu-timer.h"
Blue Swirl17a46632011-03-27 16:05:08 +000085#include "cpus.h"
Avi Kivityc5705a72011-12-20 15:59:12 +020086#include "memory.h"
Stefano Stabellinia7ae8352012-01-25 12:24:51 +000087#include "qmp-commands.h"
blueswir1511d2b12009-03-07 15:32:56 +000088
aliguoria672b462008-11-11 21:33:36 +000089#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000090
Nolan18995b92009-10-15 16:53:55 -070091#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040092#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070093#endif
94#define ARP_HTYPE_ETH 0x0001
95#define ARP_PTYPE_IP 0x0800
96#define ARP_OP_REQUEST_REV 0x3
97
98static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000099 uint8_t *mac_addr)
100{
Nolan18995b92009-10-15 16:53:55 -0700101 /* Ethernet header. */
102 memset(buf, 0xff, 6); /* destination MAC addr */
103 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
104 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000105
Nolan18995b92009-10-15 16:53:55 -0700106 /* RARP header. */
107 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
108 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
109 *(buf + 18) = 6; /* hardware addr length (ethernet) */
110 *(buf + 19) = 4; /* protocol addr length (IPv4) */
111 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
112 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
113 memset(buf + 28, 0x00, 4); /* source protocol addr */
114 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
115 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000116
Nolan18995b92009-10-15 16:53:55 -0700117 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
118 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000119
Nolan18995b92009-10-15 16:53:55 -0700120 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000121}
122
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000123static void qemu_announce_self_iter(NICState *nic, void *opaque)
124{
125 uint8_t buf[60];
126 int len;
127
128 len = announce_self_create(buf, nic->conf->macaddr.a);
129
130 qemu_send_packet_raw(&nic->nc, buf, len);
131}
132
133
Gleb Natapoved8b3302009-05-21 17:17:44 +0300134static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000135{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300136 static int count = SELF_ANNOUNCE_ROUNDS;
137 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000138
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000139 qemu_foreach_nic(qemu_announce_self_iter, NULL);
140
Nolan18995b92009-10-15 16:53:55 -0700141 if (--count) {
142 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100143 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700144 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300145 } else {
146 qemu_del_timer(timer);
147 qemu_free_timer(timer);
148 }
149}
150
151void qemu_announce_self(void)
152{
153 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100154 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300155 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000156}
157
158/***********************************************************/
159/* savevm/loadvm support */
160
161#define IO_BUF_SIZE 32768
162
163struct QEMUFile {
164 QEMUFilePutBufferFunc *put_buffer;
165 QEMUFileGetBufferFunc *get_buffer;
166 QEMUFileCloseFunc *close;
167 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400168 QEMUFileSetRateLimit *set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200169 QEMUFileGetRateLimit *get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000170 void *opaque;
171 int is_write;
172
173 int64_t buf_offset; /* start of buffer when writing, end of buffer
174 when reading */
175 int buf_index;
176 int buf_size; /* 0 when writing */
177 uint8_t buf[IO_BUF_SIZE];
178
Juan Quintela3961b4d2011-10-05 01:05:21 +0200179 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000180};
181
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200182typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000183{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200184 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000185 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200186} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000187
188typedef struct QEMUFileSocket
189{
190 int fd;
191 QEMUFile *file;
192} QEMUFileSocket;
193
194static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
195{
196 QEMUFileSocket *s = opaque;
197 ssize_t len;
198
199 do {
Blue Swirl00aa0042011-07-23 20:04:29 +0000200 len = qemu_recv(s->fd, buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000201 } while (len == -1 && socket_error() == EINTR);
202
203 if (len == -1)
204 len = -socket_error();
205
206 return len;
207}
208
209static int socket_close(void *opaque)
210{
211 QEMUFileSocket *s = opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500212 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000213 return 0;
214}
215
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200216static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000217{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200218 QEMUFileStdio *s = opaque;
219 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000220}
221
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200222static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000223{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200224 QEMUFileStdio *s = opaque;
225 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300226 int bytes;
227
228 do {
229 clearerr(fp);
230 bytes = fread(buf, 1, size, fp);
231 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
232 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000233}
234
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200235static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000236{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200237 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500238 int ret;
239 ret = pclose(s->stdio_file);
Eduardo Habkost26f1af02011-11-10 10:41:44 -0200240 if (ret == -1) {
241 ret = -errno;
242 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500243 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500244 return ret;
aliguoria672b462008-11-11 21:33:36 +0000245}
246
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200247static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000248{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200249 QEMUFileStdio *s = opaque;
Eduardo Habkost0e286702011-11-10 10:41:45 -0200250 int ret = 0;
251 if (fclose(s->stdio_file) == EOF) {
252 ret = -errno;
253 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500254 g_free(s);
Eduardo Habkost0e286702011-11-10 10:41:45 -0200255 return ret;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200256}
aliguoria672b462008-11-11 21:33:36 +0000257
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200258QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
259{
260 QEMUFileStdio *s;
261
262 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000263 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
264 return NULL;
265 }
266
Anthony Liguori7267c092011-08-20 22:09:37 -0500267 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000268
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200269 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000270
271 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200272 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
273 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000274 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200275 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
276 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000277 }
aliguoria672b462008-11-11 21:33:36 +0000278 return s->file;
279}
280
281QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
282{
283 FILE *popen_file;
284
285 popen_file = popen(command, mode);
286 if(popen_file == NULL) {
287 return NULL;
288 }
289
290 return qemu_popen(popen_file, mode);
291}
292
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200293int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200294{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200295 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200296 int fd;
297
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200298 p = (QEMUFileStdio *)f->opaque;
299 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200300
301 return fd;
302}
303
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200304QEMUFile *qemu_fdopen(int fd, const char *mode)
305{
306 QEMUFileStdio *s;
307
308 if (mode == NULL ||
309 (mode[0] != 'r' && mode[0] != 'w') ||
310 mode[1] != 'b' || mode[2] != 0) {
311 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
312 return NULL;
313 }
314
Anthony Liguori7267c092011-08-20 22:09:37 -0500315 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200316 s->stdio_file = fdopen(fd, mode);
317 if (!s->stdio_file)
318 goto fail;
319
320 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200321 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
322 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200323 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200324 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
325 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200326 }
327 return s->file;
328
329fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500330 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200331 return NULL;
332}
333
aliguoria672b462008-11-11 21:33:36 +0000334QEMUFile *qemu_fopen_socket(int fd)
335{
Anthony Liguori7267c092011-08-20 22:09:37 -0500336 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000337
aliguoria672b462008-11-11 21:33:36 +0000338 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200339 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
340 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000341 return s->file;
342}
343
aliguoria672b462008-11-11 21:33:36 +0000344static int file_put_buffer(void *opaque, const uint8_t *buf,
345 int64_t pos, int size)
346{
347 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200348 fseek(s->stdio_file, pos, SEEK_SET);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000349 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000350}
351
352static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
353{
354 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200355 fseek(s->stdio_file, pos, SEEK_SET);
356 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000357}
358
359QEMUFile *qemu_fopen(const char *filename, const char *mode)
360{
361 QEMUFileStdio *s;
362
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200363 if (mode == NULL ||
364 (mode[0] != 'r' && mode[0] != 'w') ||
365 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000366 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200367 return NULL;
368 }
369
Anthony Liguori7267c092011-08-20 22:09:37 -0500370 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000371
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200372 s->stdio_file = fopen(filename, mode);
373 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000374 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200375
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200376 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200377 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
378 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200379 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200380 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
381 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200382 }
383 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000384fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500385 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000386 return NULL;
387}
388
aliguori178e08a2009-04-05 19:10:55 +0000389static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000390 int64_t pos, int size)
391{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200392 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000393 return size;
394}
395
aliguori178e08a2009-04-05 19:10:55 +0000396static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000397{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200398 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000399}
400
401static int bdrv_fclose(void *opaque)
402{
Paolo Bonziniad492c92012-06-06 00:04:50 +0200403 return bdrv_flush(opaque);
aliguoria672b462008-11-11 21:33:36 +0000404}
405
Christoph Hellwig45566e92009-07-10 23:11:57 +0200406static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000407{
aliguoria672b462008-11-11 21:33:36 +0000408 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200409 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
410 NULL, NULL, NULL);
411 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000412}
413
414QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
415 QEMUFileGetBufferFunc *get_buffer,
416 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400417 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200418 QEMUFileSetRateLimit *set_rate_limit,
419 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000420{
421 QEMUFile *f;
422
Anthony Liguori7267c092011-08-20 22:09:37 -0500423 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000424
425 f->opaque = opaque;
426 f->put_buffer = put_buffer;
427 f->get_buffer = get_buffer;
428 f->close = close;
429 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400430 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200431 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000432 f->is_write = 0;
433
434 return f;
435}
436
Juan Quintela624b9cc2011-10-05 01:02:52 +0200437int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000438{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200439 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000440}
441
Juan Quinteladcd1d222011-09-21 23:01:54 +0200442void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000443{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200444 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000445}
446
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200447/** Sets last_error conditionally
448 *
449 * Sets last_error only if ret is negative _and_ no error
450 * was set before.
451 */
452static void qemu_file_set_if_error(QEMUFile *f, int ret)
453{
454 if (ret < 0 && !f->last_error) {
455 qemu_file_set_error(f, ret);
456 }
457}
458
459/** Flushes QEMUFile buffer
460 *
461 * In case of error, last_error is set.
462 */
aliguoria672b462008-11-11 21:33:36 +0000463void qemu_fflush(QEMUFile *f)
464{
465 if (!f->put_buffer)
466 return;
467
468 if (f->is_write && f->buf_index > 0) {
469 int len;
470
471 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
472 if (len > 0)
473 f->buf_offset += f->buf_index;
474 else
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200475 qemu_file_set_error(f, -EINVAL);
aliguoria672b462008-11-11 21:33:36 +0000476 f->buf_index = 0;
477 }
478}
479
480static void qemu_fill_buffer(QEMUFile *f)
481{
482 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200483 int pending;
aliguoria672b462008-11-11 21:33:36 +0000484
485 if (!f->get_buffer)
486 return;
487
488 if (f->is_write)
489 abort();
490
Juan Quintela0046c452011-09-30 19:28:45 +0200491 pending = f->buf_size - f->buf_index;
492 if (pending > 0) {
493 memmove(f->buf, f->buf + f->buf_index, pending);
494 }
495 f->buf_index = 0;
496 f->buf_size = pending;
497
498 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
499 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000500 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200501 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000502 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200503 } else if (len == 0) {
504 f->last_error = -EIO;
aliguoria672b462008-11-11 21:33:36 +0000505 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200506 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000507}
508
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200509/** Calls close function and set last_error if needed
510 *
511 * Internal function. qemu_fflush() must be called before this.
512 *
513 * Returns f->close() return value, or 0 if close function is not set.
514 */
515static int qemu_close(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000516{
517 int ret = 0;
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200518 if (f->close) {
aliguoria672b462008-11-11 21:33:36 +0000519 ret = f->close(f->opaque);
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200520 qemu_file_set_if_error(f, ret);
521 }
522 return ret;
523}
524
525/** Closes the file
526 *
527 * Returns negative error value if any error happened on previous operations or
528 * while closing the file. Returns 0 or positive number on success.
529 *
530 * The meaning of return value on success depends on the specific backend
531 * being used.
532 */
533int qemu_fclose(QEMUFile *f)
534{
535 int ret;
536 qemu_fflush(f);
537 ret = qemu_close(f);
538 /* If any error was spotted before closing, we should report it
539 * instead of the close() return value.
540 */
541 if (f->last_error) {
542 ret = f->last_error;
543 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500544 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000545 return ret;
546}
547
548void qemu_file_put_notify(QEMUFile *f)
549{
550 f->put_buffer(f->opaque, NULL, 0, 0);
551}
552
553void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
554{
555 int l;
556
Juan Quintela3961b4d2011-10-05 01:05:21 +0200557 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000558 fprintf(stderr,
559 "Attempted to write to buffer while read buffer is not empty\n");
560 abort();
561 }
562
Juan Quintela3961b4d2011-10-05 01:05:21 +0200563 while (!f->last_error && size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000564 l = IO_BUF_SIZE - f->buf_index;
565 if (l > size)
566 l = size;
567 memcpy(f->buf + f->buf_index, buf, l);
568 f->is_write = 1;
569 f->buf_index += l;
570 buf += l;
571 size -= l;
572 if (f->buf_index >= IO_BUF_SIZE)
573 qemu_fflush(f);
574 }
575}
576
577void qemu_put_byte(QEMUFile *f, int v)
578{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200579 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000580 fprintf(stderr,
581 "Attempted to write to buffer while read buffer is not empty\n");
582 abort();
583 }
584
585 f->buf[f->buf_index++] = v;
586 f->is_write = 1;
587 if (f->buf_index >= IO_BUF_SIZE)
588 qemu_fflush(f);
589}
590
Juan Quintelac6380722011-10-04 15:28:31 +0200591static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000592{
Juan Quintelac6380722011-10-04 15:28:31 +0200593 if (f->buf_index + size <= f->buf_size) {
594 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000595 }
aliguoria672b462008-11-11 21:33:36 +0000596}
597
Juan Quintelac6380722011-10-04 15:28:31 +0200598static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200599{
Juan Quintelac6380722011-10-04 15:28:31 +0200600 int pending;
601 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200602
Juan Quintelab9ce1452011-10-04 13:55:32 +0200603 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200604 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200605 }
Juan Quintela811814b2010-07-26 21:38:43 +0200606
Juan Quintelac6380722011-10-04 15:28:31 +0200607 index = f->buf_index + offset;
608 pending = f->buf_size - index;
609 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200610 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200611 index = f->buf_index + offset;
612 pending = f->buf_size - index;
613 }
614
615 if (pending <= 0) {
616 return 0;
617 }
618 if (size > pending) {
619 size = pending;
620 }
621
622 memcpy(buf, f->buf + index, size);
623 return size;
624}
625
626int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
627{
628 int pending = size;
629 int done = 0;
630
631 while (pending > 0) {
632 int res;
633
634 res = qemu_peek_buffer(f, buf, pending, 0);
635 if (res == 0) {
636 return done;
637 }
638 qemu_file_skip(f, res);
639 buf += res;
640 pending -= res;
641 done += res;
642 }
643 return done;
644}
645
646static int qemu_peek_byte(QEMUFile *f, int offset)
647{
648 int index = f->buf_index + offset;
649
650 if (f->is_write) {
651 abort();
652 }
653
654 if (index >= f->buf_size) {
655 qemu_fill_buffer(f);
656 index = f->buf_index + offset;
657 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200658 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200659 }
Juan Quintela811814b2010-07-26 21:38:43 +0200660 }
Juan Quintelac6380722011-10-04 15:28:31 +0200661 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200662}
663
aliguoria672b462008-11-11 21:33:36 +0000664int qemu_get_byte(QEMUFile *f)
665{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200666 int result;
aliguoria672b462008-11-11 21:33:36 +0000667
Juan Quintelac6380722011-10-04 15:28:31 +0200668 result = qemu_peek_byte(f, 0);
669 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200670 return result;
aliguoria672b462008-11-11 21:33:36 +0000671}
672
673int64_t qemu_ftell(QEMUFile *f)
674{
675 return f->buf_offset - f->buf_size + f->buf_index;
676}
677
678int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
679{
680 if (whence == SEEK_SET) {
681 /* nothing to do */
682 } else if (whence == SEEK_CUR) {
683 pos += qemu_ftell(f);
684 } else {
685 /* SEEK_END not supported */
686 return -1;
687 }
688 if (f->put_buffer) {
689 qemu_fflush(f);
690 f->buf_offset = pos;
691 } else {
692 f->buf_offset = pos;
693 f->buf_index = 0;
694 f->buf_size = 0;
695 }
696 return pos;
697}
698
699int qemu_file_rate_limit(QEMUFile *f)
700{
701 if (f->rate_limit)
702 return f->rate_limit(f->opaque);
703
704 return 0;
705}
706
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200707int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200708{
709 if (f->get_rate_limit)
710 return f->get_rate_limit(f->opaque);
711
712 return 0;
713}
714
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200715int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400716{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400717 /* any failed or completed migration keeps its state to allow probing of
718 * migration data, but has no associated file anymore */
719 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400720 return f->set_rate_limit(f->opaque, new_rate);
721
722 return 0;
723}
724
aliguoria672b462008-11-11 21:33:36 +0000725void qemu_put_be16(QEMUFile *f, unsigned int v)
726{
727 qemu_put_byte(f, v >> 8);
728 qemu_put_byte(f, v);
729}
730
731void qemu_put_be32(QEMUFile *f, unsigned int v)
732{
733 qemu_put_byte(f, v >> 24);
734 qemu_put_byte(f, v >> 16);
735 qemu_put_byte(f, v >> 8);
736 qemu_put_byte(f, v);
737}
738
739void qemu_put_be64(QEMUFile *f, uint64_t v)
740{
741 qemu_put_be32(f, v >> 32);
742 qemu_put_be32(f, v);
743}
744
745unsigned int qemu_get_be16(QEMUFile *f)
746{
747 unsigned int v;
748 v = qemu_get_byte(f) << 8;
749 v |= qemu_get_byte(f);
750 return v;
751}
752
753unsigned int qemu_get_be32(QEMUFile *f)
754{
755 unsigned int v;
756 v = qemu_get_byte(f) << 24;
757 v |= qemu_get_byte(f) << 16;
758 v |= qemu_get_byte(f) << 8;
759 v |= qemu_get_byte(f);
760 return v;
761}
762
763uint64_t qemu_get_be64(QEMUFile *f)
764{
765 uint64_t v;
766 v = (uint64_t)qemu_get_be32(f) << 32;
767 v |= qemu_get_be32(f);
768 return v;
769}
770
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200771
772/* timer */
773
774void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
775{
776 uint64_t expire_time;
777
778 expire_time = qemu_timer_expire_time_ns(ts);
779 qemu_put_be64(f, expire_time);
780}
781
782void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
783{
784 uint64_t expire_time;
785
786 expire_time = qemu_get_be64(f);
787 if (expire_time != -1) {
788 qemu_mod_timer_ns(ts, expire_time);
789 } else {
790 qemu_del_timer(ts);
791 }
792}
793
794
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100795/* bool */
796
797static int get_bool(QEMUFile *f, void *pv, size_t size)
798{
799 bool *v = pv;
800 *v = qemu_get_byte(f);
801 return 0;
802}
803
804static void put_bool(QEMUFile *f, void *pv, size_t size)
805{
806 bool *v = pv;
807 qemu_put_byte(f, *v);
808}
809
810const VMStateInfo vmstate_info_bool = {
811 .name = "bool",
812 .get = get_bool,
813 .put = put_bool,
814};
815
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200816/* 8 bit int */
817
818static int get_int8(QEMUFile *f, void *pv, size_t size)
819{
820 int8_t *v = pv;
821 qemu_get_s8s(f, v);
822 return 0;
823}
824
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200825static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200826{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200827 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200828 qemu_put_s8s(f, v);
829}
830
831const VMStateInfo vmstate_info_int8 = {
832 .name = "int8",
833 .get = get_int8,
834 .put = put_int8,
835};
836
837/* 16 bit int */
838
839static int get_int16(QEMUFile *f, void *pv, size_t size)
840{
841 int16_t *v = pv;
842 qemu_get_sbe16s(f, v);
843 return 0;
844}
845
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200846static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200847{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200848 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200849 qemu_put_sbe16s(f, v);
850}
851
852const VMStateInfo vmstate_info_int16 = {
853 .name = "int16",
854 .get = get_int16,
855 .put = put_int16,
856};
857
858/* 32 bit int */
859
860static int get_int32(QEMUFile *f, void *pv, size_t size)
861{
862 int32_t *v = pv;
863 qemu_get_sbe32s(f, v);
864 return 0;
865}
866
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200867static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200868{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200869 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200870 qemu_put_sbe32s(f, v);
871}
872
873const VMStateInfo vmstate_info_int32 = {
874 .name = "int32",
875 .get = get_int32,
876 .put = put_int32,
877};
878
Juan Quintela82501662009-08-20 19:42:32 +0200879/* 32 bit int. See that the received value is the same than the one
880 in the field */
881
882static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
883{
884 int32_t *v = pv;
885 int32_t v2;
886 qemu_get_sbe32s(f, &v2);
887
888 if (*v == v2)
889 return 0;
890 return -EINVAL;
891}
892
893const VMStateInfo vmstate_info_int32_equal = {
894 .name = "int32 equal",
895 .get = get_int32_equal,
896 .put = put_int32,
897};
898
Juan Quintela0a031e02009-08-20 19:42:37 +0200899/* 32 bit int. See that the received value is the less or the same
900 than the one in the field */
901
902static int get_int32_le(QEMUFile *f, void *pv, size_t size)
903{
904 int32_t *old = pv;
905 int32_t new;
906 qemu_get_sbe32s(f, &new);
907
908 if (*old <= new)
909 return 0;
910 return -EINVAL;
911}
912
913const VMStateInfo vmstate_info_int32_le = {
914 .name = "int32 equal",
915 .get = get_int32_le,
916 .put = put_int32,
917};
918
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200919/* 64 bit int */
920
921static int get_int64(QEMUFile *f, void *pv, size_t size)
922{
923 int64_t *v = pv;
924 qemu_get_sbe64s(f, v);
925 return 0;
926}
927
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200928static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200929{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200930 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200931 qemu_put_sbe64s(f, v);
932}
933
934const VMStateInfo vmstate_info_int64 = {
935 .name = "int64",
936 .get = get_int64,
937 .put = put_int64,
938};
939
940/* 8 bit unsigned int */
941
942static int get_uint8(QEMUFile *f, void *pv, size_t size)
943{
944 uint8_t *v = pv;
945 qemu_get_8s(f, v);
946 return 0;
947}
948
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200949static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200950{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200951 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200952 qemu_put_8s(f, v);
953}
954
955const VMStateInfo vmstate_info_uint8 = {
956 .name = "uint8",
957 .get = get_uint8,
958 .put = put_uint8,
959};
960
961/* 16 bit unsigned int */
962
963static int get_uint16(QEMUFile *f, void *pv, size_t size)
964{
965 uint16_t *v = pv;
966 qemu_get_be16s(f, v);
967 return 0;
968}
969
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200970static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200971{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200972 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200973 qemu_put_be16s(f, v);
974}
975
976const VMStateInfo vmstate_info_uint16 = {
977 .name = "uint16",
978 .get = get_uint16,
979 .put = put_uint16,
980};
981
982/* 32 bit unsigned int */
983
984static int get_uint32(QEMUFile *f, void *pv, size_t size)
985{
986 uint32_t *v = pv;
987 qemu_get_be32s(f, v);
988 return 0;
989}
990
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200991static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200992{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200993 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200994 qemu_put_be32s(f, v);
995}
996
997const VMStateInfo vmstate_info_uint32 = {
998 .name = "uint32",
999 .get = get_uint32,
1000 .put = put_uint32,
1001};
1002
Juan Quintela9122a8f2011-03-10 12:33:48 +01001003/* 32 bit uint. See that the received value is the same than the one
1004 in the field */
1005
1006static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1007{
1008 uint32_t *v = pv;
1009 uint32_t v2;
1010 qemu_get_be32s(f, &v2);
1011
1012 if (*v == v2) {
1013 return 0;
1014 }
1015 return -EINVAL;
1016}
1017
1018const VMStateInfo vmstate_info_uint32_equal = {
1019 .name = "uint32 equal",
1020 .get = get_uint32_equal,
1021 .put = put_uint32,
1022};
1023
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001024/* 64 bit unsigned int */
1025
1026static int get_uint64(QEMUFile *f, void *pv, size_t size)
1027{
1028 uint64_t *v = pv;
1029 qemu_get_be64s(f, v);
1030 return 0;
1031}
1032
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001033static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001034{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001035 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001036 qemu_put_be64s(f, v);
1037}
1038
1039const VMStateInfo vmstate_info_uint64 = {
1040 .name = "uint64",
1041 .get = get_uint64,
1042 .put = put_uint64,
1043};
1044
Juan Quintela80cd83e2009-09-10 03:04:36 +02001045/* 8 bit int. See that the received value is the same than the one
1046 in the field */
1047
1048static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1049{
1050 uint8_t *v = pv;
1051 uint8_t v2;
1052 qemu_get_8s(f, &v2);
1053
1054 if (*v == v2)
1055 return 0;
1056 return -EINVAL;
1057}
1058
1059const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001060 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001061 .get = get_uint8_equal,
1062 .put = put_uint8,
1063};
1064
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001065/* 16 bit unsigned int int. See that the received value is the same than the one
1066 in the field */
1067
1068static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1069{
1070 uint16_t *v = pv;
1071 uint16_t v2;
1072 qemu_get_be16s(f, &v2);
1073
1074 if (*v == v2)
1075 return 0;
1076 return -EINVAL;
1077}
1078
1079const VMStateInfo vmstate_info_uint16_equal = {
1080 .name = "uint16 equal",
1081 .get = get_uint16_equal,
1082 .put = put_uint16,
1083};
1084
Juan Quinteladde04632009-08-20 19:42:26 +02001085/* timers */
1086
1087static int get_timer(QEMUFile *f, void *pv, size_t size)
1088{
1089 QEMUTimer *v = pv;
1090 qemu_get_timer(f, v);
1091 return 0;
1092}
1093
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001094static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001095{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001096 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001097 qemu_put_timer(f, v);
1098}
1099
1100const VMStateInfo vmstate_info_timer = {
1101 .name = "timer",
1102 .get = get_timer,
1103 .put = put_timer,
1104};
1105
Juan Quintela6f67c502009-08-20 19:42:35 +02001106/* uint8_t buffers */
1107
1108static int get_buffer(QEMUFile *f, void *pv, size_t size)
1109{
1110 uint8_t *v = pv;
1111 qemu_get_buffer(f, v, size);
1112 return 0;
1113}
1114
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001115static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001116{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001117 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001118 qemu_put_buffer(f, v, size);
1119}
1120
1121const VMStateInfo vmstate_info_buffer = {
1122 .name = "buffer",
1123 .get = get_buffer,
1124 .put = put_buffer,
1125};
1126
Juan Quintela76507c72009-10-19 15:46:28 +02001127/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001128 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001129
1130static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1131{
Jan Kiszka21174c32009-12-02 12:36:35 +01001132 uint8_t buf[1024];
1133 int block_len;
1134
1135 while (size > 0) {
1136 block_len = MIN(sizeof(buf), size);
1137 size -= block_len;
1138 qemu_get_buffer(f, buf, block_len);
1139 }
1140 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001141}
1142
1143static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1144{
Jan Kiszka21174c32009-12-02 12:36:35 +01001145 static const uint8_t buf[1024];
1146 int block_len;
1147
1148 while (size > 0) {
1149 block_len = MIN(sizeof(buf), size);
1150 size -= block_len;
1151 qemu_put_buffer(f, buf, block_len);
1152 }
Juan Quintela76507c72009-10-19 15:46:28 +02001153}
1154
1155const VMStateInfo vmstate_info_unused_buffer = {
1156 .name = "unused_buffer",
1157 .get = get_unused_buffer,
1158 .put = put_unused_buffer,
1159};
1160
Alex Williamson7685ee62010-06-25 11:09:14 -06001161typedef struct CompatEntry {
1162 char idstr[256];
1163 int instance_id;
1164} CompatEntry;
1165
aliguoria672b462008-11-11 21:33:36 +00001166typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001167 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001168 char idstr[256];
1169 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001170 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001171 int version_id;
1172 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001173 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +00001174 SaveLiveStateHandler *save_live_state;
1175 SaveStateHandler *save_state;
1176 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001177 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001178 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001179 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001180 int no_migrate;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001181 int is_ram;
aliguoria672b462008-11-11 21:33:36 +00001182} SaveStateEntry;
1183
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001184
Blue Swirl72cf2d42009-09-12 07:36:22 +00001185static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1186 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001187static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001188
Juan Quintela8718e992009-09-01 02:12:31 +02001189static int calculate_new_instance_id(const char *idstr)
1190{
1191 SaveStateEntry *se;
1192 int instance_id = 0;
1193
Blue Swirl72cf2d42009-09-12 07:36:22 +00001194 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001195 if (strcmp(idstr, se->idstr) == 0
1196 && instance_id <= se->instance_id) {
1197 instance_id = se->instance_id + 1;
1198 }
1199 }
1200 return instance_id;
1201}
1202
Alex Williamson7685ee62010-06-25 11:09:14 -06001203static int calculate_compat_instance_id(const char *idstr)
1204{
1205 SaveStateEntry *se;
1206 int instance_id = 0;
1207
1208 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1209 if (!se->compat)
1210 continue;
1211
1212 if (strcmp(idstr, se->compat->idstr) == 0
1213 && instance_id <= se->compat->instance_id) {
1214 instance_id = se->compat->instance_id + 1;
1215 }
1216 }
1217 return instance_id;
1218}
1219
aliguoria672b462008-11-11 21:33:36 +00001220/* TODO: Individual devices generally have very little idea about the rest
1221 of the system, so instance_id should be removed/replaced.
1222 Meanwhile pass -1 as instance_id if you do not already have a clearly
1223 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001224int register_savevm_live(DeviceState *dev,
1225 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001226 int instance_id,
1227 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001228 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001229 SaveLiveStateHandler *save_live_state,
1230 SaveStateHandler *save_state,
1231 LoadStateHandler *load_state,
1232 void *opaque)
1233{
Juan Quintela8718e992009-09-01 02:12:31 +02001234 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001235
Anthony Liguori7267c092011-08-20 22:09:37 -05001236 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001237 se->version_id = version_id;
1238 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001239 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001240 se->save_live_state = save_live_state;
1241 se->save_state = save_state;
1242 se->load_state = load_state;
1243 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001244 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001245 se->no_migrate = 0;
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001246 /* if this is a live_savem then set is_ram */
1247 if (save_live_state != NULL) {
1248 se->is_ram = 1;
1249 }
aliguoria672b462008-11-11 21:33:36 +00001250
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001251 if (dev) {
1252 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001253 if (id) {
1254 pstrcpy(se->idstr, sizeof(se->idstr), id);
1255 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001256 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001257
Anthony Liguori7267c092011-08-20 22:09:37 -05001258 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001259 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1260 se->compat->instance_id = instance_id == -1 ?
1261 calculate_compat_instance_id(idstr) : instance_id;
1262 instance_id = -1;
1263 }
1264 }
1265 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1266
Juan Quintela8718e992009-09-01 02:12:31 +02001267 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001268 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001269 } else {
1270 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001271 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001272 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001273 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001274 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001275 return 0;
1276}
1277
Alex Williamson0be71e32010-06-25 11:09:07 -06001278int register_savevm(DeviceState *dev,
1279 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001280 int instance_id,
1281 int version_id,
1282 SaveStateHandler *save_state,
1283 LoadStateHandler *load_state,
1284 void *opaque)
1285{
Alex Williamson0be71e32010-06-25 11:09:07 -06001286 return register_savevm_live(dev, idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001287 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001288}
1289
Alex Williamson0be71e32010-06-25 11:09:07 -06001290void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001291{
Juan Quintela8718e992009-09-01 02:12:31 +02001292 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001293 char id[256] = "";
1294
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001295 if (dev) {
1296 char *path = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001297 if (path) {
1298 pstrcpy(id, sizeof(id), path);
1299 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001300 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001301 }
1302 }
1303 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001304
Blue Swirl72cf2d42009-09-12 07:36:22 +00001305 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001306 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001307 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001308 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001309 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001310 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001311 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001312 }
aliguori41bd13a2009-04-17 17:10:59 +00001313 }
1314}
1315
Alex Williamson0be71e32010-06-25 11:09:07 -06001316int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001317 const VMStateDescription *vmsd,
1318 void *opaque, int alias_id,
1319 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001320{
Juan Quintela8718e992009-09-01 02:12:31 +02001321 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001322
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001323 /* If this triggers, alias support can be dropped for the vmsd. */
1324 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1325
Anthony Liguori7267c092011-08-20 22:09:37 -05001326 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001327 se->version_id = vmsd->version_id;
1328 se->section_id = global_section_id++;
1329 se->save_live_state = NULL;
1330 se->save_state = NULL;
1331 se->load_state = NULL;
1332 se->opaque = opaque;
1333 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001334 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001335 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001336
Anthony Liguori09e5ab62012-02-03 12:28:43 -06001337 if (dev) {
1338 char *id = qdev_get_dev_path(dev);
Alex Williamson7685ee62010-06-25 11:09:14 -06001339 if (id) {
1340 pstrcpy(se->idstr, sizeof(se->idstr), id);
1341 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001342 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001343
Anthony Liguori7267c092011-08-20 22:09:37 -05001344 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001345 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1346 se->compat->instance_id = instance_id == -1 ?
1347 calculate_compat_instance_id(vmsd->name) : instance_id;
1348 instance_id = -1;
1349 }
1350 }
1351 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1352
Juan Quintela8718e992009-09-01 02:12:31 +02001353 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001354 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001355 } else {
1356 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001357 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001358 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001359 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001360 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001361 return 0;
1362}
1363
Alex Williamson0be71e32010-06-25 11:09:07 -06001364int vmstate_register(DeviceState *dev, int instance_id,
1365 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001366{
Alex Williamson0be71e32010-06-25 11:09:07 -06001367 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1368 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001369}
1370
Alex Williamson0be71e32010-06-25 11:09:07 -06001371void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1372 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001373{
Juan Quintela1eb75382009-09-10 03:04:29 +02001374 SaveStateEntry *se, *new_se;
1375
Blue Swirl72cf2d42009-09-12 07:36:22 +00001376 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001377 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001378 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001379 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001380 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001381 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001382 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001383 }
1384 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001385}
1386
Juan Quintela811814b2010-07-26 21:38:43 +02001387static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1388 void *opaque);
1389static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1390 void *opaque);
1391
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001392int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1393 void *opaque, int version_id)
1394{
1395 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001396 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001397
1398 if (version_id > vmsd->version_id) {
1399 return -EINVAL;
1400 }
1401 if (version_id < vmsd->minimum_version_id_old) {
1402 return -EINVAL;
1403 }
1404 if (version_id < vmsd->minimum_version_id) {
1405 return vmsd->load_state_old(f, opaque, version_id);
1406 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001407 if (vmsd->pre_load) {
1408 int ret = vmsd->pre_load(opaque);
1409 if (ret)
1410 return ret;
1411 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001412 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001413 if ((field->field_exists &&
1414 field->field_exists(opaque, version_id)) ||
1415 (!field->field_exists &&
1416 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001417 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001418 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001419 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001420
Juan Quintelae61a1e02009-12-02 12:36:38 +01001421 if (field->flags & VMS_VBUFFER) {
1422 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001423 if (field->flags & VMS_MULTIPLY) {
1424 size *= field->size;
1425 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001426 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001427 if (field->flags & VMS_ARRAY) {
1428 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001429 } else if (field->flags & VMS_VARRAY_INT32) {
1430 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001431 } else if (field->flags & VMS_VARRAY_UINT32) {
1432 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001433 } else if (field->flags & VMS_VARRAY_UINT16) {
1434 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001435 } else if (field->flags & VMS_VARRAY_UINT8) {
1436 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001437 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001438 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001439 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001440 }
1441 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001442 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001443
Juan Quintela19df4382009-09-29 22:48:41 +02001444 if (field->flags & VMS_ARRAY_OF_POINTER) {
1445 addr = *(void **)addr;
1446 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001447 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001448 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001449 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001450 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001451
1452 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001453 if (ret < 0) {
1454 return ret;
1455 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001456 }
1457 }
1458 field++;
1459 }
Juan Quintela811814b2010-07-26 21:38:43 +02001460 ret = vmstate_subsection_load(f, vmsd, opaque);
1461 if (ret != 0) {
1462 return ret;
1463 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001464 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001465 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001466 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001467 return 0;
1468}
1469
1470void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001471 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001472{
1473 VMStateField *field = vmsd->fields;
1474
Juan Quintela8fb07912009-09-10 03:04:32 +02001475 if (vmsd->pre_save) {
1476 vmsd->pre_save(opaque);
1477 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001478 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001479 if (!field->field_exists ||
1480 field->field_exists(opaque, vmsd->version_id)) {
1481 void *base_addr = opaque + field->offset;
1482 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001483 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001484
Juan Quintelae61a1e02009-12-02 12:36:38 +01001485 if (field->flags & VMS_VBUFFER) {
1486 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001487 if (field->flags & VMS_MULTIPLY) {
1488 size *= field->size;
1489 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001490 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001491 if (field->flags & VMS_ARRAY) {
1492 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001493 } else if (field->flags & VMS_VARRAY_INT32) {
1494 n_elems = *(int32_t *)(opaque+field->num_offset);
Amos Kong1329d182012-03-13 14:05:36 +08001495 } else if (field->flags & VMS_VARRAY_UINT32) {
1496 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001497 } else if (field->flags & VMS_VARRAY_UINT16) {
1498 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001499 } else if (field->flags & VMS_VARRAY_UINT8) {
1500 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001501 }
1502 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001503 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001504 }
1505 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001506 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001507
Juan Quintela85953872009-12-02 12:36:37 +01001508 if (field->flags & VMS_ARRAY_OF_POINTER) {
1509 addr = *(void **)addr;
1510 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001511 if (field->flags & VMS_STRUCT) {
1512 vmstate_save_state(f, field->vmsd, addr);
1513 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001514 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001515 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001516 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001517 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001518 field++;
1519 }
Juan Quintela811814b2010-07-26 21:38:43 +02001520 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001521}
1522
Juan Quintela4082be42009-08-20 19:42:24 +02001523static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1524{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001525 if (!se->vmsd) { /* Old style */
1526 return se->load_state(f, se->opaque, version_id);
1527 }
1528 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001529}
1530
Alex Williamsondc912122011-01-11 14:39:43 -07001531static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001532{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001533 if (!se->vmsd) { /* Old style */
1534 se->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001535 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001536 }
1537 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001538}
1539
aliguoria672b462008-11-11 21:33:36 +00001540#define QEMU_VM_FILE_MAGIC 0x5145564d
1541#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1542#define QEMU_VM_FILE_VERSION 0x00000003
1543
1544#define QEMU_VM_EOF 0x00
1545#define QEMU_VM_SECTION_START 0x01
1546#define QEMU_VM_SECTION_PART 0x02
1547#define QEMU_VM_SECTION_END 0x03
1548#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001549#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001550
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001551bool qemu_savevm_state_blocked(Error **errp)
Alex Williamsondc912122011-01-11 14:39:43 -07001552{
1553 SaveStateEntry *se;
1554
1555 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1556 if (se->no_migrate) {
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001557 error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
Alex Williamsondc912122011-01-11 14:39:43 -07001558 return true;
1559 }
1560 }
1561 return false;
1562}
1563
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001564int qemu_savevm_state_begin(QEMUFile *f,
1565 const MigrationParams *params)
aliguoria672b462008-11-11 21:33:36 +00001566{
1567 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001568 int ret;
aliguoria672b462008-11-11 21:33:36 +00001569
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001570 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1571 if(se->set_params == NULL) {
1572 continue;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001573 }
1574 se->set_params(params, se->opaque);
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001575 }
1576
aliguoria672b462008-11-11 21:33:36 +00001577 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1578 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1579
Blue Swirl72cf2d42009-09-12 07:36:22 +00001580 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001581 int len;
1582
1583 if (se->save_live_state == NULL)
1584 continue;
1585
1586 /* Section type */
1587 qemu_put_byte(f, QEMU_VM_SECTION_START);
1588 qemu_put_be32(f, se->section_id);
1589
1590 /* ID string */
1591 len = strlen(se->idstr);
1592 qemu_put_byte(f, len);
1593 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1594
1595 qemu_put_be32(f, se->instance_id);
1596 qemu_put_be32(f, se->version_id);
1597
Luiz Capitulino539de122011-12-05 14:06:56 -02001598 ret = se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001599 if (ret < 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001600 qemu_savevm_state_cancel(f);
Juan Quintela29757252011-10-19 15:22:18 +02001601 return ret;
1602 }
aliguoria672b462008-11-11 21:33:36 +00001603 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001604 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001605 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001606 qemu_savevm_state_cancel(f);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001607 }
aliguoria672b462008-11-11 21:33:36 +00001608
Juan Quintela39346382011-09-22 11:02:14 +02001609 return ret;
1610
aliguoria672b462008-11-11 21:33:36 +00001611}
1612
Juan Quintela39346382011-09-22 11:02:14 +02001613/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001614 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001615 * negative: there was one error, and we have -errno.
1616 * 0 : We haven't finished, caller have to go again
1617 * 1 : We have finished, we can go to complete phase
1618 */
Luiz Capitulino539de122011-12-05 14:06:56 -02001619int qemu_savevm_state_iterate(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001620{
1621 SaveStateEntry *se;
1622 int ret = 1;
1623
Blue Swirl72cf2d42009-09-12 07:36:22 +00001624 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001625 if (se->save_live_state == NULL)
1626 continue;
1627
1628 /* Section type */
1629 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1630 qemu_put_be32(f, se->section_id);
1631
Luiz Capitulino539de122011-12-05 14:06:56 -02001632 ret = se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001633 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001634 /* Do not proceed to the next vmstate before this one reported
1635 completion of the current stage. This serializes the migration
1636 and reduces the probability that a faster changing state is
1637 synchronized over and over again. */
1638 break;
1639 }
aliguoria672b462008-11-11 21:33:36 +00001640 }
Juan Quintela39346382011-09-22 11:02:14 +02001641 if (ret != 0) {
1642 return ret;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001643 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001644 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001645 if (ret != 0) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001646 qemu_savevm_state_cancel(f);
Juan Quintela39346382011-09-22 11:02:14 +02001647 }
1648 return ret;
aliguoria672b462008-11-11 21:33:36 +00001649}
1650
Luiz Capitulino539de122011-12-05 14:06:56 -02001651int qemu_savevm_state_complete(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001652{
1653 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001654 int ret;
aliguoria672b462008-11-11 21:33:36 +00001655
Jan Kiszkaea375f92010-03-01 19:10:30 +01001656 cpu_synchronize_all_states();
1657
Blue Swirl72cf2d42009-09-12 07:36:22 +00001658 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001659 if (se->save_live_state == NULL)
1660 continue;
1661
1662 /* Section type */
1663 qemu_put_byte(f, QEMU_VM_SECTION_END);
1664 qemu_put_be32(f, se->section_id);
1665
Luiz Capitulino539de122011-12-05 14:06:56 -02001666 ret = se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001667 if (ret < 0) {
1668 return ret;
1669 }
aliguoria672b462008-11-11 21:33:36 +00001670 }
1671
Blue Swirl72cf2d42009-09-12 07:36:22 +00001672 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001673 int len;
1674
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001675 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001676 continue;
1677
1678 /* Section type */
1679 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1680 qemu_put_be32(f, se->section_id);
1681
1682 /* ID string */
1683 len = strlen(se->idstr);
1684 qemu_put_byte(f, len);
1685 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1686
1687 qemu_put_be32(f, se->instance_id);
1688 qemu_put_be32(f, se->version_id);
1689
Alex Williamsondc912122011-01-11 14:39:43 -07001690 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001691 }
1692
1693 qemu_put_byte(f, QEMU_VM_EOF);
1694
Juan Quintela624b9cc2011-10-05 01:02:52 +02001695 return qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00001696}
1697
Luiz Capitulino539de122011-12-05 14:06:56 -02001698void qemu_savevm_state_cancel(QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001699{
1700 SaveStateEntry *se;
1701
1702 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1703 if (se->save_live_state) {
Luiz Capitulino539de122011-12-05 14:06:56 -02001704 se->save_live_state(f, -1, se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001705 }
1706 }
1707}
1708
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001709static int qemu_savevm_state(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001710{
aliguoria672b462008-11-11 21:33:36 +00001711 int ret;
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001712 MigrationParams params = {
1713 .blk = 0,
1714 .shared = 0
1715 };
aliguoria672b462008-11-11 21:33:36 +00001716
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001717 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001718 ret = -EINVAL;
1719 goto out;
1720 }
1721
Isaku Yamahata6607ae22012-06-19 18:43:09 +03001722 ret = qemu_savevm_state_begin(f, &params);
aliguoria672b462008-11-11 21:33:36 +00001723 if (ret < 0)
1724 goto out;
1725
1726 do {
Luiz Capitulino539de122011-12-05 14:06:56 -02001727 ret = qemu_savevm_state_iterate(f);
aliguoria672b462008-11-11 21:33:36 +00001728 if (ret < 0)
1729 goto out;
1730 } while (ret == 0);
1731
Luiz Capitulino539de122011-12-05 14:06:56 -02001732 ret = qemu_savevm_state_complete(f);
aliguoria672b462008-11-11 21:33:36 +00001733
1734out:
Juan Quintela39346382011-09-22 11:02:14 +02001735 if (ret == 0) {
Juan Quintela624b9cc2011-10-05 01:02:52 +02001736 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001737 }
aliguoria672b462008-11-11 21:33:36 +00001738
aliguoria672b462008-11-11 21:33:36 +00001739 return ret;
1740}
1741
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00001742static int qemu_save_device_state(QEMUFile *f)
1743{
1744 SaveStateEntry *se;
1745
1746 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1747 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1748
1749 cpu_synchronize_all_states();
1750
1751 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1752 int len;
1753
1754 if (se->is_ram) {
1755 continue;
1756 }
1757 if (se->save_state == NULL && se->vmsd == NULL) {
1758 continue;
1759 }
1760
1761 /* Section type */
1762 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1763 qemu_put_be32(f, se->section_id);
1764
1765 /* ID string */
1766 len = strlen(se->idstr);
1767 qemu_put_byte(f, len);
1768 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1769
1770 qemu_put_be32(f, se->instance_id);
1771 qemu_put_be32(f, se->version_id);
1772
1773 vmstate_save(f, se);
1774 }
1775
1776 qemu_put_byte(f, QEMU_VM_EOF);
1777
1778 return qemu_file_get_error(f);
1779}
1780
aliguoria672b462008-11-11 21:33:36 +00001781static SaveStateEntry *find_se(const char *idstr, int instance_id)
1782{
1783 SaveStateEntry *se;
1784
Blue Swirl72cf2d42009-09-12 07:36:22 +00001785 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001786 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001787 (instance_id == se->instance_id ||
1788 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001789 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001790 /* Migrating from an older version? */
1791 if (strstr(se->idstr, idstr) && se->compat) {
1792 if (!strcmp(se->compat->idstr, idstr) &&
1793 (instance_id == se->compat->instance_id ||
1794 instance_id == se->alias_id))
1795 return se;
1796 }
aliguoria672b462008-11-11 21:33:36 +00001797 }
1798 return NULL;
1799}
1800
Juan Quintela811814b2010-07-26 21:38:43 +02001801static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1802{
1803 while(sub && sub->needed) {
1804 if (strcmp(idstr, sub->vmsd->name) == 0) {
1805 return sub->vmsd;
1806 }
1807 sub++;
1808 }
1809 return NULL;
1810}
1811
1812static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1813 void *opaque)
1814{
Juan Quintelac6380722011-10-04 15:28:31 +02001815 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001816 char idstr[256];
1817 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001818 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001819 const VMStateDescription *sub_vmsd;
1820
Juan Quintelac6380722011-10-04 15:28:31 +02001821 len = qemu_peek_byte(f, 1);
1822 if (len < strlen(vmsd->name) + 1) {
1823 /* subsection name has be be "section_name/a" */
1824 return 0;
1825 }
1826 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1827 if (size != len) {
1828 return 0;
1829 }
1830 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001831
Juan Quintelac6380722011-10-04 15:28:31 +02001832 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1833 /* it don't have a valid subsection name */
1834 return 0;
1835 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001836 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001837 if (sub_vmsd == NULL) {
1838 return -ENOENT;
1839 }
Juan Quintelac6380722011-10-04 15:28:31 +02001840 qemu_file_skip(f, 1); /* subsection */
1841 qemu_file_skip(f, 1); /* len */
1842 qemu_file_skip(f, len); /* idstr */
1843 version_id = qemu_get_be32(f);
1844
Juan Quintela811814b2010-07-26 21:38:43 +02001845 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1846 if (ret) {
1847 return ret;
1848 }
1849 }
1850 return 0;
1851}
1852
1853static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1854 void *opaque)
1855{
1856 const VMStateSubsection *sub = vmsd->subsections;
1857
1858 while (sub && sub->needed) {
1859 if (sub->needed(opaque)) {
1860 const VMStateDescription *vmsd = sub->vmsd;
1861 uint8_t len;
1862
1863 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1864 len = strlen(vmsd->name);
1865 qemu_put_byte(f, len);
1866 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1867 qemu_put_be32(f, vmsd->version_id);
1868 vmstate_save_state(f, vmsd, opaque);
1869 }
1870 sub++;
1871 }
1872}
1873
aliguoria672b462008-11-11 21:33:36 +00001874typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001875 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001876 SaveStateEntry *se;
1877 int section_id;
1878 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001879} LoadStateEntry;
1880
aliguoria672b462008-11-11 21:33:36 +00001881int qemu_loadvm_state(QEMUFile *f)
1882{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001883 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1884 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001885 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001886 uint8_t section_type;
1887 unsigned int v;
1888 int ret;
1889
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02001890 if (qemu_savevm_state_blocked(NULL)) {
Alex Williamsondc912122011-01-11 14:39:43 -07001891 return -EINVAL;
1892 }
1893
aliguoria672b462008-11-11 21:33:36 +00001894 v = qemu_get_be32(f);
1895 if (v != QEMU_VM_FILE_MAGIC)
1896 return -EINVAL;
1897
1898 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001899 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1900 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1901 return -ENOTSUP;
1902 }
aliguoria672b462008-11-11 21:33:36 +00001903 if (v != QEMU_VM_FILE_VERSION)
1904 return -ENOTSUP;
1905
1906 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1907 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001908 SaveStateEntry *se;
1909 char idstr[257];
1910 int len;
1911
1912 switch (section_type) {
1913 case QEMU_VM_SECTION_START:
1914 case QEMU_VM_SECTION_FULL:
1915 /* Read section start */
1916 section_id = qemu_get_be32(f);
1917 len = qemu_get_byte(f);
1918 qemu_get_buffer(f, (uint8_t *)idstr, len);
1919 idstr[len] = 0;
1920 instance_id = qemu_get_be32(f);
1921 version_id = qemu_get_be32(f);
1922
1923 /* Find savevm section */
1924 se = find_se(idstr, instance_id);
1925 if (se == NULL) {
1926 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1927 ret = -EINVAL;
1928 goto out;
1929 }
1930
1931 /* Validate version */
1932 if (version_id > se->version_id) {
1933 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1934 version_id, idstr, se->version_id);
1935 ret = -EINVAL;
1936 goto out;
1937 }
1938
1939 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05001940 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001941
1942 le->se = se;
1943 le->section_id = section_id;
1944 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001945 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001946
Juan Quintela4082be42009-08-20 19:42:24 +02001947 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001948 if (ret < 0) {
1949 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1950 instance_id, idstr);
1951 goto out;
1952 }
aliguoria672b462008-11-11 21:33:36 +00001953 break;
1954 case QEMU_VM_SECTION_PART:
1955 case QEMU_VM_SECTION_END:
1956 section_id = qemu_get_be32(f);
1957
Blue Swirl72cf2d42009-09-12 07:36:22 +00001958 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001959 if (le->section_id == section_id) {
1960 break;
1961 }
1962 }
aliguoria672b462008-11-11 21:33:36 +00001963 if (le == NULL) {
1964 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1965 ret = -EINVAL;
1966 goto out;
1967 }
1968
Juan Quintela4082be42009-08-20 19:42:24 +02001969 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001970 if (ret < 0) {
1971 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1972 section_id);
1973 goto out;
1974 }
aliguoria672b462008-11-11 21:33:36 +00001975 break;
1976 default:
1977 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1978 ret = -EINVAL;
1979 goto out;
1980 }
1981 }
1982
Jan Kiszkaea375f92010-03-01 19:10:30 +01001983 cpu_synchronize_all_post_init();
1984
aliguoria672b462008-11-11 21:33:36 +00001985 ret = 0;
1986
1987out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001988 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1989 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001990 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00001991 }
1992
Juan Quintela42802d42011-10-05 01:14:46 +02001993 if (ret == 0) {
1994 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001995 }
aliguoria672b462008-11-11 21:33:36 +00001996
1997 return ret;
1998}
1999
aliguoria672b462008-11-11 21:33:36 +00002000static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2001 const char *name)
2002{
2003 QEMUSnapshotInfo *sn_tab, *sn;
2004 int nb_sns, i, ret;
2005
2006 ret = -ENOENT;
2007 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2008 if (nb_sns < 0)
2009 return ret;
2010 for(i = 0; i < nb_sns; i++) {
2011 sn = &sn_tab[i];
2012 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2013 *sn_info = *sn;
2014 ret = 0;
2015 break;
2016 }
2017 }
Anthony Liguori7267c092011-08-20 22:09:37 -05002018 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00002019 return ret;
2020}
2021
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002022/*
2023 * Deletes snapshots of a given name in all opened images.
2024 */
2025static int del_existing_snapshots(Monitor *mon, const char *name)
2026{
2027 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002028 QEMUSnapshotInfo sn1, *snapshot = &sn1;
2029 int ret;
2030
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002031 bs = NULL;
2032 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002033 if (bdrv_can_snapshot(bs) &&
2034 bdrv_snapshot_find(bs, snapshot, name) >= 0)
2035 {
2036 ret = bdrv_snapshot_delete(bs, name);
2037 if (ret < 0) {
2038 monitor_printf(mon,
2039 "Error while deleting snapshot on '%s'\n",
2040 bdrv_get_device_name(bs));
2041 return -1;
2042 }
2043 }
2044 }
2045
2046 return 0;
2047}
2048
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002049void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002050{
2051 BlockDriverState *bs, *bs1;
2052 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002053 int ret;
aliguoria672b462008-11-11 21:33:36 +00002054 QEMUFile *f;
2055 int saved_vm_running;
Kevin Wolfc2c9a462011-11-16 11:35:54 +01002056 uint64_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00002057#ifdef _WIN32
2058 struct _timeb tb;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002059 struct tm *ptm;
aliguoria672b462008-11-11 21:33:36 +00002060#else
2061 struct timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002062 struct tm tm;
aliguoria672b462008-11-11 21:33:36 +00002063#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002064 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002065
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002066 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002067 bs = NULL;
2068 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002069
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002070 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002071 continue;
2072 }
2073
2074 if (!bdrv_can_snapshot(bs)) {
2075 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2076 bdrv_get_device_name(bs));
2077 return;
2078 }
2079 }
2080
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002081 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002082 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002083 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002084 return;
2085 }
aliguoria672b462008-11-11 21:33:36 +00002086
Luiz Capitulino13548692011-07-29 15:36:43 -03002087 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002088 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002089
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002090 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002091
2092 /* fill auxiliary fields */
2093#ifdef _WIN32
2094 _ftime(&tb);
2095 sn->date_sec = tb.time;
2096 sn->date_nsec = tb.millitm * 1000000;
2097#else
2098 gettimeofday(&tv, NULL);
2099 sn->date_sec = tv.tv_sec;
2100 sn->date_nsec = tv.tv_usec * 1000;
2101#endif
Paolo Bonzini74475452011-03-11 16:47:48 +01002102 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002103
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002104 if (name) {
2105 ret = bdrv_snapshot_find(bs, old_sn, name);
2106 if (ret >= 0) {
2107 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2108 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2109 } else {
2110 pstrcpy(sn->name, sizeof(sn->name), name);
2111 }
2112 } else {
2113#ifdef _WIN32
Stefan Weil55dd9ff2012-04-12 22:33:12 +02002114 time_t t = tb.time;
2115 ptm = localtime(&t);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002116 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2117#else
Blue Swirld7d9b522010-09-09 19:13:04 +00002118 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2119 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002120 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2121#endif
2122 }
2123
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002124 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002125 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002126 goto the_end;
2127 }
2128
aliguoria672b462008-11-11 21:33:36 +00002129 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002130 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002131 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002132 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002133 goto the_end;
2134 }
Luiz Capitulinoe1c37d02011-12-05 14:48:01 -02002135 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00002136 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002137 qemu_fclose(f);
2138 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002139 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002140 goto the_end;
2141 }
2142
2143 /* create the snapshots */
2144
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002145 bs1 = NULL;
2146 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002147 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002148 /* Write VM state size only to the image that contains the state */
2149 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002150 ret = bdrv_snapshot_create(bs1, sn);
2151 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002152 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2153 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002154 }
2155 }
2156 }
2157
2158 the_end:
2159 if (saved_vm_running)
2160 vm_start();
2161}
2162
Stefano Stabellinia7ae8352012-01-25 12:24:51 +00002163void qmp_xen_save_devices_state(const char *filename, Error **errp)
2164{
2165 QEMUFile *f;
2166 int saved_vm_running;
2167 int ret;
2168
2169 saved_vm_running = runstate_is_running();
2170 vm_stop(RUN_STATE_SAVE_VM);
2171
2172 f = qemu_fopen(filename, "wb");
2173 if (!f) {
2174 error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2175 goto the_end;
2176 }
2177 ret = qemu_save_device_state(f);
2178 qemu_fclose(f);
2179 if (ret < 0) {
2180 error_set(errp, QERR_IO_ERROR);
2181 }
2182
2183 the_end:
2184 if (saved_vm_running)
2185 vm_start();
2186 return;
2187}
2188
Markus Armbruster03cd4652010-02-17 16:24:10 +01002189int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002190{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002191 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002192 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002193 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002194 int ret;
aliguoria672b462008-11-11 21:33:36 +00002195
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002196 bs_vm_state = bdrv_snapshots();
2197 if (!bs_vm_state) {
2198 error_report("No block device supports snapshots");
2199 return -ENOTSUP;
2200 }
2201
2202 /* Don't even try to load empty VM states */
2203 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2204 if (ret < 0) {
2205 return ret;
2206 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002207 error_report("This is a disk-only snapshot. Revert to it offline "
2208 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002209 return -EINVAL;
2210 }
2211
2212 /* Verify if there is any device that doesn't support snapshots and is
2213 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002214 bs = NULL;
2215 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002216
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002217 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002218 continue;
2219 }
2220
2221 if (!bdrv_can_snapshot(bs)) {
2222 error_report("Device '%s' is writable but does not support snapshots.",
2223 bdrv_get_device_name(bs));
2224 return -ENOTSUP;
2225 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002226
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002227 ret = bdrv_snapshot_find(bs, &sn, name);
2228 if (ret < 0) {
2229 error_report("Device '%s' does not have the requested snapshot '%s'",
2230 bdrv_get_device_name(bs), name);
2231 return ret;
2232 }
aliguoria672b462008-11-11 21:33:36 +00002233 }
2234
2235 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002236 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002237
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002238 bs = NULL;
2239 while ((bs = bdrv_next(bs))) {
2240 if (bdrv_can_snapshot(bs)) {
2241 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002242 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002243 error_report("Error %d while activating snapshot '%s' on '%s'",
2244 ret, name, bdrv_get_device_name(bs));
2245 return ret;
aliguoria672b462008-11-11 21:33:36 +00002246 }
2247 }
2248 }
2249
aliguoria672b462008-11-11 21:33:36 +00002250 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002251 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002252 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002253 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002254 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002255 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002256
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002257 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002258 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002259
aliguoria672b462008-11-11 21:33:36 +00002260 qemu_fclose(f);
2261 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002262 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002263 return ret;
aliguoria672b462008-11-11 21:33:36 +00002264 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002265
Juan Quintela05f24012009-08-20 19:42:22 +02002266 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002267}
2268
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002269void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002270{
2271 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002272 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002273 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002274
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002275 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002276 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002277 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002278 return;
2279 }
2280
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002281 bs1 = NULL;
2282 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002283 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002284 ret = bdrv_snapshot_delete(bs1, name);
2285 if (ret < 0) {
2286 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002287 monitor_printf(mon,
2288 "Snapshots not supported on device '%s'\n",
2289 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002290 else
aliguori376253e2009-03-05 23:01:23 +00002291 monitor_printf(mon, "Error %d while deleting snapshot on "
2292 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002293 }
2294 }
2295 }
2296}
2297
aliguori376253e2009-03-05 23:01:23 +00002298void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002299{
2300 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002301 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2302 int nb_sns, i, ret, available;
2303 int total;
2304 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002305 char buf[256];
2306
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002307 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002308 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002309 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002310 return;
2311 }
aliguoria672b462008-11-11 21:33:36 +00002312
2313 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2314 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002315 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002316 return;
2317 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002318
2319 if (nb_sns == 0) {
2320 monitor_printf(mon, "There is no snapshot available.\n");
2321 return;
aliguoria672b462008-11-11 21:33:36 +00002322 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002323
Anthony Liguori7267c092011-08-20 22:09:37 -05002324 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002325 total = 0;
2326 for (i = 0; i < nb_sns; i++) {
2327 sn = &sn_tab[i];
2328 available = 1;
2329 bs1 = NULL;
2330
2331 while ((bs1 = bdrv_next(bs1))) {
2332 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2333 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2334 if (ret < 0) {
2335 available = 0;
2336 break;
2337 }
2338 }
2339 }
2340
2341 if (available) {
2342 available_snapshots[total] = i;
2343 total++;
2344 }
2345 }
2346
2347 if (total > 0) {
2348 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2349 for (i = 0; i < total; i++) {
2350 sn = &sn_tab[available_snapshots[i]];
2351 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2352 }
2353 } else {
2354 monitor_printf(mon, "There is no suitable snapshot available\n");
2355 }
2356
Anthony Liguori7267c092011-08-20 22:09:37 -05002357 g_free(sn_tab);
2358 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002359
aliguoria672b462008-11-11 21:33:36 +00002360}
Avi Kivityc5705a72011-12-20 15:59:12 +02002361
2362void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2363{
Avi Kivity1ddde082012-01-08 13:18:19 +02002364 qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
Avi Kivityc5705a72011-12-20 15:59:12 +02002365 memory_region_name(mr), dev);
2366}
2367
2368void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2369{
2370 /* Nothing do to while the implementation is in RAMBlock */
2371}
2372
2373void vmstate_register_ram_global(MemoryRegion *mr)
2374{
2375 vmstate_register_ram(mr, NULL);
2376}