Statistics
| Branch: | Revision:

root / savevm.c @ b04c4134

History | View | Annotate | Download (42.9 kB)

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 <signal.h>
27
#include <time.h>
28
#include <errno.h>
29
#include <sys/time.h>
30
#include <zlib.h>
31

    
32
/* Needed early for CONFIG_BSD etc. */
33
#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
#include <arpa/inet.h>
46
#include <dirent.h>
47
#include <netdb.h>
48
#include <sys/select.h>
49
#ifdef CONFIG_BSD
50
#include <sys/stat.h>
51
#if defined(__FreeBSD__) || defined(__DragonFly__)
52
#include <libutil.h>
53
#else
54
#include <util.h>
55
#endif
56
#elif defined (__GLIBC__) && defined (__FreeBSD_kernel__)
57
#include <freebsd/stdlib.h>
58
#else
59
#ifdef __linux__
60
#include <pty.h>
61
#include <malloc.h>
62
#include <linux/rtc.h>
63
#endif
64
#endif
65
#endif
66

    
67
#ifdef _WIN32
68
#include <windows.h>
69
#include <malloc.h>
70
#include <sys/timeb.h>
71
#include <mmsystem.h>
72
#define getopt_long_only getopt_long
73
#define memalign(align, size) malloc(size)
74
#endif
75

    
76
#include "qemu-common.h"
77
#include "hw/hw.h"
78
#include "net.h"
79
#include "monitor.h"
80
#include "sysemu.h"
81
#include "qemu-timer.h"
82
#include "qemu-char.h"
83
#include "block.h"
84
#include "audio/audio.h"
85
#include "migration.h"
86
#include "qemu_socket.h"
87
#include "qemu-queue.h"
88

    
89
/* point to the block driver where the snapshots are managed */
90
static BlockDriverState *bs_snapshots;
91

    
92
#define SELF_ANNOUNCE_ROUNDS 5
93

    
94
#ifndef ETH_P_RARP
95
#define ETH_P_RARP 0x0835
96
#endif
97
#define ARP_HTYPE_ETH 0x0001
98
#define ARP_PTYPE_IP 0x0800
99
#define ARP_OP_REQUEST_REV 0x3
100

    
101
static int announce_self_create(uint8_t *buf,
102
                                uint8_t *mac_addr)
103
{
104
    /* Ethernet header. */
105
    memset(buf, 0xff, 6);         /* destination MAC addr */
106
    memcpy(buf + 6, mac_addr, 6); /* source MAC addr */
107
    *(uint16_t *)(buf + 12) = htons(ETH_P_RARP); /* ethertype */
108

    
109
    /* RARP header. */
110
    *(uint16_t *)(buf + 14) = htons(ARP_HTYPE_ETH); /* hardware addr space */
111
    *(uint16_t *)(buf + 16) = htons(ARP_PTYPE_IP); /* protocol addr space */
112
    *(buf + 18) = 6; /* hardware addr length (ethernet) */
113
    *(buf + 19) = 4; /* protocol addr length (IPv4) */
114
    *(uint16_t *)(buf + 20) = htons(ARP_OP_REQUEST_REV); /* opcode */
115
    memcpy(buf + 22, mac_addr, 6); /* source hw addr */
116
    memset(buf + 28, 0x00, 4);     /* source protocol addr */
117
    memcpy(buf + 32, mac_addr, 6); /* target hw addr */
118
    memset(buf + 38, 0x00, 4);     /* target protocol addr */
119

    
120
    /* Padding to get up to 60 bytes (ethernet min packet size, minus FCS). */
121
    memset(buf + 42, 0x00, 18);
122

    
123
    return 60; /* len (FCS will be added by hardware) */
124
}
125

    
126
static void qemu_announce_self_once(void *opaque)
127
{
128
    int i, len;
129
    uint8_t buf[60];
130
    static int count = SELF_ANNOUNCE_ROUNDS;
131
    QEMUTimer *timer = *(QEMUTimer **)opaque;
132

    
133
    for (i = 0; i < MAX_NICS; i++) {
134
        if (!nd_table[i].used)
135
            continue;
136
        len = announce_self_create(buf, nd_table[i].macaddr);
137
        qemu_send_packet_raw(nd_table[i].vc, buf, len);
138
    }
139
    if (--count) {
140
        /* delay 50ms, 150ms, 250ms, ... */
141
        qemu_mod_timer(timer, qemu_get_clock(rt_clock) +
142
                       50 + (SELF_ANNOUNCE_ROUNDS - count - 1) * 100);
143
    } else {
144
            qemu_del_timer(timer);
145
            qemu_free_timer(timer);
146
    }
147
}
148

    
149
void qemu_announce_self(void)
150
{
151
        static QEMUTimer *timer;
152
        timer = qemu_new_timer(rt_clock, qemu_announce_self_once, &timer);
153
        qemu_announce_self_once(&timer);
154
}
155

    
156
/***********************************************************/
157
/* savevm/loadvm support */
158

    
159
#define IO_BUF_SIZE 32768
160

    
161
struct QEMUFile {
162
    QEMUFilePutBufferFunc *put_buffer;
163
    QEMUFileGetBufferFunc *get_buffer;
164
    QEMUFileCloseFunc *close;
165
    QEMUFileRateLimit *rate_limit;
166
    QEMUFileSetRateLimit *set_rate_limit;
167
    void *opaque;
168
    int is_write;
169

    
170
    int64_t buf_offset; /* start of buffer when writing, end of buffer
171
                           when reading */
172
    int buf_index;
173
    int buf_size; /* 0 when writing */
174
    uint8_t buf[IO_BUF_SIZE];
175

    
176
    int has_error;
177
};
178

    
179
typedef struct QEMUFileStdio
180
{
181
    FILE *stdio_file;
182
    QEMUFile *file;
183
} QEMUFileStdio;
184

    
185
typedef struct QEMUFileSocket
186
{
187
    int fd;
188
    QEMUFile *file;
189
} QEMUFileSocket;
190

    
191
static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
192
{
193
    QEMUFileSocket *s = opaque;
194
    ssize_t len;
195

    
196
    do {
197
        len = recv(s->fd, (void *)buf, size, 0);
198
    } while (len == -1 && socket_error() == EINTR);
199

    
200
    if (len == -1)
201
        len = -socket_error();
202

    
203
    return len;
204
}
205

    
206
static int socket_close(void *opaque)
207
{
208
    QEMUFileSocket *s = opaque;
209
    qemu_free(s);
210
    return 0;
211
}
212

    
213
static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
214
{
215
    QEMUFileStdio *s = opaque;
216
    return fwrite(buf, 1, size, s->stdio_file);
217
}
218

    
219
static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
220
{
221
    QEMUFileStdio *s = opaque;
222
    FILE *fp = s->stdio_file;
223
    int bytes;
224

    
225
    do {
226
        clearerr(fp);
227
        bytes = fread(buf, 1, size, fp);
228
    } while ((bytes == 0) && ferror(fp) && (errno == EINTR));
229
    return bytes;
230
}
231

    
232
static int stdio_pclose(void *opaque)
233
{
234
    QEMUFileStdio *s = opaque;
235
    pclose(s->stdio_file);
236
    qemu_free(s);
237
    return 0;
238
}
239

    
240
static int stdio_fclose(void *opaque)
241
{
242
    QEMUFileStdio *s = opaque;
243
    fclose(s->stdio_file);
244
    qemu_free(s);
245
    return 0;
246
}
247

    
248
QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
249
{
250
    QEMUFileStdio *s;
251

    
252
    if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
253
        fprintf(stderr, "qemu_popen: Argument validity check failed\n");
254
        return NULL;
255
    }
