vmstate.c 28.2 KB
Newer Older
P
Peter Maydell 已提交
1
#include "qemu/osdep.h"
2 3 4 5 6
#include "qemu-common.h"
#include "migration/migration.h"
#include "migration/qemu-file.h"
#include "migration/vmstate.h"
#include "qemu/bitops.h"
7
#include "qemu/error-report.h"
J
Jianjun Duan 已提交
8
#include "qemu/queue.h"
9
#include "trace.h"
J
Jianjun Duan 已提交
10
#include "migration/qjson.h"
11 12

static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
13
                                    void *opaque, QJSON *vmdesc);
14 15 16
static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
                                   void *opaque);

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
static int vmstate_n_elems(void *opaque, VMStateField *field)
{
    int n_elems = 1;

    if (field->flags & VMS_ARRAY) {
        n_elems = field->num;
    } else if (field->flags & VMS_VARRAY_INT32) {
        n_elems = *(int32_t *)(opaque+field->num_offset);
    } else if (field->flags & VMS_VARRAY_UINT32) {
        n_elems = *(uint32_t *)(opaque+field->num_offset);
    } else if (field->flags & VMS_VARRAY_UINT16) {
        n_elems = *(uint16_t *)(opaque+field->num_offset);
    } else if (field->flags & VMS_VARRAY_UINT8) {
        n_elems = *(uint8_t *)(opaque+field->num_offset);
    }

33 34 35 36
    if (field->flags & VMS_MULTIPLY_ELEMENTS) {
        n_elems *= field->num;
    }

37
    trace_vmstate_n_elems(field->name, n_elems);
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    return n_elems;
}

static int vmstate_size(void *opaque, VMStateField *field)
{
    int size = field->size;

    if (field->flags & VMS_VBUFFER) {
        size = *(int32_t *)(opaque+field->size_offset);
        if (field->flags & VMS_MULTIPLY) {
            size *= field->size;
        }
    }

    return size;
}

55
static void *vmstate_base_addr(void *opaque, VMStateField *field, bool alloc)
56 57 58 59
{
    void *base_addr = opaque + field->offset;

    if (field->flags & VMS_POINTER) {
60
        if (alloc && (field->flags & VMS_ALLOC)) {
61 62 63 64 65 66 67 68 69 70
            gsize size = 0;
            if (field->flags & VMS_VBUFFER) {
                size = vmstate_size(opaque, field);
            } else {
                int n_elems = vmstate_n_elems(opaque, field);
                if (n_elems) {
                    size = n_elems * field->size;
                }
            }
            if (size) {
71
                *(void **)base_addr = g_malloc(size);
72 73
            }
        }
74
        base_addr = *(void **)base_addr;
75 76 77 78 79
    }

    return base_addr;
}

