1 #include "qemu-common.h"
2 #include "migration/migration.h"
3 #include "migration/qemu-file.h"
4 #include "migration/vmstate.h"
5 #include "qemu/bitops.h"
8 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
10 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
13 static int vmstate_n_elems(void *opaque, VMStateField *field)
17 if (field->flags & VMS_ARRAY) {
19 } else if (field->flags & VMS_VARRAY_INT32) {
20 n_elems = *(int32_t *)(opaque+field->num_offset);
21 } else if (field->flags & VMS_VARRAY_UINT32) {
22 n_elems = *(uint32_t *)(opaque+field->num_offset);
23 } else if (field->flags & VMS_VARRAY_UINT16) {
24 n_elems = *(uint16_t *)(opaque+field->num_offset);
25 } else if (field->flags & VMS_VARRAY_UINT8) {
26 n_elems = *(uint8_t *)(opaque+field->num_offset);
32 static int vmstate_size(void *opaque, VMStateField *field)
34 int size = field->size;
36 if (field->flags & VMS_VBUFFER) {
37 size = *(int32_t *)(opaque+field->size_offset);
38 if (field->flags & VMS_MULTIPLY) {
46 static void *vmstate_base_addr(void *opaque, VMStateField *field)
48 void *base_addr = opaque + field->offset;
50 if (field->flags & VMS_POINTER) {
51 base_addr = *(void **)base_addr + field->start;
57 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
58 void *opaque, int version_id)
60 VMStateField *field = vmsd->fields;
63 if (version_id > vmsd->version_id) {
66 if (version_id < vmsd->minimum_version_id) {
67 if (vmsd->load_state_old &&
68 version_id >= vmsd->minimum_version_id_old) {
69 return vmsd->load_state_old(f, opaque, version_id);
74 int ret = vmsd->pre_load(opaque);
80 if ((field->field_exists &&
81 field->field_exists(opaque, version_id)) ||
82 (!field->field_exists &&
83 field->version_id <= version_id)) {
84 void *base_addr = vmstate_base_addr(opaque, field);
85 int i, n_elems = vmstate_n_elems(opaque, field);
86 int size = vmstate_size(opaque, field);
88 for (i = 0; i < n_elems; i++) {
89 void *addr = base_addr + size * i;
91 if (field->flags & VMS_ARRAY_OF_POINTER) {
92 addr = *(void **)addr;
94 if (field->flags & VMS_STRUCT) {
95 ret = vmstate_load_state(f, field->vmsd, addr,
96 field->vmsd->version_id);
98 ret = field->info->get(f, addr, size);
102 trace_vmstate_load_field_error(field->name, ret);
106 } else if (field->flags & VMS_MUST_EXIST) {
107 fprintf(stderr, "Input validation failed: %s/%s\n",
108 vmsd->name, field->name);
113 ret = vmstate_subsection_load(f, vmsd, opaque);
117 if (vmsd->post_load) {
118 return vmsd->post_load(opaque, version_id);
123 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
126 VMStateField *field = vmsd->fields;
128 if (vmsd->pre_save) {
129 vmsd->pre_save(opaque);
131 while (field->name) {
132 if (!field->field_exists ||
133 field->field_exists(opaque, vmsd->version_id)) {
134 void *base_addr = vmstate_base_addr(opaque, field);
135 int i, n_elems = vmstate_n_elems(opaque, field);
136 int size = vmstate_size(opaque, field);
138 for (i = 0; i < n_elems; i++) {
139 void *addr = base_addr + size * i;
141 if (field->flags & VMS_ARRAY_OF_POINTER) {
142 addr = *(void **)addr;
144 if (field->flags & VMS_STRUCT) {
145 vmstate_save_state(f, field->vmsd, addr);
147 field->info->put(f, addr, size);
151 if (field->flags & VMS_MUST_EXIST) {
152 fprintf(stderr, "Output state validation failed: %s/%s\n",
153 vmsd->name, field->name);
154 assert(!(field->flags & VMS_MUST_EXIST));
159 vmstate_subsection_save(f, vmsd, opaque);
162 static const VMStateDescription *
163 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
165 while (sub && sub->needed) {
166 if (strcmp(idstr, sub->vmsd->name) == 0) {
174 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
177 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
180 uint8_t version_id, len, size;
181 const VMStateDescription *sub_vmsd;
183 len = qemu_peek_byte(f, 1);
184 if (len < strlen(vmsd->name) + 1) {
185 /* subsection name has be be "section_name/a" */
188 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
194 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
195 /* it don't have a valid subsection name */
198 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
199 if (sub_vmsd == NULL) {
202 qemu_file_skip(f, 1); /* subsection */
203 qemu_file_skip(f, 1); /* len */
204 qemu_file_skip(f, len); /* idstr */
205 version_id = qemu_get_be32(f);
207 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
215 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
218 const VMStateSubsection *sub = vmsd->subsections;
220 while (sub && sub->needed) {
221 if (sub->needed(opaque)) {
222 const VMStateDescription *vmsd = sub->vmsd;
225 qemu_put_byte(f, QEMU_VM_SUBSECTION);
226 len = strlen(vmsd->name);
227 qemu_put_byte(f, len);
228 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
229 qemu_put_be32(f, vmsd->version_id);
230 vmstate_save_state(f, vmsd, opaque);
238 static int get_bool(QEMUFile *f, void *pv, size_t size)
241 *v = qemu_get_byte(f);
245 static void put_bool(QEMUFile *f, void *pv, size_t size)
248 qemu_put_byte(f, *v);
251 const VMStateInfo vmstate_info_bool = {
259 static int get_int8(QEMUFile *f, void *pv, size_t size)
266 static void put_int8(QEMUFile *f, void *pv, size_t size)
272 const VMStateInfo vmstate_info_int8 = {
280 static int get_int16(QEMUFile *f, void *pv, size_t size)
283 qemu_get_sbe16s(f, v);
287 static void put_int16(QEMUFile *f, void *pv, size_t size)
290 qemu_put_sbe16s(f, v);
293 const VMStateInfo vmstate_info_int16 = {
301 static int get_int32(QEMUFile *f, void *pv, size_t size)
304 qemu_get_sbe32s(f, v);
308 static void put_int32(QEMUFile *f, void *pv, size_t size)
311 qemu_put_sbe32s(f, v);
314 const VMStateInfo vmstate_info_int32 = {
320 /* 32 bit int. See that the received value is the same than the one
323 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
327 qemu_get_sbe32s(f, &v2);
335 const VMStateInfo vmstate_info_int32_equal = {
336 .name = "int32 equal",
337 .get = get_int32_equal,
341 /* 32 bit int. Check that the received value is non-negative
342 * and less than or equal to the one in the field.
345 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
349 qemu_get_sbe32s(f, &loaded);
351 if (loaded >= 0 && loaded <= *cur) {
358 const VMStateInfo vmstate_info_int32_le = {
366 static int get_int64(QEMUFile *f, void *pv, size_t size)
369 qemu_get_sbe64s(f, v);
373 static void put_int64(QEMUFile *f, void *pv, size_t size)
376 qemu_put_sbe64s(f, v);
379 const VMStateInfo vmstate_info_int64 = {
385 /* 8 bit unsigned int */
387 static int get_uint8(QEMUFile *f, void *pv, size_t size)
394 static void put_uint8(QEMUFile *f, void *pv, size_t size)
400 const VMStateInfo vmstate_info_uint8 = {
406 /* 16 bit unsigned int */
408 static int get_uint16(QEMUFile *f, void *pv, size_t size)
411 qemu_get_be16s(f, v);
415 static void put_uint16(QEMUFile *f, void *pv, size_t size)
418 qemu_put_be16s(f, v);
421 const VMStateInfo vmstate_info_uint16 = {
427 /* 32 bit unsigned int */
429 static int get_uint32(QEMUFile *f, void *pv, size_t size)
432 qemu_get_be32s(f, v);
436 static void put_uint32(QEMUFile *f, void *pv, size_t size)
439 qemu_put_be32s(f, v);
442 const VMStateInfo vmstate_info_uint32 = {
448 /* 32 bit uint. See that the received value is the same than the one
451 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
455 qemu_get_be32s(f, &v2);
463 const VMStateInfo vmstate_info_uint32_equal = {
464 .name = "uint32 equal",
465 .get = get_uint32_equal,
469 /* 64 bit unsigned int */
471 static int get_uint64(QEMUFile *f, void *pv, size_t size)
474 qemu_get_be64s(f, v);
478 static void put_uint64(QEMUFile *f, void *pv, size_t size)
481 qemu_put_be64s(f, v);
484 const VMStateInfo vmstate_info_uint64 = {
490 /* 64 bit unsigned int. See that the received value is the same than the one
493 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
497 qemu_get_be64s(f, &v2);
505 const VMStateInfo vmstate_info_uint64_equal = {
506 .name = "int64 equal",
507 .get = get_uint64_equal,
511 /* 8 bit int. See that the received value is the same than the one
514 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
526 const VMStateInfo vmstate_info_uint8_equal = {
527 .name = "uint8 equal",
528 .get = get_uint8_equal,
532 /* 16 bit unsigned int int. See that the received value is the same than the one
535 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
539 qemu_get_be16s(f, &v2);
547 const VMStateInfo vmstate_info_uint16_equal = {
548 .name = "uint16 equal",
549 .get = get_uint16_equal,
555 static int get_float64(QEMUFile *f, void *pv, size_t size)
559 *v = make_float64(qemu_get_be64(f));
563 static void put_float64(QEMUFile *f, void *pv, size_t size)
567 qemu_put_be64(f, float64_val(*v));
570 const VMStateInfo vmstate_info_float64 = {
576 /* uint8_t buffers */
578 static int get_buffer(QEMUFile *f, void *pv, size_t size)
581 qemu_get_buffer(f, v, size);
585 static void put_buffer(QEMUFile *f, void *pv, size_t size)
588 qemu_put_buffer(f, v, size);
591 const VMStateInfo vmstate_info_buffer = {
597 /* unused buffers: space that was used for some fields that are
598 not useful anymore */
600 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
606 block_len = MIN(sizeof(buf), size);
608 qemu_get_buffer(f, buf, block_len);
613 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
615 static const uint8_t buf[1024];
619 block_len = MIN(sizeof(buf), size);
621 qemu_put_buffer(f, buf, block_len);
625 const VMStateInfo vmstate_info_unused_buffer = {
626 .name = "unused_buffer",
627 .get = get_unused_buffer,
628 .put = put_unused_buffer,
631 /* bitmaps (as defined by bitmap.h). Note that size here is the size
632 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
633 * bit words with the bits in big endian order. The in-memory format
634 * is an array of 'unsigned long', which may be either 32 or 64 bits.
636 /* This is the number of 64 bit words sent over the wire */
637 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
638 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
640 unsigned long *bmp = pv;
642 for (i = 0; i < BITS_TO_U64S(size); i++) {
643 uint64_t w = qemu_get_be64(f);
645 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
646 bmp[idx++] = w >> 32;
652 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
654 unsigned long *bmp = pv;
656 for (i = 0; i < BITS_TO_U64S(size); i++) {
657 uint64_t w = bmp[idx++];
658 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
659 w |= ((uint64_t)bmp[idx++]) << 32;
665 const VMStateInfo vmstate_info_bitmap = {