256

    
257
    s = qemu_mallocz(sizeof(QEMUFileStdio));
258

    
259
    s->stdio_file = stdio_file;
260

    
261
    if(mode[0] == 'r') {
262
        s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL, NULL);
263
    } else {
264
        s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL, NULL);
265
    }
266
    return s->file;
267
}
268

    
269
QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
270
{
271
    FILE *popen_file;
272

    
273
    popen_file = popen(command, mode);
274
    if(popen_file == NULL) {
275
        return NULL;
276
    }
277

    
278
    return qemu_popen(popen_file, mode);
279
}
280

    
281
int qemu_stdio_fd(QEMUFile *f)
282
{
283
    QEMUFileStdio *p;
284
    int fd;
285

    
286
    p = (QEMUFileStdio *)f->opaque;
287
    fd = fileno(p->stdio_file);
288

    
289
    return fd;
290
}
291

    
292
QEMUFile *qemu_fdopen(int fd, const char *mode)
293
{
294
    QEMUFileStdio *s;
295

    
296
    if (mode == NULL ||
297
        (mode[0] != 'r' && mode[0] != 'w') ||
298
        mode[1] != 'b' || mode[2] != 0) {
299
        fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
300
        return NULL;
301
    }
302

    
303
    s = qemu_mallocz(sizeof(QEMUFileStdio));
304
    s->stdio_file = fdopen(fd, mode);
305
    if (!s->stdio_file)
306
        goto fail;
307

    
308
    if(mode[0] == 'r') {
309
        s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose, NULL, NULL);
310
    } else {
311
        s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_fclose, NULL, NULL);
312
    }
313
    return s->file;
314

    
315
fail:
316
    qemu_free(s);
317
    return NULL;
318
}
319

    
320
QEMUFile *qemu_fopen_socket(int fd)
321
{
322
    QEMUFileSocket *s = qemu_mallocz(sizeof(QEMUFileSocket));
323

    
324
    s->fd = fd;
325
    s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close, NULL, NULL);
326
    return s->file;
327
}
328

    
329
static int file_put_buffer(void *opaque, const uint8_t *buf,
330
                            int64_t pos, int size)
331
{
332
    QEMUFileStdio *s = opaque;
333
    fseek(s->stdio_file, pos, SEEK_SET);
334
    fwrite(buf, 1, size, s->stdio_file);
335
    return size;
336
}
337

    
338
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
339
{
340
    QEMUFileStdio *s = opaque;
341
    fseek(s->stdio_file, pos, SEEK_SET);
342
    return fread(buf, 1, size, s->stdio_file);
343
}
344

    
345
QEMUFile *qemu_fopen(const char *filename, const char *mode)
346
{
347
    QEMUFileStdio *s;
348

    
349
    if (mode == NULL ||
350
        (mode[0] != 'r' && mode[0] != 'w') ||
351
        mode[1] != 'b' || mode[2] != 0) {
352
        fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
353
        return NULL;
354
    }
355

    
356
    s = qemu_mallocz(sizeof(QEMUFileStdio));
357

    
358
    s->stdio_file = fopen(filename, mode);
359
    if (!s->stdio_file)
360
        goto fail;
361

    
362
    if(mode[0] == 'w') {
363
        s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
364
    } else {
365
        s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
366
    }
367
    return s->file;
368
fail:
369
    qemu_free(s);
370
    return NULL;
371
}
372

    
373
static int block_put_buffer(void *opaque, const uint8_t *buf,
374
                           int64_t pos, int size)
375
{
376
    bdrv_save_vmstate(opaque, buf, pos, size);
377
    return size;
378
}
379

    
380
static int block_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
381
{
382
    return bdrv_load_vmstate(opaque, buf, pos, size);
383
}
384

    
385
static int bdrv_fclose(void *opaque)
386
{
387
    return 0;
388
}
389

    
390
static QEMUFile *qemu_fopen_bdrv(BlockDriverState *bs, int is_writable)
391
{
392
    if (is_writable)
393
        return qemu_fopen_ops(bs, block_put_buffer, NULL, bdrv_fclose, NULL, NULL);
394
    return qemu_fopen_ops(bs, NULL, block_get_buffer, bdrv_fclose, NULL, NULL);
395
}
396

    
397
QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
398
                         QEMUFileGetBufferFunc *get_buffer,
399
                         QEMUFileCloseFunc *close,
400
                         QEMUFileRateLimit *rate_limit,
401
                         QEMUFileSetRateLimit *set_rate_limit)
402
{
403
    QEMUFile *f;
404

    
405
    f = qemu_mallocz(sizeof(QEMUFile));
406

    
407
    f->opaque = opaque;
408
    f->put_buffer = put_buffer;
409
    f->get_buffer = get_buffer;
410
    f->close = close;
411
    f->rate_limit = rate_limit;
412
    f->set_rate_limit = set_rate_limit;
413
    f->is_write = 0;
414

    
415
    return f;
416
}
417

    
418
int qemu_file_has_error(QEMUFile *f)
419
{
420
    return f->has_error;
421
}
422

    
423
void qemu_file_set_error(QEMUFile *f)
424
{
425
    f->has_error = 1;
426
}
427

    
428
void qemu_fflush(QEMUFile *f)
429
{
430
    if (!f->put_buffer)
431
        return;
432

    
433
    if (f->is_write && f->buf_index > 0) {
434
        int len;
435

    
436
        len = f->put_buffer(f->opaque, f->buf, f->buf_offset, f->buf_index);
437
        if (len > 0)
438
            f->buf_offset += f->buf_index;
439
        else
440
            f->has_error = 1;
441
        f->buf_index = 0;
442
    }
443
}
444

    
445
static void qemu_fill_buffer(QEMUFile *f)
446
{
447
    int len;
448

    
449
    if (!f->get_buffer)
450
        return;
451

    
452
    if (f->is_write)
453
        abort();
454

    
455
    len = f->get_buffer(f->opaque, f->buf, f->buf_offset, IO_BUF_SIZE);
456
    if (len > 0) {
457
        f->buf_index = 0;
458
        f->buf_size = len;
459
        f->buf_offset += len;
460
    } else if (len != -EAGAIN)
461
        f->has_error = 1;
462
}
463

    
464
int qemu_fclose(QEMUFile *f)
465
{
466
    int ret = 0;
467
    qemu_fflush(f);
468
    if (f->close)
469
        ret = f->close(f->opaque);
470
    qemu_free(f);
471
    return ret;
472
}
473

    
474
void qemu_file_put_notify(QEMUFile *f)
475
{
476
    f->put_buffer(f->opaque, NULL, 0, 0);
477
}
478

    
479
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size)
480
{
481
    int l;
482

    
483
    if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
484
        fprintf(stderr,
485
                "Attempted to write to buffer while read buffer is not empty\n");
486
        abort();
487
    }