80 81 82 83
int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
                       void *opaque, int version_id)
{
    VMStateField *field = vmsd->fields;
84
    int ret = 0;
85

86
    trace_vmstate_load_state(vmsd->name, version_id);
87
    if (version_id > vmsd->version_id) {
J
Jianjun Duan 已提交
88 89 90
        error_report("%s: incoming version_id %d is too new "
                     "for local version_id %d",
                     vmsd->name, version_id, vmsd->version_id);
91
        trace_vmstate_load_state_end(vmsd->name, "too new", -EINVAL);
92 93 94
        return -EINVAL;
    }
    if  (version_id < vmsd->minimum_version_id) {
95 96
        if (vmsd->load_state_old &&
            version_id >= vmsd->minimum_version_id_old) {
97 98 99
            ret = vmsd->load_state_old(f, opaque, version_id);
            trace_vmstate_load_state_end(vmsd->name, "old path", ret);
            return ret;
100
        }
J
Jianjun Duan 已提交
101 102 103
        error_report("%s: incoming version_id %d is too old "
                     "for local minimum version_id  %d",
                     vmsd->name, version_id, vmsd->minimum_version_id);
104
        trace_vmstate_load_state_end(vmsd->name, "too old", -EINVAL);
105
        return -EINVAL;
106 107 108 109 110 111 112 113
    }
    if (vmsd->pre_load) {
        int ret = vmsd->pre_load(opaque);
        if (ret) {
            return ret;
        }
    }
    while (field->name) {
114
        trace_vmstate_load_state_field(vmsd->name, field->name);
115 116 117 118
        if ((field->field_exists &&
             field->field_exists(opaque, version_id)) ||
            (!field->field_exists &&
             field->version_id <= version_id)) {
119
            void *base_addr = vmstate_base_addr(opaque, field, true);
120 121 122
            int i, n_elems = vmstate_n_elems(opaque, field);
            int size = vmstate_size(opaque, field);

123 124 125 126 127 128 129 130 131 132
            for (i = 0; i < n_elems; i++) {
                void *addr = base_addr + size * i;

                if (field->flags & VMS_ARRAY_OF_POINTER) {
                    addr = *(void **)addr;
                }
                if (field->flags & VMS_STRUCT) {
                    ret = vmstate_load_state(f, field->vmsd, addr,
                                             field->vmsd->version_id);
                } else {
J
Jianjun Duan 已提交
133
                   ret = field->info->get(f, addr, size, field);
134
                }
135 136 137
                if (ret >= 0) {
                    ret = qemu_file_get_error(f);
                }
138
                if (ret < 0) {
139
                    qemu_file_set_error(f, ret);
140 141
                    error_report("Failed to load %s:%s", vmsd->name,
                                 field->name);
142
                    trace_vmstate_load_field_error(field->name, ret);
143 144 145
                    return ret;
                }
            }
M
Michael S. Tsirkin 已提交
146
        } else if (field->flags & VMS_MUST_EXIST) {
147 148
            error_report("Input validation failed: %s/%s",
                         vmsd->name, field->name);
M
Michael S. Tsirkin 已提交
149
            return -1;
150 151 152 153 154 155 156 157
        }
        field++;
    }
    ret = vmstate_subsection_load(f, vmsd, opaque);
    if (ret != 0) {
        return ret;
    }
    if (vmsd->post_load) {
158
        ret = vmsd->post_load(opaque, version_id);
159
    }
160 161
    trace_vmstate_load_state_end(vmsd->name, "end", ret);
    return ret;
162 163
}

164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static int vmfield_name_num(VMStateField *start, VMStateField *search)
{
    VMStateField *field;
    int found = 0;

    for (field = start; field->name; field++) {
        if (!strcmp(field->name, search->name)) {
            if (field == search) {
                return found;
            }
            found++;
        }
    }

    return -1;
}

static bool vmfield_name_is_unique(VMStateField *start, VMStateField *search)
{
    VMStateField *field;
    int found = 0;

    for (field = start; field->name; field++) {
        if (!strcmp(field->name, search->name)) {
            found++;
            /* name found more than once, so it's not unique */
            if (found > 1) {
                return false;
            }
        }
    }

    return true;
}

static const char *vmfield_get_type_name(VMStateField *field)
{
    const char *type = "unknown";

    if (field->flags & VMS_STRUCT) {
        type = "struct";
    } else if (field->info->name) {
        type = field->info->name;
    }

    return type;
}

static bool vmsd_can_compress(VMStateField *field)
{
    if (field->field_exists) {
        /* Dynamically existing fields mess up compression */
        return false;
    }

    if (field->flags & VMS_STRUCT) {
        VMStateField *sfield = field->vmsd->fields;
        while (sfield->name) {
            if (!vmsd_can_compress(sfield)) {
                /* Child elements can't compress, so can't we */
                return false;
            }
            sfield++;
        }

        if (field->vmsd->subsections) {
            /* Subsections may come and go, better don't compress */
            return false;
        }
    }

    return true;
}

static void vmsd_desc_field_start(const VMStateDescription *vmsd, QJSON *vmdesc,
                                  VMStateField *field, int i, int max)
{
    char *name, *old_name;
    bool is_array = max > 1;
    bool can_compress = vmsd_can_compress(field);

    if (!vmdesc) {
        return;
    }

    name = g_strdup(field->name);

    /* Field name is not unique, need to make it unique */
    if (!vmfield_name_is_unique(vmsd->fields, field)) {
        int num = vmfield_name_num(vmsd->fields, field);
        old_name = name;
        name = g_strdup_printf("%s[%d]", name, num);
        g_free(old_name);
    }

    json_start_object(vmdesc, NULL);
    json_prop_str(vmdesc, "name", name);
    if (is_array) {
        if (can_compress) {
            json_prop_int(vmdesc, "array_len", max);
        } else {
            json_prop_int(vmdesc, "index", i);
        }
    }
    json_prop_str(vmdesc, "type", vmfield_get_type_name(field));

    if (field->flags & VMS_STRUCT) {
        json_start_object(vmdesc, "struct");
    }

    g_free(name);
}

