blob: d6da050f367d700e83c8ac5708ef38c256625084 [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);
342 fwrite(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000343 return size;
344}
345
346static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
347{
348 QEMUFileStdio *s = opaque;
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200349 fseek(s->stdio_file, pos, SEEK_SET);
350 return fread(buf, 1, size, s->stdio_file);
aliguoria672b462008-11-11 21:33:36 +0000351}
352
353QEMUFile *qemu_fopen(const char *filename, const char *mode)
354{
355 QEMUFileStdio *s;
356
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200357 if (mode == NULL ||
358 (mode[0] != 'r' && mode[0] != 'w') ||
359 mode[1] != 'b' || mode[2] != 0) {
360 fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
361 return NULL;
362 }
363
aliguoria672b462008-11-11 21:33:36 +0000364 s = qemu_mallocz(sizeof(QEMUFileStdio));
aliguoria672b462008-11-11 21:33:36 +0000365
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200366 s->stdio_file = fopen(filename, mode);
367 if (!s->stdio_file)
aliguoria672b462008-11-11 21:33:36 +0000368 goto fail;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200369
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200370 if(mode[0] == 'w') {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200371 s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose,
372 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200373 } else {
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200374 s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose,
375 NULL, NULL, NULL);
Paolo Bonzini7f79dd22009-08-12 14:17:35 +0200376 }
377 return s->file;
aliguoria672b462008-11-11 21:33:36 +0000378fail:
aliguoria672b462008-11-11 21:33:36 +0000379 qemu_free(s);
380 return NULL;
381}
382
aliguori178e08a2009-04-05 19:10:55 +0000383static int block_put_buffer(void *opaque, const uint8_t *buf,
aliguoria672b462008-11-11 21:33:36 +0000384 int64_t pos, int size)
385{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200386 bdrv_save_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000387 return size;
388}
389
aliguori178e08a2009-04-05 19:10:55 +0000390static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
aliguoria672b462008-11-11 21:33:36 +0000391{
Christoph Hellwig45566e92009-07-10 23:11:57 +0200392 return bdrv_load_vmstate(opaque, buf, pos, size);
aliguoria672b462008-11-11 21:33:36 +0000393}
394
395static int bdrv_fclose(void *opaque)
396{
aliguoria672b462008-11-11 21:33:36 +0000397 return 0;
398}
399
Christoph Hellwig45566e92009-07-10 23:11:57 +0200400static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
aliguoria672b462008-11-11 21:33:36 +0000401{
aliguoria672b462008-11-11 21:33:36 +0000402 if (is_writable)
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200403 return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose,
404 NULL, NULL, NULL);
405 return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
aliguoria672b462008-11-11 21:33:36 +0000406}
407
408QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
409 QEMUFileGetBufferFunc *get_buffer,
410 QEMUFileCloseFunc *close,
Glauber Costa19629532009-05-20 18:26:57 -0400411 QEMUFileRateLimit *rate_limit,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200412 QEMUFileSetRateLimit *set_rate_limit,
413 QEMUFileGetRateLimit *get_rate_limit)
aliguoria672b462008-11-11 21:33:36 +0000414{
415 QEMUFile *f;
416
417 f = qemu_mallocz(sizeof(QEMUFile));
aliguoria672b462008-11-11 21:33:36 +0000418
419 f->opaque = opaque;
420 f->put_buffer = put_buffer;
421 f->get_buffer = get_buffer;
422 f->close = close;
423 f->rate_limit = rate_limit;
Glauber Costa19629532009-05-20 18:26:57 -0400424 f->set_rate_limit = set_rate_limit;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200425 f->get_rate_limit = get_rate_limit;
aliguoria672b462008-11-11 21:33:36 +0000426 f->is_write = 0;
427
428 return f;
429}
430
431int qemu_file_has_error(QEMUFile *f)
432{
433 return f->has_error;
434}
435
aliguori4dabe242009-04-05 19:30:51 +0000436void qemu_file_set_error(QEMUFile *f)
437{
438 f->has_error = 1;
439}
440
aliguoria672b462008-11-11 21:33:36 +0000441void qemu_fflush(QEMUFile *f)
442{
443 if (!f->put_buffer)
444 return;
445
446 if (f->is_write && f->buf_index > 0) {
447 int len;
448
449 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
450 if (len > 0)
451 f->buf_offset += f->buf_index;
452 else
453 f->has_error = 1;
454 f->buf_index = 0;
455 }
456}
457
458static void qemu_fill_buffer(QEMUFile *f)
459{
460 int len;
461
462 if (!f->get_buffer)
463 return;
464
465 if (f->is_write)
466 abort();
467
468 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
469 if (len > 0) {
470 f->buf_index = 0;
471 f->buf_size = len;
472 f->buf_offset += len;
473 } else if (len != -EAGAIN)
474 f->has_error = 1;
475}
476
477int qemu_fclose(QEMUFile *f)
478{
479 int ret = 0;
480 qemu_fflush(f);
481 if (f->close)
482 ret = f->close(f->opaque);
483 qemu_free(f);
484 return ret;
485}
486
487void qemu_file_put_notify(QEMUFile *f)
488{
489 f->put_buffer(f->opaque, NULL, 0, 0);
490}
491
492void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
493{
494 int l;
495
496 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
497 fprintf(stderr,
498 "Attempted to write to buffer while read buffer is not empty\n");
499 abort();
500 }
501
502 while (!f->has_error && size > 0) {
503 l = IO_BUF_SIZE - f->buf_index;
504 if (l > size)
505 l = size;
506 memcpy(f->buf + f->buf_index, buf, l);
507 f->is_write = 1;
508 f->buf_index += l;
509 buf += l;
510 size -= l;
511 if (f->buf_index >= IO_BUF_SIZE)
512 qemu_fflush(f);
513 }
514}
515
516void qemu_put_byte(QEMUFile *f, int v)
517{
518 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
519 fprintf(stderr,
520 "Attempted to write to buffer while read buffer is not empty\n");
521 abort();
522 }
523
524 f->buf[f->buf_index++] = v;
525 f->is_write = 1;
526 if (f->buf_index >= IO_BUF_SIZE)
527 qemu_fflush(f);
528}
529
530int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
531{
532 int size, l;
533
534 if (f->is_write)
535 abort();
536
537 size = size1;
538 while (size > 0) {
539 l = f->buf_size - f->buf_index;
540 if (l == 0) {
541 qemu_fill_buffer(f);
542 l = f->buf_size - f->buf_index;
543 if (l == 0)
544 break;
545 }
546 if (l > size)
547 l = size;
548 memcpy(buf, f->buf + f->buf_index, l);
549 f->buf_index += l;
550 buf += l;
551 size -= l;
552 }
553 return size1 - size;
554}
555
556int qemu_get_byte(QEMUFile *f)
557{
558 if (f->is_write)
559 abort();
560
561 if (f->buf_index >= f->buf_size) {
562 qemu_fill_buffer(f);
563 if (f->buf_index >= f->buf_size)
564 return 0;
565 }
566 return f->buf[f->buf_index++];
567}
568
569int64_t qemu_ftell(QEMUFile *f)
570{
571 return f->buf_offset - f->buf_size + f->buf_index;
572}
573
574int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
575{
576 if (whence == SEEK_SET) {
577 /* nothing to do */
578 } else if (whence == SEEK_CUR) {
579 pos += qemu_ftell(f);
580 } else {
581 /* SEEK_END not supported */
582 return -1;
583 }
584 if (f->put_buffer) {
585 qemu_fflush(f);
586 f->buf_offset = pos;
587 } else {
588 f->buf_offset = pos;
589 f->buf_index = 0;
590 f->buf_size = 0;
591 }
592 return pos;
593}
594
595int qemu_file_rate_limit(QEMUFile *f)
596{
597 if (f->rate_limit)
598 return f->rate_limit(f->opaque);
599
600 return 0;
601}
602
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200603size_t qemu_file_get_rate_limit(QEMUFile *f)
604{
605 if (f->get_rate_limit)
606 return f->get_rate_limit(f->opaque);
607
608 return 0;
609}
610
Glauber Costa19629532009-05-20 18:26:57 -0400611size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
612{
Glauber Costa0bb05ea2009-07-14 18:26:51 -0400613 /* any failed or completed migration keeps its state to allow probing of
614 * migration data, but has no associated file anymore */
615 if (f && f->set_rate_limit)
Glauber Costa19629532009-05-20 18:26:57 -0400616 return f->set_rate_limit(f->opaque, new_rate);
617
618 return 0;
619}
620
aliguoria672b462008-11-11 21:33:36 +0000621void qemu_put_be16(QEMUFile *f, unsigned int v)
622{
623 qemu_put_byte(f, v >> 8);
624 qemu_put_byte(f, v);
625}
626
627void qemu_put_be32(QEMUFile *f, unsigned int v)
628{
629 qemu_put_byte(f, v >> 24);
630 qemu_put_byte(f, v >> 16);
631 qemu_put_byte(f, v >> 8);
632 qemu_put_byte(f, v);
633}
634
635void qemu_put_be64(QEMUFile *f, uint64_t v)
636{
637 qemu_put_be32(f, v >> 32);
638 qemu_put_be32(f, v);
639}
640
641unsigned int qemu_get_be16(QEMUFile *f)
642{
643 unsigned int v;
644 v = qemu_get_byte(f) << 8;
645 v |= qemu_get_byte(f);
646 return v;
647}
648
649unsigned int qemu_get_be32(QEMUFile *f)
650{
651 unsigned int v;
652 v = qemu_get_byte(f) << 24;
653 v |= qemu_get_byte(f) << 16;
654 v |= qemu_get_byte(f) << 8;
655 v |= qemu_get_byte(f);
656 return v;
657}
658
659uint64_t qemu_get_be64(QEMUFile *f)
660{
661 uint64_t v;
662 v = (uint64_t)qemu_get_be32(f) << 32;
663 v |= qemu_get_be32(f);
664 return v;
665}
666
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200667/* 8 bit int */
668
669static int get_int8(QEMUFile *f, void *pv, size_t size)
670{
671 int8_t *v = pv;
672 qemu_get_s8s(f, v);
673 return 0;
674}
675
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200676static void put_int8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200677{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200678 int8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200679 qemu_put_s8s(f, v);
680}
681
682const VMStateInfo vmstate_info_int8 = {
683 .name = "int8",
684 .get = get_int8,
685 .put = put_int8,
686};
687
688/* 16 bit int */
689
690static int get_int16(QEMUFile *f, void *pv, size_t size)
691{
692 int16_t *v = pv;
693 qemu_get_sbe16s(f, v);
694 return 0;
695}
696
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200697static void put_int16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200698{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200699 int16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200700 qemu_put_sbe16s(f, v);
701}
702
703const VMStateInfo vmstate_info_int16 = {
704 .name = "int16",
705 .get = get_int16,
706 .put = put_int16,
707};
708
709/* 32 bit int */
710
711static int get_int32(QEMUFile *f, void *pv, size_t size)
712{
713 int32_t *v = pv;
714 qemu_get_sbe32s(f, v);
715 return 0;
716}
717
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200718static void put_int32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200719{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200720 int32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200721 qemu_put_sbe32s(f, v);
722}
723
724const VMStateInfo vmstate_info_int32 = {
725 .name = "int32",
726 .get = get_int32,
727 .put = put_int32,
728};
729
Juan Quintela82501662009-08-20 19:42:32 +0200730/* 32 bit int. See that the received value is the same than the one
731 in the field */
732
733static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
734{
735 int32_t *v = pv;
736 int32_t v2;
737 qemu_get_sbe32s(f, &v2);
738
739 if (*v == v2)
740 return 0;
741 return -EINVAL;
742}
743
744const VMStateInfo vmstate_info_int32_equal = {
745 .name = "int32 equal",
746 .get = get_int32_equal,
747 .put = put_int32,
748};
749
Juan Quintela0a031e02009-08-20 19:42:37 +0200750/* 32 bit int. See that the received value is the less or the same
751 than the one in the field */
752
753static int get_int32_le(QEMUFile *f, void *pv, size_t size)
754{
755 int32_t *old = pv;
756 int32_t new;
757 qemu_get_sbe32s(f, &new);
758
759 if (*old <= new)
760 return 0;
761 return -EINVAL;
762}
763
764const VMStateInfo vmstate_info_int32_le = {
765 .name = "int32 equal",
766 .get = get_int32_le,
767 .put = put_int32,
768};
769
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200770/* 64 bit int */
771
772static int get_int64(QEMUFile *f, void *pv, size_t size)
773{
774 int64_t *v = pv;
775 qemu_get_sbe64s(f, v);
776 return 0;
777}
778
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200779static void put_int64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200780{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200781 int64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200782 qemu_put_sbe64s(f, v);
783}
784
785const VMStateInfo vmstate_info_int64 = {
786 .name = "int64",
787 .get = get_int64,
788 .put = put_int64,
789};
790
791/* 8 bit unsigned int */
792
793static int get_uint8(QEMUFile *f, void *pv, size_t size)
794{
795 uint8_t *v = pv;
796 qemu_get_8s(f, v);
797 return 0;
798}
799
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200800static void put_uint8(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200801{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200802 uint8_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200803 qemu_put_8s(f, v);
804}
805
806const VMStateInfo vmstate_info_uint8 = {
807 .name = "uint8",
808 .get = get_uint8,
809 .put = put_uint8,
810};
811
812/* 16 bit unsigned int */
813
814static int get_uint16(QEMUFile *f, void *pv, size_t size)
815{
816 uint16_t *v = pv;
817 qemu_get_be16s(f, v);
818 return 0;
819}
820
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200821static void put_uint16(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200822{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200823 uint16_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200824 qemu_put_be16s(f, v);
825}
826
827const VMStateInfo vmstate_info_uint16 = {
828 .name = "uint16",
829 .get = get_uint16,
830 .put = put_uint16,
831};
832
833/* 32 bit unsigned int */
834
835static int get_uint32(QEMUFile *f, void *pv, size_t size)
836{
837 uint32_t *v = pv;
838 qemu_get_be32s(f, v);
839 return 0;
840}
841
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200842static void put_uint32(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200843{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200844 uint32_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200845 qemu_put_be32s(f, v);
846}
847
848const VMStateInfo vmstate_info_uint32 = {
849 .name = "uint32",
850 .get = get_uint32,
851 .put = put_uint32,
852};
853
854/* 64 bit unsigned int */
855
856static int get_uint64(QEMUFile *f, void *pv, size_t size)
857{
858 uint64_t *v = pv;
859 qemu_get_be64s(f, v);
860 return 0;
861}
862
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200863static void put_uint64(QEMUFile *f, void *pv, size_t size)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200864{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200865 uint64_t *v = pv;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200866 qemu_put_be64s(f, v);
867}
868
869const VMStateInfo vmstate_info_uint64 = {
870 .name = "uint64",
871 .get = get_uint64,
872 .put = put_uint64,
873};
874
Juan Quintela80cd83e2009-09-10 03:04:36 +0200875/* 8 bit int. See that the received value is the same than the one
876 in the field */
877
878static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
879{
880 uint8_t *v = pv;
881 uint8_t v2;
882 qemu_get_8s(f, &v2);
883
884 if (*v == v2)
885 return 0;
886 return -EINVAL;
887}
888
889const VMStateInfo vmstate_info_uint8_equal = {
Juan Quintelaaa1cce62009-10-15 19:16:06 +0200890 .name = "uint8 equal",
Juan Quintela80cd83e2009-09-10 03:04:36 +0200891 .get = get_uint8_equal,
892 .put = put_uint8,
893};
894
Juan Quinteladc3b83a2009-10-15 23:16:13 +0200895/* 16 bit unsigned int int. See that the received value is the same than the one
896 in the field */
897
898static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
899{
900 uint16_t *v = pv;
901 uint16_t v2;
902 qemu_get_be16s(f, &v2);
903
904 if (*v == v2)
905 return 0;
906 return -EINVAL;
907}
908
909const VMStateInfo vmstate_info_uint16_equal = {
910 .name = "uint16 equal",
911 .get = get_uint16_equal,
912 .put = put_uint16,
913};
914
Juan Quinteladde04632009-08-20 19:42:26 +0200915/* timers */
916
917static int get_timer(QEMUFile *f, void *pv, size_t size)
918{
919 QEMUTimer *v = pv;
920 qemu_get_timer(f, v);
921 return 0;
922}
923
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200924static void put_timer(QEMUFile *f, void *pv, size_t size)
Juan Quinteladde04632009-08-20 19:42:26 +0200925{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200926 QEMUTimer *v = pv;
Juan Quinteladde04632009-08-20 19:42:26 +0200927 qemu_put_timer(f, v);
928}
929
930const VMStateInfo vmstate_info_timer = {
931 .name = "timer",
932 .get = get_timer,
933 .put = put_timer,
934};
935
Juan Quintela6f67c502009-08-20 19:42:35 +0200936/* uint8_t buffers */
937
938static int get_buffer(QEMUFile *f, void *pv, size_t size)
939{
940 uint8_t *v = pv;
941 qemu_get_buffer(f, v, size);
942 return 0;
943}
944
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200945static void put_buffer(QEMUFile *f, void *pv, size_t size)
Juan Quintela6f67c502009-08-20 19:42:35 +0200946{
Juan Quintela84e2e3e2009-09-29 22:48:20 +0200947 uint8_t *v = pv;
Juan Quintela6f67c502009-08-20 19:42:35 +0200948 qemu_put_buffer(f, v, size);
949}
950
951const VMStateInfo vmstate_info_buffer = {
952 .name = "buffer",
953 .get = get_buffer,
954 .put = put_buffer,
955};
956
Juan Quintela76507c72009-10-19 15:46:28 +0200957/* unused buffers: space that was used for some fields that are
958 not usefull anymore */
959
960static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
961{
962 qemu_fseek(f, size, SEEK_CUR);
963 return 0;
964}
965
966static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
967{
968 qemu_fseek(f, size, SEEK_CUR);
969}
970
971const VMStateInfo vmstate_info_unused_buffer = {
972 .name = "unused_buffer",
973 .get = get_unused_buffer,
974 .put = put_unused_buffer,
975};
976
aliguoria672b462008-11-11 21:33:36 +0000977typedef struct SaveStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +0000978 QTAILQ_ENTRY(SaveStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +0000979 char idstr[256];
980 int instance_id;
981 int version_id;
982 int section_id;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200983 SaveSetParamsHandler *set_params;
aliguoria672b462008-11-11 21:33:36 +0000984 SaveLiveStateHandler *save_live_state;
985 SaveStateHandler *save_state;
986 LoadStateHandler *load_state;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200987 const VMStateDescription *vmsd;
aliguoria672b462008-11-11 21:33:36 +0000988 void *opaque;
aliguoria672b462008-11-11 21:33:36 +0000989} SaveStateEntry;
990
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +0200991
Blue Swirl72cf2d42009-09-12 07:36:22 +0000992static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
993 QTAILQ_HEAD_INITIALIZER(savevm_handlers);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +0200994static int global_section_id;
aliguoria672b462008-11-11 21:33:36 +0000995
Juan Quintela8718e992009-09-01 02:12:31 +0200996static int calculate_new_instance_id(const char *idstr)
997{
998 SaveStateEntry *se;
999 int instance_id = 0;
1000
Blue Swirl72cf2d42009-09-12 07:36:22 +00001001 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
Juan Quintela8718e992009-09-01 02:12:31 +02001002 if (strcmp(idstr, se->idstr) == 0
1003 && instance_id <= se->instance_id) {
1004 instance_id = se->instance_id + 1;
1005 }
1006 }
1007 return instance_id;
1008}
1009
aliguoria672b462008-11-11 21:33:36 +00001010/* TODO: Individual devices generally have very little idea about the rest
1011 of the system, so instance_id should be removed/replaced.
1012 Meanwhile pass -1 as instance_id if you do not already have a clearly
1013 distinguishing id for all instances of your device class. */
1014int register_savevm_live(const char *idstr,
1015 int instance_id,
1016 int version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001017 SaveSetParamsHandler *set_params,
aliguoria672b462008-11-11 21:33:36 +00001018 SaveLiveStateHandler *save_live_state,
1019 SaveStateHandler *save_state,
1020 LoadStateHandler *load_state,
1021 void *opaque)
1022{
Juan Quintela8718e992009-09-01 02:12:31 +02001023 SaveStateEntry *se;
aliguoria672b462008-11-11 21:33:36 +00001024
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001025 se = qemu_mallocz(sizeof(SaveStateEntry));
aliguoria672b462008-11-11 21:33:36 +00001026 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
aliguoria672b462008-11-11 21:33:36 +00001027 se->version_id = version_id;
1028 se->section_id = global_section_id++;
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001029 se->set_params = set_params;
aliguoria672b462008-11-11 21:33:36 +00001030 se->save_live_state = save_live_state;
1031 se->save_state = save_state;
1032 se->load_state = load_state;
1033 se->opaque = opaque;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001034 se->vmsd = NULL;
aliguoria672b462008-11-11 21:33:36 +00001035
Juan Quintela8718e992009-09-01 02:12:31 +02001036 if (instance_id == -1) {
1037 se->instance_id = calculate_new_instance_id(idstr);
1038 } else {
1039 se->instance_id = instance_id;
aliguoria672b462008-11-11 21:33:36 +00001040 }
Juan Quintela8718e992009-09-01 02:12:31 +02001041 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001042 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
aliguoria672b462008-11-11 21:33:36 +00001043 return 0;
1044}
1045
1046int register_savevm(const char *idstr,
1047 int instance_id,
1048 int version_id,
1049 SaveStateHandler *save_state,
1050 LoadStateHandler *load_state,
1051 void *opaque)
1052{
1053 return register_savevm_live(idstr, instance_id, version_id,
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001054 NULL, NULL, save_state, load_state, opaque);
aliguoria672b462008-11-11 21:33:36 +00001055}
1056
aliguori41bd13a2009-04-17 17:10:59 +00001057void unregister_savevm(const char *idstr, void *opaque)
1058{
Juan Quintela8718e992009-09-01 02:12:31 +02001059 SaveStateEntry *se, *new_se;
aliguori41bd13a2009-04-17 17:10:59 +00001060
Blue Swirl72cf2d42009-09-12 07:36:22 +00001061 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela8718e992009-09-01 02:12:31 +02001062 if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001063 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Juan Quintela8718e992009-09-01 02:12:31 +02001064 qemu_free(se);
aliguori41bd13a2009-04-17 17:10:59 +00001065 }
aliguori41bd13a2009-04-17 17:10:59 +00001066 }
1067}
1068
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001069int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1070 void *opaque)
1071{
Juan Quintela8718e992009-09-01 02:12:31 +02001072 SaveStateEntry *se;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001073
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001074 se = qemu_mallocz(sizeof(SaveStateEntry));
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001075 pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001076 se->version_id = vmsd->version_id;
1077 se->section_id = global_section_id++;
1078 se->save_live_state = NULL;
1079 se->save_state = NULL;
1080 se->load_state = NULL;
1081 se->opaque = opaque;
1082 se->vmsd = vmsd;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001083
Juan Quintela8718e992009-09-01 02:12:31 +02001084 if (instance_id == -1) {
1085 se->instance_id = calculate_new_instance_id(vmsd->name);
1086 } else {
1087 se->instance_id = instance_id;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001088 }
Juan Quintela8718e992009-09-01 02:12:31 +02001089 /* add at the end of list */
Blue Swirl72cf2d42009-09-12 07:36:22 +00001090 QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001091 return 0;
1092}
1093
Juan Quintela1eb75382009-09-10 03:04:29 +02001094void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001095{
Juan Quintela1eb75382009-09-10 03:04:29 +02001096 SaveStateEntry *se, *new_se;
1097
Blue Swirl72cf2d42009-09-12 07:36:22 +00001098 QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
Juan Quintela1eb75382009-09-10 03:04:29 +02001099 if (se->vmsd == vmsd && se->opaque == opaque) {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001100 QTAILQ_REMOVE(&savevm_handlers, se, entry);
Juan Quintela1eb75382009-09-10 03:04:29 +02001101 qemu_free(se);
1102 }
1103 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001104}
1105
1106int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1107 void *opaque, int version_id)
1108{
1109 VMStateField *field = vmsd->fields;
1110
1111 if (version_id > vmsd->version_id) {
1112 return -EINVAL;
1113 }
1114 if (version_id < vmsd->minimum_version_id_old) {
1115 return -EINVAL;
1116 }
1117 if (version_id < vmsd->minimum_version_id) {
1118 return vmsd->load_state_old(f, opaque, version_id);
1119 }
Juan Quintelafd4d52d2009-09-10 03:04:31 +02001120 if (vmsd->pre_load) {
1121 int ret = vmsd->pre_load(opaque);
1122 if (ret)
1123 return ret;
1124 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001125 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001126 if ((field->field_exists &&
1127 field->field_exists(opaque, version_id)) ||
1128 (!field->field_exists &&
1129 field->version_id <= version_id)) {
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001130 void *base_addr = opaque + field->offset;
1131 int ret, i, n_elems = 1;
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001132
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001133 if (field->flags & VMS_ARRAY) {
1134 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001135 } else if (field->flags & VMS_VARRAY_INT32) {
1136 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001137 } else if (field->flags & VMS_VARRAY_UINT16) {
1138 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quinteladde04632009-08-20 19:42:26 +02001139 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001140 if (field->flags & VMS_POINTER) {
1141 base_addr = *(void **)base_addr;
1142 }
1143 for (i = 0; i < n_elems; i++) {
1144 void *addr = base_addr + field->size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001145
Juan Quintela19df4382009-09-29 22:48:41 +02001146 if (field->flags & VMS_ARRAY_OF_POINTER) {
1147 addr = *(void **)addr;
1148 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001149 if (field->flags & VMS_STRUCT) {
Juan Quintelafa3aad22009-08-28 15:28:25 +02001150 ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
Juan Quintelaec245e22009-08-20 19:42:29 +02001151 } else {
1152 ret = field->info->get(f, addr, field->size);
1153
1154 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001155 if (ret < 0) {
1156 return ret;
1157 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001158 }
1159 }
1160 field++;
1161 }
Juan Quintela752ff2f2009-09-10 03:04:30 +02001162 if (vmsd->post_load) {
Juan Quintelae59fb372009-09-29 22:48:21 +02001163 return vmsd->post_load(opaque, version_id);
Juan Quintela752ff2f2009-09-10 03:04:30 +02001164 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001165 return 0;
1166}
1167
1168void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
Juan Quintela84e2e3e2009-09-29 22:48:20 +02001169 void *opaque)
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001170{
1171 VMStateField *field = vmsd->fields;
1172
Juan Quintela8fb07912009-09-10 03:04:32 +02001173 if (vmsd->pre_save) {
1174 vmsd->pre_save(opaque);
1175 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001176 while(field->name) {
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001177 if (!field->field_exists ||
1178 field->field_exists(opaque, vmsd->version_id)) {
1179 void *base_addr = opaque + field->offset;
1180 int i, n_elems = 1;
Juan Quinteladde04632009-08-20 19:42:26 +02001181
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001182 if (field->flags & VMS_ARRAY) {
1183 n_elems = field->num;
Juan Quintelad6698282009-10-16 11:27:17 +02001184 } else if (field->flags & VMS_VARRAY_INT32) {
1185 n_elems = *(int32_t *)(opaque+field->num_offset);
Juan Quintelabdb49412009-10-16 11:35:18 +02001186 } else if (field->flags & VMS_VARRAY_UINT16) {
1187 n_elems = *(uint16_t *)(opaque+field->num_offset);
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001188 }
1189 if (field->flags & VMS_POINTER) {
1190 base_addr = *(void **)base_addr;
1191 }
1192 for (i = 0; i < n_elems; i++) {
1193 void *addr = base_addr + field->size * i;
Juan Quintelaec245e22009-08-20 19:42:29 +02001194
Juan Quintelaf11f6a52009-09-29 22:49:07 +02001195 if (field->flags & VMS_STRUCT) {
1196 vmstate_save_state(f, field->vmsd, addr);
1197 } else {
1198 field->info->put(f, addr, field->size);
1199 }
Juan Quintelaec245e22009-08-20 19:42:29 +02001200 }
Juan Quintelaf752a6a2009-08-20 19:42:27 +02001201 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001202 field++;
1203 }
Juan Quintela8fb07912009-09-10 03:04:32 +02001204 if (vmsd->post_save) {
1205 vmsd->post_save(opaque);
1206 }
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001207}
1208
Juan Quintela4082be42009-08-20 19:42:24 +02001209static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1210{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001211 if (!se->vmsd) { /* Old style */
1212 return se->load_state(f, se->opaque, version_id);
1213 }
1214 return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
Juan Quintela4082be42009-08-20 19:42:24 +02001215}
1216
1217static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1218{
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001219 if (!se->vmsd) { /* Old style */
1220 se->save_state(f, se->opaque);
1221 return;
1222 }
1223 vmstate_save_state(f,se->vmsd, se->opaque);
Juan Quintela4082be42009-08-20 19:42:24 +02001224}
1225
aliguoria672b462008-11-11 21:33:36 +00001226#define QEMU_VM_FILE_MAGIC 0x5145564d
1227#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
1228#define QEMU_VM_FILE_VERSION 0x00000003
1229
1230#define QEMU_VM_EOF 0x00
1231#define QEMU_VM_SECTION_START 0x01
1232#define QEMU_VM_SECTION_PART 0x02
1233#define QEMU_VM_SECTION_END 0x03
1234#define QEMU_VM_SECTION_FULL 0x04
1235
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001236int qemu_savevm_state_begin(QEMUFile *f, int blk_enable, int shared)
aliguoria672b462008-11-11 21:33:36 +00001237{
1238 SaveStateEntry *se;
1239
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001240 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1241 if(se->set_params == NULL) {
1242 continue;
1243 }
1244 se->set_params(blk_enable, shared, se->opaque);
1245 }
1246
aliguoria672b462008-11-11 21:33:36 +00001247 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1248 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1249
Blue Swirl72cf2d42009-09-12 07:36:22 +00001250 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001251 int len;
1252
1253 if (se->save_live_state == NULL)
1254 continue;
1255
1256 /* Section type */
1257 qemu_put_byte(f, QEMU_VM_SECTION_START);
1258 qemu_put_be32(f, se->section_id);
1259
1260 /* ID string */
1261 len = strlen(se->idstr);
1262 qemu_put_byte(f, len);
1263 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1264
1265 qemu_put_be32(f, se->instance_id);
1266 qemu_put_be32(f, se->version_id);
1267
1268 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1269 }
1270
1271 if (qemu_file_has_error(f))
1272 return -EIO;
1273
1274 return 0;
1275}
1276
1277int qemu_savevm_state_iterate(QEMUFile *f)
1278{
1279 SaveStateEntry *se;
1280 int ret = 1;
1281
Blue Swirl72cf2d42009-09-12 07:36:22 +00001282 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001283 if (se->save_live_state == NULL)
1284 continue;
1285
1286 /* Section type */
1287 qemu_put_byte(f, QEMU_VM_SECTION_PART);
1288 qemu_put_be32(f, se->section_id);
1289
1290 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1291 }
1292
1293 if (ret)
1294 return 1;
1295
1296 if (qemu_file_has_error(f))
1297 return -EIO;
1298
1299 return 0;
1300}
1301
1302int qemu_savevm_state_complete(QEMUFile *f)
1303{
1304 SaveStateEntry *se;
1305
Blue Swirl72cf2d42009-09-12 07:36:22 +00001306 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001307 if (se->save_live_state == NULL)
1308 continue;
1309
1310 /* Section type */
1311 qemu_put_byte(f, QEMU_VM_SECTION_END);
1312 qemu_put_be32(f, se->section_id);
1313
1314 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1315 }
1316
Blue Swirl72cf2d42009-09-12 07:36:22 +00001317 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001318 int len;
1319
Juan Quintela9ed7d6a2009-08-20 19:42:25 +02001320 if (se->save_state == NULL && se->vmsd == NULL)
aliguoria672b462008-11-11 21:33:36 +00001321 continue;
1322
1323 /* Section type */
1324 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1325 qemu_put_be32(f, se->section_id);
1326
1327 /* ID string */
1328 len = strlen(se->idstr);
1329 qemu_put_byte(f, len);
1330 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1331
1332 qemu_put_be32(f, se->instance_id);
1333 qemu_put_be32(f, se->version_id);
1334
Juan Quintela4082be42009-08-20 19:42:24 +02001335 vmstate_save(f, se);
aliguoria672b462008-11-11 21:33:36 +00001336 }
1337
1338 qemu_put_byte(f, QEMU_VM_EOF);
1339
1340 if (qemu_file_has_error(f))
1341 return -EIO;
1342
1343 return 0;
1344}
1345
1346int qemu_savevm_state(QEMUFile *f)
1347{
1348 int saved_vm_running;
1349 int ret;
1350
1351 saved_vm_running = vm_running;
1352 vm_stop(0);
1353
1354 bdrv_flush_all();
1355
lirans@il.ibm.comc163b5c2009-11-02 15:40:58 +02001356 ret = qemu_savevm_state_begin(f, 0, 0);
aliguoria672b462008-11-11 21:33:36 +00001357 if (ret < 0)
1358 goto out;
1359
1360 do {
1361 ret = qemu_savevm_state_iterate(f);
1362 if (ret < 0)
1363 goto out;
1364 } while (ret == 0);
1365
1366 ret = qemu_savevm_state_complete(f);
1367
1368out:
1369 if (qemu_file_has_error(f))
1370 ret = -EIO;
1371
1372 if (!ret && saved_vm_running)
1373 vm_start();
1374
1375 return ret;
1376}
1377
1378static SaveStateEntry *find_se(const char *idstr, int instance_id)
1379{
1380 SaveStateEntry *se;
1381
Blue Swirl72cf2d42009-09-12 07:36:22 +00001382 QTAILQ_FOREACH(se, &savevm_handlers, entry) {
aliguoria672b462008-11-11 21:33:36 +00001383 if (!strcmp(se->idstr, idstr) &&
1384 instance_id == se->instance_id)
1385 return se;
1386 }
1387 return NULL;
1388}
1389
1390typedef struct LoadStateEntry {
Blue Swirl72cf2d42009-09-12 07:36:22 +00001391 QLIST_ENTRY(LoadStateEntry) entry;
aliguoria672b462008-11-11 21:33:36 +00001392 SaveStateEntry *se;
1393 int section_id;
1394 int version_id;
aliguoria672b462008-11-11 21:33:36 +00001395} LoadStateEntry;
1396
aliguoria672b462008-11-11 21:33:36 +00001397int qemu_loadvm_state(QEMUFile *f)
1398{
Blue Swirl72cf2d42009-09-12 07:36:22 +00001399 QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1400 QLIST_HEAD_INITIALIZER(loadvm_handlers);
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001401 LoadStateEntry *le, *new_le;
aliguoria672b462008-11-11 21:33:36 +00001402 uint8_t section_type;
1403 unsigned int v;
1404 int ret;
1405
1406 v = qemu_get_be32(f);
1407 if (v != QEMU_VM_FILE_MAGIC)
1408 return -EINVAL;
1409
1410 v = qemu_get_be32(f);
Juan Quintelabbfe1402009-09-10 03:04:24 +02001411 if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1412 fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1413 return -ENOTSUP;
1414 }
aliguoria672b462008-11-11 21:33:36 +00001415 if (v != QEMU_VM_FILE_VERSION)
1416 return -ENOTSUP;
1417
1418 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1419 uint32_t instance_id, version_id, section_id;
aliguoria672b462008-11-11 21:33:36 +00001420 SaveStateEntry *se;
1421 char idstr[257];
1422 int len;
1423
1424 switch (section_type) {
1425 case QEMU_VM_SECTION_START:
1426 case QEMU_VM_SECTION_FULL:
1427 /* Read section start */
1428 section_id = qemu_get_be32(f);
1429 len = qemu_get_byte(f);
1430 qemu_get_buffer(f, (uint8_t *)idstr, len);
1431 idstr[len] = 0;
1432 instance_id = qemu_get_be32(f);
1433 version_id = qemu_get_be32(f);
1434
1435 /* Find savevm section */
1436 se = find_se(idstr, instance_id);
1437 if (se == NULL) {
1438 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1439 ret = -EINVAL;
1440 goto out;
1441 }
1442
1443 /* Validate version */
1444 if (version_id > se->version_id) {
1445 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1446 version_id, idstr, se->version_id);
1447 ret = -EINVAL;
1448 goto out;
1449 }
1450
1451 /* Add entry */
1452 le = qemu_mallocz(sizeof(*le));
aliguoria672b462008-11-11 21:33:36 +00001453
1454 le->se = se;
1455 le->section_id = section_id;
1456 le->version_id = version_id;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001457 QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
aliguoria672b462008-11-11 21:33:36 +00001458
Juan Quintela4082be42009-08-20 19:42:24 +02001459 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001460 if (ret < 0) {
1461 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1462 instance_id, idstr);
1463 goto out;
1464 }
aliguoria672b462008-11-11 21:33:36 +00001465 break;
1466 case QEMU_VM_SECTION_PART:
1467 case QEMU_VM_SECTION_END:
1468 section_id = qemu_get_be32(f);
1469
Blue Swirl72cf2d42009-09-12 07:36:22 +00001470 QLIST_FOREACH(le, &loadvm_handlers, entry) {
Juan Quintelaf4dbb8d2009-09-01 02:12:33 +02001471 if (le->section_id == section_id) {
1472 break;
1473 }
1474 }
aliguoria672b462008-11-11 21:33:36 +00001475 if (le == NULL) {
1476 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1477 ret = -EINVAL;
1478 goto out;
1479 }
1480
Juan Quintela4082be42009-08-20 19:42:24 +02001481 ret = vmstate_load(f, le->se, le->version_id);
Juan Quintelab5a22e42009-08-20 19:42:23 +02001482 if (ret < 0) {
1483 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1484 section_id);
1485 goto out;
1486 }
aliguoria672b462008-11-11 21:33:36 +00001487 break;
1488 default:
1489 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1490 ret = -EINVAL;
1491 goto out;
1492 }
1493 }
1494
1495 ret = 0;
1496
1497out:
Blue Swirl72cf2d42009-09-12 07:36:22 +00001498 QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1499 QLIST_REMOVE(le, entry);
aliguoria672b462008-11-11 21:33:36 +00001500 qemu_free(le);
1501 }
1502
1503 if (qemu_file_has_error(f))
1504 ret = -EIO;
1505
1506 return ret;
1507}
1508
1509/* device can contain snapshots */
1510static int bdrv_can_snapshot(BlockDriverState *bs)
1511{
1512 return (bs &&
1513 !bdrv_is_removable(bs) &&
1514 !bdrv_is_read_only(bs));
1515}
1516
1517/* device must be snapshots in order to have a reliable snapshot */
1518static int bdrv_has_snapshot(BlockDriverState *bs)
1519{
1520 return (bs &&
1521 !bdrv_is_removable(bs) &&
1522 !bdrv_is_read_only(bs));
1523}
1524
1525static BlockDriverState *get_bs_snapshots(void)
1526{
1527 BlockDriverState *bs;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001528 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001529
1530 if (bs_snapshots)
1531 return bs_snapshots;
Blue Swirl72cf2d42009-09-12 07:36:22 +00001532 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001533 bs = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001534 if (bdrv_can_snapshot(bs))
1535 goto ok;
1536 }
1537 return NULL;
1538 ok:
1539 bs_snapshots = bs;
1540 return bs;
1541}
1542
1543static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1544 const char *name)
1545{
1546 QEMUSnapshotInfo *sn_tab, *sn;
1547 int nb_sns, i, ret;
1548
1549 ret = -ENOENT;
1550 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1551 if (nb_sns < 0)
1552 return ret;
1553 for(i = 0; i < nb_sns; i++) {
1554 sn = &sn_tab[i];
1555 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1556 *sn_info = *sn;
1557 ret = 0;
1558 break;
1559 }
1560 }
1561 qemu_free(sn_tab);
1562 return ret;
1563}
1564
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001565/*
1566 * Deletes snapshots of a given name in all opened images.
1567 */
1568static int del_existing_snapshots(Monitor *mon, const char *name)
1569{
1570 BlockDriverState *bs;
1571 DriveInfo *dinfo;
1572 QEMUSnapshotInfo sn1, *snapshot = &sn1;
1573 int ret;
1574
1575 QTAILQ_FOREACH(dinfo, &drives, next) {
1576 bs = dinfo->bdrv;
1577 if (bdrv_can_snapshot(bs) &&
1578 bdrv_snapshot_find(bs, snapshot, name) >= 0)
1579 {
1580 ret = bdrv_snapshot_delete(bs, name);
1581 if (ret < 0) {
1582 monitor_printf(mon,
1583 "Error while deleting snapshot on '%s'\n",
1584 bdrv_get_device_name(bs));
1585 return -1;
1586 }
1587 }
1588 }
1589
1590 return 0;
1591}
1592
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001593void do_savevm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001594{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001595 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001596 BlockDriverState *bs, *bs1;
1597 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001598 int ret;
aliguoria672b462008-11-11 21:33:36 +00001599 QEMUFile *f;
1600 int saved_vm_running;
aliguori2d22b182008-12-11 21:06:49 +00001601 uint32_t vm_state_size;
aliguoria672b462008-11-11 21:33:36 +00001602#ifdef _WIN32
1603 struct _timeb tb;
1604#else
1605 struct timeval tv;
1606#endif
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001607 const char *name = qdict_get_try_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001608
1609 bs = get_bs_snapshots();
1610 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001611 monitor_printf(mon, "No block device can accept snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001612 return;
1613 }
1614
1615 /* ??? Should this occur after vm_stop? */
1616 qemu_aio_flush();
1617
1618 saved_vm_running = vm_running;
1619 vm_stop(0);
1620
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001621 memset(sn, 0, sizeof(*sn));
aliguoria672b462008-11-11 21:33:36 +00001622 if (name) {
1623 ret = bdrv_snapshot_find(bs, old_sn, name);
1624 if (ret >= 0) {
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001625 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1626 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1627 } else {
aliguoria672b462008-11-11 21:33:36 +00001628 pstrcpy(sn->name, sizeof(sn->name), name);
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001629 }
aliguoria672b462008-11-11 21:33:36 +00001630 }
1631
1632 /* fill auxiliary fields */
1633#ifdef _WIN32
1634 _ftime(&tb);
1635 sn->date_sec = tb.time;
1636 sn->date_nsec = tb.millitm * 1000000;
1637#else
1638 gettimeofday(&tv, NULL);
1639 sn->date_sec = tv.tv_sec;
1640 sn->date_nsec = tv.tv_usec * 1000;
1641#endif
1642 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1643
Kevin Wolfcb499fb2009-11-03 17:34:37 +01001644 /* Delete old snapshots of the same name */
1645 if (del_existing_snapshots(mon, name) < 0) {
1646 goto the_end;
1647 }
1648
aliguoria672b462008-11-11 21:33:36 +00001649 /* save the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001650 f = qemu_fopen_bdrv(bs, 1);
aliguoria672b462008-11-11 21:33:36 +00001651 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001652 monitor_printf(mon, "Could not open VM state file\n");
aliguoria672b462008-11-11 21:33:36 +00001653 goto the_end;
1654 }
1655 ret = qemu_savevm_state(f);
aliguori2d22b182008-12-11 21:06:49 +00001656 vm_state_size = qemu_ftell(f);
aliguoria672b462008-11-11 21:33:36 +00001657 qemu_fclose(f);
1658 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001659 monitor_printf(mon, "Error %d while writing VM\n", ret);
aliguoria672b462008-11-11 21:33:36 +00001660 goto the_end;
1661 }
1662
1663 /* create the snapshots */
1664
Blue Swirl72cf2d42009-09-12 07:36:22 +00001665 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001666 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001667 if (bdrv_has_snapshot(bs1)) {
aliguori2d22b182008-12-11 21:06:49 +00001668 /* Write VM state size only to the image that contains the state */
1669 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
aliguoria672b462008-11-11 21:33:36 +00001670 ret = bdrv_snapshot_create(bs1, sn);
1671 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001672 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1673 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001674 }
1675 }
1676 }
1677
1678 the_end:
1679 if (saved_vm_running)
1680 vm_start();
1681}
1682
Juan Quintela05f24012009-08-20 19:42:22 +02001683int load_vmstate(Monitor *mon, const char *name)
aliguoria672b462008-11-11 21:33:36 +00001684{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001685 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001686 BlockDriverState *bs, *bs1;
aliguori2d22b182008-12-11 21:06:49 +00001687 QEMUSnapshotInfo sn;
aliguoria672b462008-11-11 21:33:36 +00001688 QEMUFile *f;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001689 int ret;
aliguoria672b462008-11-11 21:33:36 +00001690
1691 bs = get_bs_snapshots();
1692 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001693 monitor_printf(mon, "No block device supports snapshots\n");
Juan Quintela05f24012009-08-20 19:42:22 +02001694 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001695 }
1696
1697 /* Flush all IO requests so they don't interfere with the new state. */
1698 qemu_aio_flush();
1699
Blue Swirl72cf2d42009-09-12 07:36:22 +00001700 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001701 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001702 if (bdrv_has_snapshot(bs1)) {
1703 ret = bdrv_snapshot_goto(bs1, name);
1704 if (ret < 0) {
1705 if (bs != bs1)
aliguori376253e2009-03-05 23:01:23 +00001706 monitor_printf(mon, "Warning: ");
aliguoria672b462008-11-11 21:33:36 +00001707 switch(ret) {
1708 case -ENOTSUP:
aliguori376253e2009-03-05 23:01:23 +00001709 monitor_printf(mon,
1710 "Snapshots not supported on device '%s'\n",
1711 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001712 break;
1713 case -ENOENT:
aliguori376253e2009-03-05 23:01:23 +00001714 monitor_printf(mon, "Could not find snapshot '%s' on "
1715 "device '%s'\n",
1716 name, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001717 break;
1718 default:
aliguori376253e2009-03-05 23:01:23 +00001719 monitor_printf(mon, "Error %d while activating snapshot on"
1720 " '%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001721 break;
1722 }
1723 /* fatal on snapshot block device */
1724 if (bs == bs1)
Juan Quintela05f24012009-08-20 19:42:22 +02001725 return 0;
aliguoria672b462008-11-11 21:33:36 +00001726 }
1727 }
1728 }
1729
aliguori2d22b182008-12-11 21:06:49 +00001730 /* Don't even try to load empty VM states */
1731 ret = bdrv_snapshot_find(bs, &sn, name);
1732 if ((ret >= 0) && (sn.vm_state_size == 0))
Juan Quintela05f24012009-08-20 19:42:22 +02001733 return -EINVAL;
aliguori2d22b182008-12-11 21:06:49 +00001734
aliguoria672b462008-11-11 21:33:36 +00001735 /* restore the VM state */
Christoph Hellwig45566e92009-07-10 23:11:57 +02001736 f = qemu_fopen_bdrv(bs, 0);
aliguoria672b462008-11-11 21:33:36 +00001737 if (!f) {
aliguori376253e2009-03-05 23:01:23 +00001738 monitor_printf(mon, "Could not open VM state file\n");
Juan Quintela05f24012009-08-20 19:42:22 +02001739 return -EINVAL;
aliguoria672b462008-11-11 21:33:36 +00001740 }
1741 ret = qemu_loadvm_state(f);
1742 qemu_fclose(f);
1743 if (ret < 0) {
aliguori376253e2009-03-05 23:01:23 +00001744 monitor_printf(mon, "Error %d while loading VM state\n", ret);
Juan Quintela05f24012009-08-20 19:42:22 +02001745 return ret;
aliguoria672b462008-11-11 21:33:36 +00001746 }
Juan Quintela05f24012009-08-20 19:42:22 +02001747 return 0;
Juan Quintela7b630342009-08-20 19:42:20 +02001748}
1749
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001750void do_delvm(Monitor *mon, const QDict *qdict)
aliguoria672b462008-11-11 21:33:36 +00001751{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001752 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001753 BlockDriverState *bs, *bs1;
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001754 int ret;
Luiz Capitulinod54908a2009-08-28 15:27:13 -03001755 const char *name = qdict_get_str(qdict, "name");
aliguoria672b462008-11-11 21:33:36 +00001756
1757 bs = get_bs_snapshots();
1758 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001759 monitor_printf(mon, "No block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001760 return;
1761 }
1762
Blue Swirl72cf2d42009-09-12 07:36:22 +00001763 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001764 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001765 if (bdrv_has_snapshot(bs1)) {
1766 ret = bdrv_snapshot_delete(bs1, name);
1767 if (ret < 0) {
1768 if (ret == -ENOTSUP)
aliguori376253e2009-03-05 23:01:23 +00001769 monitor_printf(mon,
1770 "Snapshots not supported on device '%s'\n",
1771 bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001772 else
aliguori376253e2009-03-05 23:01:23 +00001773 monitor_printf(mon, "Error %d while deleting snapshot on "
1774 "'%s'\n", ret, bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001775 }
1776 }
1777 }
1778}
1779
aliguori376253e2009-03-05 23:01:23 +00001780void do_info_snapshots(Monitor *mon)
aliguoria672b462008-11-11 21:33:36 +00001781{
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001782 DriveInfo *dinfo;
aliguoria672b462008-11-11 21:33:36 +00001783 BlockDriverState *bs, *bs1;
1784 QEMUSnapshotInfo *sn_tab, *sn;
1785 int nb_sns, i;
1786 char buf[256];
1787
1788 bs = get_bs_snapshots();
1789 if (!bs) {
aliguori376253e2009-03-05 23:01:23 +00001790 monitor_printf(mon, "No available block device supports snapshots\n");
aliguoria672b462008-11-11 21:33:36 +00001791 return;
1792 }
aliguori376253e2009-03-05 23:01:23 +00001793 monitor_printf(mon, "Snapshot devices:");
Blue Swirl72cf2d42009-09-12 07:36:22 +00001794 QTAILQ_FOREACH(dinfo, &drives, next) {
Gerd Hoffmann751c6a12009-07-22 16:42:57 +02001795 bs1 = dinfo->bdrv;
aliguoria672b462008-11-11 21:33:36 +00001796 if (bdrv_has_snapshot(bs1)) {
1797 if (bs == bs1)
aliguori376253e2009-03-05 23:01:23 +00001798 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
aliguoria672b462008-11-11 21:33:36 +00001799 }
1800 }
aliguori376253e2009-03-05 23:01:23 +00001801 monitor_printf(mon, "\n");
aliguoria672b462008-11-11 21:33:36 +00001802
1803 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1804 if (nb_sns < 0) {
aliguori376253e2009-03-05 23:01:23 +00001805 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
aliguoria672b462008-11-11 21:33:36 +00001806 return;
1807 }
aliguori376253e2009-03-05 23:01:23 +00001808 monitor_printf(mon, "Snapshot list (from %s):\n",
1809 bdrv_get_device_name(bs));
1810 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
aliguoria672b462008-11-11 21:33:36 +00001811 for(i = 0; i < nb_sns; i++) {
1812 sn = &sn_tab[i];
aliguori376253e2009-03-05 23:01:23 +00001813 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
aliguoria672b462008-11-11 21:33:36 +00001814 }
1815 qemu_free(sn_tab);
1816}