488

    
489
    while (!f->has_error && size > 0) {
490
        l = IO_BUF_SIZE - f->buf_index;
491
        if (l > size)
492
            l = size;
493
        memcpy(f->buf + f->buf_index, buf, l);
494
        f->is_write = 1;
495
        f->buf_index += l;
496
        buf += l;
497
        size -= l;
498
        if (f->buf_index >= IO_BUF_SIZE)
499
            qemu_fflush(f);
500
    }
501
}
502

    
503
void qemu_put_byte(QEMUFile *f, int v)
504
{
505
    if (!f->has_error && f->is_write == 0 && f->buf_index > 0) {
506
        fprintf(stderr,
507
                "Attempted to write to buffer while read buffer is not empty\n");
508
        abort();
509
    }
510

    
511
    f->buf[f->buf_index++] = v;
512
    f->is_write = 1;
513
    if (f->buf_index >= IO_BUF_SIZE)
514
        qemu_fflush(f);
515
}
516

    
517
int qemu_get_buffer(QEMUFile *f, uint8_t *buf, int size1)
518
{
519
    int size, l;
520

    
521
    if (f->is_write)
522
        abort();
523

    
524
    size = size1;
525
    while (size > 0) {
526
        l = f->buf_size - f->buf_index;
527
        if (l == 0) {
528
            qemu_fill_buffer(f);
529
            l = f->buf_size - f->buf_index;
530
            if (l == 0)
531
                break;
532
        }
533
        if (l > size)
534
            l = size;
535
        memcpy(buf, f->buf + f->buf_index, l);
536
        f->buf_index += l;
537
        buf += l;
538
        size -= l;
539
    }
540
    return size1 - size;
541
}
542

    
543
int qemu_get_byte(QEMUFile *f)
544
{
545
    if (f->is_write)
546
        abort();
547

    
548
    if (f->buf_index >= f->buf_size) {
549
        qemu_fill_buffer(f);
550
        if (f->buf_index >= f->buf_size)
551
            return 0;
552
    }
553
    return f->buf[f->buf_index++];
554
}
555

    
556
int64_t qemu_ftell(QEMUFile *f)
557
{
558
    return f->buf_offset - f->buf_size + f->buf_index;
559
}
560

    
561
int64_t qemu_fseek(QEMUFile *f, int64_t pos, int whence)
562
{
563
    if (whence == SEEK_SET) {
564
        /* nothing to do */
565
    } else if (whence == SEEK_CUR) {
566
        pos += qemu_ftell(f);
567
    } else {
568
        /* SEEK_END not supported */
569
        return -1;
570
    }
571
    if (f->put_buffer) {
572
        qemu_fflush(f);
573
        f->buf_offset = pos;
574
    } else {
575
        f->buf_offset = pos;
576
        f->buf_index = 0;
577
        f->buf_size = 0;
578
    }
579
    return pos;
580
}
581

    
582
int qemu_file_rate_limit(QEMUFile *f)
583
{
584
    if (f->rate_limit)
585
        return f->rate_limit(f->opaque);
586

    
587
    return 0;
588
}
589

    
590
size_t qemu_file_set_rate_limit(QEMUFile *f, size_t new_rate)
591
{
592
    /* any failed or completed migration keeps its state to allow probing of
593
     * migration data, but has no associated file anymore */
594
    if (f && f->set_rate_limit)
595
        return f->set_rate_limit(f->opaque, new_rate);
596

    
597
    return 0;
598
}
599

    
600
void qemu_put_be16(QEMUFile *f, unsigned int v)
601
{
602
    qemu_put_byte(f, v >> 8);
603
    qemu_put_byte(f, v);
604
}
605

    
606
void qemu_put_be32(QEMUFile *f, unsigned int v)
607
{
608
    qemu_put_byte(f, v >> 24);
609
    qemu_put_byte(f, v >> 16);
610
    qemu_put_byte(f, v >> 8);
611
    qemu_put_byte(f, v);
612
}
613

    
614
void qemu_put_be64(QEMUFile *f, uint64_t v)
615
{
616
    qemu_put_be32(f, v >> 32);
617
    qemu_put_be32(f, v);
618
}
619

    
620
unsigned int qemu_get_be16(QEMUFile *f)
621
{
622
    unsigned int v;
623
    v = qemu_get_byte(f) << 8;
624
    v |= qemu_get_byte(f);
625
    return v;
626
}
627

    
628
unsigned int qemu_get_be32(QEMUFile *f)
629
{
630
    unsigned int v;
631
    v = qemu_get_byte(f) << 24;
632
    v |= qemu_get_byte(f) << 16;
633
    v |= qemu_get_byte(f) << 8;
634
    v |= qemu_get_byte(f);
635
    return v;
636
}
637

    
638
uint64_t qemu_get_be64(QEMUFile *f)
639
{
640
    uint64_t v;
641
    v = (uint64_t)qemu_get_be32(f) << 32;
642
    v |= qemu_get_be32(f);
643
    return v;
644
}
645

    
646
/* 8 bit int */
647

    
648
static int get_int8(QEMUFile *f, void *pv, size_t size)
649
{
650
    int8_t *v = pv;
651
    qemu_get_s8s(f, v);
652
    return 0;
653
}
654

    
655
static void put_int8(QEMUFile *f, void *pv, size_t size)
656
{
657
    int8_t *v = pv;
658
    qemu_put_s8s(f, v);
659
}
660

    
661
const VMStateInfo vmstate_info_int8 = {
662
    .name = "int8",
663
    .get  = get_int8,
664
    .put  = put_int8,
665
};
666

    
667
/* 16 bit int */
668

    
669
static int get_int16(QEMUFile *f, void *pv, size_t size)
670
{
671
    int16_t *v = pv;
672
    qemu_get_sbe16s(f, v);
673
    return 0;
674
}
675

    
676
static void put_int16(QEMUFile *f, void *pv, size_t size)
677
{
678
    int16_t *v = pv;
679
    qemu_put_sbe16s(f, v);
680
}
681

    
682
const VMStateInfo vmstate_info_int16 = {
683
    .name = "int16",
684
    .get  = get_int16,
685
    .put  = put_int16,
686
};
687

    
688
/* 32 bit int */
689

    
690
static int get_int32(QEMUFile *f, void *pv, size_t size)
691
{
692
    int32_t *v = pv;
693
    qemu_get_sbe32s(f, v);
694
    return 0;
695
}
696

    
697
static void put_int32(QEMUFile *f, void *pv, size_t size)
698
{
699
    int32_t *v = pv;
700
    qemu_put_sbe32s(f, v);
701
}
702

    
703
const VMStateInfo vmstate_info_int32 = {
704
    .name = "int32",
705
    .get  = get_int32,
706
    .put  = put_int32,
707
};
708

    
709
/* 32 bit int. See that the received value is the same than the one
710
   in the field */
711

    
712
static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
713
{
714
    int32_t *v = pv;
715
    int32_t v2;
716
    qemu_get_sbe32s(f, &v2);
717

    
718
    if (*v == v2)
719
        return 0;
720
    return -EINVAL;
721
}
722

    
723
const VMStateInfo vmstate_info_int32_equal = {
724
    .name = "int32 equal",
725
    .get  = get_int32_equal,
726
    .put  = put_int32,
727
};
728

    
729
/* 32 bit int. See that the received value is the less or the same
730
   than the one in the field */