static void vmsd_desc_field_end(const VMStateDescription *vmsd, QJSON *vmdesc,
                                VMStateField *field, size_t size, int i)
{
    if (!vmdesc) {
        return;
    }

    if (field->flags & VMS_STRUCT) {
        /* We printed a struct in between, close its child object */
        json_end_object(vmdesc);
    }

    json_prop_int(vmdesc, "size", size);
    json_end_object(vmdesc);
}

293 294 295 296 297 298 299 300 301 302 303

bool vmstate_save_needed(const VMStateDescription *vmsd, void *opaque)
{
    if (vmsd->needed && !vmsd->needed(opaque)) {
        /* optional section not needed */
        return false;
    }
    return true;
}


304
void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
305
                        void *opaque, QJSON *vmdesc)
306 307 308
{
    VMStateField *field = vmsd->fields;

309 310
    trace_vmstate_save_state_top(vmsd->name);

311 312 313
    if (vmsd->pre_save) {
        vmsd->pre_save(opaque);
    }
314 315 316 317 318 319 320

    if (vmdesc) {
        json_prop_str(vmdesc, "vmsd_name", vmsd->name);
        json_prop_int(vmdesc, "version", vmsd->version_id);
        json_start_array(vmdesc, "fields");
    }

321 322 323
    while (field->name) {
        if (!field->field_exists ||
            field->field_exists(opaque, vmsd->version_id)) {
324
            void *base_addr = vmstate_base_addr(opaque, field, false);
325 326
            int i, n_elems = vmstate_n_elems(opaque, field);
            int size = vmstate_size(opaque, field);
327 328
            int64_t old_offset, written_bytes;
            QJSON *vmdesc_loop = vmdesc;
329

330
            trace_vmstate_save_state_loop(vmsd->name, field->name, n_elems);
331 332 333
            for (i = 0; i < n_elems; i++) {
                void *addr = base_addr + size * i;

334 335 336
                vmsd_desc_field_start(vmsd, vmdesc_loop, field, i, n_elems);
                old_offset = qemu_ftell_fast(f);

337 338 339 340
                if (field->flags & VMS_ARRAY_OF_POINTER) {
                    addr = *(void **)addr;
                }
                if (field->flags & VMS_STRUCT) {
341
                    vmstate_save_state(f, field->vmsd, addr, vmdesc_loop);
342
                } else {
J
Jianjun Duan 已提交
343
                    field->info->put(f, addr, size, field, vmdesc_loop);
344
                }
345 346 347 348 349 350 351 352

                written_bytes = qemu_ftell_fast(f) - old_offset;
                vmsd_desc_field_end(vmsd, vmdesc_loop, field, written_bytes, i);

                /* Compressed arrays only care about the first element */
                if (vmdesc_loop && vmsd_can_compress(field)) {
                    vmdesc_loop = NULL;
                }
353
            }
M
Michael S. Tsirkin 已提交
354 355
        } else {
            if (field->flags & VMS_MUST_EXIST) {
356
                error_report("Output state validation failed: %s/%s",
M
Michael S. Tsirkin 已提交
357 358 359
                        vmsd->name, field->name);
                assert(!(field->flags & VMS_MUST_EXIST));
            }
360 361 362
        }
        field++;
    }
363 364 365 366 367 368

    if (vmdesc) {
        json_end_array(vmdesc);
    }

    vmstate_subsection_save(f, vmsd, opaque, vmdesc);
369 370 371
}

static const VMStateDescription *
372
vmstate_get_subsection(const VMStateDescription **sub, char *idstr)
373
{
374 375 376
    while (sub && *sub && (*sub)->needed) {
        if (strcmp(idstr, (*sub)->name) == 0) {
            return *sub;
377 378 379 380 381 382 383 384 385
        }
        sub++;
    }
    return NULL;
}

