sync with latest
[sdk/emulator/qemu.git] / savevm.c
1 /*
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  */
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <time.h>
27 #include <errno.h>
28 #include <sys/time.h>
29 #include <zlib.h>
30
31 /* Needed early for CONFIG_BSD etc. */
32 #include "config-host.h"
33
34 #ifndef _WIN32
35 #include <sys/times.h>
36 #include <sys/wait.h>
37 #include <termios.h>
38 #include <sys/mman.h>
39 #include <sys/ioctl.h>
40 #include <sys/resource.h>
41 #include <sys/socket.h>
42 #include <netinet/in.h>
43 #include <net/if.h>
44 #include <arpa/inet.h>
45 #include <dirent.h>
46 #include <netdb.h>
47 #include <sys/select.h>
48 #ifdef CONFIG_BSD
49 #include <sys/stat.h>
50 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
51 #include <libutil.h>
52 #else
53 #include <util.h>
54 #endif
55 #ifdef __linux__
56 #include <pty.h>
57 #include <malloc.h>
58 #include <linux/rtc.h>
59 #endif
60 #endif
61 #endif
62
63 #ifdef _WIN32
64 #include <windows.h>
65 #include <malloc.h>
66 #include <sys/timeb.h>
67 #include <mmsystem.h>
68 #define getopt_long_only getopt_long
69 #define memalign(align, size) malloc(size)
70 #endif
71
72 #include "qemu-common.h"
73 #include "hw/hw.h"
74 #include "hw/qdev.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 "audio/audio.h"
81 #include "migration.h"
82 #include "qemu_socket.h"
83 #include "qemu-queue.h"
84 #include "qemu-timer.h"
85 #include "cpus.h"
86 #include "memory.h"
87 #include "qmp-commands.h"
88 #include "trace.h"
89
90 #define SELF_ANNOUNCE_ROUNDS 5
91
92 #ifndef ETH_P_RARP
93 #define ETH_P_RARP 0x8035
94 #endif
95 #define ARP_HTYPE_ETH 0x0001
96 #define ARP_PTYPE_IP 0x0800
97 #define ARP_OP_REQUEST_REV 0x3
98
99 static int announce_self_create(uint8_t *buf,
100                                 uint8_t *mac_addr)
101 {
102     /* Ethernet header. */
103     memset(buf, 0xff, 6);         /* destination MAC addr */
104     memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
105     *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
106
107     /* RARP header. */
108     *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
109     *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
110     *(buf + 18) = 6; /* hardware addr length (ethernet) */
111     *(buf + 19) = 4; /* protocol addr length (IPv4) */
112     *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
113     memcpy(buf + 22, mac_addr, 6); /* source hw addr */
114     memset(buf + 28, 0x00, 4);     /* source protocol addr */
115     memcpy(buf + 32, mac_addr, 6); /* target hw addr */
116     memset(buf + 38, 0x00, 4);     /* target protocol addr */
117
118     /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
119     memset(buf + 42, 0x00, 18);
120
121     return 60; /* len (FCS will be added by hardware) */
122 }
123
124 static void qemu_announce_self_iter(NICState *nic, void *opaque)
125 {
126     uint8_t buf[60];
127     int len;
128
129     len = announce_self_create(buf, nic->conf->macaddr.a);
130
131     qemu_send_packet_raw(&nic->nc, buf, len);
132 }
133
134
135 static void qemu_announce_self_once(void *opaque)
136 {
137     static int count = SELF_ANNOUNCE_ROUNDS;
138     QEMUTimer *timer = *(QEMUTimer **)opaque;
139
140     qemu_foreach_nic(qemu_announce_self_iter, NULL);
141
142     if (--count) {
143         /* delay 50ms, 150ms, 250ms, ... */
144         qemu_mod_timer(timer, qemu_get_clock_ms(rt_clock) +
145                        50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
146     } else {
147             qemu_del_timer(timer);
148             qemu_free_timer(timer);
149     }
150 }
151
152 void qemu_announce_self(void)
153 {
154         static QEMUTimer *timer;
155         timer = qemu_new_timer_ms(rt_clock, qemu_announce_self_once, &timer);
156         qemu_announce_self_once(&timer);
157 }
158
159 /***********************************************************/
160 /* savevm/loadvm support */
161
162 #define IO_BUF_SIZE 32768
163
164 struct QEMUFile {
165     QEMUFilePutBufferFunc *put_buffer;
166     QEMUFileGetBufferFunc *get_buffer;
167     QEMUFileCloseFunc *close;
168     QEMUFileRateLimit *rate_limit;
169     QEMUFileSetRateLimit *set_rate_limit;
170     QEMUFileGetRateLimit *get_rate_limit;
171     void *opaque;
172     int is_write;
173
174     int64_t buf_offset; /* start of buffer when writing, end of buffer
175                            when reading */
176     int buf_index;
177     int buf_size; /* 0 when writing */
178     uint8_t buf[IO_BUF_SIZE];
179
180     int last_error;
181 };
182
183 typedef struct QEMUFileStdio
184 {
185     FILE *stdio_file;
186     QEMUFile *file;
187 } QEMUFileStdio;
188
189 typedef struct QEMUFileSocket
190 {
191     int fd;
192     QEMUFile *file;
193 } QEMUFileSocket;
194
195 static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
196 {
197     QEMUFileSocket *s = opaque;
198     ssize_t len;
199
200     do {
201         len = qemu_recv(s->fd, buf, size, 0);
202     } while (len == -1 && socket_error() == EINTR);
203
204     if (len == -1)
205         len = -socket_error();
206
207     return len;
208 }
209
210 static int socket_close(void *opaque)
211 {
212     QEMUFileSocket *s = opaque;
213     g_free(s);
214     return 0;
215 }
216
217 static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
218 {
219     QEMUFileStdio *s = opaque;
220     return fwrite(buf, 1, size, s->stdio_file);
221 }
222
223 static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
224 {
225     QEMUFileStdio *s = opaque;
226     FILE *fp = s->stdio_file;
227     int bytes;
228
229     do {
230         clearerr(fp);
231         bytes = fread(buf, 1, size, fp);
232     } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
233     return bytes;
234 }
235
236 static int stdio_pclose(void *opaque)
237 {
238     QEMUFileStdio *s = opaque;
239     int ret;
240     ret = pclose(s->stdio_file);
241     if (ret == -1) {
242         ret = -errno;
243     }
244     g_free(s);
245     return ret;
246 }
247
248 static int stdio_fclose(void *opaque)
249 {
250     QEMUFileStdio *s = opaque;
251     int ret = 0;
252     if (fclose(s->stdio_file) == EOF) {
253         ret = -errno;
254     }
255     g_free(s);
256     return ret;
257 }
258
259 QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
260 {
261     QEMUFileStdio *s;
262
263     if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
264         fprintf(stderr, "qemu_popen: Argument validity check failed\n");
265         return NULL;
266     }
267
268     s = g_malloc0(sizeof(QEMUFileStdio));
269
270     s->stdio_file = stdio_file;
271
272     if(mode[0] == 'r') {
273         s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, 
274                                  NULL, NULL, NULL);
275     } else {
276         s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, 
277                                  NULL, NULL, NULL);
278     }
279     return s->file;
280 }
281
282 QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
283 {
284     FILE *popen_file;
285
286     popen_file = popen(command, mode);
287     if(popen_file == NULL) {
288         return NULL;
289     }
290
291     return qemu_popen(popen_file, mode);
292 }
293
294 int qemu_stdio_fd(QEMUFile *f)
295 {
296     QEMUFileStdio *p;
297     int fd;
298
299     p = (QEMUFileStdio *)f->opaque;
300     fd = fileno(p->stdio_file);
301
302     return fd;
303 }
304
305 QEMUFile *qemu_fdopen(int fd, const char *mode)
306 {
307     QEMUFileStdio *s;
308
309     if (mode == NULL ||
310         (mode[0] != 'r' && mode[0] != 'w') ||
311         mode[1] != 'b' || mode[2] != 0) {
312         fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
313         return NULL;
314     }
315
316     s = g_malloc0(sizeof(QEMUFileStdio));
317     s->stdio_file = fdopen(fd, mode);
318     if (!s->stdio_file)
319         goto fail;
320
321     if(mode[0] == 'r') {
322         s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, 
323                                  NULL, NULL, NULL);
324     } else {
325         s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, 
326                                  NULL, NULL, NULL);
327     }
328     return s->file;
329
330 fail:
331     g_free(s);
332     return NULL;
333 }
334
335 QEMUFile *qemu_fopen_socket(int fd)
336 {
337     QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
338
339     s->fd = fd;
340     s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, 
341                              NULL, NULL, NULL);
342     return s->file;
343 }
344
345 static int file_put_buffer(void *opaque, const uint8_t *buf,
346                             int64_t pos, int size)
347 {
348     QEMUFileStdio *s = opaque;
349     fseek(s->stdio_file, pos, SEEK_SET);
350     return fwrite(buf, 1, size, s->stdio_file);
351 }
352
353 static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
354 {
355     QEMUFileStdio *s = opaque;
356     fseek(s->stdio_file, pos, SEEK_SET);
357     return fread(buf, 1, size, s->stdio_file);
358 }
359
360 QEMUFile *qemu_fopen(const char *filename, const char *mode)
361 {
362     QEMUFileStdio *s;
363
364     if (mode == NULL ||
365         (mode[0] != 'r' && mode[0] != 'w') ||
366         mode[1] != 'b' || mode[2] != 0) {
367         fprintf(stderr, "qemu_fopen: Argument validity check failed\n");
368         return NULL;
369     }
370
371     s = g_malloc0(sizeof(QEMUFileStdio));
372
373     s->stdio_file = fopen(filename, mode);
374     if (!s->stdio_file)
375         goto fail;
376     
377     if(mode[0] == 'w') {
378         s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, 
379                                  NULL, NULL, NULL);
380     } else {
381         s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, 
382                                NULL, NULL, NULL);
383     }
384     return s->file;
385 fail:
386     g_free(s);
387     return NULL;
388 }
389
390 static int block_put_buffer(void *opaque, const uint8_t *buf,
391                            int64_t pos, int size)
392 {
393     bdrv_save_vmstate(opaque, buf, pos, size);
394     return size;
395 }
396
397 static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
398 {
399     return bdrv_load_vmstate(opaque, buf, pos, size);
400 }
401
402 static int bdrv_fclose(void *opaque)
403 {
404     return bdrv_flush(opaque);
405 }
406
407 static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
408 {
409     if (is_writable)
410         return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, 
411                               NULL, NULL, NULL);
412     return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
413 }
414
415 QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
416                          QEMUFileGetBufferFunc *get_buffer,
417                          QEMUFileCloseFunc *close,
418                          QEMUFileRateLimit *rate_limit,
419                          QEMUFileSetRateLimit *set_rate_limit,
420                          QEMUFileGetRateLimit *get_rate_limit)
421 {
422     QEMUFile *f;
423
424     f = g_malloc0(sizeof(QEMUFile));
425
426     f->opaque = opaque;
427     f->put_buffer = put_buffer;
428     f->get_buffer = get_buffer;
429     f->close = close;
430     f->rate_limit = rate_limit;
431     f->set_rate_limit = set_rate_limit;
432     f->get_rate_limit = get_rate_limit;
433     f->is_write = 0;
434
435     return f;
436 }
437
438 int qemu_file_get_error(QEMUFile *f)
439 {
440     return f->last_error;
441 }
442
443 void qemu_file_set_error(QEMUFile *f, int ret)
444 {
445     f->last_error = ret;
446 }
447
448 /** Sets last_error conditionally
449  *
450  * Sets last_error only if ret is negative _and_ no error
451  * was set before.
452  */
453 static void qemu_file_set_if_error(QEMUFile *f, int ret)
454 {
455     if (ret < 0 && !f->last_error) {
456         qemu_file_set_error(f, ret);
457     }
458 }
459
460 /** Flushes QEMUFile buffer
461  *
462  * In case of error, last_error is set.
463  */
464 void qemu_fflush(QEMUFile *f)
465 {
466     if (!f->put_buffer)
467         return;
468
469     if (f->is_write && f->buf_index > 0) {
470         int len;
471
472         len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
473         if (len > 0)
474             f->buf_offset += f->buf_index;
475         else
476             qemu_file_set_error(f, -EINVAL);
477         f->buf_index = 0;
478     }
479 }
480
481 static void qemu_fill_buffer(QEMUFile *f)
482 {
483     int len;
484     int pending;
485
486     if (!f->get_buffer)
487         return;
488
489     if (f->is_write)
490         abort();
491
492     pending = f->buf_size - f->buf_index;
493     if (pending > 0) {
494         memmove(f->buf, f->buf + f->buf_index, pending);
495     }
496     f->buf_index = 0;
497     f->buf_size = pending;
498
499     len = f->get_buffer(f->opaque, f->buf + pending, f->buf_offset,
500                         IO_BUF_SIZE - pending);
501     if (len > 0) {
502         f->buf_size += len;
503         f->buf_offset += len;
504     } else if (len == 0) {
505         f->last_error = -EIO;
506     } else if (len != -EAGAIN)
507         qemu_file_set_error(f, len);
508 }
509
510 /** Calls close function and set last_error if needed
511  *
512  * Internal function. qemu_fflush() must be called before this.
513  *
514  * Returns f->close() return value, or 0 if close function is not set.
515  */
516 static int qemu_fclose_internal(QEMUFile *f)
517 {
518     int ret = 0;
519     if (f->close) {
520         ret = f->close(f->opaque);
521         qemu_file_set_if_error(f, ret);
522     }
523     return ret;
524 }
525
526 /** Closes the file
527  *
528  * Returns negative error value if any error happened on previous operations or
529  * while closing the file. Returns 0 or positive number on success.
530  *
531  * The meaning of return value on success depends on the specific backend
532  * being used.
533  */
534 int qemu_fclose(QEMUFile *f)
535 {
536     int ret;
537     qemu_fflush(f);
538     ret = qemu_fclose_internal(f);
539     /* If any error was spotted before closing, we should report it
540      * instead of the close() return value.
541      */
542     if (f->last_error) {
543         ret = f->last_error;
544     }
545     g_free(f);
546     return ret;
547 }
548
549 void qemu_file_put_notify(QEMUFile *f)
550 {
551     f->put_buffer(f->opaque, NULL, 0, 0);
552 }
553
554 void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
555 {
556     int l;
557
558     if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
559         fprintf(stderr,
560                 "Attempted to write to buffer while read buffer is not empty\n");
561         abort();
562     }
563
564     while (!f->last_error && size > 0) {
565         l = IO_BUF_SIZE - f->buf_index;
566         if (l > size)
567             l = size;
568         memcpy(f->buf + f->buf_index, buf, l);
569         f->is_write = 1;
570         f->buf_index += l;
571         buf += l;
572         size -= l;
573         if (f->buf_index >= IO_BUF_SIZE)
574             qemu_fflush(f);
575     }
576 }
577
578 void qemu_put_byte(QEMUFile *f, int v)
579 {
580     if (!f->last_error && f->is_write == 0 && f->buf_index > 0) {
581         fprintf(stderr,
582                 "Attempted to write to buffer while read buffer is not empty\n");
583         abort();
584     }
585
586     f->buf[f->buf_index++] = v;
587     f->is_write = 1;
588     if (f->buf_index >= IO_BUF_SIZE)
589         qemu_fflush(f);
590 }
591
592 static void qemu_file_skip(QEMUFile *f, int size)
593 {
594     if (f->buf_index + size <= f->buf_size) {
595         f->buf_index += size;
596     }
597 }
598
599 static int qemu_peek_buffer(QEMUFile *f, uint8_t *buf, int size, size_t offset)
600 {
601     int pending;
602     int index;
603
604     if (f->is_write) {
605         abort();
606     }
607
608     index = f->buf_index + offset;
609     pending = f->buf_size - index;
610     if (pending < size) {
611         qemu_fill_buffer(f);
612         index = f->buf_index + offset;
613         pending = f->buf_size - index;
614     }
615
616     if (pending <= 0) {
617         return 0;
618     }
619     if (size > pending) {
620         size = pending;
621     }
622
623     memcpy(buf, f->buf + index, size);
624     return size;
625 }
626
627 int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size)
628 {
629     int pending = size;
630     int done = 0;
631
632     while (pending > 0) {
633         int res;
634
635         res = qemu_peek_buffer(f, buf, pending, 0);
636         if (res == 0) {
637             return done;
638         }
639         qemu_file_skip(f, res);
640         buf += res;
641         pending -= res;
642         done += res;
643     }
644     return done;
645 }
646
647 static int qemu_peek_byte(QEMUFile *f, int offset)
648 {
649     int index = f->buf_index + offset;
650
651     if (f->is_write) {
652         abort();
653     }
654
655     if (index >= f->buf_size) {
656         qemu_fill_buffer(f);
657         index = f->buf_index + offset;
658         if (index >= f->buf_size) {
659             return 0;
660         }
661     }
662     return f->buf[index];
663 }
664
665 int qemu_get_byte(QEMUFile *f)
666 {
667     int result;
668
669     result = qemu_peek_byte(f, 0);
670     qemu_file_skip(f, 1);
671     return result;
672 }
673
674 int64_t qemu_ftell(QEMUFile *f)
675 {
676     return f->buf_offset - f->buf_size + f->buf_index;
677 }
678
679 int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
680 {
681     if (whence == SEEK_SET) {
682         /* nothing to do */
683     } else if (whence == SEEK_CUR) {
684         pos += qemu_ftell(f);
685     } else {
686         /* SEEK_END not supported */
687         return -1;
688     }
689     if (f->put_buffer) {
690         qemu_fflush(f);
691         f->buf_offset = pos;
692     } else {
693         f->buf_offset = pos;
694         f->buf_index = 0;
695         f->buf_size = 0;
696     }
697     return pos;
698 }
699
700 int qemu_file_rate_limit(QEMUFile *f)
701 {
702     if (f->rate_limit)
703         return f->rate_limit(f->opaque);
704
705     return 0;
706 }
707
708 int64_t qemu_file_get_rate_limit(QEMUFile *f)
709 {
710     if (f->get_rate_limit)
711         return f->get_rate_limit(f->opaque);
712
713     return 0;
714 }
715
716 int64_t qemu_file_set_rate_limit(QEMUFile *f, int64_t new_rate)
717 {
718     /* any failed or completed migration keeps its state to allow probing of
719      * migration data, but has no associated file anymore */
720     if (f && f->set_rate_limit)
721         return f->set_rate_limit(f->opaque, new_rate);
722
723     return 0;
724 }
725
726 void qemu_put_be16(QEMUFile *f, unsigned int v)
727 {
728     qemu_put_byte(f, v >> 8);
729     qemu_put_byte(f, v);
730 }
731
732 void qemu_put_be32(QEMUFile *f, unsigned int v)
733 {
734     qemu_put_byte(f, v >> 24);
735     qemu_put_byte(f, v >> 16);
736     qemu_put_byte(f, v >> 8);
737     qemu_put_byte(f, v);
738 }
739
740 void qemu_put_be64(QEMUFile *f, uint64_t v)
741 {
742     qemu_put_be32(f, v >> 32);
743     qemu_put_be32(f, v);
744 }
745
746 unsigned int qemu_get_be16(QEMUFile *f)
747 {
748     unsigned int v;
749     v = qemu_get_byte(f) << 8;
750     v |= qemu_get_byte(f);
751     return v;
752 }
753
754 unsigned int qemu_get_be32(QEMUFile *f)
755 {
756     unsigned int v;
757     v = qemu_get_byte(f) << 24;
758     v |= qemu_get_byte(f) << 16;
759     v |= qemu_get_byte(f) << 8;
760     v |= qemu_get_byte(f);
761     return v;
762 }
763
764 uint64_t qemu_get_be64(QEMUFile *f)
765 {
766     uint64_t v;
767     v = (uint64_t)qemu_get_be32(f) << 32;
768     v |= qemu_get_be32(f);
769     return v;
770 }
771
772
773 /* timer */
774
775 void qemu_put_timer(QEMUFile *f, QEMUTimer *ts)
776 {
777     uint64_t expire_time;
778
779     expire_time = qemu_timer_expire_time_ns(ts);
780     qemu_put_be64(f, expire_time);
781 }
782
783 void qemu_get_timer(QEMUFile *f, QEMUTimer *ts)
784 {
785     uint64_t expire_time;
786
787     expire_time = qemu_get_be64(f);
788     if (expire_time != -1) {
789         qemu_mod_timer_ns(ts, expire_time);
790     } else {
791         qemu_del_timer(ts);
792     }
793 }
794
795
796 /* bool */
797
798 static int get_bool(QEMUFile *f, void *pv, size_t size)
799 {
800     bool *v = pv;
801     *v = qemu_get_byte(f);
802     return 0;
803 }
804
805 static void put_bool(QEMUFile *f, void *pv, size_t size)
806 {
807     bool *v = pv;
808     qemu_put_byte(f, *v);
809 }
810
811 const VMStateInfo vmstate_info_bool = {
812     .name = "bool",
813     .get  = get_bool,
814     .put  = put_bool,
815 };
816
817 /* 8 bit int */
818
819 static int get_int8(QEMUFile *f, void *pv, size_t size)
820 {
821     int8_t *v = pv;
822     qemu_get_s8s(f, v);
823     return 0;
824 }
825
826 static void put_int8(QEMUFile *f, void *pv, size_t size)
827 {
828     int8_t *v = pv;
829     qemu_put_s8s(f, v);
830 }
831
832 const VMStateInfo vmstate_info_int8 = {
833     .name = "int8",
834     .get  = get_int8,
835     .put  = put_int8,
836 };
837
838 /* 16 bit int */
839
840 static int get_int16(QEMUFile *f, void *pv, size_t size)
841 {
842     int16_t *v = pv;
843     qemu_get_sbe16s(f, v);
844     return 0;
845 }
846
847 static void put_int16(QEMUFile *f, void *pv, size_t size)
848 {
849     int16_t *v = pv;
850     qemu_put_sbe16s(f, v);
851 }
852
853 const VMStateInfo vmstate_info_int16 = {
854     .name = "int16",
855     .get  = get_int16,
856     .put  = put_int16,
857 };
858
859 /* 32 bit int */
860
861 static int get_int32(QEMUFile *f, void *pv, size_t size)
862 {
863     int32_t *v = pv;
864     qemu_get_sbe32s(f, v);
865     return 0;
866 }
867
868 static void put_int32(QEMUFile *f, void *pv, size_t size)
869 {
870     int32_t *v = pv;
871     qemu_put_sbe32s(f, v);
872 }
873
874 const VMStateInfo vmstate_info_int32 = {
875     .name = "int32",
876     .get  = get_int32,
877     .put  = put_int32,
878 };
879
880 /* 32 bit int. See that the received value is the same than the one
881    in the field */
882
883 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
884 {
885     int32_t *v = pv;
886     int32_t v2;
887     qemu_get_sbe32s(f, &v2);
888
889     if (*v == v2)
890         return 0;
891     return -EINVAL;
892 }
893
894 const VMStateInfo vmstate_info_int32_equal = {
895     .name = "int32 equal",
896     .get  = get_int32_equal,
897     .put  = put_int32,
898 };
899
900 /* 32 bit int. See that the received value is the less or the same
901    than the one in the field */
902
903 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
904 {
905     int32_t *old = pv;
906     int32_t new;
907     qemu_get_sbe32s(f, &new);
908
909     if (*old <= new)
910         return 0;
911     return -EINVAL;
912 }
913
914 const VMStateInfo vmstate_info_int32_le = {
915     .name = "int32 equal",
916     .get  = get_int32_le,
917     .put  = put_int32,
918 };
919
920 /* 64 bit int */
921
922 static int get_int64(QEMUFile *f, void *pv, size_t size)
923 {
924     int64_t *v = pv;
925     qemu_get_sbe64s(f, v);
926     return 0;
927 }
928
929 static void put_int64(QEMUFile *f, void *pv, size_t size)
930 {
931     int64_t *v = pv;
932     qemu_put_sbe64s(f, v);
933 }
934
935 const VMStateInfo vmstate_info_int64 = {
936     .name = "int64",
937     .get  = get_int64,
938     .put  = put_int64,
939 };
940
941 /* 8 bit unsigned int */
942
943 static int get_uint8(QEMUFile *f, void *pv, size_t size)
944 {
945     uint8_t *v = pv;
946     qemu_get_8s(f, v);
947     return 0;
948 }
949
950 static void put_uint8(QEMUFile *f, void *pv, size_t size)
951 {
952     uint8_t *v = pv;
953     qemu_put_8s(f, v);
954 }
955
956 const VMStateInfo vmstate_info_uint8 = {
957     .name = "uint8",
958     .get  = get_uint8,
959     .put  = put_uint8,
960 };
961
962 /* 16 bit unsigned int */
963
964 static int get_uint16(QEMUFile *f, void *pv, size_t size)
965 {
966     uint16_t *v = pv;
967     qemu_get_be16s(f, v);
968     return 0;
969 }
970
971 static void put_uint16(QEMUFile *f, void *pv, size_t size)
972 {
973     uint16_t *v = pv;
974     qemu_put_be16s(f, v);
975 }
976
977 const VMStateInfo vmstate_info_uint16 = {
978     .name = "uint16",
979     .get  = get_uint16,
980     .put  = put_uint16,
981 };
982
983 /* 32 bit unsigned int */
984
985 static int get_uint32(QEMUFile *f, void *pv, size_t size)
986 {
987     uint32_t *v = pv;
988     qemu_get_be32s(f, v);
989     return 0;
990 }
991
992 static void put_uint32(QEMUFile *f, void *pv, size_t size)
993 {
994     uint32_t *v = pv;
995     qemu_put_be32s(f, v);
996 }
997
998 const VMStateInfo vmstate_info_uint32 = {
999     .name = "uint32",
1000     .get  = get_uint32,
1001     .put  = put_uint32,
1002 };
1003
1004 /* 32 bit uint. See that the received value is the same than the one
1005    in the field */
1006
1007 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
1008 {
1009     uint32_t *v = pv;
1010     uint32_t v2;
1011     qemu_get_be32s(f, &v2);
1012
1013     if (*v == v2) {
1014         return 0;
1015     }
1016     return -EINVAL;
1017 }
1018
1019 const VMStateInfo vmstate_info_uint32_equal = {
1020     .name = "uint32 equal",
1021     .get  = get_uint32_equal,
1022     .put  = put_uint32,
1023 };
1024
1025 /* 64 bit unsigned int */
1026
1027 static int get_uint64(QEMUFile *f, void *pv, size_t size)
1028 {
1029     uint64_t *v = pv;
1030     qemu_get_be64s(f, v);
1031     return 0;
1032 }
1033
1034 static void put_uint64(QEMUFile *f, void *pv, size_t size)
1035 {
1036     uint64_t *v = pv;
1037     qemu_put_be64s(f, v);
1038 }
1039
1040 const VMStateInfo vmstate_info_uint64 = {
1041     .name = "uint64",
1042     .get  = get_uint64,
1043     .put  = put_uint64,
1044 };
1045
1046 /* 8 bit int. See that the received value is the same than the one
1047    in the field */
1048
1049 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
1050 {
1051     uint8_t *v = pv;
1052     uint8_t v2;
1053     qemu_get_8s(f, &v2);
1054
1055     if (*v == v2)
1056         return 0;
1057     return -EINVAL;
1058 }
1059
1060 const VMStateInfo vmstate_info_uint8_equal = {
1061     .name = "uint8 equal",
1062     .get  = get_uint8_equal,
1063     .put  = put_uint8,
1064 };
1065
1066 /* 16 bit unsigned int int. See that the received value is the same than the one
1067    in the field */
1068
1069 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
1070 {
1071     uint16_t *v = pv;
1072     uint16_t v2;
1073     qemu_get_be16s(f, &v2);
1074
1075     if (*v == v2)
1076         return 0;
1077     return -EINVAL;
1078 }
1079
1080 const VMStateInfo vmstate_info_uint16_equal = {
1081     .name = "uint16 equal",
1082     .get  = get_uint16_equal,
1083     .put  = put_uint16,
1084 };
1085
1086 /* timers  */
1087
1088 static int get_timer(QEMUFile *f, void *pv, size_t size)
1089 {
1090     QEMUTimer *v = pv;
1091     qemu_get_timer(f, v);
1092     return 0;
1093 }
1094
1095 static void put_timer(QEMUFile *f, void *pv, size_t size)
1096 {
1097     QEMUTimer *v = pv;
1098     qemu_put_timer(f, v);
1099 }
1100
1101 const VMStateInfo vmstate_info_timer = {
1102     .name = "timer",
1103     .get  = get_timer,
1104     .put  = put_timer,
1105 };
1106
1107 /* uint8_t buffers */
1108
1109 static int get_buffer(QEMUFile *f, void *pv, size_t size)
1110 {
1111     uint8_t *v = pv;
1112     qemu_get_buffer(f, v, size);
1113     return 0;
1114 }
1115
1116 static void put_buffer(QEMUFile *f, void *pv, size_t size)
1117 {
1118     uint8_t *v = pv;
1119     qemu_put_buffer(f, v, size);
1120 }
1121
1122 const VMStateInfo vmstate_info_buffer = {
1123     .name = "buffer",
1124     .get  = get_buffer,
1125     .put  = put_buffer,
1126 };
1127
1128 /* unused buffers: space that was used for some fields that are
1129    not useful anymore */
1130
1131 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
1132 {
1133     uint8_t buf[1024];
1134     int block_len;
1135
1136     while (size > 0) {
1137         block_len = MIN(sizeof(buf), size);
1138         size -= block_len;
1139         qemu_get_buffer(f, buf, block_len);
1140     }
1141    return 0;
1142 }
1143
1144 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
1145 {
1146     static const uint8_t buf[1024];
1147     int block_len;
1148
1149     while (size > 0) {
1150         block_len = MIN(sizeof(buf), size);
1151         size -= block_len;
1152         qemu_put_buffer(f, buf, block_len);
1153     }
1154 }
1155
1156 const VMStateInfo vmstate_info_unused_buffer = {
1157     .name = "unused_buffer",
1158     .get  = get_unused_buffer,
1159     .put  = put_unused_buffer,
1160 };
1161
1162 typedef struct CompatEntry {
1163     char idstr[256];
1164     int instance_id;
1165 } CompatEntry;
1166
1167 typedef struct SaveStateEntry {
1168     QTAILQ_ENTRY(SaveStateEntry) entry;
1169     char idstr[256];
1170     int instance_id;
1171     int alias_id;
1172     int version_id;
1173     int section_id;
1174     SaveVMHandlers *ops;
1175     const VMStateDescription *vmsd;
1176     void *opaque;
1177     CompatEntry *compat;
1178     int no_migrate;
1179     int is_ram;
1180 } SaveStateEntry;
1181
1182
1183 static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
1184     QTAILQ_HEAD_INITIALIZER(savevm_handlers);
1185 static int global_section_id;
1186
1187 static int calculate_new_instance_id(const char *idstr)
1188 {
1189     SaveStateEntry *se;
1190     int instance_id = 0;
1191
1192     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1193         if (strcmp(idstr, se->idstr) == 0
1194             && instance_id <= se->instance_id) {
1195             instance_id = se->instance_id + 1;
1196         }
1197     }
1198     return instance_id;
1199 }
1200
1201 static int calculate_compat_instance_id(const char *idstr)
1202 {
1203     SaveStateEntry *se;
1204     int instance_id = 0;
1205
1206     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1207         if (!se->compat)
1208             continue;
1209
1210         if (strcmp(idstr, se->compat->idstr) == 0
1211             && instance_id <= se->compat->instance_id) {
1212             instance_id = se->compat->instance_id + 1;
1213         }
1214     }
1215     return instance_id;
1216 }
1217
1218 /* TODO: Individual devices generally have very little idea about the rest
1219    of the system, so instance_id should be removed/replaced.
1220    Meanwhile pass -1 as instance_id if you do not already have a clearly
1221    distinguishing id for all instances of your device class. */
1222 int register_savevm_live(DeviceState *dev,
1223                          const char *idstr,
1224                          int instance_id,
1225                          int version_id,
1226                          SaveVMHandlers *ops,
1227                          void *opaque)
1228 {
1229     SaveStateEntry *se;
1230
1231     se = g_malloc0(sizeof(SaveStateEntry));
1232     se->version_id = version_id;
1233     se->section_id = global_section_id++;
1234     se->ops = ops;
1235     se->opaque = opaque;
1236     se->vmsd = NULL;
1237     se->no_migrate = 0;
1238     /* if this is a live_savem then set is_ram */
1239     if (ops->save_live_setup != NULL) {
1240         se->is_ram = 1;
1241     }
1242
1243     if (dev) {
1244         char *id = qdev_get_dev_path(dev);
1245         if (id) {
1246             pstrcpy(se->idstr, sizeof(se->idstr), id);
1247             pstrcat(se->idstr, sizeof(se->idstr), "/");
1248             g_free(id);
1249
1250             se->compat = g_malloc0(sizeof(CompatEntry));
1251             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), idstr);
1252             se->compat->instance_id = instance_id == -1 ?
1253                          calculate_compat_instance_id(idstr) : instance_id;
1254             instance_id = -1;
1255         }
1256     }
1257     pstrcat(se->idstr, sizeof(se->idstr), idstr);
1258
1259     if (instance_id == -1) {
1260         se->instance_id = calculate_new_instance_id(se->idstr);
1261     } else {
1262         se->instance_id = instance_id;
1263     }
1264     assert(!se->compat || se->instance_id == 0);
1265     /* add at the end of list */
1266     QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1267     return 0;
1268 }
1269
1270 int register_savevm(DeviceState *dev,
1271                     const char *idstr,
1272                     int instance_id,
1273                     int version_id,
1274                     SaveStateHandler *save_state,
1275                     LoadStateHandler *load_state,
1276                     void *opaque)
1277 {
1278     SaveVMHandlers *ops = g_malloc0(sizeof(SaveVMHandlers));
1279     ops->save_state = save_state;
1280     ops->load_state = load_state;
1281     return register_savevm_live(dev, idstr, instance_id, version_id,
1282                                 ops, opaque);
1283 }
1284
1285 void unregister_savevm(DeviceState *dev, const char *idstr, void *opaque)
1286 {
1287     SaveStateEntry *se, *new_se;
1288     char id[256] = "";
1289
1290     if (dev) {
1291         char *path = qdev_get_dev_path(dev);
1292         if (path) {
1293             pstrcpy(id, sizeof(id), path);
1294             pstrcat(id, sizeof(id), "/");
1295             g_free(path);
1296         }
1297     }
1298     pstrcat(id, sizeof(id), idstr);
1299
1300     QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1301         if (strcmp(se->idstr, id) == 0 && se->opaque == opaque) {
1302             QTAILQ_REMOVE(&savevm_handlers, se, entry);
1303             if (se->compat) {
1304                 g_free(se->compat);
1305             }
1306             g_free(se->ops);
1307             g_free(se);
1308         }
1309     }
1310 }
1311
1312 int vmstate_register_with_alias_id(DeviceState *dev, int instance_id,
1313                                    const VMStateDescription *vmsd,
1314                                    void *opaque, int alias_id,
1315                                    int required_for_version)
1316 {
1317     SaveStateEntry *se;
1318
1319     /* If this triggers, alias support can be dropped for the vmsd. */
1320     assert(alias_id == -1 || required_for_version >= vmsd->minimum_version_id);
1321
1322     se = g_malloc0(sizeof(SaveStateEntry));
1323     se->version_id = vmsd->version_id;
1324     se->section_id = global_section_id++;
1325     se->opaque = opaque;
1326     se->vmsd = vmsd;
1327     se->alias_id = alias_id;
1328     se->no_migrate = vmsd->unmigratable;
1329
1330     if (dev) {
1331         char *id = qdev_get_dev_path(dev);
1332         if (id) {
1333             pstrcpy(se->idstr, sizeof(se->idstr), id);
1334             pstrcat(se->idstr, sizeof(se->idstr), "/");
1335             g_free(id);
1336
1337             se->compat = g_malloc0(sizeof(CompatEntry));
1338             pstrcpy(se->compat->idstr, sizeof(se->compat->idstr), vmsd->name);
1339             se->compat->instance_id = instance_id == -1 ?
1340                          calculate_compat_instance_id(vmsd->name) : instance_id;
1341             instance_id = -1;
1342         }
1343     }
1344     pstrcat(se->idstr, sizeof(se->idstr), vmsd->name);
1345
1346     if (instance_id == -1) {
1347         se->instance_id = calculate_new_instance_id(se->idstr);
1348     } else {
1349         se->instance_id = instance_id;
1350     }
1351     assert(!se->compat || se->instance_id == 0);
1352     /* add at the end of list */
1353     QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1354     return 0;
1355 }
1356
1357 int vmstate_register(DeviceState *dev, int instance_id,
1358                      const VMStateDescription *vmsd, void *opaque)
1359 {
1360     return vmstate_register_with_alias_id(dev, instance_id, vmsd,
1361                                           opaque, -1, 0);
1362 }
1363
1364 void vmstate_unregister(DeviceState *dev, const VMStateDescription *vmsd,
1365                         void *opaque)
1366 {
1367     SaveStateEntry *se, *new_se;
1368
1369     QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1370         if (se->vmsd == vmsd && se->opaque == opaque) {
1371             QTAILQ_REMOVE(&savevm_handlers, se, entry);
1372             if (se->compat) {
1373                 g_free(se->compat);
1374             }
1375             g_free(se);
1376         }
1377     }
1378 }
1379
1380 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1381                                     void *opaque);
1382 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1383                                    void *opaque);
1384
1385 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1386                        void *opaque, int version_id)
1387 {
1388     VMStateField *field = vmsd->fields;
1389     int ret;
1390
1391     if (version_id > vmsd->version_id) {
1392         return -EINVAL;
1393     }
1394     if (version_id < vmsd->minimum_version_id_old) {
1395         return -EINVAL;
1396     }
1397     if  (version_id < vmsd->minimum_version_id) {
1398         return vmsd->load_state_old(f, opaque, version_id);
1399     }
1400     if (vmsd->pre_load) {
1401         int ret = vmsd->pre_load(opaque);
1402         if (ret)
1403             return ret;
1404     }
1405     while(field->name) {
1406         if ((field->field_exists &&
1407              field->field_exists(opaque, version_id)) ||
1408             (!field->field_exists &&
1409              field->version_id <= version_id)) {
1410             void *base_addr = opaque + field->offset;
1411             int i, n_elems = 1;
1412             int size = field->size;
1413
1414             if (field->flags & VMS_VBUFFER) {
1415                 size = *(int32_t *)(opaque+field->size_offset);
1416                 if (field->flags & VMS_MULTIPLY) {
1417                     size *= field->size;
1418                 }
1419             }
1420             if (field->flags & VMS_ARRAY) {
1421                 n_elems = field->num;
1422             } else if (field->flags & VMS_VARRAY_INT32) {
1423                 n_elems = *(int32_t *)(opaque+field->num_offset);
1424             } else if (field->flags & VMS_VARRAY_UINT32) {
1425                 n_elems = *(uint32_t *)(opaque+field->num_offset);
1426             } else if (field->flags & VMS_VARRAY_UINT16) {
1427                 n_elems = *(uint16_t *)(opaque+field->num_offset);
1428             } else if (field->flags & VMS_VARRAY_UINT8) {
1429                 n_elems = *(uint8_t *)(opaque+field->num_offset);
1430             }
1431             if (field->flags & VMS_POINTER) {
1432                 base_addr = *(void **)base_addr + field->start;
1433             }
1434             for (i = 0; i < n_elems; i++) {
1435                 void *addr = base_addr + size * i;
1436
1437                 if (field->flags & VMS_ARRAY_OF_POINTER) {
1438                     addr = *(void **)addr;
1439                 }
1440                 if (field->flags & VMS_STRUCT) {
1441                     ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1442                 } else {
1443                     ret = field->info->get(f, addr, size);
1444
1445                 }
1446                 if (ret < 0) {
1447                     return ret;
1448                 }
1449             }
1450         }
1451         field++;
1452     }
1453     ret = vmstate_subsection_load(f, vmsd, opaque);
1454     if (ret != 0) {
1455         return ret;
1456     }
1457     if (vmsd->post_load) {
1458         return vmsd->post_load(opaque, version_id);
1459     }
1460     return 0;
1461 }
1462
1463 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1464                         void *opaque)
1465 {
1466     VMStateField *field = vmsd->fields;
1467
1468     if (vmsd->pre_save) {
1469         vmsd->pre_save(opaque);
1470     }
1471     while(field->name) {
1472         if (!field->field_exists ||
1473             field->field_exists(opaque, vmsd->version_id)) {
1474             void *base_addr = opaque + field->offset;
1475             int i, n_elems = 1;
1476             int size = field->size;
1477
1478             if (field->flags & VMS_VBUFFER) {
1479                 size = *(int32_t *)(opaque+field->size_offset);
1480                 if (field->flags & VMS_MULTIPLY) {
1481                     size *= field->size;
1482                 }
1483             }
1484             if (field->flags & VMS_ARRAY) {
1485                 n_elems = field->num;
1486             } else if (field->flags & VMS_VARRAY_INT32) {
1487                 n_elems = *(int32_t *)(opaque+field->num_offset);
1488             } else if (field->flags & VMS_VARRAY_UINT32) {
1489                 n_elems = *(uint32_t *)(opaque+field->num_offset);
1490             } else if (field->flags & VMS_VARRAY_UINT16) {
1491                 n_elems = *(uint16_t *)(opaque+field->num_offset);
1492             } else if (field->flags & VMS_VARRAY_UINT8) {
1493                 n_elems = *(uint8_t *)(opaque+field->num_offset);
1494             }
1495             if (field->flags & VMS_POINTER) {
1496                 base_addr = *(void **)base_addr + field->start;
1497             }
1498             for (i = 0; i < n_elems; i++) {
1499                 void *addr = base_addr + size * i;
1500
1501                 if (field->flags & VMS_ARRAY_OF_POINTER) {
1502                     addr = *(void **)addr;
1503                 }
1504                 if (field->flags & VMS_STRUCT) {
1505                     vmstate_save_state(f, field->vmsd, addr);
1506                 } else {
1507                     field->info->put(f, addr, size);
1508                 }
1509             }
1510         }
1511         field++;
1512     }
1513     vmstate_subsection_save(f, vmsd, opaque);
1514 }
1515
1516 static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1517 {
1518     if (!se->vmsd) {         /* Old style */
1519         return se->ops->load_state(f, se->opaque, version_id);
1520     }
1521     return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1522 }
1523
1524 static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1525 {
1526     if (!se->vmsd) {         /* Old style */
1527         se->ops->save_state(f, se->opaque);
1528         return;
1529     }
1530     vmstate_save_state(f,se->vmsd, se->opaque);
1531 }
1532
1533 #define QEMU_VM_FILE_MAGIC           0x5145564d
1534 #define QEMU_VM_FILE_VERSION_COMPAT  0x00000002
1535 #define QEMU_VM_FILE_VERSION         0x00000003
1536
1537 #define QEMU_VM_EOF                  0x00
1538 #define QEMU_VM_SECTION_START        0x01
1539 #define QEMU_VM_SECTION_PART         0x02
1540 #define QEMU_VM_SECTION_END          0x03
1541 #define QEMU_VM_SECTION_FULL         0x04
1542 #define QEMU_VM_SUBSECTION           0x05
1543
1544 bool qemu_savevm_state_blocked(Error **errp)
1545 {
1546     SaveStateEntry *se;
1547
1548     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1549         if (se->no_migrate) {
1550             error_set(errp, QERR_MIGRATION_NOT_SUPPORTED, se->idstr);
1551             return true;
1552         }
1553     }
1554     return false;
1555 }
1556
1557 int qemu_savevm_state_begin(QEMUFile *f,
1558                             const MigrationParams *params)
1559 {
1560     SaveStateEntry *se;
1561     int ret;
1562
1563     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1564         if (!se->ops || !se->ops->set_params) {
1565             continue;
1566         }
1567         se->ops->set_params(params, se->opaque);
1568     }
1569     
1570     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1571     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1572
1573     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1574         int len;
1575
1576         if (!se->ops || !se->ops->save_live_setup) {
1577             continue;
1578         }
1579         if (se->ops && se->ops->is_active) {
1580             if (!se->ops->is_active(se->opaque)) {
1581                 continue;
1582             }
1583         }
1584         /* Section type */
1585         qemu_put_byte(f, QEMU_VM_SECTION_START);
1586         qemu_put_be32(f, se->section_id);
1587
1588         /* ID string */
1589         len = strlen(se->idstr);
1590         qemu_put_byte(f, len);
1591         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1592
1593         qemu_put_be32(f, se->instance_id);
1594         qemu_put_be32(f, se->version_id);
1595
1596         ret = se->ops->save_live_setup(f, se->opaque);
1597         if (ret < 0) {
1598             qemu_savevm_state_cancel(f);
1599             return ret;
1600         }
1601     }
1602     ret = qemu_file_get_error(f);
1603     if (ret != 0) {
1604         qemu_savevm_state_cancel(f);
1605     }
1606
1607     return ret;
1608
1609 }
1610
1611 /*
1612  * this function has three return values:
1613  *   negative: there was one error, and we have -errno.
1614  *   0 : We haven't finished, caller have to go again
1615  *   1 : We have finished, we can go to complete phase
1616  */
1617 int qemu_savevm_state_iterate(QEMUFile *f)
1618 {
1619     SaveStateEntry *se;
1620     int ret = 1;
1621
1622     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1623         if (!se->ops || !se->ops->save_live_iterate) {
1624             continue;
1625         }
1626         if (se->ops && se->ops->is_active) {
1627             if (!se->ops->is_active(se->opaque)) {
1628                 continue;
1629             }
1630         }
1631         if (qemu_file_rate_limit(f)) {
1632             return 0;
1633         }
1634         trace_savevm_section_start();
1635         /* Section type */
1636         qemu_put_byte(f, QEMU_VM_SECTION_PART);
1637         qemu_put_be32(f, se->section_id);
1638
1639         ret = se->ops->save_live_iterate(f, se->opaque);
1640         trace_savevm_section_end(se->section_id);
1641
1642         if (ret <= 0) {
1643             /* Do not proceed to the next vmstate before this one reported
1644                completion of the current stage. This serializes the migration
1645                and reduces the probability that a faster changing state is
1646                synchronized over and over again. */
1647             break;
1648         }
1649     }
1650     if (ret != 0) {
1651         return ret;
1652     }
1653     ret = qemu_file_get_error(f);
1654     if (ret != 0) {
1655         qemu_savevm_state_cancel(f);
1656     }
1657     return ret;
1658 }
1659
1660 int qemu_savevm_state_complete(QEMUFile *f)
1661 {
1662     SaveStateEntry *se;
1663     int ret;
1664
1665     cpu_synchronize_all_states();
1666
1667     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1668         if (!se->ops || !se->ops->save_live_complete) {
1669             continue;
1670         }
1671         if (se->ops && se->ops->is_active) {
1672             if (!se->ops->is_active(se->opaque)) {
1673                 continue;
1674             }
1675         }
1676         trace_savevm_section_start();
1677         /* Section type */
1678         qemu_put_byte(f, QEMU_VM_SECTION_END);
1679         qemu_put_be32(f, se->section_id);
1680
1681         ret = se->ops->save_live_complete(f, se->opaque);
1682         trace_savevm_section_end(se->section_id);
1683         if (ret < 0) {
1684             return ret;
1685         }
1686     }
1687
1688     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1689         int len;
1690
1691         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1692             continue;
1693         }
1694         trace_savevm_section_start();
1695         /* Section type */
1696         qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1697         qemu_put_be32(f, se->section_id);
1698
1699         /* ID string */
1700         len = strlen(se->idstr);
1701         qemu_put_byte(f, len);
1702         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1703
1704         qemu_put_be32(f, se->instance_id);
1705         qemu_put_be32(f, se->version_id);
1706
1707         vmstate_save(f, se);
1708         trace_savevm_section_end(se->section_id);
1709     }
1710
1711     qemu_put_byte(f, QEMU_VM_EOF);
1712
1713     return qemu_file_get_error(f);
1714 }
1715
1716 void qemu_savevm_state_cancel(QEMUFile *f)
1717 {
1718     SaveStateEntry *se;
1719
1720     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1721         if (se->ops && se->ops->cancel) {
1722             se->ops->cancel(se->opaque);
1723         }
1724     }
1725 }
1726
1727 static int qemu_savevm_state(QEMUFile *f)
1728 {
1729     int ret;
1730     MigrationParams params = {
1731         .blk = 0,
1732         .shared = 0
1733     };
1734
1735     if (qemu_savevm_state_blocked(NULL)) {
1736         ret = -EINVAL;
1737         goto out;
1738     }
1739
1740     ret = qemu_savevm_state_begin(f, &params);
1741     if (ret < 0)
1742         goto out;
1743
1744     do {
1745         ret = qemu_savevm_state_iterate(f);
1746         if (ret < 0)
1747             goto out;
1748     } while (ret == 0);
1749
1750     ret = qemu_savevm_state_complete(f);
1751
1752 out:
1753     if (ret == 0) {
1754         ret = qemu_file_get_error(f);
1755     }
1756
1757     return ret;
1758 }
1759
1760 static int qemu_save_device_state(QEMUFile *f)
1761 {
1762     SaveStateEntry *se;
1763
1764     qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1765     qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1766
1767     cpu_synchronize_all_states();
1768
1769     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1770         int len;
1771
1772         if (se->is_ram) {
1773             continue;
1774         }
1775         if ((!se->ops || !se->ops->save_state) && !se->vmsd) {
1776             continue;
1777         }
1778
1779         /* Section type */
1780         qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1781         qemu_put_be32(f, se->section_id);
1782
1783         /* ID string */
1784         len = strlen(se->idstr);
1785         qemu_put_byte(f, len);
1786         qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1787
1788         qemu_put_be32(f, se->instance_id);
1789         qemu_put_be32(f, se->version_id);
1790
1791         vmstate_save(f, se);
1792     }
1793
1794     qemu_put_byte(f, QEMU_VM_EOF);
1795
1796     return qemu_file_get_error(f);
1797 }
1798
1799 static SaveStateEntry *find_se(const char *idstr, int instance_id)
1800 {
1801     SaveStateEntry *se;
1802
1803     QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1804         if (!strcmp(se->idstr, idstr) &&
1805             (instance_id == se->instance_id ||
1806              instance_id == se->alias_id))
1807             return se;
1808         /* Migrating from an older version? */
1809         if (strstr(se->idstr, idstr) && se->compat) {
1810             if (!strcmp(se->compat->idstr, idstr) &&
1811                 (instance_id == se->compat->instance_id ||
1812                  instance_id == se->alias_id))
1813                 return se;
1814         }
1815     }
1816     return NULL;
1817 }
1818
1819 static const VMStateDescription *vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
1820 {
1821     while(sub && sub->needed) {
1822         if (strcmp(idstr, sub->vmsd->name) == 0) {
1823             return sub->vmsd;
1824         }
1825         sub++;
1826     }
1827     return NULL;
1828 }
1829
1830 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
1831                                    void *opaque)
1832 {
1833     while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
1834         char idstr[256];
1835         int ret;
1836         uint8_t version_id, len, size;
1837         const VMStateDescription *sub_vmsd;
1838
1839         len = qemu_peek_byte(f, 1);
1840         if (len < strlen(vmsd->name) + 1) {
1841             /* subsection name has be be "section_name/a" */
1842             return 0;
1843         }
1844         size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
1845         if (size != len) {
1846             return 0;
1847         }
1848         idstr[size] = 0;
1849
1850         if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
1851             /* it don't have a valid subsection name */
1852             return 0;
1853         }
1854         sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
1855         if (sub_vmsd == NULL) {
1856             return -ENOENT;
1857         }
1858         qemu_file_skip(f, 1); /* subsection */
1859         qemu_file_skip(f, 1); /* len */
1860         qemu_file_skip(f, len); /* idstr */
1861         version_id = qemu_get_be32(f);
1862
1863         ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
1864         if (ret) {
1865             return ret;
1866         }
1867     }
1868     return 0;
1869 }
1870
1871 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
1872                                     void *opaque)
1873 {
1874     const VMStateSubsection *sub = vmsd->subsections;
1875
1876     while (sub && sub->needed) {
1877         if (sub->needed(opaque)) {
1878             const VMStateDescription *vmsd = sub->vmsd;
1879             uint8_t len;
1880
1881             qemu_put_byte(f, QEMU_VM_SUBSECTION);
1882             len = strlen(vmsd->name);
1883             qemu_put_byte(f, len);
1884             qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
1885             qemu_put_be32(f, vmsd->version_id);
1886             vmstate_save_state(f, vmsd, opaque);
1887         }
1888         sub++;
1889     }
1890 }
1891
1892 typedef struct LoadStateEntry {
1893     QLIST_ENTRY(LoadStateEntry) entry;
1894     SaveStateEntry *se;
1895     int section_id;
1896     int version_id;
1897 } LoadStateEntry;
1898
1899 int qemu_loadvm_state(QEMUFile *f)
1900 {
1901     QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1902         QLIST_HEAD_INITIALIZER(loadvm_handlers);
1903     LoadStateEntry *le, *new_le;
1904     uint8_t section_type;
1905     unsigned int v;
1906     int ret;
1907
1908     if (qemu_savevm_state_blocked(NULL)) {
1909         return -EINVAL;
1910     }
1911
1912     v = qemu_get_be32(f);
1913     if (v != QEMU_VM_FILE_MAGIC)
1914         return -EINVAL;
1915
1916     v = qemu_get_be32(f);
1917     if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1918         fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1919         return -ENOTSUP;
1920     }
1921     if (v != QEMU_VM_FILE_VERSION)
1922         return -ENOTSUP;
1923
1924     while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1925         uint32_t instance_id, version_id, section_id;
1926         SaveStateEntry *se;
1927         char idstr[257];
1928         int len;
1929
1930         switch (section_type) {
1931         case QEMU_VM_SECTION_START:
1932         case QEMU_VM_SECTION_FULL:
1933             /* Read section start */
1934             section_id = qemu_get_be32(f);
1935             len = qemu_get_byte(f);
1936             qemu_get_buffer(f, (uint8_t *)idstr, len);
1937             idstr[len] = 0;
1938             instance_id = qemu_get_be32(f);
1939             version_id = qemu_get_be32(f);
1940
1941             /* Find savevm section */
1942             se = find_se(idstr, instance_id);
1943             if (se == NULL) {
1944                 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1945                 ret = -EINVAL;
1946                 goto out;
1947             }
1948
1949             /* Validate version */
1950             if (version_id > se->version_id) {
1951                 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1952                         version_id, idstr, se->version_id);
1953                 ret = -EINVAL;
1954                 goto out;
1955             }
1956
1957             /* Add entry */
1958             le = g_malloc0(sizeof(*le));
1959
1960             le->se = se;
1961             le->section_id = section_id;
1962             le->version_id = version_id;
1963             QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1964
1965             ret = vmstate_load(f, le->se, le->version_id);
1966             if (ret < 0) {
1967                 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1968                         instance_id, idstr);
1969                 goto out;
1970             }
1971             break;
1972         case QEMU_VM_SECTION_PART:
1973         case QEMU_VM_SECTION_END:
1974             section_id = qemu_get_be32(f);
1975
1976             QLIST_FOREACH(le, &loadvm_handlers, entry) {
1977                 if (le->section_id == section_id) {
1978                     break;
1979                 }
1980             }
1981             if (le == NULL) {
1982                 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1983                 ret = -EINVAL;
1984                 goto out;
1985             }
1986
1987             ret = vmstate_load(f, le->se, le->version_id);
1988             if (ret < 0) {
1989                 fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1990                         section_id);
1991                 goto out;
1992             }
1993             break;
1994         default:
1995             fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1996             ret = -EINVAL;
1997             goto out;
1998         }
1999     }
2000
2001     cpu_synchronize_all_post_init();
2002
2003     ret = 0;
2004
2005 out:
2006     QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
2007         QLIST_REMOVE(le, entry);
2008         g_free(le);
2009     }
2010
2011     if (ret == 0) {
2012         ret = qemu_file_get_error(f);
2013     }
2014
2015     return ret;
2016 }
2017
2018 static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
2019                               const char *name)
2020 {
2021     QEMUSnapshotInfo *sn_tab, *sn;
2022     int nb_sns, i, ret;
2023
2024     ret = -ENOENT;
2025     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2026     if (nb_sns < 0)
2027         return ret;
2028     for(i = 0; i < nb_sns; i++) {
2029         sn = &sn_tab[i];
2030         if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
2031             *sn_info = *sn;
2032             ret = 0;
2033             break;
2034         }
2035     }
2036     g_free(sn_tab);
2037     return ret;
2038 }
2039
2040 /*
2041  * Deletes snapshots of a given name in all opened images.
2042  */
2043 static int del_existing_snapshots(Monitor *mon, const char *name)
2044 {
2045     BlockDriverState *bs;
2046     QEMUSnapshotInfo sn1, *snapshot = &sn1;
2047     int ret;
2048
2049     bs = NULL;
2050     while ((bs = bdrv_next(bs))) {
2051         if (bdrv_can_snapshot(bs) &&
2052             bdrv_snapshot_find(bs, snapshot, name) >= 0)
2053         {
2054             ret = bdrv_snapshot_delete(bs, name);
2055             if (ret < 0) {
2056                 monitor_printf(mon,
2057                                "Error while deleting snapshot on '%s'\n",
2058                                bdrv_get_device_name(bs));
2059                 return -1;
2060             }
2061         }
2062     }
2063
2064     return 0;
2065 }
2066
2067 void do_savevm(Monitor *mon, const QDict *qdict)
2068 {
2069     BlockDriverState *bs, *bs1;
2070     QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
2071     int ret;
2072     QEMUFile *f;
2073     int saved_vm_running;
2074     uint64_t vm_state_size;
2075 #ifdef _WIN32
2076     struct _timeb tb;
2077     struct tm *ptm;
2078 #else
2079     struct timeval tv;
2080     struct tm tm;
2081 #endif
2082     const char *name = qdict_get_try_str(qdict, "name");
2083
2084     /* Verify if there is a device that doesn't support snapshots and is writable */
2085     bs = NULL;
2086     while ((bs = bdrv_next(bs))) {
2087
2088         if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2089             continue;
2090         }
2091
2092         if (!bdrv_can_snapshot(bs)) {
2093             monitor_printf(mon, "Device '%s' is writable but does not support snapshots.\n",
2094                                bdrv_get_device_name(bs));
2095             return;
2096         }
2097     }
2098
2099     bs = bdrv_snapshots();
2100     if (!bs) {
2101         monitor_printf(mon, "No block device can accept snapshots\n");
2102         return;
2103     }
2104
2105     saved_vm_running = runstate_is_running();
2106     vm_stop(RUN_STATE_SAVE_VM);
2107
2108     memset(sn, 0, sizeof(*sn));
2109
2110     /* fill auxiliary fields */
2111 #ifdef _WIN32
2112     _ftime(&tb);
2113     sn->date_sec = tb.time;
2114     sn->date_nsec = tb.millitm * 1000000;
2115 #else
2116     gettimeofday(&tv, NULL);
2117     sn->date_sec = tv.tv_sec;
2118     sn->date_nsec = tv.tv_usec * 1000;
2119 #endif
2120     sn->vm_clock_nsec = qemu_get_clock_ns(vm_clock);
2121
2122     if (name) {
2123         ret = bdrv_snapshot_find(bs, old_sn, name);
2124         if (ret >= 0) {
2125             pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
2126             pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
2127         } else {
2128             pstrcpy(sn->name, sizeof(sn->name), name);
2129         }
2130     } else {
2131 #ifdef _WIN32
2132         time_t t = tb.time;
2133         ptm = localtime(&t);
2134         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", ptm);
2135 #else
2136         /* cast below needed for OpenBSD where tv_sec is still 'long' */
2137         localtime_r((const time_t *)&tv.tv_sec, &tm);
2138         strftime(sn->name, sizeof(sn->name), "vm-%Y%m%d%H%M%S", &tm);
2139 #endif
2140     }
2141
2142     /* Delete old snapshots of the same name */
2143     if (name && del_existing_snapshots(mon, name) < 0) {
2144         goto the_end;
2145     }
2146
2147     /* save the VM state */
2148     f = qemu_fopen_bdrv(bs, 1);
2149     if (!f) {
2150         monitor_printf(mon, "Could not open VM state file\n");
2151         goto the_end;
2152     }
2153     ret = qemu_savevm_state(f);
2154     vm_state_size = qemu_ftell(f);
2155     qemu_fclose(f);
2156     if (ret < 0) {
2157         monitor_printf(mon, "Error %d while writing VM\n", ret);
2158         goto the_end;
2159     }
2160
2161     /* create the snapshots */
2162
2163     bs1 = NULL;
2164     while ((bs1 = bdrv_next(bs1))) {
2165         if (bdrv_can_snapshot(bs1)) {
2166             /* Write VM state size only to the image that contains the state */
2167             sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
2168             ret = bdrv_snapshot_create(bs1, sn);
2169             if (ret < 0) {
2170                 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
2171                                bdrv_get_device_name(bs1));
2172             }
2173         }
2174     }
2175
2176  the_end:
2177     if (saved_vm_running)
2178         vm_start();
2179 }
2180
2181 void qmp_xen_save_devices_state(const char *filename, Error **errp)
2182 {
2183     QEMUFile *f;
2184     int saved_vm_running;
2185     int ret;
2186
2187     saved_vm_running = runstate_is_running();
2188     vm_stop(RUN_STATE_SAVE_VM);
2189
2190     f = qemu_fopen(filename, "wb");
2191     if (!f) {
2192         error_set(errp, QERR_OPEN_FILE_FAILED, filename);
2193         goto the_end;
2194     }
2195     ret = qemu_save_device_state(f);
2196     qemu_fclose(f);
2197     if (ret < 0) {
2198         error_set(errp, QERR_IO_ERROR);
2199     }
2200
2201  the_end:
2202     if (saved_vm_running)
2203         vm_start();
2204     return;
2205 }
2206
2207 int load_vmstate(const char *name)
2208 {
2209     BlockDriverState *bs, *bs_vm_state;
2210     QEMUSnapshotInfo sn;
2211     QEMUFile *f;
2212     int ret;
2213
2214     bs_vm_state = bdrv_snapshots();
2215     if (!bs_vm_state) {
2216         error_report("No block device supports snapshots");
2217         return -ENOTSUP;
2218     }
2219
2220     /* Don't even try to load empty VM states */
2221     ret = bdrv_snapshot_find(bs_vm_state, &sn, name);
2222     if (ret < 0) {
2223         return ret;
2224     } else if (sn.vm_state_size == 0) {
2225         error_report("This is a disk-only snapshot. Revert to it offline "
2226             "using qemu-img.");
2227         return -EINVAL;
2228     }
2229
2230     /* Verify if there is any device that doesn't support snapshots and is
2231     writable and check if the requested snapshot is available too. */
2232     bs = NULL;
2233     while ((bs = bdrv_next(bs))) {
2234
2235         if (!bdrv_is_inserted(bs) || bdrv_is_read_only(bs)) {
2236             continue;
2237         }
2238
2239         if (!bdrv_can_snapshot(bs)) {
2240             error_report("Device '%s' is writable but does not support snapshots.",
2241                                bdrv_get_device_name(bs));
2242             return -ENOTSUP;
2243         }
2244
2245         ret = bdrv_snapshot_find(bs, &sn, name);
2246         if (ret < 0) {
2247             error_report("Device '%s' does not have the requested snapshot '%s'",
2248                            bdrv_get_device_name(bs), name);
2249             return ret;
2250         }
2251     }
2252
2253     /* Flush all IO requests so they don't interfere with the new state.  */
2254     bdrv_drain_all();
2255
2256     bs = NULL;
2257     while ((bs = bdrv_next(bs))) {
2258         if (bdrv_can_snapshot(bs)) {
2259             ret = bdrv_snapshot_goto(bs, name);
2260             if (ret < 0) {
2261                 error_report("Error %d while activating snapshot '%s' on '%s'",
2262                              ret, name, bdrv_get_device_name(bs));
2263                 return ret;
2264             }
2265         }
2266     }
2267
2268     /* restore the VM state */
2269     f = qemu_fopen_bdrv(bs_vm_state, 0);
2270     if (!f) {
2271         error_report("Could not open VM state file");
2272         return -EINVAL;
2273     }
2274
2275     qemu_system_reset(VMRESET_SILENT);
2276     ret = qemu_loadvm_state(f);
2277
2278     qemu_fclose(f);
2279     if (ret < 0) {
2280         error_report("Error %d while loading VM state", ret);
2281         return ret;
2282     }
2283
2284     return 0;
2285 }
2286
2287 void do_delvm(Monitor *mon, const QDict *qdict)
2288 {
2289     BlockDriverState *bs, *bs1;
2290     int ret;
2291     const char *name = qdict_get_str(qdict, "name");
2292
2293     bs = bdrv_snapshots();
2294     if (!bs) {
2295         monitor_printf(mon, "No block device supports snapshots\n");
2296         return;
2297     }
2298
2299     bs1 = NULL;
2300     while ((bs1 = bdrv_next(bs1))) {
2301         if (bdrv_can_snapshot(bs1)) {
2302             ret = bdrv_snapshot_delete(bs1, name);
2303             if (ret < 0) {
2304                 if (ret == -ENOTSUP)
2305                     monitor_printf(mon,
2306                                    "Snapshots not supported on device '%s'\n",
2307                                    bdrv_get_device_name(bs1));
2308                 else
2309                     monitor_printf(mon, "Error %d while deleting snapshot on "
2310                                    "'%s'\n", ret, bdrv_get_device_name(bs1));
2311             }
2312         }
2313     }
2314 }
2315
2316 void do_info_snapshots(Monitor *mon)
2317 {
2318     BlockDriverState *bs, *bs1;
2319     QEMUSnapshotInfo *sn_tab, *sn, s, *sn_info = &s;
2320     int nb_sns, i, ret, available;
2321     int total;
2322     int *available_snapshots;
2323     char buf[256];
2324
2325     bs = bdrv_snapshots();
2326     if (!bs) {
2327         monitor_printf(mon, "No available block device supports snapshots\n");
2328         return;
2329     }
2330
2331     nb_sns = bdrv_snapshot_list(bs, &sn_tab);
2332     if (nb_sns < 0) {
2333         monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
2334         return;
2335     }
2336
2337     if (nb_sns == 0) {
2338         monitor_printf(mon, "There is no snapshot available.\n");
2339         return;
2340     }
2341
2342     available_snapshots = g_malloc0(sizeof(int) * nb_sns);
2343     total = 0;
2344     for (i = 0; i < nb_sns; i++) {
2345         sn = &sn_tab[i];
2346         available = 1;
2347         bs1 = NULL;
2348
2349         while ((bs1 = bdrv_next(bs1))) {
2350             if (bdrv_can_snapshot(bs1) && bs1 != bs) {
2351                 ret = bdrv_snapshot_find(bs1, sn_info, sn->id_str);
2352                 if (ret < 0) {
2353                     available = 0;
2354                     break;
2355                 }
2356             }
2357         }
2358
2359         if (available) {
2360             available_snapshots[total] = i;
2361             total++;
2362         }
2363     }
2364
2365     if (total > 0) {
2366         monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
2367         for (i = 0; i < total; i++) {
2368             sn = &sn_tab[available_snapshots[i]];
2369             monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
2370         }
2371     } else {
2372         monitor_printf(mon, "There is no suitable snapshot available\n");
2373     }
2374
2375     g_free(sn_tab);
2376     g_free(available_snapshots);
2377
2378 }
2379
2380 void vmstate_register_ram(MemoryRegion *mr, DeviceState *dev)
2381 {
2382     qemu_ram_set_idstr(memory_region_get_ram_addr(mr) & TARGET_PAGE_MASK,
2383                        memory_region_name(mr), dev);
2384 }
2385
2386 void vmstate_unregister_ram(MemoryRegion *mr, DeviceState *dev)
2387 {
2388     /* Nothing do to while the implementation is in RAMBlock */
2389 }
2390
2391 void vmstate_register_ram_global(MemoryRegion *mr)
2392 {
2393     vmstate_register_ram(mr, NULL);
2394 }
2395
2396 /*
2397   page = zrun nzrun
2398        | zrun nzrun page
2399
2400   zrun = length
2401
2402   nzrun = length byte...
2403
2404   length = uleb128 encoded integer
2405  */
2406 int xbzrle_encode_buffer(uint8_t *old_buf, uint8_t *new_buf, int slen,
2407                          uint8_t *dst, int dlen)
2408 {
2409     uint32_t zrun_len = 0, nzrun_len = 0;
2410     int d = 0, i = 0;
2411     long res, xor;
2412     uint8_t *nzrun_start = NULL;
2413
2414     g_assert(!(((uintptr_t)old_buf | (uintptr_t)new_buf | slen) %
2415                sizeof(long)));
2416
2417     while (i < slen) {
2418         /* overflow */
2419         if (d + 2 > dlen) {
2420             return -1;
2421         }
2422
2423         /* not aligned to sizeof(long) */
2424         res = (slen - i) % sizeof(long);
2425         while (res && old_buf[i] == new_buf[i]) {
2426             zrun_len++;
2427             i++;
2428             res--;
2429         }
2430
2431         /* word at a time for speed */
2432         if (!res) {
2433             while (i < slen &&
2434                    (*(long *)(old_buf + i)) == (*(long *)(new_buf + i))) {
2435                 i += sizeof(long);
2436                 zrun_len += sizeof(long);
2437             }
2438
2439             /* go over the rest */
2440             while (i < slen && old_buf[i] == new_buf[i]) {
2441                 zrun_len++;
2442                 i++;
2443             }
2444         }
2445
2446         /* buffer unchanged */
2447         if (zrun_len == slen) {
2448             return 0;
2449         }
2450
2451         /* skip last zero run */
2452         if (i == slen) {
2453             return d;
2454         }
2455
2456         d += uleb128_encode_small(dst + d, zrun_len);
2457
2458         zrun_len = 0;
2459         nzrun_start = new_buf + i;
2460
2461         /* overflow */
2462         if (d + 2 > dlen) {
2463             return -1;
2464         }
2465         /* not aligned to sizeof(long) */
2466         res = (slen - i) % sizeof(long);
2467         while (res && old_buf[i] != new_buf[i]) {
2468             i++;
2469             nzrun_len++;
2470             res--;
2471         }
2472
2473         /* word at a time for speed, use of 32-bit long okay */
2474         if (!res) {
2475             /* truncation to 32-bit long okay */
2476             long mask = (long)0x0101010101010101ULL;
2477             while (i < slen) {
2478                 xor = *(long *)(old_buf + i) ^ *(long *)(new_buf + i);
2479                 if ((xor - mask) & ~xor & (mask << 7)) {
2480                     /* found the end of an nzrun within the current long */
2481                     while (old_buf[i] != new_buf[i]) {
2482                         nzrun_len++;
2483                         i++;
2484                     }
2485                     break;
2486                 } else {
2487                     i += sizeof(long);
2488                     nzrun_len += sizeof(long);
2489                 }
2490             }
2491         }
2492
2493         d += uleb128_encode_small(dst + d, nzrun_len);
2494         /* overflow */
2495         if (d + nzrun_len > dlen) {
2496             return -1;
2497         }
2498         memcpy(dst + d, nzrun_start, nzrun_len);
2499         d += nzrun_len;
2500         nzrun_len = 0;
2501     }
2502
2503     return d;
2504 }
2505
2506 int xbzrle_decode_buffer(uint8_t *src, int slen, uint8_t *dst, int dlen)
2507 {
2508     int i = 0, d = 0;
2509     int ret;
2510     uint32_t count = 0;
2511
2512     while (i < slen) {
2513
2514         /* zrun */
2515         if ((slen - i) < 2) {
2516             return -1;
2517         }
2518
2519         ret = uleb128_decode_small(src + i, &count);
2520         if (ret < 0 || (i && !count)) {
2521             return -1;
2522         }
2523         i += ret;
2524         d += count;
2525
2526         /* overflow */
2527         if (d > dlen) {
2528             return -1;
2529         }
2530
2531         /* nzrun */
2532         if ((slen - i) < 2) {
2533             return -1;
2534         }
2535
2536         ret = uleb128_decode_small(src + i, &count);
2537         if (ret < 0 || !count) {
2538             return -1;
2539         }
2540         i += ret;
2541
2542         /* overflow */
2543         if (d + count > dlen || i + count > slen) {
2544             return -1;
2545         }
2546
2547         memcpy(dst + d, src + i, count);
2548         d += count;
2549         i += count;
2550     }
2551
2552     return d;
2553 }