731

    
732
static int get_int32_le(QEMUFile *f, void *pv, size_t size)
733
{
734
    int32_t *old = pv;
735
    int32_t new;
736
    qemu_get_sbe32s(f, &new);
737

    
738
    if (*old <= new)
739
        return 0;
740
    return -EINVAL;
741
}
742

    
743
const VMStateInfo vmstate_info_int32_le = {
744
    .name = "int32 equal",
745
    .get  = get_int32_le,
746
    .put  = put_int32,
747
};
748

    
749
/* 64 bit int */
750

    
751
static int get_int64(QEMUFile *f, void *pv, size_t size)
752
{
753
    int64_t *v = pv;
754
    qemu_get_sbe64s(f, v);
755
    return 0;
756
}
757

    
758
static void put_int64(QEMUFile *f, void *pv, size_t size)
759
{
760
    int64_t *v = pv;
761
    qemu_put_sbe64s(f, v);
762
}
763

    
764
const VMStateInfo vmstate_info_int64 = {
765
    .name = "int64",
766
    .get  = get_int64,
767
    .put  = put_int64,
768
};
769

    
770
/* 8 bit unsigned int */
771

    
772
static int get_uint8(QEMUFile *f, void *pv, size_t size)
773
{
774
    uint8_t *v = pv;
775
    qemu_get_8s(f, v);
776
    return 0;
777
}
778

    
779
static void put_uint8(QEMUFile *f, void *pv, size_t size)
780
{
781
    uint8_t *v = pv;
782
    qemu_put_8s(f, v);
783
}
784

    
785
const VMStateInfo vmstate_info_uint8 = {
786
    .name = "uint8",
787
    .get  = get_uint8,
788
    .put  = put_uint8,
789
};
790

    
791
/* 16 bit unsigned int */
792

    
793
static int get_uint16(QEMUFile *f, void *pv, size_t size)
794
{
795
    uint16_t *v = pv;
796
    qemu_get_be16s(f, v);
797
    return 0;
798
}
799

    
800
static void put_uint16(QEMUFile *f, void *pv, size_t size)
801
{
802
    uint16_t *v = pv;
803
    qemu_put_be16s(f, v);
804
}
805

    
806
const VMStateInfo vmstate_info_uint16 = {
807
    .name = "uint16",
808
    .get  = get_uint16,
809
    .put  = put_uint16,
810
};
811

    
812
/* 32 bit unsigned int */
813

    
814
static int get_uint32(QEMUFile *f, void *pv, size_t size)
815
{
816
    uint32_t *v = pv;
817
    qemu_get_be32s(f, v);
818
    return 0;
819
}
820

    
821
static void put_uint32(QEMUFile *f, void *pv, size_t size)
822
{
823
    uint32_t *v = pv;
824
    qemu_put_be32s(f, v);
825
}
826

    
827
const VMStateInfo vmstate_info_uint32 = {
828
    .name = "uint32",
829
    .get  = get_uint32,
830
    .put  = put_uint32,
831
};
832

    
833
/* 64 bit unsigned int */
834

    
835
static int get_uint64(QEMUFile *f, void *pv, size_t size)
836
{
837
    uint64_t *v = pv;
838
    qemu_get_be64s(f, v);
839
    return 0;
840
}
841

    
842
static void put_uint64(QEMUFile *f, void *pv, size_t size)
843
{
844
    uint64_t *v = pv;
845
    qemu_put_be64s(f, v);
846
}
847

    
848
const VMStateInfo vmstate_info_uint64 = {
849
    .name = "uint64",
850
    .get  = get_uint64,
851
    .put  = put_uint64,
852
};
853

    
854
/* 8 bit int. See that the received value is the same than the one
855
   in the field */
856

    
857
static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
858
{
859
    uint8_t *v = pv;
860
    uint8_t v2;
861
    qemu_get_8s(f, &v2);
862

    
863
    if (*v == v2)
864
        return 0;
865
    return -EINVAL;
866
}
867

    
868
const VMStateInfo vmstate_info_uint8_equal = {
869
    .name = "uint8 equal",
870
    .get  = get_uint8_equal,
871
    .put  = put_uint8,
872
};
873

    
874
/* 16 bit unsigned int int. See that the received value is the same than the one
875
   in the field */
876

    
877
static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
878
{
879
    uint16_t *v = pv;
880
    uint16_t v2;
881
    qemu_get_be16s(f, &v2);
882

    
883
    if (*v == v2)
884
        return 0;
885
    return -EINVAL;
886
}
887

    
888
const VMStateInfo vmstate_info_uint16_equal = {
889
    .name = "uint16 equal",
890
    .get  = get_uint16_equal,
891
    .put  = put_uint16,
892
};
893

    
894
/* timers  */
895

    
896
static int get_timer(QEMUFile *f, void *pv, size_t size)
897
{
898
    QEMUTimer *v = pv;
899
    qemu_get_timer(f, v);
900
    return 0;
901
}
902

    
903
static void put_timer(QEMUFile *f, void *pv, size_t size)
904
{
905
    QEMUTimer *v = pv;
906
    qemu_put_timer(f, v);
907
}
908

    
909
const VMStateInfo vmstate_info_timer = {
910
    .name = "timer",
911
    .get  = get_timer,
912
    .put  = put_timer,
913
};
914

    
915
/* uint8_t buffers */
916

    
917
static int get_buffer(QEMUFile *f, void *pv, size_t size)
918
{
919
    uint8_t *v = pv;
920
    qemu_get_buffer(f, v, size);
921
    return 0;
922
}
923

    
924
static void put_buffer(QEMUFile *f, void *pv, size_t size)
925
{
926
    uint8_t *v = pv;
927
    qemu_put_buffer(f, v, size);
928
}
929

    
930
const VMStateInfo vmstate_info_buffer = {
931
    .name = "buffer",
932
    .get  = get_buffer,
933
    .put  = put_buffer,
934
};
935

    
936
/* unused buffers: space that was used for some fields that are
937
   not usefull anymore */
938

    
939
static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
940
{
941
    qemu_fseek(f, size, SEEK_CUR);
942
    return 0;
943
}
944

    
945
static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
946
{
947
    qemu_fseek(f, size, SEEK_CUR);
948
}
949

    
950
const VMStateInfo vmstate_info_unused_buffer = {
951
    .name = "unused_buffer",
952
    .get  = get_unused_buffer,
953
    .put  = put_unused_buffer,
954
};
955

    
956
typedef struct SaveStateEntry {
957
    QTAILQ_ENTRY(SaveStateEntry) entry;
958
    char idstr[256];
959
    int instance_id;
960
    int version_id;
961
    int section_id;
962
    SaveLiveStateHandler *save_live_state;
963
    SaveStateHandler *save_state;
964
    LoadStateHandler *load_state;
965
    const VMStateDescription *vmsd;
966
    void *opaque;
967
} SaveStateEntry;
968

    
969
static QTAILQ_HEAD(savevm_handlers, SaveStateEntry) savevm_handlers =
970
    QTAILQ_HEAD_INITIALIZER(savevm_handlers);
971
static int global_section_id;
972

    
973
static int calculate_new_instance_id(const char *idstr)
974
{
975
    SaveStateEntry *se;
976
    int instance_id = 0;
977

    
978
    QTAILQ_FOREACH(se, &savevm_handlers, entry) {
979
        if (strcmp(idstr, se->idstr) == 0
980
            && instance_id <= se->instance_id) {
981
            instance_id = se->instance_id + 1;
982
        }
983
    }
984
    return instance_id;
985
}
986

    
987
/* TODO: Individual devices generally have very little idea about the rest
988
   of the system, so instance_id should be removed/replaced.
989
   Meanwhile pass -1 as instance_id if you do not already have a clearly
990
   distinguishing id for all instances of your device class. */