static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
                                   void *opaque)
{
386 387
    trace_vmstate_subsection_load(vmsd->name);

388
    while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
389
        char idstr[256], *idstr_ret;
390 391 392 393 394 395 396
        int ret;
        uint8_t version_id, len, size;
        const VMStateDescription *sub_vmsd;

        len = qemu_peek_byte(f, 1);
        if (len < strlen(vmsd->name) + 1) {
            /* subsection name has be be "section_name/a" */
397
            trace_vmstate_subsection_load_bad(vmsd->name, "(short)", "");
398 399
            return 0;
        }
400
        size = qemu_peek_buffer(f, (uint8_t **)&idstr_ret, len, 2);
401
        if (size != len) {
402
            trace_vmstate_subsection_load_bad(vmsd->name, "(peek fail)", "");
403 404
            return 0;
        }
405
        memcpy(idstr, idstr_ret, size);
406 407 408
        idstr[size] = 0;

        if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
409 410
            trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(prefix)");
            /* it doesn't have a valid subsection name */
411 412 413 414
            return 0;
        }
        sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
        if (sub_vmsd == NULL) {
415
            trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(lookup)");
416 417 418 419 420 421 422 423 424
            return -ENOENT;
        }
        qemu_file_skip(f, 1); /* subsection */
        qemu_file_skip(f, 1); /* len */
        qemu_file_skip(f, len); /* idstr */
        version_id = qemu_get_be32(f);

        ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
        if (ret) {
425
            trace_vmstate_subsection_load_bad(vmsd->name, idstr, "(child)");
426 427 428
            return ret;
        }
    }
429 430

    trace_vmstate_subsection_load_good(vmsd->name);
431 432 433 434
    return 0;
}

static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
435
                                    void *opaque, QJSON *vmdesc)
436
{
437
    const VMStateDescription **sub = vmsd->subsections;
438
    bool subsection_found = false;
439

440
    trace_vmstate_subsection_save_top(vmsd->name);
441 442
    while (sub && *sub && (*sub)->needed) {
        if ((*sub)->needed(opaque)) {
443
            const VMStateDescription *vmsdsub = *sub;
444 445
            uint8_t len;

446
            trace_vmstate_subsection_save_loop(vmsd->name, vmsdsub->name);
447 448 449 450 451 452 453 454 455 456
            if (vmdesc) {
                /* Only create subsection array when we have any */
                if (!subsection_found) {
                    json_start_array(vmdesc, "subsections");
                    subsection_found = true;
                }

                json_start_object(vmdesc, NULL);
            }

457
            qemu_put_byte(f, QEMU_VM_SUBSECTION);
458
            len = strlen(vmsdsub->name);
459
            qemu_put_byte(f, len);
460 461 462
            qemu_put_buffer(f, (uint8_t *)vmsdsub->name, len);
            qemu_put_be32(f, vmsdsub->version_id);
            vmstate_save_state(f, vmsdsub, opaque, vmdesc);
463 464 465 466

            if (vmdesc) {
                json_end_object(vmdesc);
            }
467 468 469
        }
        sub++;
    }
470 471 472 473

    if (vmdesc && subsection_found) {
        json_end_array(vmdesc);
    }
474 475 476 477
}

/* bool */

J
Jianjun Duan 已提交
478
static int get_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field)
479 480 481 482 483 484
{
    bool *v = pv;
    *v = qemu_get_byte(f);
    return 0;
}

J
Jianjun Duan 已提交
485 486
static int put_bool(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                    QJSON *vmdesc)
487 488 489
{
    bool *v = pv;
    qemu_put_byte(f, *v);
J
Jianjun Duan 已提交
490
    return 0;
491 492 493 494 495 496 497 498 499 500
}

const VMStateInfo vmstate_info_bool = {
    .name = "bool",
    .get  = get_bool,
    .put  = put_bool,
};

/* 8 bit int */

J
Jianjun Duan 已提交
501
static int get_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
502 503 504 505 506 507
{
    int8_t *v = pv;
    qemu_get_s8s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
508 509
static int put_int8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                     QJSON *vmdesc)
510 511 512
{
    int8_t *v = pv;
    qemu_put_s8s(f, v);
J
Jianjun Duan 已提交
513
    return 0;
514 515 516 517 518 519 520 521 522 523
}

const VMStateInfo vmstate_info_int8 = {
    .name = "int8",
    .get  = get_int8,
    .put  = put_int8,
};

/* 16 bit int */

