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 int vmstate_load_state(QEMUFile *f, const VMStateDescription *vmsd,
14 void *opaque, int version_id)
16 VMStateField *field = vmsd->fields;
19 if (version_id > vmsd->version_id) {
22 if (version_id < vmsd->minimum_version_id_old) {
25 if (version_id < vmsd->minimum_version_id) {
26 return vmsd->load_state_old(f, opaque, version_id);
29 int ret = vmsd->pre_load(opaque);
35 if ((field->field_exists &&
36 field->field_exists(opaque, version_id)) ||
37 (!field->field_exists &&
38 field->version_id <= version_id)) {
39 void *base_addr = opaque + field->offset;
41 int size = field->size;
43 if (field->flags & VMS_VBUFFER) {
44 size = *(int32_t *)(opaque+field->size_offset);
45 if (field->flags & VMS_MULTIPLY) {
49 if (field->flags & VMS_ARRAY) {
51 } else if (field->flags & VMS_VARRAY_INT32) {
52 n_elems = *(int32_t *)(opaque+field->num_offset);
53 } else if (field->flags & VMS_VARRAY_UINT32) {
54 n_elems = *(uint32_t *)(opaque+field->num_offset);
55 } else if (field->flags & VMS_VARRAY_UINT16) {
56 n_elems = *(uint16_t *)(opaque+field->num_offset);
57 } else if (field->flags & VMS_VARRAY_UINT8) {
58 n_elems = *(uint8_t *)(opaque+field->num_offset);
60 if (field->flags & VMS_POINTER) {
61 base_addr = *(void **)base_addr + field->start;
63 for (i = 0; i < n_elems; i++) {
64 void *addr = base_addr + size * i;
66 if (field->flags & VMS_ARRAY_OF_POINTER) {
67 addr = *(void **)addr;
69 if (field->flags & VMS_STRUCT) {
70 ret = vmstate_load_state(f, field->vmsd, addr,
71 field->vmsd->version_id);
73 ret = field->info->get(f, addr, size);
77 trace_vmstate_load_field_error(field->name, ret);
84 ret = vmstate_subsection_load(f, vmsd, opaque);
88 if (vmsd->post_load) {
89 return vmsd->post_load(opaque, version_id);
94 void vmstate_save_state(QEMUFile *f, const VMStateDescription *vmsd,
97 VMStateField *field = vmsd->fields;
100 vmsd->pre_save(opaque);
102 while (field->name) {
103 if (!field->field_exists ||
104 field->field_exists(opaque, vmsd->version_id)) {
105 void *base_addr = opaque + field->offset;
107 int size = field->size;
109 if (field->flags & VMS_VBUFFER) {
110 size = *(int32_t *)(opaque+field->size_offset);
111 if (field->flags & VMS_MULTIPLY) {
115 if (field->flags & VMS_ARRAY) {
116 n_elems = field->num;
117 } else if (field->flags & VMS_VARRAY_INT32) {
118 n_elems = *(int32_t *)(opaque+field->num_offset);
119 } else if (field->flags & VMS_VARRAY_UINT32) {
120 n_elems = *(uint32_t *)(opaque+field->num_offset);
121 } else if (field->flags & VMS_VARRAY_UINT16) {
122 n_elems = *(uint16_t *)(opaque+field->num_offset);
123 } else if (field->flags & VMS_VARRAY_UINT8) {
124 n_elems = *(uint8_t *)(opaque+field->num_offset);
126 if (field->flags & VMS_POINTER) {
127 base_addr = *(void **)base_addr + field->start;
129 for (i = 0; i < n_elems; i++) {
130 void *addr = base_addr + size * i;
132 if (field->flags & VMS_ARRAY_OF_POINTER) {
133 addr = *(void **)addr;
135 if (field->flags & VMS_STRUCT) {
136 vmstate_save_state(f, field->vmsd, addr);
138 field->info->put(f, addr, size);
144 vmstate_subsection_save(f, vmsd, opaque);
147 static const VMStateDescription *
148 vmstate_get_subsection(const VMStateSubsection *sub, char *idstr)
150 while (sub && sub->needed) {
151 if (strcmp(idstr, sub->vmsd->name) == 0) {
159 static int vmstate_subsection_load(QEMUFile *f, const VMStateDescription *vmsd,
162 while (qemu_peek_byte(f, 0) == QEMU_VM_SUBSECTION) {
165 uint8_t version_id, len, size;
166 const VMStateDescription *sub_vmsd;
168 len = qemu_peek_byte(f, 1);
169 if (len < strlen(vmsd->name) + 1) {
170 /* subsection name has be be "section_name/a" */
173 size = qemu_peek_buffer(f, (uint8_t *)idstr, len, 2);
179 if (strncmp(vmsd->name, idstr, strlen(vmsd->name)) != 0) {
180 /* it don't have a valid subsection name */
183 sub_vmsd = vmstate_get_subsection(vmsd->subsections, idstr);
184 if (sub_vmsd == NULL) {
187 qemu_file_skip(f, 1); /* subsection */
188 qemu_file_skip(f, 1); /* len */
189 qemu_file_skip(f, len); /* idstr */
190 version_id = qemu_get_be32(f);
192 ret = vmstate_load_state(f, sub_vmsd, opaque, version_id);
200 static void vmstate_subsection_save(QEMUFile *f, const VMStateDescription *vmsd,
203 const VMStateSubsection *sub = vmsd->subsections;
205 while (sub && sub->needed) {
206 if (sub->needed(opaque)) {
207 const VMStateDescription *vmsd = sub->vmsd;
210 qemu_put_byte(f, QEMU_VM_SUBSECTION);
211 len = strlen(vmsd->name);
212 qemu_put_byte(f, len);
213 qemu_put_buffer(f, (uint8_t *)vmsd->name, len);
214 qemu_put_be32(f, vmsd->version_id);
215 vmstate_save_state(f, vmsd, opaque);
223 static int get_bool(QEMUFile *f, void *pv, size_t size)
226 *v = qemu_get_byte(f);
230 static void put_bool(QEMUFile *f, void *pv, size_t size)
233 qemu_put_byte(f, *v);
236 const VMStateInfo vmstate_info_bool = {
244 static int get_int8(QEMUFile *f, void *pv, size_t size)
251 static void put_int8(QEMUFile *f, void *pv, size_t size)
257 const VMStateInfo vmstate_info_int8 = {
265 static int get_int16(QEMUFile *f, void *pv, size_t size)
268 qemu_get_sbe16s(f, v);
272 static void put_int16(QEMUFile *f, void *pv, size_t size)
275 qemu_put_sbe16s(f, v);
278 const VMStateInfo vmstate_info_int16 = {
286 static int get_int32(QEMUFile *f, void *pv, size_t size)
289 qemu_get_sbe32s(f, v);
293 static void put_int32(QEMUFile *f, void *pv, size_t size)
296 qemu_put_sbe32s(f, v);
299 const VMStateInfo vmstate_info_int32 = {
305 /* 32 bit int. See that the received value is the same than the one
308 static int get_int32_equal(QEMUFile *f, void *pv, size_t size)
312 qemu_get_sbe32s(f, &v2);
320 const VMStateInfo vmstate_info_int32_equal = {
321 .name = "int32 equal",
322 .get = get_int32_equal,
326 /* 32 bit int. Check that the received value is less than or equal to
327 the one in the field */
329 static int get_int32_le(QEMUFile *f, void *pv, size_t size)
333 qemu_get_sbe32s(f, &loaded);
335 if (loaded <= *cur) {
342 const VMStateInfo vmstate_info_int32_le = {
350 static int get_int64(QEMUFile *f, void *pv, size_t size)
353 qemu_get_sbe64s(f, v);
357 static void put_int64(QEMUFile *f, void *pv, size_t size)
360 qemu_put_sbe64s(f, v);
363 const VMStateInfo vmstate_info_int64 = {
369 /* 8 bit unsigned int */
371 static int get_uint8(QEMUFile *f, void *pv, size_t size)
378 static void put_uint8(QEMUFile *f, void *pv, size_t size)
384 const VMStateInfo vmstate_info_uint8 = {
390 /* 16 bit unsigned int */
392 static int get_uint16(QEMUFile *f, void *pv, size_t size)
395 qemu_get_be16s(f, v);
399 static void put_uint16(QEMUFile *f, void *pv, size_t size)
402 qemu_put_be16s(f, v);
405 const VMStateInfo vmstate_info_uint16 = {
411 /* 32 bit unsigned int */
413 static int get_uint32(QEMUFile *f, void *pv, size_t size)
416 qemu_get_be32s(f, v);
420 static void put_uint32(QEMUFile *f, void *pv, size_t size)
423 qemu_put_be32s(f, v);
426 const VMStateInfo vmstate_info_uint32 = {
432 /* 32 bit uint. See that the received value is the same than the one
435 static int get_uint32_equal(QEMUFile *f, void *pv, size_t size)
439 qemu_get_be32s(f, &v2);
447 const VMStateInfo vmstate_info_uint32_equal = {
448 .name = "uint32 equal",
449 .get = get_uint32_equal,
453 /* 64 bit unsigned int */
455 static int get_uint64(QEMUFile *f, void *pv, size_t size)
458 qemu_get_be64s(f, v);
462 static void put_uint64(QEMUFile *f, void *pv, size_t size)
465 qemu_put_be64s(f, v);
468 const VMStateInfo vmstate_info_uint64 = {
474 /* 64 bit unsigned int. See that the received value is the same than the one
477 static int get_uint64_equal(QEMUFile *f, void *pv, size_t size)
481 qemu_get_be64s(f, &v2);
489 const VMStateInfo vmstate_info_uint64_equal = {
490 .name = "int64 equal",
491 .get = get_uint64_equal,
495 /* 8 bit int. See that the received value is the same than the one
498 static int get_uint8_equal(QEMUFile *f, void *pv, size_t size)
510 const VMStateInfo vmstate_info_uint8_equal = {
511 .name = "uint8 equal",
512 .get = get_uint8_equal,
516 /* 16 bit unsigned int int. See that the received value is the same than the one
519 static int get_uint16_equal(QEMUFile *f, void *pv, size_t size)
523 qemu_get_be16s(f, &v2);
531 const VMStateInfo vmstate_info_uint16_equal = {
532 .name = "uint16 equal",
533 .get = get_uint16_equal,
539 static int get_float64(QEMUFile *f, void *pv, size_t size)
543 *v = make_float64(qemu_get_be64(f));
547 static void put_float64(QEMUFile *f, void *pv, size_t size)
551 qemu_put_be64(f, float64_val(*v));
554 const VMStateInfo vmstate_info_float64 = {
560 /* uint8_t buffers */
562 static int get_buffer(QEMUFile *f, void *pv, size_t size)
565 qemu_get_buffer(f, v, size);
569 static void put_buffer(QEMUFile *f, void *pv, size_t size)
572 qemu_put_buffer(f, v, size);
575 const VMStateInfo vmstate_info_buffer = {
581 /* unused buffers: space that was used for some fields that are
582 not useful anymore */
584 static int get_unused_buffer(QEMUFile *f, void *pv, size_t size)
590 block_len = MIN(sizeof(buf), size);
592 qemu_get_buffer(f, buf, block_len);
597 static void put_unused_buffer(QEMUFile *f, void *pv, size_t size)
599 static const uint8_t buf[1024];
603 block_len = MIN(sizeof(buf), size);
605 qemu_put_buffer(f, buf, block_len);
609 const VMStateInfo vmstate_info_unused_buffer = {
610 .name = "unused_buffer",
611 .get = get_unused_buffer,
612 .put = put_unused_buffer,
615 /* bitmaps (as defined by bitmap.h). Note that size here is the size
616 * of the bitmap in bits. The on-the-wire format of a bitmap is 64
617 * bit words with the bits in big endian order. The in-memory format
618 * is an array of 'unsigned long', which may be either 32 or 64 bits.
620 /* This is the number of 64 bit words sent over the wire */
621 #define BITS_TO_U64S(nr) DIV_ROUND_UP(nr, 64)
622 static int get_bitmap(QEMUFile *f, void *pv, size_t size)
624 unsigned long *bmp = pv;
626 for (i = 0; i < BITS_TO_U64S(size); i++) {
627 uint64_t w = qemu_get_be64(f);
629 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
630 bmp[idx++] = w >> 32;
636 static void put_bitmap(QEMUFile *f, void *pv, size_t size)
638 unsigned long *bmp = pv;
640 for (i = 0; i < BITS_TO_U64S(size); i++) {
641 uint64_t w = bmp[idx++];
642 if (sizeof(unsigned long) == 4 && idx < BITS_TO_LONGS(size)) {
643 w |= ((uint64_t)bmp[idx++]) << 32;
649 const VMStateInfo vmstate_info_bitmap = {