991
int register_savevm_live(const char *idstr,
992
                         int instance_id,
993
                         int version_id,
994
                         SaveLiveStateHandler *save_live_state,
995
                         SaveStateHandler *save_state,
996
                         LoadStateHandler *load_state,
997
                         void *opaque)
998
{
999
    SaveStateEntry *se;
1000

    
1001
    se = qemu_malloc(sizeof(SaveStateEntry));
1002
    pstrcpy(se->idstr, sizeof(se->idstr), idstr);
1003
    se->version_id = version_id;
1004
    se->section_id = global_section_id++;
1005
    se->save_live_state = save_live_state;
1006
    se->save_state = save_state;
1007
    se->load_state = load_state;
1008
    se->opaque = opaque;
1009
    se->vmsd = NULL;
1010

    
1011
    if (instance_id == -1) {
1012
        se->instance_id = calculate_new_instance_id(idstr);
1013
    } else {
1014
        se->instance_id = instance_id;
1015
    }
1016
    /* add at the end of list */
1017
    QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1018
    return 0;
1019
}
1020

    
1021
int register_savevm(const char *idstr,
1022
                    int instance_id,
1023
                    int version_id,
1024
                    SaveStateHandler *save_state,
1025
                    LoadStateHandler *load_state,
1026
                    void *opaque)
1027
{
1028
    return register_savevm_live(idstr, instance_id, version_id,
1029
                                NULL, save_state, load_state, opaque);
1030
}
1031

    
1032
void unregister_savevm(const char *idstr, void *opaque)
1033
{
1034
    SaveStateEntry *se, *new_se;
1035

    
1036
    QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1037
        if (strcmp(se->idstr, idstr) == 0 && se->opaque == opaque) {
1038
            QTAILQ_REMOVE(&savevm_handlers, se, entry);
1039
            qemu_free(se);
1040
        }
1041
    }
1042
}
1043

    
1044
int vmstate_register(int instance_id, const VMStateDescription *vmsd,
1045
                     void *opaque)
1046
{
1047
    SaveStateEntry *se;
1048

    
1049
    se = qemu_malloc(sizeof(SaveStateEntry));
1050
    pstrcpy(se->idstr, sizeof(se->idstr), vmsd->name);
1051
    se->version_id = vmsd->version_id;
1052
    se->section_id = global_section_id++;
1053
    se->save_live_state = NULL;
1054
    se->save_state = NULL;
1055
    se->load_state = NULL;
1056
    se->opaque = opaque;
1057
    se->vmsd = vmsd;
1058

    
1059
    if (instance_id == -1) {
1060
        se->instance_id = calculate_new_instance_id(vmsd->name);
1061
    } else {
1062
        se->instance_id = instance_id;
1063
    }
1064
    /* add at the end of list */
1065
    QTAILQ_INSERT_TAIL(&savevm_handlers, se, entry);
1066
    return 0;
1067
}
1068

    
1069
void vmstate_unregister(const VMStateDescription *vmsd, void *opaque)
1070
{
1071
    SaveStateEntry *se, *new_se;
1072

    
1073
    QTAILQ_FOREACH_SAFE(se, &savevm_handlers, entry, new_se) {
1074
        if (se->vmsd == vmsd && se->opaque == opaque) {
1075
            QTAILQ_REMOVE(&savevm_handlers, se, entry);
1076
            qemu_free(se);
1077
        }
1078
    }
1079
}
1080

    
1081
int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
1082
                       void *opaque, int version_id)
1083
{
1084
    VMStateField *field = vmsd->fields;
1085

    
1086
    if (version_id > vmsd->version_id) {
1087
        return -EINVAL;
1088
    }
1089
    if (version_id < vmsd->minimum_version_id_old) {
1090
        return -EINVAL;
1091
    }
1092
    if  (version_id < vmsd->minimum_version_id) {
1093
        return vmsd->load_state_old(f, opaque, version_id);
1094
    }
1095
    if (vmsd->pre_load) {
1096
        int ret = vmsd->pre_load(opaque);
1097
        if (ret)
1098
            return ret;
1099
    }
1100
    while(field->name) {
1101
        if ((field->field_exists &&
1102
             field->field_exists(opaque, version_id)) ||
1103
            (!field->field_exists &&
1104
             field->version_id <= version_id)) {
1105
            void *base_addr = opaque + field->offset;
1106
            int ret, i, n_elems = 1;
1107

    
1108
            if (field->flags & VMS_ARRAY) {
1109
                n_elems = field->num;
1110
            } else if (field->flags & VMS_VARRAY_INT32) {
1111
                n_elems = *(int32_t *)(opaque+field->num_offset);
1112
            } else if (field->flags & VMS_VARRAY_UINT16) {
1113
                n_elems = *(uint16_t *)(opaque+field->num_offset);
1114
            }
1115
            if (field->flags & VMS_POINTER) {
1116
                base_addr = *(void **)base_addr;
1117
            }
1118
            for (i = 0; i < n_elems; i++) {
1119
                void *addr = base_addr + field->size * i;
1120

    
1121
                if (field->flags & VMS_ARRAY_OF_POINTER) {
1122
                    addr = *(void **)addr;
1123
                }
1124
                if (field->flags & VMS_STRUCT) {
1125
                    ret = vmstate_load_state(f, field->vmsd, addr, field->vmsd->version_id);
1126
                } else {
1127
                    ret = field->info->get(f, addr, field->size);
1128

    
1129
                }
1130
                if (ret < 0) {
1131
                    return ret;
1132
                }
1133
            }
1134
        }
1135
        field++;
1136
    }
1137
    if (vmsd->post_load) {
1138
        return vmsd->post_load(opaque, version_id);
1139
    }
1140
    return 0;
1141
}
1142

    
1143
void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
1144
                        void *opaque)
