blob: 8f0d8f1fb3ce9789d08ef86a790c8096debc2dc4 [file] [log] [blame]
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -07001/*
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 <signal.h>
27#include <time.h>
28#include <errno.h>
29#include <sys/time.h>
30#include <zlib.h>
31
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070032/* Needed early for CONFIG_BSD etc. */
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070033#include "config-host.h"
34
35#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>
45#if defined(__NetBSD__)
46#include <net/if_tap.h>
47#endif
48#ifdef __linux__
49#include <linux/if_tun.h>
50#endif
51#include <arpa/inet.h>
52#include <dirent.h>
53#include <netdb.h>
54#include <sys/select.h>
David 'Digit' Turner2c538c82010-05-10 16:48:20 -070055#ifdef CONFIG_BSD
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -070056#include <sys/stat.h>
57#if defined(__FreeBSD__) || defined(__DragonFly__)
58#include <libutil.h>
59#else
60#include <util.h>
61#endif
62#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
63#include <freebsd/stdlib.h>
64#else
65#ifdef __linux__
66#include <pty.h>
67#include <malloc.h>
68#include <linux/rtc.h>
69#endif
70#endif
71#endif
72
73#ifdef _WIN32
74#include <windows.h>
75#include <malloc.h>
76#include <sys/timeb.h>
77#include <mmsystem.h>
78#define getopt_long_only getopt_long
79#define memalign(align, size) malloc(size)
80#endif
81
82#include "qemu-common.h"
83#include "hw/hw.h"
84#include "net.h"
85#include "monitor.h"
86#include "sysemu.h"
87#include "qemu-timer.h"
88#include "qemu-char.h"
89#include "block.h"
90#include "audio/audio.h"
91#include "migration.h"
92#include "qemu_socket.h"
93#include "qemu_file.h"
94
95/* point to the block driver where the snapshots are managed */
96static BlockDriverState *bs_snapshots;
97
98#define SELF_ANNOUNCE_ROUNDS 5
99#define ETH_P_EXPERIMENTAL 0x01F1 /* just a number */
100//#define ETH_P_EXPERIMENTAL 0x0012 /* make it the size of the packet */
101#define EXPERIMENTAL_MAGIC 0xf1f23f4f
102
103static int announce_self_create(uint8_t *buf,
104 uint8_t *mac_addr)
105{
106 uint32_t magic = EXPERIMENTAL_MAGIC;
107 uint16_t proto = htons(ETH_P_EXPERIMENTAL);
108
109 /* FIXME: should we send a different packet (arp/rarp/ping)? */
110
111 memset(buf, 0, 64);
112 memset(buf, 0xff, 6); /* h_dst */
113 memcpy(buf + 6, mac_addr, 6); /* h_src */
114 memcpy(buf + 12, &proto, 2); /* h_proto */
115 memcpy(buf + 14, &magic, 4); /* magic */
116
117 return 64; /* len */
118}
119
120static void qemu_announce_self_once(void *opaque)
121{
122 int i, len;
123 VLANState *vlan;
124 VLANClientState *vc;
125 uint8_t buf[256];
126 static int count = SELF_ANNOUNCE_ROUNDS;
127 QEMUTimer *timer = *(QEMUTimer **)opaque;
128
129 for (i = 0; i < MAX_NICS; i++) {
130 if (!nd_table[i].used)
131 continue;
132 len = announce_self_create(buf, nd_table[i].macaddr);
133 vlan = nd_table[i].vlan;
134 for(vc = vlan->first_client; vc != NULL; vc = vc->next) {
135 vc->receive(vc, buf, len);
136 }
137 }
138 if (count--) {
139 qemu_mod_timer(timer, qemu_get_clock(rt_clock) + 100);
140 } else {
141 qemu_del_timer(timer);
142 qemu_free_timer(timer);
143 }
144}
145
146void qemu_announce_self(void)
147{
148 static QEMUTimer *timer;
149 timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
150 qemu_announce_self_once(&timer);
151}
152
153/***********************************************************/
154/* savevm/loadvm support */
155
156#define IO_BUF_SIZE 32768
157
158struct QEMUFile {
159 QEMUFilePutBufferFunc *put_buffer;
160 QEMUFileGetBufferFunc *get_buffer;
161 QEMUFileCloseFunc *close;
162 QEMUFileRateLimit *rate_limit;
163 QEMUFileSetRateLimit *set_rate_limit;
David Turnera12820e2010-09-09 21:16:39 +0200164 QEMUFileGetRateLimit *get_rate_limit;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700165 void *opaque;
166 int is_write;
167
168 int64_t buf_offset; /* start of buffer when writing, end of buffer
169 when reading */
170 int buf_index;
171 int buf_size; /* 0 when writing */
172 uint8_t buf[IO_BUF_SIZE];
173
174 int has_error;
175};
176
177typedef struct QEMUFilePopen
178{
179 FILE *popen_file;
180 QEMUFile *file;
181} QEMUFilePopen;
182
183typedef struct QEMUFileSocket
184{
185 int fd;
186 QEMUFile *file;
187} QEMUFileSocket;
188
189static int file_socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
190{
191 QEMUFileSocket *s = opaque;
192 ssize_t len;
193
194 do {
195 len = recv(s->fd, (void *)buf, size, 0);
196 } while (len == -1 && socket_error() == EINTR);
197
198 if (len == -1)
199 len = -socket_error();
200
201 return len;
202}
203
204static int file_socket_close(void *opaque)
205{
206 QEMUFileSocket *s = opaque;
207 qemu_free(s);
208 return 0;
209}
210
211static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
212{
213 QEMUFilePopen *s = opaque;
214 return fwrite(buf, 1, size, s->popen_file);
215}
216
217static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
218{
219 QEMUFilePopen *s = opaque;
220 FILE *fp = s->popen_file;
221 int bytes;
222
223 do {
224 clearerr(fp);
225 bytes = fread(buf, 1, size, fp);
226 } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
227 return bytes;
228}
229
230static int popen_close(void *opaque)
231{
232 QEMUFilePopen *s = opaque;
233 pclose(s->popen_file);
234 qemu_free(s);
235 return 0;
236}
237
238QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
239{
240 QEMUFilePopen *s;
241
242 if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
243 fprintf(stderr, "qemu_popen: Argument validity check failed\n");
244 return NULL;
245 }
246
247 s = qemu_mallocz(sizeof(QEMUFilePopen));
248
249 s->popen_file = popen_file;
250
251 if(mode[0] == 'r') {
David Turnera12820e2010-09-09 21:16:39 +0200252 s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700253 } else {
David Turnera12820e2010-09-09 21:16:39 +0200254 s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700255 }
256 return s->file;
257}
258
259QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
260{
261 FILE *popen_file;
262
263 popen_file = popen(command, mode);
264 if(popen_file == NULL) {
265 return NULL;
266 }
267
268 return qemu_popen(popen_file, mode);
269}
270
271int qemu_popen_fd(QEMUFile *f)
272{
273 QEMUFilePopen *p;
274 int fd;
275
276 p = (QEMUFilePopen *)f->opaque;
277 fd = fileno(p->popen_file);
278
279 return fd;
280}
281
282QEMUFile *qemu_fopen_socket(int fd)
283{
284 QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
285
286 s->fd = fd;
David Turnera12820e2010-09-09 21:16:39 +0200287 s->file = qemu_fopen_ops(s, NULL, file_socket_get_buffer, file_socket_close, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700288 return s->file;
289}
290
291typedef struct QEMUFileStdio
292{
293 FILE *outfile;
294} QEMUFileStdio;
295
296static int file_put_buffer(void *opaque, const uint8_t *buf,
297 int64_t pos, int size)
298{
299 QEMUFileStdio *s = opaque;
300 fseek(s->outfile, pos, SEEK_SET);
301 fwrite(buf, 1, size, s->outfile);
302 return size;
303}
304
305static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
306{
307 QEMUFileStdio *s = opaque;
308 fseek(s->outfile, pos, SEEK_SET);
309 return fread(buf, 1, size, s->outfile);
310}
311
312static int file_close(void *opaque)
313{
314 QEMUFileStdio *s = opaque;
315 fclose(s->outfile);
316 qemu_free(s);
317 return 0;
318}
319
320QEMUFile *qemu_fopen(const char *filename, const char *mode)
321{
322 QEMUFileStdio *s;
323
324 s = qemu_mallocz(sizeof(QEMUFileStdio));
325
326 s->outfile = fopen(filename, mode);
327 if (!s->outfile)
328 goto fail;
329
330 if (!strcmp(mode, "wb"))
David Turnera12820e2010-09-09 21:16:39 +0200331 return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700332 else if (!strcmp(mode, "rb"))
David Turnera12820e2010-09-09 21:16:39 +0200333 return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700334
335fail:
336 if (s->outfile)
337 fclose(s->outfile);
338 qemu_free(s);
339 return NULL;
340}
341
342typedef struct QEMUFileBdrv
343{
344 BlockDriverState *bs;
345 int64_t base_offset;
346} QEMUFileBdrv;
347
348static int block_put_buffer(void *opaque, const uint8_t *buf,
349 int64_t pos, int size)
350{
351 QEMUFileBdrv *s = opaque;
352 bdrv_put_buffer(s->bs, buf, s->base_offset + pos, size);
353 return size;
354}
355
356static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
357{
358 QEMUFileBdrv *s = opaque;
359 return bdrv_get_buffer(s->bs, buf, s->base_offset + pos, size);
360}
361
362static int bdrv_fclose(void *opaque)
363{
364 QEMUFileBdrv *s = opaque;
365 qemu_free(s);
366 return 0;
367}
368
369static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int64_t offset, int is_writable)
370{
371 QEMUFileBdrv *s;
372
373 s = qemu_mallocz(sizeof(QEMUFileBdrv));
374
375 s->bs = bs;
376 s->base_offset = offset;
377
378 if (is_writable)
David Turnera12820e2010-09-09 21:16:39 +0200379 return qemu_fopen_ops(s, block_put_buffer, NULL, bdrv_fclose, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700380
David Turnera12820e2010-09-09 21:16:39 +0200381 return qemu_fopen_ops(s, NULL, block_get_buffer, bdrv_fclose, NULL, NULL, NULL);
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700382}
383
384QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
385 QEMUFileGetBufferFunc *get_buffer,
386 QEMUFileCloseFunc *close,
387 QEMUFileRateLimit *rate_limit,
David Turnera12820e2010-09-09 21:16:39 +0200388 QEMUFileSetRateLimit *set_rate_limit,
389 QEMUFileGetRateLimit *get_rate_limit)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700390{
391 QEMUFile *f;
392
393 f = qemu_mallocz(sizeof(QEMUFile));
394
395 f->opaque = opaque;
396 f->put_buffer = put_buffer;
397 f->get_buffer = get_buffer;
398 f->close = close;
399 f->rate_limit = rate_limit;
400 f->set_rate_limit = set_rate_limit;
401 f->is_write = 0;
402
403 return f;
404}
405
406int qemu_file_has_error(QEMUFile *f)
407{
408 return f->has_error;
409}
410
411void qemu_file_set_error(QEMUFile *f)
412{
413 f->has_error = 1;
414}
415
416void qemu_fflush(QEMUFile *f)
417{
418 if (!f->put_buffer)
419 return;
420
421 if (f->is_write && f->buf_index > 0) {
422 int len;
423
424 len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
425 if (len > 0)
426 f->buf_offset += f->buf_index;
427 else
428 f->has_error = 1;
429 f->buf_index = 0;
430 }
431}
432
433static void qemu_fill_buffer(QEMUFile *f)
434{
435 int len;
436
437 if (!f->get_buffer)
438 return;
439
440 if (f->is_write)
441 abort();
442
443 len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
444 if (len > 0) {
445 f->buf_index = 0;
446 f->buf_size = len;
447 f->buf_offset += len;
448 } else if (len != -EAGAIN)
449 f->has_error = 1;
450}
451
452int qemu_fclose(QEMUFile *f)
453{
454 int ret = 0;
455 qemu_fflush(f);
456 if (f->close)
457 ret = f->close(f->opaque);
458 qemu_free(f);
459 return ret;
460}
461
462void qemu_file_put_notify(QEMUFile *f)
463{
464 f->put_buffer(f->opaque, NULL, 0, 0);
465}
466
467void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
468{
469 int l;
470
471 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
472 fprintf(stderr,
473 "Attempted to write to buffer while read buffer is not empty\n");
474 abort();
475 }
476
477 while (!f->has_error && size > 0) {
478 l = IO_BUF_SIZE - f->buf_index;
479 if (l > size)
480 l = size;
481 memcpy(f->buf + f->buf_index, buf, l);
482 f->is_write = 1;
483 f->buf_index += l;
484 buf += l;
485 size -= l;
486 if (f->buf_index >= IO_BUF_SIZE)
487 qemu_fflush(f);
488 }
489}
490
491void qemu_put_byte(QEMUFile *f, int v)
492{
493 if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
494 fprintf(stderr,
495 "Attempted to write to buffer while read buffer is not empty\n");
496 abort();
497 }
498
499 f->buf[f->buf_index++] = v;
500 f->is_write = 1;
501 if (f->buf_index >= IO_BUF_SIZE)
502 qemu_fflush(f);
503}
504
505int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
506{
507 int size, l;
508
509 if (f->is_write)
510 abort();
511
512 size = size1;
513 while (size > 0) {
514 l = f->buf_size - f->buf_index;
515 if (l == 0) {
516 qemu_fill_buffer(f);
517 l = f->buf_size - f->buf_index;
518 if (l == 0)
519 break;
520 }
521 if (l > size)
522 l = size;
523 memcpy(buf, f->buf + f->buf_index, l);
524 f->buf_index += l;
525 buf += l;
526 size -= l;
527 }
528 return size1 - size;
529}
530
531int qemu_get_byte(QEMUFile *f)
532{
533 if (f->is_write)
534 abort();
535
536 if (f->buf_index >= f->buf_size) {
537 qemu_fill_buffer(f);
538 if (f->buf_index >= f->buf_size)
539 return 0;
540 }
541 return f->buf[f->buf_index++];
542}
543
544int64_t qemu_ftell(QEMUFile *f)
545{
546 return f->buf_offset - f->buf_size + f->buf_index;
547}
548
549int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
550{
551 if (whence == SEEK_SET) {
552 /* nothing to do */
553 } else if (whence == SEEK_CUR) {
554 pos += qemu_ftell(f);
555 } else {
556 /* SEEK_END not supported */
557 return -1;
558 }
559 if (f->put_buffer) {
560 qemu_fflush(f);
561 f->buf_offset = pos;
562 } else {
563 f->buf_offset = pos;
564 f->buf_index = 0;
565 f->buf_size = 0;
566 }
567 return pos;
568}
569
570int qemu_file_rate_limit(QEMUFile *f)
571{
572 if (f->rate_limit)
573 return f->rate_limit(f->opaque);
574
575 return 0;
576}
577
578size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
579{
580 if (f->set_rate_limit)
581 return f->set_rate_limit(f->opaque, new_rate);
582
583 return 0;
584}
585
586void qemu_put_be16(QEMUFile *f, unsigned int v)
587{
588 qemu_put_byte(f, v >> 8);
589 qemu_put_byte(f, v);
590}
591
592void qemu_put_be32(QEMUFile *f, unsigned int v)
593{
594 qemu_put_byte(f, v >> 24);
595 qemu_put_byte(f, v >> 16);
596 qemu_put_byte(f, v >> 8);
597 qemu_put_byte(f, v);
598}
599
600void qemu_put_be64(QEMUFile *f, uint64_t v)
601{
602 qemu_put_be32(f, v >> 32);
603 qemu_put_be32(f, v);
604}
605
606unsigned int qemu_get_be16(QEMUFile *f)
607{
608 unsigned int v;
609 v = qemu_get_byte(f) << 8;
610 v |= qemu_get_byte(f);
611 return v;
612}
613
614unsigned int qemu_get_be32(QEMUFile *f)
615{
616 unsigned int v;
617 v = qemu_get_byte(f) << 24;
618 v |= qemu_get_byte(f) << 16;
619 v |= qemu_get_byte(f) << 8;
620 v |= qemu_get_byte(f);
621 return v;
622}
623
624uint64_t qemu_get_be64(QEMUFile *f)
625{
626 uint64_t v;
627 v = (uint64_t)qemu_get_be32(f) << 32;
628 v |= qemu_get_be32(f);
629 return v;
630}
631
632void qemu_put_struct(QEMUFile* f, const QField* fields, const void* s)
633{
634 const QField* qf = fields;
635
Ot ten Thije7fd67eb2010-07-29 10:26:01 +0100636 /* Iterate over struct fields */
637 while (qf->type != Q_FIELD_END) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700638 uint8_t* p = (uint8_t*)s + qf->offset;
639
640 switch (qf->type) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700641 case Q_FIELD_BYTE:
642 qemu_put_byte(f, p[0]);
643 break;
644 case Q_FIELD_INT16:
645 qemu_put_be16(f, ((uint16_t*)p)[0]);
646 break;
647 case Q_FIELD_INT32:
648 qemu_put_be32(f, ((uint32_t*)p)[0]);
649 break;
650 case Q_FIELD_INT64:
651 qemu_put_be64(f, ((uint64_t*)p)[0]);
652 break;
653 case Q_FIELD_BUFFER:
Ot ten Thije7fd67eb2010-07-29 10:26:01 +0100654 if (qf[1].type != Q_FIELD_BUFFER_SIZE ||
655 qf[2].type != Q_FIELD_BUFFER_SIZE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700656 {
657 fprintf(stderr, "%s: invalid QFIELD_BUFFER item passed as argument. aborting\n",
658 __FUNCTION__ );
659 exit(1);
660 }
661 else
662 {
Ot ten Thije7fd67eb2010-07-29 10:26:01 +0100663 uint32_t size = ((uint32_t)qf[1].offset << 16) | (uint32_t)qf[2].offset;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700664
665 qemu_put_buffer(f, p, size);
666 qf += 2;
667 }
668 break;
669 default:
670 fprintf(stderr, "%s: invalid fields list passed as argument. aborting\n", __FUNCTION__);
671 exit(1);
672 }
673 qf++;
674 }
675}
676
677int qemu_get_struct(QEMUFile* f, const QField* fields, void* s)
678{
679 const QField* qf = fields;
680
Ot ten Thije7fd67eb2010-07-29 10:26:01 +0100681 /* Iterate over struct fields */
682 while (qf->type != Q_FIELD_END) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700683 uint8_t* p = (uint8_t*)s + qf->offset;
684
685 switch (qf->type) {
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700686 case Q_FIELD_BYTE:
687 p[0] = qemu_get_byte(f);
688 break;
689 case Q_FIELD_INT16:
690 ((uint16_t*)p)[0] = qemu_get_be16(f);
691 break;
692 case Q_FIELD_INT32:
693 ((uint32_t*)p)[0] = qemu_get_be32(f);
694 break;
695 case Q_FIELD_INT64:
696 ((uint64_t*)p)[0] = qemu_get_be64(f);
697 break;
698 case Q_FIELD_BUFFER:
Ot ten Thije7fd67eb2010-07-29 10:26:01 +0100699 if (qf[1].type != Q_FIELD_BUFFER_SIZE ||
700 qf[2].type != Q_FIELD_BUFFER_SIZE)
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700701 {
702 fprintf(stderr, "%s: invalid QFIELD_BUFFER item passed as argument.\n",
703 __FUNCTION__ );
704 return -1;
705 }
706 else
707 {
Ot ten Thije7fd67eb2010-07-29 10:26:01 +0100708 uint32_t size = ((uint32_t)qf[1].offset << 16) | (uint32_t)qf[2].offset;
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700709 int ret = qemu_get_buffer(f, p, size);
710
711 if (ret != size) {
712 fprintf(stderr, "%s: not enough bytes to load structure\n", __FUNCTION__);
713 return -1;
714 }
715 qf += 2;
716 }
717 break;
718 default:
719 fprintf(stderr, "%s: invalid fields list passed as argument. aborting\n", __FUNCTION__);
720 exit(1);
721 }
722 qf++;
723 }
724 return 0;
725}
726
Ot ten Thije871da2a2010-09-20 10:29:22 +0100727/* write a float to file */
728void qemu_put_float(QEMUFile *f, float v)
729{
730 uint8_t *bytes = (uint8_t*) &v;
731 qemu_put_buffer(f, bytes, sizeof(float));
732}
733
734/* read a float from file */
735float qemu_get_float(QEMUFile *f)
736{
737 uint8_t bytes[sizeof(float)];
738 qemu_get_buffer(f, bytes, sizeof(float));
739
740 return *((float*) bytes);
741}
742
David 'Digit' Turner5d8f37a2009-09-14 14:32:27 -0700743typedef struct SaveStateEntry {
744 char idstr[256];
745 int instance_id;
746 int version_id;
747 int section_id;
748 SaveLiveStateHandler *save_live_state;
749 SaveStateHandler *save_state;
750 LoadStateHandler *load_state;
751 void *opaque;
752 struct SaveStateEntry *next;
753} SaveStateEntry;
754
755static SaveStateEntry *first_se;
756
757/* TODO: Individual devices generally have very little idea about the rest
758 of the system, so instance_id should be removed/replaced.
759 Meanwhile pass -1 as instance_id if you do not already have a clearly
760 distinguishing id for all instances of your device class. */
761int register_savevm_live(const char *idstr,
762 int instance_id,
763 int version_id,
764 SaveLiveStateHandler *save_live_state,
765 SaveStateHandler *save_state,
766 LoadStateHandler *load_state,
767 void *opaque)
768{
769 SaveStateEntry *se, **pse;
770 static int global_section_id;
771
772 se = qemu_malloc(sizeof(SaveStateEntry));
773 pstrcpy(se->idstr, sizeof(se->idstr), idstr);
774 se->instance_id = (instance_id == -1) ? 0 : instance_id;
775 se->version_id = version_id;
776 se->section_id = global_section_id++;
777 se->save_live_state = save_live_state;
778 se->save_state = save_state;
779 se->load_state = load_state;
780 se->opaque = opaque;
781 se->next = NULL;
782
783 /* add at the end of list */
784 pse = &first_se;
785 while (*pse != NULL) {
786 if (instance_id == -1
787 && strcmp(se->idstr, (*pse)->idstr) == 0
788 && se->instance_id <= (*pse)->instance_id)
789 se->instance_id = (*pse)->instance_id + 1;
790 pse = &(*pse)->next;
791 }
792 *pse = se;
793 return 0;
794}
795
796int register_savevm(const char *idstr,
797 int instance_id,
798 int version_id,
799 SaveStateHandler *save_state,
800 LoadStateHandler *load_state,
801 void *opaque)
802{
803 return register_savevm_live(idstr, instance_id, version_id,
804 NULL, save_state, load_state, opaque);
805}
806
807void unregister_savevm(const char *idstr, void *opaque)
808{
809 SaveStateEntry **pse;
810
811 pse = &first_se;
812 while (*pse != NULL) {
813 if (strcmp((*pse)->idstr, idstr) == 0 && (*pse)->opaque == opaque) {
814 SaveStateEntry *next = (*pse)->next;
815 qemu_free(*pse);
816 *pse = next;
817 continue;
818 }
819 pse = &(*pse)->next;
820 }
821}
822
823#define QEMU_VM_FILE_MAGIC 0x5145564d
824#define QEMU_VM_FILE_VERSION_COMPAT 0x00000002
825#define QEMU_VM_FILE_VERSION 0x00000003
826
827#define QEMU_VM_EOF 0x00
828#define QEMU_VM_SECTION_START 0x01
829#define QEMU_VM_SECTION_PART 0x02
830#define QEMU_VM_SECTION_END 0x03
831#define QEMU_VM_SECTION_FULL 0x04
832
833int qemu_savevm_state_begin(QEMUFile *f)
834{
835 SaveStateEntry *se;
836
837 qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
838 qemu_put_be32(f, QEMU_VM_FILE_VERSION);
839
840 for (se = first_se; se != NULL; se = se->next) {
841 int len;
842
843 if (se->save_live_state == NULL)
844 continue;
845
846 /* Section type */
847 qemu_put_byte(f, QEMU_VM_SECTION_START);
848 qemu_put_be32(f, se->section_id);
849
850 /* ID string */
851 len = strlen(se->idstr);
852 qemu_put_byte(f, len);
853 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
854
855 qemu_put_be32(f, se->instance_id);
856 qemu_put_be32(f, se->version_id);
857
858 se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
859 }
860
861 if (qemu_file_has_error(f))
862 return -EIO;
863
864 return 0;
865}
866
867int qemu_savevm_state_iterate(QEMUFile *f)
868{
869 SaveStateEntry *se;
870 int ret = 1;
871
872 for (se = first_se; se != NULL; se = se->next) {
873 if (se->save_live_state == NULL)
874 continue;
875
876 /* Section type */
877 qemu_put_byte(f, QEMU_VM_SECTION_PART);
878 qemu_put_be32(f, se->section_id);
879
880 ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
881 }
882
883 if (ret)
884 return 1;
885
886 if (qemu_file_has_error(f))
887 return -EIO;
888
889 return 0;
890}
891
892int qemu_savevm_state_complete(QEMUFile *f)
893{
894 SaveStateEntry *se;
895
896 for (se = first_se; se != NULL; se = se->next) {
897 if (se->save_live_state == NULL)
898 continue;
899
900 /* Section type */
901 qemu_put_byte(f, QEMU_VM_SECTION_END);
902 qemu_put_be32(f, se->section_id);
903
904 se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
905 }
906
907 for(se = first_se; se != NULL; se = se->next) {
908 int len;
909
910 if (se->save_state == NULL)
911 continue;
912
913 /* Section type */
914 qemu_put_byte(f, QEMU_VM_SECTION_FULL);
915 qemu_put_be32(f, se->section_id);
916
917 /* ID string */
918 len = strlen(se->idstr);
919 qemu_put_byte(f, len);
920 qemu_put_buffer(f, (uint8_t *)se->idstr, len);
921
922 qemu_put_be32(f, se->instance_id);
923 qemu_put_be32(f, se->version_id);
924
925 se->save_state(f, se->opaque);
926 }
927
928 qemu_put_byte(f, QEMU_VM_EOF);
929
930 if (qemu_file_has_error(f))
931 return -EIO;
932
933 return 0;
934}
935
936int qemu_savevm_state(QEMUFile *f)
937{
938 int saved_vm_running;
939 int ret;
940
941 saved_vm_running = vm_running;
942 vm_stop(0);
943
944 bdrv_flush_all();
945
946 ret = qemu_savevm_state_begin(f);
947 if (ret < 0)
948 goto out;
949
950 do {
951 ret = qemu_savevm_state_iterate(f);
952 if (ret < 0)
953 goto out;
954 } while (ret == 0);
955
956 ret = qemu_savevm_state_complete(f);
957
958out:
959 if (qemu_file_has_error(f))
960 ret = -EIO;
961
962 if (!ret && saved_vm_running)
963 vm_start();
964
965 return ret;
966}
967
968static SaveStateEntry *find_se(const char *idstr, int instance_id)
969{
970 SaveStateEntry *se;
971
972 for(se = first_se; se != NULL; se = se->next) {
973 if (!strcmp(se->idstr, idstr) &&
974 instance_id == se->instance_id)
975 return se;
976 }
977 return NULL;
978}
979
980typedef struct LoadStateEntry {
981 SaveStateEntry *se;
982 int section_id;
983 int version_id;
984 struct LoadStateEntry *next;
985} LoadStateEntry;
986
987static int qemu_loadvm_state_v2(QEMUFile *f)
988{
989 SaveStateEntry *se;
990 int len, ret, instance_id, record_len, version_id;
991 int64_t total_len, end_pos, cur_pos;
992 char idstr[256];
993
994 total_len = qemu_get_be64(f);
995 end_pos = total_len + qemu_ftell(f);
996 for(;;) {
997 if (qemu_ftell(f) >= end_pos)
998 break;
999 len = qemu_get_byte(f);
1000 qemu_get_buffer(f, (uint8_t *)idstr, len);
1001 idstr[len] = '\0';
1002 instance_id = qemu_get_be32(f);
1003 version_id = qemu_get_be32(f);
1004 record_len = qemu_get_be32(f);
1005 cur_pos = qemu_ftell(f);
1006 se = find_se(idstr, instance_id);
1007 if (!se) {
1008 fprintf(stderr, "qemu: warning: instance 0x%x of device '%s' not present in current VM\n",
1009 instance_id, idstr);
1010 } else {
1011 ret = se->load_state(f, se->opaque, version_id);
1012 if (ret < 0) {
1013 fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1014 instance_id, idstr);
1015 return ret;
1016 }
1017 }
1018 /* always seek to exact end of record */
1019 qemu_fseek(f, cur_pos + record_len, SEEK_SET);
1020 }
1021
1022 if (qemu_file_has_error(f))
1023 return -EIO;
1024
1025 return 0;
1026}
1027
1028int qemu_loadvm_state(QEMUFile *f)
1029{
1030 LoadStateEntry *first_le = NULL;
1031 uint8_t section_type;
1032 unsigned int v;
1033 int ret;
1034
1035 v = qemu_get_be32(f);
1036 if (v != QEMU_VM_FILE_MAGIC)
1037 return -EINVAL;
1038
1039 v = qemu_get_be32(f);
1040 if (v == QEMU_VM_FILE_VERSION_COMPAT)
1041 return qemu_loadvm_state_v2(f);
1042 if (v != QEMU_VM_FILE_VERSION)
1043 return -ENOTSUP;
1044
1045 while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1046 uint32_t instance_id, version_id, section_id;
1047 LoadStateEntry *le;
1048 SaveStateEntry *se;
1049 char idstr[257];
1050 int len;
1051
1052 switch (section_type) {
1053 case QEMU_VM_SECTION_START:
1054 case QEMU_VM_SECTION_FULL:
1055 /* Read section start */
1056 section_id = qemu_get_be32(f);
1057 len = qemu_get_byte(f);
1058 qemu_get_buffer(f, (uint8_t *)idstr, len);
1059 idstr[len] = 0;
1060 instance_id = qemu_get_be32(f);
1061 version_id = qemu_get_be32(f);
1062
1063 /* Find savevm section */
1064 se = find_se(idstr, instance_id);
1065 if (se == NULL) {
1066 fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1067 ret = -EINVAL;
1068 goto out;
1069 }
1070
1071 /* Validate version */
1072 if (version_id > se->version_id) {
1073 fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1074 version_id, idstr, se->version_id);
1075 ret = -EINVAL;
1076 goto out;
1077 }
1078
1079 /* Add entry */
1080 le = qemu_mallocz(sizeof(*le));
1081
1082 le->se = se;
1083 le->section_id = section_id;
1084 le->version_id = version_id;
1085 le->next = first_le;
1086 first_le = le;
1087
1088 le->se->load_state(f, le->se->opaque, le->version_id);
1089 break;
1090 case QEMU_VM_SECTION_PART:
1091 case QEMU_VM_SECTION_END:
1092 section_id = qemu_get_be32(f);
1093
1094 for (le = first_le; le && le->section_id != section_id; le = le->next);
1095 if (le == NULL) {
1096 fprintf(stderr, "Unknown savevm section %d\n", section_id);
1097 ret = -EINVAL;
1098 goto out;
1099 }
1100
1101 le->se->load_state(f, le->se->opaque, le->version_id);
1102 break;
1103 default:
1104 fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1105 ret = -EINVAL;
1106 goto out;
1107 }
1108 }
1109
1110 ret = 0;
1111
1112out:
1113 while (first_le) {
1114 LoadStateEntry *le = first_le;
1115 first_le = first_le->next;
1116 qemu_free(le);
1117 }
1118
1119 if (qemu_file_has_error(f))
1120 ret = -EIO;
1121
1122 return ret;
1123}
1124
1125/* device can contain snapshots */
1126static int bdrv_can_snapshot(BlockDriverState *bs)
1127{
1128 return (bs &&
1129 !bdrv_is_removable(bs) &&
1130 !bdrv_is_read_only(bs));
1131}
1132
1133/* device must be snapshots in order to have a reliable snapshot */
1134static int bdrv_has_snapshot(BlockDriverState *bs)
1135{
1136 return (bs &&
1137 !bdrv_is_removable(bs) &&
1138 !bdrv_is_read_only(bs));
1139}
1140
1141static BlockDriverState *get_bs_snapshots(void)
1142{
1143 BlockDriverState *bs;
1144 int i;
1145
1146 if (bs_snapshots)
1147 return bs_snapshots;
1148 for(i = 0; i <= nb_drives; i++) {
1149 bs = drives_table[i].bdrv;
1150 if (bdrv_can_snapshot(bs))
1151 goto ok;
1152 }
1153 return NULL;
1154 ok:
1155 bs_snapshots = bs;
1156 return bs;
1157}
1158
1159static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1160 const char *name)
1161{
1162 QEMUSnapshotInfo *sn_tab, *sn;
1163 int nb_sns, i, ret;
1164
1165 ret = -ENOENT;
1166 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1167 if (nb_sns < 0)
1168 return ret;
1169 for(i = 0; i < nb_sns; i++) {
1170 sn = &sn_tab[i];
1171 if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1172 *sn_info = *sn;
1173 ret = 0;
1174 break;
1175 }
1176 }
1177 qemu_free(sn_tab);
1178 return ret;
1179}
1180
1181void do_savevm(Monitor *mon, const char *name)
1182{
1183 BlockDriverState *bs, *bs1;
1184 QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1185 int must_delete, ret, i;
1186 BlockDriverInfo bdi1, *bdi = &bdi1;
1187 QEMUFile *f;
1188 int saved_vm_running;
1189 uint32_t vm_state_size;
1190#ifdef _WIN32
1191 struct _timeb tb;
1192#else
1193 struct timeval tv;
1194#endif
1195
1196 bs = get_bs_snapshots();
1197 if (!bs) {
1198 monitor_printf(mon, "No block device can accept snapshots\n");
1199 return;
1200 }
1201
1202 /* ??? Should this occur after vm_stop? */
1203 qemu_aio_flush();
1204
1205 saved_vm_running = vm_running;
1206 vm_stop(0);
1207
1208 must_delete = 0;
1209 if (name) {
1210 ret = bdrv_snapshot_find(bs, old_sn, name);
1211 if (ret >= 0) {
1212 must_delete = 1;
1213 }
1214 }
1215 memset(sn, 0, sizeof(*sn));
1216 if (must_delete) {
1217 pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1218 pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1219 } else {
1220 if (name)
1221 pstrcpy(sn->name, sizeof(sn->name), name);
1222 }
1223
1224 /* fill auxiliary fields */
1225#ifdef _WIN32
1226 _ftime(&tb);
1227 sn->date_sec = tb.time;
1228 sn->date_nsec = tb.millitm * 1000000;
1229#else
1230 gettimeofday(&tv, NULL);
1231 sn->date_sec = tv.tv_sec;
1232 sn->date_nsec = tv.tv_usec * 1000;
1233#endif
1234 sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1235
1236 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1237 monitor_printf(mon, "Device %s does not support VM state snapshots\n",
1238 bdrv_get_device_name(bs));
1239 goto the_end;
1240 }
1241
1242 /* save the VM state */
1243 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 1);
1244 if (!f) {
1245 monitor_printf(mon, "Could not open VM state file\n");
1246 goto the_end;
1247 }
1248 ret = qemu_savevm_state(f);
1249 vm_state_size = qemu_ftell(f);
1250 qemu_fclose(f);
1251 if (ret < 0) {
1252 monitor_printf(mon, "Error %d while writing VM\n", ret);
1253 goto the_end;
1254 }
1255
1256 /* create the snapshots */
1257
1258 for(i = 0; i < nb_drives; i++) {
1259 bs1 = drives_table[i].bdrv;
1260 if (bdrv_has_snapshot(bs1)) {
1261 if (must_delete) {
1262 ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1263 if (ret < 0) {
1264 monitor_printf(mon,
1265 "Error while deleting snapshot on '%s'\n",
1266 bdrv_get_device_name(bs1));
1267 }
1268 }
1269 /* Write VM state size only to the image that contains the state */
1270 sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1271 ret = bdrv_snapshot_create(bs1, sn);
1272 if (ret < 0) {
1273 monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1274 bdrv_get_device_name(bs1));
1275 }
1276 }
1277 }
1278
1279 the_end:
1280 if (saved_vm_running)
1281 vm_start();
1282}
1283
1284void do_loadvm(Monitor *mon, const char *name)
1285{
1286 BlockDriverState *bs, *bs1;
1287 BlockDriverInfo bdi1, *bdi = &bdi1;
1288 QEMUSnapshotInfo sn;
1289 QEMUFile *f;
1290 int i, ret;
1291 int saved_vm_running;
1292
1293 bs = get_bs_snapshots();
1294 if (!bs) {
1295 monitor_printf(mon, "No block device supports snapshots\n");
1296 return;
1297 }
1298
1299 /* Flush all IO requests so they don't interfere with the new state. */
1300 qemu_aio_flush();
1301
1302 saved_vm_running = vm_running;
1303 vm_stop(0);
1304
1305 for(i = 0; i <= nb_drives; i++) {
1306 bs1 = drives_table[i].bdrv;
1307 if (bdrv_has_snapshot(bs1)) {
1308 ret = bdrv_snapshot_goto(bs1, name);
1309 if (ret < 0) {
1310 if (bs != bs1)
1311 monitor_printf(mon, "Warning: ");
1312 switch(ret) {
1313 case -ENOTSUP:
1314 monitor_printf(mon,
1315 "Snapshots not supported on device '%s'\n",
1316 bdrv_get_device_name(bs1));
1317 break;
1318 case -ENOENT:
1319 monitor_printf(mon, "Could not find snapshot '%s' on "
1320 "device '%s'\n",
1321 name, bdrv_get_device_name(bs1));
1322 break;
1323 default:
1324 monitor_printf(mon, "Error %d while activating snapshot on"
1325 " '%s'\n", ret, bdrv_get_device_name(bs1));
1326 break;
1327 }
1328 /* fatal on snapshot block device */
1329 if (bs == bs1)
1330 goto the_end;
1331 }
1332 }
1333 }
1334
1335 if (bdrv_get_info(bs, bdi) < 0 || bdi->vm_state_offset <= 0) {
1336 monitor_printf(mon, "Device %s does not support VM state snapshots\n",
1337 bdrv_get_device_name(bs));
1338 return;
1339 }
1340
1341 /* Don't even try to load empty VM states */
1342 ret = bdrv_snapshot_find(bs, &sn, name);
1343 if ((ret >= 0) && (sn.vm_state_size == 0))
1344 goto the_end;
1345
1346 /* restore the VM state */
1347 f = qemu_fopen_bdrv(bs, bdi->vm_state_offset, 0);
1348 if (!f) {
1349 monitor_printf(mon, "Could not open VM state file\n");
1350 goto the_end;
1351 }
1352 ret = qemu_loadvm_state(f);
1353 qemu_fclose(f);
1354 if (ret < 0) {
1355 monitor_printf(mon, "Error %d while loading VM state\n", ret);
1356 }
1357 the_end:
1358 if (saved_vm_running)
1359 vm_start();
1360}
1361
1362void do_delvm(Monitor *mon, const char *name)
1363{
1364 BlockDriverState *bs, *bs1;
1365 int i, ret;
1366
1367 bs = get_bs_snapshots();
1368 if (!bs) {
1369 monitor_printf(mon, "No block device supports snapshots\n");
1370 return;
1371 }
1372
1373 for(i = 0; i <= nb_drives; i++) {
1374 bs1 = drives_table[i].bdrv;
1375 if (bdrv_has_snapshot(bs1)) {
1376 ret = bdrv_snapshot_delete(bs1, name);
1377 if (ret < 0) {
1378 if (ret == -ENOTSUP)
1379 monitor_printf(mon,
1380 "Snapshots not supported on device '%s'\n",
1381 bdrv_get_device_name(bs1));
1382 else
1383 monitor_printf(mon, "Error %d while deleting snapshot on "
1384 "'%s'\n", ret, bdrv_get_device_name(bs1));
1385 }
1386 }
1387 }
1388}
1389
1390void do_info_snapshots(Monitor *mon)
1391{
1392 BlockDriverState *bs, *bs1;
1393 QEMUSnapshotInfo *sn_tab, *sn;
1394 int nb_sns, i;
1395 char buf[256];
1396
1397 bs = get_bs_snapshots();
1398 if (!bs) {
1399 monitor_printf(mon, "No available block device supports snapshots\n");
1400 return;
1401 }
1402 monitor_printf(mon, "Snapshot devices:");
1403 for(i = 0; i <= nb_drives; i++) {
1404 bs1 = drives_table[i].bdrv;
1405 if (bdrv_has_snapshot(bs1)) {
1406 if (bs == bs1)
1407 monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1408 }
1409 }
1410 monitor_printf(mon, "\n");
1411
1412 nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1413 if (nb_sns < 0) {
1414 monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1415 return;
1416 }
1417 monitor_printf(mon, "Snapshot list (from %s):\n",
1418 bdrv_get_device_name(bs));
1419 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1420 for(i = 0; i < nb_sns; i++) {
1421 sn = &sn_tab[i];
1422 monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1423 }
1424 qemu_free(sn_tab);
1425}