J
Jianjun Duan 已提交
524
static int get_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
525 526 527 528 529 530
{
    int16_t *v = pv;
    qemu_get_sbe16s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
531 532
static int put_int16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                     QJSON *vmdesc)
533 534 535
{
    int16_t *v = pv;
    qemu_put_sbe16s(f, v);
J
Jianjun Duan 已提交
536
    return 0;
537 538 539 540 541 542 543 544 545 546
}

const VMStateInfo vmstate_info_int16 = {
    .name = "int16",
    .get  = get_int16,
    .put  = put_int16,
};

/* 32 bit int */

J
Jianjun Duan 已提交
547
static int get_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
548 549 550 551 552 553
{
    int32_t *v = pv;
    qemu_get_sbe32s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
554 555
static int put_int32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                     QJSON *vmdesc)
556 557 558
{
    int32_t *v = pv;
    qemu_put_sbe32s(f, v);
J
Jianjun Duan 已提交
559
    return 0;
560 561 562 563 564 565 566 567 568 569 570
}

const VMStateInfo vmstate_info_int32 = {
    .name = "int32",
    .get  = get_int32,
    .put  = put_int32,
};

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

J
Jianjun Duan 已提交
571 572
static int get_int32_equal(QEMUFile *f, void *pv, size_t size,
                           VMStateField *field)
573 574 575 576 577 578 579 580
{
    int32_t *v = pv;
    int32_t v2;
    qemu_get_sbe32s(f, &v2);

    if (*v == v2) {
        return 0;
    }
581
    error_report("%" PRIx32 " != %" PRIx32, *v, v2);
582 583 584 585 586 587 588 589 590
    return -EINVAL;
}

const VMStateInfo vmstate_info_int32_equal = {
    .name = "int32 equal",
    .get  = get_int32_equal,
    .put  = put_int32,
};

591 592 593
/* 32 bit int. Check that the received value is non-negative
 * and less than or equal to the one in the field.
 */
594

J
Jianjun Duan 已提交
595
static int get_int32_le(QEMUFile *f, void *pv, size_t size, VMStateField *field)
596
{
597 598 599
    int32_t *cur = pv;
    int32_t loaded;
    qemu_get_sbe32s(f, &loaded);
600

601
    if (loaded >= 0 && loaded <= *cur) {
602
        *cur = loaded;
603 604
        return 0;
    }
605 606 607
    error_report("Invalid value %" PRId32
                 " expecting positive value <= %" PRId32,
                 loaded, *cur);
608 609 610 611
    return -EINVAL;
}

const VMStateInfo vmstate_info_int32_le = {
612
    .name = "int32 le",
613 614 615 616 617 618
    .get  = get_int32_le,
    .put  = put_int32,
};

/* 64 bit int */

J
Jianjun Duan 已提交
619
static int get_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
620 621 622 623 624 625
{
    int64_t *v = pv;
    qemu_get_sbe64s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
626 627
static int put_int64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                      QJSON *vmdesc)
628 629 630
{
    int64_t *v = pv;
    qemu_put_sbe64s(f, v);
J
Jianjun Duan 已提交
631
    return 0;
632 633 634 635 636 637 638 639 640 641
}

const VMStateInfo vmstate_info_int64 = {
    .name = "int64",
    .get  = get_int64,
    .put  = put_int64,
};

/* 8 bit unsigned int */

