blob: b634686e4f6c141fa30e6fc17bfd2db1eb1f4edc [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>
26#include <signal.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <zlib.h>
31
Juan Quintela71e72a12009-07-27 16:12:56 +020032/* Needed early for CONFIG_BSD etc. */
blueswir1d40cdb12009-03-07 16:52:02 +000033#include "config-host.h"
34
aliguoria672b462008-11-11 21:33:36 +000035#ifndef _WIN32
36#include <sys/times.h>
37#include <sys/wait.h>
38#include <termios.h>
39#include <sys/mman.h>
40#include <sys/ioctl.h>
41#include <sys/resource.h>
42#include <sys/socket.h>
43#include <netinet/in.h>
44#include <net/if.h>
aliguoria672b462008-11-11 21:33:36 +000045#include <arpa/inet.h>
46#include <dirent.h>
47#include <netdb.h>
48#include <sys/select.h>
Juan Quintela71e72a12009-07-27 16:12:56 +020049#ifdef CONFIG_BSD
aliguoria672b462008-11-11 21:33:36 +000050#include <sys/stat.h>
Aurelien Jarnoa167ba52009-11-29 18:00:41 +010051#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
aliguoria672b462008-11-11 21:33:36 +000052#include <libutil.h>
53#else
54#include <util.h>
55#endif
aliguoria672b462008-11-11 21:33:36 +000056#ifdef __linux__
57#include <pty.h>
58#include <malloc.h>
59#include <linux/rtc.h>
60#endif
61#endif
62#endif
63
64#ifdef _WIN32
aliguori49dc7682009-03-08 16:26:59 +000065#include <windows.h>
aliguoria672b462008-11-11 21:33:36 +000066#include <malloc.h>
67#include <sys/timeb.h>
68#include <mmsystem.h>
69#define getopt_long_only getopt_long
70#define memalign(align, size) malloc(size)
71#endif
72
blueswir1511d2b12009-03-07 15:32:56 +000073#include "qemu-common.h"
74#include "hw/hw.h"
75#include "net.h"
76#include "monitor.h"
77#include "sysemu.h"
78#include "qemu-timer.h"
79#include "qemu-char.h"
80#include "block.h"
81#include "audio/audio.h"
82#include "migration.h"
83#include "qemu_socket.h"
Blue Swirl72cf2d42009-09-12 07:36:22 +000084#include "qemu-queue.h"
blueswir1511d2b12009-03-07 15:32:56 +000085
aliguoria672b462008-11-11 21:33:36 +000086/* point to the block driver where the snapshots are managed */
87static BlockDriverState *bs_snapshots;
88
89#define SELF_ANNOUNCE_ROUNDS 5
aliguoria672b462008-11-11 21:33:36 +000090
Nolan18995b92009-10-15 16:53:55 -070091#ifndef ETH_P_RARP
92#define ETH_P_RARP 0x0835
93#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, ... */
143 qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
144 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;
154 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
155 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
179 int has_error;
180};
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 Swirlc5b76b32009-06-13 08:44:31 +0000200 len = recv(s->fd, (void *)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;
212 qemu_free(s);
213 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;
238 pclose(s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000239 qemu_free(s);
240 return 0;
241}
242
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200243static int stdio_fclose(void *opaque)
aliguoria672b462008-11-11 21:33:36 +0000244{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200245 QEMUFileStdio *s = opaque;
246 fclose(s->stdio_file);
247 qemu_free(s);
248 return 0;
249}
aliguoria672b462008-11-11 21:33:36 +0000250
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200251QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
252{
253 QEMUFileStdio *s;
254
255 if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
aliguoria672b462008-11-11 21:33:36 +0000256 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
257 return NULL;
258 }
259
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200260 s = qemu_mallocz(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000261
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200262 s->stdio_file = stdio_file;
aliguoria672b462008-11-11 21:33:36 +0000263
264 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200265 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose,
266 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000267 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200268 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose,
269 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000270 }
aliguoria672b462008-11-11 21:33:36 +0000271 return s->file;
272}
273
274QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
275{
276 FILE *popen_file;
277
278 popen_file = popen(command, mode);
279 if(popen_file == NULL) {
280 return NULL;
281 }
282
283 return qemu_popen(popen_file, mode);
284}
285
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200286int qemu_stdio_fd(QEMUFile *f)
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200287{
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200288 QEMUFileStdio *p;
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200289 int fd;
290
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200291 p = (QEMUFileStdio *)f->opaque;
292 fd = fileno(p->stdio_file);
Chris Lalancette8a43b1e2009-05-25 16:38:23 +0200293
294 return fd;
295}
296
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200297QEMUFile *qemu_fdopen(int fd, const char *mode)
298{
299 QEMUFileStdio *s;
300
301 if (mode == NULL ||
302 (mode[0] != 'r' && mode[0] != 'w') ||
303 mode[1] != 'b' || mode[2] != 0) {
304 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
305 return NULL;
306 }
307
308 s = qemu_mallocz(sizeof(QEMUFileStdio));
309 s->stdio_file = fdopen(fd, mode);
310 if (!s->stdio_file)
311 goto fail;
312
313 if(mode[0] == 'r') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200314 s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
315 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200316 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200317 s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose,
318 NULL, NULL, NULL);
Paolo Bonzini5ac1fad2009-08-18 15:56:25 +0200319 }
320 return s->file;
321
322fail:
323 qemu_free(s);
324 return NULL;
325}
326
aliguoria672b462008-11-11 21:33:36 +0000327QEMUFile *qemu_fopen_socket(int fd)
328{
329 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
330
aliguoria672b462008-11-11 21:33:36 +0000331 s->fd = fd;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200332 s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
333 NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000334 return s->file;
335}
336
aliguoria672b462008-11-11 21:33:36 +0000337static int file_put_buffer(void *opaque, const uint8_t *buf,
338 int64_t pos, int size)
339{
340 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200341 fseek(s->stdio_file, pos, SEEK_SET);
Kirill A. Shutemov5fdb3aa2009-12-25 18:19:19 +0000342 return fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000343}
344
345static int file_get_buffer(void *opaque, uint8_t *buf, 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);
349 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000350}
351
352QEMUFile *qemu_fopen(const char *filename, const char *mode)
353{
354 QEMUFileStdio *s;
355
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200356 if (mode == NULL ||
357 (mode[0] != 'r' && mode[0] != 'w') ||
358 mode[1] != 'b' || mode[2] != 0) {
359 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
360 return NULL;
361 }
362
aliguoria672b462008-11-11 21:33:36 +0000363 s = qemu_mallocz(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000364
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200365 s->stdio_file = fopen(filename, mode);
366 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000367 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200368
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200369 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200370 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
371 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200372 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200373 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
374 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200375 }
376 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000377fail:
aliguoria672b462008-11-11 21:33:36 +0000378 qemu_free(s);
379 return NULL;
380}
381
aliguori178e08a2009-04-05 19:10:55 +0000382static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000383 int64_t pos, int size)
384{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200385 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000386 return size;
387}
388
aliguori178e08a2009-04-05 19:10:55 +0000389static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000390{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200391 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000392}
393
394static int bdrv_fclose(void *opaque)
395{
aliguoria672b462008-11-11 21:33:36 +0000396 return 0;
397}
398
Christoph Hellwig45566e92009-07-10 23:11:57 +0200399static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000400{
aliguoria672b462008-11-11 21:33:36 +0000401 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200402 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
403 NULL, NULL, NULL);
404 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000405}
406
407QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
408 QEMUFileGetBufferFunc *get_buffer,
409 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400410 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200411 QEMUFileSetRateLimit *set_rate_limit,
412 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000413{
414 QEMUFile *f;
415
416 f = qemu_mallocz(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000417
418 f->opaque = opaque;
419 f->put_buffer = put_buffer;
420 f->get_buffer = get_buffer;
421 f->close = close;
422 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400423 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200424 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000425 f->is_write = 0;
426
427 return f;
428}
429
430int qemu_file_has_error(QEMUFile *f)
431{
432 return f->has_error;
433}
434
aliguori4dabe242009-04-05 19:30:51 +0000435void qemu_file_set_error(QEMUFile *f)
436{
437 f->has_error = 1;
438}
439
aliguoria672b462008-11-11 21:33:36 +0000440void qemu_fflush(QEMUFile *f)
441{
442 if (!f->put_buffer)
443 return;
444
445 if (f->is_write && f->buf_index > 0) {
446 int len;
447
448 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
449 if (len > 0)
450 f->buf_offset += f->buf_index;
451 else
452 f->has_error = 1;
453 f->buf_index = 0;
454 }
455}
456
457static void qemu_fill_buffer(QEMUFile *f)
458{
459 int len;
460
461 if (!f->get_buffer)
462 return;
463
464 if (f->is_write)
465 abort();
466
467 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
468 if (len > 0) {
469 f->buf_index = 0;
470 f->buf_size = len;
471 f->buf_offset += len;
472 } else if (len != -EAGAIN)
473 f->has_error = 1;
474}
475
476int qemu_fclose(QEMUFile *f)
477{
478 int ret = 0;
479 qemu_fflush(f);
480 if (f->close)
481 ret = f->close(f->opaque);
482 qemu_free(f);
483 return ret;
484}
485
486void qemu_file_put_notify(QEMUFile *f)
487{
488 f->put_buffer(f->opaque, NULL, 0, 0);
489}
490
491void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
492{
493 int l;
494
495 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
496 fprintf(stderr,
497 "Attempted to write to buffer while read buffer is not empty\n");
498 abort();
499 }
500
501 while (!f->has_error && size > 0) {
502 l = IO_BUF_SIZE - f->buf_index;
503 if (l > size)
504 l = size;
505 memcpy(f->buf + f->buf_index, buf, l);
506 f->is_write = 1;
507 f->buf_index += l;
508 buf += l;
509 size -= l;
510 if (f->buf_index >= IO_BUF_SIZE)
511 qemu_fflush(f);
512 }
513}
514
515void qemu_put_byte(QEMUFile *f, int v)
516{
517 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
518 fprintf(stderr,
519 "Attempted to write to buffer while read buffer is not empty\n");
520 abort();
521 }
522
523 f->buf[f->buf_index++] = v;
524 f->is_write = 1;
525 if (f->buf_index >= IO_BUF_SIZE)
526 qemu_fflush(f);
527}
528
529int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
530{
531 int size, l;
532
533 if (f->is_write)
534 abort();
535
536 size = size1;
537 while (size > 0) {
538 l = f->buf_size - f->buf_index;
539 if (l == 0) {
540 qemu_fill_buffer(f);
541 l = f->buf_size - f->buf_index;
542 if (l == 0)
543 break;
544 }
545 if (l > size)
546 l = size;
547 memcpy(buf, f->buf + f->buf_index, l);
548 f->buf_index += l;
549 buf += l;
550 size -= l;
551 }
552 return size1 - size;
553}
554
555int qemu_get_byte(QEMUFile *f)
556{
557 if (f->is_write)
558 abort();
559
560 if (f->buf_index >= f->buf_size) {
561 qemu_fill_buffer(f);
562 if (f->buf_index >= f->buf_size)
563 return 0;
564 }
565 return f->buf[f->buf_index++];
566}
567
568int64_t qemu_ftell(QEMUFile *f)
569{
570 return f->buf_offset - f->buf_size + f->buf_index;
571}
572
573int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
574{
575 if (whence == SEEK_SET) {
576 /* nothing to do */
577 } else if (whence == SEEK_CUR) {
578 pos += qemu_ftell(f);
579 } else {
580 /* SEEK_END not supported */
581 return -1;
582 }
583 if (f->put_buffer) {
584 qemu_fflush(f);
585 f->buf_offset = pos;
586 } else {
587 f->buf_offset = pos;
588 f->buf_index = 0;
589 f->buf_size = 0;
590 }
591 return pos;
592}
593
594int qemu_file_rate_limit(QEMUFile *f)
595{
596 if (f->rate_limit)
597 return f->rate_limit(f->opaque);
598
599 return 0;
600}
601
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200602size_t qemu_file_get_rate_limit(QEMUFile *f)
603{
604 if (f->get_rate_limit)
605 return f->get_rate_limit(f->opaque);
606
607 return 0;
608}
609
Glauber Costa19629532009-05-20 18:26:57 -0400610size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
611{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400612 /* any failed or completed migration keeps its state to allow probing of
613 * migration data, but has no associated file anymore */
614 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400615 return f->set_rate_limit(f->opaque, new_rate);
616
617 return 0;
618}
619
aliguoria672b462008-11-11 21:33:36 +0000620void qemu_put_be16(QEMUFile *f, unsigned int v)
621{
622 qemu_put_byte(f, v >> 8);
623 qemu_put_byte(f, v);
624}
625
626void qemu_put_be32(QEMUFile *f, unsigned int v)
627{
628 qemu_put_byte(f, v >> 24);
629 qemu_put_byte(f, v >> 16);
630 qemu_put_byte(f, v >> 8);
631 qemu_put_byte(f, v);
632}
633
634void qemu_put_be64(QEMUFile *f, uint64_t v)
635{
636 qemu_put_be32(f, v >> 32);
637 qemu_put_be32(f, v);
638}
639
640unsigned int qemu_get_be16(QEMUFile *f)
641{
642 unsigned int v;
643 v = qemu_get_byte(f) << 8;
644 v |= qemu_get_byte(f);
645 return v;
646}
647
648unsigned int qemu_get_be32(QEMUFile *f)
649{
650 unsigned int v;
651 v = qemu_get_byte(f) << 24;
652 v |= qemu_get_byte(f) << 16;
653 v |= qemu_get_byte(f) << 8;
654 v |= qemu_get_byte(f);
655 return v;
656}
657
658uint64_t qemu_get_be64(QEMUFile *f)
659{
660 uint64_t v;
661 v = (uint64_t)qemu_get_be32(f) << 32;
662 v |= qemu_get_be32(f);
663 return v;
664}
665
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200666/* 8 bit int */
667
668static int get_int8(QEMUFile *f, void *pv, size_t size)
669{
670 int8_t *v = pv;
671 qemu_get_s8s(f, v);
672 return 0;
673}
674
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200675static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200676{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200677 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200678 qemu_put_s8s(f, v);
679}
680
681const VMStateInfo vmstate_info_int8 = {
682 .name = "int8",
683 .get = get_int8,
684 .put = put_int8,
685};
686
687/* 16 bit int */
688
689static int get_int16(QEMUFile *f, void *pv, size_t size)
690{
691 int16_t *v = pv;
692 qemu_get_sbe16s(f, v);
693 return 0;
694}
695
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200696static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200697{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200698 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200699 qemu_put_sbe16s(f, v);
700}
701
702const VMStateInfo vmstate_info_int16 = {
703 .name = "int16",
704 .get = get_int16,
705 .put = put_int16,
706};
707
708/* 32 bit int */
709
710static int get_int32(QEMUFile *f, void *pv, size_t size)
711{
712 int32_t *v = pv;
713 qemu_get_sbe32s(f, v);
714 return 0;
715}
716
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200717static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200718{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200719 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200720 qemu_put_sbe32s(f, v);
721}
722
723const VMStateInfo vmstate_info_int32 = {
724 .name = "int32",
725 .get = get_int32,
726 .put = put_int32,
727};
728
Juan Quintela82501662009-08-20 19:42:32 +0200729/* 32 bit int. See that the received value is the same than the one
730 in the field */
731
732static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
733{
734 int32_t *v = pv;
735 int32_t v2;
736 qemu_get_sbe32s(f, &v2);
737
738 if (*v == v2)
739 return 0;
740 return -EINVAL;
741}
742
743const VMStateInfo vmstate_info_int32_equal = {
744 .name = "int32 equal",
745 .get = get_int32_equal,
746 .put = put_int32,
747};
748
Juan Quintela0a031e02009-08-20 19:42:37 +0200749/* 32 bit int. See that the received value is the less or the same
750 than the one in the field */
751
752static int get_int32_le(QEMUFile *f, void *pv, size_t size)
753{
754 int32_t *old = pv;
755 int32_t new;
756 qemu_get_sbe32s(f, &new);
757
758 if (*old <= new)
759 return 0;
760 return -EINVAL;
761}
762
763const VMStateInfo vmstate_info_int32_le = {
764 .name = "int32 equal",
765 .get = get_int32_le,
766 .put = put_int32,
767};
768
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200769/* 64 bit int */
770
771static int get_int64(QEMUFile *f, void *pv, size_t size)
772{
773 int64_t *v = pv;
774 qemu_get_sbe64s(f, v);
775 return 0;
776}
777
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200778static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200779{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200780 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200781 qemu_put_sbe64s(f, v);
782}
783
784const VMStateInfo vmstate_info_int64 = {
785 .name = "int64",
786 .get = get_int64,
787 .put = put_int64,
788};
789
790/* 8 bit unsigned int */
791
792static int get_uint8(QEMUFile *f, void *pv, size_t size)
793{
794 uint8_t *v = pv;
795 qemu_get_8s(f, v);
796 return 0;
797}
798
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200799static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200800{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200801 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200802 qemu_put_8s(f, v);
803}
804
805const VMStateInfo vmstate_info_uint8 = {
806 .name = "uint8",
807 .get = get_uint8,
808 .put = put_uint8,
809};
810
811/* 16 bit unsigned int */
812
813static int get_uint16(QEMUFile *f, void *pv, size_t size)
814{
815 uint16_t *v = pv;
816 qemu_get_be16s(f, v);
817 return 0;
818}
819
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200820static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200821{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200822 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200823 qemu_put_be16s(f, v);
824}
825
826const VMStateInfo vmstate_info_uint16 = {
827 .name = "uint16",
828 .get = get_uint16,
829 .put = put_uint16,
830};
831
832/* 32 bit unsigned int */
833
834static int get_uint32(QEMUFile *f, void *pv, size_t size)
835{
836 uint32_t *v = pv;
837 qemu_get_be32s(f, v);
838 return 0;
839}
840
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200841static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200842{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200843 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200844 qemu_put_be32s(f, v);
845}
846
847const VMStateInfo vmstate_info_uint32 = {
848 .name = "uint32",
849 .get = get_uint32,
850 .put = put_uint32,
851};
852
853/* 64 bit unsigned int */
854
855static int get_uint64(QEMUFile *f, void *pv, size_t size)
856{
857 uint64_t *v = pv;
858 qemu_get_be64s(f, v);
859 return 0;
860}
861
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200862static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200863{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200864 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200865 qemu_put_be64s(f, v);
866}
867
868const VMStateInfo vmstate_info_uint64 = {
869 .name = "uint64",
870 .get = get_uint64,
871 .put = put_uint64,
872};
873
Juan Quintela80cd83e2009-09-10 03:04:36 +0200874/* 8 bit int. See that the received value is the same than the one
875 in the field */
876
877static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
878{
879 uint8_t *v = pv;
880 uint8_t v2;
881 qemu_get_8s(f, &v2);
882
883 if (*v == v2)
884 return 0;
885 return -EINVAL;
886}
887
888const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +0200889 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +0200890 .get = get_uint8_equal,
891 .put = put_uint8,
892};
893
Juan Quinteladc3b83a2009-10-15 23:16:13 +0200894/* 16 bit unsigned int int. See that the received value is the same than the one
895 in the field */
896
897static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
898{
899 uint16_t *v = pv;
900 uint16_t v2;
901 qemu_get_be16s(f, &v2);
902
903 if (*v == v2)
904 return 0;
905 return -EINVAL;
906}
907
908const VMStateInfo vmstate_info_uint16_equal = {
909 .name = "uint16 equal",
910 .get = get_uint16_equal,
911 .put = put_uint16,
912};
913
Juan Quinteladde04632009-08-20 19:42:26 +0200914/* timers */
915
916static int get_timer(QEMUFile *f, void *pv, size_t size)
917{
918 QEMUTimer *v = pv;
919 qemu_get_timer(f, v);
920 return 0;
921}
922
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200923static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +0200924{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200925 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +0200926 qemu_put_timer(f, v);
927}
928
929const VMStateInfo vmstate_info_timer = {
930 .name = "timer",
931 .get = get_timer,
932 .put = put_timer,
933};
934
Juan Quintela6f67c502009-08-20 19:42:35 +0200935/* uint8_t buffers */
936
937static int get_buffer(QEMUFile *f, void *pv, size_t size)
938{
939 uint8_t *v = pv;
940 qemu_get_buffer(f, v, size);
941 return 0;
942}
943
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200944static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +0200945{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200946 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +0200947 qemu_put_buffer(f, v, size);
948}
949
950const VMStateInfo vmstate_info_buffer = {
951 .name = "buffer",
952 .get = get_buffer,
953 .put = put_buffer,
954};
955
Juan Quintela76507c72009-10-19 15:46:28 +0200956/* unused buffers: space that was used for some fields that are
957 not usefull anymore */
958
959static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
960{
Jan Kiszka21174c32009-12-02 12:36:35 +0100961 uint8_t buf[1024];
962 int block_len;
963
964 while (size > 0) {
965 block_len = MIN(sizeof(buf), size);
966 size -= block_len;
967 qemu_get_buffer(f, buf, block_len);
968 }
969 return 0;
Juan Quintela76507c72009-10-19 15:46:28 +0200970}
971
972static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
973{
Jan Kiszka21174c32009-12-02 12:36:35 +0100974 static const uint8_t buf[1024];
975 int block_len;
976
977 while (size > 0) {
978 block_len = MIN(sizeof(buf), size);
979 size -= block_len;
980 qemu_put_buffer(f, buf, block_len);
981 }
Juan Quintela76507c72009-10-19 15:46:28 +0200982}
983
984const VMStateInfo vmstate_info_unused_buffer = {
985 .name = "unused_buffer",
986 .get = get_unused_buffer,
987 .put = put_unused_buffer,
988};
989
aliguoria672b462008-11-11 21:33:36 +0000990typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000991 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +0000992 char idstr[256];
993 int instance_id;
994 int version_id;
995 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200996 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +0000997 SaveLiveStateHandler *save_live_state;
998 SaveStateHandler *save_state;
999 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001000 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +00001001 void *opaque;
aliguoria672b462008-11-11 21:33:36 +00001002} SaveStateEntry;
1003
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001004
Blue Swirl72cf2d42009-09-12 07:36:22 +00001005static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1006 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001007static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +00001008
Juan Quintela8718e992009-09-01 02:12:31 +02001009static int calculate_new_instance_id(const char *idstr)
1010{
1011 SaveStateEntry *se;
1012 int instance_id = 0;
1013
Blue Swirl72cf2d42009-09-12 07:36:22 +00001014 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001015 if (strcmp(idstr, se->idstr) == 0
1016 && instance_id <= se->instance_id) {
1017 instance_id = se->instance_id + 1;
1018 }
1019 }
1020 return instance_id;
1021}
1022
aliguoria672b462008-11-11 21:33:36 +00001023/* TODO: Individual devices generally have very little idea about the rest
1024 of the system, so instance_id should be removed/replaced.
1025 Meanwhile pass -1 as instance_id if you do not already have a clearly
1026 distinguishing id for all instances of your device class. */
1027int register_savevm_live(const char *idstr,
1028 int instance_id,
1029 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001030 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001031 SaveLiveStateHandler *save_live_state,
1032 SaveStateHandler *save_state,
1033 LoadStateHandler *load_state,
1034 void *opaque)
1035{
Juan Quintela8718e992009-09-01 02:12:31 +02001036 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001037
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001038 se = qemu_mallocz(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001039 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
aliguoria672b462008-11-11 21:33:36 +00001040 se->version_id = version_id;
1041 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001042 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001043 se->save_live_state = save_live_state;
1044 se->save_state = save_state;
1045 se->load_state = load_state;
1046 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001047 se->vmsd = NULL;
aliguoria672b462008-11-11 21:33:36 +00001048
Juan Quintela8718e992009-09-01 02:12:31 +02001049 if (instance_id == -1) {
1050 se->instance_id = calculate_new_instance_id(idstr);
1051 } else {
1052 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001053 }
Juan Quintela8718e992009-09-01 02:12:31 +02001054 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001055 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001056 return 0;
1057}
1058
1059int register_savevm(const char *idstr,
1060 int instance_id,
1061 int version_id,
1062 SaveStateHandler *save_state,
1063 LoadStateHandler *load_state,
1064 void *opaque)
1065{
1066 return register_savevm_live(idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001067 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001068}
1069
aliguori41bd13a2009-04-17 17:10:59 +00001070void unregister_savevm(const char *idstr, void *opaque)
1071{
Juan Quintela8718e992009-09-01 02:12:31 +02001072 SaveStateEntry *se, *new_se;
aliguori41bd13a2009-04-17 17:10:59 +00001073
Blue Swirl72cf2d42009-09-12 07:36:22 +00001074 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela8718e992009-09-01 02:12:31 +02001075 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001076 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Juan Quintela8718e992009-09-01 02:12:31 +02001077 qemu_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001078 }
aliguori41bd13a2009-04-17 17:10:59 +00001079 }
1080}
1081
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001082int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1083 void *opaque)
1084{
Juan Quintela8718e992009-09-01 02:12:31 +02001085 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001086
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001087 se = qemu_mallocz(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001088 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001089 se->version_id = vmsd->version_id;
1090 se->section_id = global_section_id++;
1091 se->save_live_state = NULL;
1092 se->save_state = NULL;
1093 se->load_state = NULL;
1094 se->opaque = opaque;
1095 se->vmsd = vmsd;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001096
Juan Quintela8718e992009-09-01 02:12:31 +02001097 if (instance_id == -1) {
1098 se->instance_id = calculate_new_instance_id(vmsd->name);
1099 } else {
1100 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001101 }
Juan Quintela8718e992009-09-01 02:12:31 +02001102 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001103 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001104 return 0;
1105}
1106
Juan Quintela1eb75382009-09-10 03:04:29 +02001107void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001108{
Juan Quintela1eb75382009-09-10 03:04:29 +02001109 SaveStateEntry *se, *new_se;
1110
Blue Swirl72cf2d42009-09-12 07:36:22 +00001111 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001112 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001113 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Juan Quintela1eb75382009-09-10 03:04:29 +02001114 qemu_free(se);
1115 }
1116 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001117}
1118
1119int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1120 void *opaque, int version_id)
1121{
1122 VMStateField *field = vmsd->fields;
1123
1124 if (version_id > vmsd->version_id) {
1125 return -EINVAL;
1126 }
1127 if (version_id < vmsd->minimum_version_id_old) {
1128 return -EINVAL;
1129 }
1130 if (version_id < vmsd->minimum_version_id) {
1131 return vmsd->load_state_old(f, opaque, version_id);
1132 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001133 if (vmsd->pre_load) {
1134 int ret = vmsd->pre_load(opaque);
1135 if (ret)
1136 return ret;
1137 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001138 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001139 if ((field->field_exists &&
1140 field->field_exists(opaque, version_id)) ||
1141 (!field->field_exists &&
1142 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001143 void *base_addr = opaque + field->offset;
1144 int ret, i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001145 int size = field->size;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001146
Juan Quintelae61a1e02009-12-02 12:36:38 +01001147 if (field->flags & VMS_VBUFFER) {
1148 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001149 if (field->flags & VMS_MULTIPLY) {
1150 size *= field->size;
1151 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001152 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001153 if (field->flags & VMS_ARRAY) {
1154 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001155 } else if (field->flags & VMS_VARRAY_INT32) {
1156 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001157 } else if (field->flags & VMS_VARRAY_UINT16) {
1158 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001159 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001160 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001161 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001162 }
1163 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001164 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001165
Juan Quintela19df4382009-09-29 22:48:41 +02001166 if (field->flags & VMS_ARRAY_OF_POINTER) {
1167 addr = *(void **)addr;
1168 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001169 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001170 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001171 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001172 ret = field->info->get(f, addr, size);
Juan Quintelaec245e22009-08-20 19:42:29 +02001173
1174 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001175 if (ret < 0) {
1176 return ret;
1177 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001178 }
1179 }
1180 field++;
1181 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001182 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001183 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001184 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001185 return 0;
1186}
1187
1188void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001189 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001190{
1191 VMStateField *field = vmsd->fields;
1192
Juan Quintela8fb07912009-09-10 03:04:32 +02001193 if (vmsd->pre_save) {
1194 vmsd->pre_save(opaque);
1195 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001196 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001197 if (!field->field_exists ||
1198 field->field_exists(opaque, vmsd->version_id)) {
1199 void *base_addr = opaque + field->offset;
1200 int i, n_elems = 1;
Juan Quintelae61a1e02009-12-02 12:36:38 +01001201 int size = field->size;
Juan Quinteladde04632009-08-20 19:42:26 +02001202
Juan Quintelae61a1e02009-12-02 12:36:38 +01001203 if (field->flags & VMS_VBUFFER) {
1204 size = *(int32_t *)(opaque+field->size_offset);
Juan Quintela33599e22009-12-02 12:36:43 +01001205 if (field->flags & VMS_MULTIPLY) {
1206 size *= field->size;
1207 }
Juan Quintelae61a1e02009-12-02 12:36:38 +01001208 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001209 if (field->flags & VMS_ARRAY) {
1210 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001211 } else if (field->flags & VMS_VARRAY_INT32) {
1212 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001213 } else if (field->flags & VMS_VARRAY_UINT16) {
1214 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001215 }
1216 if (field->flags & VMS_POINTER) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001217 base_addr = *(void **)base_addr + field->start;
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001218 }
1219 for (i = 0; i < n_elems; i++) {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001220 void *addr = base_addr + size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001221
Juan Quintela85953872009-12-02 12:36:37 +01001222 if (field->flags & VMS_ARRAY_OF_POINTER) {
1223 addr = *(void **)addr;
1224 }
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001225 if (field->flags & VMS_STRUCT) {
1226 vmstate_save_state(f, field->vmsd, addr);
1227 } else {
Juan Quintelae61a1e02009-12-02 12:36:38 +01001228 field->info->put(f, addr, size);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001229 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001230 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001231 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001232 field++;
1233 }
Juan Quintela8fb07912009-09-10 03:04:32 +02001234 if (vmsd->post_save) {
1235 vmsd->post_save(opaque);
1236 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001237}
1238
Juan Quintela4082be42009-08-20 19:42:24 +02001239static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1240{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001241 if (!se->vmsd) { /* Old style */
1242 return se->load_state(f, se->opaque, version_id);
1243 }
1244 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001245}
1246
1247static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1248{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001249 if (!se->vmsd) { /* Old style */
1250 se->save_state(f, se->opaque);
1251 return;
1252 }
1253 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001254}
1255
aliguoria672b462008-11-11 21:33:36 +00001256#define QEMU_VM_FILE_MAGIC 0x5145564d
1257#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1258#define QEMU_VM_FILE_VERSION 0x00000003
1259
1260#define QEMU_VM_EOF 0x00
1261#define QEMU_VM_SECTION_START 0x01
1262#define QEMU_VM_SECTION_PART 0x02
1263#define QEMU_VM_SECTION_END 0x03
1264#define QEMU_VM_SECTION_FULL 0x04
1265
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001266int qemu_savevm_state_begin(Monitor *mon, QEMUFile *f, int blk_enable,
1267 int shared)
aliguoria672b462008-11-11 21:33:36 +00001268{
1269 SaveStateEntry *se;
1270
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001271 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1272 if(se->set_params == NULL) {
1273 continue;
1274 }
1275 se->set_params(blk_enable, shared, se->opaque);
1276 }
1277
aliguoria672b462008-11-11 21:33:36 +00001278 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1279 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1280
Blue Swirl72cf2d42009-09-12 07:36:22 +00001281 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001282 int len;
1283
1284 if (se->save_live_state == NULL)
1285 continue;
1286
1287 /* Section type */
1288 qemu_put_byte(f, QEMU_VM_SECTION_START);
1289 qemu_put_be32(f, se->section_id);
1290
1291 /* ID string */
1292 len = strlen(se->idstr);
1293 qemu_put_byte(f, len);
1294 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1295
1296 qemu_put_be32(f, se->instance_id);
1297 qemu_put_be32(f, se->version_id);
1298
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001299 se->save_live_state(mon, f, QEMU_VM_SECTION_START, se->opaque);
aliguoria672b462008-11-11 21:33:36 +00001300 }
1301
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001302 if (qemu_file_has_error(f)) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001303 qemu_savevm_state_cancel(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001304 return -EIO;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001305 }
aliguoria672b462008-11-11 21:33:36 +00001306
1307 return 0;
1308}
1309
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001310int qemu_savevm_state_iterate(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001311{
1312 SaveStateEntry *se;
1313 int ret = 1;
1314
Blue Swirl72cf2d42009-09-12 07:36:22 +00001315 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001316 if (se->save_live_state == NULL)
1317 continue;
1318
1319 /* Section type */
1320 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1321 qemu_put_be32(f, se->section_id);
1322
Jan Kiszka90697be2009-12-01 15:19:55 +01001323 ret = se->save_live_state(mon, f, QEMU_VM_SECTION_PART, se->opaque);
1324 if (!ret) {
1325 /* Do not proceed to the next vmstate before this one reported
1326 completion of the current stage. This serializes the migration
1327 and reduces the probability that a faster changing state is
1328 synchronized over and over again. */
1329 break;
1330 }
aliguoria672b462008-11-11 21:33:36 +00001331 }
1332
1333 if (ret)
1334 return 1;
1335
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001336 if (qemu_file_has_error(f)) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001337 qemu_savevm_state_cancel(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001338 return -EIO;
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001339 }
aliguoria672b462008-11-11 21:33:36 +00001340
1341 return 0;
1342}
1343
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001344int qemu_savevm_state_complete(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001345{
1346 SaveStateEntry *se;
1347
Jan Kiszkaea375f92010-03-01 19:10:30 +01001348 cpu_synchronize_all_states();
1349
Blue Swirl72cf2d42009-09-12 07:36:22 +00001350 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001351 if (se->save_live_state == NULL)
1352 continue;
1353
1354 /* Section type */
1355 qemu_put_byte(f, QEMU_VM_SECTION_END);
1356 qemu_put_be32(f, se->section_id);
1357
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001358 se->save_live_state(mon, f, QEMU_VM_SECTION_END, se->opaque);
aliguoria672b462008-11-11 21:33:36 +00001359 }
1360
Blue Swirl72cf2d42009-09-12 07:36:22 +00001361 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001362 int len;
1363
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001364 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001365 continue;
1366
1367 /* Section type */
1368 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1369 qemu_put_be32(f, se->section_id);
1370
1371 /* ID string */
1372 len = strlen(se->idstr);
1373 qemu_put_byte(f, len);
1374 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1375
1376 qemu_put_be32(f, se->instance_id);
1377 qemu_put_be32(f, se->version_id);
1378
Juan Quintela4082be42009-08-20 19:42:24 +02001379 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001380 }
1381
1382 qemu_put_byte(f, QEMU_VM_EOF);
1383
1384 if (qemu_file_has_error(f))
1385 return -EIO;
1386
1387 return 0;
1388}
1389
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001390void qemu_savevm_state_cancel(Monitor *mon, QEMUFile *f)
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001391{
1392 SaveStateEntry *se;
1393
1394 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1395 if (se->save_live_state) {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001396 se->save_live_state(mon, f, -1, se->opaque);
Jan Kiszka4ec7fcc2009-11-30 18:21:21 +01001397 }
1398 }
1399}
1400
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001401static int qemu_savevm_state(Monitor *mon, QEMUFile *f)
aliguoria672b462008-11-11 21:33:36 +00001402{
1403 int saved_vm_running;
1404 int ret;
1405
1406 saved_vm_running = vm_running;
1407 vm_stop(0);
1408
1409 bdrv_flush_all();
1410
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001411 ret = qemu_savevm_state_begin(mon, f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001412 if (ret < 0)
1413 goto out;
1414
1415 do {
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001416 ret = qemu_savevm_state_iterate(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001417 if (ret < 0)
1418 goto out;
1419 } while (ret == 0);
1420
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001421 ret = qemu_savevm_state_complete(mon, f);
aliguoria672b462008-11-11 21:33:36 +00001422
1423out:
1424 if (qemu_file_has_error(f))
1425 ret = -EIO;
1426
1427 if (!ret && saved_vm_running)
1428 vm_start();
1429
1430 return ret;
1431}
1432
1433static SaveStateEntry *find_se(const char *idstr, int instance_id)
1434{
1435 SaveStateEntry *se;
1436
Blue Swirl72cf2d42009-09-12 07:36:22 +00001437 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001438 if (!strcmp(se->idstr, idstr) &&
1439 instance_id == se->instance_id)
1440 return se;
1441 }
1442 return NULL;
1443}
1444
1445typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001446 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001447 SaveStateEntry *se;
1448 int section_id;
1449 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001450} LoadStateEntry;
1451
aliguoria672b462008-11-11 21:33:36 +00001452int qemu_loadvm_state(QEMUFile *f)
1453{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001454 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1455 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001456 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001457 uint8_t section_type;
1458 unsigned int v;
1459 int ret;
1460
1461 v = qemu_get_be32(f);
1462 if (v != QEMU_VM_FILE_MAGIC)
1463 return -EINVAL;
1464
1465 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001466 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1467 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1468 return -ENOTSUP;
1469 }
aliguoria672b462008-11-11 21:33:36 +00001470 if (v != QEMU_VM_FILE_VERSION)
1471 return -ENOTSUP;
1472
1473 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1474 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001475 SaveStateEntry *se;
1476 char idstr[257];
1477 int len;
1478
1479 switch (section_type) {
1480 case QEMU_VM_SECTION_START:
1481 case QEMU_VM_SECTION_FULL:
1482 /* Read section start */
1483 section_id = qemu_get_be32(f);
1484 len = qemu_get_byte(f);
1485 qemu_get_buffer(f, (uint8_t *)idstr, len);
1486 idstr[len] = 0;
1487 instance_id = qemu_get_be32(f);
1488 version_id = qemu_get_be32(f);
1489
1490 /* Find savevm section */
1491 se = find_se(idstr, instance_id);
1492 if (se == NULL) {
1493 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1494 ret = -EINVAL;
1495 goto out;
1496 }
1497
1498 /* Validate version */
1499 if (version_id > se->version_id) {
1500 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1501 version_id, idstr, se->version_id);
1502 ret = -EINVAL;
1503 goto out;
1504 }
1505
1506 /* Add entry */
1507 le = qemu_mallocz(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001508
1509 le->se = se;
1510 le->section_id = section_id;
1511 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001512 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001513
Juan Quintela4082be42009-08-20 19:42:24 +02001514 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001515 if (ret < 0) {
1516 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1517 instance_id, idstr);
1518 goto out;
1519 }
aliguoria672b462008-11-11 21:33:36 +00001520 break;
1521 case QEMU_VM_SECTION_PART:
1522 case QEMU_VM_SECTION_END:
1523 section_id = qemu_get_be32(f);
1524
Blue Swirl72cf2d42009-09-12 07:36:22 +00001525 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001526 if (le->section_id == section_id) {
1527 break;
1528 }
1529 }
aliguoria672b462008-11-11 21:33:36 +00001530 if (le == NULL) {
1531 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1532 ret = -EINVAL;
1533 goto out;
1534 }
1535
Juan Quintela4082be42009-08-20 19:42:24 +02001536 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001537 if (ret < 0) {
1538 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1539 section_id);
1540 goto out;
1541 }
aliguoria672b462008-11-11 21:33:36 +00001542 break;
1543 default:
1544 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1545 ret = -EINVAL;
1546 goto out;
1547 }
1548 }
1549
Jan Kiszkaea375f92010-03-01 19:10:30 +01001550 cpu_synchronize_all_post_init();
1551
aliguoria672b462008-11-11 21:33:36 +00001552 ret = 0;
1553
1554out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001555 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1556 QLIST_REMOVE(le, entry);
aliguoria672b462008-11-11 21:33:36 +00001557 qemu_free(le);
1558 }
1559
1560 if (qemu_file_has_error(f))
1561 ret = -EIO;
1562
1563 return ret;
1564}
1565
1566/* device can contain snapshots */
1567static int bdrv_can_snapshot(BlockDriverState *bs)
1568{
1569 return (bs &&
1570 !bdrv_is_removable(bs) &&
1571 !bdrv_is_read_only(bs));
1572}
1573
1574/* device must be snapshots in order to have a reliable snapshot */
1575static int bdrv_has_snapshot(BlockDriverState *bs)
1576{
1577 return (bs &&
1578 !bdrv_is_removable(bs) &&
1579 !bdrv_is_read_only(bs));
1580}
1581
1582static BlockDriverState *get_bs_snapshots(void)
1583{
1584 BlockDriverState *bs;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001585 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001586
1587 if (bs_snapshots)
1588 return bs_snapshots;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001589 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001590 bs = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001591 if (bdrv_can_snapshot(bs))
1592 goto ok;
1593 }
1594 return NULL;
1595 ok:
1596 bs_snapshots = bs;
1597 return bs;
1598}
1599
1600static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1601 const char *name)
1602{
1603 QEMUSnapshotInfo *sn_tab, *sn;
1604 int nb_sns, i, ret;
1605
1606 ret = -ENOENT;
1607 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1608 if (nb_sns < 0)
1609 return ret;
1610 for(i = 0; i < nb_sns; i++) {
1611 sn = &sn_tab[i];
1612 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1613 *sn_info = *sn;
1614 ret = 0;
1615 break;
1616 }
1617 }
1618 qemu_free(sn_tab);
1619 return ret;
1620}
1621
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001622/*
1623 * Deletes snapshots of a given name in all opened images.
1624 */
1625static int del_existing_snapshots(Monitor *mon, const char *name)
1626{
1627 BlockDriverState *bs;
1628 DriveInfo *dinfo;
1629 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1630 int ret;
1631
1632 QTAILQ_FOREACH(dinfo, &drives, next) {
1633 bs = dinfo->bdrv;
1634 if (bdrv_can_snapshot(bs) &&
1635 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1636 {
1637 ret = bdrv_snapshot_delete(bs, name);
1638 if (ret < 0) {
1639 monitor_printf(mon,
1640 "Error while deleting snapshot on '%s'\n",
1641 bdrv_get_device_name(bs));
1642 return -1;
1643 }
1644 }
1645 }
1646
1647 return 0;
1648}
1649
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001650void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001651{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001652 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001653 BlockDriverState *bs, *bs1;
1654 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001655 int ret;
aliguoria672b462008-11-11 21:33:36 +00001656 QEMUFile *f;
1657 int saved_vm_running;
aliguori2d22b182008-12-11 21:06:49 +00001658 uint32_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00001659#ifdef _WIN32
1660 struct _timeb tb;
1661#else
1662 struct timeval tv;
1663#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001664 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001665
1666 bs = get_bs_snapshots();
1667 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001668 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001669 return;
1670 }
1671
1672 /* ??? Should this occur after vm_stop? */
1673 qemu_aio_flush();
1674
1675 saved_vm_running = vm_running;
1676 vm_stop(0);
1677
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001678 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00001679 if (name) {
1680 ret = bdrv_snapshot_find(bs, old_sn, name);
1681 if (ret >= 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001682 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1683 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1684 } else {
aliguoria672b462008-11-11 21:33:36 +00001685 pstrcpy(sn->name, sizeof(sn->name), name);
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001686 }
aliguoria672b462008-11-11 21:33:36 +00001687 }
1688
1689 /* fill auxiliary fields */
1690#ifdef _WIN32
1691 _ftime(&tb);
1692 sn->date_sec = tb.time;
1693 sn->date_nsec = tb.millitm * 1000000;
1694#else
1695 gettimeofday(&tv, NULL);
1696 sn->date_sec = tv.tv_sec;
1697 sn->date_nsec = tv.tv_usec * 1000;
1698#endif
1699 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1700
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001701 /* Delete old snapshots of the same name */
Marcelo Tosattif139a412010-01-20 14:26:34 -02001702 if (name && del_existing_snapshots(mon, name) < 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001703 goto the_end;
1704 }
1705
aliguoria672b462008-11-11 21:33:36 +00001706 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001707 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00001708 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001709 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00001710 goto the_end;
1711 }
Jan Kiszkaf327aa02009-11-30 18:21:21 +01001712 ret = qemu_savevm_state(mon, f);
aliguori2d22b182008-12-11 21:06:49 +00001713 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00001714 qemu_fclose(f);
1715 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001716 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00001717 goto the_end;
1718 }
1719
1720 /* create the snapshots */
1721
Blue Swirl72cf2d42009-09-12 07:36:22 +00001722 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001723 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001724 if (bdrv_has_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00001725 /* Write VM state size only to the image that contains the state */
1726 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00001727 ret = bdrv_snapshot_create(bs1, sn);
1728 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001729 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1730 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001731 }
1732 }
1733 }
1734
1735 the_end:
1736 if (saved_vm_running)
1737 vm_start();
1738}
1739
Markus Armbruster03cd4652010-02-17 16:24:10 +01001740int load_vmstate(const char *name)
aliguoria672b462008-11-11 21:33:36 +00001741{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001742 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001743 BlockDriverState *bs, *bs1;
aliguori2d22b182008-12-11 21:06:49 +00001744 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00001745 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001746 int ret;
aliguoria672b462008-11-11 21:33:36 +00001747
1748 bs = get_bs_snapshots();
1749 if (!bs) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01001750 qemu_error("No block device supports snapshots\n");
Juan Quintela05f24012009-08-20 19:42:22 +02001751 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001752 }
1753
1754 /* Flush all IO requests so they don't interfere with the new state. */
1755 qemu_aio_flush();
1756
Blue Swirl72cf2d42009-09-12 07:36:22 +00001757 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001758 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001759 if (bdrv_has_snapshot(bs1)) {
1760 ret = bdrv_snapshot_goto(bs1, name);
1761 if (ret < 0) {
1762 if (bs != bs1)
Markus Armbruster03cd4652010-02-17 16:24:10 +01001763 qemu_error("Warning: ");
aliguoria672b462008-11-11 21:33:36 +00001764 switch(ret) {
1765 case -ENOTSUP:
Markus Armbruster03cd4652010-02-17 16:24:10 +01001766 qemu_error("Snapshots not supported on device '%s'\n",
1767 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001768 break;
1769 case -ENOENT:
Markus Armbruster03cd4652010-02-17 16:24:10 +01001770 qemu_error("Could not find snapshot '%s' on device '%s'\n",
1771 name, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001772 break;
1773 default:
Markus Armbruster03cd4652010-02-17 16:24:10 +01001774 qemu_error("Error %d while activating snapshot on '%s'\n",
1775 ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001776 break;
1777 }
1778 /* fatal on snapshot block device */
1779 if (bs == bs1)
Juan Quintela05f24012009-08-20 19:42:22 +02001780 return 0;
aliguoria672b462008-11-11 21:33:36 +00001781 }
1782 }
1783 }
1784
aliguori2d22b182008-12-11 21:06:49 +00001785 /* Don't even try to load empty VM states */
1786 ret = bdrv_snapshot_find(bs, &sn, name);
1787 if ((ret >= 0) && (sn.vm_state_size == 0))
Juan Quintela05f24012009-08-20 19:42:22 +02001788 return -EINVAL;
aliguori2d22b182008-12-11 21:06:49 +00001789
aliguoria672b462008-11-11 21:33:36 +00001790 /* restore the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001791 f = qemu_fopen_bdrv(bs, 0);
aliguoria672b462008-11-11 21:33:36 +00001792 if (!f) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01001793 qemu_error("Could not open VM state file\n");
Juan Quintela05f24012009-08-20 19:42:22 +02001794 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001795 }
1796 ret = qemu_loadvm_state(f);
1797 qemu_fclose(f);
1798 if (ret < 0) {
Markus Armbruster03cd4652010-02-17 16:24:10 +01001799 qemu_error("Error %d while loading VM state\n", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02001800 return ret;
aliguoria672b462008-11-11 21:33:36 +00001801 }
Juan Quintela05f24012009-08-20 19:42:22 +02001802 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02001803}
1804
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001805void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001806{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001807 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001808 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001809 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001810 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001811
1812 bs = get_bs_snapshots();
1813 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001814 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001815 return;
1816 }
1817
Blue Swirl72cf2d42009-09-12 07:36:22 +00001818 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001819 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001820 if (bdrv_has_snapshot(bs1)) {
1821 ret = bdrv_snapshot_delete(bs1, name);
1822 if (ret < 0) {
1823 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00001824 monitor_printf(mon,
1825 "Snapshots not supported on device '%s'\n",
1826 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001827 else
aliguori376253e2009-03-05 23:01:23 +00001828 monitor_printf(mon, "Error %d while deleting snapshot on "
1829 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001830 }
1831 }
1832 }
1833}
1834
aliguori376253e2009-03-05 23:01:23 +00001835void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00001836{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001837 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001838 BlockDriverState *bs, *bs1;
1839 QEMUSnapshotInfo *sn_tab, *sn;
1840 int nb_sns, i;
1841 char buf[256];
1842
1843 bs = get_bs_snapshots();
1844 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001845 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001846 return;
1847 }
aliguori376253e2009-03-05 23:01:23 +00001848 monitor_printf(mon, "Snapshot devices:");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001849 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001850 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001851 if (bdrv_has_snapshot(bs1)) {
1852 if (bs == bs1)
aliguori376253e2009-03-05 23:01:23 +00001853 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001854 }
1855 }
aliguori376253e2009-03-05 23:01:23 +00001856 monitor_printf(mon, "\n");
aliguoria672b462008-11-11 21:33:36 +00001857
1858 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1859 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00001860 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00001861 return;
1862 }
aliguori376253e2009-03-05 23:01:23 +00001863 monitor_printf(mon, "Snapshot list (from %s):\n",
1864 bdrv_get_device_name(bs));
1865 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
aliguoria672b462008-11-11 21:33:36 +00001866 for(i = 0; i < nb_sns; i++) {
1867 sn = &sn_tab[i];
aliguori376253e2009-03-05 23:01:23 +00001868 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
aliguoria672b462008-11-11 21:33:36 +00001869 }
1870 qemu_free(sn_tab);
1871}