blob: adff42140c31936500113875e32bf82bd4368a36 [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"
blueswir1511d2b12009-03-07 15:32:56 +000086
aliguoria672b462008-11-11 21:33:36 +000087#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000088
Nolan18995b92009-10-15 16:53:55 -070089#ifndef ETH_P_RARP
Stefan Bergerf8778a72010-04-24 08:54:07 -040090#define ETH_P_RARP 0x8035
Nolan18995b92009-10-15 16:53:55 -070091#endif
92#define ARP_HTYPE_ETH 0x0001
93#define ARP_PTYPE_IP 0x0800
94#define ARP_OP_REQUEST_REV 0x3
95
96static int announce_self_create(uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +000097 uint8_t *mac_addr)
98{
Nolan18995b92009-10-15 16:53:55 -070099 /* Ethernet header. */
100 memset(buf, 0xff, 6); /* destination MAC addr */
101 memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
102 *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
aliguoria672b462008-11-11 21:33:36 +0000103
Nolan18995b92009-10-15 16:53:55 -0700104 /* RARP header. */
105 *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
106 *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
107 *(buf + 18) = 6; /* hardware addr length (ethernet) */
108 *(buf + 19) = 4; /* protocol addr length (IPv4) */
109 *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
110 memcpy(buf + 22, mac_addr, 6); /* source hw addr */
111 memset(buf + 28, 0x00, 4); /* source protocol addr */
112 memcpy(buf + 32, mac_addr, 6); /* target hw addr */
113 memset(buf + 38, 0x00, 4); /* target protocol addr */
aliguoria672b462008-11-11 21:33:36 +0000114
Nolan18995b92009-10-15 16:53:55 -0700115 /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
116 memset(buf + 42, 0x00, 18);
aliguoria672b462008-11-11 21:33:36 +0000117
Nolan18995b92009-10-15 16:53:55 -0700118 return 60; /* len (FCS will be added by hardware) */
aliguoria672b462008-11-11 21:33:36 +0000119}
120
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000121static void qemu_announce_self_iter(NICState *nic, void *opaque)
122{
123 uint8_t buf[60];
124 int len;
125
126 len = announce_self_create(buf, nic->conf->macaddr.a);
127
128 qemu_send_packet_raw(&nic->nc, buf, len);
129}
130
131
Gleb Natapoved8b3302009-05-21 17:17:44 +0300132static void qemu_announce_self_once(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000133{
Gleb Natapoved8b3302009-05-21 17:17:44 +0300134 static int count = SELF_ANNOUNCE_ROUNDS;
135 QEMUTimer *timer = *(QEMUTimer **)opaque;
aliguoria672b462008-11-11 21:33:36 +0000136
Mark McLoughlinf401ca22009-11-25 18:49:32 +0000137 qemu_foreach_nic(qemu_announce_self_iter, NULL);
138
Nolan18995b92009-10-15 16:53:55 -0700139 if (--count) {
140 /* delay 50ms, 150ms, 250ms, ... */
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100141 qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
Nolan18995b92009-10-15 16:53:55 -0700142 50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300143 } else {
144 qemu_del_timer(timer);
145 qemu_free_timer(timer);
146 }
147}
148
149void qemu_announce_self(void)
150{
151 static QEMUTimer *timer;
Paolo Bonzini7bd427d2011-03-11 16:47:48 +0100152 timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
Gleb Natapoved8b3302009-05-21 17:17:44 +0300153 qemu_announce_self_once(&timer);
aliguoria672b462008-11-11 21:33:36 +0000154}
155
156/***********************************************************/
157/* savevm/loadvm support */
158
159#define IO_BUF_SIZE 32768
160
161struct QEMUFile {
162 QEMUFilePutBufferFunc *put_buffer;
163 QEMUFileGetBufferFunc *get_buffer;
164 QEMUFileCloseFunc *close;
165 QEMUFileRateLimit *rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400166 QEMUFileSetRateLimit *set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200167 QEMUFileGetRateLimit *get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000168 void *opaque;
169 int is_write;
170
171 int64_t buf_offset; /* start of buffer when writing, end of buffer
172 when reading */
173 int buf_index;
174 int buf_size; /* 0 when writing */
175 uint8_t buf[IO_BUF_SIZE];
176
Juan Quintela3961b4d2011-10-05 01:05:21 +0200177 int last_error;
aliguoria672b462008-11-11 21:33:36 +0000178};
179
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200180typedef struct QEMUFileStdio
aliguoria672b462008-11-11 21:33:36 +0000181{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200182 FILE *stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000183 QEMUFile *file;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200184} QEMUFileStdio;
aliguoria672b462008-11-11 21:33:36 +0000185
186typedef struct QEMUFileSocket
187{
188 int fd;
189 QEMUFile *file;
190} QEMUFileSocket;
191
192static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
193{
194 QEMUFileSocket *s = opaque;
195 ssize_t len;
196
197 do {
Blue Swirl00aa0042011-07-23 20:04:29 +0000198 len = qemu_recv(s->fd, buf, size, 0);
aliguoria672b462008-11-11 21:33:36 +0000199 } while (len == -1 && socket_error() == EINTR);
200
201 if (len == -1)
202 len = -socket_error();
203
204 return len;
205}
206
207static int socket_close(void *opaque)
208{
209 QEMUFileSocket *s = opaque;
Anthony Liguori7267c092011-08-20 22:09:37 -0500210 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000211 return 0;
212}
213
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200214static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000215{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200216 QEMUFileStdio *s = opaque;
217 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000218}
219
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200220static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000221{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200222 QEMUFileStdio *s = opaque;
223 FILE *fp = s->stdio_file;
Uri Lublin8a67ec42009-06-08 19:27:21 +0300224 int bytes;
225
226 do {
227 clearerr(fp);
228 bytes = fread(buf, 1, size, fp);
229 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
230 return bytes;
aliguoria672b462008-11-11 21:33:36 +0000231}
232
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200233static int stdio_pclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000234{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200235 QEMUFileStdio *s = opaque;
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500236 int ret;
237 ret = pclose(s->stdio_file);
Anthony Liguori7267c092011-08-20 22:09:37 -0500238 g_free(s);
Anthony Liguori41ef56e2010-06-02 14:55:25 -0500239 return ret;
aliguoria672b462008-11-11 21:33:36 +0000240}
241
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200242static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000243{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200244 QEMUFileStdio *s = opaque;
245 fclose(s->stdio_file);
Anthony Liguori7267c092011-08-20 22:09:37 -0500246 g_free(s);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200247 return 0;
248}
aliguoria672b462008-11-11 21:33:36 +0000249
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200250QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
251{
252 QEMUFileStdio *s;
253
254 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000255 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
256 return NULL;
257 }
258
Anthony Liguori7267c092011-08-20 22:09:37 -0500259 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000260
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200261 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000262
263 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200264 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
265 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000266 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200267 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
268 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000269 }
aliguoria672b462008-11-11 21:33:36 +0000270 return s->file;
271}
272
273QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
274{
275 FILE *popen_file;
276
277 popen_file = popen(command, mode);
278 if(popen_file == NULL) {
279 return NULL;
280 }
281
282 return qemu_popen(popen_file, mode);
283}
284
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200285int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200286{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200287 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200288 int fd;
289
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200290 p = (QEMUFileStdio *)f->opaque;
291 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200292
293 return fd;
294}
295
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200296QEMUFile *qemu_fdopen(int fd, const char *mode)
297{
298 QEMUFileStdio *s;
299
300 if (mode == NULL ||
301 (mode[0] != 'r' && mode[0] != 'w') ||
302 mode[1] != 'b' || mode[2] != 0) {
303 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
304 return NULL;
305 }
306
Anthony Liguori7267c092011-08-20 22:09:37 -0500307 s = g_malloc0(sizeof(QEMUFileStdio));
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200308 s->stdio_file = fdopen(fd, mode);
309 if (!s->stdio_file)
310 goto fail;
311
312 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200313 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
314 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200315 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200316 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
317 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200318 }
319 return s->file;
320
321fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500322 g_free(s);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200323 return NULL;
324}
325
aliguoria672b462008-11-11 21:33:36 +0000326QEMUFile *qemu_fopen_socket(int fd)
327{
Anthony Liguori7267c092011-08-20 22:09:37 -0500328 QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
aliguoria672b462008-11-11 21:33:36 +0000329
aliguoria672b462008-11-11 21:33:36 +0000330 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200331 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
332 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000333 return s->file;
334}
335
aliguoria672b462008-11-11 21:33:36 +0000336static int file_put_buffer(void *opaque, const uint8_t *buf,
337 int64_t pos, int size)
338{
339 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200340 fseek(s->stdio_file, pos, SEEK_SET);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000341 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000342}
343
344static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
345{
346 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200347 fseek(s->stdio_file, pos, SEEK_SET);
348 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000349}
350
351QEMUFile *qemu_fopen(const char *filename, const char *mode)
352{
353 QEMUFileStdio *s;
354
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200355 if (mode == NULL ||
356 (mode[0] != 'r' && mode[0] != 'w') ||
357 mode[1] != 'b' || mode[2] != 0) {
Blue Swirl090414a2010-03-13 11:36:09 +0000358 fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200359 return NULL;
360 }
361
Anthony Liguori7267c092011-08-20 22:09:37 -0500362 s = g_malloc0(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000363
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200364 s->stdio_file = fopen(filename, mode);
365 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000366 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200367
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200368 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200369 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
370 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200371 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200372 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
373 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200374 }
375 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000376fail:
Anthony Liguori7267c092011-08-20 22:09:37 -0500377 g_free(s);
aliguoria672b462008-11-11 21:33:36 +0000378 return NULL;
379}
380
aliguori178e08a2009-04-05 19:10:55 +0000381static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000382 int64_t pos, int size)
383{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200384 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000385 return size;
386}
387
aliguori178e08a2009-04-05 19:10:55 +0000388static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000389{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200390 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000391}
392
393static int bdrv_fclose(void *opaque)
394{
aliguoria672b462008-11-11 21:33:36 +0000395 return 0;
396}
397
Christoph Hellwig45566e92009-07-10 23:11:57 +0200398static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000399{
aliguoria672b462008-11-11 21:33:36 +0000400 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200401 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
402 NULL, NULL, NULL);
403 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000404}
405
406QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
407 QEMUFileGetBufferFunc *get_buffer,
408 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400409 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200410 QEMUFileSetRateLimit *set_rate_limit,
411 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000412{
413 QEMUFile *f;
414
Anthony Liguori7267c092011-08-20 22:09:37 -0500415 f = g_malloc0(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000416
417 f->opaque = opaque;
418 f->put_buffer = put_buffer;
419 f->get_buffer = get_buffer;
420 f->close = close;
421 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400422 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200423 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000424 f->is_write = 0;
425
426 return f;
427}
428
Juan Quintela624b9cc2011-10-05 01:02:52 +0200429int qemu_file_get_error(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000430{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200431 return f->last_error;
aliguoria672b462008-11-11 21:33:36 +0000432}
433
Juan Quinteladcd1d222011-09-21 23:01:54 +0200434void qemu_file_set_error(QEMUFile *f, int ret)
aliguori4dabe242009-04-05 19:30:51 +0000435{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200436 f->last_error = ret;
aliguori4dabe242009-04-05 19:30:51 +0000437}
438
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200439/** Sets last_error conditionally
440 *
441 * Sets last_error only if ret is negative _and_ no error
442 * was set before.
443 */
444static void qemu_file_set_if_error(QEMUFile *f, int ret)
445{
446 if (ret < 0 && !f->last_error) {
447 qemu_file_set_error(f, ret);
448 }
449}
450
451/** Flushes QEMUFile buffer
452 *
453 * In case of error, last_error is set.
454 */
aliguoria672b462008-11-11 21:33:36 +0000455void qemu_fflush(QEMUFile *f)
456{
457 if (!f->put_buffer)
458 return;
459
460 if (f->is_write && f->buf_index > 0) {
461 int len;
462
463 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
464 if (len > 0)
465 f->buf_offset += f->buf_index;
466 else
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200467 qemu_file_set_error(f, -EINVAL);
aliguoria672b462008-11-11 21:33:36 +0000468 f->buf_index = 0;
469 }
470}
471
472static void qemu_fill_buffer(QEMUFile *f)
473{
474 int len;
Juan Quintela0046c452011-09-30 19:28:45 +0200475 int pending;
aliguoria672b462008-11-11 21:33:36 +0000476
477 if (!f->get_buffer)
478 return;
479
480 if (f->is_write)
481 abort();
482
Juan Quintela0046c452011-09-30 19:28:45 +0200483 pending = f->buf_size - f->buf_index;
484 if (pending > 0) {
485 memmove(f->buf, f->buf + f->buf_index, pending);
486 }
487 f->buf_index = 0;
488 f->buf_size = pending;
489
490 len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
491 IO_BUF_SIZE - pending);
aliguoria672b462008-11-11 21:33:36 +0000492 if (len > 0) {
Juan Quintela0046c452011-09-30 19:28:45 +0200493 f->buf_size += len;
aliguoria672b462008-11-11 21:33:36 +0000494 f->buf_offset += len;
Juan Quintelafa39a302011-10-25 19:18:58 +0200495 } else if (len == 0) {
496 f->last_error = -EIO;
aliguoria672b462008-11-11 21:33:36 +0000497 } else if (len != -EAGAIN)
Eduardo Habkostc29110d2011-11-10 10:41:39 -0200498 qemu_file_set_error(f, len);
aliguoria672b462008-11-11 21:33:36 +0000499}
500
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200501/** Calls close function and set last_error if needed
502 *
503 * Internal function. qemu_fflush() must be called before this.
504 *
505 * Returns f->close() return value, or 0 if close function is not set.
506 */
507static int qemu_close(QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +0000508{
509 int ret = 0;
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200510 if (f->close) {
aliguoria672b462008-11-11 21:33:36 +0000511 ret = f->close(f->opaque);
Eduardo Habkostd82ca912011-11-10 10:41:43 -0200512 qemu_file_set_if_error(f, ret);
513 }
514 return ret;
515}
516
517/** Closes the file
518 *
519 * Returns negative error value if any error happened on previous operations or
520 * while closing the file. Returns 0 or positive number on success.
521 *
522 * The meaning of return value on success depends on the specific backend
523 * being used.
524 */
525int qemu_fclose(QEMUFile *f)
526{
527 int ret;
528 qemu_fflush(f);
529 ret = qemu_close(f);
530 /* If any error was spotted before closing, we should report it
531 * instead of the close() return value.
532 */
533 if (f->last_error) {
534 ret = f->last_error;
535 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500536 g_free(f);
aliguoria672b462008-11-11 21:33:36 +0000537 return ret;
538}
539
540void qemu_file_put_notify(QEMUFile *f)
541{
542 f->put_buffer(f->opaque, NULL, 0, 0);
543}
544
545void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
546{
547 int l;
548
Juan Quintela3961b4d2011-10-05 01:05:21 +0200549 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000550 fprintf(stderr,
551 "Attempted to write to buffer while read buffer is not empty\n");
552 abort();
553 }
554
Juan Quintela3961b4d2011-10-05 01:05:21 +0200555 while (!f->last_error && size > 0) {
aliguoria672b462008-11-11 21:33:36 +0000556 l = IO_BUF_SIZE - f->buf_index;
557 if (l > size)
558 l = size;
559 memcpy(f->buf + f->buf_index, buf, l);
560 f->is_write = 1;
561 f->buf_index += l;
562 buf += l;
563 size -= l;
564 if (f->buf_index >= IO_BUF_SIZE)
565 qemu_fflush(f);
566 }
567}
568
569void qemu_put_byte(QEMUFile *f, int v)
570{
Juan Quintela3961b4d2011-10-05 01:05:21 +0200571 if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
aliguoria672b462008-11-11 21:33:36 +0000572 fprintf(stderr,
573 "Attempted to write to buffer while read buffer is not empty\n");
574 abort();
575 }
576
577 f->buf[f->buf_index++] = v;
578 f->is_write = 1;
579 if (f->buf_index >= IO_BUF_SIZE)
580 qemu_fflush(f);
581}
582
Juan Quintelac6380722011-10-04 15:28:31 +0200583static void qemu_file_skip(QEMUFile *f, int size)
aliguoria672b462008-11-11 21:33:36 +0000584{
Juan Quintelac6380722011-10-04 15:28:31 +0200585 if (f->buf_index + size <= f->buf_size) {
586 f->buf_index += size;
aliguoria672b462008-11-11 21:33:36 +0000587 }
aliguoria672b462008-11-11 21:33:36 +0000588}
589
Juan Quintelac6380722011-10-04 15:28:31 +0200590static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
Juan Quintela811814b2010-07-26 21:38:43 +0200591{
Juan Quintelac6380722011-10-04 15:28:31 +0200592 int pending;
593 int index;
Juan Quintela811814b2010-07-26 21:38:43 +0200594
Juan Quintelab9ce1452011-10-04 13:55:32 +0200595 if (f->is_write) {
Juan Quintela811814b2010-07-26 21:38:43 +0200596 abort();
Juan Quintela811814b2010-07-26 21:38:43 +0200597 }
Juan Quintela811814b2010-07-26 21:38:43 +0200598
Juan Quintelac6380722011-10-04 15:28:31 +0200599 index = f->buf_index + offset;
600 pending = f->buf_size - index;
601 if (pending < size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200602 qemu_fill_buffer(f);
Juan Quintelac6380722011-10-04 15:28:31 +0200603 index = f->buf_index + offset;
604 pending = f->buf_size - index;
605 }
606
607 if (pending <= 0) {
608 return 0;
609 }
610 if (size > pending) {
611 size = pending;
612 }
613
614 memcpy(buf, f->buf + index, size);
615 return size;
616}
617
618int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
619{
620 int pending = size;
621 int done = 0;
622
623 while (pending > 0) {
624 int res;
625
626 res = qemu_peek_buffer(f, buf, pending, 0);
627 if (res == 0) {
628 return done;
629 }
630 qemu_file_skip(f, res);
631 buf += res;
632 pending -= res;
633 done += res;
634 }
635 return done;
636}
637
638static int qemu_peek_byte(QEMUFile *f, int offset)
639{
640 int index = f->buf_index + offset;
641
642 if (f->is_write) {
643 abort();
644 }
645
646 if (index >= f->buf_size) {
647 qemu_fill_buffer(f);
648 index = f->buf_index + offset;
649 if (index >= f->buf_size) {
Juan Quintela811814b2010-07-26 21:38:43 +0200650 return 0;
Juan Quintelab9ce1452011-10-04 13:55:32 +0200651 }
Juan Quintela811814b2010-07-26 21:38:43 +0200652 }
Juan Quintelac6380722011-10-04 15:28:31 +0200653 return f->buf[index];
Juan Quintela811814b2010-07-26 21:38:43 +0200654}
655
aliguoria672b462008-11-11 21:33:36 +0000656int qemu_get_byte(QEMUFile *f)
657{
Juan Quintela65f3bb32011-10-06 14:29:32 +0200658 int result;
aliguoria672b462008-11-11 21:33:36 +0000659
Juan Quintelac6380722011-10-04 15:28:31 +0200660 result = qemu_peek_byte(f, 0);
661 qemu_file_skip(f, 1);
Juan Quintela65f3bb32011-10-06 14:29:32 +0200662 return result;
aliguoria672b462008-11-11 21:33:36 +0000663}
664
665int64_t qemu_ftell(QEMUFile *f)
666{
667 return f->buf_offset - f->buf_size + f->buf_index;
668}
669
670int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
671{
672 if (whence == SEEK_SET) {
673 /* nothing to do */
674 } else if (whence == SEEK_CUR) {
675 pos += qemu_ftell(f);
676 } else {
677 /* SEEK_END not supported */
678 return -1;
679 }
680 if (f->put_buffer) {
681 qemu_fflush(f);
682 f->buf_offset = pos;
683 } else {
684 f->buf_offset = pos;
685 f->buf_index = 0;
686 f->buf_size = 0;
687 }
688 return pos;
689}
690
691int qemu_file_rate_limit(QEMUFile *f)
692{
693 if (f->rate_limit)
694 return f->rate_limit(f->opaque);
695
696 return 0;
697}
698
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200699int64_t qemu_file_get_rate_limit(QEMUFile *f)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200700{
701 if (f->get_rate_limit)
702 return f->get_rate_limit(f->opaque);
703
704 return 0;
705}
706
Michael S. Tsirkin3d002df2010-11-23 19:05:54 +0200707int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
Glauber Costa19629532009-05-20 18:26:57 -0400708{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400709 /* any failed or completed migration keeps its state to allow probing of
710 * migration data, but has no associated file anymore */
711 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400712 return f->set_rate_limit(f->opaque, new_rate);
713
714 return 0;
715}
716
aliguoria672b462008-11-11 21:33:36 +0000717void qemu_put_be16(QEMUFile *f, unsigned int v)
718{
719 qemu_put_byte(f, v >> 8);
720 qemu_put_byte(f, v);
721}
722
723void qemu_put_be32(QEMUFile *f, unsigned int v)
724{
725 qemu_put_byte(f, v >> 24);
726 qemu_put_byte(f, v >> 16);
727 qemu_put_byte(f, v >> 8);
728 qemu_put_byte(f, v);
729}
730
731void qemu_put_be64(QEMUFile *f, uint64_t v)
732{
733 qemu_put_be32(f, v >> 32);
734 qemu_put_be32(f, v);
735}
736
737unsigned int qemu_get_be16(QEMUFile *f)
738{
739 unsigned int v;
740 v = qemu_get_byte(f) << 8;
741 v |= qemu_get_byte(f);
742 return v;
743}
744
745unsigned int qemu_get_be32(QEMUFile *f)
746{
747 unsigned int v;
748 v = qemu_get_byte(f) << 24;
749 v |= qemu_get_byte(f) << 16;
750 v |= qemu_get_byte(f) << 8;
751 v |= qemu_get_byte(f);
752 return v;
753}
754
755uint64_t qemu_get_be64(QEMUFile *f)
756{
757 uint64_t v;
758 v = (uint64_t)qemu_get_be32(f) << 32;
759 v |= qemu_get_be32(f);
760 return v;
761}
762
Paolo Bonzini2ff68d02011-09-12 16:21:44 +0200763
764/* timer */
765
766void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
767{
768 uint64_t expire_time;
769
770 expire_time = qemu_timer_expire_time_ns(ts);
771 qemu_put_be64(f, expire_time);
772}
773
774void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
775{
776 uint64_t expire_time;
777
778 expire_time = qemu_get_be64(f);
779 if (expire_time != -1) {
780 qemu_mod_timer_ns(ts, expire_time);
781 } else {
782 qemu_del_timer(ts);
783 }
784}
785
786
Gerd Hoffmanncdae5cf2010-11-01 15:51:54 +0100787/* bool */
788
789static int get_bool(QEMUFile *f, void *pv, size_t size)
790{
791 bool *v = pv;
792 *v = qemu_get_byte(f);
793 return 0;
794}
795
796static void put_bool(QEMUFile *f, void *pv, size_t size)
797{
798 bool *v = pv;
799 qemu_put_byte(f, *v);
800}
801
802const VMStateInfo vmstate_info_bool = {
803 .name = "bool",
804 .get = get_bool,
805 .put = put_bool,
806};
807
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200808/* 8 bit int */
809
810static int get_int8(QEMUFile *f, void *pv, size_t size)
811{
812 int8_t *v = pv;
813 qemu_get_s8s(f, v);
814 return 0;
815}
816
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200817static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200818{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200819 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200820 qemu_put_s8s(f, v);
821}
822
823const VMStateInfo vmstate_info_int8 = {
824 .name = "int8",
825 .get = get_int8,
826 .put = put_int8,
827};
828
829/* 16 bit int */
830
831static int get_int16(QEMUFile *f, void *pv, size_t size)
832{
833 int16_t *v = pv;
834 qemu_get_sbe16s(f, v);
835 return 0;
836}
837
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200838static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200839{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200840 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200841 qemu_put_sbe16s(f, v);
842}
843
844const VMStateInfo vmstate_info_int16 = {
845 .name = "int16",
846 .get = get_int16,
847 .put = put_int16,
848};
849
850/* 32 bit int */
851
852static int get_int32(QEMUFile *f, void *pv, size_t size)
853{
854 int32_t *v = pv;
855 qemu_get_sbe32s(f, v);
856 return 0;
857}
858
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200859static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200860{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200861 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200862 qemu_put_sbe32s(f, v);
863}
864
865const VMStateInfo vmstate_info_int32 = {
866 .name = "int32",
867 .get = get_int32,
868 .put = put_int32,
869};
870
Juan Quintela82501662009-08-20 19:42:32 +0200871/* 32 bit int. See that the received value is the same than the one
872 in the field */
873
874static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
875{
876 int32_t *v = pv;
877 int32_t v2;
878 qemu_get_sbe32s(f, &v2);
879
880 if (*v == v2)
881 return 0;
882 return -EINVAL;
883}
884
885const VMStateInfo vmstate_info_int32_equal = {
886 .name = "int32 equal",
887 .get = get_int32_equal,
888 .put = put_int32,
889};
890
Juan Quintela0a031e02009-08-20 19:42:37 +0200891/* 32 bit int. See that the received value is the less or the same
892 than the one in the field */
893
894static int get_int32_le(QEMUFile *f, void *pv, size_t size)
895{
896 int32_t *old = pv;
897 int32_t new;
898 qemu_get_sbe32s(f, &new);
899
900 if (*old <= new)
901 return 0;
902 return -EINVAL;
903}
904
905const VMStateInfo vmstate_info_int32_le = {
906 .name = "int32 equal",
907 .get = get_int32_le,
908 .put = put_int32,
909};
910
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200911/* 64 bit int */
912
913static int get_int64(QEMUFile *f, void *pv, size_t size)
914{
915 int64_t *v = pv;
916 qemu_get_sbe64s(f, v);
917 return 0;
918}
919
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200920static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200921{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200922 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200923 qemu_put_sbe64s(f, v);
924}
925
926const VMStateInfo vmstate_info_int64 = {
927 .name = "int64",
928 .get = get_int64,
929 .put = put_int64,
930};
931
932/* 8 bit unsigned int */
933
934static int get_uint8(QEMUFile *f, void *pv, size_t size)
935{
936 uint8_t *v = pv;
937 qemu_get_8s(f, v);
938 return 0;
939}
940
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200941static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200942{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200943 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200944 qemu_put_8s(f, v);
945}
946
947const VMStateInfo vmstate_info_uint8 = {
948 .name = "uint8",
949 .get = get_uint8,
950 .put = put_uint8,
951};
952
953/* 16 bit unsigned int */
954
955static int get_uint16(QEMUFile *f, void *pv, size_t size)
956{
957 uint16_t *v = pv;
958 qemu_get_be16s(f, v);
959 return 0;
960}
961
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200962static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200963{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200964 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200965 qemu_put_be16s(f, v);
966}
967
968const VMStateInfo vmstate_info_uint16 = {
969 .name = "uint16",
970 .get = get_uint16,
971 .put = put_uint16,
972};
973
974/* 32 bit unsigned int */
975
976static int get_uint32(QEMUFile *f, void *pv, size_t size)
977{
978 uint32_t *v = pv;
979 qemu_get_be32s(f, v);
980 return 0;
981}
982
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200983static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200984{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200985 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200986 qemu_put_be32s(f, v);
987}
988
989const VMStateInfo vmstate_info_uint32 = {
990 .name = "uint32",
991 .get = get_uint32,
992 .put = put_uint32,
993};
994
Juan Quintela9122a8f2011-03-10 12:33:48 +0100995/* 32 bit uint. See that the received value is the same than the one
996 in the field */
997
998static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
999{
1000 uint32_t *v = pv;
1001 uint32_t v2;
1002 qemu_get_be32s(f, &v2);
1003
1004 if (*v == v2) {
1005 return 0;
1006 }
1007 return -EINVAL;
1008}
1009
1010const VMStateInfo vmstate_info_uint32_equal = {
1011 .name = "uint32 equal",
1012 .get = get_uint32_equal,
1013 .put = put_uint32,
1014};
1015
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001016/* 64 bit unsigned int */
1017
1018static int get_uint64(QEMUFile *f, void *pv, size_t size)
1019{
1020 uint64_t *v = pv;
1021 qemu_get_be64s(f, v);
1022 return 0;
1023}
1024
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001025static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001026{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001027 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001028 qemu_put_be64s(f, v);
1029}
1030
1031const VMStateInfo vmstate_info_uint64 = {
1032 .name = "uint64",
1033 .get = get_uint64,
1034 .put = put_uint64,
1035};
1036
Juan Quintela80cd83e2009-09-10 03:04:36 +02001037/* 8 bit int. See that the received value is the same than the one
1038 in the field */
1039
1040static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1041{
1042 uint8_t *v = pv;
1043 uint8_t v2;
1044 qemu_get_8s(f, &v2);
1045
1046 if (*v == v2)
1047 return 0;
1048 return -EINVAL;
1049}
1050
1051const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +02001052 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +02001053 .get = get_uint8_equal,
1054 .put = put_uint8,
1055};
1056
Juan Quinteladc3b83a2009-10-15 23:16:13 +02001057/* 16 bit unsigned int int. See that the received value is the same than the one
1058 in the field */
1059
1060static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1061{
1062 uint16_t *v = pv;
1063 uint16_t v2;
1064 qemu_get_be16s(f, &v2);
1065
1066 if (*v == v2)
1067 return 0;
1068 return -EINVAL;
1069}
1070
1071const VMStateInfo vmstate_info_uint16_equal = {
1072 .name = "uint16 equal",
1073 .get = get_uint16_equal,
1074 .put = put_uint16,
1075};
1076
Juan Quinteladde04632009-08-20 19:42:26 +02001077/* timers */
1078
1079static int get_timer(QEMUFile *f, void *pv, size_t size)
1080{
1081 QEMUTimer *v = pv;
1082 qemu_get_timer(f, v);
1083 return 0;
1084}
1085
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001086static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +02001087{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001088 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +02001089 qemu_put_timer(f, v);
1090}
1091
1092const VMStateInfo vmstate_info_timer = {
1093 .name = "timer",
1094 .get = get_timer,
1095 .put = put_timer,
1096};
1097
Juan Quintela6f67c502009-08-20 19:42:35 +02001098/* uint8_t buffers */
1099
1100static int get_buffer(QEMUFile *f, void *pv, size_t size)
1101{
1102 uint8_t *v = pv;
1103 qemu_get_buffer(f, v, size);
1104 return 0;
1105}
1106
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001107static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +02001108{
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001109 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +02001110 qemu_put_buffer(f, v, size);
1111}
1112
1113const VMStateInfo vmstate_info_buffer = {
1114 .name = "buffer",
1115 .get = get_buffer,
1116 .put = put_buffer,
1117};
1118
Juan Quintela76507c72009-10-19 15:46:28 +02001119/* unused buffers: space that was used for some fields that are
Stefan Weil61cc8702011-04-13 22:45:22 +02001120 not useful anymore */
Juan Quintela76507c72009-10-19 15:46:28 +02001121
1122static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1123{
Jan Kiszka21174c32009-12-02 12:36:35 +01001124 uint8_t buf[1024];
1125 int block_len;
1126
1127 while (size > 0) {
1128 block_len = MIN(sizeof(buf), size);
1129 size -= block_len;
1130 qemu_get_buffer(f, buf, block_len);
1131 }
1132 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +02001133}
1134
1135static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1136{
Jan Kiszka21174c32009-12-02 12:36:35 +01001137 static const uint8_t buf[1024];
1138 int block_len;
1139
1140 while (size > 0) {
1141 block_len = MIN(sizeof(buf), size);
1142 size -= block_len;
1143 qemu_put_buffer(f, buf, block_len);
1144 }
Juan Quintela76507c72009-10-19 15:46:28 +02001145}
1146
1147const VMStateInfo vmstate_info_unused_buffer = {
1148 .name = "unused_buffer",
1149 .get = get_unused_buffer,
1150 .put = put_unused_buffer,
1151};
1152
Alex Williamson7685ee62010-06-25 11:09:14 -06001153typedef struct CompatEntry {
1154 char idstr[256];
1155 int instance_id;
1156} CompatEntry;
1157
aliguoria672b462008-11-11 21:33:36 +00001158typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001159 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001160 char idstr[256];
1161 int instance_id;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001162 int alias_id;
aliguoria672b462008-11-11 21:33:36 +00001163 int version_id;
1164 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001165 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +00001166 SaveLiveStateHandler *save_live_state;
1167 SaveStateHandler *save_state;
1168 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001169 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001170 void *opaque;
Alex Williamson7685ee62010-06-25 11:09:14 -06001171 CompatEntry *compat;
Cam Macdonell24312962010-07-26 18:11:00 -06001172 int no_migrate;
aliguoria672b462008-11-11 21:33:36 +00001173} SaveStateEntry;
1174
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001175
Blue Swirl72cf2d42009-09-12 07:36:22 +00001176static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1177 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001178static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001179
Juan Quintela8718e992009-09-01 02:12:31 +02001180static int calculate_new_instance_id(const char *idstr)
1181{
1182 SaveStateEntry *se;
1183 int instance_id = 0;
1184
Blue Swirl72cf2d42009-09-12 07:36:22 +00001185 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001186 if (strcmp(idstr, se->idstr) == 0
1187 && instance_id <= se->instance_id) {
1188 instance_id = se->instance_id + 1;
1189 }
1190 }
1191 return instance_id;
1192}
1193
Alex Williamson7685ee62010-06-25 11:09:14 -06001194static int calculate_compat_instance_id(const char *idstr)
1195{
1196 SaveStateEntry *se;
1197 int instance_id = 0;
1198
1199 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1200 if (!se->compat)
1201 continue;
1202
1203 if (strcmp(idstr, se->compat->idstr) == 0
1204 && instance_id <= se->compat->instance_id) {
1205 instance_id = se->compat->instance_id + 1;
1206 }
1207 }
1208 return instance_id;
1209}
1210
aliguoria672b462008-11-11 21:33:36 +00001211/* TODO: Individual devices generally have very little idea about the rest
1212 of the system, so instance_id should be removed/replaced.
1213 Meanwhile pass -1 as instance_id if you do not already have a clearly
1214 distinguishing id for all instances of your device class. */
Alex Williamson0be71e32010-06-25 11:09:07 -06001215int register_savevm_live(DeviceState *dev,
1216 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001217 int instance_id,
1218 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001219 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001220 SaveLiveStateHandler *save_live_state,
1221 SaveStateHandler *save_state,
1222 LoadStateHandler *load_state,
1223 void *opaque)
1224{
Juan Quintela8718e992009-09-01 02:12:31 +02001225 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001226
Anthony Liguori7267c092011-08-20 22:09:37 -05001227 se = g_malloc0(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001228 se->version_id = version_id;
1229 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001230 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001231 se->save_live_state = save_live_state;
1232 se->save_state = save_state;
1233 se->load_state = load_state;
1234 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001235 se->vmsd = NULL;
Cam Macdonell24312962010-07-26 18:11:00 -06001236 se->no_migrate = 0;
aliguoria672b462008-11-11 21:33:36 +00001237
Alex Williamson7685ee62010-06-25 11:09:14 -06001238 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1239 char *id = dev->parent_bus->info->get_dev_path(dev);
1240 if (id) {
1241 pstrcpy(se->idstr, sizeof(se->idstr), id);
1242 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001243 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001244
Anthony Liguori7267c092011-08-20 22:09:37 -05001245 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001246 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1247 se->compat->instance_id = instance_id == -1 ?
1248 calculate_compat_instance_id(idstr) : instance_id;
1249 instance_id = -1;
1250 }
1251 }
1252 pstrcat(se->idstr, sizeof(se->idstr), idstr);
1253
Juan Quintela8718e992009-09-01 02:12:31 +02001254 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001255 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001256 } else {
1257 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001258 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001259 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001260 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001261 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001262 return 0;
1263}
1264
Alex Williamson0be71e32010-06-25 11:09:07 -06001265int register_savevm(DeviceState *dev,
1266 const char *idstr,
aliguoria672b462008-11-11 21:33:36 +00001267 int instance_id,
1268 int version_id,
1269 SaveStateHandler *save_state,
1270 LoadStateHandler *load_state,
1271 void *opaque)
1272{
Alex Williamson0be71e32010-06-25 11:09:07 -06001273 return register_savevm_live(dev, idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001274 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001275}
1276
Alex Williamson0be71e32010-06-25 11:09:07 -06001277void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
aliguori41bd13a2009-04-17 17:10:59 +00001278{
Juan Quintela8718e992009-09-01 02:12:31 +02001279 SaveStateEntry *se, *new_se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001280 char id[256] = "";
1281
1282 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1283 char *path = dev->parent_bus->info->get_dev_path(dev);
1284 if (path) {
1285 pstrcpy(id, sizeof(id), path);
1286 pstrcat(id, sizeof(id), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001287 g_free(path);
Alex Williamson7685ee62010-06-25 11:09:14 -06001288 }
1289 }
1290 pstrcat(id, sizeof(id), idstr);
aliguori41bd13a2009-04-17 17:10:59 +00001291
Blue Swirl72cf2d42009-09-12 07:36:22 +00001292 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001293 if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001294 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001295 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001296 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001297 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001298 g_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001299 }
aliguori41bd13a2009-04-17 17:10:59 +00001300 }
1301}
1302
Alex Williamson0be71e32010-06-25 11:09:07 -06001303int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001304 const VMStateDescription *vmsd,
1305 void *opaque, int alias_id,
1306 int required_for_version)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001307{
Juan Quintela8718e992009-09-01 02:12:31 +02001308 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001309
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001310 /* If this triggers, alias support can be dropped for the vmsd. */
1311 assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1312
Anthony Liguori7267c092011-08-20 22:09:37 -05001313 se = g_malloc0(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001314 se->version_id = vmsd->version_id;
1315 se->section_id = global_section_id++;
1316 se->save_live_state = NULL;
1317 se->save_state = NULL;
1318 se->load_state = NULL;
1319 se->opaque = opaque;
1320 se->vmsd = vmsd;
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001321 se->alias_id = alias_id;
Gerd Hoffmann2837c8e2011-07-08 10:44:35 +02001322 se->no_migrate = vmsd->unmigratable;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001323
Alex Williamson7685ee62010-06-25 11:09:14 -06001324 if (dev && dev->parent_bus && dev->parent_bus->info->get_dev_path) {
1325 char *id = dev->parent_bus->info->get_dev_path(dev);
1326 if (id) {
1327 pstrcpy(se->idstr, sizeof(se->idstr), id);
1328 pstrcat(se->idstr, sizeof(se->idstr), "/");
Anthony Liguori7267c092011-08-20 22:09:37 -05001329 g_free(id);
Alex Williamson7685ee62010-06-25 11:09:14 -06001330
Anthony Liguori7267c092011-08-20 22:09:37 -05001331 se->compat = g_malloc0(sizeof(CompatEntry));
Alex Williamson7685ee62010-06-25 11:09:14 -06001332 pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1333 se->compat->instance_id = instance_id == -1 ?
1334 calculate_compat_instance_id(vmsd->name) : instance_id;
1335 instance_id = -1;
1336 }
1337 }
1338 pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1339
Juan Quintela8718e992009-09-01 02:12:31 +02001340 if (instance_id == -1) {
Alex Williamson7685ee62010-06-25 11:09:14 -06001341 se->instance_id = calculate_new_instance_id(se->idstr);
Juan Quintela8718e992009-09-01 02:12:31 +02001342 } else {
1343 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001344 }
Alex Williamson7685ee62010-06-25 11:09:14 -06001345 assert(!se->compat || se->instance_id == 0);
Juan Quintela8718e992009-09-01 02:12:31 +02001346 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001347 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001348 return 0;
1349}
1350
Alex Williamson0be71e32010-06-25 11:09:07 -06001351int vmstate_register(DeviceState *dev, int instance_id,
1352 const VMStateDescription *vmsd, void *opaque)
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001353{
Alex Williamson0be71e32010-06-25 11:09:07 -06001354 return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1355 opaque, -1, 0);
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001356}
1357
Alex Williamson0be71e32010-06-25 11:09:07 -06001358void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1359 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001360{
Juan Quintela1eb75382009-09-10 03:04:29 +02001361 SaveStateEntry *se, *new_se;
1362
Blue Swirl72cf2d42009-09-12 07:36:22 +00001363 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001364 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001365 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Alex Williamson69e58af2010-07-21 08:35:31 -06001366 if (se->compat) {
Anthony Liguori7267c092011-08-20 22:09:37 -05001367 g_free(se->compat);
Alex Williamson69e58af2010-07-21 08:35:31 -06001368 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001369 g_free(se);
Juan Quintela1eb75382009-09-10 03:04:29 +02001370 }
1371 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001372}
1373
Juan Quintela811814b2010-07-26 21:38:43 +02001374static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1375 void *opaque);
1376static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1377 void *opaque);
1378
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001379int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1380 void *opaque, int version_id)
1381{
1382 VMStateField *field = vmsd->fields;
Juan Quintela811814b2010-07-26 21:38:43 +02001383 int ret;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001384
1385 if (version_id > vmsd->version_id) {
1386 return -EINVAL;
1387 }
1388 if (version_id < vmsd->minimum_version_id_old) {
1389 return -EINVAL;
1390 }
1391 if (version_id < vmsd->minimum_version_id) {
1392 return vmsd->load_state_old(f, opaque, version_id);
1393 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001394 if (vmsd->pre_load) {
1395 int ret = vmsd->pre_load(opaque);
1396 if (ret)
1397 return ret;
1398 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001399 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001400 if ((field->field_exists &&
1401 field->field_exists(opaque, version_id)) ||
1402 (!field->field_exists &&
1403 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001404 void *base_addr = opaque + field->offset;
Juan Quintela811814b2010-07-26 21:38:43 +02001405 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001406 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001407
Juan Quintelae61a1e02009-12-02 12:36:38 +01001408 if (field->flags & VMS_VBUFFER) {
1409 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001410 if (field->flags & VMS_MULTIPLY) {
1411 size *= field->size;
1412 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001413 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001414 if (field->flags & VMS_ARRAY) {
1415 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001416 } else if (field->flags & VMS_VARRAY_INT32) {
1417 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelaa624b082011-03-10 12:33:50 +01001418 } else if (field->flags & VMS_VARRAY_UINT32) {
1419 n_elems = *(uint32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001420 } else if (field->flags & VMS_VARRAY_UINT16) {
1421 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintela82fa39b2011-03-10 12:33:49 +01001422 } else if (field->flags & VMS_VARRAY_UINT8) {
1423 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001424 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001425 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001426 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001427 }
1428 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001429 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001430
Juan Quintela19df4382009-09-29 22:48:41 +02001431 if (field->flags & VMS_ARRAY_OF_POINTER) {
1432 addr = *(void **)addr;
1433 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001434 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001435 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001436 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001437 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001438
1439 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001440 if (ret < 0) {
1441 return ret;
1442 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001443 }
1444 }
1445 field++;
1446 }
Juan Quintela811814b2010-07-26 21:38:43 +02001447 ret = vmstate_subsection_load(f, vmsd, opaque);
1448 if (ret != 0) {
1449 return ret;
1450 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001451 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001452 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001453 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001454 return 0;
1455}
1456
1457void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001458 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001459{
1460 VMStateField *field = vmsd->fields;
1461
Juan Quintela8fb07912009-09-10 03:04:32 +02001462 if (vmsd->pre_save) {
1463 vmsd->pre_save(opaque);
1464 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001465 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001466 if (!field->field_exists ||
1467 field->field_exists(opaque, vmsd->version_id)) {
1468 void *base_addr = opaque + field->offset;
1469 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001470 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001471
Juan Quintelae61a1e02009-12-02 12:36:38 +01001472 if (field->flags & VMS_VBUFFER) {
1473 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001474 if (field->flags & VMS_MULTIPLY) {
1475 size *= field->size;
1476 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001477 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001478 if (field->flags & VMS_ARRAY) {
1479 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001480 } else if (field->flags & VMS_VARRAY_INT32) {
1481 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001482 } else if (field->flags & VMS_VARRAY_UINT16) {
1483 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelab7844212011-03-15 15:53:25 +01001484 } else if (field->flags & VMS_VARRAY_UINT8) {
1485 n_elems = *(uint8_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001486 }
1487 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001488 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001489 }
1490 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001491 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001492
Juan Quintela85953872009-12-02 12:36:37 +01001493 if (field->flags & VMS_ARRAY_OF_POINTER) {
1494 addr = *(void **)addr;
1495 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001496 if (field->flags & VMS_STRUCT) {
1497 vmstate_save_state(f, field->vmsd, addr);
1498 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001499 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001500 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001501 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001502 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001503 field++;
1504 }
Juan Quintela811814b2010-07-26 21:38:43 +02001505 vmstate_subsection_save(f, vmsd, opaque);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001506}
1507
Juan Quintela4082be42009-08-20 19:42:24 +02001508static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1509{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001510 if (!se->vmsd) { /* Old style */
1511 return se->load_state(f, se->opaque, version_id);
1512 }
1513 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001514}
1515
Alex Williamsondc912122011-01-11 14:39:43 -07001516static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
Juan Quintela4082be42009-08-20 19:42:24 +02001517{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001518 if (!se->vmsd) { /* Old style */
1519 se->save_state(f, se->opaque);
Alex Williamsondc912122011-01-11 14:39:43 -07001520 return;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001521 }
1522 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001523}
1524
aliguoria672b462008-11-11 21:33:36 +00001525#define QEMU_VM_FILE_MAGIC 0x5145564d
1526#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1527#define QEMU_VM_FILE_VERSION 0x00000003
1528
1529#define QEMU_VM_EOF 0x00
1530#define QEMU_VM_SECTION_START 0x01
1531#define QEMU_VM_SECTION_PART 0x02
1532#define QEMU_VM_SECTION_END 0x03
1533#define QEMU_VM_SECTION_FULL 0x04
Juan Quintela811814b2010-07-26 21:38:43 +02001534#define QEMU_VM_SUBSECTION 0x05
aliguoria672b462008-11-11 21:33:36 +00001535
Alex Williamsondc912122011-01-11 14:39:43 -07001536bool qemu_savevm_state_blocked(Monitor *mon)
1537{
1538 SaveStateEntry *se;
1539
1540 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1541 if (se->no_migrate) {
1542 monitor_printf(mon, "state blocked by non-migratable device '%s'\n",
1543 se->idstr);
1544 return true;
1545 }
1546 }
1547 return false;
1548}
1549
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001550int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1551 int shared)
aliguoria672b462008-11-11 21:33:36 +00001552{
1553 SaveStateEntry *se;
Juan Quintela39346382011-09-22 11:02:14 +02001554 int ret;
aliguoria672b462008-11-11 21:33:36 +00001555
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001556 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1557 if(se->set_params == NULL) {
1558 continue;
1559 }
1560 se->set_params(blk_enable, shared, se->opaque);
1561 }
1562
aliguoria672b462008-11-11 21:33:36 +00001563 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1564 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1565
Blue Swirl72cf2d42009-09-12 07:36:22 +00001566 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001567 int len;
1568
1569 if (se->save_live_state == NULL)
1570 continue;
1571
1572 /* Section type */
1573 qemu_put_byte(f, QEMU_VM_SECTION_START);
1574 qemu_put_be32(f, se->section_id);
1575
1576 /* ID string */
1577 len = strlen(se->idstr);
1578 qemu_put_byte(f, len);
1579 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1580
1581 qemu_put_be32(f, se->instance_id);
1582 qemu_put_be32(f, se->version_id);
1583
Juan Quintela29757252011-10-19 15:22:18 +02001584 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
1585 if (ret < 0) {
1586 qemu_savevm_state_cancel(mon, f);
1587 return ret;
1588 }
aliguoria672b462008-11-11 21:33:36 +00001589 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001590 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001591 if (ret != 0) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001592 qemu_savevm_state_cancel(mon, f);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001593 }
aliguoria672b462008-11-11 21:33:36 +00001594
Juan Quintela39346382011-09-22 11:02:14 +02001595 return ret;
1596
aliguoria672b462008-11-11 21:33:36 +00001597}
1598
Juan Quintela39346382011-09-22 11:02:14 +02001599/*
Dong Xu Wang07f35072011-11-22 18:06:26 +08001600 * this function has three return values:
Juan Quintela39346382011-09-22 11:02:14 +02001601 * negative: there was one error, and we have -errno.
1602 * 0 : We haven't finished, caller have to go again
1603 * 1 : We have finished, we can go to complete phase
1604 */
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001605int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001606{
1607 SaveStateEntry *se;
1608 int ret = 1;
1609
Blue Swirl72cf2d42009-09-12 07:36:22 +00001610 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001611 if (se->save_live_state == NULL)
1612 continue;
1613
1614 /* Section type */
1615 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1616 qemu_put_be32(f, se->section_id);
1617
Jan Kiszka90697be2009-12-01 15:19:55 +01001618 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
Juan Quintela29757252011-10-19 15:22:18 +02001619 if (ret <= 0) {
Jan Kiszka90697be2009-12-01 15:19:55 +01001620 /* Do not proceed to the next vmstate before this one reported
1621 completion of the current stage. This serializes the migration
1622 and reduces the probability that a faster changing state is
1623 synchronized over and over again. */
1624 break;
1625 }
aliguoria672b462008-11-11 21:33:36 +00001626 }
Juan Quintela39346382011-09-22 11:02:14 +02001627 if (ret != 0) {
1628 return ret;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001629 }
Juan Quintela624b9cc2011-10-05 01:02:52 +02001630 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001631 if (ret != 0) {
1632 qemu_savevm_state_cancel(mon, f);
1633 }
1634 return ret;
aliguoria672b462008-11-11 21:33:36 +00001635}
1636
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001637int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001638{
1639 SaveStateEntry *se;
Juan Quintela29757252011-10-19 15:22:18 +02001640 int ret;
aliguoria672b462008-11-11 21:33:36 +00001641
Jan Kiszkaea375f92010-03-01 19:10:30 +01001642 cpu_synchronize_all_states();
1643
Blue Swirl72cf2d42009-09-12 07:36:22 +00001644 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001645 if (se->save_live_state == NULL)
1646 continue;
1647
1648 /* Section type */
1649 qemu_put_byte(f, QEMU_VM_SECTION_END);
1650 qemu_put_be32(f, se->section_id);
1651
Juan Quintela29757252011-10-19 15:22:18 +02001652 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
1653 if (ret < 0) {
1654 return ret;
1655 }
aliguoria672b462008-11-11 21:33:36 +00001656 }
1657
Blue Swirl72cf2d42009-09-12 07:36:22 +00001658 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001659 int len;
1660
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001661 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001662 continue;
1663
1664 /* Section type */
1665 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1666 qemu_put_be32(f, se->section_id);
1667
1668 /* ID string */
1669 len = strlen(se->idstr);
1670 qemu_put_byte(f, len);
1671 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1672
1673 qemu_put_be32(f, se->instance_id);
1674 qemu_put_be32(f, se->version_id);
1675
Alex Williamsondc912122011-01-11 14:39:43 -07001676 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001677 }
1678
1679 qemu_put_byte(f, QEMU_VM_EOF);
1680
Juan Quintela624b9cc2011-10-05 01:02:52 +02001681 return qemu_file_get_error(f);
aliguoria672b462008-11-11 21:33:36 +00001682}
1683
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001684void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001685{
1686 SaveStateEntry *se;
1687
1688 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1689 if (se->save_live_state) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001690 se->save_live_state(mon, f, -1, se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001691 }
1692 }
1693}
1694
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001695static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001696{
aliguoria672b462008-11-11 21:33:36 +00001697 int ret;
1698
Alex Williamsondc912122011-01-11 14:39:43 -07001699 if (qemu_savevm_state_blocked(mon)) {
1700 ret = -EINVAL;
1701 goto out;
1702 }
1703
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001704 ret = qemu_savevm_state_begin(mon, f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001705 if (ret < 0)
1706 goto out;
1707
1708 do {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001709 ret = qemu_savevm_state_iterate(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001710 if (ret < 0)
1711 goto out;
1712 } while (ret == 0);
1713
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001714 ret = qemu_savevm_state_complete(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001715
1716out:
Juan Quintela39346382011-09-22 11:02:14 +02001717 if (ret == 0) {
Juan Quintela624b9cc2011-10-05 01:02:52 +02001718 ret = qemu_file_get_error(f);
Juan Quintela39346382011-09-22 11:02:14 +02001719 }
aliguoria672b462008-11-11 21:33:36 +00001720
aliguoria672b462008-11-11 21:33:36 +00001721 return ret;
1722}
1723
1724static SaveStateEntry *find_se(const char *idstr, int instance_id)
1725{
1726 SaveStateEntry *se;
1727
Blue Swirl72cf2d42009-09-12 07:36:22 +00001728 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001729 if (!strcmp(se->idstr, idstr) &&
Jan Kiszka4d2ffa02010-05-15 13:32:40 +02001730 (instance_id == se->instance_id ||
1731 instance_id == se->alias_id))
aliguoria672b462008-11-11 21:33:36 +00001732 return se;
Alex Williamson7685ee62010-06-25 11:09:14 -06001733 /* Migrating from an older version? */
1734 if (strstr(se->idstr, idstr) && se->compat) {
1735 if (!strcmp(se->compat->idstr, idstr) &&
1736 (instance_id == se->compat->instance_id ||
1737 instance_id == se->alias_id))
1738 return se;
1739 }
aliguoria672b462008-11-11 21:33:36 +00001740 }
1741 return NULL;
1742}
1743
Juan Quintela811814b2010-07-26 21:38:43 +02001744static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1745{
1746 while(sub && sub->needed) {
1747 if (strcmp(idstr, sub->vmsd->name) == 0) {
1748 return sub->vmsd;
1749 }
1750 sub++;
1751 }
1752 return NULL;
1753}
1754
1755static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1756 void *opaque)
1757{
Juan Quintelac6380722011-10-04 15:28:31 +02001758 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
Juan Quintela811814b2010-07-26 21:38:43 +02001759 char idstr[256];
1760 int ret;
Juan Quintelac6380722011-10-04 15:28:31 +02001761 uint8_t version_id, len, size;
Juan Quintela811814b2010-07-26 21:38:43 +02001762 const VMStateDescription *sub_vmsd;
1763
Juan Quintelac6380722011-10-04 15:28:31 +02001764 len = qemu_peek_byte(f, 1);
1765 if (len < strlen(vmsd->name) + 1) {
1766 /* subsection name has be be "section_name/a" */
1767 return 0;
1768 }
1769 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1770 if (size != len) {
1771 return 0;
1772 }
1773 idstr[size] = 0;
Juan Quintela811814b2010-07-26 21:38:43 +02001774
Juan Quintelac6380722011-10-04 15:28:31 +02001775 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1776 /* it don't have a valid subsection name */
1777 return 0;
1778 }
Juan Quintela3da9eeb2011-09-30 19:46:43 +02001779 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
Juan Quintela811814b2010-07-26 21:38:43 +02001780 if (sub_vmsd == NULL) {
1781 return -ENOENT;
1782 }
Juan Quintelac6380722011-10-04 15:28:31 +02001783 qemu_file_skip(f, 1); /* subsection */
1784 qemu_file_skip(f, 1); /* len */
1785 qemu_file_skip(f, len); /* idstr */
1786 version_id = qemu_get_be32(f);
1787
Juan Quintela811814b2010-07-26 21:38:43 +02001788 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1789 if (ret) {
1790 return ret;
1791 }
1792 }
1793 return 0;
1794}
1795
1796static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1797 void *opaque)
1798{
1799 const VMStateSubsection *sub = vmsd->subsections;
1800
1801 while (sub && sub->needed) {
1802 if (sub->needed(opaque)) {
1803 const VMStateDescription *vmsd = sub->vmsd;
1804 uint8_t len;
1805
1806 qemu_put_byte(f, QEMU_VM_SUBSECTION);
1807 len = strlen(vmsd->name);
1808 qemu_put_byte(f, len);
1809 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1810 qemu_put_be32(f, vmsd->version_id);
1811 vmstate_save_state(f, vmsd, opaque);
1812 }
1813 sub++;
1814 }
1815}
1816
aliguoria672b462008-11-11 21:33:36 +00001817typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001818 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001819 SaveStateEntry *se;
1820 int section_id;
1821 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001822} LoadStateEntry;
1823
aliguoria672b462008-11-11 21:33:36 +00001824int qemu_loadvm_state(QEMUFile *f)
1825{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001826 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1827 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001828 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001829 uint8_t section_type;
1830 unsigned int v;
1831 int ret;
1832
Alex Williamsondc912122011-01-11 14:39:43 -07001833 if (qemu_savevm_state_blocked(default_mon)) {
1834 return -EINVAL;
1835 }
1836
aliguoria672b462008-11-11 21:33:36 +00001837 v = qemu_get_be32(f);
1838 if (v != QEMU_VM_FILE_MAGIC)
1839 return -EINVAL;
1840
1841 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001842 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1843 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1844 return -ENOTSUP;
1845 }
aliguoria672b462008-11-11 21:33:36 +00001846 if (v != QEMU_VM_FILE_VERSION)
1847 return -ENOTSUP;
1848
1849 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1850 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001851 SaveStateEntry *se;
1852 char idstr[257];
1853 int len;
1854
1855 switch (section_type) {
1856 case QEMU_VM_SECTION_START:
1857 case QEMU_VM_SECTION_FULL:
1858 /* Read section start */
1859 section_id = qemu_get_be32(f);
1860 len = qemu_get_byte(f);
1861 qemu_get_buffer(f, (uint8_t *)idstr, len);
1862 idstr[len] = 0;
1863 instance_id = qemu_get_be32(f);
1864 version_id = qemu_get_be32(f);
1865
1866 /* Find savevm section */
1867 se = find_se(idstr, instance_id);
1868 if (se == NULL) {
1869 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1870 ret = -EINVAL;
1871 goto out;
1872 }
1873
1874 /* Validate version */
1875 if (version_id > se->version_id) {
1876 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1877 version_id, idstr, se->version_id);
1878 ret = -EINVAL;
1879 goto out;
1880 }
1881
1882 /* Add entry */
Anthony Liguori7267c092011-08-20 22:09:37 -05001883 le = g_malloc0(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001884
1885 le->se = se;
1886 le->section_id = section_id;
1887 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001888 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001889
Juan Quintela4082be42009-08-20 19:42:24 +02001890 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001891 if (ret < 0) {
1892 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1893 instance_id, idstr);
1894 goto out;
1895 }
aliguoria672b462008-11-11 21:33:36 +00001896 break;
1897 case QEMU_VM_SECTION_PART:
1898 case QEMU_VM_SECTION_END:
1899 section_id = qemu_get_be32(f);
1900
Blue Swirl72cf2d42009-09-12 07:36:22 +00001901 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001902 if (le->section_id == section_id) {
1903 break;
1904 }
1905 }
aliguoria672b462008-11-11 21:33:36 +00001906 if (le == NULL) {
1907 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1908 ret = -EINVAL;
1909 goto out;
1910 }
1911
Juan Quintela4082be42009-08-20 19:42:24 +02001912 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001913 if (ret < 0) {
1914 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1915 section_id);
1916 goto out;
1917 }
aliguoria672b462008-11-11 21:33:36 +00001918 break;
1919 default:
1920 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1921 ret = -EINVAL;
1922 goto out;
1923 }
1924 }
1925
Jan Kiszkaea375f92010-03-01 19:10:30 +01001926 cpu_synchronize_all_post_init();
1927
aliguoria672b462008-11-11 21:33:36 +00001928 ret = 0;
1929
1930out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001931 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1932 QLIST_REMOVE(le, entry);
Anthony Liguori7267c092011-08-20 22:09:37 -05001933 g_free(le);
aliguoria672b462008-11-11 21:33:36 +00001934 }
1935
Juan Quintela42802d42011-10-05 01:14:46 +02001936 if (ret == 0) {
1937 ret = qemu_file_get_error(f);
Juan Quintela624b9cc2011-10-05 01:02:52 +02001938 }
aliguoria672b462008-11-11 21:33:36 +00001939
1940 return ret;
1941}
1942
aliguoria672b462008-11-11 21:33:36 +00001943static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1944 const char *name)
1945{
1946 QEMUSnapshotInfo *sn_tab, *sn;
1947 int nb_sns, i, ret;
1948
1949 ret = -ENOENT;
1950 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1951 if (nb_sns < 0)
1952 return ret;
1953 for(i = 0; i < nb_sns; i++) {
1954 sn = &sn_tab[i];
1955 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1956 *sn_info = *sn;
1957 ret = 0;
1958 break;
1959 }
1960 }
Anthony Liguori7267c092011-08-20 22:09:37 -05001961 g_free(sn_tab);
aliguoria672b462008-11-11 21:33:36 +00001962 return ret;
1963}
1964
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001965/*
1966 * Deletes snapshots of a given name in all opened images.
1967 */
1968static int del_existing_snapshots(Monitor *mon, const char *name)
1969{
1970 BlockDriverState *bs;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001971 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1972 int ret;
1973
Markus Armbrusterdbc13592010-06-02 18:55:21 +02001974 bs = NULL;
1975 while ((bs = bdrv_next(bs))) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001976 if (bdrv_can_snapshot(bs) &&
1977 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1978 {
1979 ret = bdrv_snapshot_delete(bs, name);
1980 if (ret < 0) {
1981 monitor_printf(mon,
1982 "Error while deleting snapshot on '%s'\n",
1983 bdrv_get_device_name(bs));
1984 return -1;
1985 }
1986 }
1987 }
1988
1989 return 0;
1990}
1991
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001992void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001993{
1994 BlockDriverState *bs, *bs1;
1995 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001996 int ret;
aliguoria672b462008-11-11 21:33:36 +00001997 QEMUFile *f;
1998 int saved_vm_running;
aliguori2d22b182008-12-11 21:06:49 +00001999 uint32_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00002000#ifdef _WIN32
2001 struct _timeb tb;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002002 struct tm *ptm;
aliguoria672b462008-11-11 21:33:36 +00002003#else
2004 struct timeval tv;
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002005 struct tm tm;
aliguoria672b462008-11-11 21:33:36 +00002006#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002007 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002008
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002009 /* Verify if there is a device that doesn't support snapshots and is writable */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002010 bs = NULL;
2011 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002012
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002013 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002014 continue;
2015 }
2016
2017 if (!bdrv_can_snapshot(bs)) {
2018 monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2019 bdrv_get_device_name(bs));
2020 return;
2021 }
2022 }
2023
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002024 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002025 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002026 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002027 return;
2028 }
aliguoria672b462008-11-11 21:33:36 +00002029
Luiz Capitulino13548692011-07-29 15:36:43 -03002030 saved_vm_running = runstate_is_running();
Luiz Capitulino0461d5a2011-09-30 14:45:27 -03002031 vm_stop(RUN_STATE_SAVE_VM);
aliguoria672b462008-11-11 21:33:36 +00002032
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002033 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00002034
2035 /* fill auxiliary fields */
2036#ifdef _WIN32
2037 _ftime(&tb);
2038 sn->date_sec = tb.time;
2039 sn->date_nsec = tb.millitm * 1000000;
2040#else
2041 gettimeofday(&tv, NULL);
2042 sn->date_sec = tv.tv_sec;
2043 sn->date_nsec = tv.tv_usec * 1000;
2044#endif
Paolo Bonzini74475452011-03-11 16:47:48 +01002045 sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
aliguoria672b462008-11-11 21:33:36 +00002046
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002047 if (name) {
2048 ret = bdrv_snapshot_find(bs, old_sn, name);
2049 if (ret >= 0) {
2050 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2051 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2052 } else {
2053 pstrcpy(sn->name, sizeof(sn->name), name);
2054 }
2055 } else {
2056#ifdef _WIN32
2057 ptm = localtime(&tb.time);
2058 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2059#else
Blue Swirld7d9b522010-09-09 19:13:04 +00002060 /* cast below needed for OpenBSD where tv_sec is still 'long' */
2061 localtime_r((const time_t *)&tv.tv_sec, &tm);
Miguel Di Ciurcio Filho7d631a12010-08-04 14:55:49 -03002062 strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2063#endif
2064 }
2065
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002066 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02002067 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01002068 goto the_end;
2069 }
2070
aliguoria672b462008-11-11 21:33:36 +00002071 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02002072 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00002073 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00002074 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00002075 goto the_end;
2076 }
Jan Kiszkaf327aa02009-11-30 18:21:21 +01002077 ret = qemu_savevm_state(mon, f);
aliguori2d22b182008-12-11 21:06:49 +00002078 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00002079 qemu_fclose(f);
2080 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002081 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00002082 goto the_end;
2083 }
2084
2085 /* create the snapshots */
2086
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002087 bs1 = NULL;
2088 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002089 if (bdrv_can_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00002090 /* Write VM state size only to the image that contains the state */
2091 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00002092 ret = bdrv_snapshot_create(bs1, sn);
2093 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00002094 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2095 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002096 }
2097 }
2098 }
2099
2100 the_end:
2101 if (saved_vm_running)
2102 vm_start();
2103}
2104
Markus Armbruster03cd4652010-02-17 16:24:10 +01002105int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00002106{
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002107 BlockDriverState *bs, *bs_vm_state;
aliguori2d22b182008-12-11 21:06:49 +00002108 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00002109 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002110 int ret;
aliguoria672b462008-11-11 21:33:36 +00002111
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002112 bs_vm_state = bdrv_snapshots();
2113 if (!bs_vm_state) {
2114 error_report("No block device supports snapshots");
2115 return -ENOTSUP;
2116 }
2117
2118 /* Don't even try to load empty VM states */
2119 ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2120 if (ret < 0) {
2121 return ret;
2122 } else if (sn.vm_state_size == 0) {
Kevin Wolfe11480d2011-03-01 10:48:12 +01002123 error_report("This is a disk-only snapshot. Revert to it offline "
2124 "using qemu-img.");
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002125 return -EINVAL;
2126 }
2127
2128 /* Verify if there is any device that doesn't support snapshots and is
2129 writable and check if the requested snapshot is available too. */
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002130 bs = NULL;
2131 while ((bs = bdrv_next(bs))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002132
Markus Armbruster07b70bf2011-08-03 15:08:11 +02002133 if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002134 continue;
2135 }
2136
2137 if (!bdrv_can_snapshot(bs)) {
2138 error_report("Device '%s' is writable but does not support snapshots.",
2139 bdrv_get_device_name(bs));
2140 return -ENOTSUP;
2141 }
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002142
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002143 ret = bdrv_snapshot_find(bs, &sn, name);
2144 if (ret < 0) {
2145 error_report("Device '%s' does not have the requested snapshot '%s'",
2146 bdrv_get_device_name(bs), name);
2147 return ret;
2148 }
aliguoria672b462008-11-11 21:33:36 +00002149 }
2150
2151 /* Flush all IO requests so they don't interfere with the new state. */
Stefan Hajnoczi922453b2011-11-30 12:23:43 +00002152 bdrv_drain_all();
aliguoria672b462008-11-11 21:33:36 +00002153
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002154 bs = NULL;
2155 while ((bs = bdrv_next(bs))) {
2156 if (bdrv_can_snapshot(bs)) {
2157 ret = bdrv_snapshot_goto(bs, name);
aliguoria672b462008-11-11 21:33:36 +00002158 if (ret < 0) {
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002159 error_report("Error %d while activating snapshot '%s' on '%s'",
2160 ret, name, bdrv_get_device_name(bs));
2161 return ret;
aliguoria672b462008-11-11 21:33:36 +00002162 }
2163 }
2164 }
2165
aliguoria672b462008-11-11 21:33:36 +00002166 /* restore the VM state */
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002167 f = qemu_fopen_bdrv(bs_vm_state, 0);
aliguoria672b462008-11-11 21:33:36 +00002168 if (!f) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002169 error_report("Could not open VM state file");
Juan Quintela05f24012009-08-20 19:42:22 +02002170 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00002171 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002172
Jan Kiszka5a8a49d2011-06-14 18:29:45 +02002173 qemu_system_reset(VMRESET_SILENT);
aliguoria672b462008-11-11 21:33:36 +00002174 ret = qemu_loadvm_state(f);
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002175
aliguoria672b462008-11-11 21:33:36 +00002176 qemu_fclose(f);
2177 if (ret < 0) {
Markus Armbruster1ecda022010-02-18 17:25:24 +01002178 error_report("Error %d while loading VM state", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02002179 return ret;
aliguoria672b462008-11-11 21:33:36 +00002180 }
Miguel Di Ciurcio Filhof0aa7a82010-07-19 15:25:01 -03002181
Juan Quintela05f24012009-08-20 19:42:22 +02002182 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02002183}
2184
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002185void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00002186{
2187 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02002188 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03002189 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00002190
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002191 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002192 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002193 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002194 return;
2195 }
2196
Markus Armbrusterdbc13592010-06-02 18:55:21 +02002197 bs1 = NULL;
2198 while ((bs1 = bdrv_next(bs1))) {
Miguel Di Ciurcio Filhofeeee5a2010-06-08 10:40:55 -03002199 if (bdrv_can_snapshot(bs1)) {
aliguoria672b462008-11-11 21:33:36 +00002200 ret = bdrv_snapshot_delete(bs1, name);
2201 if (ret < 0) {
2202 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00002203 monitor_printf(mon,
2204 "Snapshots not supported on device '%s'\n",
2205 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002206 else
aliguori376253e2009-03-05 23:01:23 +00002207 monitor_printf(mon, "Error %d while deleting snapshot on "
2208 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00002209 }
2210 }
2211 }
2212}
2213
aliguori376253e2009-03-05 23:01:23 +00002214void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00002215{
2216 BlockDriverState *bs, *bs1;
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002217 QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2218 int nb_sns, i, ret, available;
2219 int total;
2220 int *available_snapshots;
aliguoria672b462008-11-11 21:33:36 +00002221 char buf[256];
2222
Markus Armbrusterf9092b12010-06-25 10:33:39 +02002223 bs = bdrv_snapshots();
aliguoria672b462008-11-11 21:33:36 +00002224 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00002225 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00002226 return;
2227 }
aliguoria672b462008-11-11 21:33:36 +00002228
2229 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2230 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00002231 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00002232 return;
2233 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002234
2235 if (nb_sns == 0) {
2236 monitor_printf(mon, "There is no snapshot available.\n");
2237 return;
aliguoria672b462008-11-11 21:33:36 +00002238 }
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002239
Anthony Liguori7267c092011-08-20 22:09:37 -05002240 available_snapshots = g_malloc0(sizeof(int) * nb_sns);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002241 total = 0;
2242 for (i = 0; i < nb_sns; i++) {
2243 sn = &sn_tab[i];
2244 available = 1;
2245 bs1 = NULL;
2246
2247 while ((bs1 = bdrv_next(bs1))) {
2248 if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2249 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2250 if (ret < 0) {
2251 available = 0;
2252 break;
2253 }
2254 }
2255 }
2256
2257 if (available) {
2258 available_snapshots[total] = i;
2259 total++;
2260 }
2261 }
2262
2263 if (total > 0) {
2264 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2265 for (i = 0; i < total; i++) {
2266 sn = &sn_tab[available_snapshots[i]];
2267 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2268 }
2269 } else {
2270 monitor_printf(mon, "There is no suitable snapshot available\n");
2271 }
2272
Anthony Liguori7267c092011-08-20 22:09:37 -05002273 g_free(sn_tab);
2274 g_free(available_snapshots);
Miguel Di Ciurcio Filhof9209912010-08-04 14:55:48 -03002275
aliguoria672b462008-11-11 21:33:36 +00002276}