J
Jianjun Duan 已提交
642
static int get_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field)
643 644 645 646 647 648
{
    uint8_t *v = pv;
    qemu_get_8s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
649 650
static int put_uint8(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                     QJSON *vmdesc)
651 652 653
{
    uint8_t *v = pv;
    qemu_put_8s(f, v);
J
Jianjun Duan 已提交
654
    return 0;
655 656 657 658 659 660 661 662 663 664
}

const VMStateInfo vmstate_info_uint8 = {
    .name = "uint8",
    .get  = get_uint8,
    .put  = put_uint8,
};

/* 16 bit unsigned int */

J
Jianjun Duan 已提交
665
static int get_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field)
666 667 668 669 670 671
{
    uint16_t *v = pv;
    qemu_get_be16s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
672 673
static int put_uint16(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                      QJSON *vmdesc)
674 675 676
{
    uint16_t *v = pv;
    qemu_put_be16s(f, v);
J
Jianjun Duan 已提交
677
    return 0;
678 679 680 681 682 683 684 685 686 687
}

const VMStateInfo vmstate_info_uint16 = {
    .name = "uint16",
    .get  = get_uint16,
    .put  = put_uint16,
};

/* 32 bit unsigned int */

J
Jianjun Duan 已提交
688
static int get_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field)
689 690 691 692 693 694
{
    uint32_t *v = pv;
    qemu_get_be32s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
695 696
static int put_uint32(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                      QJSON *vmdesc)
697 698 699
{
    uint32_t *v = pv;
    qemu_put_be32s(f, v);
J
Jianjun Duan 已提交
700
    return 0;
701 702 703 704 705 706 707 708 709 710 711
}

const VMStateInfo vmstate_info_uint32 = {
    .name = "uint32",
    .get  = get_uint32,
    .put  = put_uint32,
};

/* 32 bit uint. See that the received value is the same than the one
   in the field */

J
Jianjun Duan 已提交
712 713
static int get_uint32_equal(QEMUFile *f, void *pv, size_t size,
                            VMStateField *field)
714 715 716 717 718 719 720 721
{
    uint32_t *v = pv;
    uint32_t v2;
    qemu_get_be32s(f, &v2);

    if (*v == v2) {
        return 0;
    }
722
    error_report("%" PRIx32 " != %" PRIx32, *v, v2);
723 724 725 726 727 728 729 730 731 732 733
    return -EINVAL;
}

const VMStateInfo vmstate_info_uint32_equal = {
    .name = "uint32 equal",
    .get  = get_uint32_equal,
    .put  = put_uint32,
};

/* 64 bit unsigned int */

J
Jianjun Duan 已提交
734
static int get_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field)
735 736 737 738 739 740
{
    uint64_t *v = pv;
    qemu_get_be64s(f, v);
    return 0;
}

J
Jianjun Duan 已提交
741 742
static int put_uint64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                      QJSON *vmdesc)
743 744 745
{
    uint64_t *v = pv;
    qemu_put_be64s(f, v);
J
Jianjun Duan 已提交
746
    return 0;
747 748 749 750 751 752 753 754 755 756 757
}

const VMStateInfo vmstate_info_uint64 = {
    .name = "uint64",
    .get  = get_uint64,
    .put  = put_uint64,
};

/* 64 bit unsigned int. See that the received value is the same than the one
   in the field */

J
Jianjun Duan 已提交
758 759
static int get_uint64_equal(QEMUFile *f, void *pv, size_t size,
                            VMStateField *field)
760 761 762 763 764 765 766 767
{
    uint64_t *v = pv;
    uint64_t v2;
    qemu_get_be64s(f, &v2);

    if (*v == v2) {
        return 0;
    }
768
    error_report("%" PRIx64 " != %" PRIx64, *v, v2);
769 770 771 772 773 774 775 776 777 778 779 780
    return -EINVAL;
}

const VMStateInfo vmstate_info_uint64_equal = {
    .name = "int64 equal",
    .get  = get_uint64_equal,
    .put  = put_uint64,
};

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

J
Jianjun Duan 已提交
781 782
static int get_uint8_equal(QEMUFile *f, void *pv, size_t size,
                           VMStateField *field)
783 784 785 786 787 788 789 790
{
    uint8_t *v = pv;
    uint8_t v2;
    qemu_get_8s(f, &v2);

    if (*v == v2) {
        return 0;
    }
791
    error_report("%x != %x", *v, v2);
792 793 794 795 796 797 798 799 800 801 802 803
    return -EINVAL;
}

const VMStateInfo vmstate_info_uint8_equal = {
    .name = "uint8 equal",
    .get  = get_uint8_equal,
    .put  = put_uint8,
};

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

J
Jianjun Duan 已提交
804 805
static int get_uint16_equal(QEMUFile *f, void *pv, size_t size,
                            VMStateField *field)
806 807 808 809 810 811 812 813
{
    uint16_t *v = pv;
    uint16_t v2;
    qemu_get_be16s(f, &v2);

    if (*v == v2) {
        return 0;
    }
814
    error_report("%x != %x", *v, v2);
815 816 817 818 819 820 821 822 823 824 825
    return -EINVAL;
}

const VMStateInfo vmstate_info_uint16_equal = {
    .name = "uint16 equal",
    .get  = get_uint16_equal,
    .put  = put_uint16,
};

/* floating point */

J
Jianjun Duan 已提交
826 827
static int get_float64(QEMUFile *f, void *pv, size_t size,
                       VMStateField *field)