1145
{
1146
    VMStateField *field = vmsd->fields;
1147

    
1148
    if (vmsd->pre_save) {
1149
        vmsd->pre_save(opaque);
1150
    }
1151
    while(field->name) {
1152
        if (!field->field_exists ||
1153
            field->field_exists(opaque, vmsd->version_id)) {
1154
            void *base_addr = opaque + field->offset;
1155
            int i, n_elems = 1;
1156

    
1157
            if (field->flags & VMS_ARRAY) {
1158
                n_elems = field->num;
1159
            } else if (field->flags & VMS_VARRAY_INT32) {
1160
                n_elems = *(int32_t *)(opaque+field->num_offset);
1161
            } else if (field->flags & VMS_VARRAY_UINT16) {
1162
                n_elems = *(uint16_t *)(opaque+field->num_offset);
1163
            }
1164
            if (field->flags & VMS_POINTER) {
1165
                base_addr = *(void **)base_addr;
1166
            }
1167
            for (i = 0; i < n_elems; i++) {
1168
                void *addr = base_addr + field->size * i;
1169

    
1170
                if (field->flags & VMS_STRUCT) {
1171
                    vmstate_save_state(f, field->vmsd, addr);
1172
                } else {
1173
                    field->info->put(f, addr, field->size);
1174
                }
1175
            }
1176
        }
1177
        field++;
1178
    }
1179
    if (vmsd->post_save) {
1180
        vmsd->post_save(opaque);
1181
    }
1182
}
1183

    
1184
static int vmstate_load(QEMUFile *f, SaveStateEntry *se, int version_id)
1185
{
1186
    if (!se->vmsd) {         /* Old style */
1187
        return se->load_state(f, se->opaque, version_id);
1188
    }
1189
    return vmstate_load_state(f, se->vmsd, se->opaque, version_id);
1190
}
1191

    
1192
static void vmstate_save(QEMUFile *f, SaveStateEntry *se)
1193
{
1194
    if (!se->vmsd) {         /* Old style */
1195
        se->save_state(f, se->opaque);
1196
        return;
1197
    }
1198
    vmstate_save_state(f,se->vmsd, se->opaque);
1199
}
1200

    
1201
#define QEMU_VM_FILE_MAGIC           0x5145564d
1202
#define QEMU_VM_FILE_VERSION_COMPAT  0x00000002
1203
#define QEMU_VM_FILE_VERSION         0x00000003
1204

    
1205
#define QEMU_VM_EOF                  0x00
1206
#define QEMU_VM_SECTION_START        0x01
1207
#define QEMU_VM_SECTION_PART         0x02
1208
#define QEMU_VM_SECTION_END          0x03
1209
#define QEMU_VM_SECTION_FULL         0x04
1210

    
1211
int qemu_savevm_state_begin(QEMUFile *f)
1212
{
1213
    SaveStateEntry *se;
1214

    
1215
    qemu_put_be32(f, QEMU_VM_FILE_MAGIC);
1216
    qemu_put_be32(f, QEMU_VM_FILE_VERSION);
1217

    
1218
    QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1219
        int len;
1220

    
1221
        if (se->save_live_state == NULL)
1222
            continue;
1223

    
1224
        /* Section type */
1225
        qemu_put_byte(f, QEMU_VM_SECTION_START);
1226
        qemu_put_be32(f, se->section_id);
1227

    
1228
        /* ID string */
1229
        len = strlen(se->idstr);
1230
        qemu_put_byte(f, len);
1231
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1232

    
1233
        qemu_put_be32(f, se->instance_id);
1234
        qemu_put_be32(f, se->version_id);
1235

    
1236
        se->save_live_state(f, QEMU_VM_SECTION_START, se->opaque);
1237
    }
1238

    
1239
    if (qemu_file_has_error(f))
1240
        return -EIO;
1241

    
1242
    return 0;
1243
}
1244

    
1245
int qemu_savevm_state_iterate(QEMUFile *f)
1246
{
1247
    SaveStateEntry *se;
1248
    int ret = 1;
1249

    
1250
    QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1251
        if (se->save_live_state == NULL)
1252
            continue;
1253

    
1254
        /* Section type */
1255
        qemu_put_byte(f, QEMU_VM_SECTION_PART);
1256
        qemu_put_be32(f, se->section_id);
1257

    
1258
        ret &= !!se->save_live_state(f, QEMU_VM_SECTION_PART, se->opaque);
1259
    }
1260

    
1261
    if (ret)
1262
        return 1;
1263

    
1264
    if (qemu_file_has_error(f))
1265
        return -EIO;
1266

    
1267
    return 0;
1268
}
1269

    
1270
int qemu_savevm_state_complete(QEMUFile *f)
1271
{
1272
    SaveStateEntry *se;
1273

    
1274
    QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1275
        if (se->save_live_state == NULL)
1276
            continue;
1277

    
1278
        /* Section type */
1279
        qemu_put_byte(f, QEMU_VM_SECTION_END);
1280
        qemu_put_be32(f, se->section_id);
1281

    
1282
        se->save_live_state(f, QEMU_VM_SECTION_END, se->opaque);
1283
    }
1284

    
1285
    QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1286
        int len;
1287

    
1288
        if (se->save_state == NULL && se->vmsd == NULL)
1289
            continue;
1290

    
1291
        /* Section type */
1292
        qemu_put_byte(f, QEMU_VM_SECTION_FULL);
1293
        qemu_put_be32(f, se->section_id);
1294

    
1295
        /* ID string */
1296
        len = strlen(se->idstr);
1297
        qemu_put_byte(f, len);
1298
        qemu_put_buffer(f, (uint8_t *)se->idstr, len);
1299

    
1300
        qemu_put_be32(f, se->instance_id);
1301
        qemu_put_be32(f, se->version_id);
1302

    
1303
        vmstate_save(f, se);
1304
    }
1305

    
1306
    qemu_put_byte(f, QEMU_VM_EOF);
1307

    
1308
    if (qemu_file_has_error(f))
1309
        return -EIO;
1310

    
1311
    return 0;
