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"
7 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
9 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
12 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
13 void *opaque, int version_id)
15 VMStateField *field = vmsd->fields;
18 if (version_id > vmsd->version_id) {
21 if (version_id < vmsd->minimum_version_id_old) {
24 if (version_id < vmsd->minimum_version_id) {
25 return vmsd->load_state_old(f, opaque, version_id);
28 int ret = vmsd->pre_load(opaque);
34 if ((field->field_exists &&
35 field->field_exists(opaque, version_id)) ||
36 (!field->field_exists &&
37 field->version_id <= version_id)) {
38 void *base_addr = opaque + field->offset;
40 int size = field->size;
42 if (field->flags & VMS_VBUFFER) {
43 size = *(int32_t *)(opaque+field->size_offset);
44 if (field->flags & VMS_MULTIPLY) {
48 if (field->flags & VMS_ARRAY) {
50 } else if (field->flags & VMS_VARRAY_INT32) {
51 n_elems = *(int32_t *)(opaque+field->num_offset);
52 } else if (field->flags & VMS_VARRAY_UINT32) {
53 n_elems = *(uint32_t *)(opaque+field->num_offset);
54 } else if (field->flags & VMS_VARRAY_UINT16) {
55 n_elems = *(uint16_t *)(opaque+field->num_offset);
56 } else if (field->flags & VMS_VARRAY_UINT8) {
57 n_elems = *(uint8_t *)(opaque+field->num_offset);
59 if (field->flags & VMS_POINTER) {
60 base_addr = *(void **)base_addr + field->start;
62 for (i = 0; i < n_elems; i++) {
63 void *addr = base_addr + size * i;
65 if (field->flags & VMS_ARRAY_OF_POINTER) {
66 addr = *(void **)addr;
68 if (field->flags & VMS_STRUCT) {
69 ret = vmstate_load_state(f, field->vmsd, addr,
70 field->vmsd->version_id);
72 ret = field->info->get(f, addr, size);
82 ret = vmstate_subsection_load(f, vmsd, opaque);
86 if (vmsd->post_load) {
87 return vmsd->post_load(opaque, version_id);
92 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
95 VMStateField *field = vmsd->fields;
98 vmsd->pre_save(opaque);
100 while (field->name) {
101 if (!field->field_exists ||
102 field->field_exists(opaque, vmsd->version_id)) {
103 void *base_addr = opaque + field->offset;
105 int size = field->size;
107 if (field->flags & VMS_VBUFFER) {
108 size = *(int32_t *)(opaque+field->size_offset);
109 if (field->flags & VMS_MULTIPLY) {
113 if (field->flags & VMS_ARRAY) {
114 n_elems = field->num;
115 } else if (field->flags & VMS_VARRAY_INT32) {
116 n_elems = *(int32_t *)(opaque+field->num_offset);
117 } else if (field->flags & VMS_VARRAY_UINT32) {
118 n_elems = *(uint32_t *)(opaque+field->num_offset);
119 } else if (field->flags & VMS_VARRAY_UINT16) {
120 n_elems = *(uint16_t *)(opaque+field->num_offset);
121 } else if (field->flags & VMS_VARRAY_UINT8) {
122 n_elems = *(uint8_t *)(opaque+field->num_offset);
124 if (field->flags & VMS_POINTER) {
125 base_addr = *(void **)base_addr + field->start;
127 for (i = 0; i < n_elems; i++) {
128 void *addr = base_addr + size * i;
130 if (field->flags & VMS_ARRAY_OF_POINTER) {
131 addr = *(void **)addr;
133 if (field->flags & VMS_STRUCT) {
134 vmstate_save_state(f, field->vmsd, addr);
136 field->info->put(f, addr, size);
142 vmstate_subsection_save(f, vmsd, opaque);
145 static const VMStateDescription *
146 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
148 while (sub && sub->needed) {
149 if (strcmp(idstr, sub->vmsd->name) == 0) {
157 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
160 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
163 uint8_t version_id, len, size;
164 const VMStateDescription *sub_vmsd;
166 len = qemu_peek_byte(f, 1);
167 if (len < strlen(vmsd->name) + 1) {
168 /* subsection name has be be "section_name/a" */
171 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
177 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
178 /* it don't have a valid subsection name */
181 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
182 if (sub_vmsd == NULL) {
185 qemu_file_skip(f, 1); /* subsection */
186 qemu_file_skip(f, 1); /* len */
187 qemu_file_skip(f, len); /* idstr */
188 version_id = qemu_get_be32(f);
190 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
198 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
201 const VMStateSubsection *sub = vmsd->subsections;
203 while (sub && sub->needed) {
204 if (sub->needed(opaque)) {
205 const VMStateDescription *vmsd = sub->vmsd;
208 qemu_put_byte(f, QEMU_VM_SUBSECTION);
209 len = strlen(vmsd->name);
210 qemu_put_byte(f, len);
211 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
212 qemu_put_be32(f, vmsd->version_id);
213 vmstate_save_state(f, vmsd, opaque);
221 static int get_bool(QEMUFile *f, void *pv, size_t size)
224 *v = qemu_get_byte(f);
228 static void put_bool(QEMUFile *f, void *pv, size_t size)
231 qemu_put_byte(f, *v);
234 const VMStateInfo vmstate_info_bool = {
242 static int get_int8(QEMUFile *f, void *pv, size_t size)
249 static void put_int8(QEMUFile *f, void *pv, size_t size)
255 const VMStateInfo vmstate_info_int8 = {
263 static int get_int16(QEMUFile *f, void *pv, size_t size)
266 qemu_get_sbe16s(f, v);
270 static void put_int16(QEMUFile *f, void *pv, size_t size)
273 qemu_put_sbe16s(f, v);
276 const VMStateInfo vmstate_info_int16 = {
284 static int get_int32(QEMUFile *f, void *pv, size_t size)
287 qemu_get_sbe32s(f, v);
291 static void put_int32(QEMUFile *f, void *pv, size_t size)
294 qemu_put_sbe32s(f, v);
297 const VMStateInfo vmstate_info_int32 = {
303 /* 32 bit int. See that the received value is the same than the one
306 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
310 qemu_get_sbe32s(f, &v2);
318 const VMStateInfo vmstate_info_int32_equal = {
319 .name = "int32 equal",
320 .get = get_int32_equal,
324 /* 32 bit int. Check that the received value is less than or equal to
325 the one in the field */
327 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
331 qemu_get_sbe32s(f, &loaded);
333 if (loaded <= *cur) {
340 const VMStateInfo vmstate_info_int32_le = {
348 static int get_int64(QEMUFile *f, void *pv, size_t size)
351 qemu_get_sbe64s(f, v);
355 static void put_int64(QEMUFile *f, void *pv, size_t size)
358 qemu_put_sbe64s(f, v);
361 const VMStateInfo vmstate_info_int64 = {
367 /* 8 bit unsigned int */
369 static int get_uint8(QEMUFile *f, void *pv, size_t size)
376 static void put_uint8(QEMUFile *f, void *pv, size_t size)
382 const VMStateInfo vmstate_info_uint8 = {
388 /* 16 bit unsigned int */
390 static int get_uint16(QEMUFile *f, void *pv, size_t size)
393 qemu_get_be16s(f, v);
397 static void put_uint16(QEMUFile *f, void *pv, size_t size)
400 qemu_put_be16s(f, v);
403 const VMStateInfo vmstate_info_uint16 = {
409 /* 32 bit unsigned int */
411 static int get_uint32(QEMUFile *f, void *pv, size_t size)
414 qemu_get_be32s(f, v);
418 static void put_uint32(QEMUFile *f, void *pv, size_t size)
421 qemu_put_be32s(f, v);
424 const VMStateInfo vmstate_info_uint32 = {
430 /* 32 bit uint. See that the received value is the same than the one
433 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
437 qemu_get_be32s(f, &v2);
445 const VMStateInfo vmstate_info_uint32_equal = {
446 .name = "uint32 equal",
447 .get = get_uint32_equal,
451 /* 64 bit unsigned int */
453 static int get_uint64(QEMUFile *f, void *pv, size_t size)
456 qemu_get_be64s(f, v);
460 static void put_uint64(QEMUFile *f, void *pv, size_t size)
463 qemu_put_be64s(f, v);
466 const VMStateInfo vmstate_info_uint64 = {
472 /* 64 bit unsigned int. See that the received value is the same than the one
475 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
479 qemu_get_be64s(f, &v2);
487 const VMStateInfo vmstate_info_uint64_equal = {
488 .name = "int64 equal",
489 .get = get_uint64_equal,
493 /* 8 bit int. See that the received value is the same than the one
496 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
508 const VMStateInfo vmstate_info_uint8_equal = {
509 .name = "uint8 equal",
510 .get = get_uint8_equal,
514 /* 16 bit unsigned int int. See that the received value is the same than the one
517 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
521 qemu_get_be16s(f, &v2);
529 const VMStateInfo vmstate_info_uint16_equal = {
530 .name = "uint16 equal",
531 .get = get_uint16_equal,
537 static int get_float64(QEMUFile *f, void *pv, size_t size)
541 *v = make_float64(qemu_get_be64(f));
545 static void put_float64(QEMUFile *f, void *pv, size_t size)
549 qemu_put_be64(f, float64_val(*v));
552 const VMStateInfo vmstate_info_float64 = {
558 /* uint8_t buffers */
560 static int get_buffer(QEMUFile *f, void *pv, size_t size)
563 qemu_get_buffer(f, v, size);
567 static void put_buffer(QEMUFile *f, void *pv, size_t size)
570 qemu_put_buffer(f, v, size);
573 const VMStateInfo vmstate_info_buffer = {
579 /* unused buffers: space that was used for some fields that are
580 not useful anymore */
582 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
588 block_len = MIN(sizeof(buf), size);
590 qemu_get_buffer(f, buf, block_len);
595 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
597 static const uint8_t buf[1024];
601 block_len = MIN(sizeof(buf), size);
603 qemu_put_buffer(f, buf, block_len);
607 const VMStateInfo vmstate_info_unused_buffer = {
608 .name = "unused_buffer",
609 .get = get_unused_buffer,
610 .put = put_unused_buffer,
613 /* bitmaps (as defined by bitmap.h). Note that size here is the size
614 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
615 * bit words with the bits in big endian order. The in-memory format
616 * is an array of 'unsigned long', which may be either 32 or 64 bits.
618 /* This is the number of 64 bit words sent over the wire */
619 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
620 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
622 unsigned long *bmp = pv;
624 for (i = 0; i < BITS_TO_U64S(size); i++) {
625 uint64_t w = qemu_get_be64(f);
627 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
628 bmp[idx++] = w >> 32;
634 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
636 unsigned long *bmp = pv;
638 for (i = 0; i < BITS_TO_U64S(size); i++) {
639 uint64_t w = bmp[idx++];
640 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
641 w |= ((uint64_t)bmp[idx++]) << 32;
647 const VMStateInfo vmstate_info_bitmap = {