828 829 830 831 832 833 834
{
    float64 *v = pv;

    *v = make_float64(qemu_get_be64(f));
    return 0;
}

J
Jianjun Duan 已提交
835 836
static int put_float64(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                       QJSON *vmdesc)
837 838 839 840
{
    uint64_t *v = pv;

    qemu_put_be64(f, float64_val(*v));
J
Jianjun Duan 已提交
841
    return 0;
842 843 844 845 846 847 848 849
}

const VMStateInfo vmstate_info_float64 = {
    .name = "float64",
    .get  = get_float64,
    .put  = put_float64,
};

850 851
/* CPU_DoubleU type */

J
Jianjun Duan 已提交
852 853
static int get_cpudouble(QEMUFile *f, void *pv, size_t size,
                         VMStateField *field)
854 855 856 857 858 859 860
{
    CPU_DoubleU *v = pv;
    qemu_get_be32s(f, &v->l.upper);
    qemu_get_be32s(f, &v->l.lower);
    return 0;
}

J
Jianjun Duan 已提交
861 862
static int put_cpudouble(QEMUFile *f, void *pv, size_t size,
                         VMStateField *field, QJSON *vmdesc)
863 864 865 866
{
    CPU_DoubleU *v = pv;
    qemu_put_be32s(f, &v->l.upper);
    qemu_put_be32s(f, &v->l.lower);
J
Jianjun Duan 已提交
867
    return 0;
868 869 870 871 872 873 874 875
}

const VMStateInfo vmstate_info_cpudouble = {
    .name = "CPU_Double_U",
    .get  = get_cpudouble,
    .put  = put_cpudouble,
};

876 877
/* uint8_t buffers */

J
Jianjun Duan 已提交
878 879
static int get_buffer(QEMUFile *f, void *pv, size_t size,
                      VMStateField *field)
880 881 882 883 884 885
{
    uint8_t *v = pv;
    qemu_get_buffer(f, v, size);
    return 0;
}

J
Jianjun Duan 已提交
886 887
static int put_buffer(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                      QJSON *vmdesc)
888 889 890
{
    uint8_t *v = pv;
    qemu_put_buffer(f, v, size);
J
Jianjun Duan 已提交
891
    return 0;
892 893 894 895 896 897 898 899 900 901 902
}

const VMStateInfo vmstate_info_buffer = {
    .name = "buffer",
    .get  = get_buffer,
    .put  = put_buffer,
};

/* unused buffers: space that was used for some fields that are
   not useful anymore */

J
Jianjun Duan 已提交
903 904
static int get_unused_buffer(QEMUFile *f, void *pv, size_t size,
                             VMStateField *field)
905 906 907 908 909 910 911 912 913 914 915 916
{
    uint8_t buf[1024];
    int block_len;

    while (size > 0) {
        block_len = MIN(sizeof(buf), size);
        size -= block_len;
        qemu_get_buffer(f, buf, block_len);
    }
   return 0;
}

J
Jianjun Duan 已提交
917 918
static int put_unused_buffer(QEMUFile *f, void *pv, size_t size,
                             VMStateField *field, QJSON *vmdesc)
919 920 921 922 923 924 925 926 927
{
    static const uint8_t buf[1024];
    int block_len;

    while (size > 0) {
        block_len = MIN(sizeof(buf), size);
        size -= block_len;
        qemu_put_buffer(f, buf, block_len);
    }
J
Jianjun Duan 已提交
928 929

    return 0;
930 931 932 933 934 935 936 937
}

const VMStateInfo vmstate_info_unused_buffer = {
    .name = "unused_buffer",
    .get  = get_unused_buffer,
    .put  = put_unused_buffer,
};

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977
/* vmstate_info_tmp, see VMSTATE_WITH_TMP, the idea is that we allocate
 * a temporary buffer and the pre_load/pre_save methods in the child vmsd
 * copy stuff from the parent into the child and do calculations to fill
 * in fields that don't really exist in the parent but need to be in the
 * stream.
 */
static int get_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field)
{
    int ret;
    const VMStateDescription *vmsd = field->vmsd;
    int version_id = field->version_id;
    void *tmp = g_malloc(size);

    /* Writes the parent field which is at the start of the tmp */
    *(void **)tmp = pv;
    ret = vmstate_load_state(f, vmsd, tmp, version_id);
    g_free(tmp);
    return ret;
}