1312
}
1313

    
1314
int qemu_savevm_state(QEMUFile *f)
1315
{
1316
    int saved_vm_running;
1317
    int ret;
1318

    
1319
    saved_vm_running = vm_running;
1320
    vm_stop(0);
1321

    
1322
    bdrv_flush_all();
1323

    
1324
    ret = qemu_savevm_state_begin(f);
1325
    if (ret < 0)
1326
        goto out;
1327

    
1328
    do {
1329
        ret = qemu_savevm_state_iterate(f);
1330
        if (ret < 0)
1331
            goto out;
1332
    } while (ret == 0);
1333

    
1334
    ret = qemu_savevm_state_complete(f);
1335

    
1336
out:
1337
    if (qemu_file_has_error(f))
1338
        ret = -EIO;
1339

    
1340
    if (!ret && saved_vm_running)
1341
        vm_start();
1342

    
1343
    return ret;
1344
}
1345

    
1346
static SaveStateEntry *find_se(const char *idstr, int instance_id)
1347
{
1348
    SaveStateEntry *se;
1349

    
1350
    QTAILQ_FOREACH(se, &savevm_handlers, entry) {
1351
        if (!strcmp(se->idstr, idstr) &&
1352
            instance_id == se->instance_id)
1353
            return se;
1354
    }
1355
    return NULL;
1356
}
1357

    
1358
typedef struct LoadStateEntry {
1359
    QLIST_ENTRY(LoadStateEntry) entry;
1360
    SaveStateEntry *se;
1361
    int section_id;
1362
    int version_id;
1363
} LoadStateEntry;
1364

    
1365
int qemu_loadvm_state(QEMUFile *f)
1366
{
1367
    QLIST_HEAD(, LoadStateEntry) loadvm_handlers =
1368
        QLIST_HEAD_INITIALIZER(loadvm_handlers);
1369
    LoadStateEntry *le, *new_le;
1370
    uint8_t section_type;
1371
    unsigned int v;
1372
    int ret;
1373

    
1374
    v = qemu_get_be32(f);
1375
    if (v != QEMU_VM_FILE_MAGIC)
1376
        return -EINVAL;
1377

    
1378
    v = qemu_get_be32(f);
1379
    if (v == QEMU_VM_FILE_VERSION_COMPAT) {
1380
        fprintf(stderr, "SaveVM v2 format is obsolete and don't work anymore\n");
1381
        return -ENOTSUP;
1382
    }
1383
    if (v != QEMU_VM_FILE_VERSION)
1384
        return -ENOTSUP;
1385

    
1386
    while ((section_type = qemu_get_byte(f)) != QEMU_VM_EOF) {
1387
        uint32_t instance_id, version_id, section_id;
1388
        SaveStateEntry *se;
1389
        char idstr[257];
1390
        int len;
1391

    
1392
        switch (section_type) {
1393
        case QEMU_VM_SECTION_START:
1394
        case QEMU_VM_SECTION_FULL:
1395
            /* Read section start */
1396
            section_id = qemu_get_be32(f);
1397
            len = qemu_get_byte(f);
1398
            qemu_get_buffer(f, (uint8_t *)idstr, len);
1399
            idstr[len] = 0;
1400
            instance_id = qemu_get_be32(f);
1401
            version_id = qemu_get_be32(f);
1402

    
1403
            /* Find savevm section */
1404
            se = find_se(idstr, instance_id);
1405
            if (se == NULL) {
1406
                fprintf(stderr, "Unknown savevm section or instance '%s' %d\n", idstr, instance_id);
1407
                ret = -EINVAL;
1408
                goto out;
1409
            }
1410

    
1411
            /* Validate version */
1412
            if (version_id > se->version_id) {
1413
                fprintf(stderr, "savevm: unsupported version %d for '%s' v%d\n",
1414
                        version_id, idstr, se->version_id);
1415
                ret = -EINVAL;
1416
                goto out;
1417
            }
1418

    
1419
            /* Add entry */
1420
            le = qemu_mallocz(sizeof(*le));
1421

    
1422
            le->se = se;
1423
            le->section_id = section_id;
1424
            le->version_id = version_id;
1425
            QLIST_INSERT_HEAD(&loadvm_handlers, le, entry);
1426

    
1427
            ret = vmstate_load(f, le->se, le->version_id);
1428
            if (ret < 0) {
1429
                fprintf(stderr, "qemu: warning: error while loading state for instance 0x%x of device '%s'\n",
1430
                        instance_id, idstr);
1431
                goto out;
1432
            }
1433
            break;
1434
        case QEMU_VM_SECTION_PART:
1435
        case QEMU_VM_SECTION_END:
1436
            section_id = qemu_get_be32(f);
1437

    
1438
            QLIST_FOREACH(le, &loadvm_handlers, entry) {
1439
                if (le->section_id == section_id) {
1440
                    break;
1441
                }
1442
            }
1443
            if (le == NULL) {
1444
                fprintf(stderr, "Unknown savevm section %d\n", section_id);
1445
                ret = -EINVAL;
1446
                goto out;
1447
            }
1448

    
1449
            ret = vmstate_load(f, le->se, le->version_id);
1450
            if (ret < 0) {
1451
                fprintf(stderr, "qemu: warning: error while loading state section id %d\n",
1452
                        section_id);
1453
                goto out;
1454
            }
1455
            break;
1456
        default:
1457
            fprintf(stderr, "Unknown savevm section type %d\n", section_type);
1458
            ret = -EINVAL;
1459
            goto out;
1460
        }
1461
    }
1462

    
1463
    ret = 0;
1464

    
1465
out:
1466
    QLIST_FOREACH_SAFE(le, &loadvm_handlers, entry, new_le) {
1467
        QLIST_REMOVE(le, entry);
1468
        qemu_free(le);
1469
    }
1470

    
1471
    if (qemu_file_has_error(f))
1472
        ret = -EIO;
1473

    
1474
    return ret;
1475
}
1476

    
1477
/* device can contain snapshots */
1478
static int bdrv_can_snapshot(BlockDriverState *bs)
1479
{
1480
    return (bs &&
1481
            !bdrv_is_removable(bs) &&
1482
            !bdrv_is_read_only(bs));
1483
}
1484

    
1485
/* device must be snapshots in order to have a reliable snapshot */
1486
static int bdrv_has_snapshot(BlockDriverState *bs)
1487
{
1488
    return (bs &&
1489
            !bdrv_is_removable(bs) &&
1490
            !bdrv_is_read_only(bs));
1491
}
1492

    
1493
static BlockDriverState *get_bs_snapshots(void)
1494
{
1495
    BlockDriverState *bs;
1496
    DriveInfo *dinfo;
1497

    
1498
    if (bs_snapshots)
1499
        return bs_snapshots;
1500
    QTAILQ_FOREACH(dinfo, &drives, next) {
1501
        bs = dinfo->bdrv;
1502
        if (bdrv_can_snapshot(bs))
1503
            goto ok;
1504
    }
1505
    return NULL;
1506
 ok:
1507
    bs_snapshots = bs;
1508
    return bs;
1509
}
1510

    
1511
static int bdrv_snapshot_find(BlockDriverState *bs, QEMUSnapshotInfo *sn_info,
1512
                              const char *name)