static int put_tmp(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                    QJSON *vmdesc)
{
    const VMStateDescription *vmsd = field->vmsd;
    void *tmp = g_malloc(size);

    /* Writes the parent field which is at the start of the tmp */
    *(void **)tmp = pv;
    vmstate_save_state(f, vmsd, tmp, vmdesc);
    g_free(tmp);

    return 0;
}

const VMStateInfo vmstate_info_tmp = {
    .name = "tmp",
    .get = get_tmp,
    .put = put_tmp,
};

978 979 980 981 982 983 984
/* bitmaps (as defined by bitmap.h). Note that size here is the size
 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
 * bit words with the bits in big endian order. The in-memory format
 * is an array of 'unsigned long', which may be either 32 or 64 bits.
 */
/* This is the number of 64 bit words sent over the wire */
#define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
J
Jianjun Duan 已提交
985
static int get_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field)
986 987 988 989 990 991 992 993 994 995 996 997 998
{
    unsigned long *bmp = pv;
    int i, idx = 0;
    for (i = 0; i < BITS_TO_U64S(size); i++) {
        uint64_t w = qemu_get_be64(f);
        bmp[idx++] = w;
        if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
            bmp[idx++] = w >> 32;
        }
    }
    return 0;
}

J
Jianjun Duan 已提交
999 1000
static int put_bitmap(QEMUFile *f, void *pv, size_t size, VMStateField *field,
                      QJSON *vmdesc)
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
{
    unsigned long *bmp = pv;
    int i, idx = 0;
    for (i = 0; i < BITS_TO_U64S(size); i++) {
        uint64_t w = bmp[idx++];
        if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
            w |= ((uint64_t)bmp[idx++]) << 32;
        }
        qemu_put_be64(f, w);
    }
J
Jianjun Duan 已提交
1011 1012

    return 0;
1013 1014 1015 1016 1017 1018 1019
}

const VMStateInfo vmstate_info_bitmap = {
    .name = "bitmap",
    .get = get_bitmap,
    .put = put_bitmap,
};
J
Jianjun Duan 已提交
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087

/* get for QTAILQ
 * meta data about the QTAILQ is encoded in a VMStateField structure
 */
static int get_qtailq(QEMUFile *f, void *pv, size_t unused_size,
                      VMStateField *field)
{
    int ret = 0;
    const VMStateDescription *vmsd = field->vmsd;
    /* size of a QTAILQ element */
    size_t size = field->size;
    /* offset of the QTAILQ entry in a QTAILQ element */
    size_t entry_offset = field->start;
    int version_id = field->version_id;
    void *elm;

    trace_get_qtailq(vmsd->name, version_id);
    if (version_id > vmsd->version_id) {
        error_report("%s %s",  vmsd->name, "too new");
        trace_get_qtailq_end(vmsd->name, "too new", -EINVAL);

        return -EINVAL;
    }
    if (version_id < vmsd->minimum_version_id) {
        error_report("%s %s",  vmsd->name, "too old");
        trace_get_qtailq_end(vmsd->name, "too old", -EINVAL);
        return -EINVAL;
    }

    while (qemu_get_byte(f)) {
        elm = g_malloc(size);
        ret = vmstate_load_state(f, vmsd, elm, version_id);
        if (ret) {
            return ret;
        }
        QTAILQ_RAW_INSERT_TAIL(pv, elm, entry_offset);
    }

    trace_get_qtailq_end(vmsd->name, "end", ret);
    return ret;
}

/* put for QTAILQ */
static int put_qtailq(QEMUFile *f, void *pv, size_t unused_size,
                      VMStateField *field, QJSON *vmdesc)
{
    const VMStateDescription *vmsd = field->vmsd;
    /* offset of the QTAILQ entry in a QTAILQ element*/
    size_t entry_offset = field->start;
    void *elm;

    trace_put_qtailq(vmsd->name, vmsd->version_id);

    QTAILQ_RAW_FOREACH(elm, pv, entry_offset) {
        qemu_put_byte(f, true);
        vmstate_save_state(f, vmsd, elm, vmdesc);
    }
    qemu_put_byte(f, false);

    trace_put_qtailq_end(vmsd->name, "end");

    return 0;
}
const VMStateInfo vmstate_info_qtailq = {
    .name = "qtailq",
    .get  = get_qtailq,
    .put  = put_qtailq,
};