1513
{
1514
    QEMUSnapshotInfo *sn_tab, *sn;
1515
    int nb_sns, i, ret;
1516

    
1517
    ret = -ENOENT;
1518
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1519
    if (nb_sns < 0)
1520
        return ret;
1521
    for(i = 0; i < nb_sns; i++) {
1522
        sn = &sn_tab[i];
1523
        if (!strcmp(sn->id_str, name) || !strcmp(sn->name, name)) {
1524
            *sn_info = *sn;
1525
            ret = 0;
1526
            break;
1527
        }
1528
    }
1529
    qemu_free(sn_tab);
1530
    return ret;
1531
}
1532

    
1533
void do_savevm(Monitor *mon, const QDict *qdict)
1534
{
1535
    DriveInfo *dinfo;
1536
    BlockDriverState *bs, *bs1;
1537
    QEMUSnapshotInfo sn1, *sn = &sn1, old_sn1, *old_sn = &old_sn1;
1538
    int must_delete, ret;
1539
    QEMUFile *f;
1540
    int saved_vm_running;
1541
    uint32_t vm_state_size;
1542
#ifdef _WIN32
1543
    struct _timeb tb;
1544
#else
1545
    struct timeval tv;
1546
#endif
1547
    const char *name = qdict_get_try_str(qdict, "name");
1548

    
1549
    bs = get_bs_snapshots();
1550
    if (!bs) {
1551
        monitor_printf(mon, "No block device can accept snapshots\n");
1552
        return;
1553
    }
1554

    
1555
    /* ??? Should this occur after vm_stop?  */
1556
    qemu_aio_flush();
1557

    
1558
    saved_vm_running = vm_running;
1559
    vm_stop(0);
1560

    
1561
    must_delete = 0;
1562
    if (name) {
1563
        ret = bdrv_snapshot_find(bs, old_sn, name);
1564
        if (ret >= 0) {
1565
            must_delete = 1;
1566
        }
1567
    }
1568
    memset(sn, 0, sizeof(*sn));
1569
    if (must_delete) {
1570
        pstrcpy(sn->name, sizeof(sn->name), old_sn->name);
1571
        pstrcpy(sn->id_str, sizeof(sn->id_str), old_sn->id_str);
1572
    } else {
1573
        if (name)
1574
            pstrcpy(sn->name, sizeof(sn->name), name);
1575
    }
1576

    
1577
    /* fill auxiliary fields */
1578
#ifdef _WIN32
1579
    _ftime(&tb);
1580
    sn->date_sec = tb.time;
1581
    sn->date_nsec = tb.millitm * 1000000;
1582
#else
1583
    gettimeofday(&tv, NULL);
1584
    sn->date_sec = tv.tv_sec;
1585
    sn->date_nsec = tv.tv_usec * 1000;
1586
#endif
1587
    sn->vm_clock_nsec = qemu_get_clock(vm_clock);
1588

    
1589
    /* save the VM state */
1590
    f = qemu_fopen_bdrv(bs, 1);
1591
    if (!f) {
1592
        monitor_printf(mon, "Could not open VM state file\n");
1593
        goto the_end;
1594
    }
1595
    ret = qemu_savevm_state(f);
1596
    vm_state_size = qemu_ftell(f);
1597
    qemu_fclose(f);
1598
    if (ret < 0) {
1599
        monitor_printf(mon, "Error %d while writing VM\n", ret);
1600
        goto the_end;
1601
    }
1602

    
1603
    /* create the snapshots */
1604

    
1605
    QTAILQ_FOREACH(dinfo, &drives, next) {
1606
        bs1 = dinfo->bdrv;
1607
        if (bdrv_has_snapshot(bs1)) {
1608
            if (must_delete) {
1609
                ret = bdrv_snapshot_delete(bs1, old_sn->id_str);
1610
                if (ret < 0) {
1611
                    monitor_printf(mon,
1612
                                   "Error while deleting snapshot on '%s'\n",
1613
                                   bdrv_get_device_name(bs1));
1614
                }
1615
            }
1616
            /* Write VM state size only to the image that contains the state */
1617
            sn->vm_state_size = (bs == bs1 ? vm_state_size : 0);
1618
            ret = bdrv_snapshot_create(bs1, sn);
1619
            if (ret < 0) {
1620
                monitor_printf(mon, "Error while creating snapshot on '%s'\n",
1621
                               bdrv_get_device_name(bs1));
1622
            }
1623
        }
1624
    }
1625

    
1626
 the_end:
1627
    if (saved_vm_running)
1628
        vm_start();
1629
}
1630

    
1631
int load_vmstate(Monitor *mon, const char *name)
1632
{
1633
    DriveInfo *dinfo;
1634
    BlockDriverState *bs, *bs1;
1635
    QEMUSnapshotInfo sn;
1636
    QEMUFile *f;
1637
    int ret;
1638

    
1639
    bs = get_bs_snapshots();
1640
    if (!bs) {
1641
        monitor_printf(mon, "No block device supports snapshots\n");
1642
        return -EINVAL;
1643
    }
1644

    
1645
    /* Flush all IO requests so they don't interfere with the new state.  */
1646
    qemu_aio_flush();
1647

    
1648
    QTAILQ_FOREACH(dinfo, &drives, next) {
1649
        bs1 = dinfo->bdrv;
1650
        if (bdrv_has_snapshot(bs1)) {
1651
            ret = bdrv_snapshot_goto(bs1, name);
1652
            if (ret < 0) {
1653
                if (bs != bs1)
1654
                    monitor_printf(mon, "Warning: ");
1655
                switch(ret) {
1656
                case -ENOTSUP:
1657
                    monitor_printf(mon,
1658
                                   "Snapshots not supported on device '%s'\n",
1659
                                   bdrv_get_device_name(bs1));
1660
                    break;
1661
                case -ENOENT:
1662
                    monitor_printf(mon, "Could not find snapshot '%s' on "
1663
                                   "device '%s'\n",
1664
                                   name, bdrv_get_device_name(bs1));
1665
                    break;
1666
                default:
1667
                    monitor_printf(mon, "Error %d while activating snapshot on"
1668
                                   " '%s'\n", ret, bdrv_get_device_name(bs1));
1669
                    break;
1670
                }
1671
                /* fatal on snapshot block device */
1672
                if (bs == bs1)
1673
                    return 0;
1674
            }
1675
        }
1676
    }
1677

    
1678
    /* Don't even try to load empty VM states */
1679
    ret = bdrv_snapshot_find(bs, &sn, name);
1680
    if ((ret >= 0) && (sn.vm_state_size == 0))
1681
        return -EINVAL;
1682

    
1683
    /* restore the VM state */
1684
    f = qemu_fopen_bdrv(bs, 0);
1685
    if (!f) {
1686
        monitor_printf(mon, "Could not open VM state file\n");
1687
        return -EINVAL;
1688
    }
1689
    ret = qemu_loadvm_state(f);
1690
    qemu_fclose(f);
1691
    if (ret < 0) {
1692
        monitor_printf(mon, "Error %d while loading VM state\n", ret);
1693
        return ret;
1694
    }
1695
    return 0;
1696
}
1697

    
1698
void do_delvm(Monitor *mon, const QDict *qdict)
1699
{
1700
    DriveInfo *dinfo;
1701
    BlockDriverState *bs, *bs1;
1702
    int ret;
1703
    const char *name = qdict_get_str(qdict, "name");
1704

    
1705
    bs = get_bs_snapshots();
1706
    if (!bs) {
1707
        monitor_printf(mon, "No block device supports snapshots\n");
1708
        return;
1709
    }
1710

    
1711
    QTAILQ_FOREACH(dinfo, &drives, next) {
1712
        bs1 = dinfo->bdrv;
1713
        if (bdrv_has_snapshot(bs1)) {
1714
            ret = bdrv_snapshot_delete(bs1, name);
1715
            if (ret < 0) {
1716
                if (ret == -ENOTSUP)
1717
                    monitor_printf(mon,
1718
                                   "Snapshots not supported on device '%s'\n",
1719
                                   bdrv_get_device_name(bs1));
1720
                else
1721
                    monitor_printf(mon, "Error %d while deleting snapshot on "
1722
                                   "'%s'\n", ret, bdrv_get_device_name(bs1));
1723
            }
1724
        }
1725
    }
1726
}
1727

    
1728
void do_info_snapshots(Monitor *mon)
1729
{
1730
    DriveInfo *dinfo;
1731
    BlockDriverState *bs, *bs1;
1732
    QEMUSnapshotInfo *sn_tab, *sn;
1733
    int nb_sns, i;
1734
    char buf[256];
1735

    
1736
    bs = get_bs_snapshots();
1737
    if (!bs) {
1738
        monitor_printf(mon, "No available block device supports snapshots\n");
1739
        return;
1740
    }
1741
    monitor_printf(mon, "Snapshot devices:");
1742
    QTAILQ_FOREACH(dinfo, &drives, next) {
1743
        bs1 = dinfo->bdrv;
1744
        if (bdrv_has_snapshot(bs1)) {
1745
            if (bs == bs1)
1746
                monitor_printf(mon, " %s", bdrv_get_device_name(bs1));
1747
        }
1748
    }
1749
    monitor_printf(mon, "\n");
1750

    
1751
    nb_sns = bdrv_snapshot_list(bs, &sn_tab);
1752
    if (nb_sns < 0) {
1753
        monitor_printf(mon, "bdrv_snapshot_list: error %d\n", nb_sns);
1754
        return;
1755
    }
1756
    monitor_printf(mon, "Snapshot list (from %s):\n",
1757
                   bdrv_get_device_name(bs));
1758
    monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), NULL));
1759
    for(i = 0; i < nb_sns; i++) {
1760
        sn = &sn_tab[i];
1761
        monitor_printf(mon, "%s\n", bdrv_snapshot_dump(buf, sizeof(buf), sn));
1762
    }
1763
    qemu_free(sn_tab);
1764
}